| 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/shell/public/cpp/application_runner.h" | 5 #include "mojo/shell/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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/process/launch.h" | 11 #include "base/process/launch.h" |
| 12 #include "mojo/message_pump/message_pump_mojo.h" |
| 12 #include "mojo/shell/public/cpp/shell_client.h" | 13 #include "mojo/shell/public/cpp/shell_client.h" |
| 13 #include "mojo/shell/public/cpp/shell_connection.h" | 14 #include "mojo/shell/public/cpp/shell_connection.h" |
| 14 | 15 |
| 15 namespace mojo { | 16 namespace mojo { |
| 16 | 17 |
| 17 int g_application_runner_argc; | 18 int g_application_runner_argc; |
| 18 const char* const* g_application_runner_argv; | 19 const char* const* g_application_runner_argv; |
| 19 | 20 |
| 20 ApplicationRunner::ApplicationRunner(ShellClient* client) | 21 ApplicationRunner::ApplicationRunner(ShellClient* client) |
| 21 : client_(scoped_ptr<ShellClient>(client)), | 22 : client_(scoped_ptr<ShellClient>(client)), |
| 22 message_loop_type_(base::MessageLoop::TYPE_DEFAULT), | 23 message_loop_type_(base::MessageLoop::TYPE_CUSTOM), |
| 23 has_run_(false) {} | 24 has_run_(false) {} |
| 24 | 25 |
| 25 ApplicationRunner::~ApplicationRunner() {} | 26 ApplicationRunner::~ApplicationRunner() {} |
| 26 | 27 |
| 27 void ApplicationRunner::InitBaseCommandLine() { | 28 void ApplicationRunner::InitBaseCommandLine() { |
| 28 base::CommandLine::Init(g_application_runner_argc, g_application_runner_argv); | 29 base::CommandLine::Init(g_application_runner_argc, g_application_runner_argv); |
| 29 } | 30 } |
| 30 | 31 |
| 31 void ApplicationRunner::set_message_loop_type(base::MessageLoop::Type type) { | 32 void ApplicationRunner::set_message_loop_type(base::MessageLoop::Type type) { |
| 32 DCHECK_NE(base::MessageLoop::TYPE_CUSTOM, type); | 33 DCHECK_NE(base::MessageLoop::TYPE_CUSTOM, type); |
| 33 DCHECK(!has_run_); | 34 DCHECK(!has_run_); |
| 34 | 35 |
| 35 message_loop_type_ = type; | 36 message_loop_type_ = type; |
| 36 } | 37 } |
| 37 | 38 |
| 38 MojoResult ApplicationRunner::Run(MojoHandle shell_client_request_handle, | 39 MojoResult ApplicationRunner::Run(MojoHandle shell_client_request_handle, |
| 39 bool init_base) { | 40 bool init_base) { |
| 40 DCHECK(!has_run_); | 41 DCHECK(!has_run_); |
| 41 has_run_ = true; | 42 has_run_ = true; |
| 42 | 43 |
| 43 scoped_ptr<base::AtExitManager> at_exit; | 44 scoped_ptr<base::AtExitManager> at_exit; |
| 44 if (init_base) { | 45 if (init_base) { |
| 45 InitBaseCommandLine(); | 46 InitBaseCommandLine(); |
| 46 at_exit.reset(new base::AtExitManager); | 47 at_exit.reset(new base::AtExitManager); |
| 47 } | 48 } |
| 48 | 49 |
| 49 { | 50 { |
| 50 scoped_ptr<base::MessageLoop> loop; | 51 scoped_ptr<base::MessageLoop> loop; |
| 51 loop.reset(new base::MessageLoop(message_loop_type_)); | 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_)); |
| 52 | 56 |
| 53 connection_.reset(new ShellConnection( | 57 connection_.reset(new ShellConnection( |
| 54 client_.get(), | 58 client_.get(), |
| 55 MakeRequest<shell::mojom::ShellClient>(MakeScopedHandle( | 59 MakeRequest<shell::mojom::ShellClient>(MakeScopedHandle( |
| 56 MessagePipeHandle(shell_client_request_handle))))); | 60 MessagePipeHandle(shell_client_request_handle))))); |
| 57 loop->Run(); | 61 loop->Run(); |
| 58 // It's very common for the client to cache the app and terminate on errors. | 62 // It's very common for the client to cache the app and terminate on errors. |
| 59 // If we don't delete the client before the app we run the risk of the | 63 // If we don't delete the client before the app we run the risk of the |
| 60 // client having a stale reference to the app and trying to use it. | 64 // client having a stale reference to the app and trying to use it. |
| 61 // Note that we destruct the message loop first because that might trigger | 65 // Note that we destruct the message loop first because that might trigger |
| (...skipping 13 matching lines...) Expand all Loading... |
| 75 !base::CommandLine::ForCurrentProcess()->HasSwitch("single-process"); | 79 !base::CommandLine::ForCurrentProcess()->HasSwitch("single-process"); |
| 76 } | 80 } |
| 77 return Run(shell_client_request_handle, init_base); | 81 return Run(shell_client_request_handle, init_base); |
| 78 } | 82 } |
| 79 | 83 |
| 80 void ApplicationRunner::DestroyShellConnection() { | 84 void ApplicationRunner::DestroyShellConnection() { |
| 81 connection_.reset(); | 85 connection_.reset(); |
| 82 } | 86 } |
| 83 | 87 |
| 84 } // namespace mojo | 88 } // namespace mojo |
| OLD | NEW |