| 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 <algorithm> | |
| 6 #include <utility> | |
| 7 | |
| 8 #include "base/bind.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "mojo/application/public/cpp/application_delegate.h" | |
| 11 #include "mojo/application/public/cpp/application_impl.h" | |
| 12 #include "mojo/application/public/cpp/lib/service_registry.h" | |
| 13 #include "mojo/converters/network/network_type_converters.h" | |
| 14 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
| 15 #include "mojo/public/cpp/environment/logging.h" | |
| 16 | |
| 17 namespace mojo { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 void DefaultTerminationClosure() { | |
| 22 if (base::MessageLoop::current() && | |
| 23 base::MessageLoop::current()->is_running()) | |
| 24 base::MessageLoop::current()->QuitWhenIdle(); | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 ApplicationImpl::ConnectParams::ConnectParams(const std::string& url) | |
| 30 : ConnectParams(URLRequest::From(url)) {} | |
| 31 ApplicationImpl::ConnectParams::ConnectParams(URLRequestPtr request) | |
| 32 : request_(std::move(request)), filter_(CapabilityFilter::New()) { | |
| 33 filter_->filter.mark_non_null(); | |
| 34 } | |
| 35 ApplicationImpl::ConnectParams::~ConnectParams() {} | |
| 36 | |
| 37 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, | |
| 38 InterfaceRequest<Application> request) | |
| 39 : ApplicationImpl(delegate, | |
| 40 std::move(request), | |
| 41 base::Bind(&DefaultTerminationClosure)) {} | |
| 42 | |
| 43 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, | |
| 44 InterfaceRequest<Application> request, | |
| 45 const Closure& termination_closure) | |
| 46 : delegate_(delegate), | |
| 47 binding_(this, std::move(request)), | |
| 48 termination_closure_(termination_closure), | |
| 49 app_lifetime_helper_(this), | |
| 50 quit_requested_(false), | |
| 51 weak_factory_(this) {} | |
| 52 | |
| 53 ApplicationImpl::~ApplicationImpl() { | |
| 54 app_lifetime_helper_.OnQuit(); | |
| 55 } | |
| 56 | |
| 57 scoped_ptr<ApplicationConnection> ApplicationImpl::ConnectToApplication( | |
| 58 const std::string& url) { | |
| 59 ConnectParams params(url); | |
| 60 params.set_filter(CreatePermissiveCapabilityFilter()); | |
| 61 return ConnectToApplication(¶ms); | |
| 62 } | |
| 63 | |
| 64 scoped_ptr<ApplicationConnection> | |
| 65 ApplicationImpl::ConnectToApplication(ConnectParams* params) { | |
| 66 if (!shell_) | |
| 67 return nullptr; | |
| 68 DCHECK(params); | |
| 69 URLRequestPtr request = params->TakeRequest(); | |
| 70 ServiceProviderPtr local_services; | |
| 71 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services); | |
| 72 ServiceProviderPtr remote_services; | |
| 73 std::string application_url = request->url.To<std::string>(); | |
| 74 // We allow all interfaces on outgoing connections since we are presumably in | |
| 75 // a position to know who we're talking to. | |
| 76 // TODO(beng): is this a valid assumption or do we need to figure some way to | |
| 77 // filter here too? | |
| 78 std::set<std::string> allowed; | |
| 79 allowed.insert("*"); | |
| 80 InterfaceRequest<ServiceProvider> remote_services_proxy = | |
| 81 GetProxy(&remote_services); | |
| 82 scoped_ptr<internal::ServiceRegistry> registry(new internal::ServiceRegistry( | |
| 83 application_url, application_url, std::move(remote_services), | |
| 84 std::move(local_request), allowed)); | |
| 85 shell_->ConnectToApplication(std::move(request), | |
| 86 std::move(remote_services_proxy), | |
| 87 std::move(local_services), params->TakeFilter(), | |
| 88 registry->GetConnectToApplicationCallback()); | |
| 89 if (!delegate_->ConfigureOutgoingConnection(registry.get())) | |
| 90 return nullptr; | |
| 91 return std::move(registry); | |
| 92 } | |
| 93 | |
| 94 void ApplicationImpl::WaitForInitialize() { | |
| 95 DCHECK(!shell_.is_bound()); | |
| 96 binding_.WaitForIncomingMethodCall(); | |
| 97 } | |
| 98 | |
| 99 void ApplicationImpl::Quit() { | |
| 100 // We can't quit immediately, since there could be in-flight requests from the | |
| 101 // shell. So check with it first. | |
| 102 if (shell_) { | |
| 103 quit_requested_ = true; | |
| 104 shell_->QuitApplication(); | |
| 105 } else { | |
| 106 QuitNow(); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 void ApplicationImpl::Initialize(ShellPtr shell, const mojo::String& url) { | |
| 111 shell_ = std::move(shell); | |
| 112 shell_.set_connection_error_handler([this]() { OnConnectionError(); }); | |
| 113 url_ = url; | |
| 114 delegate_->Initialize(this); | |
| 115 } | |
| 116 | |
| 117 void ApplicationImpl::AcceptConnection( | |
| 118 const String& requestor_url, | |
| 119 InterfaceRequest<ServiceProvider> services, | |
| 120 ServiceProviderPtr exposed_services, | |
| 121 Array<String> allowed_interfaces, | |
| 122 const String& url) { | |
| 123 scoped_ptr<ApplicationConnection> registry(new internal::ServiceRegistry( | |
| 124 url, requestor_url, std::move(exposed_services), std::move(services), | |
| 125 allowed_interfaces.To<std::set<std::string>>())); | |
| 126 if (!delegate_->ConfigureIncomingConnection(registry.get())) | |
| 127 return; | |
| 128 | |
| 129 // If we were quitting because we thought there were no more services for this | |
| 130 // app in use, then that has changed so cancel the quit request. | |
| 131 if (quit_requested_) | |
| 132 quit_requested_ = false; | |
| 133 | |
| 134 incoming_connections_.push_back(std::move(registry)); | |
| 135 } | |
| 136 | |
| 137 void ApplicationImpl::OnQuitRequested(const Callback<void(bool)>& callback) { | |
| 138 // If by the time we got the reply from the shell, more requests had come in | |
| 139 // then we don't want to quit the app anymore so we return false. Otherwise | |
| 140 // |quit_requested_| is true so we tell the shell to proceed with the quit. | |
| 141 callback.Run(quit_requested_); | |
| 142 if (quit_requested_) | |
| 143 QuitNow(); | |
| 144 } | |
| 145 | |
| 146 void ApplicationImpl::OnConnectionError() { | |
| 147 base::WeakPtr<ApplicationImpl> ptr(weak_factory_.GetWeakPtr()); | |
| 148 | |
| 149 // We give the delegate notice first, since it might want to do something on | |
| 150 // shell connection errors other than immediate termination of the run | |
| 151 // loop. The application might want to continue servicing connections other | |
| 152 // than the one to the shell. | |
| 153 bool quit_now = delegate_->OnShellConnectionError(); | |
| 154 if (quit_now) | |
| 155 QuitNow(); | |
| 156 if (!ptr) | |
| 157 return; | |
| 158 shell_ = nullptr; | |
| 159 } | |
| 160 | |
| 161 void ApplicationImpl::QuitNow() { | |
| 162 delegate_->Quit(); | |
| 163 termination_closure_.Run(); | |
| 164 } | |
| 165 | |
| 166 void ApplicationImpl::UnbindConnections( | |
| 167 InterfaceRequest<Application>* application_request, | |
| 168 ShellPtr* shell) { | |
| 169 *application_request = binding_.Unbind(); | |
| 170 shell->Bind(shell_.PassInterface()); | |
| 171 } | |
| 172 | |
| 173 CapabilityFilterPtr CreatePermissiveCapabilityFilter() { | |
| 174 CapabilityFilterPtr filter(CapabilityFilter::New()); | |
| 175 Array<String> all_interfaces; | |
| 176 all_interfaces.push_back("*"); | |
| 177 filter->filter.insert("*", std::move(all_interfaces)); | |
| 178 return filter; | |
| 179 } | |
| 180 | |
| 181 } // namespace mojo | |
| OLD | NEW |