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_SERVICE_REGISTRATION_MANAGER_H_ | 5 #ifndef EXTENSIONS_BROWSER_SERVICE_REGISTRATION_MANAGER_H_ |
6 #define EXTENSIONS_BROWSER_SERVICE_REGISTRATION_MANAGER_H_ | 6 #define EXTENSIONS_BROWSER_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 |
| 23 class ServiceRegistrationManagerTestApi; |
| 24 |
22 namespace internal { | 25 namespace internal { |
23 | 26 |
24 // A base class for forwarding calls to ServiceRegistry::AddService(). | 27 // A base class for forwarding calls to ServiceRegistry::AddService(). |
25 class ServiceFactoryBase { | 28 class ServiceFactoryBase { |
26 public: | 29 public: |
27 virtual ~ServiceFactoryBase() {} | 30 virtual ~ServiceFactoryBase() {} |
28 | 31 |
29 // Add this service factory to |service_registry|. | 32 // Add this service factory to |service_registry|. |
30 virtual void Register(content::ServiceRegistry* service_registry) const = 0; | 33 virtual void Register(content::ServiceRegistry* service_registry) const = 0; |
| 34 |
| 35 virtual std::string GetName() const = 0; |
31 }; | 36 }; |
32 | 37 |
33 template <typename Interface> | 38 template <typename Interface> |
34 class ServiceFactory : public ServiceFactoryBase { | 39 class ServiceFactory : public ServiceFactoryBase { |
35 public: | 40 public: |
36 explicit ServiceFactory( | 41 explicit ServiceFactory( |
37 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& factory) | 42 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& factory) |
38 : factory_(factory) {} | 43 : factory_(factory) {} |
39 virtual ~ServiceFactory() {} | 44 virtual ~ServiceFactory() {} |
40 | 45 |
41 virtual void Register( | 46 virtual void Register( |
42 content::ServiceRegistry* service_registry) const override { | 47 content::ServiceRegistry* service_registry) const override { |
43 service_registry->AddService(factory_); | 48 service_registry->AddService(factory_); |
44 } | 49 } |
45 | 50 |
| 51 virtual std::string GetName() const override { return Interface::Name_; } |
| 52 |
46 private: | 53 private: |
47 const base::Callback<void(mojo::InterfaceRequest<Interface>)> factory_; | 54 const base::Callback<void(mojo::InterfaceRequest<Interface>)> factory_; |
48 DISALLOW_COPY_AND_ASSIGN(ServiceFactory); | 55 DISALLOW_COPY_AND_ASSIGN(ServiceFactory); |
49 }; | 56 }; |
50 | 57 |
51 } // namespace internal | 58 } // namespace internal |
52 | 59 |
53 // A meta service registry. This allows registration of service factories and | 60 // A meta service registry. This allows registration of service factories and |
54 // their associated extensions API permission name. Whenever a RenderFrameHost | 61 // their associated extensions API permission name. Whenever a RenderFrameHost |
55 // is created, each service that the render frame should have access to (based | 62 // is created, each service that the render frame should have access to (based |
(...skipping 19 matching lines...) Expand all Loading... |
75 factories_.push_back( | 82 factories_.push_back( |
76 std::make_pair(permission_name, | 83 std::make_pair(permission_name, |
77 linked_ptr<internal::ServiceFactoryBase>( | 84 linked_ptr<internal::ServiceFactoryBase>( |
78 new internal::ServiceFactory<Interface>(factory)))); | 85 new internal::ServiceFactory<Interface>(factory)))); |
79 } | 86 } |
80 | 87 |
81 // Adds the service factories appropriate for |render_frame_host| to its | 88 // Adds the service factories appropriate for |render_frame_host| to its |
82 // ServiceRegistry. | 89 // ServiceRegistry. |
83 void AddServicesToRenderFrame(content::RenderFrameHost* render_frame_host); | 90 void AddServicesToRenderFrame(content::RenderFrameHost* render_frame_host); |
84 | 91 |
| 92 protected: |
| 93 virtual void AddServiceToServiceRegistry( |
| 94 content::ServiceRegistry* service_registry, |
| 95 internal::ServiceFactoryBase* service_factory); |
| 96 |
85 private: | 97 private: |
| 98 friend class ServiceRegistrationManagerTestApi; |
| 99 |
| 100 static void SetServiceRegistrationManagerForTest( |
| 101 ServiceRegistrationManager* service_registration_manager); |
| 102 static void ClearServiceRegistrationManagerForTest(); |
| 103 |
86 // All factories and their corresponding API permissions. | 104 // All factories and their corresponding API permissions. |
87 std::vector<std::pair<std::string, linked_ptr<internal::ServiceFactoryBase>>> | 105 std::vector<std::pair<std::string, linked_ptr<internal::ServiceFactoryBase>>> |
88 factories_; | 106 factories_; |
89 | 107 |
90 DISALLOW_COPY_AND_ASSIGN(ServiceRegistrationManager); | 108 DISALLOW_COPY_AND_ASSIGN(ServiceRegistrationManager); |
91 }; | 109 }; |
92 | 110 |
93 } // namespace extensions | 111 } // namespace extensions |
94 | 112 |
95 #endif // EXTENSIONS_BROWSER_SERVICE_REGISTRATION_MANAGER_H_ | 113 #endif // EXTENSIONS_BROWSER_SERVICE_REGISTRATION_MANAGER_H_ |
OLD | NEW |