| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "services/ui/launcher/launcher_app.h" | 5 #include "services/ui/launcher/launcher_app.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| 11 #include "mojo/application/application_runner_chromium.h" | |
| 12 #include "mojo/common/tracing_impl.h" | 11 #include "mojo/common/tracing_impl.h" |
| 13 #include "mojo/public/c/system/main.h" | |
| 14 #include "mojo/public/cpp/application/application_impl.h" | |
| 15 #include "mojo/public/cpp/application/connect.h" | 12 #include "mojo/public/cpp/application/connect.h" |
| 13 #include "mojo/public/cpp/application/run_application.h" |
| 16 #include "mojo/public/cpp/application/service_provider_impl.h" | 14 #include "mojo/public/cpp/application/service_provider_impl.h" |
| 17 | 15 |
| 18 namespace launcher { | 16 namespace launcher { |
| 19 | 17 |
| 20 LauncherApp::LauncherApp() : app_impl_(nullptr), next_id_(0u) {} | 18 LauncherApp::LauncherApp() : next_id_(0u) {} |
| 21 | 19 |
| 22 LauncherApp::~LauncherApp() {} | 20 LauncherApp::~LauncherApp() {} |
| 23 | 21 |
| 24 void LauncherApp::Initialize(mojo::ApplicationImpl* app_impl) { | 22 void LauncherApp::OnInitialize() { |
| 25 app_impl_ = app_impl; | |
| 26 | |
| 27 auto command_line = base::CommandLine::ForCurrentProcess(); | 23 auto command_line = base::CommandLine::ForCurrentProcess(); |
| 28 command_line->InitFromArgv(app_impl_->args()); | 24 command_line->InitFromArgv(args()); |
| 29 logging::LoggingSettings settings; | 25 logging::LoggingSettings settings; |
| 30 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | 26 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
| 31 logging::InitLogging(settings); | 27 logging::InitLogging(settings); |
| 32 | 28 |
| 33 tracing_.Initialize(app_impl_->shell(), &app_impl_->args()); | 29 tracing_.Initialize(shell(), &args()); |
| 34 TRACE_EVENT0("launcher", __func__); | 30 TRACE_EVENT0("launcher", __func__); |
| 35 | 31 |
| 36 InitCompositor(); | 32 InitCompositor(); |
| 37 InitViewManager(); | 33 InitViewManager(); |
| 38 InitViewAssociates(command_line->GetSwitchValueASCII("view_associate_urls")); | 34 InitViewAssociates(command_line->GetSwitchValueASCII("view_associate_urls")); |
| 39 | 35 |
| 40 for (size_t i = 0; i < command_line->GetArgs().size(); ++i) { | 36 for (size_t i = 0; i < command_line->GetArgs().size(); ++i) { |
| 41 Launch(command_line->GetArgs()[i]); | 37 Launch(command_line->GetArgs()[i]); |
| 42 } | 38 } |
| 43 } | 39 } |
| 44 | 40 |
| 45 void LauncherApp::InitCompositor() { | 41 void LauncherApp::InitCompositor() { |
| 46 mojo::ConnectToService(app_impl_->shell(), "mojo:compositor_service", | 42 mojo::ConnectToService(shell(), "mojo:compositor_service", |
| 47 GetProxy(&compositor_)); | 43 GetProxy(&compositor_)); |
| 48 compositor_.set_connection_error_handler(base::Bind( | 44 compositor_.set_connection_error_handler(base::Bind( |
| 49 &LauncherApp::OnCompositorConnectionError, base::Unretained(this))); | 45 &LauncherApp::OnCompositorConnectionError, base::Unretained(this))); |
| 50 } | 46 } |
| 51 | 47 |
| 52 void LauncherApp::InitViewManager() { | 48 void LauncherApp::InitViewManager() { |
| 53 mojo::ConnectToService(app_impl_->shell(), "mojo:view_manager_service", | 49 mojo::ConnectToService(shell(), "mojo:view_manager_service", |
| 54 GetProxy(&view_manager_)); | 50 GetProxy(&view_manager_)); |
| 55 view_manager_.set_connection_error_handler(base::Bind( | 51 view_manager_.set_connection_error_handler(base::Bind( |
| 56 &LauncherApp::OnViewManagerConnectionError, base::Unretained(this))); | 52 &LauncherApp::OnViewManagerConnectionError, base::Unretained(this))); |
| 57 } | 53 } |
| 58 | 54 |
| 59 void LauncherApp::InitViewAssociates( | 55 void LauncherApp::InitViewAssociates( |
| 60 const std::string& associate_urls_command_line_param) { | 56 const std::string& associate_urls_command_line_param) { |
| 61 // Build up the list of ViewAssociates we are going to start | 57 // Build up the list of ViewAssociates we are going to start |
| 62 auto associate_urls = | 58 auto associate_urls = |
| 63 SplitString(associate_urls_command_line_param, ",", base::KEEP_WHITESPACE, | 59 SplitString(associate_urls_command_line_param, ",", base::KEEP_WHITESPACE, |
| 64 base::SPLIT_WANT_ALL); | 60 base::SPLIT_WANT_ALL); |
| 65 | 61 |
| 66 // If there's nothing we got from the command line, use our own list | 62 // If there's nothing we got from the command line, use our own list |
| 67 if (associate_urls.empty()) { | 63 if (associate_urls.empty()) { |
| 68 // TODO(jeffbrown): Replace this hardcoded list. | 64 // TODO(jeffbrown): Replace this hardcoded list. |
| 69 associate_urls.push_back("mojo:input_manager_service"); | 65 associate_urls.push_back("mojo:input_manager_service"); |
| 70 } | 66 } |
| 71 | 67 |
| 72 view_associate_owners_.reserve(associate_urls.size()); | 68 view_associate_owners_.reserve(associate_urls.size()); |
| 73 | 69 |
| 74 // Connect to ViewAssociates. | 70 // Connect to ViewAssociates. |
| 75 for (const auto& url : associate_urls) { | 71 for (const auto& url : associate_urls) { |
| 76 // Connect to the ViewAssociate. | 72 // Connect to the ViewAssociate. |
| 77 DVLOG(2) << "Connecting to ViewAssociate " << url; | 73 DVLOG(2) << "Connecting to ViewAssociate " << url; |
| 78 mojo::ui::ViewAssociatePtr view_associate; | 74 mojo::ui::ViewAssociatePtr view_associate; |
| 79 mojo::ConnectToService(app_impl_->shell(), url, GetProxy(&view_associate)); | 75 mojo::ConnectToService(shell(), url, GetProxy(&view_associate)); |
| 80 | 76 |
| 81 // Wire up the associate to the ViewManager. | 77 // Wire up the associate to the ViewManager. |
| 82 mojo::ui::ViewAssociateOwnerPtr view_associate_owner; | 78 mojo::ui::ViewAssociateOwnerPtr view_associate_owner; |
| 83 view_manager_->RegisterViewAssociate(view_associate.Pass(), | 79 view_manager_->RegisterViewAssociate(view_associate.Pass(), |
| 84 GetProxy(&view_associate_owner), url); | 80 GetProxy(&view_associate_owner), url); |
| 85 | 81 |
| 86 view_associate_owner.set_connection_error_handler(base::Bind( | 82 view_associate_owner.set_connection_error_handler(base::Bind( |
| 87 &LauncherApp::OnViewAssociateConnectionError, base::Unretained(this))); | 83 &LauncherApp::OnViewAssociateConnectionError, base::Unretained(this))); |
| 88 | 84 |
| 89 view_associate_owners_.push_back(view_associate_owner.Pass()); | 85 view_associate_owners_.push_back(view_associate_owner.Pass()); |
| 90 } | 86 } |
| 91 view_manager_->FinishedRegisteringViewAssociates(); | 87 view_manager_->FinishedRegisteringViewAssociates(); |
| 92 } | 88 } |
| 93 | 89 |
| 94 bool LauncherApp::ConfigureIncomingConnection( | 90 bool LauncherApp::OnAcceptConnection( |
| 95 mojo::ServiceProviderImpl* service_provider_impl) { | 91 mojo::ServiceProviderImpl* service_provider_impl) { |
| 96 // Only present the launcher interface to the shell. | 92 // Only present the launcher interface to the shell. |
| 97 if (service_provider_impl->connection_context().remote_url.empty()) { | 93 if (service_provider_impl->connection_context().remote_url.empty()) { |
| 98 service_provider_impl->AddService<Launcher>( | 94 service_provider_impl->AddService<Launcher>( |
| 99 [this](const mojo::ConnectionContext& connection_context, | 95 [this](const mojo::ConnectionContext& connection_context, |
| 100 mojo::InterfaceRequest<Launcher> launcher_request) { | 96 mojo::InterfaceRequest<Launcher> launcher_request) { |
| 101 bindings_.AddBinding(this, launcher_request.Pass()); | 97 bindings_.AddBinding(this, launcher_request.Pass()); |
| 102 }); | 98 }); |
| 103 } | 99 } |
| 104 return true; | 100 return true; |
| 105 } | 101 } |
| 106 | 102 |
| 107 void LauncherApp::Launch(const mojo::String& application_url) { | 103 void LauncherApp::Launch(const mojo::String& application_url) { |
| 108 DVLOG(1) << "Launching " << application_url; | 104 DVLOG(1) << "Launching " << application_url; |
| 109 | 105 |
| 110 mojo::NativeViewportPtr viewport; | 106 mojo::NativeViewportPtr viewport; |
| 111 mojo::ConnectToService(app_impl_->shell(), "mojo:native_viewport_service", | 107 mojo::ConnectToService(shell(), "mojo:native_viewport_service", |
| 112 GetProxy(&viewport)); | 108 GetProxy(&viewport)); |
| 113 | 109 |
| 114 mojo::ui::ViewProviderPtr view_provider; | 110 mojo::ui::ViewProviderPtr view_provider; |
| 115 mojo::ConnectToService(app_impl_->shell(), application_url, | 111 mojo::ConnectToService(shell(), application_url, GetProxy(&view_provider)); |
| 116 GetProxy(&view_provider)); | |
| 117 | 112 |
| 118 LaunchInternal(viewport.Pass(), view_provider.Pass()); | 113 LaunchInternal(viewport.Pass(), view_provider.Pass()); |
| 119 } | 114 } |
| 115 |
| 120 void LauncherApp::LaunchOnViewport( | 116 void LauncherApp::LaunchOnViewport( |
| 121 mojo::InterfaceHandle<mojo::NativeViewport> viewport, | 117 mojo::InterfaceHandle<mojo::NativeViewport> viewport, |
| 122 mojo::InterfaceHandle<mojo::ui::ViewProvider> view_provider) { | 118 mojo::InterfaceHandle<mojo::ui::ViewProvider> view_provider) { |
| 123 LaunchInternal(mojo::NativeViewportPtr::Create(viewport.Pass()), | 119 LaunchInternal(mojo::NativeViewportPtr::Create(viewport.Pass()), |
| 124 mojo::ui::ViewProviderPtr::Create(view_provider.Pass())); | 120 mojo::ui::ViewProviderPtr::Create(view_provider.Pass())); |
| 125 } | 121 } |
| 126 | 122 |
| 127 void LauncherApp::LaunchInternal(mojo::NativeViewportPtr viewport, | 123 void LauncherApp::LaunchInternal(mojo::NativeViewportPtr viewport, |
| 128 mojo::ui::ViewProviderPtr view_provider) { | 124 mojo::ui::ViewProviderPtr view_provider) { |
| 129 uint32_t next_id = next_id_++; | 125 uint32_t next_id = next_id_++; |
| 130 std::unique_ptr<LaunchInstance> instance(new LaunchInstance( | 126 std::unique_ptr<LaunchInstance> instance(new LaunchInstance( |
| 131 app_impl_, viewport.Pass(), view_provider.Pass(), compositor_.get(), | 127 viewport.Pass(), view_provider.Pass(), compositor_.get(), |
| 132 view_manager_.get(), base::Bind(&LauncherApp::OnLaunchTermination, | 128 view_manager_.get(), base::Bind(&LauncherApp::OnLaunchTermination, |
| 133 base::Unretained(this), next_id))); | 129 base::Unretained(this), next_id))); |
| 134 instance->Launch(); | 130 instance->Launch(); |
| 135 launch_instances_.emplace(next_id, std::move(instance)); | 131 launch_instances_.emplace(next_id, std::move(instance)); |
| 136 } | 132 } |
| 137 | 133 |
| 138 void LauncherApp::OnLaunchTermination(uint32_t id) { | 134 void LauncherApp::OnLaunchTermination(uint32_t id) { |
| 139 launch_instances_.erase(id); | 135 launch_instances_.erase(id); |
| 140 if (launch_instances_.empty()) { | 136 if (launch_instances_.empty()) { |
| 141 app_impl_->Terminate(); | 137 mojo::TerminateApplication(MOJO_RESULT_OK); |
| 142 } | 138 } |
| 143 } | 139 } |
| 144 | 140 |
| 145 void LauncherApp::OnCompositorConnectionError() { | 141 void LauncherApp::OnCompositorConnectionError() { |
| 146 LOG(ERROR) << "Exiting due to compositor connection error."; | 142 LOG(ERROR) << "Exiting due to compositor connection error."; |
| 147 app_impl_->Terminate(); | 143 mojo::TerminateApplication(MOJO_RESULT_UNKNOWN); |
| 148 } | 144 } |
| 149 | 145 |
| 150 void LauncherApp::OnViewManagerConnectionError() { | 146 void LauncherApp::OnViewManagerConnectionError() { |
| 151 LOG(ERROR) << "Exiting due to view manager connection error."; | 147 LOG(ERROR) << "Exiting due to view manager connection error."; |
| 152 app_impl_->Terminate(); | 148 mojo::TerminateApplication(MOJO_RESULT_UNKNOWN); |
| 153 } | 149 } |
| 154 | 150 |
| 155 void LauncherApp::OnViewAssociateConnectionError() { | 151 void LauncherApp::OnViewAssociateConnectionError() { |
| 156 LOG(ERROR) << "Exiting due to view associate connection error."; | 152 LOG(ERROR) << "Exiting due to view associate connection error."; |
| 157 app_impl_->Terminate(); | 153 mojo::TerminateApplication(MOJO_RESULT_UNKNOWN); |
| 158 }; | 154 }; |
| 159 | 155 |
| 160 } // namespace launcher | 156 } // namespace launcher |
| OLD | NEW |