Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / mojo / application / public / cpp / lib / application_impl.cc
blobdca66d6eddb3a13dc7ee3ada8bfa7996362ec30b
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.
5 #include "mojo/application/public/cpp/application_impl.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/message_loop/message_loop.h"
11 #include "mojo/application/public/cpp/application_delegate.h"
12 #include "mojo/application/public/cpp/lib/service_registry.h"
13 #include "mojo/public/cpp/bindings/interface_ptr.h"
14 #include "mojo/public/cpp/environment/logging.h"
16 namespace mojo {
18 namespace {
20 void DefaultTerminationClosure() {
21 if (base::MessageLoop::current() &&
22 base::MessageLoop::current()->is_running())
23 base::MessageLoop::current()->Quit();
26 } // namespace
28 // TODO(beng): upstream this into mojo repo, array.h
29 template <typename E, typename T>
30 struct TypeConverter<std::set<E>, Array<T>> {
31 static std::set<E> Convert(const Array<T>& input) {
32 std::set<E> result;
33 if (!input.is_null()) {
34 for (size_t i = 0; i < input.size(); ++i)
35 result.insert(TypeConverter<E, T>::Convert(input[i]));
37 return result;
41 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
42 InterfaceRequest<Application> request)
43 : ApplicationImpl(delegate, request.Pass(),
44 base::Bind(&DefaultTerminationClosure)) {
47 ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
48 InterfaceRequest<Application> request,
49 const base::Closure& termination_closure)
50 : delegate_(delegate),
51 binding_(this, request.Pass()),
52 termination_closure_(termination_closure),
53 app_lifetime_helper_(this),
54 quit_requested_(false),
55 in_destructor_(false),
56 weak_factory_(this) {
59 void ApplicationImpl::ClearConnections() {
60 // Copy the ServiceRegistryLists because they will be mutated by
61 // ApplicationConnection::CloseConnection.
62 ServiceRegistryList incoming_service_registries(incoming_service_registries_);
63 for (internal::ServiceRegistry* registry : incoming_service_registries)
64 registry->CloseConnection();
65 DCHECK(incoming_service_registries_.empty());
67 ServiceRegistryList outgoing_service_registries(outgoing_service_registries_);
68 for (internal::ServiceRegistry* registry : outgoing_service_registries)
69 registry->CloseConnection();
70 DCHECK(outgoing_service_registries_.empty());
73 ApplicationImpl::~ApplicationImpl() {
74 DCHECK(!in_destructor_);
75 in_destructor_ = true;
76 ClearConnections();
77 app_lifetime_helper_.ApplicationTerminated();
80 ApplicationConnection* ApplicationImpl::ConnectToApplication(
81 mojo::URLRequestPtr request,
82 CapabilityFilterPtr filter) {
83 if (!shell_)
84 return nullptr;
85 ServiceProviderPtr local_services;
86 InterfaceRequest<ServiceProvider> local_request = GetProxy(&local_services);
87 ServiceProviderPtr remote_services;
88 std::string application_url = request->url.To<std::string>();
89 shell_->ConnectToApplication(request.Pass(), GetProxy(&remote_services),
90 local_services.Pass(), filter.Pass());
91 // We allow all interfaces on outgoing connections since we are presumably in
92 // a position to know who we're talking to.
93 // TODO(beng): is this a valid assumption or do we need to figure some way to
94 // filter here too?
95 std::set<std::string> allowed;
96 allowed.insert("*");
97 internal::ServiceRegistry* registry = new internal::ServiceRegistry(
98 this, application_url, application_url, remote_services.Pass(),
99 local_request.Pass(), allowed);
100 if (!delegate_->ConfigureOutgoingConnection(registry)) {
101 registry->CloseConnection();
102 return nullptr;
104 outgoing_service_registries_.push_back(registry);
105 return registry;
108 void ApplicationImpl::CloseConnection(ApplicationConnection* connection) {
109 if (!in_destructor_)
110 delegate_->OnWillCloseConnection(connection);
111 auto outgoing_it = std::find(outgoing_service_registries_.begin(),
112 outgoing_service_registries_.end(),
113 connection);
114 if (outgoing_it != outgoing_service_registries_.end()) {
115 outgoing_service_registries_.erase(outgoing_it);
116 return;
118 auto incoming_it = std::find(incoming_service_registries_.begin(),
119 incoming_service_registries_.end(),
120 connection);
121 if (incoming_it != incoming_service_registries_.end())
122 incoming_service_registries_.erase(incoming_it);
125 void ApplicationImpl::Initialize(ShellPtr shell, const mojo::String& url) {
126 shell_ = shell.Pass();
127 shell_.set_connection_error_handler([this]() { OnConnectionError(); });
128 url_ = url;
129 delegate_->Initialize(this);
132 void ApplicationImpl::WaitForInitialize() {
133 if (!shell_)
134 binding_.WaitForIncomingMethodCall();
137 void ApplicationImpl::UnbindConnections(
138 InterfaceRequest<Application>* application_request,
139 ShellPtr* shell) {
140 *application_request = binding_.Unbind();
141 shell->Bind(shell_.PassInterface());
144 void ApplicationImpl::Terminate() {
145 // We can't quit immediately, since there could be in-flight requests from the
146 // shell. So check with it first.
147 if (shell_) {
148 quit_requested_ = true;
149 shell_->QuitApplication();
150 } else {
151 QuitNow();
155 void ApplicationImpl::QuitNow() {
156 delegate_->Quit();
157 termination_closure_.Run();
160 void ApplicationImpl::AcceptConnection(
161 const String& requestor_url,
162 InterfaceRequest<ServiceProvider> services,
163 ServiceProviderPtr exposed_services,
164 Array<String> allowed_interfaces,
165 const String& url) {
166 internal::ServiceRegistry* registry = new internal::ServiceRegistry(
167 this, url, requestor_url, exposed_services.Pass(), services.Pass(),
168 allowed_interfaces.To<std::set<std::string>>());
169 if (!delegate_->ConfigureIncomingConnection(registry)) {
170 registry->CloseConnection();
171 return;
173 incoming_service_registries_.push_back(registry);
175 // If we were quitting because we thought there were no more services for this
176 // app in use, then that has changed so cancel the quit request.
177 if (quit_requested_)
178 quit_requested_ = false;
181 void ApplicationImpl::OnQuitRequested(const Callback<void(bool)>& callback) {
182 // If by the time we got the reply from the shell, more requests had come in
183 // then we don't want to quit the app anymore so we return false. Otherwise
184 // |quit_requested_| is true so we tell the shell to proceed with the quit.
185 callback.Run(quit_requested_);
186 if (quit_requested_)
187 QuitNow();
190 void ApplicationImpl::OnConnectionError() {
191 base::WeakPtr<ApplicationImpl> ptr(weak_factory_.GetWeakPtr());
193 // We give the delegate notice first, since it might want to do something on
194 // shell connection errors other than immediate termination of the run
195 // loop. The application might want to continue servicing connections other
196 // than the one to the shell.
197 bool quit_now = delegate_->OnShellConnectionError();
198 if (quit_now)
199 QuitNow();
200 if (!ptr)
201 return;
202 shell_ = nullptr;
205 } // namespace mojo