|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * vim: sw=2 ts=8 et : |
|
3 */ |
|
4 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 #include "base/message_loop.h" |
|
9 #include "chrome/common/child_process_info.h" |
|
10 |
|
11 #include "mozilla/ipc/Transport.h" |
|
12 |
|
13 using namespace base; |
|
14 using namespace std; |
|
15 |
|
16 namespace mozilla { |
|
17 namespace ipc { |
|
18 |
|
19 bool |
|
20 CreateTransport(ProcessHandle aProcOne, ProcessHandle /*unused*/, |
|
21 TransportDescriptor* aOne, TransportDescriptor* aTwo) |
|
22 { |
|
23 // This id is used to name the IPC pipe. The pointer passed to this |
|
24 // function isn't significant. |
|
25 wstring id = ChildProcessInfo::GenerateRandomChannelID(aOne); |
|
26 // Use MODE_SERVER to force creation of the pipe |
|
27 Transport t(id, Transport::MODE_SERVER, nullptr); |
|
28 HANDLE serverPipe = t.GetServerPipeHandle(); |
|
29 if (!serverPipe) { |
|
30 return false; |
|
31 } |
|
32 |
|
33 // NB: we create the server pipe immediately, instead of just |
|
34 // grabbing an ID, on purpose. In the current setup, the client |
|
35 // needs to connect to an existing server pipe, so to prevent race |
|
36 // conditions, we create the server side here and then dup it to the |
|
37 // eventual server process. |
|
38 HANDLE serverDup; |
|
39 DWORD access = 0; |
|
40 DWORD options = DUPLICATE_SAME_ACCESS; |
|
41 if (!DuplicateHandle(GetCurrentProcess(), serverPipe, aProcOne, |
|
42 &serverDup, |
|
43 access, |
|
44 FALSE/*not inheritable*/, |
|
45 options)) { |
|
46 return false; |
|
47 } |
|
48 |
|
49 aOne->mPipeName = aTwo->mPipeName = id; |
|
50 aOne->mServerPipe = serverDup; |
|
51 aTwo->mServerPipe = INVALID_HANDLE_VALUE; |
|
52 return true; |
|
53 } |
|
54 |
|
55 Transport* |
|
56 OpenDescriptor(const TransportDescriptor& aTd, Transport::Mode aMode) |
|
57 { |
|
58 return new Transport(aTd.mPipeName, aTd.mServerPipe, aMode, nullptr); |
|
59 } |
|
60 |
|
61 Transport* |
|
62 OpenDescriptor(const FileDescriptor& aFd, Transport::Mode aMode) |
|
63 { |
|
64 NS_NOTREACHED("Not implemented!"); |
|
65 return nullptr; |
|
66 } |
|
67 |
|
68 void |
|
69 CloseDescriptor(const TransportDescriptor& aTd) |
|
70 { |
|
71 CloseHandle(aTd.mServerPipe); |
|
72 } |
|
73 |
|
74 } // namespace ipc |
|
75 } // namespace mozilla |