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_SERVICE_FACTORY_H_
6 #define COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "components/browser_context_keyed_service/browser_context_keyed_base_factory.h"
13 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
14 #include "components/browser_context_keyed_service/browser_context_keyed_service_export.h"
16 class BrowserContextDependencyManager
;
17 class BrowserContextKeyedService
;
19 // Base class for Factories that take a BrowserContext object and return some
20 // service on a one-to-one mapping. Each factory that derives from this class
21 // *must* be a Singleton (only unit tests don't do that).
23 // We do this because services depend on each other and we need to control
24 // shutdown/destruction order. In each derived classes' constructors, the
25 // implementors must explicitly state which services are depended on.
26 class BROWSER_CONTEXT_KEYED_SERVICE_EXPORT BrowserContextKeyedServiceFactory
27 : public BrowserContextKeyedBaseFactory
{
29 // A function that supplies the instance of a BrowserContextKeyedService
30 // for a given BrowserContext. This is used primarily for testing, where
31 // we want to feed a specific mock into the BCKSF system.
32 typedef BrowserContextKeyedService
*
33 (*FactoryFunction
)(content::BrowserContext
* context
);
35 // Associates |factory| with |context| so that |factory| is used to create
36 // the BrowserContextKeyedService when requested. |factory| can be NULL
37 // to signal that BrowserContextKeyedService should be NULL. Multiple calls to
38 // SetTestingFactory() are allowed; previous services will be shut down.
39 void SetTestingFactory(content::BrowserContext
* context
,
40 FactoryFunction factory
);
42 // Associates |factory| with |context| and immediately returns the created
43 // BrowserContextKeyedService. Since the factory will be used immediately,
44 // it may not be NULL.
45 BrowserContextKeyedService
* SetTestingFactoryAndUse(
46 content::BrowserContext
* context
,
47 FactoryFunction factory
);
50 // BrowserContextKeyedServiceFactories must communicate with a
51 // BrowserContextDependencyManager. For all non-test code, write your subclass
52 // constructors like this:
54 // MyServiceFactory::MyServiceFactory()
55 // : BrowserContextKeyedServiceFactory(
57 // BrowserContextDependencyManager::GetInstance())
59 BrowserContextKeyedServiceFactory(const char* name
,
60 BrowserContextDependencyManager
* manager
);
61 virtual ~BrowserContextKeyedServiceFactory();
63 // Common implementation that maps |context| to some service object. Deals
64 // with incognito contexts per subclass instructions with
65 // GetBrowserContextRedirectedInIncognito() and
66 // GetBrowserContextOwnInstanceInIncognito() through the
67 // GetBrowserContextToUse() method on the base. If |create| is true, the
68 // service will be created using BuildServiceInstanceFor() if it doesn't
70 BrowserContextKeyedService
* GetServiceForBrowserContext(
71 content::BrowserContext
* context
,
74 // Maps |context| to |service| with debug checks to prevent duplication.
75 void Associate(content::BrowserContext
* context
,
76 BrowserContextKeyedService
* service
);
78 // All subclasses of BrowserContextKeyedServiceFactory must return a
79 // BrowserContextKeyedService instead of just a BrowserContextKeyedBase.
80 virtual BrowserContextKeyedService
* BuildServiceInstanceFor(
81 content::BrowserContext
* context
) const = 0;
83 // A helper object actually listens for notifications about BrowserContext
84 // destruction, calculates the order in which things are destroyed and then
85 // does a two pass shutdown.
87 // First, BrowserContextShutdown() is called on every ServiceFactory and will
88 // usually call BrowserContextKeyedService::Shutdown(), which gives each
89 // BrowserContextKeyedService a chance to remove dependencies on other
90 // services that it may be holding.
92 // Secondly, BrowserContextDestroyed() is called on every ServiceFactory
93 // and the default implementation removes it from |mapping_| and deletes
95 virtual void BrowserContextShutdown(
96 content::BrowserContext
* context
) OVERRIDE
;
97 virtual void BrowserContextDestroyed(
98 content::BrowserContext
* context
) OVERRIDE
;
100 virtual void SetEmptyTestingFactory(
101 content::BrowserContext
* context
) OVERRIDE
;
102 virtual void CreateServiceNow(content::BrowserContext
* context
) OVERRIDE
;
105 friend class BrowserContextDependencyManager
;
106 friend class BrowserContextDependencyManagerUnittests
;
108 typedef std::map
<content::BrowserContext
*, BrowserContextKeyedService
*>
109 BrowserContextKeyedServices
;
110 typedef std::map
<content::BrowserContext
*, FactoryFunction
>
111 BrowserContextOverriddenFunctions
;
113 // The mapping between a BrowserContext and its service.
114 std::map
<content::BrowserContext
*, BrowserContextKeyedService
*> mapping_
;
116 // The mapping between a BrowserContext and its overridden FactoryFunction.
117 std::map
<content::BrowserContext
*, FactoryFunction
> factories_
;
119 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedServiceFactory
);
122 #endif // COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_