| 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/public/cpp/application/application_impl.h" | 5 #include "mojo/public/cpp/application/application_impl.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "mojo/public/cpp/application/application_delegate.h" | 9 #include "mojo/public/cpp/application/application_delegate.h" |
| 8 #include "mojo/public/cpp/application/lib/service_registry.h" | 10 #include "mojo/public/cpp/application/lib/service_registry.h" |
| 9 #include "mojo/public/cpp/bindings/interface_ptr.h" | 11 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 12 #include "mojo/public/cpp/bindings/interface_request.h" |
| 10 #include "mojo/public/cpp/environment/logging.h" | 13 #include "mojo/public/cpp/environment/logging.h" |
| 14 #include "mojo/public/cpp/system/message_pipe.h" |
| 11 | 15 |
| 12 namespace mojo { | 16 namespace mojo { |
| 13 | 17 |
| 14 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, | 18 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, |
| 15 InterfaceRequest<Application> request) | 19 InterfaceRequest<Application> request) |
| 16 : delegate_(delegate), binding_(this, request.Pass()) { | 20 : delegate_(delegate), binding_(this, request.Pass()) {} |
| 17 } | 21 |
| 22 ApplicationImpl::~ApplicationImpl() {} |
| 18 | 23 |
| 19 bool ApplicationImpl::HasArg(const std::string& arg) const { | 24 bool ApplicationImpl::HasArg(const std::string& arg) const { |
| 20 return std::find(args_.begin(), args_.end(), arg) != args_.end(); | 25 return std::find(args_.begin(), args_.end(), arg) != args_.end(); |
| 21 } | 26 } |
| 22 | 27 |
| 23 void ApplicationImpl::ClearConnections() { | 28 InterfacePtrInfo<ApplicationConnector> |
| 24 for (ServiceRegistryList::iterator i(incoming_service_registries_.begin()); | 29 ApplicationImpl::CreateApplicationConnector() { |
| 25 i != incoming_service_registries_.end(); | 30 MOJO_CHECK(shell_); |
| 26 ++i) | 31 MessagePipe pipe; |
| 27 delete *i; | 32 shell_->CreateApplicationConnector( |
| 28 for (ServiceRegistryList::iterator i(outgoing_service_registries_.begin()); | 33 MakeRequest<ApplicationConnector>(pipe.handle1.Pass())); |
| 29 i != outgoing_service_registries_.end(); | 34 return InterfacePtrInfo<ApplicationConnector>(pipe.handle0.Pass(), 0u); |
| 30 ++i) | |
| 31 delete *i; | |
| 32 incoming_service_registries_.clear(); | |
| 33 outgoing_service_registries_.clear(); | |
| 34 } | |
| 35 | |
| 36 ApplicationImpl::~ApplicationImpl() { | |
| 37 ClearConnections(); | |
| 38 } | 35 } |
| 39 | 36 |
| 40 ApplicationConnection* ApplicationImpl::ConnectToApplication( | 37 ApplicationConnection* ApplicationImpl::ConnectToApplication( |
| 41 const String& application_url) { | 38 const String& application_url) { |
| 42 MOJO_CHECK(shell_); | 39 MOJO_CHECK(shell_); |
| 43 ServiceProviderPtr local_services; | 40 ServiceProviderPtr local_services; |
| 44 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); | 41 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); |
| 45 ServiceProviderPtr remote_services; | 42 ServiceProviderPtr remote_services; |
| 46 shell_->ConnectToApplication(application_url, GetProxy(&remote_services), | 43 shell_->ConnectToApplication(application_url, GetProxy(&remote_services), |
| 47 local_services.Pass()); | 44 local_services.Pass()); |
| 48 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 45 internal::ServiceRegistry* registry = new internal::ServiceRegistry( |
| 49 this, application_url, application_url, remote_services.Pass(), | 46 this, application_url, application_url, remote_services.Pass(), |
| 50 local_request.Pass()); | 47 local_request.Pass()); |
| 51 outgoing_service_registries_.push_back(registry); | 48 outgoing_service_registries_.emplace_back(registry); |
| 52 return registry; | 49 return registry; |
| 53 } | 50 } |
| 54 | 51 |
| 55 void ApplicationImpl::Initialize(ShellPtr shell, | |
| 56 Array<String> args, | |
| 57 const mojo::String& url) { | |
| 58 shell_ = shell.Pass(); | |
| 59 shell_.set_connection_error_handler([this]() { OnShellError(); }); | |
| 60 url_ = url; | |
| 61 args_ = args.To<std::vector<std::string>>(); | |
| 62 delegate_->Initialize(this); | |
| 63 } | |
| 64 | |
| 65 void ApplicationImpl::WaitForInitialize() { | 52 void ApplicationImpl::WaitForInitialize() { |
| 66 if (!shell_) | 53 if (!shell_) |
| 67 binding_.WaitForIncomingMethodCall(); | 54 binding_.WaitForIncomingMethodCall(); |
| 68 } | 55 } |
| 69 | 56 |
| 70 void ApplicationImpl::UnbindConnections( | 57 void ApplicationImpl::UnbindConnections( |
| 71 InterfaceRequest<Application>* application_request, | 58 InterfaceRequest<Application>* application_request, |
| 72 ShellPtr* shell) { | 59 ShellPtr* shell) { |
| 73 *application_request = binding_.Unbind(); | 60 *application_request = binding_.Unbind(); |
| 74 shell->Bind(shell_.PassInterface()); | 61 shell->Bind(shell_.PassInterface()); |
| 75 } | 62 } |
| 76 | 63 |
| 64 void ApplicationImpl::Initialize(ShellPtr shell, |
| 65 Array<String> args, |
| 66 const mojo::String& url) { |
| 67 shell_ = shell.Pass(); |
| 68 shell_.set_connection_error_handler([this]() { |
| 69 delegate_->Quit(); |
| 70 incoming_service_registries_.clear(); |
| 71 outgoing_service_registries_.clear(); |
| 72 Terminate(); |
| 73 }); |
| 74 url_ = url; |
| 75 args_ = args.To<std::vector<std::string>>(); |
| 76 delegate_->Initialize(this); |
| 77 } |
| 78 |
| 77 void ApplicationImpl::AcceptConnection( | 79 void ApplicationImpl::AcceptConnection( |
| 78 const String& requestor_url, | 80 const String& requestor_url, |
| 79 InterfaceRequest<ServiceProvider> services, | 81 InterfaceRequest<ServiceProvider> services, |
| 80 ServiceProviderPtr exposed_services, | 82 ServiceProviderPtr exposed_services, |
| 81 const String& url) { | 83 const String& url) { |
| 82 internal::ServiceRegistry* registry = new internal::ServiceRegistry( | 84 std::unique_ptr<internal::ServiceRegistry> registry( |
| 83 this, url, requestor_url, exposed_services.Pass(), services.Pass()); | 85 new internal::ServiceRegistry(this, url, requestor_url, |
| 84 if (!delegate_->ConfigureIncomingConnection(registry)) { | 86 exposed_services.Pass(), services.Pass())); |
| 85 delete registry; | 87 if (!delegate_->ConfigureIncomingConnection(registry.get())) |
| 86 return; | 88 return; |
| 87 } | 89 incoming_service_registries_.push_back(std::move(registry)); |
| 88 incoming_service_registries_.push_back(registry); | |
| 89 } | 90 } |
| 90 | 91 |
| 91 void ApplicationImpl::RequestQuit() { | 92 void ApplicationImpl::RequestQuit() { |
| 92 delegate_->Quit(); | 93 delegate_->Quit(); |
| 93 Terminate(); | 94 Terminate(); |
| 94 } | 95 } |
| 95 | 96 |
| 96 } // namespace mojo | 97 } // namespace mojo |
| OLD | NEW |