Update V8 to version 4.6.55.
[chromium-blink-merge.git] / extensions / browser / browser_context_keyed_api_factory.h
blobad87e22d84b0754f0c23b46d36380ecea9dcf325
1 // Copyright 2014 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.
5 #ifndef EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_
6 #define EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_
8 #include "components/keyed_service/content/browser_context_dependency_manager.h"
9 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
10 #include "components/keyed_service/core/keyed_service.h"
11 #include "extensions/browser/extension_system_provider.h"
12 #include "extensions/browser/extensions_browser_client.h"
14 namespace extensions {
16 template <typename T>
17 class BrowserContextKeyedAPIFactory;
19 // Instantiations of BrowserContextKeyedAPIFactory 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 BrowserContextKeyedAPIFactory for the service.
23 class BrowserContextKeyedAPI : public KeyedService {
24 protected:
25 // Defaults for flags that control BrowserContextKeyedAPIFactory behavior.
26 // These can be overridden by subclasses to change that behavior.
27 // See BrowserContextKeyedBaseFactory for usage.
29 // These flags affect what instance is returned when Get() 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;
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, BrowserContextKeyedAPIs often do tasks at startup.)
40 static const bool kServiceIsCreatedWithBrowserContext = true;
42 // If set to true, GetForProfile returns NULL for TestingBrowserContexts.
43 static const bool kServiceIsNULLWhileTesting = false;
45 // Users of this factory template must define a GetFactoryInstance()
46 // and manage their own instances (using LazyInstance), because those cannot
47 // be included in more than one translation unit (and thus cannot be
48 // initialized in a header file).
50 // In the header file, declare GetFactoryInstance(), e.g.:
51 // class HistoryAPI {
52 // ...
53 // public:
54 // static BrowserContextKeyedAPIFactory<HistoryAPI>* GetFactoryInstance();
55 // };
57 // In the cc file, provide the implementation, e.g.:
58 // static base::LazyInstance<BrowserContextKeyedAPIFactory<HistoryAPI> >
59 // g_factory = LAZY_INSTANCE_INITIALIZER;
61 // // static
62 // BrowserContextKeyedAPIFactory<HistoryAPI>*
63 // HistoryAPI::GetFactoryInstance() {
64 // return g_factory.Pointer();
65 // }
68 // A template for factories for KeyedServices that manage extension APIs. T is
69 // a KeyedService that uses this factory template instead of its own separate
70 // factory definition to manage its per-profile instances.
71 template <typename T>
72 class BrowserContextKeyedAPIFactory : public BrowserContextKeyedServiceFactory {
73 public:
74 static T* Get(content::BrowserContext* context) {
75 return static_cast<T*>(
76 T::GetFactoryInstance()->GetServiceForBrowserContext(context, true));
79 static T* GetIfExists(content::BrowserContext* context) {
80 return static_cast<T*>(
81 T::GetFactoryInstance()->GetServiceForBrowserContext(context, false));
84 // Declare dependencies on other factories.
85 // By default, ExtensionSystemFactory is the only dependency; however,
86 // specializations can override this. Declare your specialization in
87 // your header file after the BrowserContextKeyedAPI class definition.
88 // Then in the cc file (or inline in the header), define it, e.g.:
89 // template <>
90 // void BrowserContextKeyedAPIFactory<
91 // PushMessagingAPI>::DeclareFactoryDependencies() {
92 // DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
93 // DependsOn(ProfileSyncServiceFactory::GetInstance());
94 // }
95 void DeclareFactoryDependencies() {
96 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
99 BrowserContextKeyedAPIFactory()
100 : BrowserContextKeyedServiceFactory(
101 T::service_name(),
102 BrowserContextDependencyManager::GetInstance()) {
103 DeclareFactoryDependencies();
106 ~BrowserContextKeyedAPIFactory() override {}
108 private:
109 // BrowserContextKeyedServiceFactory implementation.
110 KeyedService* BuildServiceInstanceFor(
111 content::BrowserContext* context) const override {
112 return new T(context);
115 // BrowserContextKeyedBaseFactory implementation.
116 // These can be effectively overridden with template specializations.
117 content::BrowserContext* GetBrowserContextToUse(
118 content::BrowserContext* context) const override {
119 if (T::kServiceRedirectedInIncognito)
120 return ExtensionsBrowserClient::Get()->GetOriginalContext(context);
122 if (T::kServiceHasOwnInstanceInIncognito)
123 return context;
125 return BrowserContextKeyedServiceFactory::GetBrowserContextToUse(context);
128 bool ServiceIsCreatedWithBrowserContext() const override {
129 return T::kServiceIsCreatedWithBrowserContext;
132 bool ServiceIsNULLWhileTesting() const override {
133 return T::kServiceIsNULLWhileTesting;
136 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedAPIFactory);
139 } // namespace extensions
141 #endif // EXTENSIONS_BROWSER_BROWSER_CONTEXT_KEYED_API_FACTORY_H_