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

Side by Side Diff: services/service_manager/service_manager.cc

Issue 2476063002: Service Manager: Rework Service and ServiceContext lifetime (Closed)
Patch Set: . Created 4 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 "services/service_manager/service_manager.h" 5 #include "services/service_manager/service_manager.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/debug/alias.h" 13 #include "base/debug/alias.h"
14 #include "base/guid.h" 14 #include "base/guid.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/process/process.h" 17 #include "base/process/process.h"
18 #include "base/process/process_handle.h" 18 #include "base/process/process_handle.h"
19 #include "base/stl_util.h" 19 #include "base/stl_util.h"
20 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
21 #include "base/trace_event/trace_event.h" 21 #include "base/trace_event/trace_event.h"
22 #include "mojo/public/cpp/bindings/binding.h" 22 #include "mojo/public/cpp/bindings/binding.h"
23 #include "mojo/public/cpp/bindings/binding_set.h" 23 #include "mojo/public/cpp/bindings/binding_set.h"
24 #include "services/service_manager/connect_util.h" 24 #include "services/service_manager/connect_util.h"
25 #include "services/service_manager/public/cpp/connector.h" 25 #include "services/service_manager/public/cpp/connector.h"
26 #include "services/service_manager/public/cpp/interface_registry.h"
26 #include "services/service_manager/public/cpp/names.h" 27 #include "services/service_manager/public/cpp/names.h"
28 #include "services/service_manager/public/cpp/service.h"
27 #include "services/service_manager/public/cpp/service_context.h" 29 #include "services/service_manager/public/cpp/service_context.h"
28 #include "services/service_manager/public/interfaces/connector.mojom.h" 30 #include "services/service_manager/public/interfaces/connector.mojom.h"
29 #include "services/service_manager/public/interfaces/service.mojom.h" 31 #include "services/service_manager/public/interfaces/service.mojom.h"
30 #include "services/service_manager/public/interfaces/service_manager.mojom.h" 32 #include "services/service_manager/public/interfaces/service_manager.mojom.h"
31 33
32 namespace service_manager { 34 namespace service_manager {
33 35
34 namespace { 36 namespace {
35 37
36 const char kCatalogName[] = "service:catalog"; 38 const char kCatalogName[] = "service:catalog";
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 mojo::BindingSet<mojom::ServiceManager> service_manager_bindings_; 447 mojo::BindingSet<mojom::ServiceManager> service_manager_bindings_;
446 base::ProcessId pid_ = base::kNullProcessId; 448 base::ProcessId pid_ = base::kNullProcessId;
447 Instance* parent_ = nullptr; 449 Instance* parent_ = nullptr;
448 InstanceMap children_; 450 InstanceMap children_;
449 State state_; 451 State state_;
450 base::WeakPtrFactory<Instance> weak_factory_; 452 base::WeakPtrFactory<Instance> weak_factory_;
451 453
452 DISALLOW_COPY_AND_ASSIGN(Instance); 454 DISALLOW_COPY_AND_ASSIGN(Instance);
453 }; 455 };
454 456
457 class ServiceManager::ServiceImpl : public Service {
458 public:
459 explicit ServiceImpl(ServiceManager* service_manager)
460 : service_manager_(service_manager) {}
461 ~ServiceImpl() override {}
462
463 // Service:
464 bool OnConnect(const ServiceInfo& remote_info,
465 InterfaceRegistry* registry) override {
466 // The only interface ServiceManager exposes is mojom::ServiceManager, and
467 // access to this interface is brokered by a policy specific to each caller,
468 // managed by the caller's instance. Here we look to see who's calling,
469 // and forward to the caller's instance to continue.
470 Instance* instance = nullptr;
471 for (const auto& entry : service_manager_->identity_to_instance_) {
472 if (entry.first == remote_info.identity) {
473 instance = entry.second;
474 break;
475 }
476 }
477
478 DCHECK(instance);
479 return instance->OnConnect(remote_info, registry);
480 }
481
482 private:
483 ServiceManager* const service_manager_;
484
485 DISALLOW_COPY_AND_ASSIGN(ServiceImpl);
486 };
487
455 // static 488 // static
456 ServiceManager::TestAPI::TestAPI(ServiceManager* service_manager) 489 ServiceManager::TestAPI::TestAPI(ServiceManager* service_manager)
457 : service_manager_(service_manager) {} 490 : service_manager_(service_manager) {}
458 ServiceManager::TestAPI::~TestAPI() {} 491 ServiceManager::TestAPI::~TestAPI() {}
459 492
460 bool ServiceManager::TestAPI::HasRunningInstanceForName( 493 bool ServiceManager::TestAPI::HasRunningInstanceForName(
461 const std::string& name) const { 494 const std::string& name) const {
462 for (const auto& entry : service_manager_->identity_to_instance_) { 495 for (const auto& entry : service_manager_->identity_to_instance_) {
463 if (entry.first.name() == name) 496 if (entry.first.name() == name)
464 return true; 497 return true;
(...skipping 18 matching lines...) Expand all
483 spec.requires["*"].insert("service_manager:service_factory"); 516 spec.requires["*"].insert("service_manager:service_factory");
484 spec.requires["service:catalog"].insert("service_manager:resolver"); 517 spec.requires["service:catalog"].insert("service_manager:resolver");
485 spec.requires["service:tracing"].insert("app"); 518 spec.requires["service:tracing"].insert("app");
486 InterfaceProviderSpecMap specs; 519 InterfaceProviderSpecMap specs;
487 specs[mojom::kServiceManager_ConnectorSpec] = spec; 520 specs[mojom::kServiceManager_ConnectorSpec] = spec;
488 521
489 service_manager_instance_ = 522 service_manager_instance_ =
490 CreateInstance(Identity(), CreateServiceManagerIdentity(), specs); 523 CreateInstance(Identity(), CreateServiceManagerIdentity(), specs);
491 service_manager_instance_->StartWithService(std::move(service)); 524 service_manager_instance_->StartWithService(std::move(service));
492 singletons_.insert(kServiceManagerName); 525 singletons_.insert(kServiceManagerName);
493 service_context_.reset(new ServiceContext(this, std::move(request))); 526 service_context_.reset(new ServiceContext(
527 base::MakeUnique<ServiceImpl>(this), std::move(request)));
494 528
495 if (catalog) 529 if (catalog)
496 InitCatalog(std::move(catalog)); 530 InitCatalog(std::move(catalog));
497 } 531 }
498 532
499 ServiceManager::~ServiceManager() { 533 ServiceManager::~ServiceManager() {
500 // Ensure we tear down the ServiceManager instance last. This is to avoid 534 // Ensure we tear down the ServiceManager instance last. This is to avoid
501 // hitting bindings DCHECKs, since the ServiceManager or Catalog may at any 535 // hitting bindings DCHECKs, since the ServiceManager or Catalog may at any
502 // given time own in-flight responders for Instances' Connector requests. 536 // given time own in-flight responders for Instances' Connector requests.
503 std::unique_ptr<Instance> service_manager_instance; 537 std::unique_ptr<Instance> service_manager_instance;
(...skipping 27 matching lines...) Expand all
531 params->set_target(embedder_identity); 565 params->set_target(embedder_identity);
532 566
533 mojom::ServicePtr service; 567 mojom::ServicePtr service;
534 mojom::ServiceRequest request = mojo::GetProxy(&service); 568 mojom::ServiceRequest request = mojo::GetProxy(&service);
535 Connect(std::move(params), std::move(service), nullptr); 569 Connect(std::move(params), std::move(service), nullptr);
536 570
537 return request; 571 return request;
538 } 572 }
539 573
540 //////////////////////////////////////////////////////////////////////////////// 574 ////////////////////////////////////////////////////////////////////////////////
541 // ServiceManager, Service implementation:
542
543 bool ServiceManager::OnConnect(const ServiceInfo& remote_info,
544 InterfaceRegistry* registry) {
545 // The only interface we expose is mojom::ServiceManager, and access to this
546 // interface is brokered by a policy specific to each caller, managed by the
547 // caller's instance. Here we look to see who's calling, and forward to the
548 // caller's instance to continue.
549 Instance* instance = nullptr;
550 for (const auto& entry : identity_to_instance_) {
551 if (entry.first == remote_info.identity) {
552 instance = entry.second;
553 break;
554 }
555 }
556 DCHECK(instance);
557 return instance->OnConnect(remote_info, registry);
558 }
559
560 ////////////////////////////////////////////////////////////////////////////////
561 // ServiceManager, private: 575 // ServiceManager, private:
562 576
563 void ServiceManager::InitCatalog(mojom::ServicePtr catalog) { 577 void ServiceManager::InitCatalog(mojom::ServicePtr catalog) {
564 // TODO(beng): It'd be great to build this from the manifest, however there's 578 // TODO(beng): It'd be great to build this from the manifest, however there's
565 // a bit of a chicken-and-egg problem. 579 // a bit of a chicken-and-egg problem.
566 InterfaceProviderSpec spec; 580 InterfaceProviderSpec spec;
567 spec.provides["app"].insert("filesystem::mojom::Directory"); 581 spec.provides["app"].insert("filesystem::mojom::Directory");
568 spec.provides["catalog:catalog"].insert("catalog::mojom::Catalog"); 582 spec.provides["catalog:catalog"].insert("catalog::mojom::Catalog");
569 spec.provides["service_manager:resolver"].insert( 583 spec.provides["service_manager:resolver"].insert(
570 "service_manager::mojom::Resolver"); 584 "service_manager::mojom::Resolver");
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
901 // Now that the instance has a Service, we can connect to it. 915 // Now that the instance has a Service, we can connect to it.
902 bool connected = instance->ConnectToService(&params); 916 bool connected = instance->ConnectToService(&params);
903 DCHECK(connected); 917 DCHECK(connected);
904 } 918 }
905 919
906 base::WeakPtr<ServiceManager> ServiceManager::GetWeakPtr() { 920 base::WeakPtr<ServiceManager> ServiceManager::GetWeakPtr() {
907 return weak_ptr_factory_.GetWeakPtr(); 921 return weak_ptr_factory_.GetWeakPtr();
908 } 922 }
909 923
910 } // namespace service_manager 924 } // namespace service_manager
OLDNEW
« no previous file with comments | « services/service_manager/service_manager.h ('k') | services/service_manager/tests/connect/connect_test_app.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698