| 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 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 scoped_ptr<ChildControllerImpl> controller_; | 168 scoped_ptr<ChildControllerImpl> controller_; |
| 169 | 169 |
| 170 // Used to unblock the main thread on shutdown. | 170 // Used to unblock the main thread on shutdown. |
| 171 Blocker::Unblocker shutdown_unblocker_; | 171 Blocker::Unblocker shutdown_unblocker_; |
| 172 | 172 |
| 173 DISALLOW_COPY_AND_ASSIGN(AppContext); | 173 DISALLOW_COPY_AND_ASSIGN(AppContext); |
| 174 }; | 174 }; |
| 175 | 175 |
| 176 // ChildControllerImpl --------------------------------------------------------- | 176 // ChildControllerImpl --------------------------------------------------------- |
| 177 | 177 |
| 178 class ChildControllerImpl : public ChildController, public mojo::ErrorHandler { | 178 class ChildControllerImpl : public ChildController { |
| 179 public: | 179 public: |
| 180 ~ChildControllerImpl() override { | 180 ~ChildControllerImpl() override { |
| 181 DCHECK(thread_checker_.CalledOnValidThread()); | 181 DCHECK(thread_checker_.CalledOnValidThread()); |
| 182 | 182 |
| 183 // TODO(vtl): Pass in the result from |MainMain()|. | 183 // TODO(vtl): Pass in the result from |MainMain()|. |
| 184 on_app_complete_.Run(MOJO_RESULT_UNIMPLEMENTED); | 184 on_app_complete_.Run(MOJO_RESULT_UNIMPLEMENTED); |
| 185 } | 185 } |
| 186 | 186 |
| 187 // To be executed on the controller thread. Creates the |ChildController|, | 187 // To be executed on the controller thread. Creates the |ChildController|, |
| 188 // etc. | 188 // etc. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 203 DCHECK(impl->channel_info_); | 203 DCHECK(impl->channel_info_); |
| 204 impl->Bind(host_message_pipe.Pass()); | 204 impl->Bind(host_message_pipe.Pass()); |
| 205 | 205 |
| 206 app_context->set_controller(impl.Pass()); | 206 app_context->set_controller(impl.Pass()); |
| 207 } | 207 } |
| 208 | 208 |
| 209 void Bind(mojo::ScopedMessagePipeHandle handle) { | 209 void Bind(mojo::ScopedMessagePipeHandle handle) { |
| 210 binding_.Bind(handle.Pass()); | 210 binding_.Bind(handle.Pass()); |
| 211 } | 211 } |
| 212 | 212 |
| 213 // |mojo::ErrorHandler| methods: | |
| 214 void OnConnectionError() override { | |
| 215 // A connection error means the connection to the shell is lost. This is not | |
| 216 // recoverable. | |
| 217 LOG(ERROR) << "Connection error to the shell"; | |
| 218 _exit(1); | |
| 219 } | |
| 220 | |
| 221 // |ChildController| methods: | 213 // |ChildController| methods: |
| 222 void StartApp(const mojo::String& app_path, | 214 void StartApp(const mojo::String& app_path, |
| 223 mojo::InterfaceRequest<mojo::Application> application_request, | 215 mojo::InterfaceRequest<mojo::Application> application_request, |
| 224 const StartAppCallback& on_app_complete) override { | 216 const StartAppCallback& on_app_complete) override { |
| 225 DVLOG(2) << "ChildControllerImpl::StartApp(" << app_path << ", ...)"; | 217 DVLOG(2) << "ChildControllerImpl::StartApp(" << app_path << ", ...)"; |
| 226 DCHECK(thread_checker_.CalledOnValidThread()); | 218 DCHECK(thread_checker_.CalledOnValidThread()); |
| 227 | 219 |
| 228 on_app_complete_ = on_app_complete; | 220 on_app_complete_ = on_app_complete; |
| 229 unblocker_.Unblock(base::Bind(&ChildControllerImpl::StartAppOnMainThread, | 221 unblocker_.Unblock(base::Bind(&ChildControllerImpl::StartAppOnMainThread, |
| 230 base::FilePath::FromUTF8Unsafe(app_path), | 222 base::FilePath::FromUTF8Unsafe(app_path), |
| 231 base::Passed(&application_request))); | 223 base::Passed(&application_request))); |
| 232 } | 224 } |
| 233 | 225 |
| 234 void ExitNow(int32_t exit_code) override { | 226 void ExitNow(int32_t exit_code) override { |
| 235 DVLOG(2) << "ChildControllerImpl::ExitNow(" << exit_code << ")"; | 227 DVLOG(2) << "ChildControllerImpl::ExitNow(" << exit_code << ")"; |
| 236 _exit(exit_code); | 228 _exit(exit_code); |
| 237 } | 229 } |
| 238 | 230 |
| 239 private: | 231 private: |
| 240 ChildControllerImpl(AppContext* app_context, | 232 ChildControllerImpl(AppContext* app_context, |
| 241 const Blocker::Unblocker& unblocker) | 233 const Blocker::Unblocker& unblocker) |
| 242 : app_context_(app_context), | 234 : app_context_(app_context), |
| 243 unblocker_(unblocker), | 235 unblocker_(unblocker), |
| 244 channel_info_(nullptr), | 236 channel_info_(nullptr), |
| 245 binding_(this) { | 237 binding_(this) { |
| 246 binding_.set_error_handler(this); | 238 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
| 239 } |
| 240 |
| 241 void OnConnectionError() { |
| 242 // A connection error means the connection to the shell is lost. This is not |
| 243 // recoverable. |
| 244 LOG(ERROR) << "Connection error to the shell"; |
| 245 _exit(1); |
| 247 } | 246 } |
| 248 | 247 |
| 249 // Callback for |mojo::embedder::ConnectToMaster()|. | 248 // Callback for |mojo::embedder::ConnectToMaster()|. |
| 250 void DidConnectToMaster() { | 249 void DidConnectToMaster() { |
| 251 DVLOG(2) << "ChildControllerImpl::DidCreateChannel()"; | 250 DVLOG(2) << "ChildControllerImpl::DidCreateChannel()"; |
| 252 DCHECK(thread_checker_.CalledOnValidThread()); | 251 DCHECK(thread_checker_.CalledOnValidThread()); |
| 253 } | 252 } |
| 254 | 253 |
| 255 static void StartAppOnMainThread( | 254 static void StartAppOnMainThread( |
| 256 const base::FilePath& app_path, | 255 const base::FilePath& app_path, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 FROM_HERE, base::Bind(&shell::ChildControllerImpl::Init, | 306 FROM_HERE, base::Bind(&shell::ChildControllerImpl::Init, |
| 308 base::Unretained(&app_context), child_connection_id, | 307 base::Unretained(&app_context), child_connection_id, |
| 309 blocker.GetUnblocker())); | 308 blocker.GetUnblocker())); |
| 310 // This will block, then run whatever the controller wants. | 309 // This will block, then run whatever the controller wants. |
| 311 blocker.Block(); | 310 blocker.Block(); |
| 312 | 311 |
| 313 app_context.Shutdown(); | 312 app_context.Shutdown(); |
| 314 | 313 |
| 315 return 0; | 314 return 0; |
| 316 } | 315 } |
| OLD | NEW |