1 // Copyright 2015 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/shell/application_instance.h"
8 #include "base/stl_util.h"
9 #include "mojo/application/public/interfaces/content_handler.mojom.h"
10 #include "mojo/common/common_type_converters.h"
11 #include "mojo/common/url_type_converters.h"
12 #include "mojo/shell/application_manager.h"
13 #include "mojo/shell/content_handler_connection.h"
18 ApplicationInstance::ApplicationInstance(
19 ApplicationPtr application
,
20 ApplicationManager
* manager
,
21 const Identity
& identity
,
22 uint32_t requesting_content_handler_id
,
23 const base::Closure
& on_application_end
)
26 allow_any_application_(identity
.filter().size() == 1 &&
27 identity
.filter().count("*") == 1),
28 requesting_content_handler_id_(requesting_content_handler_id
),
29 on_application_end_(on_application_end
),
30 application_(application
.Pass()),
32 queue_requests_(false) {
33 binding_
.set_connection_error_handler([this]() { OnConnectionError(); });
36 ApplicationInstance::~ApplicationInstance() {
37 for (auto request
: queued_client_requests_
)
38 request
->connect_callback().Run(kInvalidContentHandlerID
);
39 STLDeleteElements(&queued_client_requests_
);
42 void ApplicationInstance::InitializeApplication() {
44 binding_
.Bind(GetProxy(&shell
));
45 application_
->Initialize(shell
.Pass(), identity_
.url().spec());
48 void ApplicationInstance::ConnectToClient(
49 scoped_ptr
<ConnectToApplicationParams
> params
) {
50 if (queue_requests_
) {
51 queued_client_requests_
.push_back(params
.release());
55 CallAcceptConnection(params
.Pass());
58 // Shell implementation:
59 void ApplicationInstance::ConnectToApplication(
60 URLRequestPtr app_request
,
61 InterfaceRequest
<ServiceProvider
> services
,
62 ServiceProviderPtr exposed_services
,
63 CapabilityFilterPtr filter
,
64 const ConnectToApplicationCallback
& callback
) {
65 std::string url_string
= app_request
->url
.To
<std::string
>();
67 if (!url
.is_valid()) {
68 LOG(ERROR
) << "Error: invalid URL: " << url_string
;
69 callback
.Run(kInvalidContentHandlerID
);
72 if (allow_any_application_
||
73 identity_
.filter().find(url
.spec()) != identity_
.filter().end()) {
74 CapabilityFilter capability_filter
= GetPermissiveCapabilityFilter();
75 if (!filter
.is_null())
76 capability_filter
= filter
->filter
.To
<CapabilityFilter
>();
78 scoped_ptr
<ConnectToApplicationParams
> params(
79 new ConnectToApplicationParams
);
80 params
->SetSource(this);
81 params
->SetTargetURLRequest(
83 Identity(GURL(app_request
->url
), std::string(), capability_filter
));
84 params
->set_services(services
.Pass());
85 params
->set_exposed_services(exposed_services
.Pass());
86 params
->set_connect_callback(callback
);
87 manager_
->ConnectToApplication(params
.Pass());
89 LOG(WARNING
) << "CapabilityFilter prevented connection from: " <<
90 identity_
.url() << " to: " << url
.spec();
91 callback
.Run(kInvalidContentHandlerID
);
95 void ApplicationInstance::QuitApplication() {
96 queue_requests_
= true;
97 application_
->OnQuitRequested(
98 base::Bind(&ApplicationInstance::OnQuitRequestedResult
,
99 base::Unretained(this)));
102 void ApplicationInstance::CallAcceptConnection(
103 scoped_ptr
<ConnectToApplicationParams
> params
) {
104 params
->connect_callback().Run(requesting_content_handler_id_
);
105 AllowedInterfaces interfaces
;
106 interfaces
.insert("*");
107 if (!params
->source().is_null())
108 interfaces
= GetAllowedInterfaces(params
->source().filter(), identity_
);
110 application_
->AcceptConnection(
111 params
->source().url().spec(), params
->TakeServices(),
112 params
->TakeExposedServices(), Array
<String
>::From(interfaces
).Pass(),
113 params
->target().url().spec());
116 void ApplicationInstance::OnConnectionError() {
117 std::vector
<ConnectToApplicationParams
*> queued_client_requests
;
118 queued_client_requests_
.swap(queued_client_requests
);
119 auto manager
= manager_
;
120 manager_
->OnApplicationInstanceError(this);
123 // If any queued requests came to shell during time it was shutting down,
125 for (auto request
: queued_client_requests
) {
126 // Unfortunately, it is possible that |request->target_url_request()| is
127 // null at this point. Consider the following sequence:
128 // 1) connect_request_1 arrives at the application manager; the manager
129 // decides to fetch the app.
130 // 2) connect_request_2 arrives for the same app; because the app is not
131 // running yet, the manager decides to fetch the app again.
132 // 3) The fetch for step (1) completes and an application instance app_a is
134 // 4) app_a goes into two-phase shutdown.
135 // 5) The fetch for step (2) completes; the manager finds that there is a
136 // running app already, so it connects to app_a.
137 // 6) connect_request_2 is queued (and eventually gets here), but its
138 // original_request field was already lost to NetworkFetcher at step (2).
140 // TODO(yzshen): It seems we should register a pending application instance
141 // before starting the fetch. So at step (2) the application manager knows
142 // that it can wait for the first fetch to complete instead of doing a
143 // second one directly.
144 if (!request
->target_url_request()) {
145 URLRequestPtr url_request
= mojo::URLRequest::New();
146 url_request
->url
= request
->target().url().spec();
147 request
->SetTargetURLRequest(url_request
.Pass(), request
->target());
149 manager
->ConnectToApplication(make_scoped_ptr(request
));
153 void ApplicationInstance::OnQuitRequestedResult(bool can_quit
) {
157 queue_requests_
= false;
158 for (auto request
: queued_client_requests_
)
159 CallAcceptConnection(make_scoped_ptr(request
));
161 queued_client_requests_
.clear();