Bug 1642744 [wpt PR 23920] - [ScrollTimeline] Update compositor timeline from blink...
[gecko.git] / ipc / glue / Transport_win.h
blob9250be3dd15d81ee02f8a41f419644a690f3d542
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_ipc_Transport_win_h
8 #define mozilla_ipc_Transport_win_h 1
10 #include <string>
12 #include "base/process.h"
13 #include "ipc/IPCMessageUtils.h"
14 #include "nsWindowsHelpers.h"
16 namespace mozilla {
17 namespace ipc {
19 struct TransportDescriptor {
20 std::wstring mPipeName;
21 HANDLE mServerPipeHandle;
22 base::ProcessId mDestinationProcessId;
25 HANDLE
26 TransferHandleToProcess(HANDLE source, base::ProcessId pid);
28 } // namespace ipc
29 } // namespace mozilla
31 namespace IPC {
33 template <>
34 struct ParamTraits<mozilla::ipc::TransportDescriptor> {
35 typedef mozilla::ipc::TransportDescriptor paramType;
36 static void Write(Message* aMsg, const paramType& aParam) {
37 HANDLE pipe = mozilla::ipc::TransferHandleToProcess(
38 aParam.mServerPipeHandle, aParam.mDestinationProcessId);
39 DWORD duplicateFromProcessId = 0;
40 if (!pipe) {
41 if (XRE_IsParentProcess()) {
42 // If we are the parent and failed to transfer then there is no hope,
43 // just close the handle.
44 ::CloseHandle(aParam.mServerPipeHandle);
45 } else {
46 // We are probably sending to parent so it should be able to duplicate.
47 pipe = aParam.mServerPipeHandle;
48 duplicateFromProcessId = ::GetCurrentProcessId();
52 WriteParam(aMsg, aParam.mPipeName);
53 WriteParam(aMsg, pipe);
54 WriteParam(aMsg, duplicateFromProcessId);
55 WriteParam(aMsg, aParam.mDestinationProcessId);
57 static bool Read(const Message* aMsg, PickleIterator* aIter,
58 paramType* aResult) {
59 DWORD duplicateFromProcessId;
60 bool r = (ReadParam(aMsg, aIter, &aResult->mPipeName) &&
61 ReadParam(aMsg, aIter, &aResult->mServerPipeHandle) &&
62 ReadParam(aMsg, aIter, &duplicateFromProcessId) &&
63 ReadParam(aMsg, aIter, &aResult->mDestinationProcessId));
64 if (!r) {
65 return r;
68 MOZ_RELEASE_ASSERT(
69 aResult->mServerPipeHandle,
70 "Main process failed to duplicate pipe handle to child.");
72 // If this is a not the "server" side descriptor, we have finished.
73 if (aResult->mServerPipeHandle == INVALID_HANDLE_VALUE) {
74 return true;
77 MOZ_RELEASE_ASSERT(aResult->mDestinationProcessId ==
78 base::GetCurrentProcId());
80 // If the pipe has already been duplicated to us, we have finished.
81 if (!duplicateFromProcessId) {
82 return true;
85 // Otherwise duplicate the handle to us.
86 nsAutoHandle sourceProcess(
87 ::OpenProcess(PROCESS_DUP_HANDLE, FALSE, duplicateFromProcessId));
88 if (!sourceProcess) {
89 return false;
92 HANDLE ourHandle;
93 BOOL duped = ::DuplicateHandle(
94 sourceProcess, aResult->mServerPipeHandle, ::GetCurrentProcess(),
95 &ourHandle, 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
96 if (!duped) {
97 aResult->mServerPipeHandle = INVALID_HANDLE_VALUE;
98 return false;
101 aResult->mServerPipeHandle = ourHandle;
102 return true;
106 } // namespace IPC
108 #endif // mozilla_ipc_Transport_win_h