2016-01-06 07:50:39 +03:00
|
|
|
/* -*- 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"
|
2016-06-10 10:23:21 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2016-01-06 07:50:39 +03:00
|
|
|
|
|
|
|
using namespace mozilla::layers;
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2016-06-10 10:23:21 +03:00
|
|
|
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
|
|
|
|
|
2016-01-06 07:50:39 +03:00
|
|
|
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();
|
|
|
|
|
2016-01-27 11:32:03 +03:00
|
|
|
size_t nbytes = nativeHandle->numInts * sizeof(int);
|
|
|
|
aMsg->WriteSize(nbytes);
|
|
|
|
aMsg->WriteBytes((nativeHandle->data + nativeHandle->numFds), nbytes);
|
2016-01-06 07:50:39 +03:00
|
|
|
|
|
|
|
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,
|
2016-04-21 07:09:15 +03:00
|
|
|
PickleIterator* aIter, paramType* aResult)
|
2016-01-06 07:50:39 +03:00
|
|
|
{
|
2016-01-27 11:32:03 +03:00
|
|
|
size_t nbytes;
|
2016-06-10 10:23:21 +03:00
|
|
|
if (!aMsg->ReadSize(aIter, &nbytes)) {
|
2016-01-06 07:50:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-27 11:32:03 +03:00
|
|
|
|
|
|
|
if (nbytes % sizeof(int) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t numInts = nbytes / sizeof(int);
|
2016-01-06 07:50:39 +03:00
|
|
|
size_t numFds = aMsg->num_fds();
|
2016-06-10 10:23:21 +03:00
|
|
|
mozilla::UniquePtr<native_handle, native_handle_Delete> nativeHandle(
|
|
|
|
native_handle_create(numFds, numInts));
|
2016-01-27 11:32:03 +03:00
|
|
|
if (!nativeHandle) {
|
2016-01-06 07:50:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-10 10:23:21 +03:00
|
|
|
auto data =
|
|
|
|
reinterpret_cast<char*>(nativeHandle->data + nativeHandle->numFds);
|
|
|
|
if (!aMsg->ReadBytesInto(aIter, data, nbytes)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-27 11:32:03 +03:00
|
|
|
|
2016-06-10 10:23:21 +03:00
|
|
|
for (size_t i = 0; i < numFds; ++i) {
|
2016-01-06 07:50:39 +03:00
|
|
|
base::FileDescriptor fd;
|
|
|
|
if (!aMsg->ReadFileDescriptor(aIter, &fd)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nativeHandle->data[i] = fd.fd;
|
2016-06-10 10:23:21 +03:00
|
|
|
nativeHandle->numFds = i + 1; // set number of valid file descriptors
|
2016-01-06 07:50:39 +03:00
|
|
|
}
|
|
|
|
|
2016-06-10 10:23:21 +03:00
|
|
|
GonkNativeHandle handle(new GonkNativeHandle::NhObj(nativeHandle.get()));
|
2016-02-22 03:44:50 +03:00
|
|
|
handle.TransferToAnother(*aResult);
|
|
|
|
|
2016-06-10 10:23:21 +03:00
|
|
|
mozilla::Unused << nativeHandle.release();
|
|
|
|
|
2016-01-06 07:50:39 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace IPC
|