OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/edk/system/simple_broker.h" | |
6 | |
7 #include "base/process/process.h" | |
8 #include "mojo/edk/embedder/platform_channel_pair.h" | |
9 | |
10 #if defined(OS_WIN) | |
11 #include <windows.h> | |
12 #endif | |
13 | |
14 namespace mojo { | |
15 namespace edk { | |
16 | |
17 SimpleBroker::SimpleBroker() { | |
18 } | |
19 | |
20 SimpleBroker::~SimpleBroker() { | |
21 } | |
22 | |
23 #if defined(OS_WIN) | |
24 void SimpleBroker::CreatePlatformChannelPair( | |
25 ScopedPlatformHandle* server, ScopedPlatformHandle* client) { | |
26 PlatformChannelPair channel_pair; | |
27 *server = channel_pair.PassServerHandle(); | |
28 *client = channel_pair.PassClientHandle(); | |
29 } | |
30 | |
31 void SimpleBroker::HandleToToken(const PlatformHandle* platform_handles, | |
32 size_t count, | |
33 uint64_t* tokens) { | |
34 // Since we're not sure which process might ultimately deserialize the message | |
35 // we can't duplicate the handle now. Instead, write the process ID and handle | |
36 // now and let the receiver duplicate it. | |
37 uint32_t current_process_id = base::GetCurrentProcId(); | |
38 for (size_t i = 0; i < count; ++i) { | |
39 tokens[i] = current_process_id; | |
40 tokens[i] = tokens[i] << 32; | |
41 // Windows HANDLES are always 32 bit per | |
42 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa384203(v=vs.85
).aspx | |
43 #if defined(_WIN64) | |
44 tokens[i] |= static_cast<int32_t>( | |
45 reinterpret_cast<int64_t>(platform_handles[i].handle)); | |
46 #else | |
47 tokens[i] |= reinterpret_cast<int32_t>(platform_handles[i].handle); | |
48 #endif | |
49 } | |
50 } | |
51 | |
52 void SimpleBroker::TokenToHandle(const uint64_t* tokens, | |
53 size_t count, | |
54 PlatformHandle* handles) { | |
55 for (size_t i = 0; i < count; ++i) { | |
56 DWORD pid = tokens[i] >> 32; | |
57 #if defined(_WIN64) | |
58 // Sign-extend to preserve INVALID_HANDLE_VALUE. | |
59 HANDLE source_handle = reinterpret_cast<HANDLE>( | |
60 static_cast<int64_t>(static_cast<int32_t>(tokens[i] & 0xFFFFFFFF))); | |
61 #else | |
62 HANDLE source_handle = reinterpret_cast<HANDLE>(tokens[i] & 0xFFFFFFFF); | |
63 #endif | |
64 base::Process sender = | |
65 base::Process::OpenWithAccess(pid, PROCESS_DUP_HANDLE); | |
66 handles[i] = PlatformHandle(); | |
67 if (!sender.IsValid()) { | |
68 // Sender died. | |
69 } else { | |
70 BOOL dup_result = DuplicateHandle( | |
71 sender.Handle(), source_handle, | |
72 base::GetCurrentProcessHandle(), &handles[i].handle, 0, | |
73 FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE); | |
74 DCHECK(dup_result); | |
75 } | |
76 } | |
77 } | |
78 #endif | |
79 | |
80 } // namespace edk | |
81 } // namespace mojo | |
OLD | NEW |