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 "components/keyed_service/core/keyed_service_base_factory.h"
9 #include "components/keyed_service/core/keyed_service_export.h"
11 class BrowserContextDependencyManager
;
18 namespace user_prefs
{
19 class PrefRegistrySyncable
;
22 // Base class for Factories that take a BrowserContext object and return some
25 // Unless you're trying to make a new type of Factory, you probably don't want
26 // this class, but its subclasses: BrowserContextKeyedServiceFactory and
27 // RefcountedBrowserContextKeyedServiceFactory. This object describes general
28 // dependency management between Factories; subclasses react to lifecycle
29 // events and implement memory management.
31 // Note: this class is deprecated and should not be used and will be removed
32 // once http://crbug.com/131843 and http://crbug.com/131844 are closed. If you
33 // need to implement a new way to manage KeyedService lifetime, base your code
34 // on KeyedServiceBaseFactory instead.
35 class KEYED_SERVICE_EXPORT BrowserContextKeyedBaseFactory
36 : public KeyedServiceBaseFactory
{
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 BrowserContextKeyedBaseFactory(const char* name
,
50 BrowserContextDependencyManager
* manager
);
51 ~BrowserContextKeyedBaseFactory() override
;
53 // Interface for people building a concrete FooServiceFactory: --------------
55 // Finds which browser context (if any) to use.
56 virtual content::BrowserContext
* GetBrowserContextToUse(
57 content::BrowserContext
* context
) const;
59 // By default, we create instances of a service lazily and wait until
60 // GetForBrowserContext() is called on our subclass. Some services need to be
61 // created as soon as the BrowserContext has been brought up.
62 virtual bool ServiceIsCreatedWithBrowserContext() const;
64 // By default, TestingBrowserContexts will be treated like normal contexts.
65 // You can override this so that by default, the service associated with the
66 // TestingBrowserContext is NULL. (This is just a shortcut around
67 // SetTestingFactory() to make sure our contexts don't directly refer to the
68 // services they use.)
69 bool ServiceIsNULLWhileTesting() const override
;
71 // Interface for people building a type of BrowserContextKeyedFactory: -------
73 // A helper object actually listens for notifications about BrowserContext
74 // destruction, calculates the order in which things are destroyed and then
75 // does a two pass shutdown.
77 // It is up to the individual factory types to determine what this two pass
78 // shutdown means. The general framework guarantees the following:
80 // - Each BrowserContextShutdown() is called in dependency order (and you may
81 // reach out to other services during this phase).
83 // - Each BrowserContextDestroyed() is called in dependency order. We will
84 // NOTREACHED() if you attempt to GetForBrowserContext() any other service.
85 // You should delete/deref/do other final memory management things during
86 // this phase. You must also call the base class method as the last thing
88 virtual void BrowserContextShutdown(content::BrowserContext
* context
) = 0;
89 virtual void BrowserContextDestroyed(content::BrowserContext
* context
);
92 // Registers any user preferences on this service. This is called by
93 // RegisterPrefsIfNecessaryForContext() and should be overriden by any service
94 // that wants to register profile-specific preferences.
95 virtual void RegisterProfilePrefs(
96 user_prefs::PrefRegistrySyncable
* registry
) {}
98 // Because of ServiceIsNULLWhileTesting(), we need a way to tell different
99 // subclasses that they should disable testing.
100 virtual void SetEmptyTestingFactory(content::BrowserContext
* context
) = 0;
102 // Returns true if a testing factory function has been set for |context|.
103 virtual bool HasTestingFactory(content::BrowserContext
* context
) = 0;
105 // We also need a generalized, non-returning method that generates the object
106 // now for when we're creating the context.
107 virtual void CreateServiceNow(content::BrowserContext
* context
) = 0;
109 // KeyedServiceBaseFactory:
110 base::SupportsUserData
* GetContextToUse(
111 base::SupportsUserData
* context
) const final
;
112 bool ServiceIsCreatedWithContext() const final
;
113 void ContextShutdown(base::SupportsUserData
* context
) final
;
114 void ContextDestroyed(base::SupportsUserData
* context
) final
;
115 void RegisterPrefs(user_prefs::PrefRegistrySyncable
* registry
) final
;
116 void SetEmptyTestingFactory(base::SupportsUserData
* context
) final
;
117 bool HasTestingFactory(base::SupportsUserData
* context
) final
;
118 void CreateServiceNow(base::SupportsUserData
* context
) final
;
121 #endif // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_