OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/shell/application_instance.h" | 5 #include "mojo/shell/application_instance.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "mojo/application/public/interfaces/content_handler.mojom.h" | 9 #include "mojo/application/public/interfaces/content_handler.mojom.h" |
10 #include "mojo/common/common_type_converters.h" | 10 #include "mojo/common/common_type_converters.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 if (it->first == "*") | 26 if (it->first == "*") |
27 canonicalized[it->first] = it->second; | 27 canonicalized[it->first] = it->second; |
28 else | 28 else |
29 canonicalized[GURL(it->first).spec()] = it->second; | 29 canonicalized[GURL(it->first).spec()] = it->second; |
30 } | 30 } |
31 return canonicalized; | 31 return canonicalized; |
32 } | 32 } |
33 | 33 |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 ApplicationInstance::QueuedClientRequest::QueuedClientRequest() | |
37 : originator(nullptr) {} | |
38 | |
39 ApplicationInstance::QueuedClientRequest::~QueuedClientRequest() { | |
40 } | |
41 | |
42 ApplicationInstance::ApplicationInstance( | 36 ApplicationInstance::ApplicationInstance( |
43 ApplicationPtr application, | 37 ApplicationPtr application, |
44 ApplicationManager* manager, | 38 ApplicationManager* manager, |
45 const Identity& originator_identity, | 39 const Identity& originator_identity, |
46 const Identity& identity, | 40 const Identity& identity, |
47 const CapabilityFilter& filter, | 41 const CapabilityFilter& filter, |
48 uint32_t requesting_content_handler_id, | 42 uint32_t requesting_content_handler_id, |
49 const base::Closure& on_application_end) | 43 const base::Closure& on_application_end) |
50 : manager_(manager), | 44 : manager_(manager), |
51 originator_identity_(originator_identity), | 45 originator_identity_(originator_identity), |
52 identity_(identity), | 46 identity_(identity), |
53 filter_(CanonicalizeFilter(filter)), | 47 filter_(CanonicalizeFilter(filter)), |
54 allow_any_application_(filter.size() == 1 && filter.count("*") == 1), | 48 allow_any_application_(filter.size() == 1 && filter.count("*") == 1), |
55 requesting_content_handler_id_(requesting_content_handler_id), | 49 requesting_content_handler_id_(requesting_content_handler_id), |
56 on_application_end_(on_application_end), | 50 on_application_end_(on_application_end), |
57 application_(application.Pass()), | 51 application_(application.Pass()), |
58 binding_(this), | 52 binding_(this), |
59 queue_requests_(false) { | 53 queue_requests_(false) { |
60 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 54 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
61 } | 55 } |
62 | 56 |
63 ApplicationInstance::~ApplicationInstance() { | 57 ApplicationInstance::~ApplicationInstance() { |
| 58 for (auto request : queued_client_requests_) |
| 59 request->connect_callback().Run(kInvalidContentHandlerID); |
64 STLDeleteElements(&queued_client_requests_); | 60 STLDeleteElements(&queued_client_requests_); |
65 } | 61 } |
66 | 62 |
67 void ApplicationInstance::InitializeApplication() { | 63 void ApplicationInstance::InitializeApplication() { |
68 ShellPtr shell; | 64 ShellPtr shell; |
69 binding_.Bind(GetProxy(&shell)); | 65 binding_.Bind(GetProxy(&shell)); |
70 application_->Initialize(shell.Pass(), identity_.url.spec()); | 66 application_->Initialize(shell.Pass(), identity_.url.spec()); |
71 } | 67 } |
72 | 68 |
73 void ApplicationInstance::ConnectToClient( | 69 void ApplicationInstance::ConnectToClient( |
74 ApplicationInstance* originator, | 70 scoped_ptr<ConnectToApplicationParams> params) { |
75 const GURL& requested_url, | |
76 const GURL& requestor_url, | |
77 InterfaceRequest<ServiceProvider> services, | |
78 ServiceProviderPtr exposed_services, | |
79 const CapabilityFilter& filter, | |
80 const ConnectToApplicationCallback& callback) { | |
81 callback.Run(requesting_content_handler_id_); | |
82 if (queue_requests_) { | 71 if (queue_requests_) { |
83 QueuedClientRequest* queued_request = new QueuedClientRequest(); | 72 queued_client_requests_.push_back(params.release()); |
84 queued_request->originator = originator; | |
85 queued_request->requested_url = requested_url; | |
86 queued_request->requestor_url = requestor_url; | |
87 queued_request->services = services.Pass(); | |
88 queued_request->exposed_services = exposed_services.Pass(); | |
89 queued_request->filter = filter; | |
90 queued_client_requests_.push_back(queued_request); | |
91 return; | 73 return; |
92 } | 74 } |
93 | 75 |
94 CallAcceptConnection(originator, requestor_url, services.Pass(), | 76 CallAcceptConnection(params.Pass()); |
95 exposed_services.Pass(), requested_url); | |
96 } | |
97 | |
98 AllowedInterfaces ApplicationInstance::GetAllowedInterfaces( | |
99 const Identity& identity) const { | |
100 // Start by looking for interfaces specific to the supplied identity. | |
101 auto it = filter_.find(identity.url.spec()); | |
102 if (it != filter_.end()) | |
103 return it->second; | |
104 | |
105 // Fall back to looking for a wildcard rule. | |
106 it = filter_.find("*"); | |
107 if (filter_.size() == 1 && it != filter_.end()) | |
108 return it->second; | |
109 | |
110 // Finally, nothing is allowed. | |
111 return AllowedInterfaces(); | |
112 } | 77 } |
113 | 78 |
114 // Shell implementation: | 79 // Shell implementation: |
115 void ApplicationInstance::ConnectToApplication( | 80 void ApplicationInstance::ConnectToApplication( |
116 URLRequestPtr app_request, | 81 URLRequestPtr app_request, |
117 InterfaceRequest<ServiceProvider> services, | 82 InterfaceRequest<ServiceProvider> services, |
118 ServiceProviderPtr exposed_services, | 83 ServiceProviderPtr exposed_services, |
119 CapabilityFilterPtr filter, | 84 CapabilityFilterPtr filter, |
120 const ConnectToApplicationCallback& callback) { | 85 const ConnectToApplicationCallback& callback) { |
121 std::string url_string = app_request->url.To<std::string>(); | 86 std::string url_string = app_request->url.To<std::string>(); |
122 GURL url(url_string); | 87 GURL url(url_string); |
123 if (!url.is_valid()) { | 88 if (!url.is_valid()) { |
124 LOG(ERROR) << "Error: invalid URL: " << url_string; | 89 LOG(ERROR) << "Error: invalid URL: " << url_string; |
125 callback.Run(kInvalidContentHandlerID); | 90 callback.Run(kInvalidContentHandlerID); |
126 return; | 91 return; |
127 } | 92 } |
128 if (allow_any_application_ || filter_.find(url.spec()) != filter_.end()) { | 93 if (allow_any_application_ || filter_.find(url.spec()) != filter_.end()) { |
129 CapabilityFilter capability_filter = GetPermissiveCapabilityFilter(); | 94 CapabilityFilter capability_filter = GetPermissiveCapabilityFilter(); |
130 if (!filter.is_null()) | 95 if (!filter.is_null()) |
131 capability_filter = filter->filter.To<CapabilityFilter>(); | 96 capability_filter = filter->filter.To<CapabilityFilter>(); |
132 manager_->ConnectToApplication( | 97 manager_->ConnectToApplication( |
133 this, app_request.Pass(), std::string(), identity_.url, services.Pass(), | 98 this, app_request.Pass(), std::string(), GURL(), services.Pass(), |
134 exposed_services.Pass(), capability_filter, base::Closure(), callback); | 99 exposed_services.Pass(), capability_filter, base::Closure(), callback); |
135 } else { | 100 } else { |
136 LOG(WARNING) << "CapabilityFilter prevented connection from: " << | 101 LOG(WARNING) << "CapabilityFilter prevented connection from: " << |
137 identity_.url << " to: " << url.spec(); | 102 identity_.url << " to: " << url.spec(); |
138 callback.Run(kInvalidContentHandlerID); | 103 callback.Run(kInvalidContentHandlerID); |
139 } | 104 } |
140 } | 105 } |
141 | 106 |
142 void ApplicationInstance::QuitApplication() { | 107 void ApplicationInstance::QuitApplication() { |
143 queue_requests_ = true; | 108 queue_requests_ = true; |
144 application_->OnQuitRequested( | 109 application_->OnQuitRequested( |
145 base::Bind(&ApplicationInstance::OnQuitRequestedResult, | 110 base::Bind(&ApplicationInstance::OnQuitRequestedResult, |
146 base::Unretained(this))); | 111 base::Unretained(this))); |
147 } | 112 } |
148 | 113 |
149 void ApplicationInstance::CallAcceptConnection( | 114 void ApplicationInstance::CallAcceptConnection( |
150 ApplicationInstance* originator, | 115 scoped_ptr<ConnectToApplicationParams> params) { |
151 const GURL& requestor_url, | 116 params->connect_callback().Run(requesting_content_handler_id_); |
152 InterfaceRequest<ServiceProvider> services, | |
153 ServiceProviderPtr exposed_services, | |
154 const GURL& requested_url) { | |
155 AllowedInterfaces interfaces; | 117 AllowedInterfaces interfaces; |
156 interfaces.insert("*"); | 118 interfaces.insert("*"); |
157 if (originator) | 119 if (!params->originator_identity().is_null()) |
158 interfaces = originator->GetAllowedInterfaces(identity_); | 120 interfaces = GetAllowedInterfaces(params->originator_filter(), identity_); |
159 application_->AcceptConnection(requestor_url.spec(), | 121 |
160 services.Pass(), | 122 application_->AcceptConnection( |
161 exposed_services.Pass(), | 123 params->originator_identity().url.spec(), params->TakeServices(), |
162 Array<String>::From(interfaces).Pass(), | 124 params->TakeExposedServices(), Array<String>::From(interfaces).Pass(), |
163 requested_url.spec()); | 125 params->app_url().spec()); |
164 } | 126 } |
165 | 127 |
166 void ApplicationInstance::OnConnectionError() { | 128 void ApplicationInstance::OnConnectionError() { |
167 std::vector<QueuedClientRequest*> queued_client_requests; | 129 std::vector<ConnectToApplicationParams*> queued_client_requests; |
168 queued_client_requests_.swap(queued_client_requests); | 130 queued_client_requests_.swap(queued_client_requests); |
169 auto manager = manager_; | 131 auto manager = manager_; |
170 manager_->OnApplicationInstanceError(this); | 132 manager_->OnApplicationInstanceError(this); |
171 //|this| is deleted. | 133 //|this| is deleted. |
172 | 134 |
173 // If any queued requests came to shell during time it was shutting down, | 135 // If any queued requests came to shell during time it was shutting down, |
174 // start them now. | 136 // start them now. |
175 for (auto request : queued_client_requests) { | 137 for (auto request : queued_client_requests) { |
176 mojo::URLRequestPtr url(mojo::URLRequest::New()); | 138 // Unfortunately, it is possible that |request->app_url_request()| is null |
177 url->url = mojo::String::From(request->requested_url.spec()); | 139 // at this point. Consider the following sequence: |
178 ApplicationInstance* originator = | 140 // 1) connect_request_1 arrives at the application manager; the manager |
179 manager->GetApplicationInstance(originator_identity_); | 141 // decides to fetch the app. |
180 manager->ConnectToApplication( | 142 // 2) connect_request_2 arrives for the same app; because the app is not |
181 originator, url.Pass(), std::string(), request->requestor_url, | 143 // running yet, the manager decides to fetch the app again. |
182 request->services.Pass(), request->exposed_services.Pass(), | 144 // 3) The fetch for step (1) completes and an application instance app_a is |
183 request->filter, base::Closure(), EmptyConnectCallback()); | 145 // registered. |
| 146 // 4) app_a goes into two-phase shutdown. |
| 147 // 5) The fetch for step (2) completes; the manager finds that there is a |
| 148 // running app already, so it connects to app_a. |
| 149 // 6) connect_request_2 is queued (and eventually gets here), but its |
| 150 // original_request field was already lost to NetworkFetcher at step (2). |
| 151 // |
| 152 // TODO(yzshen): It seems we should register a pending application instance |
| 153 // before starting the fetch. So at step (2) the application manager knows |
| 154 // that it can wait for the first fetch to complete instead of doing a |
| 155 // second one directly. |
| 156 if (!request->app_url_request()) { |
| 157 URLRequestPtr url_request = mojo::URLRequest::New(); |
| 158 url_request->url = request->app_url().spec(); |
| 159 request->SetURLInfo(url_request.Pass()); |
| 160 } |
| 161 manager->ConnectToApplication(make_scoped_ptr(request)); |
184 } | 162 } |
185 STLDeleteElements(&queued_client_requests); | |
186 } | 163 } |
187 | 164 |
188 void ApplicationInstance::OnQuitRequestedResult(bool can_quit) { | 165 void ApplicationInstance::OnQuitRequestedResult(bool can_quit) { |
189 if (can_quit) | 166 if (can_quit) |
190 return; | 167 return; |
191 | 168 |
192 queue_requests_ = false; | 169 queue_requests_ = false; |
193 for (auto request : queued_client_requests_) { | 170 for (auto request : queued_client_requests_) |
194 CallAcceptConnection( | 171 CallAcceptConnection(make_scoped_ptr(request)); |
195 request->originator, request->requestor_url, request->services.Pass(), | 172 |
196 request->exposed_services.Pass(), request->requested_url); | 173 queued_client_requests_.clear(); |
197 } | |
198 STLDeleteElements(&queued_client_requests_); | |
199 } | 174 } |
200 | 175 |
201 } // namespace shell | 176 } // namespace shell |
202 } // namespace mojo | 177 } // namespace mojo |
OLD | NEW |