Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ | 5 #ifndef EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ | 6 #define EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ |
| 7 | 7 |
| 8 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" | 8 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" |
| 9 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" | 9 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" |
| 10 #include "components/browser_context_keyed_service/browser_context_keyed_service _factory.h" | 10 #include "components/browser_context_keyed_service/browser_context_keyed_service _factory.h" |
| 11 #include "extensions/browser/extension_system_provider.h" | 11 #include "extensions/browser/extension_system_provider.h" |
| 12 #include "extensions/browser/extensions_browser_client.h" | 12 #include "extensions/browser/extensions_browser_client.h" |
| 13 | 13 |
| 14 namespace extensions { | 14 namespace extensions { |
| 15 | 15 |
| 16 template <typename T> | 16 template <typename T> |
| 17 class ProfileKeyedAPIFactory; | 17 class BrowserContextKeyedAPIFactory; |
| 18 | 18 |
| 19 // Instantiations of ProfileKeyedAPIFactory should use this base class | 19 // Instantiations of BrowserContextKeyedAPIFactory should use this base class |
| 20 // and also define a static const char* service_name() function (used in the | 20 // and also define a static const char* service_name() function (used in the |
| 21 // BrowserContextKeyedBaseFactory constructor). These fields should | 21 // BrowserContextKeyedBaseFactory constructor). These fields should |
| 22 // be accessible to the ProfileKeyedAPIFactory for the service. | 22 // be accessible to the BrowserContextKeyedAPIFactory for the service. |
| 23 class ProfileKeyedAPI : public BrowserContextKeyedService { | 23 class BrowserContextKeyedAPI : public BrowserContextKeyedService { |
| 24 protected: | 24 protected: |
| 25 // Defaults for flags that control ProfileKeyedAPIFactory behavior. | 25 // Defaults for flags that control BrowserContextKeyedAPIFactory behavior. |
| 26 // These can be overridden by subclasses to change that behavior. | 26 // These can be overridden by subclasses to change that behavior. |
| 27 // See BrowserContextKeyedBaseFactory for usage. | 27 // See BrowserContextKeyedBaseFactory for usage. |
| 28 | 28 |
| 29 // These flags affect what instance is returned when GetForProfile is called | 29 // These flags affect what instance is returned when Get is called |
|
James Cook
2014/03/05 02:33:30
optional nit: I sometimes say "Get()" when referri
Yoyo Zhou
2014/03/05 02:51:30
Done.
| |
| 30 // on an incognito profile. By default, it returns NULL. If | 30 // on an incognito profile. By default, it returns NULL. If |
| 31 // kServiceRedirectedInIncognito is true, it returns the instance for the | 31 // kServiceRedirectedInIncognito is true, it returns the instance for the |
| 32 // corresponding regular profile. If kServiceHasOwnInstanceInIncognito | 32 // corresponding regular profile. If kServiceHasOwnInstanceInIncognito |
| 33 // is true, it returns a separate instance. | 33 // is true, it returns a separate instance. |
| 34 static const bool kServiceRedirectedInIncognito = false; | 34 static const bool kServiceRedirectedInIncognito = false; |
| 35 static const bool kServiceHasOwnInstanceInIncognito = false; | 35 static const bool kServiceHasOwnInstanceInIncognito = false; |
| 36 | 36 |
| 37 // If set to false, don't start the service at BrowserContext creation time. | 37 // If set to false, don't start the service at BrowserContext creation time. |
| 38 // (The default differs from the BrowserContextKeyedBaseFactory default, | 38 // (The default differs from the BrowserContextKeyedBaseFactory default, |
| 39 // because historically, ProfileKeyedAPIs often do tasks at startup.) | 39 // because historically, BrowserContextKeyedAPIs often do tasks at startup.) |
| 40 static const bool kServiceIsCreatedWithBrowserContext = true; | 40 static const bool kServiceIsCreatedWithBrowserContext = true; |
| 41 | 41 |
| 42 // If set to true, GetForProfile returns NULL for TestingBrowserContexts. | 42 // If set to true, GetForProfile returns NULL for TestingBrowserContexts. |
| 43 static const bool kServiceIsNULLWhileTesting = false; | 43 static const bool kServiceIsNULLWhileTesting = false; |
| 44 | 44 |
| 45 // Users of this factory template must define a GetFactoryInstance() | 45 // Users of this factory template must define a GetFactoryInstance() |
| 46 // and manage their own instances (typically using LazyInstance or | 46 // and manage their own instances (typically using LazyInstance or |
| 47 // Singleton), because those cannot be included in more than one | 47 // Singleton), because those cannot be included in more than one |
| 48 // translation unit (and thus cannot be initialized in a header file). | 48 // translation unit (and thus cannot be initialized in a header file). |
| 49 // | 49 // |
| 50 // In the header file, declare GetFactoryInstance(), e.g.: | 50 // In the header file, declare GetFactoryInstance(), e.g.: |
| 51 // class ProcessesAPI { | 51 // class ProcessesAPI { |
| 52 // ... | 52 // ... |
| 53 // public: | 53 // public: |
| 54 // static ProfileKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance(); | 54 // static BrowserContextKeyedAPIFactory<ProcessesAPI>* |
| 55 // GetFactoryInstance(); | |
|
James Cook
2014/03/05 02:33:30
nit: Indent this like real code. Or shorten the e
Yoyo Zhou
2014/03/05 02:51:30
Changed to HistoryAPI to make it short enough.
| |
| 55 // }; | 56 // }; |
| 56 // | 57 // |
| 57 // In the cc file, provide the implementation, e.g.: | 58 // In the cc file, provide the implementation, e.g.: |
| 58 // static base::LazyInstance<ProfileKeyedAPIFactory<ProcessesAPI> > | 59 // static base::LazyInstance<BrowserContextKeyedAPIFactory<ProcessesAPI> > |
| 59 // g_factory = LAZY_INSTANCE_INITIALIZER; | 60 // g_factory = LAZY_INSTANCE_INITIALIZER; |
| 60 // | 61 // |
| 61 // // static | 62 // // static |
| 62 // ProfileKeyedAPIFactory<ProcessesAPI>* | 63 // BrowserContextKeyedAPIFactory<ProcessesAPI>* |
| 63 // ProcessesAPI::GetFactoryInstance() { | 64 // ProcessesAPI::GetFactoryInstance() { |
| 64 // return g_factory.Pointer(); | 65 // return g_factory.Pointer(); |
| 65 // } | 66 // } |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 // A template for factories for BrowserContextKeyedServices that manage | 69 // A template for factories for BrowserContextKeyedServices that manage |
| 69 // extension APIs. T is a BrowserContextKeyedService that uses this factory | 70 // extension APIs. T is a BrowserContextKeyedService that uses this factory |
| 70 // template instead of its own separate factory definition to manage its | 71 // template instead of its own separate factory definition to manage its |
| 71 // per-profile instances. | 72 // per-profile instances. |
| 72 template <typename T> | 73 template <typename T> |
| 73 class ProfileKeyedAPIFactory : public BrowserContextKeyedServiceFactory { | 74 class BrowserContextKeyedAPIFactory : public BrowserContextKeyedServiceFactory { |
| 74 public: | 75 public: |
| 75 // TODO(yoz): Rename to Get(). | 76 static T* Get(content::BrowserContext* context) { |
|
James Cook
2014/03/05 02:33:30
Hooray!
| |
| 76 static T* GetForProfile(content::BrowserContext* context) { | |
| 77 return static_cast<T*>( | 77 return static_cast<T*>( |
| 78 T::GetFactoryInstance()->GetServiceForBrowserContext(context, true)); | 78 T::GetFactoryInstance()->GetServiceForBrowserContext(context, true)); |
| 79 } | 79 } |
| 80 | 80 |
| 81 // Declare dependencies on other factories. | 81 // Declare dependencies on other factories. |
| 82 // By default, ExtensionSystemFactory is the only dependency; however, | 82 // By default, ExtensionSystemFactory is the only dependency; however, |
| 83 // specializations can override this. Declare your specialization in | 83 // specializations can override this. Declare your specialization in |
| 84 // your header file after the ProfileKeyedAPI class definition. | 84 // your header file after the BrowserContextKeyedAPI class definition. |
| 85 // Then in the cc file (or inline in the header), define it, e.g.: | 85 // Then in the cc file (or inline in the header), define it, e.g.: |
| 86 // template <> | 86 // template <> |
| 87 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { | 87 // void BrowserContextKeyedAPIFactory< |
|
James Cook
2014/03/05 02:33:30
Wow, that's an ugly line break. It's OK, but would
Yoyo Zhou
2014/03/05 02:51:30
81 characters. *tears*
| |
| 88 // PushMessagingAPI>::DeclareFactoryDependencies() { | |
| 88 // DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 89 // DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 89 // DependsOn(ProfileSyncServiceFactory::GetInstance()); | 90 // DependsOn(ProfileSyncServiceFactory::GetInstance()); |
| 90 // } | 91 // } |
| 91 void DeclareFactoryDependencies() { | 92 void DeclareFactoryDependencies() { |
| 92 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 93 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 93 } | 94 } |
| 94 | 95 |
| 95 ProfileKeyedAPIFactory() | 96 BrowserContextKeyedAPIFactory() |
| 96 : BrowserContextKeyedServiceFactory( | 97 : BrowserContextKeyedServiceFactory( |
| 97 T::service_name(), | 98 T::service_name(), |
| 98 BrowserContextDependencyManager::GetInstance()) { | 99 BrowserContextDependencyManager::GetInstance()) { |
| 99 DeclareFactoryDependencies(); | 100 DeclareFactoryDependencies(); |
| 100 } | 101 } |
| 101 | 102 |
| 102 virtual ~ProfileKeyedAPIFactory() { | 103 virtual ~BrowserContextKeyedAPIFactory() {} |
| 103 } | |
| 104 | 104 |
| 105 private: | 105 private: |
| 106 // BrowserContextKeyedServiceFactory implementation. | 106 // BrowserContextKeyedServiceFactory implementation. |
| 107 virtual BrowserContextKeyedService* BuildServiceInstanceFor( | 107 virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
| 108 content::BrowserContext* context) const OVERRIDE { | 108 content::BrowserContext* context) const OVERRIDE { |
| 109 return new T(context); | 109 return new T(context); |
| 110 } | 110 } |
| 111 | 111 |
| 112 // BrowserContextKeyedBaseFactory implementation. | 112 // BrowserContextKeyedBaseFactory implementation. |
| 113 // These can be effectively overridden with template specializations. | 113 // These can be effectively overridden with template specializations. |
| 114 virtual content::BrowserContext* GetBrowserContextToUse( | 114 virtual content::BrowserContext* GetBrowserContextToUse( |
| 115 content::BrowserContext* context) const OVERRIDE { | 115 content::BrowserContext* context) const OVERRIDE { |
| 116 if (T::kServiceRedirectedInIncognito) | 116 if (T::kServiceRedirectedInIncognito) |
| 117 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); | 117 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); |
| 118 | 118 |
| 119 if (T::kServiceHasOwnInstanceInIncognito) | 119 if (T::kServiceHasOwnInstanceInIncognito) |
| 120 return context; | 120 return context; |
| 121 | 121 |
| 122 return BrowserContextKeyedServiceFactory::GetBrowserContextToUse(context); | 122 return BrowserContextKeyedServiceFactory::GetBrowserContextToUse(context); |
| 123 } | 123 } |
| 124 | 124 |
| 125 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE { | 125 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE { |
| 126 return T::kServiceIsCreatedWithBrowserContext; | 126 return T::kServiceIsCreatedWithBrowserContext; |
| 127 } | 127 } |
| 128 | 128 |
| 129 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { | 129 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { |
| 130 return T::kServiceIsNULLWhileTesting; | 130 return T::kServiceIsNULLWhileTesting; |
| 131 } | 131 } |
| 132 | 132 |
| 133 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory); | 133 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedAPIFactory); |
| 134 }; | 134 }; |
| 135 | 135 |
| 136 } // namespace extensions | 136 } // namespace extensions |
| 137 | 137 |
| 138 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ | 138 #endif // EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_ |
| OLD | NEW |