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