Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1243)

Side by Side Diff: chrome/browser/extensions/api/profile_keyed_api_factory.h

Issue 11682005: Remove some Profile-keyed factory boilerplate: management omnibox preference push_messaging. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: - Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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;
24 static const bool kServiceIsNULLWhileTesting = false; 27 static const bool kServiceIsNULLWhileTesting = false;
28
29 // Users of this factory template must define a GetFactoryInstance()
30 // and manage their own instances (typically using LazyInstance or
31 // Singleton), because those cannot be included in more than one
32 // translation unit (and thus cannot be initialized in a header file).
33 //
34 // In the header file, declare GetFactoryInstance(), e.g.:
35 // class ProcessesAPI {
36 // ...
37 // public:
38 // static ProfileKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance();
39 // };
40 //
41 // In the cc file, provide the implementation, e.g.:
42 // static base::LazyInstance<ProfileKeyedAPIFactory<ProcessesAPI> >
43 // g_factory = LAZY_INSTANCE_INITIALIZER;
44 //
45 // // static
46 // ProfileKeyedAPIFactory<ProcessesAPI>*
47 // ProcessesAPI::GetFactoryInstance() {
48 // return &g_factory.Get();
49 // }
25 }; 50 };
26 51
27 // A template for factories for ProfileKeyedServices that manage extension APIs. 52 // A template for factories for ProfileKeyedServices that manage extension APIs.
28 // T is a ProfileKeyedService that uses this factory template instead of 53 // T is a ProfileKeyedService that uses this factory template instead of
29 // its own separate factory definition to manage its per-profile instances. 54 // its own separate factory definition to manage its per-profile instances.
30 template <typename T> 55 template <typename T>
31 class ProfileKeyedAPIFactory : public ProfileKeyedServiceFactory { 56 class ProfileKeyedAPIFactory : public ProfileKeyedServiceFactory {
32 public: 57 public:
33 static T* GetForProfile(Profile* profile) { 58 static T* GetForProfile(Profile* profile) {
34 return static_cast<T*>( 59 return static_cast<T*>(
35 GetInstance()->GetServiceForProfile(profile, true)); 60 T::GetFactoryInstance()->GetServiceForProfile(profile, true));
36 } 61 }
37 62
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. 63 // Declare dependencies on other factories.
61 // By default, ExtensionSystemFactory is the only dependency; however, 64 // By default, ExtensionSystemFactory is the only dependency; however,
62 // specializations can override this. Declare your specialization in 65 // specializations can override this. Declare your specialization in
63 // your header file after the ProfileKeyedAPI class definition. 66 // your header file after the ProfileKeyedAPI class definition.
64 // Then in the cc file (or inline in the header), define it, e.g.: 67 // Then in the cc file (or inline in the header), define it, e.g.:
65 // template <> 68 // template <>
66 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() { 69 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() {
67 // DependsOn(ExtensionSystemFactory::GetInstance()); 70 // DependsOn(ExtensionSystemFactory::GetInstance());
68 // DependsOn(ProfileSyncServiceFactory::GetInstance()); 71 // DependsOn(ProfileSyncServiceFactory::GetInstance());
69 // } 72 // }
(...skipping 30 matching lines...) Expand all
100 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE { 103 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE {
101 return T::kServiceIsNULLWhileTesting; 104 return T::kServiceIsNULLWhileTesting;
102 } 105 }
103 106
104 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory); 107 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory);
105 }; 108 };
106 109
107 } // namespace extensions 110 } // namespace extensions
108 111
109 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_ 112 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698