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 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/profiles/incognito_helpers.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
|
James Cook
2014/02/25 17:59:16
Hooray for deleting these!
| |
| 10 #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" |
| 11 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" | 9 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" |
| 12 #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" |
| 13 #include "extensions/browser/extension_system_provider.h" | 11 #include "extensions/browser/extension_system_provider.h" |
| 14 #include "extensions/browser/extensions_browser_client.h" | 12 #include "extensions/browser/extensions_browser_client.h" |
| 15 | 13 |
| 16 namespace extensions { | 14 namespace extensions { |
| 17 | 15 |
| 18 template <typename T> | 16 template <typename T> |
| 19 class ProfileKeyedAPIFactory; | 17 class ProfileKeyedAPIFactory; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 // } | 52 // } |
| 55 }; | 53 }; |
| 56 | 54 |
| 57 // A template for factories for BrowserContextKeyedServices that manage | 55 // A template for factories for BrowserContextKeyedServices that manage |
| 58 // extension APIs. T is a BrowserContextKeyedService that uses this factory | 56 // extension APIs. T is a BrowserContextKeyedService that uses this factory |
| 59 // template instead of its own separate factory definition to manage its | 57 // template instead of its own separate factory definition to manage its |
| 60 // per-profile instances. | 58 // per-profile instances. |
| 61 template <typename T> | 59 template <typename T> |
| 62 class ProfileKeyedAPIFactory : public BrowserContextKeyedServiceFactory { | 60 class ProfileKeyedAPIFactory : public BrowserContextKeyedServiceFactory { |
| 63 public: | 61 public: |
| 64 // TODO(yoz): Delete this one. | 62 // TODO(yoz): Rename to Get(). |
| 65 static T* GetForProfile(Profile* profile) { | 63 static T* GetForProfile(content::BrowserContext* context) { |
|
James Cook
2014/02/25 17:59:16
w00t
| |
| 66 return static_cast<T*>( | 64 return static_cast<T*>( |
| 67 T::GetFactoryInstance()->GetServiceForBrowserContext(profile, true)); | 65 T::GetFactoryInstance()->GetServiceForBrowserContext(context, true)); |
| 68 } | |
| 69 | |
| 70 static T* GetForProfile(content::BrowserContext* context) { | |
| 71 return static_cast<T*>(T::GetFactoryInstance()->GetServiceForBrowserContext( | |
| 72 Profile::FromBrowserContext(context), true)); | |
| 73 } | 66 } |
| 74 | 67 |
| 75 // Declare dependencies on other factories. | 68 // Declare dependencies on other factories. |
| 76 // By default, ExtensionSystemFactory is the only dependency; however, | 69 // By default, ExtensionSystemFactory is the only dependency; however, |
| 77 // specializations can override this. Declare your specialization in | 70 // specializations can override this. Declare your specialization in |
| 78 // your header file after the ProfileKeyedAPI class definition. | 71 // your header file after the ProfileKeyedAPI class definition. |
| 79 // Then in the cc file (or inline in the header), define it, e.g.: | 72 // Then in the cc file (or inline in the header), define it, e.g.: |
| 80 // template <> | 73 // template <> |
| 81 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { | 74 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { |
| 82 // DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 75 // DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 83 // DependsOn(ProfileSyncServiceFactory::GetInstance()); | 76 // DependsOn(ProfileSyncServiceFactory::GetInstance()); |
| 84 // } | 77 // } |
| 85 void DeclareFactoryDependencies() { | 78 void DeclareFactoryDependencies() { |
| 86 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 79 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 87 } | 80 } |
| 88 | 81 |
| 89 ProfileKeyedAPIFactory() | 82 ProfileKeyedAPIFactory() |
| 90 : BrowserContextKeyedServiceFactory( | 83 : BrowserContextKeyedServiceFactory( |
| 91 T::service_name(), | 84 T::service_name(), |
| 92 BrowserContextDependencyManager::GetInstance()) { | 85 BrowserContextDependencyManager::GetInstance()) { |
| 93 DeclareFactoryDependencies(); | 86 DeclareFactoryDependencies(); |
| 94 } | 87 } |
| 95 | 88 |
| 96 virtual ~ProfileKeyedAPIFactory() { | 89 virtual ~ProfileKeyedAPIFactory() { |
| 97 } | 90 } |
| 98 | 91 |
| 99 private: | 92 private: |
| 100 // BrowserContextKeyedServiceFactory implementation. | 93 // BrowserContextKeyedServiceFactory implementation. |
| 101 virtual BrowserContextKeyedService* BuildServiceInstanceFor( | 94 virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
| 102 content::BrowserContext* profile) const OVERRIDE { | 95 content::BrowserContext* context) const OVERRIDE { |
| 103 return new T(static_cast<Profile*>(profile)); | 96 return new T(context); |
| 104 } | 97 } |
| 105 | 98 |
| 106 // BrowserContextKeyedBaseFactory implementation. | 99 // BrowserContextKeyedBaseFactory implementation. |
| 107 // These can be effectively overridden with template specializations. | 100 // These can be effectively overridden with template specializations. |
| 108 virtual content::BrowserContext* GetBrowserContextToUse( | 101 virtual content::BrowserContext* GetBrowserContextToUse( |
| 109 content::BrowserContext* context) const OVERRIDE { | 102 content::BrowserContext* context) const OVERRIDE { |
| 110 if (T::kServiceRedirectedInIncognito) | 103 if (T::kServiceRedirectedInIncognito) |
| 111 return chrome::GetBrowserContextRedirectedInIncognito(context); | 104 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); |
| 112 | 105 |
| 113 if (T::kServiceHasOwnInstanceInIncognito) | 106 if (T::kServiceHasOwnInstanceInIncognito) |
| 114 return chrome::GetBrowserContextOwnInstanceInIncognito(context); | 107 return context; |
| 115 | 108 |
| 116 return BrowserContextKeyedServiceFactory::GetBrowserContextToUse(context); | 109 return BrowserContextKeyedServiceFactory::GetBrowserContextToUse(context); |
| 117 } | 110 } |
| 118 | 111 |
| 119 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE { | 112 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE { |
| 120 return T::kServiceIsCreatedWithBrowserContext; | 113 return T::kServiceIsCreatedWithBrowserContext; |
| 121 } | 114 } |
| 122 | 115 |
| 123 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { | 116 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { |
| 124 return T::kServiceIsNULLWhileTesting; | 117 return T::kServiceIsNULLWhileTesting; |
| 125 } | 118 } |
| 126 | 119 |
| 127 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory); | 120 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory); |
| 128 }; | 121 }; |
| 129 | 122 |
| 130 } // namespace extensions | 123 } // namespace extensions |
| 131 | 124 |
| 132 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ | 125 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ |
| OLD | NEW |