| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/application/public/cpp/application_runner.h" | |
| 6 | |
| 7 #include "base/at_exit.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/process/launch.h" | |
| 12 #include "mojo/application/public/cpp/application_delegate.h" | |
| 13 #include "mojo/application/public/cpp/application_impl.h" | |
| 14 #include "mojo/message_pump/message_pump_mojo.h" | |
| 15 | |
| 16 namespace mojo { | |
| 17 | |
| 18 int g_application_runner_argc; | |
| 19 const char* const* g_application_runner_argv; | |
| 20 | |
| 21 ApplicationRunner::ApplicationRunner(ApplicationDelegate* delegate) | |
| 22 : delegate_(scoped_ptr<ApplicationDelegate>(delegate)), | |
| 23 message_loop_type_(base::MessageLoop::TYPE_CUSTOM), | |
| 24 has_run_(false) {} | |
| 25 | |
| 26 ApplicationRunner::~ApplicationRunner() {} | |
| 27 | |
| 28 void ApplicationRunner::InitBaseCommandLine() { | |
| 29 base::CommandLine::Init(g_application_runner_argc, g_application_runner_argv); | |
| 30 } | |
| 31 | |
| 32 void ApplicationRunner::set_message_loop_type(base::MessageLoop::Type type) { | |
| 33 DCHECK_NE(base::MessageLoop::TYPE_CUSTOM, type); | |
| 34 DCHECK(!has_run_); | |
| 35 | |
| 36 message_loop_type_ = type; | |
| 37 } | |
| 38 | |
| 39 MojoResult ApplicationRunner::Run(MojoHandle application_request_handle, | |
| 40 bool init_base) { | |
| 41 DCHECK(!has_run_); | |
| 42 has_run_ = true; | |
| 43 | |
| 44 scoped_ptr<base::AtExitManager> at_exit; | |
| 45 if (init_base) { | |
| 46 InitBaseCommandLine(); | |
| 47 at_exit.reset(new base::AtExitManager); | |
| 48 } | |
| 49 | |
| 50 { | |
| 51 scoped_ptr<base::MessageLoop> loop; | |
| 52 if (message_loop_type_ == base::MessageLoop::TYPE_CUSTOM) | |
| 53 loop.reset(new base::MessageLoop(common::MessagePumpMojo::Create())); | |
| 54 else | |
| 55 loop.reset(new base::MessageLoop(message_loop_type_)); | |
| 56 | |
| 57 ApplicationImpl impl(delegate_.get(), | |
| 58 MakeRequest<Application>(MakeScopedHandle( | |
| 59 MessagePipeHandle(application_request_handle)))); | |
| 60 loop->Run(); | |
| 61 // It's very common for the delegate to cache the app and terminate on | |
| 62 // errors. If we don't delete the delegate before the app we run the risk | |
| 63 // of the delegate having a stale reference to the app and trying to use it. | |
| 64 // Note that we destruct the message loop first because that might trigger | |
| 65 // connection error handlers and they might access objects created by the | |
| 66 // delegate. | |
| 67 loop.reset(); | |
| 68 delegate_.reset(); | |
| 69 } | |
| 70 return MOJO_RESULT_OK; | |
| 71 } | |
| 72 | |
| 73 MojoResult ApplicationRunner::Run(MojoHandle application_request_handle) { | |
| 74 return Run(application_request_handle, true); | |
| 75 } | |
| 76 | |
| 77 } // namespace mojo | |
| OLD | NEW |