| 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_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ | 5 #ifndef EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ |
| 6 #define EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ | 6 #define EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 9 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 10 #include "components/keyed_service/content/browser_context_keyed_service_factory
.h" | 10 #include "components/keyed_service/content/browser_context_keyed_service_factory
.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 // static base::LazyInstance<BrowserContextKeyedAPIFactory<HistoryAPI>>:: | 59 // static base::LazyInstance<BrowserContextKeyedAPIFactory<HistoryAPI>>:: |
| 60 // DestructorAtExit g_factory = LAZY_INSTANCE_INITIALIZER; | 60 // DestructorAtExit g_factory = LAZY_INSTANCE_INITIALIZER; |
| 61 // | 61 // |
| 62 // // static | 62 // // static |
| 63 // BrowserContextKeyedAPIFactory<HistoryAPI>* | 63 // BrowserContextKeyedAPIFactory<HistoryAPI>* |
| 64 // HistoryAPI::GetFactoryInstance() { | 64 // HistoryAPI::GetFactoryInstance() { |
| 65 // return g_factory.Pointer(); | 65 // return g_factory.Pointer(); |
| 66 // } | 66 // } |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 // Declare dependencies on other factories. |
| 70 // By default, ExtensionSystemFactory is the only dependency; however, |
| 71 // specializations can override this. Declare your specialization in |
| 72 // your header file after the BrowserContextKeyedAPI class definition. |
| 73 // Declare this struct in the header file. The implementation may optionally |
| 74 // be placed in your .cc file. |
| 75 // This method should be used instead of |
| 76 // BrowserContextKeyedAPIFactory<T>::DeclareFactoryDependencies() because it |
| 77 // permits partial specialization, as in the case of ApiResourceManager<T>. |
| 78 // |
| 79 // template <> |
| 80 // struct BrowserContextFactoryDependencies<MyService> { |
| 81 // static void DeclareFactoryDependencies( |
| 82 // BrowserContextKeyedAPIFactory<ApiResourceManager<T>>* factory) { |
| 83 // factory->DependsOn( |
| 84 // ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 85 // factory->DependsOn(ProfileSyncServiceFactory::GetInstance()); |
| 86 // ... |
| 87 // } |
| 88 // }; |
| 89 template <typename T> |
| 90 struct BrowserContextFactoryDependencies { |
| 91 static void DeclareFactoryDependencies( |
| 92 BrowserContextKeyedAPIFactory<T>* factory) { |
| 93 factory->DependsOn( |
| 94 ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 95 } |
| 96 }; |
| 97 |
| 69 // A template for factories for KeyedServices that manage extension APIs. T is | 98 // A template for factories for KeyedServices that manage extension APIs. T is |
| 70 // a KeyedService that uses this factory template instead of its own separate | 99 // a KeyedService that uses this factory template instead of its own separate |
| 71 // factory definition to manage its per-profile instances. | 100 // factory definition to manage its per-profile instances. |
| 72 template <typename T> | 101 template <typename T> |
| 73 class BrowserContextKeyedAPIFactory : public BrowserContextKeyedServiceFactory { | 102 class BrowserContextKeyedAPIFactory : public BrowserContextKeyedServiceFactory { |
| 74 public: | 103 public: |
| 75 static T* Get(content::BrowserContext* context) { | 104 static T* Get(content::BrowserContext* context) { |
| 76 return static_cast<T*>( | 105 return static_cast<T*>( |
| 77 T::GetFactoryInstance()->GetServiceForBrowserContext(context, true)); | 106 T::GetFactoryInstance()->GetServiceForBrowserContext(context, true)); |
| 78 } | 107 } |
| 79 | 108 |
| 80 static T* GetIfExists(content::BrowserContext* context) { | 109 static T* GetIfExists(content::BrowserContext* context) { |
| 81 return static_cast<T*>( | 110 return static_cast<T*>( |
| 82 T::GetFactoryInstance()->GetServiceForBrowserContext(context, false)); | 111 T::GetFactoryInstance()->GetServiceForBrowserContext(context, false)); |
| 83 } | 112 } |
| 84 | 113 |
| 85 // Declare dependencies on other factories. | 114 // Declares dependencies on other factories. |
| 86 // By default, ExtensionSystemFactory is the only dependency; however, | 115 // Deprecated. Use BrowserContextFactoryDependencies<> to declare |
| 87 // specializations can override this. Declare your specialization in | 116 // dependencies instead, as that form allows for partial specializations like |
| 88 // your header file after the BrowserContextKeyedAPI class definition. | 117 // in the case of ApiResourceManager<T>. |
| 89 // Then in the cc file (or inline in the header), define it, e.g.: | |
| 90 // template <> | |
| 91 // void BrowserContextKeyedAPIFactory< | |
| 92 // PushMessagingAPI>::DeclareFactoryDependencies() { | |
| 93 // DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | |
| 94 // DependsOn(ProfileSyncServiceFactory::GetInstance()); | |
| 95 // } | |
| 96 void DeclareFactoryDependencies() { | 118 void DeclareFactoryDependencies() { |
| 97 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 119 BrowserContextFactoryDependencies<T>::DeclareFactoryDependencies(this); |
| 98 } | 120 } |
| 99 | 121 |
| 100 BrowserContextKeyedAPIFactory() | 122 BrowserContextKeyedAPIFactory() |
| 101 : BrowserContextKeyedServiceFactory( | 123 : BrowserContextKeyedServiceFactory( |
| 102 T::service_name(), | 124 T::service_name(), |
| 103 BrowserContextDependencyManager::GetInstance()) { | 125 BrowserContextDependencyManager::GetInstance()) { |
| 104 DeclareFactoryDependencies(); | 126 DeclareFactoryDependencies(); |
| 105 } | 127 } |
| 106 | 128 |
| 107 ~BrowserContextKeyedAPIFactory() override {} | 129 ~BrowserContextKeyedAPIFactory() override {} |
| 108 | 130 |
| 109 private: | 131 private: |
| 132 friend struct BrowserContextFactoryDependencies<T>; |
| 133 |
| 110 // BrowserContextKeyedServiceFactory implementation. | 134 // BrowserContextKeyedServiceFactory implementation. |
| 111 KeyedService* BuildServiceInstanceFor( | 135 KeyedService* BuildServiceInstanceFor( |
| 112 content::BrowserContext* context) const override { | 136 content::BrowserContext* context) const override { |
| 113 return new T(context); | 137 return new T(context); |
| 114 } | 138 } |
| 115 | 139 |
| 116 // BrowserContextKeyedBaseFactory implementation. | 140 // BrowserContextKeyedBaseFactory implementation. |
| 117 // These can be effectively overridden with template specializations. | 141 // These can be effectively overridden with template specializations. |
| 118 content::BrowserContext* GetBrowserContextToUse( | 142 content::BrowserContext* GetBrowserContextToUse( |
| 119 content::BrowserContext* context) const override { | 143 content::BrowserContext* context) const override { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 133 bool ServiceIsNULLWhileTesting() const override { | 157 bool ServiceIsNULLWhileTesting() const override { |
| 134 return T::kServiceIsNULLWhileTesting; | 158 return T::kServiceIsNULLWhileTesting; |
| 135 } | 159 } |
| 136 | 160 |
| 137 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedAPIFactory); | 161 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedAPIFactory); |
| 138 }; | 162 }; |
| 139 | 163 |
| 140 } // namespace extensions | 164 } // namespace extensions |
| 141 | 165 |
| 142 #endif // EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ | 166 #endif // EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ |
| OLD | NEW |