summaryrefslogtreecommitdiffstats
path: root/gfx/layers/ipc/GonkNativeHandleUtils.cpp
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /gfx/layers/ipc/GonkNativeHandleUtils.cpp
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'gfx/layers/ipc/GonkNativeHandleUtils.cpp')
-rw-r--r--gfx/layers/ipc/GonkNativeHandleUtils.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/gfx/layers/ipc/GonkNativeHandleUtils.cpp b/gfx/layers/ipc/GonkNativeHandleUtils.cpp
new file mode 100644
index 000000000..fb2efecb3
--- /dev/null
+++ b/gfx/layers/ipc/GonkNativeHandleUtils.cpp
@@ -0,0 +1,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