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"
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"
20 void DefaultTerminationClosure() {
21 if (base::MessageLoop::current() &&
22 base::MessageLoop::current()->is_running())
23 base::MessageLoop::current()->Quit();
28 ApplicationImpl::ApplicationImpl(ApplicationDelegate
* delegate
,
29 InterfaceRequest
<Application
> request
)
30 : ApplicationImpl(delegate
, request
.Pass(),
31 base::Bind(&DefaultTerminationClosure
)) {
34 ApplicationImpl::ApplicationImpl(ApplicationDelegate
* delegate
,
35 InterfaceRequest
<Application
> request
,
36 const Closure
& termination_closure
)
37 : delegate_(delegate
),
38 binding_(this, request
.Pass()),
39 termination_closure_(termination_closure
),
40 app_lifetime_helper_(this),
41 quit_requested_(false),
42 weak_factory_(this) {}
44 ApplicationImpl::~ApplicationImpl() {
45 app_lifetime_helper_
.OnQuit();
48 scoped_ptr
<ApplicationConnection
> ApplicationImpl::ConnectToApplication(
49 URLRequestPtr request
) {
50 return ConnectToApplicationWithCapabilityFilter(request
.Pass(), nullptr);
53 scoped_ptr
<ApplicationConnection
>
54 ApplicationImpl::ConnectToApplicationWithCapabilityFilter(
55 URLRequestPtr request
,
56 CapabilityFilterPtr filter
) {
59 ServiceProviderPtr local_services
;
60 InterfaceRequest
<ServiceProvider
> local_request
= GetProxy(&local_services
);
61 ServiceProviderPtr remote_services
;
62 std::string application_url
= request
->url
.To
<std::string
>();
63 // We allow all interfaces on outgoing connections since we are presumably in
64 // a position to know who we're talking to.
65 // TODO(beng): is this a valid assumption or do we need to figure some way to
67 std::set
<std::string
> allowed
;
69 InterfaceRequest
<ServiceProvider
> remote_services_proxy
=
70 GetProxy(&remote_services
);
71 scoped_ptr
<internal::ServiceRegistry
> registry(new internal::ServiceRegistry(
72 application_url
, application_url
, remote_services
.Pass(),
73 local_request
.Pass(), allowed
));
74 shell_
->ConnectToApplication(request
.Pass(), remote_services_proxy
.Pass(),
75 local_services
.Pass(), filter
.Pass(),
76 registry
->GetConnectToApplicationCallback());
77 if (!delegate_
->ConfigureOutgoingConnection(registry
.get()))
79 return registry
.Pass();
82 void ApplicationImpl::Initialize(ShellPtr shell
, const mojo::String
& url
) {
83 shell_
= shell
.Pass();
84 shell_
.set_connection_error_handler([this]() { OnConnectionError(); });
86 delegate_
->Initialize(this);
89 void ApplicationImpl::Quit() {
90 // We can't quit immediately, since there could be in-flight requests from the
91 // shell. So check with it first.
93 quit_requested_
= true;
94 shell_
->QuitApplication();
100 void ApplicationImpl::AcceptConnection(
101 const String
& requestor_url
,
102 InterfaceRequest
<ServiceProvider
> services
,
103 ServiceProviderPtr exposed_services
,
104 Array
<String
> allowed_interfaces
,
106 scoped_ptr
<ApplicationConnection
> registry(new internal::ServiceRegistry(
107 url
, requestor_url
, exposed_services
.Pass(), services
.Pass(),
108 allowed_interfaces
.To
<std::set
<std::string
>>()));
109 if (!delegate_
->ConfigureIncomingConnection(registry
.get()))
112 // If we were quitting because we thought there were no more services for this
113 // app in use, then that has changed so cancel the quit request.
115 quit_requested_
= false;
117 incoming_connections_
.push_back(registry
.Pass());
120 void ApplicationImpl::OnQuitRequested(const Callback
<void(bool)>& callback
) {
121 // If by the time we got the reply from the shell, more requests had come in
122 // then we don't want to quit the app anymore so we return false. Otherwise
123 // |quit_requested_| is true so we tell the shell to proceed with the quit.
124 callback
.Run(quit_requested_
);
129 void ApplicationImpl::OnConnectionError() {
130 base::WeakPtr
<ApplicationImpl
> ptr(weak_factory_
.GetWeakPtr());
132 // We give the delegate notice first, since it might want to do something on
133 // shell connection errors other than immediate termination of the run
134 // loop. The application might want to continue servicing connections other
135 // than the one to the shell.
136 bool quit_now
= delegate_
->OnShellConnectionError();
144 void ApplicationImpl::QuitNow() {
146 termination_closure_
.Run();
149 void ApplicationImpl::UnbindConnections(
150 InterfaceRequest
<Application
>* application_request
,
152 *application_request
= binding_
.Unbind();
153 shell
->Bind(shell_
.PassInterface());