DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (5350524bb654)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: sw=2 ts=8 et :
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "GonkNativeHandleUtils.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Unused.h"

using namespace mozilla::layers;

namespace IPC {

namespace {

class native_handle_Delete
{
public:
  void operator()(native_handle* aNativeHandle) const
  {
    native_handle_close(aNativeHandle); // closes file descriptors
    native_handle_delete(aNativeHandle);
  }
};

} // anonymous namespace

void
ParamTraits<GonkNativeHandle>::Write(Message* aMsg,
                                const paramType& aParam)
{
  GonkNativeHandle handle = aParam;
  MOZ_ASSERT(handle.IsValid());

  RefPtr<GonkNativeHandle::NhObj> nhObj = handle.GetAndResetNhObj();
  native_handle_t* nativeHandle = nhObj->GetAndResetNativeHandle();

  size_t nbytes = nativeHandle->numInts * sizeof(int);
  aMsg->WriteSize(nbytes);
  aMsg->WriteBytes((nativeHandle->data + nativeHandle->numFds), nbytes);

  for (size_t i = 0; i < static_cast<size_t>(nativeHandle->numFds); ++i) {
    aMsg->WriteFileDescriptor(base::FileDescriptor(nativeHandle->data[i], true));
  }
}

bool
ParamTraits<GonkNativeHandle>::Read(const Message* aMsg,
                               PickleIterator* aIter, paramType* aResult)
{
  size_t nbytes;
  if (!aMsg->ReadSize(aIter, &nbytes)) {
    return false;
  }

  if (nbytes % sizeof(int) != 0) {
    return false;
  }

  size_t numInts = nbytes / sizeof(int);
  size_t numFds = aMsg->num_fds();
  mozilla::UniquePtr<native_handle, native_handle_Delete> nativeHandle(
    native_handle_create(numFds, numInts));
  if (!nativeHandle) {
    return false;
  }

  auto data =
    reinterpret_cast<char*>(nativeHandle->data + nativeHandle->numFds);
  if (!aMsg->ReadBytesInto(aIter, data, nbytes)) {
    return false;
  }

  for (size_t i = 0; i < numFds; ++i) {
    base::FileDescriptor fd;
    if (!aMsg->ReadFileDescriptor(aIter, &fd)) {
      return false;
    }
    nativeHandle->data[i] = fd.fd;
    nativeHandle->numFds = i + 1; // set number of valid file descriptors
  }

  GonkNativeHandle handle(new GonkNativeHandle::NhObj(nativeHandle.get()));
  handle.TransferToAnother(*aResult);

  mozilla::Unused << nativeHandle.release();

  return true;
}

} // namespace IPC