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_REFCOUNTED_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_
6 #define COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/ref_counted.h"
11 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
12 #include "components/keyed_service/content/refcounted_browser_context_keyed_service.h"
13 #include "components/keyed_service/core/keyed_service_export.h"
15 class RefcountedBrowserContextKeyedService
;
21 // A specialized BrowserContextKeyedServiceFactory that manages a
22 // RefcountedThreadSafe<>.
24 // While the factory returns RefcountedThreadSafe<>s, the factory itself is a
25 // base::NotThreadSafe. Only call methods on this object on the UI thread.
27 // Implementers of RefcountedBrowserContextKeyedService should note that
28 // we guarantee that ShutdownOnUIThread() is called on the UI thread, but actual
29 // object destruction can happen anywhere.
30 class KEYED_SERVICE_EXPORT RefcountedBrowserContextKeyedServiceFactory
31 : public BrowserContextKeyedBaseFactory
{
33 // A function that supplies the instance of a KeyedService for a given
34 // BrowserContext. This is used primarily for testing, where we want to feed
35 // a specific mock into the BCKSF system.
36 typedef scoped_refptr
<RefcountedBrowserContextKeyedService
>(
37 *TestingFactoryFunction
)(content::BrowserContext
* context
);
39 // Associates |factory| with |context| so that |factory| is used to create
40 // the KeyedService when requested. |factory| can be NULL to signal that
41 // KeyedService should be NULL. Multiple calls to SetTestingFactory() are
42 // allowed; previous services will be shut down.
43 void SetTestingFactory(content::BrowserContext
* context
,
44 TestingFactoryFunction factory
);
46 // Associates |factory| with |context| and immediately returns the created
47 // KeyedService. Since the factory will be used immediately, it may not be
49 scoped_refptr
<RefcountedBrowserContextKeyedService
> SetTestingFactoryAndUse(
50 content::BrowserContext
* context
,
51 TestingFactoryFunction factory
);
54 RefcountedBrowserContextKeyedServiceFactory(
56 BrowserContextDependencyManager
* manager
);
57 virtual ~RefcountedBrowserContextKeyedServiceFactory();
59 scoped_refptr
<RefcountedBrowserContextKeyedService
>
60 GetServiceForBrowserContext(content::BrowserContext
* context
,
63 // Maps |context| to |service| with debug checks to prevent duplication.
65 content::BrowserContext
* context
,
66 const scoped_refptr
<RefcountedBrowserContextKeyedService
>& service
);
68 // All subclasses of RefcountedBrowserContextKeyedServiceFactory must return
69 // a RefcountedBrowserContextKeyedService instead of just
70 // a BrowserContextKeyedBase.
71 virtual scoped_refptr
<RefcountedBrowserContextKeyedService
>
72 BuildServiceInstanceFor(content::BrowserContext
* context
) const = 0;
74 virtual void BrowserContextShutdown(content::BrowserContext
* context
)
76 virtual void BrowserContextDestroyed(content::BrowserContext
* context
)
78 virtual void SetEmptyTestingFactory(content::BrowserContext
* context
)
80 virtual void CreateServiceNow(content::BrowserContext
* context
) OVERRIDE
;
83 typedef std::map
<content::BrowserContext
*,
84 scoped_refptr
<RefcountedBrowserContextKeyedService
> >
86 typedef std::map
<content::BrowserContext
*, TestingFactoryFunction
>
87 BrowserContextOverriddenTestingFunctions
;
89 // The mapping between a BrowserContext and its refcounted service.
90 RefCountedStorage mapping_
;
92 // The mapping between a BrowserContext and its overridden
93 // TestingFactoryFunction.
94 BrowserContextOverriddenTestingFunctions testing_factories_
;
96 DISALLOW_COPY_AND_ASSIGN(RefcountedBrowserContextKeyedServiceFactory
);
99 #endif // COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_