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_controller.h" | 5 #include "mojo/edk/system/node_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
16 #include "base/process/process_handle.h" | 16 #include "base/process/process_handle.h" |
| 17 #include "base/rand_util.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "base/timer/elapsed_timer.h" | 19 #include "base/timer/elapsed_timer.h" |
19 #include "crypto/random.h" | |
20 #include "mojo/edk/embedder/embedder_internal.h" | 20 #include "mojo/edk/embedder/embedder_internal.h" |
21 #include "mojo/edk/embedder/platform_channel_pair.h" | 21 #include "mojo/edk/embedder/platform_channel_pair.h" |
22 #include "mojo/edk/system/broker.h" | 22 #include "mojo/edk/system/broker.h" |
23 #include "mojo/edk/system/broker_host.h" | 23 #include "mojo/edk/system/broker_host.h" |
24 #include "mojo/edk/system/core.h" | 24 #include "mojo/edk/system/core.h" |
25 #include "mojo/edk/system/ports_message.h" | 25 #include "mojo/edk/system/ports_message.h" |
26 | 26 |
27 #if defined(OS_MACOSX) && !defined(OS_IOS) | 27 #if defined(OS_MACOSX) && !defined(OS_IOS) |
28 #include "mojo/edk/system/mach_port_relay.h" | 28 #include "mojo/edk/system/mach_port_relay.h" |
29 #endif | 29 #endif |
30 | 30 |
| 31 #if !defined(OS_NACL) |
| 32 #include "crypto/random.h" |
| 33 #endif |
| 34 |
31 namespace mojo { | 35 namespace mojo { |
32 namespace edk { | 36 namespace edk { |
33 | 37 |
34 namespace { | 38 namespace { |
35 | 39 |
| 40 #if defined(OS_NACL) |
| 41 template <typename T> |
| 42 void GenerateRandomName(T* out) { base::RandBytes(out, sizeof(T)); } |
| 43 #else |
36 template <typename T> | 44 template <typename T> |
37 void GenerateRandomName(T* out) { crypto::RandBytes(out, sizeof(T)); } | 45 void GenerateRandomName(T* out) { crypto::RandBytes(out, sizeof(T)); } |
| 46 #endif |
38 | 47 |
39 ports::NodeName GetRandomNodeName() { | 48 ports::NodeName GetRandomNodeName() { |
40 ports::NodeName name; | 49 ports::NodeName name; |
41 GenerateRandomName(&name); | 50 GenerateRandomName(&name); |
42 return name; | 51 return name; |
43 } | 52 } |
44 | 53 |
45 void RecordPeerCount(size_t count) { | 54 void RecordPeerCount(size_t count) { |
46 DCHECK_LE(count, static_cast<size_t>(std::numeric_limits<int32_t>::max())); | 55 DCHECK_LE(count, static_cast<size_t>(std::numeric_limits<int32_t>::max())); |
47 | 56 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 io_task_runner_->PostTask( | 144 io_task_runner_->PostTask( |
136 FROM_HERE, | 145 FROM_HERE, |
137 base::Bind(&NodeController::ConnectToChildOnIOThread, | 146 base::Bind(&NodeController::ConnectToChildOnIOThread, |
138 base::Unretained(this), | 147 base::Unretained(this), |
139 process_handle, | 148 process_handle, |
140 base::Passed(&platform_handle))); | 149 base::Passed(&platform_handle))); |
141 } | 150 } |
142 | 151 |
143 void NodeController::ConnectToParent(ScopedPlatformHandle platform_handle) { | 152 void NodeController::ConnectToParent(ScopedPlatformHandle platform_handle) { |
144 // TODO(amistry): Consider the need for a broker on Windows. | 153 // TODO(amistry): Consider the need for a broker on Windows. |
145 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 154 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_NACL) |
146 // On posix, use the bootstrap channel for the broker and receive the node's | 155 // On posix, use the bootstrap channel for the broker and receive the node's |
147 // channel synchronously as the first message from the broker. | 156 // channel synchronously as the first message from the broker. |
148 base::ElapsedTimer timer; | 157 base::ElapsedTimer timer; |
149 broker_.reset(new Broker(std::move(platform_handle))); | 158 broker_.reset(new Broker(std::move(platform_handle))); |
150 platform_handle = broker_->GetParentPlatformHandle(); | 159 platform_handle = broker_->GetParentPlatformHandle(); |
151 UMA_HISTOGRAM_TIMES("Mojo.System.GetParentPlatformHandleSyncTime", | 160 UMA_HISTOGRAM_TIMES("Mojo.System.GetParentPlatformHandleSyncTime", |
152 timer.Elapsed()); | 161 timer.Elapsed()); |
153 #endif | 162 #endif |
154 | 163 |
155 io_task_runner_->PostTask( | 164 io_task_runner_->PostTask( |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 | 239 |
231 int NodeController::MergeLocalPorts(const ports::PortRef& port0, | 240 int NodeController::MergeLocalPorts(const ports::PortRef& port0, |
232 const ports::PortRef& port1) { | 241 const ports::PortRef& port1) { |
233 int rv = node_->MergeLocalPorts(port0, port1); | 242 int rv = node_->MergeLocalPorts(port0, port1); |
234 AcceptIncomingMessages(); | 243 AcceptIncomingMessages(); |
235 return rv; | 244 return rv; |
236 } | 245 } |
237 | 246 |
238 scoped_refptr<PlatformSharedBuffer> NodeController::CreateSharedBuffer( | 247 scoped_refptr<PlatformSharedBuffer> NodeController::CreateSharedBuffer( |
239 size_t num_bytes) { | 248 size_t num_bytes) { |
240 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 249 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_NACL) |
241 // Shared buffer creation failure is fatal, so always use the broker when we | 250 // Shared buffer creation failure is fatal, so always use the broker when we |
242 // have one. This does mean that a non-root process that has children will use | 251 // have one. This does mean that a non-root process that has children will use |
243 // the broker for shared buffer creation even though that process is | 252 // the broker for shared buffer creation even though that process is |
244 // privileged. | 253 // privileged. |
245 if (broker_) { | 254 if (broker_) { |
246 return broker_->GetSharedBuffer(num_bytes); | 255 return broker_->GetSharedBuffer(num_bytes); |
247 } | 256 } |
248 #endif | 257 #endif |
249 return PlatformSharedBuffer::Create(num_bytes); | 258 return PlatformSharedBuffer::Create(num_bytes); |
250 } | 259 } |
251 | 260 |
252 void NodeController::RequestShutdown(const base::Closure& callback) { | 261 void NodeController::RequestShutdown(const base::Closure& callback) { |
253 { | 262 { |
254 base::AutoLock lock(shutdown_lock_); | 263 base::AutoLock lock(shutdown_lock_); |
255 shutdown_callback_ = callback; | 264 shutdown_callback_ = callback; |
256 shutdown_callback_flag_.Set(true); | 265 shutdown_callback_flag_.Set(true); |
257 } | 266 } |
258 | 267 |
259 AttemptShutdownIfRequested(); | 268 AttemptShutdownIfRequested(); |
260 } | 269 } |
261 | 270 |
262 void NodeController::ConnectToChildOnIOThread( | 271 void NodeController::ConnectToChildOnIOThread( |
263 base::ProcessHandle process_handle, | 272 base::ProcessHandle process_handle, |
264 ScopedPlatformHandle platform_handle) { | 273 ScopedPlatformHandle platform_handle) { |
265 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 274 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
266 | 275 |
267 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 276 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_NACL) |
268 PlatformChannelPair node_channel; | 277 PlatformChannelPair node_channel; |
269 // BrokerHost owns itself. | 278 // BrokerHost owns itself. |
270 BrokerHost* broker_host = new BrokerHost(std::move(platform_handle)); | 279 BrokerHost* broker_host = new BrokerHost(std::move(platform_handle)); |
271 broker_host->SendChannel(node_channel.PassClientHandle()); | 280 broker_host->SendChannel(node_channel.PassClientHandle()); |
272 scoped_refptr<NodeChannel> channel = NodeChannel::Create( | 281 scoped_refptr<NodeChannel> channel = NodeChannel::Create( |
273 this, node_channel.PassServerHandle(), io_task_runner_); | 282 this, node_channel.PassServerHandle(), io_task_runner_); |
274 #else | 283 #else |
275 scoped_refptr<NodeChannel> channel = | 284 scoped_refptr<NodeChannel> channel = |
276 NodeChannel::Create(this, std::move(platform_handle), io_task_runner_); | 285 NodeChannel::Create(this, std::move(platform_handle), io_task_runner_); |
277 #endif | 286 #endif |
(...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1010 shutdown_callback_flag_.Set(false); | 1019 shutdown_callback_flag_.Set(false); |
1011 } | 1020 } |
1012 | 1021 |
1013 DCHECK(!callback.is_null()); | 1022 DCHECK(!callback.is_null()); |
1014 | 1023 |
1015 callback.Run(); | 1024 callback.Run(); |
1016 } | 1025 } |
1017 | 1026 |
1018 } // namespace edk | 1027 } // namespace edk |
1019 } // namespace mojo | 1028 } // namespace mojo |
OLD | NEW |