| 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 CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ |
| 7 | 7 |
| 8 #include "chrome/browser/extensions/extension_system_factory.h" | 8 #include "chrome/browser/extensions/extension_system_factory.h" |
| 9 #include "chrome/browser/profiles/incognito_helpers.h" | 9 #include "chrome/browser/profiles/incognito_helpers.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "components/browser_context_keyed_service/browser_context_dependency_ma
nager.h" | 11 #include "components/browser_context_keyed_service/browser_context_dependency_ma
nager.h" |
| 12 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 12 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
| 13 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" | 13 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" |
| 14 | 14 |
| 15 namespace extensions { | 15 namespace extensions { |
| 16 | 16 |
| 17 template <typename T> | 17 template <typename T> |
| 18 class ProfileKeyedAPIFactory; | 18 class ProfileKeyedAPIFactory; |
| 19 | 19 |
| 20 // Instantiations of ProfileKeyedAPIFactory should use this base class | 20 // Instantiations of ProfileKeyedAPIFactory should use this base class |
| 21 // and also define a static const char* service_name() function (used in the | 21 // and also define a static const char* service_name() function (used in the |
| 22 // ProfileKeyedBaseFactory constructor). These fields should be accessible | 22 // BrowserContextKeyedBaseFactory constructor). These fields should be accessibl
e |
| 23 // to the ProfileKeyedAPIFactory for the service. | 23 // to the ProfileKeyedAPIFactory for the service. |
| 24 class ProfileKeyedAPI : public ProfileKeyedService { | 24 class ProfileKeyedAPI : public BrowserContextKeyedService { |
| 25 protected: | 25 protected: |
| 26 // Defaults for flags that control ProfileKeyedAPIFactory behavior. | 26 // Defaults for flags that control ProfileKeyedAPIFactory behavior. |
| 27 // See ProfileKeyedBaseFactory for usage. | 27 // See BrowserContextKeyedBaseFactory for usage. |
| 28 static const bool kServiceRedirectedInIncognito = false; | 28 static const bool kServiceRedirectedInIncognito = false; |
| 29 static const bool kServiceIsNULLWhileTesting = false; | 29 static const bool kServiceIsNULLWhileTesting = false; |
| 30 static const bool kServiceHasOwnInstanceInIncognito = false; | 30 static const bool kServiceHasOwnInstanceInIncognito = false; |
| 31 | 31 |
| 32 // Users of this factory template must define a GetFactoryInstance() | 32 // Users of this factory template must define a GetFactoryInstance() |
| 33 // and manage their own instances (typically using LazyInstance or | 33 // and manage their own instances (typically using LazyInstance or |
| 34 // Singleton), because those cannot be included in more than one | 34 // Singleton), because those cannot be included in more than one |
| 35 // translation unit (and thus cannot be initialized in a header file). | 35 // translation unit (and thus cannot be initialized in a header file). |
| 36 // | 36 // |
| 37 // In the header file, declare GetFactoryInstance(), e.g.: | 37 // In the header file, declare GetFactoryInstance(), e.g.: |
| 38 // class ProcessesAPI { | 38 // class ProcessesAPI { |
| 39 // ... | 39 // ... |
| 40 // public: | 40 // public: |
| 41 // static ProfileKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance(); | 41 // static ProfileKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance(); |
| 42 // }; | 42 // }; |
| 43 // | 43 // |
| 44 // In the cc file, provide the implementation, e.g.: | 44 // In the cc file, provide the implementation, e.g.: |
| 45 // static base::LazyInstance<ProfileKeyedAPIFactory<ProcessesAPI> > | 45 // static base::LazyInstance<ProfileKeyedAPIFactory<ProcessesAPI> > |
| 46 // g_factory = LAZY_INSTANCE_INITIALIZER; | 46 // g_factory = LAZY_INSTANCE_INITIALIZER; |
| 47 // | 47 // |
| 48 // // static | 48 // // static |
| 49 // ProfileKeyedAPIFactory<ProcessesAPI>* | 49 // ProfileKeyedAPIFactory<ProcessesAPI>* |
| 50 // ProcessesAPI::GetFactoryInstance() { | 50 // ProcessesAPI::GetFactoryInstance() { |
| 51 // return &g_factory.Get(); | 51 // return &g_factory.Get(); |
| 52 // } | 52 // } |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 // A template for factories for ProfileKeyedServices that manage extension APIs. | 55 // A template for factories for BrowserContextKeyedServices that manage extensio
n APIs. |
| 56 // T is a ProfileKeyedService that uses this factory template instead of | 56 // T is a BrowserContextKeyedService that uses this factory template instead of |
| 57 // its own separate factory definition to manage its per-profile instances. | 57 // its own separate factory definition to manage its per-profile instances. |
| 58 template <typename T> | 58 template <typename T> |
| 59 class ProfileKeyedAPIFactory : public ProfileKeyedServiceFactory { | 59 class ProfileKeyedAPIFactory : public BrowserContextKeyedServiceFactory { |
| 60 public: | 60 public: |
| 61 static T* GetForProfile(Profile* profile) { | 61 static T* GetForProfile(Profile* profile) { |
| 62 return static_cast<T*>( | 62 return static_cast<T*>( |
| 63 T::GetFactoryInstance()->GetServiceForProfile(profile, true)); | 63 T::GetFactoryInstance()->GetServiceForBrowserContext(profile, true)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 // Declare dependencies on other factories. | 66 // Declare dependencies on other factories. |
| 67 // By default, ExtensionSystemFactory is the only dependency; however, | 67 // By default, ExtensionSystemFactory is the only dependency; however, |
| 68 // specializations can override this. Declare your specialization in | 68 // specializations can override this. Declare your specialization in |
| 69 // your header file after the ProfileKeyedAPI class definition. | 69 // your header file after the ProfileKeyedAPI class definition. |
| 70 // Then in the cc file (or inline in the header), define it, e.g.: | 70 // Then in the cc file (or inline in the header), define it, e.g.: |
| 71 // template <> | 71 // template <> |
| 72 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { | 72 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { |
| 73 // DependsOn(ExtensionSystemFactory::GetInstance()); | 73 // DependsOn(ExtensionSystemFactory::GetInstance()); |
| 74 // DependsOn(ProfileSyncServiceFactory::GetInstance()); | 74 // DependsOn(ProfileSyncServiceFactory::GetInstance()); |
| 75 // } | 75 // } |
| 76 void DeclareFactoryDependencies() { | 76 void DeclareFactoryDependencies() { |
| 77 DependsOn(ExtensionSystemFactory::GetInstance()); | 77 DependsOn(ExtensionSystemFactory::GetInstance()); |
| 78 } | 78 } |
| 79 | 79 |
| 80 ProfileKeyedAPIFactory() | 80 ProfileKeyedAPIFactory() |
| 81 : ProfileKeyedServiceFactory(T::service_name(), | 81 : BrowserContextKeyedServiceFactory( |
| 82 ProfileDependencyManager::GetInstance()) { | 82 T::service_name(), |
| 83 BrowserContextDependencyManager::GetInstance()) { |
| 83 DeclareFactoryDependencies(); | 84 DeclareFactoryDependencies(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 virtual ~ProfileKeyedAPIFactory() { | 87 virtual ~ProfileKeyedAPIFactory() { |
| 87 } | 88 } |
| 88 | 89 |
| 89 private: | 90 private: |
| 90 // ProfileKeyedServiceFactory implementation. | 91 // BrowserContextKeyedServiceFactory implementation. |
| 91 virtual ProfileKeyedService* BuildServiceInstanceFor( | 92 virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
| 92 content::BrowserContext* profile) const OVERRIDE { | 93 content::BrowserContext* profile) const OVERRIDE { |
| 93 return new T(static_cast<Profile*>(profile)); | 94 return new T(static_cast<Profile*>(profile)); |
| 94 } | 95 } |
| 95 | 96 |
| 96 // ProfileKeyedBaseFactory implementation. | 97 // BrowserContextKeyedBaseFactory implementation. |
| 97 // These can be effectively overridden with template specializations. | 98 // These can be effectively overridden with template specializations. |
| 98 virtual content::BrowserContext* GetBrowserContextToUse( | 99 virtual content::BrowserContext* GetBrowserContextToUse( |
| 99 content::BrowserContext* context) const OVERRIDE { | 100 content::BrowserContext* context) const OVERRIDE { |
| 100 if (T::kServiceRedirectedInIncognito) | 101 if (T::kServiceRedirectedInIncognito) |
| 101 return chrome::GetBrowserContextRedirectedInIncognito(context); | 102 return chrome::GetBrowserContextRedirectedInIncognito(context); |
| 102 | 103 |
| 103 if (T::kServiceHasOwnInstanceInIncognito) | 104 if (T::kServiceHasOwnInstanceInIncognito) |
| 104 return chrome::GetBrowserContextOwnInstanceInIncognito(context); | 105 return chrome::GetBrowserContextOwnInstanceInIncognito(context); |
| 105 | 106 |
| 106 return ProfileKeyedServiceFactory::GetBrowserContextToUse(context); | 107 return BrowserContextKeyedServiceFactory::GetBrowserContextToUse(context); |
| 107 } | 108 } |
| 108 | 109 |
| 109 virtual bool ServiceIsCreatedWithProfile() const OVERRIDE { | 110 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE { |
| 110 return true; | 111 return true; |
| 111 } | 112 } |
| 112 | 113 |
| 113 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { | 114 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { |
| 114 return T::kServiceIsNULLWhileTesting; | 115 return T::kServiceIsNULLWhileTesting; |
| 115 } | 116 } |
| 116 | 117 |
| 117 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory); | 118 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory); |
| 118 }; | 119 }; |
| 119 | 120 |
| 120 } // namespace extensions | 121 } // namespace extensions |
| 121 | 122 |
| 122 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ | 123 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ |
| OLD | NEW |