Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: mojo/edk/system/child_broker_host.cc

Issue 1465183005: Rename mojo::TokenSerializer to mojo::Broker. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix win component Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/parent_token_serializer_win.h" 5 #include "mojo/edk/system/child_broker_host.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "mojo/edk/embedder/platform_channel_pair.h" 9 #include "mojo/edk/embedder/platform_channel_pair.h"
10 #include "mojo/edk/system/broker_messages.h"
11 #include "mojo/edk/system/broker_state.h"
10 #include "mojo/edk/system/configuration.h" 12 #include "mojo/edk/system/configuration.h"
11 #include "mojo/edk/system/parent_token_serializer_state_win.h"
12 #include "mojo/edk/system/token_serializer_messages_win.h"
13 13
14 namespace mojo { 14 namespace mojo {
15 namespace edk { 15 namespace edk {
16 16
17 namespace { 17 namespace {
18 static const int kDefaultReadBufferSize = 256; 18 static const int kDefaultReadBufferSize = 256;
19 } 19 }
20 20
21 ParentTokenSerializer::ParentTokenSerializer(HANDLE child_process, 21 ChildBrokerHost::ChildBrokerHost(base::ProcessHandle child_process,
22 ScopedPlatformHandle pipe) 22 ScopedPlatformHandle pipe)
23 : child_process_(child_process), 23 : child_process_(child_process),
24 pipe_(pipe.Pass()), 24 pipe_(pipe.Pass()),
25 num_bytes_read_(0) { 25 num_bytes_read_(0) {
26 #if defined(OS_WIN)
26 memset(&read_context_.overlapped, 0, sizeof(read_context_.overlapped)); 27 memset(&read_context_.overlapped, 0, sizeof(read_context_.overlapped));
27 read_context_.handler = this; 28 read_context_.handler = this;
28 memset(&write_context_.overlapped, 0, sizeof(write_context_.overlapped)); 29 memset(&write_context_.overlapped, 0, sizeof(write_context_.overlapped));
29 write_context_.handler = this; 30 write_context_.handler = this;
31 #else
32 // TODO(jam)
33 (void)child_process_; // Suppress -Wunused-private-field.
34 (void)num_bytes_read_; // Suppress -Wunused-private-field.
35 #endif
30 36
31 read_data_.resize(kDefaultReadBufferSize); 37 read_data_.resize(kDefaultReadBufferSize);
32 ParentTokenSerializerState::GetInstance()->token_serialize_thread()->PostTask( 38 BrokerState::GetInstance()->broker_thread()->PostTask(
33 FROM_HERE, 39 FROM_HERE,
34 base::Bind(&ParentTokenSerializer::RegisterIOHandler, 40 base::Bind(&ChildBrokerHost::RegisterIOHandler, base::Unretained(this)));
35 base::Unretained(this)));
36 } 41 }
37 42
38 ParentTokenSerializer::~ParentTokenSerializer() { 43 ChildBrokerHost::~ChildBrokerHost() {
39 } 44 }
40 45
41 void ParentTokenSerializer::RegisterIOHandler() { 46 void ChildBrokerHost::RegisterIOHandler() {
47 #if defined(OS_WIN)
42 base::MessageLoopForIO::current()->RegisterIOHandler( 48 base::MessageLoopForIO::current()->RegisterIOHandler(
43 pipe_.get().handle, this); 49 pipe_.get().handle, this);
44 BeginRead(); 50 BeginRead();
51 #elif defined(OS_POSIX)
52 // TOOD(jam): setup
53 #endif
45 } 54 }
46 55
47 void ParentTokenSerializer::BeginRead() { 56 void ChildBrokerHost::BeginRead() {
57 #if defined(OS_WIN)
48 BOOL rv = ReadFile(pipe_.get().handle, &read_data_[num_bytes_read_], 58 BOOL rv = ReadFile(pipe_.get().handle, &read_data_[num_bytes_read_],
49 static_cast<int>(read_data_.size() - num_bytes_read_), 59 static_cast<int>(read_data_.size() - num_bytes_read_),
50 nullptr, &read_context_.overlapped); 60 nullptr, &read_context_.overlapped);
51 if (rv || GetLastError() == ERROR_IO_PENDING) 61 if (rv || GetLastError() == ERROR_IO_PENDING)
52 return; 62 return;
53 63
54 if (rv == ERROR_BROKEN_PIPE) { 64 if (rv == ERROR_BROKEN_PIPE) {
55 delete this; 65 delete this;
56 return; 66 return;
57 } 67 }
58 68
59 NOTREACHED() << "Unknown error in ParentTokenSerializer " << rv; 69 NOTREACHED() << "Unknown error in ChildBrokerHost " << rv;
70 #endif
60 } 71 }
61 72
62 void ParentTokenSerializer::OnIOCompleted( 73 #if defined(OS_WIN)
63 base::MessageLoopForIO::IOContext* context, 74 void ChildBrokerHost::OnIOCompleted(base::MessageLoopForIO::IOContext* context,
64 DWORD bytes_transferred, 75 DWORD bytes_transferred,
65 DWORD error) { 76 DWORD error) {
66 if (context != &read_context_) 77 if (context != &read_context_)
67 return; 78 return;
68 79
69 if (error == ERROR_BROKEN_PIPE) { 80 if (error == ERROR_BROKEN_PIPE) {
70 delete this; 81 delete this;
71 return; // Child process exited or crashed. 82 return; // Child process exited or crashed.
72 } 83 }
73 84
74 if (error != ERROR_SUCCESS) { 85 if (error != ERROR_SUCCESS) {
75 NOTREACHED() << "Error " << error << " in ParentTokenSerializer."; 86 NOTREACHED() << "Error " << error << " in ChildBrokerHost.";
76 delete this; 87 delete this;
77 return; 88 return;
78 } 89 }
79 90
80 num_bytes_read_ += bytes_transferred; 91 num_bytes_read_ += bytes_transferred;
81 CHECK_GE(num_bytes_read_, sizeof(uint32_t)); 92 CHECK_GE(num_bytes_read_, sizeof(uint32_t));
82 TokenSerializerMessage* message = 93 BrokerMessage* message = reinterpret_cast<BrokerMessage*>(&read_data_[0]);
83 reinterpret_cast<TokenSerializerMessage*>(&read_data_[0]);
84 if (num_bytes_read_ < message->size) { 94 if (num_bytes_read_ < message->size) {
85 read_data_.resize(message->size); 95 read_data_.resize(message->size);
86 BeginRead(); 96 BeginRead();
87 return; 97 return;
88 } 98 }
89 99
90 if (message->id == CREATE_PLATFORM_CHANNEL_PAIR) { 100 if (message->id == CREATE_PLATFORM_CHANNEL_PAIR) {
91 PlatformChannelPair channel_pair; 101 PlatformChannelPair channel_pair;
92 uint32_t response_size = 2 * sizeof(HANDLE); 102 uint32_t response_size = 2 * sizeof(HANDLE);
93 write_data_.resize(response_size); 103 write_data_.resize(response_size);
94 HANDLE* handles = reinterpret_cast<HANDLE*>(&write_data_[0]); 104 HANDLE* handles = reinterpret_cast<HANDLE*>(&write_data_[0]);
95 handles[0] = DuplicateToChild( 105 handles[0] = DuplicateToChild(
96 channel_pair.PassServerHandle().release().handle); 106 channel_pair.PassServerHandle().release().handle);
97 handles[1] = DuplicateToChild( 107 handles[1] = DuplicateToChild(
98 channel_pair.PassClientHandle().release().handle); 108 channel_pair.PassClientHandle().release().handle);
99 } else if (message->id == HANDLE_TO_TOKEN) { 109 } else if (message->id == HANDLE_TO_TOKEN) {
100 uint32_t count = 110 uint32_t count =
101 (message->size - kTokenSerializerMessageHeaderSize) / sizeof(HANDLE); 111 (message->size - kBrokerMessageHeaderSize) / sizeof(HANDLE);
102 if (count > GetConfiguration().max_message_num_handles) { 112 if (count > GetConfiguration().max_message_num_handles) {
103 NOTREACHED() << "Too many handles from child process. Closing channel."; 113 NOTREACHED() << "Too many handles from child process. Closing channel.";
104 delete this; 114 delete this;
105 return; 115 return;
106 } 116 }
107 uint32_t response_size = count * sizeof(uint64_t); 117 uint32_t response_size = count * sizeof(uint64_t);
108 write_data_.resize(response_size); 118 write_data_.resize(response_size);
109 uint64_t* tokens = reinterpret_cast<uint64_t*>(&write_data_[0]); 119 uint64_t* tokens = reinterpret_cast<uint64_t*>(&write_data_[0]);
110 std::vector<PlatformHandle> duplicated_handles(count); 120 std::vector<PlatformHandle> duplicated_handles(count);
111 for (uint32_t i = 0; i < count; ++i) { 121 for (uint32_t i = 0; i < count; ++i) {
112 duplicated_handles[i] = 122 duplicated_handles[i] =
113 PlatformHandle(DuplicateFromChild(message->handles[i])); 123 PlatformHandle(DuplicateFromChild(message->handles[i]));
114 } 124 }
115 ParentTokenSerializerState::GetInstance()->HandleToToken( 125 BrokerState::GetInstance()->HandleToToken(
116 &duplicated_handles[0], count, tokens); 126 &duplicated_handles[0], count, tokens);
117 } else if (message->id == TOKEN_TO_HANDLE) { 127 } else if (message->id == TOKEN_TO_HANDLE) {
118 uint32_t count = 128 uint32_t count =
119 (message->size - kTokenSerializerMessageHeaderSize) / 129 (message->size - kBrokerMessageHeaderSize) / sizeof(uint64_t);
120 sizeof(uint64_t);
121 if (count > GetConfiguration().max_message_num_handles) { 130 if (count > GetConfiguration().max_message_num_handles) {
122 NOTREACHED() << "Too many tokens from child process. Closing channel."; 131 NOTREACHED() << "Too many tokens from child process. Closing channel.";
123 delete this; 132 delete this;
124 return; 133 return;
125 } 134 }
126 uint32_t response_size = count * sizeof(HANDLE); 135 uint32_t response_size = count * sizeof(HANDLE);
127 write_data_.resize(response_size); 136 write_data_.resize(response_size);
128 HANDLE* handles = reinterpret_cast<HANDLE*>(&write_data_[0]); 137 HANDLE* handles = reinterpret_cast<HANDLE*>(&write_data_[0]);
129 std::vector<PlatformHandle> temp_handles(count); 138 std::vector<PlatformHandle> temp_handles(count);
130 ParentTokenSerializerState::GetInstance()->TokenToHandle( 139 BrokerState::GetInstance()->TokenToHandle(
131 &message->tokens[0], count, &temp_handles[0]); 140 &message->tokens[0], count, &temp_handles[0]);
132 for (uint32_t i = 0; i < count; ++i) { 141 for (uint32_t i = 0; i < count; ++i) {
133 if (temp_handles[i].is_valid()) { 142 if (temp_handles[i].is_valid()) {
134 handles[i] = DuplicateToChild(temp_handles[i].handle); 143 handles[i] = DuplicateToChild(temp_handles[i].handle);
135 } else { 144 } else {
136 NOTREACHED() << "Unknown token"; 145 NOTREACHED() << "Unknown token";
137 handles[i] = INVALID_HANDLE_VALUE; 146 handles[i] = INVALID_HANDLE_VALUE;
138 } 147 }
139 } 148 }
140 } else { 149 } else {
141 NOTREACHED() << "Unknown command. Stopping reading."; 150 NOTREACHED() << "Unknown command. Stopping reading.";
142 delete this; 151 delete this;
143 return; 152 return;
144 } 153 }
145 154
146 BOOL rv = WriteFile(pipe_.get().handle, &write_data_[0], 155 BOOL rv = WriteFile(pipe_.get().handle, &write_data_[0],
147 static_cast<int>(write_data_.size()), NULL, 156 static_cast<int>(write_data_.size()), NULL,
148 &write_context_.overlapped); 157 &write_context_.overlapped);
149 DCHECK(rv || GetLastError() == ERROR_IO_PENDING); 158 DCHECK(rv || GetLastError() == ERROR_IO_PENDING);
150 159
151 // Start reading again. 160 // Start reading again.
152 num_bytes_read_ = 0; 161 num_bytes_read_ = 0;
153 BeginRead(); 162 BeginRead();
154 } 163 }
155 164
156 165 HANDLE ChildBrokerHost::DuplicateToChild(HANDLE handle) {
157 HANDLE ParentTokenSerializer::DuplicateToChild(HANDLE handle) {
158 HANDLE rv = INVALID_HANDLE_VALUE; 166 HANDLE rv = INVALID_HANDLE_VALUE;
159 BOOL result = DuplicateHandle(base::GetCurrentProcessHandle(), handle, 167 BOOL result = DuplicateHandle(base::GetCurrentProcessHandle(), handle,
160 child_process_, &rv, 0, FALSE, 168 child_process_, &rv, 0, FALSE,
161 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE); 169 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
162 DCHECK(result); 170 DCHECK(result);
163 return rv; 171 return rv;
164 } 172 }
165 173
166 HANDLE ParentTokenSerializer::DuplicateFromChild(HANDLE handle) { 174 HANDLE ChildBrokerHost::DuplicateFromChild(HANDLE handle) {
167 HANDLE rv = INVALID_HANDLE_VALUE; 175 HANDLE rv = INVALID_HANDLE_VALUE;
168 BOOL result = DuplicateHandle(child_process_, handle, 176 BOOL result = DuplicateHandle(child_process_, handle,
169 base::GetCurrentProcessHandle(), &rv, 0, FALSE, 177 base::GetCurrentProcessHandle(), &rv, 0, FALSE,
170 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE); 178 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
171 DCHECK(result); 179 DCHECK(result);
172 return rv; 180 return rv;
173 } 181 }
182 #endif
174 183
175 } // namespace edk 184 } // namespace edk
176 } // namespace mojo 185 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698