OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <unistd.h> | 5 #include <unistd.h> |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
19 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
21 #include "base/threading/thread_checker.h" | 21 #include "base/threading/thread_checker.h" |
22 #include "mojo/common/message_pump_mojo.h" | 22 #include "mojo/common/message_pump_mojo.h" |
23 #include "mojo/edk/embedder/embedder.h" | 23 #include "mojo/edk/embedder/embedder.h" |
24 #include "mojo/edk/embedder/platform_channel_pair.h" | 24 #include "mojo/edk/embedder/platform_channel_pair.h" |
25 #include "mojo/edk/embedder/process_delegate.h" | |
26 #include "mojo/edk/embedder/scoped_platform_handle.h" | 25 #include "mojo/edk/embedder/scoped_platform_handle.h" |
27 #include "mojo/edk/embedder/simple_platform_support.h" | 26 #include "mojo/edk/embedder/simple_platform_support.h" |
| 27 #include "mojo/edk/embedder/slave_process_delegate.h" |
28 #include "mojo/public/cpp/system/core.h" | 28 #include "mojo/public/cpp/system/core.h" |
29 #include "shell/child_controller.mojom.h" | 29 #include "shell/child_controller.mojom.h" |
30 #include "shell/child_switches.h" | 30 #include "shell/child_switches.h" |
31 #include "shell/init.h" | 31 #include "shell/init.h" |
32 #include "shell/native_application_support.h" | 32 #include "shell/native_application_support.h" |
33 | 33 |
34 namespace shell { | 34 namespace shell { |
35 namespace { | 35 namespace { |
36 | 36 |
37 // Blocker --------------------------------------------------------------------- | 37 // Blocker --------------------------------------------------------------------- |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 base::Closure run_after_; | 76 base::Closure run_after_; |
77 | 77 |
78 DISALLOW_COPY_AND_ASSIGN(Blocker); | 78 DISALLOW_COPY_AND_ASSIGN(Blocker); |
79 }; | 79 }; |
80 | 80 |
81 // AppContext ------------------------------------------------------------------ | 81 // AppContext ------------------------------------------------------------------ |
82 | 82 |
83 class ChildControllerImpl; | 83 class ChildControllerImpl; |
84 | 84 |
85 // Should be created and initialized on the main thread. | 85 // Should be created and initialized on the main thread. |
86 class AppContext : public mojo::embedder::ProcessDelegate { | 86 class AppContext : public mojo::embedder::SlaveProcessDelegate { |
87 public: | 87 public: |
88 AppContext() | 88 AppContext() |
89 : io_thread_("io_thread"), controller_thread_("controller_thread") {} | 89 : io_thread_("io_thread"), controller_thread_("controller_thread") {} |
90 ~AppContext() override {} | 90 ~AppContext() override {} |
91 | 91 |
92 void Init() { | 92 void Init(mojo::embedder::ScopedPlatformHandle platform_handle) { |
93 // Initialize Mojo before starting any threads. | 93 // Initialize Mojo before starting any threads. |
94 mojo::embedder::Init( | 94 mojo::embedder::Init( |
95 make_scoped_ptr(new mojo::embedder::SimplePlatformSupport())); | 95 make_scoped_ptr(new mojo::embedder::SimplePlatformSupport())); |
96 | 96 |
97 // Create and start our I/O thread. | 97 // Create and start our I/O thread. |
98 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); | 98 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); |
99 CHECK(io_thread_.StartWithOptions(io_thread_options)); | 99 CHECK(io_thread_.StartWithOptions(io_thread_options)); |
100 io_runner_ = io_thread_.message_loop_proxy().get(); | 100 io_runner_ = io_thread_.message_loop_proxy().get(); |
101 CHECK(io_runner_.get()); | 101 CHECK(io_runner_.get()); |
102 | 102 |
103 // Create and start our controller thread. | 103 // Create and start our controller thread. |
104 base::Thread::Options controller_thread_options; | 104 base::Thread::Options controller_thread_options; |
105 controller_thread_options.message_loop_type = | 105 controller_thread_options.message_loop_type = |
106 base::MessageLoop::TYPE_CUSTOM; | 106 base::MessageLoop::TYPE_CUSTOM; |
107 controller_thread_options.message_pump_factory = | 107 controller_thread_options.message_pump_factory = |
108 base::Bind(&mojo::common::MessagePumpMojo::Create); | 108 base::Bind(&mojo::common::MessagePumpMojo::Create); |
109 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); | 109 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); |
110 controller_runner_ = controller_thread_.message_loop_proxy().get(); | 110 controller_runner_ = controller_thread_.message_loop_proxy().get(); |
111 CHECK(controller_runner_.get()); | 111 CHECK(controller_runner_.get()); |
112 | 112 |
113 // TODO(vtl): This should be SLAVE, not NONE. | 113 mojo::embedder::InitIPCSupport(mojo::embedder::ProcessType::SLAVE, |
114 mojo::embedder::InitIPCSupport(mojo::embedder::ProcessType::NONE, | |
115 controller_runner_, this, io_runner_, | 114 controller_runner_, this, io_runner_, |
116 mojo::embedder::ScopedPlatformHandle()); | 115 platform_handle.Pass()); |
117 } | 116 } |
118 | 117 |
119 void Shutdown() { | 118 void Shutdown() { |
120 Blocker blocker; | 119 Blocker blocker; |
121 shutdown_unblocker_ = blocker.GetUnblocker(); | 120 shutdown_unblocker_ = blocker.GetUnblocker(); |
122 controller_runner_->PostTask( | 121 controller_runner_->PostTask( |
123 FROM_HERE, base::Bind(&AppContext::ShutdownOnControllerThread, | 122 FROM_HERE, base::Bind(&AppContext::ShutdownOnControllerThread, |
124 base::Unretained(this))); | 123 base::Unretained(this))); |
125 blocker.Block(); | 124 blocker.Block(); |
126 } | 125 } |
(...skipping 12 matching lines...) Expand all Loading... |
139 | 138 |
140 private: | 139 private: |
141 void ShutdownOnControllerThread() { | 140 void ShutdownOnControllerThread() { |
142 // First, destroy the controller. | 141 // First, destroy the controller. |
143 controller_.reset(); | 142 controller_.reset(); |
144 | 143 |
145 // Next shutdown IPC. We'll unblock the main thread in OnShutdownComplete(). | 144 // Next shutdown IPC. We'll unblock the main thread in OnShutdownComplete(). |
146 mojo::embedder::ShutdownIPCSupport(); | 145 mojo::embedder::ShutdownIPCSupport(); |
147 } | 146 } |
148 | 147 |
149 // ProcessDelegate implementation. | 148 // SlaveProcessDelegate implementation. |
150 void OnShutdownComplete() override { | 149 void OnShutdownComplete() override { |
151 shutdown_unblocker_.Unblock(base::Closure()); | 150 shutdown_unblocker_.Unblock(base::Closure()); |
152 } | 151 } |
153 | 152 |
| 153 void OnMasterDisconnect() override { |
| 154 // We've lost the connection to the master process. This is not recoverable. |
| 155 LOG(ERROR) << "Disconnected from master"; |
| 156 _exit(1); |
| 157 } |
| 158 |
154 base::Thread io_thread_; | 159 base::Thread io_thread_; |
155 scoped_refptr<base::SingleThreadTaskRunner> io_runner_; | 160 scoped_refptr<base::SingleThreadTaskRunner> io_runner_; |
156 | 161 |
157 base::Thread controller_thread_; | 162 base::Thread controller_thread_; |
158 scoped_refptr<base::SingleThreadTaskRunner> controller_runner_; | 163 scoped_refptr<base::SingleThreadTaskRunner> controller_runner_; |
159 | 164 |
160 // Accessed only on the controller thread. | 165 // Accessed only on the controller thread. |
161 scoped_ptr<ChildControllerImpl> controller_; | 166 scoped_ptr<ChildControllerImpl> controller_; |
162 | 167 |
163 // Used to unblock the main thread on shutdown. | 168 // Used to unblock the main thread on shutdown. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 } | 208 } |
204 | 209 |
205 void Bind(mojo::ScopedMessagePipeHandle handle) { | 210 void Bind(mojo::ScopedMessagePipeHandle handle) { |
206 binding_.Bind(handle.Pass()); | 211 binding_.Bind(handle.Pass()); |
207 } | 212 } |
208 | 213 |
209 // |mojo::ErrorHandler| methods: | 214 // |mojo::ErrorHandler| methods: |
210 void OnConnectionError() override { | 215 void OnConnectionError() override { |
211 // A connection error means the connection to the shell is lost. This is not | 216 // A connection error means the connection to the shell is lost. This is not |
212 // recoverable. | 217 // recoverable. |
213 LOG(ERROR) << "Connection error to the shell."; | 218 LOG(ERROR) << "Connection error to the shell"; |
214 _exit(1); | 219 _exit(1); |
215 } | 220 } |
216 | 221 |
217 // |ChildController| methods: | 222 // |ChildController| methods: |
218 void StartApp(const mojo::String& app_path, | 223 void StartApp(const mojo::String& app_path, |
219 mojo::InterfaceRequest<mojo::Application> application_request, | 224 mojo::InterfaceRequest<mojo::Application> application_request, |
220 const StartAppCallback& on_app_complete) override { | 225 const StartAppCallback& on_app_complete) override { |
221 DVLOG(2) << "ChildControllerImpl::StartApp(" << app_path << ", ...)"; | 226 DVLOG(2) << "ChildControllerImpl::StartApp(" << app_path << ", ...)"; |
222 DCHECK(thread_checker_.CalledOnValidThread()); | 227 DCHECK(thread_checker_.CalledOnValidThread()); |
223 | 228 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 | 277 |
273 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); | 278 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); |
274 }; | 279 }; |
275 | 280 |
276 } // namespace | 281 } // namespace |
277 } // namespace shell | 282 } // namespace shell |
278 | 283 |
279 int main(int argc, char** argv) { | 284 int main(int argc, char** argv) { |
280 base::AtExitManager at_exit; | 285 base::AtExitManager at_exit; |
281 base::CommandLine::Init(argc, argv); | 286 base::CommandLine::Init(argc, argv); |
| 287 const base::CommandLine& command_line = |
| 288 *base::CommandLine::ForCurrentProcess(); |
282 | 289 |
283 shell::InitializeLogging(); | 290 shell::InitializeLogging(); |
284 | 291 |
285 // Make sure that we're really meant to be invoked as the child process. | 292 // Make sure that we're really meant to be invoked as the child process. |
286 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( | |
287 switches::kChildProcess)); | |
288 | 293 |
289 mojo::embedder::ScopedPlatformHandle platform_channel = | 294 CHECK(command_line.HasSwitch(switches::kChildConnectionId)); |
| 295 std::string child_connection_id = |
| 296 command_line.GetSwitchValueASCII(switches::kChildConnectionId); |
| 297 CHECK(!child_connection_id.empty()); |
| 298 |
| 299 mojo::embedder::ScopedPlatformHandle platform_handle = |
290 mojo::embedder::PlatformChannelPair::PassClientHandleFromParentProcess( | 300 mojo::embedder::PlatformChannelPair::PassClientHandleFromParentProcess( |
291 *base::CommandLine::ForCurrentProcess()); | 301 command_line); |
292 CHECK(platform_channel.is_valid()); | 302 CHECK(platform_handle.is_valid()); |
293 | 303 |
294 shell::AppContext app_context; | 304 shell::AppContext app_context; |
295 app_context.Init(); | 305 app_context.Init(platform_handle.Pass()); |
| 306 |
| 307 mojo::embedder::ScopedPlatformHandle platform_channel; |
| 308 mojo::embedder::ConnectToMaster(child_connection_id, &platform_channel); |
296 | 309 |
297 shell::Blocker blocker; | 310 shell::Blocker blocker; |
298 app_context.controller_runner()->PostTask( | 311 app_context.controller_runner()->PostTask( |
299 FROM_HERE, | 312 FROM_HERE, |
300 base::Bind(&shell::ChildControllerImpl::Init, | 313 base::Bind(&shell::ChildControllerImpl::Init, |
301 base::Unretained(&app_context), | 314 base::Unretained(&app_context), |
302 base::Passed(&platform_channel), blocker.GetUnblocker())); | 315 base::Passed(&platform_channel), blocker.GetUnblocker())); |
303 // This will block, then run whatever the controller wants. | 316 // This will block, then run whatever the controller wants. |
304 blocker.Block(); | 317 blocker.Block(); |
305 | 318 |
306 app_context.Shutdown(); | 319 app_context.Shutdown(); |
307 | 320 |
308 return 0; | 321 return 0; |
309 } | 322 } |
OLD | NEW |