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.
5 #ifndef COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_
6 #define COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_
10 #include "base/threading/non_thread_safe.h"
11 #include "components/browser_context_keyed_service/browser_context_keyed_service_export.h"
12 #include "components/browser_context_keyed_service/dependency_node.h"
14 class BrowserContextDependencyManager;
21 namespace user_prefs {
22 class PrefRegistrySyncable;
25 // Base class for Factories that take a BrowserContext object and return some
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 BROWSER_CONTEXT_KEYED_SERVICE_EXPORT
34 BrowserContextKeyedBaseFactory
35 : public base::NonThreadSafe,
36 NON_EXPORTED_BASE(public DependencyNode) {
38 // Registers preferences used in this service on the pref service of
39 // |context|. This is the public interface and is safe to be called multiple
40 // times because testing code can have multiple services of the same type
41 // attached to a single |context|. Only test code is allowed to call this
43 // TODO(gab): This method can be removed entirely when
44 // PrefService::DeprecatedGetPrefRegistry() is phased out.
45 void RegisterUserPrefsOnBrowserContextForTest(
46 content::BrowserContext* context);
49 // Returns our name. We don't keep track of this in release mode.
50 const char* name() const { return service_name_; }
54 BrowserContextKeyedBaseFactory(const char* name,
55 BrowserContextDependencyManager* manager);
56 virtual ~BrowserContextKeyedBaseFactory();
58 // The main public interface for declaring dependencies between services
59 // created by factories.
60 void DependsOn(BrowserContextKeyedBaseFactory* rhs);
62 // Calls RegisterProfilePrefs() after doing house keeping required to work
63 // alongside RegisterUserPrefsOnBrowserContextForTest().
64 // TODO(gab): This method can be replaced by RegisterProfilePrefs() directly
65 // once RegisterUserPrefsOnBrowserContextForTest() is phased out.
66 void RegisterProfilePrefsIfNecessaryForContext(
67 const content::BrowserContext* context,
68 user_prefs::PrefRegistrySyncable* registry);
70 // Interface for people building a concrete FooServiceFactory: --------------
72 // Finds which browser context (if any) to use.
73 virtual content::BrowserContext* GetBrowserContextToUse(
74 content::BrowserContext* context) const;
76 // By default, we create instances of a service lazily and wait until
77 // GetForBrowserContext() is called on our subclass. Some services need to be
78 // created as soon as the BrowserContext has been brought up.
79 virtual bool ServiceIsCreatedWithBrowserContext() const;
81 // By default, TestingBrowserContexts will be treated like normal contexts.
82 // You can override this so that by default, the service associated with the
83 // TestingBrowserContext is NULL. (This is just a shortcut around
84 // SetTestingFactory() to make sure our contexts don't directly refer to the
85 // services they use.)
86 virtual bool ServiceIsNULLWhileTesting() const;
88 // Interface for people building a type of BrowserContextKeyedFactory: -------
90 // A helper object actually listens for notifications about BrowserContext
91 // destruction, calculates the order in which things are destroyed and then
92 // does a two pass shutdown.
94 // It is up to the individual factory types to determine what this two pass
95 // shutdown means. The general framework guarantees the following:
97 // - Each BrowserContextShutdown() is called in dependency order (and you may
98 // reach out to other services during this phase).
100 // - Each BrowserContextDestroyed() is called in dependency order. We will
101 // NOTREACHED() if you attempt to GetForBrowserContext() any other service.
102 // You should delete/deref/do other final memory management things during
103 // this phase. You must also call the base class method as the last thing
105 virtual void BrowserContextShutdown(content::BrowserContext* context) = 0;
106 virtual void BrowserContextDestroyed(content::BrowserContext* context);
108 // Returns whether we've registered the preferences on this context.
109 bool ArePreferencesSetOn(content::BrowserContext* context) const;
111 // Mark context as Preferences set.
112 void MarkPreferencesSetOn(content::BrowserContext* context);
114 // Which BrowserContextDependencyManager we should communicate with.
115 // In real code, this will always be
116 // BrowserContextDependencyManager::GetInstance(), but unit tests will want
117 // to use their own copy.
118 BrowserContextDependencyManager* dependency_manager_;
121 friend class BrowserContextDependencyManager;
122 friend class BrowserContextDependencyManagerUnittests;
124 // Registers any user preferences on this service. This is called by
125 // RegisterProfilePrefsIfNecessary() and should be overriden by any service
126 // that wants to register profile-specific preferences.
127 virtual void RegisterProfilePrefs(
128 user_prefs::PrefRegistrySyncable* registry) {}
130 // These two methods are for tight integration with the
131 // BrowserContextDependencyManager.
133 // Because of ServiceIsNULLWhileTesting(), we need a way to tell different
134 // subclasses that they should disable testing.
135 virtual void SetEmptyTestingFactory(content::BrowserContext* context) = 0;
137 // We also need a generalized, non-returning method that generates the object
138 // now for when we're creating the context.
139 virtual void CreateServiceNow(content::BrowserContext* context) = 0;
141 // BrowserContexts that have this service's preferences registered on them.
142 std::set<const content::BrowserContext*> registered_preferences_;
145 // A static string passed in to our constructor. Should be unique across all
146 // services. This is used only for debugging in debug mode. (We can print
147 // pretty graphs with GraphViz with this information.)
148 const char* service_name_;
152 #endif // COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_