| 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 "shell/app_child_process.h" | 5 #include "shell/app_child_process.h" |
| 6 | 6 |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.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/process_delegate.h" | 24 #include "mojo/edk/embedder/process_delegate.h" |
| 25 #include "mojo/edk/embedder/simple_platform_support.h" | 25 #include "mojo/edk/embedder/simple_platform_support.h" |
| 26 #include "mojo/public/cpp/system/core.h" | 26 #include "mojo/public/cpp/system/core.h" |
| 27 #include "shell/app_child_process.mojom.h" | 27 #include "shell/child_controller.mojom.h" |
| 28 #include "shell/native_application_support.h" | 28 #include "shell/native_application_support.h" |
| 29 | 29 |
| 30 namespace mojo { | 30 namespace mojo { |
| 31 namespace shell { | 31 namespace shell { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 // Blocker --------------------------------------------------------------------- | 35 // Blocker --------------------------------------------------------------------- |
| 36 | 36 |
| 37 // Blocks a thread until another thread unblocks it, at which point it unblocks | 37 // Blocks a thread until another thread unblocks it, at which point it unblocks |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 70 |
| 71 private: | 71 private: |
| 72 base::WaitableEvent event_; | 72 base::WaitableEvent event_; |
| 73 base::Closure run_after_; | 73 base::Closure run_after_; |
| 74 | 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(Blocker); | 75 DISALLOW_COPY_AND_ASSIGN(Blocker); |
| 76 }; | 76 }; |
| 77 | 77 |
| 78 // AppContext ------------------------------------------------------------------ | 78 // AppContext ------------------------------------------------------------------ |
| 79 | 79 |
| 80 class AppChildControllerImpl; | 80 class ChildControllerImpl; |
| 81 | 81 |
| 82 // Should be created and initialized on the main thread. | 82 // Should be created and initialized on the main thread. |
| 83 class AppContext : public embedder::ProcessDelegate { | 83 class AppContext : public embedder::ProcessDelegate { |
| 84 public: | 84 public: |
| 85 AppContext() | 85 AppContext() |
| 86 : io_thread_("io_thread"), controller_thread_("controller_thread") {} | 86 : io_thread_("io_thread"), controller_thread_("controller_thread") {} |
| 87 ~AppContext() override {} | 87 ~AppContext() override {} |
| 88 | 88 |
| 89 void Init() { | 89 void Init() { |
| 90 // Initialize Mojo before starting any threads. | 90 // Initialize Mojo before starting any threads. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 120 base::Unretained(this))); | 120 base::Unretained(this))); |
| 121 blocker.Block(); | 121 blocker.Block(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 base::SingleThreadTaskRunner* io_runner() const { return io_runner_.get(); } | 124 base::SingleThreadTaskRunner* io_runner() const { return io_runner_.get(); } |
| 125 | 125 |
| 126 base::SingleThreadTaskRunner* controller_runner() const { | 126 base::SingleThreadTaskRunner* controller_runner() const { |
| 127 return controller_runner_.get(); | 127 return controller_runner_.get(); |
| 128 } | 128 } |
| 129 | 129 |
| 130 AppChildControllerImpl* controller() const { return controller_.get(); } | 130 ChildControllerImpl* controller() const { return controller_.get(); } |
| 131 | 131 |
| 132 void set_controller(scoped_ptr<AppChildControllerImpl> controller) { | 132 void set_controller(scoped_ptr<ChildControllerImpl> controller) { |
| 133 controller_ = controller.Pass(); | 133 controller_ = controller.Pass(); |
| 134 } | 134 } |
| 135 | 135 |
| 136 private: | 136 private: |
| 137 void ShutdownOnControllerThread() { | 137 void ShutdownOnControllerThread() { |
| 138 // First, destroy the controller. | 138 // First, destroy the controller. |
| 139 controller_.reset(); | 139 controller_.reset(); |
| 140 | 140 |
| 141 // Next shutdown IPC. We'll unblock the main thread in OnShutdownComplete(). | 141 // Next shutdown IPC. We'll unblock the main thread in OnShutdownComplete(). |
| 142 embedder::ShutdownIPCSupport(); | 142 embedder::ShutdownIPCSupport(); |
| 143 } | 143 } |
| 144 | 144 |
| 145 // ProcessDelegate implementation. | 145 // ProcessDelegate implementation. |
| 146 void OnShutdownComplete() override { | 146 void OnShutdownComplete() override { |
| 147 shutdown_unblocker_.Unblock(base::Closure()); | 147 shutdown_unblocker_.Unblock(base::Closure()); |
| 148 } | 148 } |
| 149 | 149 |
| 150 base::Thread io_thread_; | 150 base::Thread io_thread_; |
| 151 scoped_refptr<base::SingleThreadTaskRunner> io_runner_; | 151 scoped_refptr<base::SingleThreadTaskRunner> io_runner_; |
| 152 | 152 |
| 153 base::Thread controller_thread_; | 153 base::Thread controller_thread_; |
| 154 scoped_refptr<base::SingleThreadTaskRunner> controller_runner_; | 154 scoped_refptr<base::SingleThreadTaskRunner> controller_runner_; |
| 155 | 155 |
| 156 // Accessed only on the controller thread. | 156 // Accessed only on the controller thread. |
| 157 scoped_ptr<AppChildControllerImpl> controller_; | 157 scoped_ptr<ChildControllerImpl> controller_; |
| 158 | 158 |
| 159 // Used to unblock the main thread on shutdown. | 159 // Used to unblock the main thread on shutdown. |
| 160 Blocker::Unblocker shutdown_unblocker_; | 160 Blocker::Unblocker shutdown_unblocker_; |
| 161 | 161 |
| 162 DISALLOW_COPY_AND_ASSIGN(AppContext); | 162 DISALLOW_COPY_AND_ASSIGN(AppContext); |
| 163 }; | 163 }; |
| 164 | 164 |
| 165 // AppChildControllerImpl ------------------------------------------------------ | 165 // ChildControllerImpl --------------------------------------------------------- |
| 166 | 166 |
| 167 class AppChildControllerImpl : public AppChildController, public ErrorHandler { | 167 class ChildControllerImpl : public ChildController, public ErrorHandler { |
| 168 public: | 168 public: |
| 169 ~AppChildControllerImpl() override { | 169 ~ChildControllerImpl() override { |
| 170 DCHECK(thread_checker_.CalledOnValidThread()); | 170 DCHECK(thread_checker_.CalledOnValidThread()); |
| 171 | 171 |
| 172 // TODO(vtl): Pass in the result from |MainMain()|. | 172 // TODO(vtl): Pass in the result from |MainMain()|. |
| 173 on_app_complete_.Run(MOJO_RESULT_UNIMPLEMENTED); | 173 on_app_complete_.Run(MOJO_RESULT_UNIMPLEMENTED); |
| 174 } | 174 } |
| 175 | 175 |
| 176 // To be executed on the controller thread. Creates the |AppChildController|, | 176 // To be executed on the controller thread. Creates the |ChildController|, |
| 177 // etc. | 177 // etc. |
| 178 static void Init(AppContext* app_context, | 178 static void Init(AppContext* app_context, |
| 179 embedder::ScopedPlatformHandle platform_channel, | 179 embedder::ScopedPlatformHandle platform_channel, |
| 180 const Blocker::Unblocker& unblocker) { | 180 const Blocker::Unblocker& unblocker) { |
| 181 DCHECK(app_context); | 181 DCHECK(app_context); |
| 182 DCHECK(platform_channel.is_valid()); | 182 DCHECK(platform_channel.is_valid()); |
| 183 | 183 |
| 184 DCHECK(!app_context->controller()); | 184 DCHECK(!app_context->controller()); |
| 185 | 185 |
| 186 scoped_ptr<AppChildControllerImpl> impl( | 186 scoped_ptr<ChildControllerImpl> impl( |
| 187 new AppChildControllerImpl(app_context, unblocker)); | 187 new ChildControllerImpl(app_context, unblocker)); |
| 188 | 188 |
| 189 ScopedMessagePipeHandle host_message_pipe(embedder::CreateChannel( | 189 ScopedMessagePipeHandle host_message_pipe(embedder::CreateChannel( |
| 190 platform_channel.Pass(), app_context->io_runner(), | 190 platform_channel.Pass(), app_context->io_runner(), |
| 191 base::Bind(&AppChildControllerImpl::DidCreateChannel, | 191 base::Bind(&ChildControllerImpl::DidCreateChannel, |
| 192 base::Unretained(impl.get())), | 192 base::Unretained(impl.get())), |
| 193 base::MessageLoopProxy::current())); | 193 base::MessageLoopProxy::current())); |
| 194 | 194 |
| 195 impl->Bind(host_message_pipe.Pass()); | 195 impl->Bind(host_message_pipe.Pass()); |
| 196 | 196 |
| 197 app_context->set_controller(impl.Pass()); | 197 app_context->set_controller(impl.Pass()); |
| 198 } | 198 } |
| 199 | 199 |
| 200 void Bind(ScopedMessagePipeHandle handle) { binding_.Bind(handle.Pass()); } | 200 void Bind(ScopedMessagePipeHandle handle) { binding_.Bind(handle.Pass()); } |
| 201 | 201 |
| 202 // |ErrorHandler| methods: | 202 // |ErrorHandler| methods: |
| 203 void OnConnectionError() override { | 203 void OnConnectionError() override { |
| 204 // A connection error means the connection to the shell is lost. This is not | 204 // A connection error means the connection to the shell is lost. This is not |
| 205 // recoverable. | 205 // recoverable. |
| 206 LOG(ERROR) << "Connection error to the shell."; | 206 LOG(ERROR) << "Connection error to the shell."; |
| 207 _exit(1); | 207 _exit(1); |
| 208 } | 208 } |
| 209 | 209 |
| 210 // |AppChildController| methods: | 210 // |ChildController| methods: |
| 211 void StartApp(const String& app_path, | 211 void StartApp(const String& app_path, |
| 212 bool clean_app_path, | 212 bool clean_app_path, |
| 213 InterfaceRequest<Application> application_request, | 213 InterfaceRequest<Application> application_request, |
| 214 const StartAppCallback& on_app_complete) override { | 214 const StartAppCallback& on_app_complete) override { |
| 215 DVLOG(2) << "AppChildControllerImpl::StartApp(" << app_path << ", ...)"; | 215 DVLOG(2) << "ChildControllerImpl::StartApp(" << app_path << ", ...)"; |
| 216 DCHECK(thread_checker_.CalledOnValidThread()); | 216 DCHECK(thread_checker_.CalledOnValidThread()); |
| 217 | 217 |
| 218 on_app_complete_ = on_app_complete; | 218 on_app_complete_ = on_app_complete; |
| 219 unblocker_.Unblock(base::Bind(&AppChildControllerImpl::StartAppOnMainThread, | 219 unblocker_.Unblock(base::Bind(&ChildControllerImpl::StartAppOnMainThread, |
| 220 base::FilePath::FromUTF8Unsafe(app_path), | 220 base::FilePath::FromUTF8Unsafe(app_path), |
| 221 clean_app_path | 221 clean_app_path |
| 222 ? NativeApplicationCleanup::DELETE | 222 ? NativeApplicationCleanup::DELETE |
| 223 : NativeApplicationCleanup::DONT_DELETE, | 223 : NativeApplicationCleanup::DONT_DELETE, |
| 224 base::Passed(&application_request))); | 224 base::Passed(&application_request))); |
| 225 } | 225 } |
| 226 | 226 |
| 227 void ExitNow(int32_t exit_code) override { | 227 void ExitNow(int32_t exit_code) override { |
| 228 DVLOG(2) << "AppChildControllerImpl::ExitNow(" << exit_code << ")"; | 228 DVLOG(2) << "ChildControllerImpl::ExitNow(" << exit_code << ")"; |
| 229 _exit(exit_code); | 229 _exit(exit_code); |
| 230 } | 230 } |
| 231 | 231 |
| 232 private: | 232 private: |
| 233 AppChildControllerImpl(AppContext* app_context, | 233 ChildControllerImpl(AppContext* app_context, |
| 234 const Blocker::Unblocker& unblocker) | 234 const Blocker::Unblocker& unblocker) |
| 235 : app_context_(app_context), | 235 : app_context_(app_context), |
| 236 unblocker_(unblocker), | 236 unblocker_(unblocker), |
| 237 channel_info_(nullptr), | 237 channel_info_(nullptr), |
| 238 binding_(this) { | 238 binding_(this) { |
| 239 binding_.set_error_handler(this); | 239 binding_.set_error_handler(this); |
| 240 } | 240 } |
| 241 | 241 |
| 242 // Callback for |embedder::CreateChannel()|. | 242 // Callback for |embedder::CreateChannel()|. |
| 243 void DidCreateChannel(embedder::ChannelInfo* channel_info) { | 243 void DidCreateChannel(embedder::ChannelInfo* channel_info) { |
| 244 DVLOG(2) << "AppChildControllerImpl::DidCreateChannel()"; | 244 DVLOG(2) << "ChildControllerImpl::DidCreateChannel()"; |
| 245 DCHECK(thread_checker_.CalledOnValidThread()); | 245 DCHECK(thread_checker_.CalledOnValidThread()); |
| 246 channel_info_ = channel_info; | 246 channel_info_ = channel_info; |
| 247 } | 247 } |
| 248 | 248 |
| 249 static void StartAppOnMainThread( | 249 static void StartAppOnMainThread( |
| 250 const base::FilePath& app_path, | 250 const base::FilePath& app_path, |
| 251 NativeApplicationCleanup cleanup, | 251 NativeApplicationCleanup cleanup, |
| 252 InterfaceRequest<Application> application_request) { | 252 InterfaceRequest<Application> application_request) { |
| 253 // TODO(vtl): This is copied from in_process_native_runner.cc. | 253 // TODO(vtl): This is copied from in_process_native_runner.cc. |
| 254 DVLOG(2) << "Loading/running Mojo app from " << app_path.value() | 254 DVLOG(2) << "Loading/running Mojo app from " << app_path.value() |
| 255 << " out of process"; | 255 << " out of process"; |
| 256 | 256 |
| 257 // We intentionally don't unload the native library as its lifetime is the | 257 // We intentionally don't unload the native library as its lifetime is the |
| 258 // same as that of the process. | 258 // same as that of the process. |
| 259 base::NativeLibrary app_library = LoadNativeApplication(app_path, cleanup); | 259 base::NativeLibrary app_library = LoadNativeApplication(app_path, cleanup); |
| 260 RunNativeApplication(app_library, application_request.Pass()); | 260 RunNativeApplication(app_library, application_request.Pass()); |
| 261 } | 261 } |
| 262 | 262 |
| 263 base::ThreadChecker thread_checker_; | 263 base::ThreadChecker thread_checker_; |
| 264 AppContext* const app_context_; | 264 AppContext* const app_context_; |
| 265 Blocker::Unblocker unblocker_; | 265 Blocker::Unblocker unblocker_; |
| 266 StartAppCallback on_app_complete_; | 266 StartAppCallback on_app_complete_; |
| 267 | 267 |
| 268 embedder::ChannelInfo* channel_info_; | 268 embedder::ChannelInfo* channel_info_; |
| 269 Binding<AppChildController> binding_; | 269 Binding<ChildController> binding_; |
| 270 | 270 |
| 271 DISALLOW_COPY_AND_ASSIGN(AppChildControllerImpl); | 271 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); |
| 272 }; | 272 }; |
| 273 | 273 |
| 274 } // namespace | 274 } // namespace |
| 275 | 275 |
| 276 // AppChildProcess ------------------------------------------------------------- | 276 // AppChildProcess ------------------------------------------------------------- |
| 277 | 277 |
| 278 AppChildProcess::AppChildProcess() { | 278 AppChildProcess::AppChildProcess() { |
| 279 } | 279 } |
| 280 | 280 |
| 281 AppChildProcess::~AppChildProcess() { | 281 AppChildProcess::~AppChildProcess() { |
| 282 } | 282 } |
| 283 | 283 |
| 284 void AppChildProcess::Main() { | 284 void AppChildProcess::Main() { |
| 285 DVLOG(2) << "AppChildProcess::Main()"; | 285 DVLOG(2) << "AppChildProcess::Main()"; |
| 286 | 286 |
| 287 DCHECK(!base::MessageLoop::current()); | 287 DCHECK(!base::MessageLoop::current()); |
| 288 | 288 |
| 289 AppContext app_context; | 289 AppContext app_context; |
| 290 app_context.Init(); | 290 app_context.Init(); |
| 291 | 291 |
| 292 Blocker blocker; | 292 Blocker blocker; |
| 293 app_context.controller_runner()->PostTask( | 293 app_context.controller_runner()->PostTask( |
| 294 FROM_HERE, | 294 FROM_HERE, |
| 295 base::Bind(&AppChildControllerImpl::Init, base::Unretained(&app_context), | 295 base::Bind(&ChildControllerImpl::Init, base::Unretained(&app_context), |
| 296 base::Passed(platform_channel()), blocker.GetUnblocker())); | 296 base::Passed(platform_channel()), blocker.GetUnblocker())); |
| 297 // This will block, then run whatever the controller wants. | 297 // This will block, then run whatever the controller wants. |
| 298 blocker.Block(); | 298 blocker.Block(); |
| 299 | 299 |
| 300 app_context.Shutdown(); | 300 app_context.Shutdown(); |
| 301 } | 301 } |
| 302 | 302 |
| 303 } // namespace shell | 303 } // namespace shell |
| 304 } // namespace mojo | 304 } // namespace mojo |
| OLD | NEW |