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

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

Issue 11649053: Banish boilerplate. Profile-keyed API factory for extension API classes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: jyasskin's 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_
7
8 #include "chrome/browser/extensions/extension_system_factory.h"
9 #include "chrome/browser/profiles/profile_dependency_manager.h"
10 #include "chrome/browser/profiles/profile_keyed_service.h"
11 #include "chrome/browser/profiles/profile_keyed_service_factory.h"
12
13 namespace extensions {
14
15 // Instantiations of ProfileKeyedAPIFactory should use this base class
16 // and also define a static const char* service_name() function (used in the
17 // ProfileKeyedBaseFactory constructor). These fields should be accessible
18 // to the ProfileKeyedAPIFactory for the service.
19 class ProfileKeyedAPI : public ProfileKeyedService {
20 protected:
21 // Defaults for flags that control ProfileKeyedAPIFactory behavior.
22 // See ProfileKeyedBaseFactory for usage.
23 static const bool kServiceRedirectedInIncognito = false;
24 static const bool kServiceIsNULLWhileTesting = false;
25 };
26
27 // A template for factories for ProfileKeyedServices that manage extension APIs.
28 // T is a ProfileKeyedService that uses this factory template instead of
29 // its own separate factory definition to manage its per-profile instances.
30 template <typename T>
31 class ProfileKeyedAPIFactory : public ProfileKeyedServiceFactory {
32 public:
33 static T* GetForProfile(Profile* profile) {
34 return static_cast<T*>(
35 GetInstance()->GetServiceForProfile(profile, true));
36 }
37
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.
61 // By default, ExtensionSystemFactory is the only dependency; however,
62 // specializations can override this. Declare your specialization in
63 // your header file after the ProfileKeyedAPI class definition.
64 // Then in the cc file (or inline in the header), define it, e.g.:
65 // template <>
66 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() {
67 // DependsOn(ExtensionSystemFactory::GetInstance());
68 // DependsOn(ProfileSyncServiceFactory::GetInstance());
69 // }
70 void DeclareFactoryDependencies() {
71 DependsOn(ExtensionSystemFactory::GetInstance());
72 }
73
74 ProfileKeyedAPIFactory()
75 : ProfileKeyedServiceFactory(T::service_name(),
76 ProfileDependencyManager::GetInstance()) {
77 DeclareFactoryDependencies();
78 }
79
80 virtual ~ProfileKeyedAPIFactory() {
81 }
82
83 private:
84 // ProfileKeyedServiceFactory implementation.
85 virtual ProfileKeyedService* BuildServiceInstanceFor(
86 Profile* profile) const OVERRIDE {
87 return new T(profile);
88 }
89
90 // ProfileKeyedBaseFactory implementation.
91 // These can be effectively overridden with template specializations.
92 virtual bool ServiceRedirectedInIncognito() const OVERRIDE {
93 return T::kServiceRedirectedInIncognito;
94 }
95
96 virtual bool ServiceIsCreatedWithProfile() const OVERRIDE {
97 return true;
98 }
99
100 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE {
101 return T::kServiceIsNULLWhileTesting;
102 }
103
104 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory);
105 };
106
107 } // namespace extensions
108
109 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698