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/profile_dependency_manager.h" | 9 #include "chrome/browser/profiles/profile_dependency_manager.h" |
10 #include "chrome/browser/profiles/profile_keyed_service.h" | 10 #include "chrome/browser/profiles/profile_keyed_service.h" |
11 #include "chrome/browser/profiles/profile_keyed_service_factory.h" | 11 #include "chrome/browser/profiles/profile_keyed_service_factory.h" |
12 | 12 |
13 namespace extensions { | 13 namespace extensions { |
14 | 14 |
| 15 template <typename T> |
| 16 class ProfileKeyedAPIFactory; |
| 17 |
15 // Instantiations of ProfileKeyedAPIFactory should use this base class | 18 // Instantiations of ProfileKeyedAPIFactory should use this base class |
16 // and also define a static const char* service_name() function (used in the | 19 // and also define a static const char* service_name() function (used in the |
17 // ProfileKeyedBaseFactory constructor). These fields should be accessible | 20 // ProfileKeyedBaseFactory constructor). These fields should be accessible |
18 // to the ProfileKeyedAPIFactory for the service. | 21 // to the ProfileKeyedAPIFactory for the service. |
19 class ProfileKeyedAPI : public ProfileKeyedService { | 22 class ProfileKeyedAPI : public ProfileKeyedService { |
20 protected: | 23 protected: |
21 // Defaults for flags that control ProfileKeyedAPIFactory behavior. | 24 // Defaults for flags that control ProfileKeyedAPIFactory behavior. |
22 // See ProfileKeyedBaseFactory for usage. | 25 // See ProfileKeyedBaseFactory for usage. |
23 static const bool kServiceRedirectedInIncognito = false; | 26 static const bool kServiceRedirectedInIncognito = false; |
| 27 static const bool kServiceIsCreatedWithProfile = true; |
24 static const bool kServiceIsNULLWhileTesting = false; | 28 static const bool kServiceIsNULLWhileTesting = false; |
| 29 |
| 30 // Users of this factory template must define a GetFactoryInstance() |
| 31 // and manage their own instances (typically using LazyInstance or |
| 32 // Singleton), because those cannot be included in more than one |
| 33 // translation unit (and thus cannot be initialized in a header file). |
| 34 // |
| 35 // In the header file, declare GetFactoryInstance(), e.g.: |
| 36 // class ProcessesAPI { |
| 37 // ... |
| 38 // public: |
| 39 // static ProfileKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance(); |
| 40 // }; |
| 41 // |
| 42 // In the cc file, provide the implementation, e.g.: |
| 43 // static base::LazyInstance<ProfileKeyedAPIFactory<ProcessesAPI> > |
| 44 // g_factory = LAZY_INSTANCE_INITIALIZER; |
| 45 // |
| 46 // // static |
| 47 // ProfileKeyedAPIFactory<ProcessesAPI>* |
| 48 // ProcessesAPI::GetFactoryInstance() { |
| 49 // return &g_factory.Get(); |
| 50 // } |
25 }; | 51 }; |
26 | 52 |
27 // A template for factories for ProfileKeyedServices that manage extension APIs. | 53 // A template for factories for ProfileKeyedServices that manage extension APIs. |
28 // T is a ProfileKeyedService that uses this factory template instead of | 54 // T is a ProfileKeyedService that uses this factory template instead of |
29 // its own separate factory definition to manage its per-profile instances. | 55 // its own separate factory definition to manage its per-profile instances. |
30 template <typename T> | 56 template <typename T> |
31 class ProfileKeyedAPIFactory : public ProfileKeyedServiceFactory { | 57 class ProfileKeyedAPIFactory : public ProfileKeyedServiceFactory { |
32 public: | 58 public: |
33 static T* GetForProfile(Profile* profile) { | 59 static T* GetForProfile(Profile* profile) { |
34 return static_cast<T*>( | 60 return static_cast<T*>( |
35 GetInstance()->GetServiceForProfile(profile, true)); | 61 T::GetFactoryInstance()->GetServiceForProfile(profile, true)); |
36 } | 62 } |
37 | 63 |
38 // Users of this factory template must manage their own instances | |
39 // (typically using LazyInstance or Singleton), because those cannot be | |
40 // included in more than one translation unit (and thus cannot be initialized | |
41 // in a header file). | |
42 // | |
43 // In the header file, declare the specialization, e.g.: | |
44 // template <> | |
45 // ProfileKeyedAPIFactory<ProcessesAPI> | |
46 // ProfileKeyedAPIFactory<ProcessesAPI>::GetInstance(); | |
47 // | |
48 // In the cc file, provide the implementation: | |
49 // static base::LazyInstance<ProfileKeyedAPIFactory<ProcessesAPI> > | |
50 // g_factory = LAZY_INSTANCE_INITIALIZER; | |
51 // | |
52 // template <> | |
53 // ProfileKeyedAPIFactory<ProcessesAPI>* | |
54 // ProfileKeyedAPIFactory<ProcessesAPI>::GetInstance() { | |
55 // return &g_factory.Get(); | |
56 // } | |
57 | |
58 static ProfileKeyedAPIFactory* GetInstance(); | |
59 | |
60 // Declare dependencies on other factories. | 64 // Declare dependencies on other factories. |
61 // By default, ExtensionSystemFactory is the only dependency; however, | 65 // By default, ExtensionSystemFactory is the only dependency; however, |
62 // specializations can override this. Declare your specialization in | 66 // specializations can override this. Declare your specialization in |
63 // your header file after the ProfileKeyedAPI class definition. | 67 // your header file after the ProfileKeyedAPI class definition. |
64 // Then in the cc file (or inline in the header), define it, e.g.: | 68 // Then in the cc file (or inline in the header), define it, e.g.: |
65 // template <> | 69 // template <> |
66 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { | 70 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { |
67 // DependsOn(ExtensionSystemFactory::GetInstance()); | 71 // DependsOn(ExtensionSystemFactory::GetInstance()); |
68 // DependsOn(ProfileSyncServiceFactory::GetInstance()); | 72 // DependsOn(ProfileSyncServiceFactory::GetInstance()); |
69 // } | 73 // } |
(...skipping 17 matching lines...) Expand all Loading... |
87 return new T(profile); | 91 return new T(profile); |
88 } | 92 } |
89 | 93 |
90 // ProfileKeyedBaseFactory implementation. | 94 // ProfileKeyedBaseFactory implementation. |
91 // These can be effectively overridden with template specializations. | 95 // These can be effectively overridden with template specializations. |
92 virtual bool ServiceRedirectedInIncognito() const OVERRIDE { | 96 virtual bool ServiceRedirectedInIncognito() const OVERRIDE { |
93 return T::kServiceRedirectedInIncognito; | 97 return T::kServiceRedirectedInIncognito; |
94 } | 98 } |
95 | 99 |
96 virtual bool ServiceIsCreatedWithProfile() const OVERRIDE { | 100 virtual bool ServiceIsCreatedWithProfile() const OVERRIDE { |
97 return true; | 101 return T::kServiceIsCreatedWithProfile; |
98 } | 102 } |
99 | 103 |
100 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { | 104 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { |
101 return T::kServiceIsNULLWhileTesting; | 105 return T::kServiceIsNULLWhileTesting; |
102 } | 106 } |
103 | 107 |
104 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory); | 108 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory); |
105 }; | 109 }; |
106 | 110 |
107 } // namespace extensions | 111 } // namespace extensions |
108 | 112 |
109 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ | 113 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ |
OLD | NEW |