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