OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_manager.h" | 5 #include "mojo/shell/application_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 12 matching lines...) Expand all Loading... | |
23 #include "mojo/shell/update_fetcher.h" | 23 #include "mojo/shell/update_fetcher.h" |
24 | 24 |
25 namespace mojo { | 25 namespace mojo { |
26 namespace shell { | 26 namespace shell { |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 // Used by TestAPI. | 30 // Used by TestAPI. |
31 bool has_created_instance = false; | 31 bool has_created_instance = false; |
32 | 32 |
33 void OnEmptyOnConnectCallback(uint32_t content_handler_id) {} | |
34 | |
33 } // namespace | 35 } // namespace |
34 | 36 |
35 // static | 37 // static |
36 ApplicationManager::TestAPI::TestAPI(ApplicationManager* manager) | 38 ApplicationManager::TestAPI::TestAPI(ApplicationManager* manager) |
37 : manager_(manager) { | 39 : manager_(manager) { |
38 } | 40 } |
39 | 41 |
40 ApplicationManager::TestAPI::~TestAPI() { | 42 ApplicationManager::TestAPI::~TestAPI() { |
41 } | 43 } |
42 | 44 |
43 bool ApplicationManager::TestAPI::HasCreatedInstance() { | 45 bool ApplicationManager::TestAPI::HasCreatedInstance() { |
44 return has_created_instance; | 46 return has_created_instance; |
45 } | 47 } |
46 | 48 |
47 bool ApplicationManager::TestAPI::HasRunningInstanceForURL( | 49 bool ApplicationManager::TestAPI::HasRunningInstanceForURL( |
48 const GURL& url) const { | 50 const GURL& url) const { |
49 return manager_->identity_to_instance_.find(Identity(url)) != | 51 return manager_->identity_to_instance_.find(Identity(url)) != |
50 manager_->identity_to_instance_.end(); | 52 manager_->identity_to_instance_.end(); |
51 } | 53 } |
52 | 54 |
53 ApplicationManager::ApplicationManager(Delegate* delegate) | 55 ApplicationManager::ApplicationManager(Delegate* delegate) |
54 : delegate_(delegate), weak_ptr_factory_(this) { | 56 : delegate_(delegate), |
55 } | 57 disable_cache_(false), |
58 content_handler_id_counter_(0u), | |
59 weak_ptr_factory_(this) {} | |
56 | 60 |
57 ApplicationManager::~ApplicationManager() { | 61 ApplicationManager::~ApplicationManager() { |
58 URLToContentHandlerMap url_to_content_handler(url_to_content_handler_); | 62 URLToContentHandlerMap url_to_content_handler(url_to_content_handler_); |
59 for (auto& pair : url_to_content_handler) | 63 for (auto& pair : url_to_content_handler) |
60 pair.second->CloseConnection(); | 64 pair.second->CloseConnection(); |
61 TerminateShellConnections(); | 65 TerminateShellConnections(); |
62 STLDeleteValues(&url_to_loader_); | 66 STLDeleteValues(&url_to_loader_); |
63 STLDeleteValues(&scheme_to_loader_); | 67 STLDeleteValues(&scheme_to_loader_); |
64 } | 68 } |
65 | 69 |
66 void ApplicationManager::TerminateShellConnections() { | 70 void ApplicationManager::TerminateShellConnections() { |
67 STLDeleteValues(&identity_to_instance_); | 71 STLDeleteValues(&identity_to_instance_); |
68 } | 72 } |
69 | 73 |
70 void ApplicationManager::ConnectToApplication( | 74 void ApplicationManager::ConnectToApplication( |
71 ApplicationInstance* originator, | 75 ApplicationInstance* originator, |
72 mojo::URLRequestPtr requested_url, | 76 mojo::URLRequestPtr requested_url, |
73 const std::string& qualifier, | 77 const std::string& qualifier, |
74 const GURL& requestor_url, | 78 const GURL& requestor_url, |
79 uint32_t requesting_content_handler_id, | |
75 InterfaceRequest<ServiceProvider> services, | 80 InterfaceRequest<ServiceProvider> services, |
76 ServiceProviderPtr exposed_services, | 81 ServiceProviderPtr exposed_services, |
77 const CapabilityFilter& filter, | 82 const CapabilityFilter& filter, |
78 const base::Closure& on_application_end) { | 83 const base::Closure& on_application_end, |
84 const Shell::ConnectToApplicationCallback& connect_callback) { | |
79 GURL requested_gurl(requested_url->url.To<std::string>()); | 85 GURL requested_gurl(requested_url->url.To<std::string>()); |
80 TRACE_EVENT_INSTANT1( | 86 TRACE_EVENT_INSTANT1( |
81 "mojo_shell", "ApplicationManager::ConnectToApplication", | 87 "mojo_shell", "ApplicationManager::ConnectToApplication", |
82 TRACE_EVENT_SCOPE_THREAD, "requested_url", requested_gurl.spec()); | 88 TRACE_EVENT_SCOPE_THREAD, "requested_url", requested_gurl.spec()); |
83 DCHECK(requested_gurl.is_valid()); | 89 DCHECK(requested_gurl.is_valid()); |
84 | 90 |
85 // We check both the mapped and resolved urls for existing instances because | 91 // We check both the mapped and resolved urls for existing instances because |
86 // external applications can be registered for the unresolved mojo:foo urls. | 92 // external applications can be registered for the unresolved mojo:foo urls. |
87 | 93 |
88 GURL mapped_url = delegate_->ResolveMappings(requested_gurl); | 94 GURL mapped_url = delegate_->ResolveMappings(requested_gurl); |
89 if (ConnectToRunningApplication(originator, mapped_url, qualifier, | 95 if (ConnectToRunningApplication(originator, mapped_url, qualifier, |
90 requestor_url, &services, | 96 requestor_url, &services, &exposed_services, |
91 &exposed_services, filter)) { | 97 filter, connect_callback)) { |
92 return; | 98 return; |
93 } | 99 } |
94 | 100 |
95 GURL resolved_url = delegate_->ResolveMojoURL(mapped_url); | 101 GURL resolved_url = delegate_->ResolveMojoURL(mapped_url); |
96 if (ConnectToRunningApplication(originator, resolved_url, qualifier, | 102 if (ConnectToRunningApplication(originator, resolved_url, qualifier, |
97 requestor_url, &services, | 103 requestor_url, &services, &exposed_services, |
98 &exposed_services, filter)) { | 104 filter, connect_callback)) { |
99 return; | 105 return; |
100 } | 106 } |
101 | 107 |
102 // The application is not running, let's compute the parameters. | 108 // The application is not running, let's compute the parameters. |
103 if (ConnectToApplicationWithLoader( | 109 if (ConnectToApplicationWithLoader( |
104 originator, requested_gurl, qualifier, mapped_url, requestor_url, | 110 originator, requested_gurl, qualifier, mapped_url, requestor_url, |
105 &services, &exposed_services, filter, on_application_end, | 111 requesting_content_handler_id, &services, &exposed_services, filter, |
106 GetLoaderForURL(mapped_url))) { | 112 on_application_end, connect_callback, GetLoaderForURL(mapped_url))) { |
107 return; | 113 return; |
108 } | 114 } |
109 | 115 |
110 if (ConnectToApplicationWithLoader( | 116 if (ConnectToApplicationWithLoader( |
111 originator, requested_gurl, qualifier, resolved_url, requestor_url, | 117 originator, requested_gurl, qualifier, resolved_url, requestor_url, |
112 &services, &exposed_services, filter, on_application_end, | 118 requesting_content_handler_id, &services, &exposed_services, filter, |
119 on_application_end, connect_callback, | |
113 GetLoaderForURL(resolved_url))) { | 120 GetLoaderForURL(resolved_url))) { |
114 return; | 121 return; |
115 } | 122 } |
116 | 123 |
117 if (ConnectToApplicationWithLoader( | 124 if (ConnectToApplicationWithLoader( |
118 originator, requested_gurl, qualifier, resolved_url, requestor_url, | 125 originator, requested_gurl, qualifier, resolved_url, requestor_url, |
119 &services, &exposed_services, filter, on_application_end, | 126 requesting_content_handler_id, &services, &exposed_services, filter, |
120 default_loader_.get())) { | 127 on_application_end, connect_callback, default_loader_.get())) { |
121 return; | 128 return; |
122 } | 129 } |
123 | 130 |
124 auto callback = base::Bind( | 131 auto callback = base::Bind( |
125 &ApplicationManager::HandleFetchCallback, weak_ptr_factory_.GetWeakPtr(), | 132 &ApplicationManager::HandleFetchCallback, weak_ptr_factory_.GetWeakPtr(), |
126 originator, requested_gurl, qualifier, requestor_url, | 133 originator, requested_gurl, qualifier, requestor_url, |
127 base::Passed(services.Pass()), base::Passed(exposed_services.Pass()), | 134 requesting_content_handler_id, base::Passed(services.Pass()), |
128 filter, on_application_end); | 135 base::Passed(exposed_services.Pass()), filter, on_application_end, |
136 connect_callback); | |
129 | 137 |
130 if (delegate_->CreateFetcher( | 138 if (delegate_->CreateFetcher( |
131 resolved_url, | 139 resolved_url, |
132 base::Bind(callback, NativeApplicationCleanup::DONT_DELETE))) { | 140 base::Bind(callback, NativeApplicationCleanup::DONT_DELETE))) { |
133 return; | 141 return; |
134 } | 142 } |
135 | 143 |
136 if (resolved_url.SchemeIsFile()) { | 144 if (resolved_url.SchemeIsFile()) { |
137 // LocalFetcher uses the network service to infer MIME types from URLs. | 145 // LocalFetcher uses the network service to infer MIME types from URLs. |
138 // Skip this for mojo URLs to avoid recursively loading the network service. | 146 // Skip this for mojo URLs to avoid recursively loading the network service. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 url_loader_factory_.get(), base::Bind(callback, cleanup)); | 186 url_loader_factory_.get(), base::Bind(callback, cleanup)); |
179 } | 187 } |
180 | 188 |
181 bool ApplicationManager::ConnectToRunningApplication( | 189 bool ApplicationManager::ConnectToRunningApplication( |
182 ApplicationInstance* originator, | 190 ApplicationInstance* originator, |
183 const GURL& resolved_url, | 191 const GURL& resolved_url, |
184 const std::string& qualifier, | 192 const std::string& qualifier, |
185 const GURL& requestor_url, | 193 const GURL& requestor_url, |
186 InterfaceRequest<ServiceProvider>* services, | 194 InterfaceRequest<ServiceProvider>* services, |
187 ServiceProviderPtr* exposed_services, | 195 ServiceProviderPtr* exposed_services, |
188 const CapabilityFilter& filter) { | 196 const CapabilityFilter& filter, |
197 const Shell::ConnectToApplicationCallback& connect_callback) { | |
189 ApplicationInstance* instance = | 198 ApplicationInstance* instance = |
190 GetApplicationInstance(Identity(resolved_url, qualifier)); | 199 GetApplicationInstance(Identity(resolved_url, qualifier)); |
191 if (!instance) | 200 if (!instance) |
192 return false; | 201 return false; |
193 | 202 |
194 instance->ConnectToClient(originator, resolved_url, requestor_url, | 203 instance->ConnectToClient(originator, resolved_url, requestor_url, |
195 services->Pass(), exposed_services->Pass(), | 204 services->Pass(), exposed_services->Pass(), filter); |
196 filter); | 205 connect_callback.Run(instance->requesting_content_handler_id()); |
Ben Goodger (Google)
2015/08/31 17:41:07
is ConnectToClient a more central place to run thi
sky
2015/08/31 19:46:07
Done.
| |
197 return true; | 206 return true; |
198 } | 207 } |
199 | 208 |
200 bool ApplicationManager::ConnectToApplicationWithLoader( | 209 bool ApplicationManager::ConnectToApplicationWithLoader( |
201 ApplicationInstance* originator, | 210 ApplicationInstance* originator, |
202 const GURL& requested_url, | 211 const GURL& requested_url, |
203 const std::string& qualifier, | 212 const std::string& qualifier, |
204 const GURL& resolved_url, | 213 const GURL& resolved_url, |
205 const GURL& requestor_url, | 214 const GURL& requestor_url, |
215 uint32_t requesting_content_handler_id, | |
206 InterfaceRequest<ServiceProvider>* services, | 216 InterfaceRequest<ServiceProvider>* services, |
207 ServiceProviderPtr* exposed_services, | 217 ServiceProviderPtr* exposed_services, |
208 const CapabilityFilter& filter, | 218 const CapabilityFilter& filter, |
209 const base::Closure& on_application_end, | 219 const base::Closure& on_application_end, |
220 const Shell::ConnectToApplicationCallback& connect_callback, | |
210 ApplicationLoader* loader) { | 221 ApplicationLoader* loader) { |
211 if (!loader) | 222 if (!loader) |
212 return false; | 223 return false; |
213 | 224 |
214 const GURL app_url = | 225 const GURL app_url = |
215 requested_url.SchemeIs("mojo") ? requested_url : resolved_url; | 226 requested_url.SchemeIs("mojo") ? requested_url : resolved_url; |
216 | 227 |
217 loader->Load( | 228 loader->Load(resolved_url, |
218 resolved_url, | 229 RegisterInstance(originator, app_url, qualifier, requestor_url, |
219 RegisterInstance(originator, app_url, qualifier, requestor_url, | 230 requesting_content_handler_id, services->Pass(), |
220 services->Pass(), exposed_services->Pass(), | 231 exposed_services->Pass(), filter, |
221 filter, on_application_end)); | 232 on_application_end, connect_callback, nullptr)); |
222 return true; | 233 return true; |
223 } | 234 } |
224 | 235 |
225 InterfaceRequest<Application> ApplicationManager::RegisterInstance( | 236 InterfaceRequest<Application> ApplicationManager::RegisterInstance( |
226 ApplicationInstance* originator, | 237 ApplicationInstance* originator, |
227 const GURL& app_url, | 238 const GURL& app_url, |
228 const std::string& qualifier, | 239 const std::string& qualifier, |
229 const GURL& requestor_url, | 240 const GURL& requestor_url, |
241 const uint32_t requesting_content_handler_id, | |
230 InterfaceRequest<ServiceProvider> services, | 242 InterfaceRequest<ServiceProvider> services, |
231 ServiceProviderPtr exposed_services, | 243 ServiceProviderPtr exposed_services, |
232 const CapabilityFilter& filter, | 244 const CapabilityFilter& filter, |
233 const base::Closure& on_application_end) { | 245 const base::Closure& on_application_end, |
246 const Shell::ConnectToApplicationCallback& connect_callback, | |
247 ApplicationInstance** resulting_instance) { | |
234 Identity app_identity(app_url, qualifier); | 248 Identity app_identity(app_url, qualifier); |
235 | 249 |
236 ApplicationPtr application; | 250 ApplicationPtr application; |
237 InterfaceRequest<Application> application_request = GetProxy(&application); | 251 InterfaceRequest<Application> application_request = GetProxy(&application); |
238 ApplicationInstance* instance = new ApplicationInstance( | 252 ApplicationInstance* instance = new ApplicationInstance( |
239 application.Pass(), this, | 253 application.Pass(), this, |
240 originator ? originator->identity() : Identity(GURL()), app_identity, | 254 originator ? originator->identity() : Identity(GURL()), app_identity, |
241 filter, on_application_end); | 255 filter, requesting_content_handler_id, on_application_end); |
242 DCHECK(identity_to_instance_.find(app_identity) == | 256 DCHECK(identity_to_instance_.find(app_identity) == |
243 identity_to_instance_.end()); | 257 identity_to_instance_.end()); |
244 identity_to_instance_[app_identity] = instance; | 258 identity_to_instance_[app_identity] = instance; |
245 instance->InitializeApplication(); | 259 instance->InitializeApplication(); |
246 instance->ConnectToClient(originator, app_url, requestor_url, services.Pass(), | 260 instance->ConnectToClient(originator, app_url, requestor_url, services.Pass(), |
247 exposed_services.Pass(), filter); | 261 exposed_services.Pass(), filter); |
262 connect_callback.Run(kInvalidContentHandlerID); | |
263 if (resulting_instance) | |
264 *resulting_instance = instance; | |
248 return application_request.Pass(); | 265 return application_request.Pass(); |
249 } | 266 } |
250 | 267 |
251 ApplicationInstance* ApplicationManager::GetApplicationInstance( | 268 ApplicationInstance* ApplicationManager::GetApplicationInstance( |
252 const Identity& identity) const { | 269 const Identity& identity) const { |
253 const auto& instance_it = identity_to_instance_.find(identity); | 270 const auto& instance_it = identity_to_instance_.find(identity); |
254 if (instance_it != identity_to_instance_.end()) | 271 if (instance_it != identity_to_instance_.end()) |
255 return instance_it->second; | 272 return instance_it->second; |
256 return nullptr; | 273 return nullptr; |
257 } | 274 } |
258 | 275 |
259 void ApplicationManager::HandleFetchCallback( | 276 void ApplicationManager::HandleFetchCallback( |
260 ApplicationInstance* originator, | 277 ApplicationInstance* originator, |
261 const GURL& requested_url, | 278 const GURL& requested_url, |
262 const std::string& qualifier, | 279 const std::string& qualifier, |
263 const GURL& requestor_url, | 280 const GURL& requestor_url, |
281 uint32_t requesting_content_handler_id, | |
264 InterfaceRequest<ServiceProvider> services, | 282 InterfaceRequest<ServiceProvider> services, |
265 ServiceProviderPtr exposed_services, | 283 ServiceProviderPtr exposed_services, |
266 const CapabilityFilter& filter, | 284 const CapabilityFilter& filter, |
267 const base::Closure& on_application_end, | 285 const base::Closure& on_application_end, |
286 const Shell::ConnectToApplicationCallback& connect_callback, | |
268 NativeApplicationCleanup cleanup, | 287 NativeApplicationCleanup cleanup, |
269 scoped_ptr<Fetcher> fetcher) { | 288 scoped_ptr<Fetcher> fetcher) { |
270 if (!fetcher) { | 289 if (!fetcher) { |
271 // Network error. Drop |application_request| to tell requestor. | 290 // Network error. Drop |application_request| to tell requestor. |
291 connect_callback.Run(kInvalidContentHandlerID); | |
272 return; | 292 return; |
273 } | 293 } |
274 | 294 |
275 GURL redirect_url = fetcher->GetRedirectURL(); | 295 GURL redirect_url = fetcher->GetRedirectURL(); |
276 if (!redirect_url.is_empty()) { | 296 if (!redirect_url.is_empty()) { |
277 // And around we go again... Whee! | 297 // And around we go again... Whee! |
278 // TODO(sky): this loses |requested_url|. | 298 // TODO(sky): this loses |requested_url|. |
279 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 299 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
280 request->url = mojo::String::From(redirect_url.spec()); | 300 request->url = mojo::String::From(redirect_url.spec()); |
281 HttpHeaderPtr header = HttpHeader::New(); | 301 HttpHeaderPtr header = HttpHeader::New(); |
282 header->name = "Referer"; | 302 header->name = "Referer"; |
283 header->value = fetcher->GetRedirectReferer().spec(); | 303 header->value = fetcher->GetRedirectReferer().spec(); |
284 request->headers.push_back(header.Pass()); | 304 request->headers.push_back(header.Pass()); |
285 ConnectToApplication(originator, request.Pass(), qualifier, requestor_url, | 305 ConnectToApplication(originator, request.Pass(), qualifier, requestor_url, |
286 services.Pass(), exposed_services.Pass(), filter, | 306 requesting_content_handler_id, services.Pass(), |
287 on_application_end); | 307 exposed_services.Pass(), filter, on_application_end, |
308 connect_callback); | |
288 return; | 309 return; |
289 } | 310 } |
290 | 311 |
291 // We already checked if the application was running before we fetched it, but | 312 // We already checked if the application was running before we fetched it, but |
292 // it might have started while the fetch was outstanding. We don't want to | 313 // it might have started while the fetch was outstanding. We don't want to |
293 // have two copies of the app running, so check again. | 314 // have two copies of the app running, so check again. |
294 // | 315 // |
295 // Also, it's possible the original URL was redirected to an app that is | 316 // Also, it's possible the original URL was redirected to an app that is |
296 // already running. | 317 // already running. |
297 if (ConnectToRunningApplication(originator, requested_url, qualifier, | 318 if (ConnectToRunningApplication(originator, requested_url, qualifier, |
298 requestor_url, &services, | 319 requestor_url, &services, &exposed_services, |
299 &exposed_services, filter)) { | 320 filter, connect_callback)) { |
300 return; | 321 return; |
301 } | 322 } |
302 | 323 |
303 const GURL app_url = | 324 const GURL app_url = |
304 requested_url.scheme() == "mojo" ? requested_url : fetcher->GetURL(); | 325 requested_url.scheme() == "mojo" ? requested_url : fetcher->GetURL(); |
305 | 326 |
306 InterfaceRequest<Application> request( | 327 ApplicationInstance* app = nullptr; |
307 RegisterInstance(originator, app_url, qualifier, requestor_url, | 328 InterfaceRequest<Application> request(RegisterInstance( |
308 services.Pass(), exposed_services.Pass(), filter, | 329 originator, app_url, qualifier, requestor_url, |
309 on_application_end)); | 330 requesting_content_handler_id, services.Pass(), exposed_services.Pass(), |
331 filter, on_application_end, EmptyConnectCallback(), &app)); | |
310 | 332 |
311 // For resources that are loaded with content handlers, we group app instances | 333 // For resources that are loaded with content handlers, we group app instances |
312 // by site. | 334 // by site. |
313 | 335 |
314 // If the response begins with a #!mojo <content-handler-url>, use it. | 336 // If the response begins with a #!mojo <content-handler-url>, use it. |
315 GURL content_handler_url; | 337 GURL content_handler_url; |
316 std::string shebang; | 338 std::string shebang; |
317 bool enable_multi_process = base::CommandLine::ForCurrentProcess()->HasSwitch( | 339 bool enable_multi_process = base::CommandLine::ForCurrentProcess()->HasSwitch( |
318 switches::kEnableMultiprocess); | 340 switches::kEnableMultiprocess); |
319 | 341 |
320 if (fetcher->PeekContentHandler(&shebang, &content_handler_url)) { | 342 if (fetcher->PeekContentHandler(&shebang, &content_handler_url)) { |
321 URLResponsePtr response(fetcher->AsURLResponse( | 343 URLResponsePtr response(fetcher->AsURLResponse( |
322 blocking_pool_, static_cast<int>(shebang.size()))); | 344 blocking_pool_, static_cast<int>(shebang.size()))); |
323 std::string site = | 345 std::string site = |
324 enable_multi_process ? response->site.To<std::string>() : std::string(); | 346 enable_multi_process ? response->site.To<std::string>() : std::string(); |
325 LoadWithContentHandler(originator, content_handler_url, requestor_url, site, | 347 LoadWithContentHandler(originator, content_handler_url, requestor_url, site, |
326 filter, request.Pass(), response.Pass()); | 348 filter, connect_callback, app, request.Pass(), |
349 response.Pass()); | |
327 return; | 350 return; |
328 } | 351 } |
329 | 352 |
330 MimeTypeToURLMap::iterator iter = mime_type_to_url_.find(fetcher->MimeType()); | 353 MimeTypeToURLMap::iterator iter = mime_type_to_url_.find(fetcher->MimeType()); |
331 if (iter != mime_type_to_url_.end()) { | 354 if (iter != mime_type_to_url_.end()) { |
332 URLResponsePtr response(fetcher->AsURLResponse(blocking_pool_, 0)); | 355 URLResponsePtr response(fetcher->AsURLResponse(blocking_pool_, 0)); |
333 std::string site = | 356 std::string site = |
334 enable_multi_process ? response->site.To<std::string>() : std::string(); | 357 enable_multi_process ? response->site.To<std::string>() : std::string(); |
335 LoadWithContentHandler(originator, iter->second, requestor_url, site, | 358 LoadWithContentHandler(originator, iter->second, requestor_url, site, |
336 filter, request.Pass(), response.Pass()); | 359 filter, connect_callback, app, request.Pass(), |
360 response.Pass()); | |
337 return; | 361 return; |
338 } | 362 } |
339 | 363 |
340 auto alias_iter = application_package_alias_.find(app_url); | 364 auto alias_iter = application_package_alias_.find(app_url); |
341 if (alias_iter != application_package_alias_.end()) { | 365 if (alias_iter != application_package_alias_.end()) { |
342 // We replace the qualifier with the one our package alias requested. | 366 // We replace the qualifier with the one our package alias requested. |
343 URLResponsePtr response(URLResponse::New()); | 367 URLResponsePtr response(URLResponse::New()); |
344 response->url = String::From(app_url.spec()); | 368 response->url = String::From(app_url.spec()); |
345 | 369 |
346 std::string qualifier; | 370 std::string qualifier; |
347 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 371 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
348 switches::kEnableMultiprocess)) { | 372 switches::kEnableMultiprocess)) { |
349 // Why can't we use this in single process mode? Because of | 373 // Why can't we use this in single process mode? Because of |
350 // base::AtExitManager. If you link in ApplicationRunner into | 374 // base::AtExitManager. If you link in ApplicationRunner into |
351 // your code, and then we make initialize multiple copies of the | 375 // your code, and then we make initialize multiple copies of the |
352 // application, we end up with multiple AtExitManagers and will check on | 376 // application, we end up with multiple AtExitManagers and will check on |
353 // the second one being created. | 377 // the second one being created. |
354 // | 378 // |
355 // Why doesn't that happen when running different apps? Because | 379 // Why doesn't that happen when running different apps? Because |
356 // your_thing.mojo!base::AtExitManager and | 380 // your_thing.mojo!base::AtExitManager and |
357 // my_thing.mojo!base::AtExitManager are different symbols. | 381 // my_thing.mojo!base::AtExitManager are different symbols. |
358 qualifier = alias_iter->second.second; | 382 qualifier = alias_iter->second.second; |
359 } | 383 } |
360 | 384 |
361 LoadWithContentHandler(originator, alias_iter->second.first, requestor_url, | 385 LoadWithContentHandler(originator, alias_iter->second.first, requestor_url, |
362 qualifier, filter, request.Pass(), | 386 qualifier, filter, connect_callback, app, |
363 response.Pass()); | 387 request.Pass(), response.Pass()); |
364 return; | 388 return; |
365 } | 389 } |
366 | 390 |
367 // TODO(aa): Sanity check that the thing we got looks vaguely like a mojo | 391 // TODO(aa): Sanity check that the thing we got looks vaguely like a mojo |
368 // application. That could either mean looking for the platform-specific dll | 392 // application. That could either mean looking for the platform-specific dll |
369 // header, or looking for some specific mojo signature prepended to the | 393 // header, or looking for some specific mojo signature prepended to the |
370 // library. | 394 // library. |
371 // TODO(vtl): (Maybe this should be done by the factory/runner?) | 395 // TODO(vtl): (Maybe this should be done by the factory/runner?) |
372 | 396 |
373 GURL base_resolved_url = GetBaseURLAndQuery(fetcher->GetURL(), nullptr); | 397 GURL base_resolved_url = GetBaseURLAndQuery(fetcher->GetURL(), nullptr); |
374 NativeRunnerFactory::Options options; | 398 NativeRunnerFactory::Options options; |
375 if (url_to_native_options_.find(base_resolved_url) != | 399 if (url_to_native_options_.find(base_resolved_url) != |
376 url_to_native_options_.end()) { | 400 url_to_native_options_.end()) { |
377 DVLOG(2) << "Applying stored native options to resolved URL " | 401 DVLOG(2) << "Applying stored native options to resolved URL " |
378 << fetcher->GetURL(); | 402 << fetcher->GetURL(); |
379 options = url_to_native_options_[base_resolved_url]; | 403 options = url_to_native_options_[base_resolved_url]; |
380 } | 404 } |
381 | 405 |
382 // TODO(erg): Have a better way of switching the sandbox on. For now, switch | 406 // TODO(erg): Have a better way of switching the sandbox on. For now, switch |
383 // it on hard coded when we're using some of the sandboxable core services. | 407 // it on hard coded when we're using some of the sandboxable core services. |
384 bool start_sandboxed = false; | 408 bool start_sandboxed = false; |
385 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 409 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
386 switches::kMojoNoSandbox)) { | 410 switches::kMojoNoSandbox)) { |
387 if (app_url == GURL("mojo://core_services/") && qualifier == "Core") | 411 if (app_url == GURL("mojo://core_services/") && qualifier == "Core") |
388 start_sandboxed = true; | 412 start_sandboxed = true; |
389 else if (app_url == GURL("mojo://html_viewer/")) | 413 else if (app_url == GURL("mojo://html_viewer/")) |
390 start_sandboxed = true; | 414 start_sandboxed = true; |
391 } | 415 } |
392 | 416 |
417 connect_callback.Run(kInvalidContentHandlerID); | |
418 | |
393 fetcher->AsPath(blocking_pool_, | 419 fetcher->AsPath(blocking_pool_, |
394 base::Bind(&ApplicationManager::RunNativeApplication, | 420 base::Bind(&ApplicationManager::RunNativeApplication, |
395 weak_ptr_factory_.GetWeakPtr(), | 421 weak_ptr_factory_.GetWeakPtr(), |
396 base::Passed(request.Pass()), start_sandboxed, | 422 base::Passed(request.Pass()), start_sandboxed, |
397 options, cleanup, base::Passed(fetcher.Pass()))); | 423 options, cleanup, base::Passed(fetcher.Pass()))); |
398 } | 424 } |
399 | 425 |
400 void ApplicationManager::RunNativeApplication( | 426 void ApplicationManager::RunNativeApplication( |
401 InterfaceRequest<Application> application_request, | 427 InterfaceRequest<Application> application_request, |
402 bool start_sandboxed, | 428 bool start_sandboxed, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
440 application_package_alias_[alias] = | 466 application_package_alias_[alias] = |
441 std::make_pair(content_handler_package, qualifier); | 467 std::make_pair(content_handler_package, qualifier); |
442 } | 468 } |
443 | 469 |
444 void ApplicationManager::LoadWithContentHandler( | 470 void ApplicationManager::LoadWithContentHandler( |
445 ApplicationInstance* originator, | 471 ApplicationInstance* originator, |
446 const GURL& content_handler_url, | 472 const GURL& content_handler_url, |
447 const GURL& requestor_url, | 473 const GURL& requestor_url, |
448 const std::string& qualifier, | 474 const std::string& qualifier, |
449 const CapabilityFilter& filter, | 475 const CapabilityFilter& filter, |
476 const Shell::ConnectToApplicationCallback& connect_callback, | |
477 ApplicationInstance* app, | |
450 InterfaceRequest<Application> application_request, | 478 InterfaceRequest<Application> application_request, |
451 URLResponsePtr url_response) { | 479 URLResponsePtr url_response) { |
452 ContentHandlerConnection* connection = nullptr; | 480 ContentHandlerConnection* connection = nullptr; |
453 std::pair<GURL, std::string> key(content_handler_url, qualifier); | 481 std::pair<GURL, std::string> key(content_handler_url, qualifier); |
454 // TODO(beng): Figure out the extent to which capability filter should be | 482 // TODO(beng): Figure out the extent to which capability filter should be |
455 // factored into handler identity. | 483 // factored into handler identity. |
456 URLToContentHandlerMap::iterator iter = url_to_content_handler_.find(key); | 484 URLToContentHandlerMap::iterator iter = url_to_content_handler_.find(key); |
457 if (iter != url_to_content_handler_.end()) { | 485 if (iter != url_to_content_handler_.end()) { |
458 connection = iter->second; | 486 connection = iter->second; |
459 } else { | 487 } else { |
460 connection = new ContentHandlerConnection( | 488 connection = new ContentHandlerConnection( |
461 originator, this, content_handler_url, requestor_url, qualifier, | 489 originator, this, content_handler_url, requestor_url, qualifier, filter, |
462 filter); | 490 ++content_handler_id_counter_); |
463 url_to_content_handler_[key] = connection; | 491 url_to_content_handler_[key] = connection; |
464 } | 492 } |
465 | 493 |
494 // TODO(sky): this isn't right if there is a nested content handler. | |
495 app->set_requesting_content_handler_id(connection->id()); | |
466 connection->content_handler()->StartApplication(application_request.Pass(), | 496 connection->content_handler()->StartApplication(application_request.Pass(), |
467 url_response.Pass()); | 497 url_response.Pass()); |
498 connection->ScheduleTargetIdCallback(connect_callback); | |
468 } | 499 } |
469 | 500 |
470 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, | 501 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, |
471 const GURL& url) { | 502 const GURL& url) { |
472 URLToLoaderMap::iterator it = url_to_loader_.find(url); | 503 URLToLoaderMap::iterator it = url_to_loader_.find(url); |
473 if (it != url_to_loader_.end()) | 504 if (it != url_to_loader_.end()) |
474 delete it->second; | 505 delete it->second; |
475 url_to_loader_[url] = loader.release(); | 506 url_to_loader_[url] = loader.release(); |
476 } | 507 } |
477 | 508 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
538 std::find(native_runners_.begin(), native_runners_.end(), runner)); | 569 std::find(native_runners_.begin(), native_runners_.end(), runner)); |
539 } | 570 } |
540 | 571 |
541 ScopedMessagePipeHandle ApplicationManager::ConnectToServiceByName( | 572 ScopedMessagePipeHandle ApplicationManager::ConnectToServiceByName( |
542 const GURL& application_url, | 573 const GURL& application_url, |
543 const std::string& interface_name) { | 574 const std::string& interface_name) { |
544 ServiceProviderPtr services; | 575 ServiceProviderPtr services; |
545 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 576 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
546 request->url = mojo::String::From(application_url.spec()); | 577 request->url = mojo::String::From(application_url.spec()); |
547 ConnectToApplication(nullptr, request.Pass(), std::string(), GURL(), | 578 ConnectToApplication(nullptr, request.Pass(), std::string(), GURL(), |
548 GetProxy(&services), nullptr, | 579 kInvalidContentHandlerID, GetProxy(&services), nullptr, |
549 GetPermissiveCapabilityFilter(), base::Closure()); | 580 GetPermissiveCapabilityFilter(), base::Closure(), |
581 EmptyConnectCallback()); | |
550 MessagePipe pipe; | 582 MessagePipe pipe; |
551 services->ConnectToService(interface_name, pipe.handle1.Pass()); | 583 services->ConnectToService(interface_name, pipe.handle1.Pass()); |
552 return pipe.handle0.Pass(); | 584 return pipe.handle0.Pass(); |
553 } | 585 } |
554 | 586 |
587 Shell::ConnectToApplicationCallback EmptyConnectCallback() { | |
588 return base::Bind(&OnEmptyOnConnectCallback); | |
589 } | |
590 | |
555 } // namespace shell | 591 } // namespace shell |
556 } // namespace mojo | 592 } // namespace mojo |
OLD | NEW |