Allow tests to provide a TestingFactoryFunction for NULLWhileTesting factories.
[chromium-blink-merge.git] / components / keyed_service / content / browser_context_keyed_base_factory.h
blob86e569d5dcddc8123d4b501226681990444546b9
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 COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_
6 #define COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_
8 #include <set>
10 #include "base/threading/non_thread_safe.h"
11 #include "components/keyed_service/core/dependency_node.h"
12 #include "components/keyed_service/core/keyed_service_export.h"
14 class BrowserContextDependencyManager;
15 class PrefService;
17 namespace content {
18 class BrowserContext;
21 namespace user_prefs {
22 class PrefRegistrySyncable;
25 // Base class for Factories that take a BrowserContext object and return some
26 // service.
28 // Unless you're trying to make a new type of Factory, you probably don't want
29 // this class, but its subclasses: BrowserContextKeyedServiceFactory and
30 // RefcountedBrowserContextKeyedServiceFactory. This object describes general
31 // dependency management between Factories; subclasses react to lifecycle
32 // events and implement memory management.
33 class KEYED_SERVICE_EXPORT BrowserContextKeyedBaseFactory
34 : public base::NonThreadSafe,
35 NON_EXPORTED_BASE(public DependencyNode) {
36 public:
37 // Registers preferences used in this service on the pref service of
38 // |context|. This is the public interface and is safe to be called multiple
39 // times because testing code can have multiple services of the same type
40 // attached to a single |context|. Only test code is allowed to call this
41 // method.
42 // TODO(gab): This method can be removed entirely when
43 // PrefService::DeprecatedGetPrefRegistry() is phased out.
44 void RegisterUserPrefsOnBrowserContextForTest(
45 content::BrowserContext* context);
47 #ifndef NDEBUG
48 // Returns our name. We don't keep track of this in release mode.
49 const char* name() const { return service_name_; }
50 #endif
52 protected:
53 BrowserContextKeyedBaseFactory(const char* name,
54 BrowserContextDependencyManager* manager);
55 virtual ~BrowserContextKeyedBaseFactory();
57 // The main public interface for declaring dependencies between services
58 // created by factories.
59 void DependsOn(BrowserContextKeyedBaseFactory* rhs);
61 // Calls RegisterProfilePrefs() after doing house keeping required to work
62 // alongside RegisterUserPrefsOnBrowserContextForTest().
63 // TODO(gab): This method can be replaced by RegisterProfilePrefs() directly
64 // once RegisterUserPrefsOnBrowserContextForTest() is phased out.
65 void RegisterProfilePrefsIfNecessaryForContext(
66 const content::BrowserContext* context,
67 user_prefs::PrefRegistrySyncable* registry);
69 // Interface for people building a concrete FooServiceFactory: --------------
71 // Finds which browser context (if any) to use.
72 virtual content::BrowserContext* GetBrowserContextToUse(
73 content::BrowserContext* context) const;
75 // By default, we create instances of a service lazily and wait until
76 // GetForBrowserContext() is called on our subclass. Some services need to be
77 // created as soon as the BrowserContext has been brought up.
78 virtual bool ServiceIsCreatedWithBrowserContext() const;
80 // By default, TestingBrowserContexts will be treated like normal contexts.
81 // You can override this so that by default, the service associated with the
82 // TestingBrowserContext is NULL. (This is just a shortcut around
83 // SetTestingFactory() to make sure our contexts don't directly refer to the
84 // services they use.)
85 virtual bool ServiceIsNULLWhileTesting() const;
87 // Interface for people building a type of BrowserContextKeyedFactory: -------
89 // A helper object actually listens for notifications about BrowserContext
90 // destruction, calculates the order in which things are destroyed and then
91 // does a two pass shutdown.
93 // It is up to the individual factory types to determine what this two pass
94 // shutdown means. The general framework guarantees the following:
96 // - Each BrowserContextShutdown() is called in dependency order (and you may
97 // reach out to other services during this phase).
99 // - Each BrowserContextDestroyed() is called in dependency order. We will
100 // NOTREACHED() if you attempt to GetForBrowserContext() any other service.
101 // You should delete/deref/do other final memory management things during
102 // this phase. You must also call the base class method as the last thing
103 // you do.
104 virtual void BrowserContextShutdown(content::BrowserContext* context) = 0;
105 virtual void BrowserContextDestroyed(content::BrowserContext* context);
107 // Returns whether we've registered the preferences on this context.
108 bool ArePreferencesSetOn(content::BrowserContext* context) const;
110 // Mark context as Preferences set.
111 void MarkPreferencesSetOn(content::BrowserContext* context);
113 // Which BrowserContextDependencyManager we should communicate with.
114 // In real code, this will always be
115 // BrowserContextDependencyManager::GetInstance(), but unit tests will want
116 // to use their own copy.
117 BrowserContextDependencyManager* dependency_manager_;
119 private:
120 friend class BrowserContextDependencyManager;
121 friend class BrowserContextDependencyManagerUnittests;
123 // Registers any user preferences on this service. This is called by
124 // RegisterProfilePrefsIfNecessary() and should be overriden by any service
125 // that wants to register profile-specific preferences.
126 virtual void RegisterProfilePrefs(
127 user_prefs::PrefRegistrySyncable* registry) {}
129 // These two methods are for tight integration with the
130 // BrowserContextDependencyManager.
132 // Because of ServiceIsNULLWhileTesting(), we need a way to tell different
133 // subclasses that they should disable testing.
134 virtual void SetEmptyTestingFactory(content::BrowserContext* context) = 0;
136 // Returns true if a testing factory function has been set for |context|.
137 virtual bool HasTestingFactory(content::BrowserContext* context) = 0;
139 // We also need a generalized, non-returning method that generates the object
140 // now for when we're creating the context.
141 virtual void CreateServiceNow(content::BrowserContext* context) = 0;
143 // BrowserContexts that have this service's preferences registered on them.
144 std::set<const content::BrowserContext*> registered_preferences_;
146 #if !defined(NDEBUG)
147 // A static string passed in to our constructor. Should be unique across all
148 // services. This is used only for debugging in debug mode. (We can print
149 // pretty graphs with GraphViz with this information.)
150 const char* service_name_;
151 #endif
154 #endif // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_