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