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. | |
|
Devlin
2017/05/15 18:24:52
For background, maybe we should include a comment
Kevin M
2017/05/15 18:34:39
Done - added the explanation to the deprecated Dec
Devlin
2017/05/15 18:37:47
I dunno - I think it might help avoid a well-meani
Kevin M
2017/05/15 18:43:01
Done.
| |
| 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. | |
| 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() { | 113 void DeclareFactoryDependencies() { |
| 97 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 114 BrowserContextFactoryDependencies<T>::DeclareFactoryDependencies(this); |
| 98 } | 115 } |
| 99 | 116 |
| 100 BrowserContextKeyedAPIFactory() | 117 BrowserContextKeyedAPIFactory() |
| 101 : BrowserContextKeyedServiceFactory( | 118 : BrowserContextKeyedServiceFactory( |
| 102 T::service_name(), | 119 T::service_name(), |
| 103 BrowserContextDependencyManager::GetInstance()) { | 120 BrowserContextDependencyManager::GetInstance()) { |
| 104 DeclareFactoryDependencies(); | 121 DeclareFactoryDependencies(); |
| 105 } | 122 } |
| 106 | 123 |
| 107 ~BrowserContextKeyedAPIFactory() override {} | 124 ~BrowserContextKeyedAPIFactory() override {} |
| 108 | 125 |
| 109 private: | 126 private: |
| 127 friend struct BrowserContextFactoryDependencies<T>; | |
| 128 | |
| 110 // BrowserContextKeyedServiceFactory implementation. | 129 // BrowserContextKeyedServiceFactory implementation. |
| 111 KeyedService* BuildServiceInstanceFor( | 130 KeyedService* BuildServiceInstanceFor( |
| 112 content::BrowserContext* context) const override { | 131 content::BrowserContext* context) const override { |
| 113 return new T(context); | 132 return new T(context); |
| 114 } | 133 } |
| 115 | 134 |
| 116 // BrowserContextKeyedBaseFactory implementation. | 135 // BrowserContextKeyedBaseFactory implementation. |
| 117 // These can be effectively overridden with template specializations. | 136 // These can be effectively overridden with template specializations. |
| 118 content::BrowserContext* GetBrowserContextToUse( | 137 content::BrowserContext* GetBrowserContextToUse( |
| 119 content::BrowserContext* context) const override { | 138 content::BrowserContext* context) const override { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 133 bool ServiceIsNULLWhileTesting() const override { | 152 bool ServiceIsNULLWhileTesting() const override { |
| 134 return T::kServiceIsNULLWhileTesting; | 153 return T::kServiceIsNULLWhileTesting; |
| 135 } | 154 } |
| 136 | 155 |
| 137 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedAPIFactory); | 156 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedAPIFactory); |
| 138 }; | 157 }; |
| 139 | 158 |
| 140 } // namespace extensions | 159 } // namespace extensions |
| 141 | 160 |
| 142 #endif // EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ | 161 #endif // EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ |
| OLD | NEW |