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/child_token_serializer_win.h" | 5 #include "mojo/edk/system/child_broker.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "mojo/edk/embedder/embedder_internal.h" | 8 #include "mojo/edk/embedder/embedder_internal.h" |
9 #include "mojo/edk/system/token_serializer_messages_win.h" | 9 #include "mojo/edk/system/broker_messages.h" |
10 | 10 |
11 namespace mojo { | 11 namespace mojo { |
12 namespace edk { | 12 namespace edk { |
13 | 13 |
14 ChildTokenSerializer* ChildTokenSerializer::GetInstance() { | 14 ChildBroker* ChildBroker::GetInstance() { |
15 return base::Singleton< | 15 return base::Singleton< |
16 ChildTokenSerializer, | 16 ChildBroker, base::LeakySingletonTraits<ChildBroker>>::get(); |
17 base::LeakySingletonTraits<ChildTokenSerializer>>::get(); | |
18 } | 17 } |
19 | 18 |
20 void ChildTokenSerializer::SetParentTokenSerializerHandle( | 19 void ChildBroker::SetChildBrokerHostHandle(ScopedPlatformHandle handle) { |
21 ScopedPlatformHandle handle) { | |
22 handle_ = handle.Pass(); | 20 handle_ = handle.Pass(); |
23 lock_.Unlock(); | 21 lock_.Unlock(); |
24 } | 22 } |
25 | 23 |
26 void ChildTokenSerializer::CreatePlatformChannelPair( | 24 #if defined(OS_WIN) |
| 25 void ChildBroker::CreatePlatformChannelPair( |
27 ScopedPlatformHandle* server, ScopedPlatformHandle* client) { | 26 ScopedPlatformHandle* server, ScopedPlatformHandle* client) { |
28 TokenSerializerMessage message; | 27 BrokerMessage message; |
29 message.size = kTokenSerializerMessageHeaderSize; | 28 message.size = kBrokerMessageHeaderSize; |
30 message.id = CREATE_PLATFORM_CHANNEL_PAIR; | 29 message.id = CREATE_PLATFORM_CHANNEL_PAIR; |
31 | 30 |
32 uint32_t response_size = 2 * sizeof(HANDLE); | 31 uint32_t response_size = 2 * sizeof(HANDLE); |
33 HANDLE handles[2]; | 32 HANDLE handles[2]; |
34 if (WriteAndReadResponse(&message, handles, response_size)) { | 33 if (WriteAndReadResponse(&message, handles, response_size)) { |
35 server->reset(PlatformHandle(handles[0])); | 34 server->reset(PlatformHandle(handles[0])); |
36 client->reset(PlatformHandle(handles[1])); | 35 client->reset(PlatformHandle(handles[1])); |
37 } | 36 } |
38 } | 37 } |
39 | 38 |
40 void ChildTokenSerializer::HandleToToken(const PlatformHandle* platform_handles, | 39 void ChildBroker::HandleToToken(const PlatformHandle* platform_handles, |
41 size_t count, | 40 size_t count, |
42 uint64_t* tokens) { | 41 uint64_t* tokens) { |
43 uint32_t size = kTokenSerializerMessageHeaderSize + | 42 uint32_t size = kBrokerMessageHeaderSize + |
44 static_cast<int>(count) * sizeof(HANDLE); | 43 static_cast<int>(count) * sizeof(HANDLE); |
45 std::vector<char> message_buffer(size); | 44 std::vector<char> message_buffer(size); |
46 TokenSerializerMessage* message = | 45 BrokerMessage* message = reinterpret_cast<BrokerMessage*>(&message_buffer[0]); |
47 reinterpret_cast<TokenSerializerMessage*>(&message_buffer[0]); | |
48 message->size = size; | 46 message->size = size; |
49 message->id = HANDLE_TO_TOKEN; | 47 message->id = HANDLE_TO_TOKEN; |
50 for (size_t i = 0; i < count; ++i) | 48 for (size_t i = 0; i < count; ++i) |
51 message->handles[i] = platform_handles[i].handle; | 49 message->handles[i] = platform_handles[i].handle; |
52 | 50 |
53 uint32_t response_size = static_cast<int>(count) * sizeof(uint64_t); | 51 uint32_t response_size = static_cast<int>(count) * sizeof(uint64_t); |
54 WriteAndReadResponse(message, tokens, response_size); | 52 WriteAndReadResponse(message, tokens, response_size); |
55 } | 53 } |
56 | 54 |
57 void ChildTokenSerializer::TokenToHandle(const uint64_t* tokens, | 55 void ChildBroker::TokenToHandle(const uint64_t* tokens, |
58 size_t count, | 56 size_t count, |
59 PlatformHandle* handles) { | 57 PlatformHandle* handles) { |
60 uint32_t size = kTokenSerializerMessageHeaderSize + | 58 uint32_t size = kBrokerMessageHeaderSize + |
61 static_cast<int>(count) * sizeof(uint64_t); | 59 static_cast<int>(count) * sizeof(uint64_t); |
62 std::vector<char> message_buffer(size); | 60 std::vector<char> message_buffer(size); |
63 TokenSerializerMessage* message = | 61 BrokerMessage* message = |
64 reinterpret_cast<TokenSerializerMessage*>(&message_buffer[0]); | 62 reinterpret_cast<BrokerMessage*>(&message_buffer[0]); |
65 message->size = size; | 63 message->size = size; |
66 message->id = TOKEN_TO_HANDLE; | 64 message->id = TOKEN_TO_HANDLE; |
67 memcpy(&message->tokens[0], tokens, count * sizeof(uint64_t)); | 65 memcpy(&message->tokens[0], tokens, count * sizeof(uint64_t)); |
68 | 66 |
69 std::vector<HANDLE> handles_temp(count); | 67 std::vector<HANDLE> handles_temp(count); |
70 uint32_t response_size = | 68 uint32_t response_size = |
71 static_cast<uint32_t>(handles_temp.size()) * sizeof(HANDLE); | 69 static_cast<uint32_t>(handles_temp.size()) * sizeof(HANDLE); |
72 if (WriteAndReadResponse(message, &handles_temp[0], response_size)) { | 70 if (WriteAndReadResponse(message, &handles_temp[0], response_size)) { |
73 for (uint32_t i = 0; i < count; ++i) | 71 for (uint32_t i = 0; i < count; ++i) |
74 handles[i].handle = handles_temp[i]; | 72 handles[i].handle = handles_temp[i]; |
75 } | 73 } |
76 } | 74 } |
| 75 #endif |
77 | 76 |
78 ChildTokenSerializer::ChildTokenSerializer() { | 77 ChildBroker::ChildBroker() { |
79 DCHECK(!internal::g_token_serializer); | 78 DCHECK(!internal::g_broker); |
80 internal::g_token_serializer = this; | 79 internal::g_broker = this; |
81 // Block any threads from calling this until we have a pipe to the parent. | 80 // Block any threads from calling this until we have a pipe to the parent. |
82 lock_.Lock(); | 81 lock_.Lock(); |
83 } | 82 } |
84 | 83 |
85 ChildTokenSerializer::~ChildTokenSerializer() { | 84 ChildBroker::~ChildBroker() { |
86 } | 85 } |
87 | 86 |
88 bool ChildTokenSerializer::WriteAndReadResponse(TokenSerializerMessage* message, | 87 bool ChildBroker::WriteAndReadResponse(BrokerMessage* message, |
89 void* response, | 88 void* response, |
90 uint32_t response_size) { | 89 uint32_t response_size) { |
91 lock_.Lock(); | 90 lock_.Lock(); |
92 CHECK(handle_.is_valid()); | 91 CHECK(handle_.is_valid()); |
93 | 92 |
94 bool result = true; | 93 bool result = true; |
| 94 #if defined(OS_WIN) |
95 DWORD bytes_written = 0; | 95 DWORD bytes_written = 0; |
96 // This will always write in one chunk per | 96 // This will always write in one chunk per |
97 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365150.aspx. | 97 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365150.aspx. |
98 BOOL rv = WriteFile(handle_.get().handle, message, message->size, | 98 BOOL rv = WriteFile(handle_.get().handle, message, message->size, |
99 &bytes_written, NULL); | 99 &bytes_written, NULL); |
100 if (!rv || bytes_written != message->size) { | 100 if (!rv || bytes_written != message->size) { |
101 LOG(ERROR) << "Child token serializer couldn't write message."; | 101 LOG(ERROR) << "Child token serializer couldn't write message."; |
102 result = false; | 102 result = false; |
103 } else { | 103 } else { |
104 while (response_size) { | 104 while (response_size) { |
105 DWORD bytes_read = 0; | 105 DWORD bytes_read = 0; |
106 rv = ReadFile(handle_.get().handle, response, response_size, &bytes_read, | 106 rv = ReadFile(handle_.get().handle, response, response_size, &bytes_read, |
107 NULL); | 107 NULL); |
108 if (!rv) { | 108 if (!rv) { |
109 LOG(ERROR) << "Child token serializer couldn't read result."; | 109 LOG(ERROR) << "Child token serializer couldn't read result."; |
110 result = false; | 110 result = false; |
111 break; | 111 break; |
112 } | 112 } |
113 response_size -= bytes_read; | 113 response_size -= bytes_read; |
114 response = static_cast<char*>(response) + bytes_read; | 114 response = static_cast<char*>(response) + bytes_read; |
115 } | 115 } |
116 } | 116 } |
| 117 #endif |
117 | 118 |
118 lock_.Unlock(); | 119 lock_.Unlock(); |
119 | 120 |
120 return result; | 121 return result; |
121 } | 122 } |
122 | 123 |
123 } // namespace edk | 124 } // namespace edk |
124 } // namespace mojo | 125 } // namespace mojo |
OLD | NEW |