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" |
(...skipping 13 matching lines...) Expand all Loading... |
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" | 25 #include "mojo/edk/embedder/process_delegate.h" |
26 #include "mojo/edk/embedder/scoped_platform_handle.h" | 26 #include "mojo/edk/embedder/scoped_platform_handle.h" |
27 #include "mojo/edk/embedder/simple_platform_support.h" | 27 #include "mojo/edk/embedder/simple_platform_support.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/init.h" | 30 #include "shell/init.h" |
31 #include "shell/native_application_support.h" | 31 #include "shell/native_application_support.h" |
32 #include "shell/switches.h" | 32 #include "shell/switches.h" |
33 | 33 |
34 namespace mojo { | |
35 namespace shell { | 34 namespace shell { |
36 | |
37 namespace { | 35 namespace { |
38 | 36 |
39 // Blocker --------------------------------------------------------------------- | 37 // Blocker --------------------------------------------------------------------- |
40 | 38 |
41 // Blocks a thread until another thread unblocks it, at which point it unblocks | 39 // Blocks a thread until another thread unblocks it, at which point it unblocks |
42 // and runs a closure provided by that thread. | 40 // and runs a closure provided by that thread. |
43 class Blocker { | 41 class Blocker { |
44 public: | 42 public: |
45 class Unblocker { | 43 class Unblocker { |
46 public: | 44 public: |
(...skipping 30 matching lines...) Expand all Loading... |
77 base::Closure run_after_; | 75 base::Closure run_after_; |
78 | 76 |
79 DISALLOW_COPY_AND_ASSIGN(Blocker); | 77 DISALLOW_COPY_AND_ASSIGN(Blocker); |
80 }; | 78 }; |
81 | 79 |
82 // AppContext ------------------------------------------------------------------ | 80 // AppContext ------------------------------------------------------------------ |
83 | 81 |
84 class ChildControllerImpl; | 82 class ChildControllerImpl; |
85 | 83 |
86 // Should be created and initialized on the main thread. | 84 // Should be created and initialized on the main thread. |
87 class AppContext : public embedder::ProcessDelegate { | 85 class AppContext : public mojo::embedder::ProcessDelegate { |
88 public: | 86 public: |
89 AppContext() | 87 AppContext() |
90 : io_thread_("io_thread"), controller_thread_("controller_thread") {} | 88 : io_thread_("io_thread"), controller_thread_("controller_thread") {} |
91 ~AppContext() override {} | 89 ~AppContext() override {} |
92 | 90 |
93 void Init() { | 91 void Init() { |
94 // Initialize Mojo before starting any threads. | 92 // Initialize Mojo before starting any threads. |
95 embedder::Init(make_scoped_ptr(new embedder::SimplePlatformSupport())); | 93 mojo::embedder::Init( |
| 94 make_scoped_ptr(new mojo::embedder::SimplePlatformSupport())); |
96 | 95 |
97 // Create and start our I/O thread. | 96 // Create and start our I/O thread. |
98 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); | 97 base::Thread::Options io_thread_options(base::MessageLoop::TYPE_IO, 0); |
99 CHECK(io_thread_.StartWithOptions(io_thread_options)); | 98 CHECK(io_thread_.StartWithOptions(io_thread_options)); |
100 io_runner_ = io_thread_.message_loop_proxy().get(); | 99 io_runner_ = io_thread_.message_loop_proxy().get(); |
101 CHECK(io_runner_.get()); | 100 CHECK(io_runner_.get()); |
102 | 101 |
103 // Create and start our controller thread. | 102 // Create and start our controller thread. |
104 base::Thread::Options controller_thread_options; | 103 base::Thread::Options controller_thread_options; |
105 controller_thread_options.message_loop_type = | 104 controller_thread_options.message_loop_type = |
106 base::MessageLoop::TYPE_CUSTOM; | 105 base::MessageLoop::TYPE_CUSTOM; |
107 controller_thread_options.message_pump_factory = | 106 controller_thread_options.message_pump_factory = |
108 base::Bind(&common::MessagePumpMojo::Create); | 107 base::Bind(&mojo::common::MessagePumpMojo::Create); |
109 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); | 108 CHECK(controller_thread_.StartWithOptions(controller_thread_options)); |
110 controller_runner_ = controller_thread_.message_loop_proxy().get(); | 109 controller_runner_ = controller_thread_.message_loop_proxy().get(); |
111 CHECK(controller_runner_.get()); | 110 CHECK(controller_runner_.get()); |
112 | 111 |
113 // TODO(vtl): This should be SLAVE, not NONE. | 112 // TODO(vtl): This should be SLAVE, not NONE. |
114 embedder::InitIPCSupport(embedder::ProcessType::NONE, controller_runner_, | 113 mojo::embedder::InitIPCSupport(mojo::embedder::ProcessType::NONE, |
115 this, io_runner_, | 114 controller_runner_, this, io_runner_, |
116 embedder::ScopedPlatformHandle()); | 115 mojo::embedder::ScopedPlatformHandle()); |
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 } |
127 | 126 |
128 base::SingleThreadTaskRunner* io_runner() const { return io_runner_.get(); } | 127 base::SingleThreadTaskRunner* io_runner() const { return io_runner_.get(); } |
129 | 128 |
130 base::SingleThreadTaskRunner* controller_runner() const { | 129 base::SingleThreadTaskRunner* controller_runner() const { |
131 return controller_runner_.get(); | 130 return controller_runner_.get(); |
132 } | 131 } |
133 | 132 |
134 ChildControllerImpl* controller() const { return controller_.get(); } | 133 ChildControllerImpl* controller() const { return controller_.get(); } |
135 | 134 |
136 void set_controller(scoped_ptr<ChildControllerImpl> controller) { | 135 void set_controller(scoped_ptr<ChildControllerImpl> controller) { |
137 controller_ = controller.Pass(); | 136 controller_ = controller.Pass(); |
138 } | 137 } |
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 embedder::ShutdownIPCSupport(); | 145 mojo::embedder::ShutdownIPCSupport(); |
147 } | 146 } |
148 | 147 |
149 // ProcessDelegate implementation. | 148 // ProcessDelegate 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 |
154 base::Thread io_thread_; | 153 base::Thread io_thread_; |
155 scoped_refptr<base::SingleThreadTaskRunner> io_runner_; | 154 scoped_refptr<base::SingleThreadTaskRunner> io_runner_; |
156 | 155 |
157 base::Thread controller_thread_; | 156 base::Thread controller_thread_; |
158 scoped_refptr<base::SingleThreadTaskRunner> controller_runner_; | 157 scoped_refptr<base::SingleThreadTaskRunner> controller_runner_; |
159 | 158 |
160 // Accessed only on the controller thread. | 159 // Accessed only on the controller thread. |
161 scoped_ptr<ChildControllerImpl> controller_; | 160 scoped_ptr<ChildControllerImpl> controller_; |
162 | 161 |
163 // Used to unblock the main thread on shutdown. | 162 // Used to unblock the main thread on shutdown. |
164 Blocker::Unblocker shutdown_unblocker_; | 163 Blocker::Unblocker shutdown_unblocker_; |
165 | 164 |
166 DISALLOW_COPY_AND_ASSIGN(AppContext); | 165 DISALLOW_COPY_AND_ASSIGN(AppContext); |
167 }; | 166 }; |
168 | 167 |
169 // ChildControllerImpl --------------------------------------------------------- | 168 // ChildControllerImpl --------------------------------------------------------- |
170 | 169 |
171 class ChildControllerImpl : public ChildController, public ErrorHandler { | 170 class ChildControllerImpl : public ChildController, public mojo::ErrorHandler { |
172 public: | 171 public: |
173 ~ChildControllerImpl() override { | 172 ~ChildControllerImpl() override { |
174 DCHECK(thread_checker_.CalledOnValidThread()); | 173 DCHECK(thread_checker_.CalledOnValidThread()); |
175 | 174 |
176 // TODO(vtl): Pass in the result from |MainMain()|. | 175 // TODO(vtl): Pass in the result from |MainMain()|. |
177 on_app_complete_.Run(MOJO_RESULT_UNIMPLEMENTED); | 176 on_app_complete_.Run(MOJO_RESULT_UNIMPLEMENTED); |
178 } | 177 } |
179 | 178 |
180 // To be executed on the controller thread. Creates the |ChildController|, | 179 // To be executed on the controller thread. Creates the |ChildController|, |
181 // etc. | 180 // etc. |
182 static void Init(AppContext* app_context, | 181 static void Init(AppContext* app_context, |
183 embedder::ScopedPlatformHandle platform_channel, | 182 mojo::embedder::ScopedPlatformHandle platform_channel, |
184 const Blocker::Unblocker& unblocker) { | 183 const Blocker::Unblocker& unblocker) { |
185 DCHECK(app_context); | 184 DCHECK(app_context); |
186 DCHECK(platform_channel.is_valid()); | 185 DCHECK(platform_channel.is_valid()); |
187 | 186 |
188 DCHECK(!app_context->controller()); | 187 DCHECK(!app_context->controller()); |
189 | 188 |
190 scoped_ptr<ChildControllerImpl> impl( | 189 scoped_ptr<ChildControllerImpl> impl( |
191 new ChildControllerImpl(app_context, unblocker)); | 190 new ChildControllerImpl(app_context, unblocker)); |
192 | 191 |
193 ScopedMessagePipeHandle host_message_pipe(embedder::CreateChannel( | 192 mojo::ScopedMessagePipeHandle host_message_pipe( |
194 platform_channel.Pass(), app_context->io_runner(), | 193 mojo::embedder::CreateChannel( |
195 base::Bind(&ChildControllerImpl::DidCreateChannel, | 194 platform_channel.Pass(), app_context->io_runner(), |
196 base::Unretained(impl.get())), | 195 base::Bind(&ChildControllerImpl::DidCreateChannel, |
197 base::MessageLoopProxy::current())); | 196 base::Unretained(impl.get())), |
| 197 base::MessageLoopProxy::current())); |
198 | 198 |
199 impl->Bind(host_message_pipe.Pass()); | 199 impl->Bind(host_message_pipe.Pass()); |
200 | 200 |
201 app_context->set_controller(impl.Pass()); | 201 app_context->set_controller(impl.Pass()); |
202 } | 202 } |
203 | 203 |
204 void Bind(ScopedMessagePipeHandle handle) { binding_.Bind(handle.Pass()); } | 204 void Bind(mojo::ScopedMessagePipeHandle handle) { |
| 205 binding_.Bind(handle.Pass()); |
| 206 } |
205 | 207 |
206 // |ErrorHandler| methods: | 208 // |mojo::ErrorHandler| methods: |
207 void OnConnectionError() override { | 209 void OnConnectionError() override { |
208 // A connection error means the connection to the shell is lost. This is not | 210 // A connection error means the connection to the shell is lost. This is not |
209 // recoverable. | 211 // recoverable. |
210 LOG(ERROR) << "Connection error to the shell."; | 212 LOG(ERROR) << "Connection error to the shell."; |
211 _exit(1); | 213 _exit(1); |
212 } | 214 } |
213 | 215 |
214 // |ChildController| methods: | 216 // |ChildController| methods: |
215 void StartApp(const String& app_path, | 217 void StartApp(const mojo::String& app_path, |
216 bool clean_app_path, | 218 bool clean_app_path, |
217 InterfaceRequest<Application> application_request, | 219 mojo::InterfaceRequest<mojo::Application> application_request, |
218 const StartAppCallback& on_app_complete) override { | 220 const StartAppCallback& on_app_complete) override { |
219 DVLOG(2) << "ChildControllerImpl::StartApp(" << app_path << ", ...)"; | 221 DVLOG(2) << "ChildControllerImpl::StartApp(" << app_path << ", ...)"; |
220 DCHECK(thread_checker_.CalledOnValidThread()); | 222 DCHECK(thread_checker_.CalledOnValidThread()); |
221 | 223 |
222 on_app_complete_ = on_app_complete; | 224 on_app_complete_ = on_app_complete; |
223 unblocker_.Unblock(base::Bind(&ChildControllerImpl::StartAppOnMainThread, | 225 unblocker_.Unblock(base::Bind(&ChildControllerImpl::StartAppOnMainThread, |
224 base::FilePath::FromUTF8Unsafe(app_path), | 226 base::FilePath::FromUTF8Unsafe(app_path), |
225 clean_app_path | 227 clean_app_path |
226 ? NativeApplicationCleanup::DELETE | 228 ? NativeApplicationCleanup::DELETE |
227 : NativeApplicationCleanup::DONT_DELETE, | 229 : NativeApplicationCleanup::DONT_DELETE, |
228 base::Passed(&application_request))); | 230 base::Passed(&application_request))); |
229 } | 231 } |
230 | 232 |
231 void ExitNow(int32_t exit_code) override { | 233 void ExitNow(int32_t exit_code) override { |
232 DVLOG(2) << "ChildControllerImpl::ExitNow(" << exit_code << ")"; | 234 DVLOG(2) << "ChildControllerImpl::ExitNow(" << exit_code << ")"; |
233 _exit(exit_code); | 235 _exit(exit_code); |
234 } | 236 } |
235 | 237 |
236 private: | 238 private: |
237 ChildControllerImpl(AppContext* app_context, | 239 ChildControllerImpl(AppContext* app_context, |
238 const Blocker::Unblocker& unblocker) | 240 const Blocker::Unblocker& unblocker) |
239 : app_context_(app_context), | 241 : app_context_(app_context), |
240 unblocker_(unblocker), | 242 unblocker_(unblocker), |
241 channel_info_(nullptr), | 243 channel_info_(nullptr), |
242 binding_(this) { | 244 binding_(this) { |
243 binding_.set_error_handler(this); | 245 binding_.set_error_handler(this); |
244 } | 246 } |
245 | 247 |
246 // Callback for |embedder::CreateChannel()|. | 248 // Callback for |mojo::embedder::CreateChannel()|. |
247 void DidCreateChannel(embedder::ChannelInfo* channel_info) { | 249 void DidCreateChannel(mojo::embedder::ChannelInfo* channel_info) { |
248 DVLOG(2) << "ChildControllerImpl::DidCreateChannel()"; | 250 DVLOG(2) << "ChildControllerImpl::DidCreateChannel()"; |
249 DCHECK(thread_checker_.CalledOnValidThread()); | 251 DCHECK(thread_checker_.CalledOnValidThread()); |
250 channel_info_ = channel_info; | 252 channel_info_ = channel_info; |
251 } | 253 } |
252 | 254 |
253 static void StartAppOnMainThread( | 255 static void StartAppOnMainThread( |
254 const base::FilePath& app_path, | 256 const base::FilePath& app_path, |
255 NativeApplicationCleanup cleanup, | 257 NativeApplicationCleanup cleanup, |
256 InterfaceRequest<Application> application_request) { | 258 mojo::InterfaceRequest<mojo::Application> application_request) { |
257 // TODO(vtl): This is copied from in_process_native_runner.cc. | 259 // TODO(vtl): This is copied from in_process_native_runner.cc. |
258 DVLOG(2) << "Loading/running Mojo app from " << app_path.value() | 260 DVLOG(2) << "Loading/running Mojo app from " << app_path.value() |
259 << " out of process"; | 261 << " out of process"; |
260 | 262 |
261 // We intentionally don't unload the native library as its lifetime is the | 263 // We intentionally don't unload the native library as its lifetime is the |
262 // same as that of the process. | 264 // same as that of the process. |
263 base::NativeLibrary app_library = LoadNativeApplication(app_path, cleanup); | 265 base::NativeLibrary app_library = LoadNativeApplication(app_path, cleanup); |
264 RunNativeApplication(app_library, application_request.Pass()); | 266 RunNativeApplication(app_library, application_request.Pass()); |
265 } | 267 } |
266 | 268 |
267 base::ThreadChecker thread_checker_; | 269 base::ThreadChecker thread_checker_; |
268 AppContext* const app_context_; | 270 AppContext* const app_context_; |
269 Blocker::Unblocker unblocker_; | 271 Blocker::Unblocker unblocker_; |
270 StartAppCallback on_app_complete_; | 272 StartAppCallback on_app_complete_; |
271 | 273 |
272 embedder::ChannelInfo* channel_info_; | 274 mojo::embedder::ChannelInfo* channel_info_; |
273 Binding<ChildController> binding_; | 275 mojo::Binding<ChildController> binding_; |
274 | 276 |
275 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); | 277 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); |
276 }; | 278 }; |
277 | 279 |
278 } // namespace | 280 } // namespace |
279 | |
280 } // namespace shell | 281 } // namespace shell |
281 } // namespace mojo | |
282 | 282 |
283 int main(int argc, char** argv) { | 283 int main(int argc, char** argv) { |
284 base::AtExitManager at_exit; | 284 base::AtExitManager at_exit; |
285 base::CommandLine::Init(argc, argv); | 285 base::CommandLine::Init(argc, argv); |
286 | 286 |
287 mojo::shell::InitializeLogging(); | 287 shell::InitializeLogging(); |
288 | 288 |
289 // Make sure that we're really meant to be invoked as the child process. | 289 // Make sure that we're really meant to be invoked as the child process. |
290 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( | 290 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
291 switches::kChildProcess)); | 291 switches::kChildProcess)); |
292 | 292 |
293 mojo::embedder::ScopedPlatformHandle platform_channel = | 293 mojo::embedder::ScopedPlatformHandle platform_channel = |
294 mojo::embedder::PlatformChannelPair::PassClientHandleFromParentProcess( | 294 mojo::embedder::PlatformChannelPair::PassClientHandleFromParentProcess( |
295 *base::CommandLine::ForCurrentProcess()); | 295 *base::CommandLine::ForCurrentProcess()); |
296 CHECK(platform_channel.is_valid()); | 296 CHECK(platform_channel.is_valid()); |
297 | 297 |
298 mojo::shell::AppContext app_context; | 298 shell::AppContext app_context; |
299 app_context.Init(); | 299 app_context.Init(); |
300 | 300 |
301 mojo::shell::Blocker blocker; | 301 shell::Blocker blocker; |
302 app_context.controller_runner()->PostTask( | 302 app_context.controller_runner()->PostTask( |
303 FROM_HERE, | 303 FROM_HERE, |
304 base::Bind(&mojo::shell::ChildControllerImpl::Init, | 304 base::Bind(&shell::ChildControllerImpl::Init, |
305 base::Unretained(&app_context), | 305 base::Unretained(&app_context), |
306 base::Passed(&platform_channel), blocker.GetUnblocker())); | 306 base::Passed(&platform_channel), blocker.GetUnblocker())); |
307 // This will block, then run whatever the controller wants. | 307 // This will block, then run whatever the controller wants. |
308 blocker.Block(); | 308 blocker.Block(); |
309 | 309 |
310 app_context.Shutdown(); | 310 app_context.Shutdown(); |
311 | 311 |
312 return 0; | 312 return 0; |
313 } | 313 } |
OLD | NEW |