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