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 "mojo/shell/runner/host/child_process.h" | 5 #include "mojo/shell/runner/host/child_process.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "base/memory/ref_counted.h" | 21 #include "base/memory/ref_counted.h" |
22 #include "base/memory/scoped_ptr.h" | 22 #include "base/memory/scoped_ptr.h" |
23 #include "base/message_loop/message_loop.h" | 23 #include "base/message_loop/message_loop.h" |
24 #include "base/single_thread_task_runner.h" | 24 #include "base/single_thread_task_runner.h" |
25 #include "base/synchronization/waitable_event.h" | 25 #include "base/synchronization/waitable_event.h" |
26 #include "base/thread_task_runner_handle.h" | 26 #include "base/thread_task_runner_handle.h" |
27 #include "base/threading/thread.h" | 27 #include "base/threading/thread.h" |
28 #include "base/threading/thread_checker.h" | 28 #include "base/threading/thread_checker.h" |
29 #include "mojo/edk/embedder/embedder.h" | 29 #include "mojo/edk/embedder/embedder.h" |
30 #include "mojo/edk/embedder/platform_channel_pair.h" | 30 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 31 #include "mojo/edk/embedder/process_delegate.h" |
| 32 #include "mojo/edk/embedder/scoped_platform_handle.h" |
31 #include "mojo/message_pump/message_pump_mojo.h" | 33 #include "mojo/message_pump/message_pump_mojo.h" |
32 #include "mojo/public/cpp/bindings/binding.h" | 34 #include "mojo/public/cpp/bindings/binding.h" |
33 #include "mojo/public/cpp/system/core.h" | 35 #include "mojo/public/cpp/system/core.h" |
34 #include "mojo/shell/runner/child/child_controller.mojom.h" | 36 #include "mojo/shell/runner/child/child_controller.mojom.h" |
35 #include "mojo/shell/runner/common/switches.h" | 37 #include "mojo/shell/runner/common/switches.h" |
36 #include "mojo/shell/runner/host/native_application_support.h" | 38 #include "mojo/shell/runner/host/native_application_support.h" |
37 #include "mojo/shell/runner/init.h" | 39 #include "mojo/shell/runner/init.h" |
38 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | |
39 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h" | |
40 #include "third_party/mojo/src/mojo/edk/embedder/process_delegate.h" | |
41 #include "third_party/mojo/src/mojo/edk/embedder/scoped_platform_handle.h" | |
42 | 40 |
43 #if defined(OS_LINUX) && !defined(OS_ANDROID) | 41 #if defined(OS_LINUX) && !defined(OS_ANDROID) |
44 #include "base/rand_util.h" | 42 #include "base/rand_util.h" |
45 #include "base/sys_info.h" | 43 #include "base/sys_info.h" |
46 #include "mojo/shell/runner/host/linux_sandbox.h" | 44 #include "mojo/shell/runner/host/linux_sandbox.h" |
47 #endif | 45 #endif |
48 | 46 |
49 namespace mojo { | 47 namespace mojo { |
50 namespace shell { | 48 namespace shell { |
51 | 49 |
52 namespace { | 50 namespace { |
53 | 51 |
54 void DidCreateChannel(embedder::ChannelInfo* channel_info) { | |
55 DVLOG(2) << "ChildControllerImpl::DidCreateChannel()"; | |
56 } | |
57 | |
58 // Blocker --------------------------------------------------------------------- | 52 // Blocker --------------------------------------------------------------------- |
59 | 53 |
60 // Blocks a thread until another thread unblocks it, at which point it unblocks | 54 // Blocks a thread until another thread unblocks it, at which point it unblocks |
61 // and runs a closure provided by that thread. | 55 // and runs a closure provided by that thread. |
62 class Blocker { | 56 class Blocker { |
63 public: | 57 public: |
64 class Unblocker { | 58 class Unblocker { |
65 public: | 59 public: |
66 explicit Unblocker(Blocker* blocker = nullptr) : blocker_(blocker) {} | 60 explicit Unblocker(Blocker* blocker = nullptr) : blocker_(blocker) {} |
67 ~Unblocker() {} | 61 ~Unblocker() {} |
(...skipping 29 matching lines...) Expand all Loading... |
97 base::Closure run_after_; | 91 base::Closure run_after_; |
98 | 92 |
99 DISALLOW_COPY_AND_ASSIGN(Blocker); | 93 DISALLOW_COPY_AND_ASSIGN(Blocker); |
100 }; | 94 }; |
101 | 95 |
102 // AppContext ------------------------------------------------------------------ | 96 // AppContext ------------------------------------------------------------------ |
103 | 97 |
104 class ChildControllerImpl; | 98 class ChildControllerImpl; |
105 | 99 |
106 // Should be created and initialized on the main thread. | 100 // Should be created and initialized on the main thread. |
107 // TODO(use_chrome_edk) | 101 class AppContext : public edk::ProcessDelegate { |
108 // class AppContext : public edk::ProcessDelegate { | |
109 class AppContext : public embedder::ProcessDelegate { | |
110 public: | 102 public: |
111 AppContext() | 103 AppContext() |
112 : io_thread_("io_thread"), controller_thread_("controller_thread") {} | 104 : io_thread_("io_thread"), controller_thread_("controller_thread") {} |
113 ~AppContext() override {} | 105 ~AppContext() override {} |
114 | 106 |
115 void Init() { | 107 void Init() { |
116 embedder::PreInitializeChildProcess(); | |
117 // Initialize Mojo before starting any threads. | 108 // Initialize Mojo before starting any threads. |
118 embedder::Init(); | 109 edk::Init(); |
119 | 110 |
120 // Create and start our I/O thread. | 111 // Create and start our I/O thread. |
121 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); | 112 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); |
122 CHECK(io_thread_.StartWithOptions(io_thread_options)); | 113 CHECK(io_thread_.StartWithOptions(io_thread_options)); |
123 io_runner_ = io_thread_.task_runner().get(); | 114 io_runner_ = io_thread_.task_runner().get(); |
124 CHECK(io_runner_.get()); | 115 CHECK(io_runner_.get()); |
125 | 116 |
126 // TODO(vtl): This should be SLAVE, not NONE. | 117 // TODO(vtl): This should be SLAVE, not NONE. |
127 // This must be created before controller_thread_ since MessagePumpMojo will | 118 // This must be created before controller_thread_ since MessagePumpMojo will |
128 // create a message pipe which requires this code to be run first. | 119 // create a message pipe which requires this code to be run first. |
129 embedder::InitIPCSupport(embedder::ProcessType::NONE, this, io_runner_, | 120 edk::InitIPCSupport(this, io_runner_); |
130 embedder::ScopedPlatformHandle()); | |
131 } | 121 } |
132 | 122 |
133 void StartControllerThread() { | 123 void StartControllerThread() { |
134 // Create and start our controller thread. | 124 // Create and start our controller thread. |
135 base::Thread::Options controller_thread_options; | 125 base::Thread::Options controller_thread_options; |
136 controller_thread_options.message_loop_type = | 126 controller_thread_options.message_loop_type = |
137 base::MessageLoop::TYPE_CUSTOM; | 127 base::MessageLoop::TYPE_CUSTOM; |
138 controller_thread_options.message_pump_factory = | 128 controller_thread_options.message_pump_factory = |
139 base::Bind(&common::MessagePumpMojo::Create); | 129 base::Bind(&common::MessagePumpMojo::Create); |
140 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); | 130 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); |
(...skipping 21 matching lines...) Expand all Loading... |
162 void set_controller(scoped_ptr<ChildControllerImpl> controller) { | 152 void set_controller(scoped_ptr<ChildControllerImpl> controller) { |
163 controller_ = std::move(controller); | 153 controller_ = std::move(controller); |
164 } | 154 } |
165 | 155 |
166 private: | 156 private: |
167 void ShutdownOnControllerThread() { | 157 void ShutdownOnControllerThread() { |
168 // First, destroy the controller. | 158 // First, destroy the controller. |
169 controller_.reset(); | 159 controller_.reset(); |
170 | 160 |
171 // Next shutdown IPC. We'll unblock the main thread in OnShutdownComplete(). | 161 // Next shutdown IPC. We'll unblock the main thread in OnShutdownComplete(). |
172 embedder::ShutdownIPCSupport(); | 162 edk::ShutdownIPCSupport(); |
173 } | 163 } |
174 | 164 |
175 // ProcessDelegate implementation. | 165 // ProcessDelegate implementation. |
176 void OnShutdownComplete() override { | 166 void OnShutdownComplete() override { |
177 shutdown_unblocker_.Unblock(base::Closure()); | 167 shutdown_unblocker_.Unblock(base::Closure()); |
178 } | 168 } |
179 | 169 |
180 base::Thread io_thread_; | 170 base::Thread io_thread_; |
181 scoped_refptr<base::SingleThreadTaskRunner> io_runner_; | 171 scoped_refptr<base::SingleThreadTaskRunner> io_runner_; |
182 | 172 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 new mojo::shell::LinuxSandbox(permissions)); | 284 new mojo::shell::LinuxSandbox(permissions)); |
295 sandbox->Warmup(); | 285 sandbox->Warmup(); |
296 sandbox->EngageNamespaceSandbox(); | 286 sandbox->EngageNamespaceSandbox(); |
297 sandbox->EngageSeccompSandbox(); | 287 sandbox->EngageSeccompSandbox(); |
298 sandbox->Seal(); | 288 sandbox->Seal(); |
299 return sandbox; | 289 return sandbox; |
300 } | 290 } |
301 #endif | 291 #endif |
302 | 292 |
303 void InitializeHostMessagePipe( | 293 void InitializeHostMessagePipe( |
304 embedder::ScopedPlatformHandle platform_channel, | 294 edk::ScopedPlatformHandle platform_channel, |
305 scoped_refptr<base::TaskRunner> io_task_runner, | 295 scoped_refptr<base::TaskRunner> io_task_runner, |
306 const base::Callback<void(ScopedMessagePipeHandle)>& callback) { | 296 const base::Callback<void(ScopedMessagePipeHandle)>& callback) { |
307 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | 297 edk::SetParentPipeHandle(std::move(platform_channel)); |
308 embedder::SetParentPipeHandle(std::move(platform_channel)); | 298 std::string primordial_pipe_token = |
309 std::string primordial_pipe_token = | 299 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
310 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 300 switches::kPrimordialPipeToken); |
311 switches::kPrimordialPipeToken); | 301 edk::CreateChildMessagePipe(primordial_pipe_token, callback); |
312 edk::CreateChildMessagePipe(primordial_pipe_token, callback); | |
313 } else { | |
314 ScopedMessagePipeHandle host_message_pipe; | |
315 host_message_pipe = | |
316 embedder::CreateChannel(std::move(platform_channel), | |
317 base::Bind(&DidCreateChannel), io_task_runner); | |
318 callback.Run(std::move(host_message_pipe)); | |
319 } | |
320 } | 302 } |
321 | 303 |
322 void OnHostMessagePipeCreated(AppContext* app_context, | 304 void OnHostMessagePipeCreated(AppContext* app_context, |
323 base::NativeLibrary app_library, | 305 base::NativeLibrary app_library, |
324 const Blocker::Unblocker& unblocker, | 306 const Blocker::Unblocker& unblocker, |
325 ScopedMessagePipeHandle pipe) { | 307 ScopedMessagePipeHandle pipe) { |
326 app_context->controller_runner()->PostTask( | 308 app_context->controller_runner()->PostTask( |
327 FROM_HERE, | 309 FROM_HERE, |
328 base::Bind(&ChildControllerImpl::Init, base::Unretained(app_context), | 310 base::Bind(&ChildControllerImpl::Init, base::Unretained(app_context), |
329 base::Unretained(app_library), base::Passed(&pipe), | 311 base::Unretained(app_library), base::Passed(&pipe), |
(...skipping 23 matching lines...) Expand all Loading... |
353 #if !defined(OFFICIAL_BUILD) | 335 #if !defined(OFFICIAL_BUILD) |
354 // Initialize stack dumping just before initializing sandbox to make | 336 // Initialize stack dumping just before initializing sandbox to make |
355 // sure symbol names in all loaded libraries will be cached. | 337 // sure symbol names in all loaded libraries will be cached. |
356 base::debug::EnableInProcessStackDumping(); | 338 base::debug::EnableInProcessStackDumping(); |
357 #endif | 339 #endif |
358 #if defined(OS_LINUX) && !defined(OS_ANDROID) | 340 #if defined(OS_LINUX) && !defined(OS_ANDROID) |
359 if (command_line.HasSwitch(switches::kEnableSandbox)) | 341 if (command_line.HasSwitch(switches::kEnableSandbox)) |
360 sandbox = InitializeSandbox(); | 342 sandbox = InitializeSandbox(); |
361 #endif | 343 #endif |
362 | 344 |
363 embedder::ScopedPlatformHandle platform_channel = | 345 edk::ScopedPlatformHandle platform_channel = |
364 embedder::PlatformChannelPair::PassClientHandleFromParentProcess( | 346 edk::PlatformChannelPair::PassClientHandleFromParentProcess(command_line); |
365 command_line); | |
366 CHECK(platform_channel.is_valid()); | 347 CHECK(platform_channel.is_valid()); |
367 | 348 |
368 DCHECK(!base::MessageLoop::current()); | 349 DCHECK(!base::MessageLoop::current()); |
369 | 350 |
370 Blocker blocker; | 351 Blocker blocker; |
371 AppContext app_context; | 352 AppContext app_context; |
372 app_context.Init(); | 353 app_context.Init(); |
373 app_context.StartControllerThread(); | 354 app_context.StartControllerThread(); |
374 | 355 |
375 app_context.controller_runner()->PostTask( | 356 app_context.controller_runner()->PostTask( |
376 FROM_HERE, | 357 FROM_HERE, |
377 base::Bind( | 358 base::Bind( |
378 &InitializeHostMessagePipe, base::Passed(&platform_channel), | 359 &InitializeHostMessagePipe, base::Passed(&platform_channel), |
379 make_scoped_refptr(app_context.io_runner()), | 360 make_scoped_refptr(app_context.io_runner()), |
380 base::Bind(&OnHostMessagePipeCreated, base::Unretained(&app_context), | 361 base::Bind(&OnHostMessagePipeCreated, base::Unretained(&app_context), |
381 base::Unretained(app_library), blocker.GetUnblocker()))); | 362 base::Unretained(app_library), blocker.GetUnblocker()))); |
382 | 363 |
383 // This will block, then run whatever the controller wants. | 364 // This will block, then run whatever the controller wants. |
384 blocker.Block(); | 365 blocker.Block(); |
385 | 366 |
386 app_context.Shutdown(); | 367 app_context.Shutdown(); |
387 | 368 |
388 return 0; | 369 return 0; |
389 } | 370 } |
390 | 371 |
391 } // namespace shell | 372 } // namespace shell |
392 } // namespace mojo | 373 } // namespace mojo |
OLD | NEW |