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_IOS_BROWSER_STATE_KEYED_SERVICE_FACTORY_H_
6 #define COMPONENTS_KEYED_SERVICE_IOS_BROWSER_STATE_KEYED_SERVICE_FACTORY_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "components/keyed_service/core/keyed_service_export.h"
12 #include "components/keyed_service/core/keyed_service_factory.h"
14 class BrowserStateDependencyManager
;
21 // Base class for Factories that take a BrowserState object and return some
22 // service on a one-to-one mapping. Each factory that derives from this class
23 // *must* be a Singleton (only unit tests don't do that).
25 // We do this because services depend on each other and we need to control
26 // shutdown/destruction order. In each derived classes' constructors, the
27 // implementors must explicitly state which services are depended on.
28 class KEYED_SERVICE_EXPORT BrowserStateKeyedServiceFactory
29 : public KeyedServiceFactory
{
31 // A function that supplies the instance of a KeyedService for a given
32 // BrowserState. This is used primarily for testing, where we want to feed
33 // a specific mock into the BSKSF system.
34 typedef scoped_ptr
<KeyedService
>(*TestingFactoryFunction
)(
35 web::BrowserState
* context
);
37 // Associates |factory| with |context| so that |factory| is used to create
38 // the KeyedService when requested. |factory| can be NULL to signal that
39 // KeyedService should be NULL. Multiple calls to SetTestingFactory() are
40 // allowed; previous services will be shut down.
41 void SetTestingFactory(web::BrowserState
* context
,
42 TestingFactoryFunction factory
);
44 // Associates |factory| with |context| and immediately returns the created
45 // KeyedService. Since the factory will be used immediately, it may not be
47 KeyedService
* SetTestingFactoryAndUse(web::BrowserState
* context
,
48 TestingFactoryFunction factory
);
51 // BrowserStateKeyedServiceFactories must communicate with a
52 // BrowserStateDependencyManager. For all non-test code, write your subclass
53 // constructors like this:
55 // MyServiceFactory::MyServiceFactory()
56 // : BrowserStateKeyedServiceFactory(
58 // BrowserStateDependencyManager::GetInstance()) {
60 BrowserStateKeyedServiceFactory(const char* name
,
61 /*BrowserState*/DependencyManager
* manager
);
62 ~BrowserStateKeyedServiceFactory() override
;
64 // Common implementation that maps |context| to some service object. Deals
65 // with incognito and testing contexts per subclass instructions. If |create|
66 // is true, the service will be created using BuildServiceInstanceFor() if it
67 // doesn't already exist.
68 KeyedService
* GetServiceForBrowserState(web::BrowserState
* context
,
71 // Interface for people building a concrete FooServiceFactory: --------------
73 // Finds which browser state (if any) to use.
74 virtual web::BrowserState
* GetBrowserStateToUse(
75 web::BrowserState
* context
) const;
77 // By default, we create instances of a service lazily and wait until
78 // GetForBrowserState() is called on our subclass. Some services need to be
79 // created as soon as the BrowserState has been brought up.
80 virtual bool ServiceIsCreatedWithBrowserState() const;
82 // By default, TestingBrowserStates will be treated like normal contexts.
83 // You can override this so that by default, the service associated with the
84 // TestingBrowserState is NULL. (This is just a shortcut around
85 // SetTestingFactory() to make sure our contexts don't directly refer to the
86 // services they use.)
87 bool ServiceIsNULLWhileTesting() const override
;
89 // Interface for people building a type of BrowserStateKeyedFactory: -------
91 // All subclasses of BrowserStateKeyedServiceFactory must return a
92 // KeyedService instead of just a BrowserStateKeyedBase.
93 virtual scoped_ptr
<KeyedService
> BuildServiceInstanceFor(
94 web::BrowserState
* context
) const = 0;
96 // A helper object actually listens for notifications about BrowserState
97 // destruction, calculates the order in which things are destroyed and then
98 // does a two pass shutdown.
100 // First, BrowserStateShutdown() is called on every ServiceFactory and will
101 // usually call KeyedService::Shutdown(), which gives each
102 // KeyedService a chance to remove dependencies on other
103 // services that it may be holding.
105 // Secondly, BrowserStateDestroyed() is called on every ServiceFactory
106 // and the default implementation removes it from |mapping_| and deletes
108 virtual void BrowserStateShutdown(web::BrowserState
* context
);
109 virtual void BrowserStateDestroyed(web::BrowserState
* context
);
112 // Registers any user preferences on this service. This is called by
113 // RegisterPrefsIfNecessaryForContext() and should be overriden by any service
114 // that wants to register profile-specific preferences.
115 virtual void RegisterBrowserStatePrefs(
116 user_prefs::PrefRegistrySyncable
* registry
) {}
118 // KeyedServiceFactory:
119 scoped_ptr
<KeyedService
> BuildServiceInstanceFor(
120 base::SupportsUserData
* context
) const final
;
121 bool IsOffTheRecord(base::SupportsUserData
* context
) const final
;
123 // KeyedServiceBaseFactory:
125 base::SupportsUserData
* GetTypedContext(
126 base::SupportsUserData
* context
) const override
;
127 base::SupportsUserData
* GetContextForDependencyManager(
128 base::SupportsUserData
* context
) const override
;
129 #endif // defined(OS_IOS)
130 base::SupportsUserData
* GetContextToUse(
131 base::SupportsUserData
* context
) const final
;
132 bool ServiceIsCreatedWithContext() const final
;
133 void ContextShutdown(base::SupportsUserData
* context
) final
;
134 void ContextDestroyed(base::SupportsUserData
* context
) final
;
135 void RegisterPrefs(user_prefs::PrefRegistrySyncable
* registry
) final
;
137 DISALLOW_COPY_AND_ASSIGN(BrowserStateKeyedServiceFactory
);
140 #endif // COMPONENTS_KEYED_SERVICE_IOS_BROWSER_STATE_KEYED_SERVICE_FACTORY_H_