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 |
23 // to the ProfileKeyedAPIFactory for the service. | 23 // be accessible 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 |
56 // T is a ProfileKeyedService that uses this factory template instead of | 56 // extension APIs. T is a BrowserContextKeyedService that uses this factory |
57 // its own separate factory definition to manage its per-profile instances. | 57 // template instead of its own separate factory definition to manage its |
| 58 // per-profile instances. |
58 template <typename T> | 59 template <typename T> |
59 class ProfileKeyedAPIFactory : public ProfileKeyedServiceFactory { | 60 class ProfileKeyedAPIFactory : public BrowserContextKeyedServiceFactory { |
60 public: | 61 public: |
61 static T* GetForProfile(Profile* profile) { | 62 static T* GetForProfile(Profile* profile) { |
62 return static_cast<T*>( | 63 return static_cast<T*>( |
63 T::GetFactoryInstance()->GetServiceForProfile(profile, true)); | 64 T::GetFactoryInstance()->GetServiceForBrowserContext(profile, true)); |
64 } | 65 } |
65 | 66 |
66 // Declare dependencies on other factories. | 67 // Declare dependencies on other factories. |
67 // By default, ExtensionSystemFactory is the only dependency; however, | 68 // By default, ExtensionSystemFactory is the only dependency; however, |
68 // specializations can override this. Declare your specialization in | 69 // specializations can override this. Declare your specialization in |
69 // your header file after the ProfileKeyedAPI class definition. | 70 // your header file after the ProfileKeyedAPI class definition. |
70 // Then in the cc file (or inline in the header), define it, e.g.: | 71 // Then in the cc file (or inline in the header), define it, e.g.: |
71 // template <> | 72 // template <> |
72 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { | 73 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { |
73 // DependsOn(ExtensionSystemFactory::GetInstance()); | 74 // DependsOn(ExtensionSystemFactory::GetInstance()); |
74 // DependsOn(ProfileSyncServiceFactory::GetInstance()); | 75 // DependsOn(ProfileSyncServiceFactory::GetInstance()); |
75 // } | 76 // } |
76 void DeclareFactoryDependencies() { | 77 void DeclareFactoryDependencies() { |
77 DependsOn(ExtensionSystemFactory::GetInstance()); | 78 DependsOn(ExtensionSystemFactory::GetInstance()); |
78 } | 79 } |
79 | 80 |
80 ProfileKeyedAPIFactory() | 81 ProfileKeyedAPIFactory() |
81 : ProfileKeyedServiceFactory(T::service_name(), | 82 : BrowserContextKeyedServiceFactory( |
82 ProfileDependencyManager::GetInstance()) { | 83 T::service_name(), |
| 84 BrowserContextDependencyManager::GetInstance()) { |
83 DeclareFactoryDependencies(); | 85 DeclareFactoryDependencies(); |
84 } | 86 } |
85 | 87 |
86 virtual ~ProfileKeyedAPIFactory() { | 88 virtual ~ProfileKeyedAPIFactory() { |
87 } | 89 } |
88 | 90 |
89 private: | 91 private: |
90 // ProfileKeyedServiceFactory implementation. | 92 // BrowserContextKeyedServiceFactory implementation. |
91 virtual ProfileKeyedService* BuildServiceInstanceFor( | 93 virtual BrowserContextKeyedService* BuildServiceInstanceFor( |
92 content::BrowserContext* profile) const OVERRIDE { | 94 content::BrowserContext* profile) const OVERRIDE { |
93 return new T(static_cast<Profile*>(profile)); | 95 return new T(static_cast<Profile*>(profile)); |
94 } | 96 } |
95 | 97 |
96 // ProfileKeyedBaseFactory implementation. | 98 // BrowserContextKeyedBaseFactory implementation. |
97 // These can be effectively overridden with template specializations. | 99 // These can be effectively overridden with template specializations. |
98 virtual content::BrowserContext* GetBrowserContextToUse( | 100 virtual content::BrowserContext* GetBrowserContextToUse( |
99 content::BrowserContext* context) const OVERRIDE { | 101 content::BrowserContext* context) const OVERRIDE { |
100 if (T::kServiceRedirectedInIncognito) | 102 if (T::kServiceRedirectedInIncognito) |
101 return chrome::GetBrowserContextRedirectedInIncognito(context); | 103 return chrome::GetBrowserContextRedirectedInIncognito(context); |
102 | 104 |
103 if (T::kServiceHasOwnInstanceInIncognito) | 105 if (T::kServiceHasOwnInstanceInIncognito) |
104 return chrome::GetBrowserContextOwnInstanceInIncognito(context); | 106 return chrome::GetBrowserContextOwnInstanceInIncognito(context); |
105 | 107 |
106 return ProfileKeyedServiceFactory::GetBrowserContextToUse(context); | 108 return BrowserContextKeyedServiceFactory::GetBrowserContextToUse(context); |
107 } | 109 } |
108 | 110 |
109 virtual bool ServiceIsCreatedWithProfile() const OVERRIDE { | 111 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE { |
110 return true; | 112 return true; |
111 } | 113 } |
112 | 114 |
113 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { | 115 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { |
114 return T::kServiceIsNULLWhileTesting; | 116 return T::kServiceIsNULLWhileTesting; |
115 } | 117 } |
116 | 118 |
117 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory); | 119 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory); |
118 }; | 120 }; |
119 | 121 |
120 } // namespace extensions | 122 } // namespace extensions |
121 | 123 |
122 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ | 124 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ |
OLD | NEW |