| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/node_channel.h" | 5 #include "mojo/edk/system/node_channel.h" |
| 6 | 6 |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <sstream> | 9 #include <sstream> |
| 10 | 10 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 struct AcceptParentData { | 53 struct AcceptParentData { |
| 54 ports::NodeName token; | 54 ports::NodeName token; |
| 55 ports::NodeName child_name; | 55 ports::NodeName child_name; |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 // This message may include a process handle on plaforms that require it. | 58 // This message may include a process handle on plaforms that require it. |
| 59 struct AddBrokerClientData { | 59 struct AddBrokerClientData { |
| 60 ports::NodeName client_name; | 60 ports::NodeName client_name; |
| 61 #if !defined(OS_WIN) |
| 62 uint32_t process_handle; |
| 63 uint32_t padding; |
| 64 #endif |
| 61 }; | 65 }; |
| 62 | 66 |
| 67 #if !defined(OS_WIN) |
| 68 static_assert(sizeof(base::ProcessHandle) == sizeof(uint32_t), |
| 69 "Unexpected pid size"); |
| 70 static_assert(sizeof(AddBrokerClientData) % kChannelMessageAlignment == 0, |
| 71 "Invalid AddBrokerClientData size."); |
| 72 #endif |
| 73 |
| 63 // This data is followed by a platform channel handle to the broker. | 74 // This data is followed by a platform channel handle to the broker. |
| 64 struct BrokerClientAddedData { | 75 struct BrokerClientAddedData { |
| 65 ports::NodeName client_name; | 76 ports::NodeName client_name; |
| 66 }; | 77 }; |
| 67 | 78 |
| 68 // This data may be followed by a platform channel handle to the broker. If not, | 79 // This data may be followed by a platform channel handle to the broker. If not, |
| 69 // then the parent is the broker and its channel should be used as such. | 80 // then the parent is the broker and its channel should be used as such. |
| 70 struct AcceptBrokerClientData { | 81 struct AcceptBrokerClientData { |
| 71 ports::NodeName broker_name; | 82 ports::NodeName broker_name; |
| 72 }; | 83 }; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 159 |
| 149 void NodeChannel::ShutDown() { | 160 void NodeChannel::ShutDown() { |
| 150 base::AutoLock lock(channel_lock_); | 161 base::AutoLock lock(channel_lock_); |
| 151 if (channel_) { | 162 if (channel_) { |
| 152 channel_->ShutDown(); | 163 channel_->ShutDown(); |
| 153 channel_ = nullptr; | 164 channel_ = nullptr; |
| 154 } | 165 } |
| 155 } | 166 } |
| 156 | 167 |
| 157 void NodeChannel::SetRemoteProcessHandle(base::ProcessHandle process_handle) { | 168 void NodeChannel::SetRemoteProcessHandle(base::ProcessHandle process_handle) { |
| 158 #if defined(OS_WIN) | |
| 159 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 169 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 160 base::AutoLock lock(remote_process_handle_lock_); | 170 base::AutoLock lock(remote_process_handle_lock_); |
| 161 remote_process_handle_ = process_handle; | 171 remote_process_handle_ = process_handle; |
| 162 #endif | |
| 163 } | 172 } |
| 164 | 173 |
| 165 bool NodeChannel::HasRemoteProcessHandle() { | 174 bool NodeChannel::HasRemoteProcessHandle() { |
| 166 #if defined(OS_WIN) | |
| 167 base::AutoLock lock(remote_process_handle_lock_); | 175 base::AutoLock lock(remote_process_handle_lock_); |
| 168 return remote_process_handle_ != base::kNullProcessHandle; | 176 return remote_process_handle_ != base::kNullProcessHandle; |
| 169 #else | |
| 170 return false; | |
| 171 #endif | |
| 172 } | 177 } |
| 173 | 178 |
| 174 ScopedPlatformHandle NodeChannel::CopyRemoteProcessHandle() { | 179 base::ProcessHandle NodeChannel::CopyRemoteProcessHandle() { |
| 180 base::AutoLock lock(remote_process_handle_lock_); |
| 175 #if defined(OS_WIN) | 181 #if defined(OS_WIN) |
| 176 base::AutoLock lock(remote_process_handle_lock_); | |
| 177 if (remote_process_handle_ != base::kNullProcessHandle) { | 182 if (remote_process_handle_ != base::kNullProcessHandle) { |
| 178 // Privileged nodes use this to pass their childrens' process handles to the | 183 // Privileged nodes use this to pass their childrens' process handles to the |
| 179 // broker on launch. | 184 // broker on launch. |
| 180 HANDLE handle = remote_process_handle_; | 185 HANDLE handle = remote_process_handle_; |
| 181 BOOL result = DuplicateHandle( | 186 BOOL result = DuplicateHandle( |
| 182 base::GetCurrentProcessHandle(), remote_process_handle_, | 187 base::GetCurrentProcessHandle(), remote_process_handle_, |
| 183 base::GetCurrentProcessHandle(), &handle, 0, FALSE, | 188 base::GetCurrentProcessHandle(), &handle, 0, FALSE, |
| 184 DUPLICATE_SAME_ACCESS); | 189 DUPLICATE_SAME_ACCESS); |
| 185 DCHECK(result); | 190 DCHECK(result); |
| 186 return ScopedPlatformHandle(PlatformHandle(handle)); | 191 return handle; |
| 187 } | 192 } |
| 193 return base::kNullProcessHandle; |
| 194 #else |
| 195 return remote_process_handle_; |
| 188 #endif | 196 #endif |
| 189 return ScopedPlatformHandle(); | |
| 190 } | 197 } |
| 191 | 198 |
| 192 void NodeChannel::SetRemoteNodeName(const ports::NodeName& name) { | 199 void NodeChannel::SetRemoteNodeName(const ports::NodeName& name) { |
| 193 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 200 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 194 remote_node_name_ = name; | 201 remote_node_name_ = name; |
| 195 } | 202 } |
| 196 | 203 |
| 197 void NodeChannel::AcceptChild(const ports::NodeName& parent_name, | 204 void NodeChannel::AcceptChild(const ports::NodeName& parent_name, |
| 198 const ports::NodeName& token) { | 205 const ports::NodeName& token) { |
| 199 AcceptChildData* data; | 206 AcceptChildData* data; |
| 200 Channel::MessagePtr message = CreateMessage( | 207 Channel::MessagePtr message = CreateMessage( |
| 201 MessageType::ACCEPT_CHILD, sizeof(AcceptChildData), 0, &data); | 208 MessageType::ACCEPT_CHILD, sizeof(AcceptChildData), 0, &data); |
| 202 data->parent_name = parent_name; | 209 data->parent_name = parent_name; |
| 203 data->token = token; | 210 data->token = token; |
| 204 WriteChannelMessage(std::move(message)); | 211 WriteChannelMessage(std::move(message)); |
| 205 } | 212 } |
| 206 | 213 |
| 207 void NodeChannel::AcceptParent(const ports::NodeName& token, | 214 void NodeChannel::AcceptParent(const ports::NodeName& token, |
| 208 const ports::NodeName& child_name) { | 215 const ports::NodeName& child_name) { |
| 209 AcceptParentData* data; | 216 AcceptParentData* data; |
| 210 Channel::MessagePtr message = CreateMessage( | 217 Channel::MessagePtr message = CreateMessage( |
| 211 MessageType::ACCEPT_PARENT, sizeof(AcceptParentData), 0, &data); | 218 MessageType::ACCEPT_PARENT, sizeof(AcceptParentData), 0, &data); |
| 212 data->token = token; | 219 data->token = token; |
| 213 data->child_name = child_name; | 220 data->child_name = child_name; |
| 214 WriteChannelMessage(std::move(message)); | 221 WriteChannelMessage(std::move(message)); |
| 215 } | 222 } |
| 216 | 223 |
| 217 void NodeChannel::AddBrokerClient(const ports::NodeName& client_name, | 224 void NodeChannel::AddBrokerClient(const ports::NodeName& client_name, |
| 218 ScopedPlatformHandle process_handle) { | 225 base::ProcessHandle process_handle) { |
| 219 AddBrokerClientData* data; | 226 AddBrokerClientData* data; |
| 220 ScopedPlatformHandleVectorPtr handles(new PlatformHandleVector()); | 227 ScopedPlatformHandleVectorPtr handles(new PlatformHandleVector()); |
| 221 #if defined(OS_WIN) | 228 #if defined(OS_WIN) |
| 222 handles->push_back(process_handle.release()); | 229 handles->push_back(PlatformHandle(process_handle)); |
| 223 #endif | 230 #endif |
| 224 Channel::MessagePtr message = CreateMessage( | 231 Channel::MessagePtr message = CreateMessage( |
| 225 MessageType::ADD_BROKER_CLIENT, sizeof(AddBrokerClientData), | 232 MessageType::ADD_BROKER_CLIENT, sizeof(AddBrokerClientData), |
| 226 handles->size(), &data); | 233 handles->size(), &data); |
| 227 message->SetHandles(std::move(handles)); | 234 message->SetHandles(std::move(handles)); |
| 228 data->client_name = client_name; | 235 data->client_name = client_name; |
| 236 #if !defined(OS_WIN) |
| 237 data->process_handle = process_handle; |
| 238 #endif |
| 229 WriteChannelMessage(std::move(message)); | 239 WriteChannelMessage(std::move(message)); |
| 230 } | 240 } |
| 231 | 241 |
| 232 void NodeChannel::BrokerClientAdded(const ports::NodeName& client_name, | 242 void NodeChannel::BrokerClientAdded(const ports::NodeName& client_name, |
| 233 ScopedPlatformHandle broker_channel) { | 243 ScopedPlatformHandle broker_channel) { |
| 234 BrokerClientAddedData* data; | 244 BrokerClientAddedData* data; |
| 235 ScopedPlatformHandleVectorPtr handles(new PlatformHandleVector()); | 245 ScopedPlatformHandleVectorPtr handles(new PlatformHandleVector()); |
| 236 if (broker_channel.is_valid()) | 246 if (broker_channel.is_valid()) |
| 237 handles->push_back(broker_channel.release()); | 247 handles->push_back(broker_channel.release()); |
| 238 Channel::MessagePtr message = CreateMessage( | 248 Channel::MessagePtr message = CreateMessage( |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 const AddBrokerClientData* data; | 385 const AddBrokerClientData* data; |
| 376 GetMessagePayload(payload, &data); | 386 GetMessagePayload(payload, &data); |
| 377 ScopedPlatformHandle process_handle; | 387 ScopedPlatformHandle process_handle; |
| 378 #if defined(OS_WIN) | 388 #if defined(OS_WIN) |
| 379 if (!handles || handles->size() != 1) { | 389 if (!handles || handles->size() != 1) { |
| 380 DLOG(ERROR) << "Dropping invalid AddBrokerClient message."; | 390 DLOG(ERROR) << "Dropping invalid AddBrokerClient message."; |
| 381 break; | 391 break; |
| 382 } | 392 } |
| 383 process_handle = ScopedPlatformHandle(handles->at(0)); | 393 process_handle = ScopedPlatformHandle(handles->at(0)); |
| 384 handles->clear(); | 394 handles->clear(); |
| 395 delegate_->OnAddBrokerClient(remote_node_name_, data->client_name, |
| 396 process_handle.release().handle); |
| 385 #else | 397 #else |
| 386 if (handles && handles->size() != 0) { | 398 if (handles && handles->size() != 0) { |
| 387 DLOG(ERROR) << "Dropping invalid AddBrokerClient message."; | 399 DLOG(ERROR) << "Dropping invalid AddBrokerClient message."; |
| 388 break; | 400 break; |
| 389 } | 401 } |
| 402 delegate_->OnAddBrokerClient(remote_node_name_, data->client_name, |
| 403 data->process_handle); |
| 390 #endif | 404 #endif |
| 391 delegate_->OnAddBrokerClient(remote_node_name_, data->client_name, | |
| 392 std::move(process_handle)); | |
| 393 break; | 405 break; |
| 394 } | 406 } |
| 395 | 407 |
| 396 case MessageType::BROKER_CLIENT_ADDED: { | 408 case MessageType::BROKER_CLIENT_ADDED: { |
| 397 const BrokerClientAddedData* data; | 409 const BrokerClientAddedData* data; |
| 398 GetMessagePayload(payload, &data); | 410 GetMessagePayload(payload, &data); |
| 399 ScopedPlatformHandle broker_channel; | 411 ScopedPlatformHandle broker_channel; |
| 400 if (!handles || handles->size() != 1) { | 412 if (!handles || handles->size() != 1) { |
| 401 DLOG(ERROR) << "Dropping invalid BrokerClientAdded message."; | 413 DLOG(ERROR) << "Dropping invalid BrokerClientAdded message."; |
| 402 break; | 414 break; |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 | 554 |
| 543 base::AutoLock lock(channel_lock_); | 555 base::AutoLock lock(channel_lock_); |
| 544 if (!channel_) | 556 if (!channel_) |
| 545 DLOG(ERROR) << "Dropping message on closed channel."; | 557 DLOG(ERROR) << "Dropping message on closed channel."; |
| 546 else | 558 else |
| 547 channel_->Write(std::move(message)); | 559 channel_->Write(std::move(message)); |
| 548 } | 560 } |
| 549 | 561 |
| 550 } // namespace edk | 562 } // namespace edk |
| 551 } // namespace mojo | 563 } // namespace mojo |
| OLD | NEW |