Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / extensions / browser / mojo / service_registration_manager.h
blobe57fba24458fc07eaec81442bba7e523d7e079e4
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
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_BROWSER_MOJO_SERVICE_REGISTRATION_MANAGER_H_
6 #define EXTENSIONS_BROWSER_MOJO_SERVICE_REGISTRATION_MANAGER_H_
8 #include <string>
9 #include <utility>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/memory/linked_ptr.h"
14 #include "content/public/common/service_registry.h"
15 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
17 namespace content {
18 class RenderFrameHost;
21 namespace extensions {
23 namespace internal {
25 // A base class for forwarding calls to ServiceRegistry::AddService().
26 class ServiceFactoryBase {
27 public:
28 virtual ~ServiceFactoryBase() {}
30 // Add this service factory to |service_registry|.
31 virtual void Register(content::ServiceRegistry* service_registry) const = 0;
33 virtual std::string GetName() const = 0;
36 template <typename Interface>
37 class ServiceFactory : public ServiceFactoryBase {
38 public:
39 explicit ServiceFactory(
40 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& factory)
41 : factory_(factory) {}
42 ~ServiceFactory() override {}
44 void Register(content::ServiceRegistry* service_registry) const override {
45 service_registry->AddService(factory_);
48 std::string GetName() const override { return Interface::Name_; }
50 private:
51 const base::Callback<void(mojo::InterfaceRequest<Interface>)> factory_;
52 DISALLOW_COPY_AND_ASSIGN(ServiceFactory);
55 } // namespace internal
57 // A meta service registry. This allows registration of service factories and
58 // their associated extensions API permission name. Whenever a RenderFrameHost
59 // is created, each service that the render frame should have access to (based
60 // on its SiteInstance), is added to the ServiceRegistry for that
61 // RenderFrameHost.
62 class ServiceRegistrationManager {
63 public:
64 ServiceRegistrationManager();
65 virtual ~ServiceRegistrationManager();
67 static ServiceRegistrationManager* GetSharedInstance();
69 // Registers a ServiceFactory to be provided to extensions with the
70 // |permission_name| API permission.
72 // TODO(sammc): Add support for service factories that take the Extension*
73 // (or extension ID) and BrowserContext to allow fine-grained service and
74 // permission customization.
76 // TODO(sammc): Support more flexible service filters than just API permission
77 // names.
78 template <typename Interface>
79 void AddServiceFactory(
80 const std::string& permission_name,
81 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& factory) {
82 factories_.push_back(
83 std::make_pair(permission_name,
84 linked_ptr<internal::ServiceFactoryBase>(
85 new internal::ServiceFactory<Interface>(factory))));
88 // Adds the service factories appropriate for |render_frame_host| to its
89 // ServiceRegistry.
90 void AddServicesToRenderFrame(content::RenderFrameHost* render_frame_host);
92 protected:
93 virtual void AddServiceToServiceRegistry(
94 content::ServiceRegistry* service_registry,
95 internal::ServiceFactoryBase* service_factory);
97 private:
98 friend class TestServiceRegistrationManager;
100 static void SetServiceRegistrationManagerForTest(
101 ServiceRegistrationManager* service_registration_manager);
103 // All factories and their corresponding API permissions.
104 std::vector<std::pair<std::string, linked_ptr<internal::ServiceFactoryBase>>>
105 factories_;
107 DISALLOW_COPY_AND_ASSIGN(ServiceRegistrationManager);
110 } // namespace extensions
112 #endif // EXTENSIONS_BROWSER_MOJO_SERVICE_REGISTRATION_MANAGER_H_