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
12 #include "base/process.h"
13 #include "ipc/IPCMessageUtils.h"
14 #include "nsWindowsHelpers.h"
19 struct TransportDescriptor
{
20 std::wstring mPipeName
;
21 HANDLE mServerPipeHandle
;
22 base::ProcessId mDestinationProcessId
;
26 TransferHandleToProcess(HANDLE source
, base::ProcessId pid
);
29 } // namespace mozilla
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;
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
);
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
,
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
));
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
) {
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
) {
85 // Otherwise duplicate the handle to us.
86 nsAutoHandle
sourceProcess(
87 ::OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, duplicateFromProcessId
));
93 BOOL duped
= ::DuplicateHandle(
94 sourceProcess
, aResult
->mServerPipeHandle
, ::GetCurrentProcess(),
95 &ourHandle
, 0, FALSE
, DUPLICATE_CLOSE_SOURCE
| DUPLICATE_SAME_ACCESS
);
97 aResult
->mServerPipeHandle
= INVALID_HANDLE_VALUE
;
101 aResult
->mServerPipeHandle
= ourHandle
;
108 #endif // mozilla_ipc_Transport_win_h