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