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 <memory> |
| 8 |
7 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
8 #include "base/bind.h" | 10 #include "base/bind.h" |
9 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
10 #include "base/command_line.h" | 12 #include "base/command_line.h" |
11 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
12 #include "base/location.h" | 14 #include "base/location.h" |
13 #include "base/logging.h" | 15 #include "base/logging.h" |
14 #include "base/macros.h" | 16 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
16 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
17 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
18 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
19 #include "base/synchronization/waitable_event.h" | 21 #include "base/synchronization/waitable_event.h" |
20 #include "base/thread_task_runner_handle.h" | 22 #include "base/thread_task_runner_handle.h" |
21 #include "base/threading/thread.h" | 23 #include "base/threading/thread.h" |
22 #include "base/threading/thread_checker.h" | 24 #include "base/threading/thread_checker.h" |
| 25 #include "mojo/edk/base_edk/platform_handle_watcher_impl.h" |
23 #include "mojo/edk/base_edk/platform_task_runner_impl.h" | 26 #include "mojo/edk/base_edk/platform_task_runner_impl.h" |
24 #include "mojo/edk/embedder/embedder.h" | 27 #include "mojo/edk/embedder/embedder.h" |
25 #include "mojo/edk/embedder/platform_channel_pair.h" | 28 #include "mojo/edk/embedder/platform_channel_pair.h" |
26 #include "mojo/edk/embedder/simple_platform_support.h" | 29 #include "mojo/edk/embedder/simple_platform_support.h" |
27 #include "mojo/edk/embedder/slave_process_delegate.h" | 30 #include "mojo/edk/embedder/slave_process_delegate.h" |
| 31 #include "mojo/edk/platform/platform_handle_watcher.h" |
28 #include "mojo/edk/platform/scoped_platform_handle.h" | 32 #include "mojo/edk/platform/scoped_platform_handle.h" |
29 #include "mojo/edk/platform/task_runner.h" | 33 #include "mojo/edk/platform/task_runner.h" |
| 34 #include "mojo/edk/util/make_unique.h" |
30 #include "mojo/edk/util/ref_ptr.h" | 35 #include "mojo/edk/util/ref_ptr.h" |
31 #include "mojo/message_pump/message_pump_mojo.h" | 36 #include "mojo/message_pump/message_pump_mojo.h" |
32 #include "mojo/public/cpp/bindings/binding.h" | 37 #include "mojo/public/cpp/bindings/binding.h" |
33 #include "mojo/public/cpp/system/core.h" | 38 #include "mojo/public/cpp/system/core.h" |
34 #include "shell/child_controller.mojom.h" | 39 #include "shell/child_controller.mojom.h" |
35 #include "shell/child_switches.h" | 40 #include "shell/child_switches.h" |
36 #include "shell/init.h" | 41 #include "shell/init.h" |
37 #include "shell/native_application_support.h" | 42 #include "shell/native_application_support.h" |
38 | 43 |
| 44 using mojo::platform::PlatformHandleWatcher; |
39 using mojo::platform::ScopedPlatformHandle; | 45 using mojo::platform::ScopedPlatformHandle; |
| 46 using mojo::platform::TaskRunner; |
40 using mojo::util::MakeRefCounted; | 47 using mojo::util::MakeRefCounted; |
| 48 using mojo::util::MakeUnique; |
41 using mojo::util::RefPtr; | 49 using mojo::util::RefPtr; |
42 | 50 |
43 namespace shell { | 51 namespace shell { |
44 namespace { | 52 namespace { |
45 | 53 |
46 // Blocker --------------------------------------------------------------------- | 54 // Blocker --------------------------------------------------------------------- |
47 | 55 |
48 // Blocks a thread until another thread unblocks it, at which point it unblocks | 56 // Blocks a thread until another thread unblocks it, at which point it unblocks |
49 // and runs a closure provided by that thread. | 57 // and runs a closure provided by that thread. |
50 class Blocker { | 58 class Blocker { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 // TODO(vtl): Use make_unique when C++14 is available. | 111 // TODO(vtl): Use make_unique when C++14 is available. |
104 mojo::embedder::Init(std::unique_ptr<mojo::embedder::PlatformSupport>( | 112 mojo::embedder::Init(std::unique_ptr<mojo::embedder::PlatformSupport>( |
105 new mojo::embedder::SimplePlatformSupport())); | 113 new mojo::embedder::SimplePlatformSupport())); |
106 | 114 |
107 // Create and start our I/O thread. | 115 // Create and start our I/O thread. |
108 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); | 116 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); |
109 CHECK(io_thread_.StartWithOptions(io_thread_options)); | 117 CHECK(io_thread_.StartWithOptions(io_thread_options)); |
110 io_runner_ = MakeRefCounted<base_edk::PlatformTaskRunnerImpl>( | 118 io_runner_ = MakeRefCounted<base_edk::PlatformTaskRunnerImpl>( |
111 io_thread_.task_runner()); | 119 io_thread_.task_runner()); |
112 CHECK(io_runner_); | 120 CHECK(io_runner_); |
| 121 io_watcher_ = MakeUnique<base_edk::PlatformHandleWatcherImpl>( |
| 122 static_cast<base::MessageLoopForIO*>(io_thread_.message_loop())); |
113 | 123 |
114 // Create and start our controller thread. | 124 // Create and start our controller thread. |
115 base::Thread::Options controller_thread_options; | 125 base::Thread::Options controller_thread_options; |
116 controller_thread_options.message_loop_type = | 126 controller_thread_options.message_loop_type = |
117 base::MessageLoop::TYPE_CUSTOM; | 127 base::MessageLoop::TYPE_CUSTOM; |
118 controller_thread_options.message_pump_factory = | 128 controller_thread_options.message_pump_factory = |
119 base::Bind(&mojo::common::MessagePumpMojo::Create); | 129 base::Bind(&mojo::common::MessagePumpMojo::Create); |
120 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); | 130 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); |
121 controller_runner_ = MakeRefCounted<base_edk::PlatformTaskRunnerImpl>( | 131 controller_runner_ = MakeRefCounted<base_edk::PlatformTaskRunnerImpl>( |
122 controller_thread_.task_runner()); | 132 controller_thread_.task_runner()); |
123 CHECK(controller_runner_); | 133 CHECK(controller_runner_); |
124 | 134 |
125 mojo::embedder::InitIPCSupport(mojo::embedder::ProcessType::SLAVE, | 135 mojo::embedder::InitIPCSupport( |
126 controller_runner_.Clone(), this, | 136 mojo::embedder::ProcessType::SLAVE, controller_runner_.Clone(), this, |
127 io_runner_.Clone(), platform_handle.Pass()); | 137 io_runner_.Clone(), io_watcher_.get(), platform_handle.Pass()); |
128 } | 138 } |
129 | 139 |
130 void Shutdown() { | 140 void Shutdown() { |
131 Blocker blocker; | 141 Blocker blocker; |
132 shutdown_unblocker_ = blocker.GetUnblocker(); | 142 shutdown_unblocker_ = blocker.GetUnblocker(); |
133 controller_runner_->PostTask([this]() { ShutdownOnControllerThread(); }); | 143 controller_runner_->PostTask([this]() { ShutdownOnControllerThread(); }); |
134 blocker.Block(); | 144 blocker.Block(); |
135 } | 145 } |
136 | 146 |
137 const RefPtr<mojo::platform::TaskRunner>& controller_runner() const { | 147 const RefPtr<TaskRunner>& controller_runner() const { |
138 return controller_runner_; | 148 return controller_runner_; |
139 } | 149 } |
140 | 150 |
141 ChildControllerImpl* controller() const { return controller_.get(); } | 151 ChildControllerImpl* controller() const { return controller_.get(); } |
142 | 152 |
143 void set_controller(scoped_ptr<ChildControllerImpl> controller) { | 153 void set_controller(scoped_ptr<ChildControllerImpl> controller) { |
144 controller_ = controller.Pass(); | 154 controller_ = controller.Pass(); |
145 } | 155 } |
146 | 156 |
147 private: | 157 private: |
(...skipping 10 matching lines...) Expand all Loading... |
158 shutdown_unblocker_.Unblock(base::Closure()); | 168 shutdown_unblocker_.Unblock(base::Closure()); |
159 } | 169 } |
160 | 170 |
161 void OnMasterDisconnect() override { | 171 void OnMasterDisconnect() override { |
162 // We've lost the connection to the master process. This is not recoverable. | 172 // We've lost the connection to the master process. This is not recoverable. |
163 LOG(ERROR) << "Disconnected from master"; | 173 LOG(ERROR) << "Disconnected from master"; |
164 _exit(1); | 174 _exit(1); |
165 } | 175 } |
166 | 176 |
167 base::Thread io_thread_; | 177 base::Thread io_thread_; |
168 RefPtr<mojo::platform::TaskRunner> io_runner_; | 178 RefPtr<TaskRunner> io_runner_; |
| 179 std::unique_ptr<PlatformHandleWatcher> io_watcher_; |
169 | 180 |
170 base::Thread controller_thread_; | 181 base::Thread controller_thread_; |
171 RefPtr<mojo::platform::TaskRunner> controller_runner_; | 182 RefPtr<TaskRunner> controller_runner_; |
172 | 183 |
173 // Accessed only on the controller thread. | 184 // Accessed only on the controller thread. |
174 scoped_ptr<ChildControllerImpl> controller_; | 185 scoped_ptr<ChildControllerImpl> controller_; |
175 | 186 |
176 // Used to unblock the main thread on shutdown. | 187 // Used to unblock the main thread on shutdown. |
177 Blocker::Unblocker shutdown_unblocker_; | 188 Blocker::Unblocker shutdown_unblocker_; |
178 | 189 |
179 DISALLOW_COPY_AND_ASSIGN(AppContext); | 190 DISALLOW_COPY_AND_ASSIGN(AppContext); |
180 }; | 191 }; |
181 | 192 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 | 280 |
270 // We intentionally don't unload the native library as its lifetime is the | 281 // We intentionally don't unload the native library as its lifetime is the |
271 // same as that of the process. | 282 // same as that of the process. |
272 base::NativeLibrary app_library = LoadNativeApplication(app_path); | 283 base::NativeLibrary app_library = LoadNativeApplication(app_path); |
273 RunNativeApplication(app_library, application_request.Pass()); | 284 RunNativeApplication(app_library, application_request.Pass()); |
274 } | 285 } |
275 | 286 |
276 base::ThreadChecker thread_checker_; | 287 base::ThreadChecker thread_checker_; |
277 AppContext* const app_context_; | 288 AppContext* const app_context_; |
278 Blocker::Unblocker unblocker_; | 289 Blocker::Unblocker unblocker_; |
279 RefPtr<mojo::platform::TaskRunner> mojo_task_runner_; | 290 RefPtr<TaskRunner> mojo_task_runner_; |
280 StartAppCallback on_app_complete_; | 291 StartAppCallback on_app_complete_; |
281 | 292 |
282 mojo::embedder::ChannelInfo* channel_info_; | 293 mojo::embedder::ChannelInfo* channel_info_; |
283 mojo::Binding<ChildController> binding_; | 294 mojo::Binding<ChildController> binding_; |
284 | 295 |
285 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); | 296 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); |
286 }; | 297 }; |
287 | 298 |
288 } // namespace | 299 } // namespace |
289 } // namespace shell | 300 } // namespace shell |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 shell::ChildControllerImpl::Init(&app_context, child_connection_id, | 332 shell::ChildControllerImpl::Init(&app_context, child_connection_id, |
322 unblocker); | 333 unblocker); |
323 }); | 334 }); |
324 // This will block, then run whatever the controller wants. | 335 // This will block, then run whatever the controller wants. |
325 blocker.Block(); | 336 blocker.Block(); |
326 | 337 |
327 app_context.Shutdown(); | 338 app_context.Shutdown(); |
328 | 339 |
329 return 0; | 340 return 0; |
330 } | 341 } |
OLD | NEW |