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 <map> | |
8 #include <string> | 9 #include <string> |
9 #include <utility> | 10 #include <utility> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/callback.h" | 13 #include "base/callback.h" |
13 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
14 #include "content/public/common/service_registry.h" | 15 #include "content/public/common/service_registry.h" |
15 #include "mojo/public/cpp/bindings/interface_request.h" | 16 #include "mojo/public/cpp/bindings/interface_request.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 class RenderFrameHost; | 19 class RenderFrameHost; |
19 } | 20 } |
20 | 21 |
21 namespace extensions { | 22 namespace extensions { |
22 namespace internal { | 23 namespace internal { |
23 | 24 |
24 class ServiceFactoryBase { | 25 class ServiceFactoryBase { |
25 public: | 26 public: |
26 virtual ~ServiceFactoryBase() {} | 27 virtual ~ServiceFactoryBase() {} |
27 | 28 |
28 // Add this service factory to |service_registry|. | 29 // Add this service factory to |service_registry|. |
29 virtual void Register(content::ServiceRegistry* service_registry) const = 0; | 30 virtual void Register(content::ServiceRegistry* service_registry) const = 0; |
31 | |
32 virtual std::string GetName() const = 0; | |
30 }; | 33 }; |
31 | 34 |
32 template <typename Interface> | 35 template <typename Interface> |
33 class ServiceFactory : public ServiceFactoryBase { | 36 class ServiceFactory : public ServiceFactoryBase { |
34 public: | 37 public: |
35 explicit ServiceFactory( | 38 explicit ServiceFactory( |
36 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& factory) | 39 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& factory) |
37 : factory_(factory) {} | 40 : factory_(factory) {} |
38 virtual ~ServiceFactory() {} | 41 virtual ~ServiceFactory() {} |
39 | 42 |
40 virtual void Register( | 43 virtual void Register( |
41 content::ServiceRegistry* service_registry) const override { | 44 content::ServiceRegistry* service_registry) const override { |
42 service_registry->AddService(factory_); | 45 service_registry->AddService(factory_); |
43 } | 46 } |
44 | 47 |
48 virtual 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 |
(...skipping 15 matching lines...) Expand all Loading... | |
70 template <typename Interface> | 75 template <typename Interface> |
71 void AddServiceFactory( | 76 void AddServiceFactory( |
72 const std::string& permission_name, | 77 const std::string& permission_name, |
73 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& factory) { | 78 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& factory) { |
74 factories_.push_back( | 79 factories_.push_back( |
75 std::make_pair(permission_name, | 80 std::make_pair(permission_name, |
76 linked_ptr<internal::ServiceFactoryBase>( | 81 linked_ptr<internal::ServiceFactoryBase>( |
77 new internal::ServiceFactory<Interface>(factory)))); | 82 new internal::ServiceFactory<Interface>(factory)))); |
78 } | 83 } |
79 | 84 |
85 // Overrides an existing service factory with |factory| for testing. This | |
86 // does not alter the permission checks used to determine whether a service | |
87 // is available. | |
88 template <typename Interface> | |
89 void OverrideServiceFactoryForTest( | |
raymes
2014/10/15 21:07:09
It seems a bit nicer to me to avoid having this te
Sam McNally
2014/10/20 07:21:07
Done.
| |
90 const base::Callback<void(mojo::InterfaceRequest<Interface>)>& factory) { | |
91 bool inserted = | |
92 test_factories_.insert(std::make_pair( | |
93 Interface::Name_, | |
94 linked_ptr<internal::ServiceFactoryBase>( | |
95 new internal::ServiceFactory<Interface>( | |
96 factory)))).second; | |
97 DCHECK(inserted); | |
98 } | |
99 | |
100 // Clears any overrides created using OverrideServiceFactoryForTest(). | |
101 void ClearOverridesForTest(); | |
102 | |
80 // Adds the service factories appropriate for |render_frame_host| to its | 103 // Adds the service factories appropriate for |render_frame_host| to its |
81 // ServiceRegistry. | 104 // ServiceRegistry. |
82 void AddToRenderFrame(content::RenderFrameHost* render_frame_host); | 105 void AddToRenderFrame(content::RenderFrameHost* render_frame_host); |
83 | 106 |
84 private: | 107 private: |
85 // All factories and their corresponding API permissions. | 108 // All factories and their corresponding API permissions. |
86 std::vector<std::pair<std::string, linked_ptr<internal::ServiceFactoryBase>>> | 109 std::vector<std::pair<std::string, linked_ptr<internal::ServiceFactoryBase>>> |
87 factories_; | 110 factories_; |
88 | 111 |
112 std::map<std::string, linked_ptr<internal::ServiceFactoryBase>> | |
113 test_factories_; | |
114 | |
89 DISALLOW_COPY_AND_ASSIGN(ServiceRegistrationManager); | 115 DISALLOW_COPY_AND_ASSIGN(ServiceRegistrationManager); |
90 }; | 116 }; |
91 | 117 |
92 } // namespace extensions | 118 } // namespace extensions |
93 | 119 |
94 #endif // EXTENSIONS_BROWSER_SERVICE_REGISTRATION_MANAGER_H_ | 120 #endif // EXTENSIONS_BROWSER_SERVICE_REGISTRATION_MANAGER_H_ |
OLD | NEW |