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

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

Issue 178193030: Rename ProfileKeyedAPI to BrowserContextKeyedAPI and GetProfile to Get. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nits Created 6 years, 9 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 "components/browser_context_keyed_service/browser_context_dependency_ma nager.h"
9 #include "components/browser_context_keyed_service/browser_context_keyed_service .h"
10 #include "components/browser_context_keyed_service/browser_context_keyed_service _factory.h"
11 #include "extensions/browser/extension_system_provider.h"
12 #include "extensions/browser/extensions_browser_client.h"
13
14 namespace extensions {
15
16 template <typename T>
17 class ProfileKeyedAPIFactory;
18
19 // Instantiations of ProfileKeyedAPIFactory should use this base class
20 // and also define a static const char* service_name() function (used in the
21 // BrowserContextKeyedBaseFactory constructor). These fields should
22 // be accessible to the ProfileKeyedAPIFactory for the service.
23 class ProfileKeyedAPI : public BrowserContextKeyedService {
24 protected:
25 // Defaults for flags that control ProfileKeyedAPIFactory behavior.
26 // These can be overridden by subclasses to change that behavior.
27 // See BrowserContextKeyedBaseFactory for usage.
28
29 // These flags affect what instance is returned when GetForProfile is called
30 // on an incognito profile. By default, it returns NULL. If
31 // kServiceRedirectedInIncognito is true, it returns the instance for the
32 // corresponding regular profile. If kServiceHasOwnInstanceInIncognito
33 // is true, it returns a separate instance.
34 static const bool kServiceRedirectedInIncognito = false;
35 static const bool kServiceHasOwnInstanceInIncognito = false;
36
37 // If set to false, don't start the service at BrowserContext creation time.
38 // (The default differs from the BrowserContextKeyedBaseFactory default,
39 // because historically, ProfileKeyedAPIs often do tasks at startup.)
40 static const bool kServiceIsCreatedWithBrowserContext = true;
41
42 // If set to true, GetForProfile returns NULL for TestingBrowserContexts.
43 static const bool kServiceIsNULLWhileTesting = false;
44
45 // Users of this factory template must define a GetFactoryInstance()
46 // and manage their own instances (typically using LazyInstance or
47 // Singleton), because those cannot be included in more than one
48 // translation unit (and thus cannot be initialized in a header file).
49 //
50 // In the header file, declare GetFactoryInstance(), e.g.:
51 // class ProcessesAPI {
52 // ...
53 // public:
54 // static ProfileKeyedAPIFactory<ProcessesAPI>* GetFactoryInstance();
55 // };
56 //
57 // In the cc file, provide the implementation, e.g.:
58 // static base::LazyInstance<ProfileKeyedAPIFactory<ProcessesAPI> >
59 // g_factory = LAZY_INSTANCE_INITIALIZER;
60 //
61 // // static
62 // ProfileKeyedAPIFactory<ProcessesAPI>*
63 // ProcessesAPI::GetFactoryInstance() {
64 // return g_factory.Pointer();
65 // }
66 };
67
68 // A template for factories for BrowserContextKeyedServices that manage
69 // extension APIs. T is a BrowserContextKeyedService that uses this factory
70 // template instead of its own separate factory definition to manage its
71 // per-profile instances.
72 template <typename T>
73 class ProfileKeyedAPIFactory : public BrowserContextKeyedServiceFactory {
74 public:
75 // TODO(yoz): Rename to Get().
76 static T* GetForProfile(content::BrowserContext* context) {
77 return static_cast<T*>(
78 T::GetFactoryInstance()->GetServiceForBrowserContext(context, true));
79 }
80
81 // Declare dependencies on other factories.
82 // By default, ExtensionSystemFactory is the only dependency; however,
83 // specializations can override this. Declare your specialization in
84 // your header file after the ProfileKeyedAPI class definition.
85 // Then in the cc file (or inline in the header), define it, e.g.:
86 // template <>
87 // ProfileKeyedAPIFactory<PushMessagingAPI>::DeclareFactoryDependencies() {
88 // DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
89 // DependsOn(ProfileSyncServiceFactory::GetInstance());
90 // }
91 void DeclareFactoryDependencies() {
92 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
93 }
94
95 ProfileKeyedAPIFactory()
96 : BrowserContextKeyedServiceFactory(
97 T::service_name(),
98 BrowserContextDependencyManager::GetInstance()) {
99 DeclareFactoryDependencies();
100 }
101
102 virtual ~ProfileKeyedAPIFactory() {
103 }
104
105 private:
106 // BrowserContextKeyedServiceFactory implementation.
107 virtual BrowserContextKeyedService* BuildServiceInstanceFor(
108 content::BrowserContext* context) const OVERRIDE {
109 return new T(context);
110 }
111
112 // BrowserContextKeyedBaseFactory implementation.
113 // These can be effectively overridden with template specializations.
114 virtual content::BrowserContext* GetBrowserContextToUse(
115 content::BrowserContext* context) const OVERRIDE {
116 if (T::kServiceRedirectedInIncognito)
117 return ExtensionsBrowserClient::Get()->GetOriginalContext(context);
118
119 if (T::kServiceHasOwnInstanceInIncognito)
120 return context;
121
122 return BrowserContextKeyedServiceFactory::GetBrowserContextToUse(context);
123 }
124
125 virtual bool ServiceIsCreatedWithBrowserContext() const OVERRIDE {
126 return T::kServiceIsCreatedWithBrowserContext;
127 }
128
129 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE {
130 return T::kServiceIsNULLWhileTesting;
131 }
132
133 DISALLOW_COPY_AND_ASSIGN(ProfileKeyedAPIFactory);
134 };
135
136 } // namespace extensions
137
138 #endif // CHROME_BROWSER_EXTENSIONS_API_PROFILE_KEYED_API_FACTORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698