Chromium Code Reviews| 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 // template <> | |
| 76 // struct BrowserContextFactoryDependencies<MyService> { | |
| 77 // static void DeclareFactoryDependencies( | |
| 78 // BrowserContextKeyedAPIFactory<ApiResourceManager<T>>* factory) { | |
| 79 // factory->DependsOn( | |
| 80 // ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | |
| 81 // factory->DependsOn(ProfileSyncServiceFactory::GetInstance()); | |
| 82 // ... | |
| 83 // } | |
| 84 // }; | |
| 85 template <typename T> | |
| 86 struct BrowserContextFactoryDependencies { | |
| 87 static void DeclareFactoryDependencies( | |
| 88 BrowserContextKeyedAPIFactory<T>* factory) { | |
| 89 factory->DependsOn( | |
| 90 ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | |
| 91 } | |
| 92 }; | |
| 93 | |
| 69 // A template for factories for KeyedServices that manage extension APIs. T is | 94 // 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 | 95 // a KeyedService that uses this factory template instead of its own separate |
| 71 // factory definition to manage its per-profile instances. | 96 // factory definition to manage its per-profile instances. |
| 72 template <typename T> | 97 template <typename T> |
| 73 class BrowserContextKeyedAPIFactory : public BrowserContextKeyedServiceFactory { | 98 class BrowserContextKeyedAPIFactory : public BrowserContextKeyedServiceFactory { |
| 74 public: | 99 public: |
| 75 static T* Get(content::BrowserContext* context) { | 100 static T* Get(content::BrowserContext* context) { |
| 76 return static_cast<T*>( | 101 return static_cast<T*>( |
| 77 T::GetFactoryInstance()->GetServiceForBrowserContext(context, true)); | 102 T::GetFactoryInstance()->GetServiceForBrowserContext(context, true)); |
| 78 } | 103 } |
| 79 | 104 |
| 80 static T* GetIfExists(content::BrowserContext* context) { | 105 static T* GetIfExists(content::BrowserContext* context) { |
| 81 return static_cast<T*>( | 106 return static_cast<T*>( |
| 82 T::GetFactoryInstance()->GetServiceForBrowserContext(context, false)); | 107 T::GetFactoryInstance()->GetServiceForBrowserContext(context, false)); |
| 83 } | 108 } |
| 84 | 109 |
| 85 // Declare dependencies on other factories. | 110 // Declares dependencies on other factories. |
| 86 // By default, ExtensionSystemFactory is the only dependency; however, | 111 // Deprecated. Use BrowserContextFactoryDependencies<> to declare |
| 87 // specializations can override this. Declare your specialization in | 112 // dependencies instead. |
| 88 // your header file after the BrowserContextKeyedAPI class definition. | 113 void DeclareFactoryDependencies() {} |
| 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() { | |
| 97 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | |
| 98 } | |
| 99 | 114 |
| 100 BrowserContextKeyedAPIFactory() | 115 BrowserContextKeyedAPIFactory() |
| 101 : BrowserContextKeyedServiceFactory( | 116 : BrowserContextKeyedServiceFactory( |
| 102 T::service_name(), | 117 T::service_name(), |
| 103 BrowserContextDependencyManager::GetInstance()) { | 118 BrowserContextDependencyManager::GetInstance()) { |
| 104 DeclareFactoryDependencies(); | 119 DeclareFactoryDependencies(); |
| 120 BrowserContextFactoryDependencies<T>::DeclareFactoryDependencies(this); | |
|
Devlin
2017/05/12 22:53:59
Hmm... wouldn't doing this result in ExtensionSyst
Kevin M
2017/05/15 18:09:04
Hm, yes. Thanks. How's this for backwards compatib
Devlin
2017/05/15 18:24:52
SGTM; that should work!
| |
| 105 } | 121 } |
| 106 | 122 |
| 107 ~BrowserContextKeyedAPIFactory() override {} | 123 ~BrowserContextKeyedAPIFactory() override {} |
| 108 | 124 |
| 109 private: | 125 private: |
| 126 friend struct BrowserContextFactoryDependencies<T>; | |
| 127 | |
| 110 // BrowserContextKeyedServiceFactory implementation. | 128 // BrowserContextKeyedServiceFactory implementation. |
| 111 KeyedService* BuildServiceInstanceFor( | 129 KeyedService* BuildServiceInstanceFor( |
| 112 content::BrowserContext* context) const override { | 130 content::BrowserContext* context) const override { |
| 113 return new T(context); | 131 return new T(context); |
| 114 } | 132 } |
| 115 | 133 |
| 116 // BrowserContextKeyedBaseFactory implementation. | 134 // BrowserContextKeyedBaseFactory implementation. |
| 117 // These can be effectively overridden with template specializations. | 135 // These can be effectively overridden with template specializations. |
| 118 content::BrowserContext* GetBrowserContextToUse( | 136 content::BrowserContext* GetBrowserContextToUse( |
| 119 content::BrowserContext* context) const override { | 137 content::BrowserContext* context) const override { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 133 bool ServiceIsNULLWhileTesting() const override { | 151 bool ServiceIsNULLWhileTesting() const override { |
| 134 return T::kServiceIsNULLWhileTesting; | 152 return T::kServiceIsNULLWhileTesting; |
| 135 } | 153 } |
| 136 | 154 |
| 137 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedAPIFactory); | 155 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedAPIFactory); |
| 138 }; | 156 }; |
| 139 | 157 |
| 140 } // namespace extensions | 158 } // namespace extensions |
| 141 | 159 |
| 142 #endif // EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ | 160 #endif // EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ |
| OLD | NEW |