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