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