Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: shell/application_manager/application_manager.cc

Issue 1444433002: Added anti-singleton services (new process/thread per connection). (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Made MakeApplicationIdentity, updated README Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <inttypes.h>
6
5 #include "shell/application_manager/application_manager.h" 7 #include "shell/application_manager/application_manager.h"
viettrungluu 2015/11/13 18:41:26 This include should go *first* (see the style guid
Sean Klein 2015/11/16 02:24:26 Done (well, removed, actually, since no longer use
6 8
7 #include "base/bind.h" 9 #include "base/bind.h"
8 #include "base/command_line.h" 10 #include "base/command_line.h"
9 #include "base/logging.h" 11 #include "base/logging.h"
10 #include "base/macros.h" 12 #include "base/macros.h"
11 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
12 #include "base/trace_event/trace_event.h" 14 #include "base/trace_event/trace_event.h"
13 #include "mojo/public/cpp/bindings/binding.h" 15 #include "mojo/public/cpp/bindings/binding.h"
14 #include "mojo/services/authenticating_url_loader_interceptor/interfaces/authent icating_url_loader_interceptor_meta_factory.mojom.h" 16 #include "mojo/services/authenticating_url_loader_interceptor/interfaces/authent icating_url_loader_interceptor_meta_factory.mojom.h"
15 #include "mojo/services/authentication/interfaces/authentication.mojom.h" 17 #include "mojo/services/authentication/interfaces/authentication.mojom.h"
(...skipping 21 matching lines...) Expand all
37 const std::vector<std::string>& v2) { 39 const std::vector<std::string>& v2) {
38 if (!v1.size()) 40 if (!v1.size())
39 return v2; 41 return v2;
40 if (!v2.size()) 42 if (!v2.size())
41 return v1; 43 return v1;
42 std::vector<std::string> result(v1); 44 std::vector<std::string> result(v1);
43 result.insert(result.end(), v1.begin(), v1.end()); 45 result.insert(result.end(), v1.begin(), v1.end());
44 return result; 46 return result;
45 } 47 }
46 48
49 std::string create_id_string(uint64_t uid) {
viettrungluu 2015/11/13 18:41:26 Nit: Functions should be NamedLikeThis.
Sean Klein 2015/11/16 02:24:26 Done (well, also removed)
50 // 20 == Max number of digits in a 64 bit integer.
51 char buffer[21];
52 CHECK_EQ(20, snprintf(buffer, sizeof(buffer), "%020" PRIu64, uid));
viettrungluu 2015/11/13 18:41:26 Probably you should just use one of the functions
Sean Klein 2015/11/16 02:24:26 Updating to "Uint64ToString", which eliminates the
53 return std::string(buffer);
54 }
55
47 } // namespace 56 } // namespace
48 57
49 class ApplicationManager::ContentHandlerConnection { 58 class ApplicationManager::ContentHandlerConnection {
50 public: 59 public:
51 ContentHandlerConnection(ApplicationManager* manager, 60 ContentHandlerConnection(ApplicationManager* manager, Identity id)
52 const GURL& content_handler_url) 61 : manager_(manager), id_(id) {
53 : manager_(manager), content_handler_url_(content_handler_url) {
54 ServiceProviderPtr services; 62 ServiceProviderPtr services;
55 manager->ConnectToApplication(content_handler_url, GURL(), 63 manager->ConnectToApplication(id_.url, GURL(), mojo::GetProxy(&services),
56 mojo::GetProxy(&services), nullptr, 64 nullptr, base::Closure());
57 base::Closure());
58 mojo::MessagePipe pipe; 65 mojo::MessagePipe pipe;
59 content_handler_.Bind( 66 content_handler_.Bind(
60 mojo::InterfacePtrInfo<mojo::ContentHandler>(pipe.handle0.Pass(), 0u)); 67 mojo::InterfacePtrInfo<mojo::ContentHandler>(pipe.handle0.Pass(), 0u));
61 services->ConnectToService(mojo::ContentHandler::Name_, 68 services->ConnectToService(mojo::ContentHandler::Name_,
62 pipe.handle1.Pass()); 69 pipe.handle1.Pass());
63 content_handler_.set_connection_error_handler( 70 content_handler_.set_connection_error_handler(
64 [this]() { manager_->OnContentHandlerError(this); }); 71 [this]() { manager_->OnContentHandlerError(this); });
65 } 72 }
66 ~ContentHandlerConnection() {} 73 ~ContentHandlerConnection() {}
67 74
68 mojo::ContentHandler* content_handler() { return content_handler_.get(); } 75 mojo::ContentHandler* content_handler() { return content_handler_.get(); }
69 76
70 GURL content_handler_url() { return content_handler_url_; } 77 GURL content_handler_url() { return id_.url; }
78
79 Identity get_identity() { return id_; }
viettrungluu 2015/11/13 18:41:26 nit: Getters of this type usually omit the word "g
Sean Klein 2015/11/16 02:24:26 Done (renamed to identity, added const, returning
71 80
72 private: 81 private:
73 ApplicationManager* manager_; 82 ApplicationManager* manager_;
74 GURL content_handler_url_; 83 Identity id_;
viettrungluu 2015/11/13 18:41:26 nit: Maybe this should be const. (And I guess the
Sean Klein 2015/11/16 02:24:26 Done.
75 mojo::ContentHandlerPtr content_handler_; 84 mojo::ContentHandlerPtr content_handler_;
76 85
77 DISALLOW_COPY_AND_ASSIGN(ContentHandlerConnection); 86 DISALLOW_COPY_AND_ASSIGN(ContentHandlerConnection);
78 }; 87 };
79 88
80 // static 89 // static
81 ApplicationManager::TestAPI::TestAPI(ApplicationManager* manager) 90 ApplicationManager::TestAPI::TestAPI(ApplicationManager* manager)
82 : manager_(manager) { 91 : manager_(manager) {
83 } 92 }
84 93
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 bool ApplicationManager::ConnectToRunningApplication( 232 bool ApplicationManager::ConnectToRunningApplication(
224 const GURL& resolved_url, 233 const GURL& resolved_url,
225 const GURL& requestor_url, 234 const GURL& requestor_url,
226 InterfaceRequest<ServiceProvider>* services, 235 InterfaceRequest<ServiceProvider>* services,
227 ServiceProviderPtr* exposed_services) { 236 ServiceProviderPtr* exposed_services) {
228 GURL application_url = GetBaseURLAndQuery(resolved_url, nullptr); 237 GURL application_url = GetBaseURLAndQuery(resolved_url, nullptr);
229 ShellImpl* shell_impl = GetShellImpl(application_url); 238 ShellImpl* shell_impl = GetShellImpl(application_url);
230 if (!shell_impl) 239 if (!shell_impl)
231 return false; 240 return false;
232 241
242 DCHECK(!this->GetNativeApplicationOptionsForURL(application_url)
viettrungluu 2015/11/13 18:41:26 No need for "this->" (here and elsewhere).
Sean Klein 2015/11/16 02:24:26 Done.
243 ->force_unique_process);
244
233 ConnectToClient(shell_impl, resolved_url, requestor_url, services->Pass(), 245 ConnectToClient(shell_impl, resolved_url, requestor_url, services->Pass(),
234 exposed_services->Pass()); 246 exposed_services->Pass());
235 return true; 247 return true;
236 } 248 }
237 249
238 bool ApplicationManager::ConnectToApplicationWithLoader( 250 bool ApplicationManager::ConnectToApplicationWithLoader(
239 const GURL& resolved_url, 251 const GURL& resolved_url,
240 const GURL& requestor_url, 252 const GURL& requestor_url,
241 InterfaceRequest<ServiceProvider>* services, 253 InterfaceRequest<ServiceProvider>* services,
242 ServiceProviderPtr* exposed_services, 254 ServiceProviderPtr* exposed_services,
243 const base::Closure& on_application_end, 255 const base::Closure& on_application_end,
244 const std::vector<std::string>& parameters, 256 const std::vector<std::string>& parameters,
245 ApplicationLoader* loader) { 257 ApplicationLoader* loader) {
246 if (!loader) 258 if (!loader)
247 return false; 259 return false;
248 260
249 loader->Load( 261 loader->Load(
250 resolved_url, 262 resolved_url,
251 RegisterShell(resolved_url, requestor_url, services->Pass(), 263 RegisterShell(resolved_url, requestor_url, services->Pass(),
252 exposed_services->Pass(), on_application_end, parameters)); 264 exposed_services->Pass(), on_application_end, parameters));
253 return true; 265 return true;
254 } 266 }
255 267
268 Identity ApplicationManager::MakeApplicationIdentity(const GURL& resolved_url) {
269 static uint64_t uid = 1;
viettrungluu 2015/11/13 18:41:26 nit: Calling it "uid" is a little strange.
Sean Klein 2015/11/16 02:24:26 Renaming to "unique_id_number", since it is intend
270 bool force_unique_process = this->GetNativeApplicationOptionsForURL(
271 GetBaseURLAndQuery(resolved_url, nullptr))
272 ->force_unique_process;
273 if (force_unique_process) {
viettrungluu 2015/11/13 18:41:26 Style nits: - In this file/directory, we omit brac
Sean Klein 2015/11/16 02:24:26 Done.
274 return Identity(resolved_url, create_id_string(uid++));
275 } else {
276 return Identity(resolved_url);
277 }
278 }
279
256 InterfaceRequest<Application> ApplicationManager::RegisterShell( 280 InterfaceRequest<Application> ApplicationManager::RegisterShell(
257 const GURL& resolved_url, 281 const GURL& resolved_url,
258 const GURL& requestor_url, 282 const GURL& requestor_url,
259 InterfaceRequest<ServiceProvider> services, 283 InterfaceRequest<ServiceProvider> services,
260 ServiceProviderPtr exposed_services, 284 ServiceProviderPtr exposed_services,
261 const base::Closure& on_application_end, 285 const base::Closure& on_application_end,
262 const std::vector<std::string>& parameters) { 286 const std::vector<std::string>& parameters) {
263 Identity app_identity(resolved_url); 287 Identity app_identity = MakeApplicationIdentity(resolved_url);
264 288
265 mojo::ApplicationPtr application; 289 mojo::ApplicationPtr application;
266 InterfaceRequest<Application> application_request = 290 InterfaceRequest<Application> application_request =
267 mojo::GetProxy(&application); 291 mojo::GetProxy(&application);
268 ShellImpl* shell = 292 ShellImpl* shell =
269 new ShellImpl(application.Pass(), this, app_identity, on_application_end); 293 new ShellImpl(application.Pass(), this, app_identity, on_application_end);
270 identity_to_shell_impl_[app_identity] = make_scoped_ptr(shell); 294 identity_to_shell_impl_[app_identity] = make_scoped_ptr(shell);
271 shell->InitializeApplication(mojo::Array<mojo::String>::From(parameters)); 295 shell->InitializeApplication(mojo::Array<mojo::String>::From(parameters));
272 ConnectToClient(shell, resolved_url, requestor_url, services.Pass(), 296 ConnectToClient(shell, resolved_url, requestor_url, services.Pass(),
273 exposed_services.Pass()); 297 exposed_services.Pass());
274 return application_request; 298 return application_request;
275 } 299 }
276 300
301 // Note: If a service was created with a unique ID, intending to be unique
302 // (such that multiple requests for a service result in unique processes), then
303 // 'GetShellImpl' should return nullptr.
277 ShellImpl* ApplicationManager::GetShellImpl(const GURL& url) { 304 ShellImpl* ApplicationManager::GetShellImpl(const GURL& url) {
278 const auto& shell_it = identity_to_shell_impl_.find(Identity(url)); 305 const auto& shell_it = identity_to_shell_impl_.find(Identity(url));
279 if (shell_it != identity_to_shell_impl_.end()) 306 if (shell_it != identity_to_shell_impl_.end())
280 return shell_it->second.get(); 307 return shell_it->second.get();
281 return nullptr; 308 return nullptr;
282 } 309 }
283 310
284 void ApplicationManager::ConnectToClient( 311 void ApplicationManager::ConnectToClient(
285 ShellImpl* shell_impl, 312 ShellImpl* shell_impl,
286 const GURL& resolved_url, 313 const GURL& resolved_url,
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 DCHECK(content_handler_url.is_valid()) 430 DCHECK(content_handler_url.is_valid())
404 << "Content handler URL is invalid for mime type " << mime_type; 431 << "Content handler URL is invalid for mime type " << mime_type;
405 mime_type_to_url_[mime_type] = content_handler_url; 432 mime_type_to_url_[mime_type] = content_handler_url;
406 } 433 }
407 434
408 void ApplicationManager::LoadWithContentHandler( 435 void ApplicationManager::LoadWithContentHandler(
409 const GURL& content_handler_url, 436 const GURL& content_handler_url,
410 InterfaceRequest<Application> application_request, 437 InterfaceRequest<Application> application_request,
411 mojo::URLResponsePtr url_response) { 438 mojo::URLResponsePtr url_response) {
412 ContentHandlerConnection* connection = nullptr; 439 ContentHandlerConnection* connection = nullptr;
413 auto it = url_to_content_handler_.find(content_handler_url); 440 Identity content_handler_id = MakeApplicationIdentity(content_handler_url);
414 if (it != url_to_content_handler_.end()) { 441 auto it = identity_to_content_handler_.find(content_handler_id);
442 if (it != identity_to_content_handler_.end()) {
415 connection = it->second.get(); 443 connection = it->second.get();
416 } else { 444 } else {
417 connection = new ContentHandlerConnection(this, content_handler_url); 445 connection = new ContentHandlerConnection(this, content_handler_id);
418 url_to_content_handler_[content_handler_url] = make_scoped_ptr(connection); 446 identity_to_content_handler_[content_handler_id] =
447 make_scoped_ptr(connection);
419 } 448 }
420 449
421 connection->content_handler()->StartApplication(application_request.Pass(), 450 connection->content_handler()->StartApplication(application_request.Pass(),
422 url_response.Pass()); 451 url_response.Pass());
423 } 452 }
424 453
425 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, 454 void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader,
426 const GURL& url) { 455 const GURL& url) {
427 url_to_loader_[url] = loader.Pass(); 456 url_to_loader_[url] = loader.Pass();
428 } 457 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 auto it = identity_to_shell_impl_.find(identity); 507 auto it = identity_to_shell_impl_.find(identity);
479 DCHECK(it != identity_to_shell_impl_.end()); 508 DCHECK(it != identity_to_shell_impl_.end());
480 identity_to_shell_impl_.erase(it); 509 identity_to_shell_impl_.erase(it);
481 if (!on_application_end.is_null()) 510 if (!on_application_end.is_null())
482 on_application_end.Run(); 511 on_application_end.Run();
483 } 512 }
484 513
485 void ApplicationManager::OnContentHandlerError( 514 void ApplicationManager::OnContentHandlerError(
486 ContentHandlerConnection* content_handler) { 515 ContentHandlerConnection* content_handler) {
487 // Remove the mapping to the content handler. 516 // Remove the mapping to the content handler.
488 auto it = 517 auto it = identity_to_content_handler_.find(content_handler->get_identity());
489 url_to_content_handler_.find(content_handler->content_handler_url()); 518 DCHECK(it != identity_to_content_handler_.end());
490 DCHECK(it != url_to_content_handler_.end()); 519 identity_to_content_handler_.erase(it);
491 url_to_content_handler_.erase(it);
492 } 520 }
493 521
494 mojo::ScopedMessagePipeHandle ApplicationManager::ConnectToServiceByName( 522 mojo::ScopedMessagePipeHandle ApplicationManager::ConnectToServiceByName(
495 const GURL& application_url, 523 const GURL& application_url,
496 const std::string& interface_name) { 524 const std::string& interface_name) {
497 ServiceProviderPtr services; 525 ServiceProviderPtr services;
498 ConnectToApplication(application_url, GURL(), mojo::GetProxy(&services), 526 ConnectToApplication(application_url, GURL(), mojo::GetProxy(&services),
499 nullptr, base::Closure()); 527 nullptr, base::Closure());
500 mojo::MessagePipe pipe; 528 mojo::MessagePipe pipe;
501 services->ConnectToService(interface_name, pipe.handle1.Pass()); 529 services->ConnectToService(interface_name, pipe.handle1.Pass());
502 return pipe.handle0.Pass(); 530 return pipe.handle0.Pass();
503 } 531 }
504 532
505 std::vector<std::string> ApplicationManager::GetArgsForURL(const GURL& url) { 533 std::vector<std::string> ApplicationManager::GetArgsForURL(const GURL& url) {
506 const auto& args_it = url_to_args_.find(url); 534 const auto& args_it = url_to_args_.find(url);
507 if (args_it != url_to_args_.end()) 535 if (args_it != url_to_args_.end())
508 return args_it->second; 536 return args_it->second;
509 return std::vector<std::string>(); 537 return std::vector<std::string>();
510 } 538 }
511 539
512 void ApplicationManager::CleanupRunner(NativeRunner* runner) { 540 void ApplicationManager::CleanupRunner(NativeRunner* runner) {
513 native_runners_.erase( 541 native_runners_.erase(
514 std::find(native_runners_.begin(), native_runners_.end(), runner)); 542 std::find(native_runners_.begin(), native_runners_.end(), runner));
515 } 543 }
516 544
517 } // namespace shell 545 } // namespace shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698