Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / keyed_service / content / refcounted_browser_context_keyed_service_factory.h
blobf8350fdb539aa2bb98c7b0219004edcef6e8d6ae
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/core/keyed_service_export.h"
12 #include "components/keyed_service/core/refcounted_keyed_service_factory.h"
14 class BrowserContextDependencyManager;
15 class RefcountedKeyedService;
17 namespace content {
18 class BrowserContext;
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 RefcountedKeyedService should note that we guarantee that
28 // ShutdownOnUIThread() is called on the UI thread, but actual object
29 // destruction can happen anywhere.
30 class KEYED_SERVICE_EXPORT RefcountedBrowserContextKeyedServiceFactory
31 : public RefcountedKeyedServiceFactory {
32 public:
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<RefcountedKeyedService>(*TestingFactoryFunction)(
37 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
48 // NULL.
49 scoped_refptr<RefcountedKeyedService> SetTestingFactoryAndUse(
50 content::BrowserContext* context,
51 TestingFactoryFunction factory);
53 protected:
54 // RefcountedBrowserContextKeyedServiceFactories must communicate with a
55 // BrowserContextDependencyManager. For all non-test code, write your subclass
56 // constructors like this:
58 // MyServiceFactory::MyServiceFactory()
59 // : RefcountedBrowserContextKeyedServiceFactory(
60 // "MyService",
61 // BrowserContextDependencyManager::GetInstance())
62 // {}
63 RefcountedBrowserContextKeyedServiceFactory(
64 const char* name,
65 BrowserContextDependencyManager* manager);
66 ~RefcountedBrowserContextKeyedServiceFactory() override;
68 // Common implementation that maps |context| to some service object. Deals
69 // with incognito contexts per subclass instructions with
70 // GetBrowserContextRedirectedInIncognito() and
71 // GetBrowserContextOwnInstanceInIncognito() through the
72 // GetBrowserContextToUse() method on the base. If |create| is true, the
73 // service will be created using BuildServiceInstanceFor() if it doesn't
74 // already exist.
75 scoped_refptr<RefcountedKeyedService> GetServiceForBrowserContext(
76 content::BrowserContext* context,
77 bool create);
79 // Interface for people building a concrete FooServiceFactory: --------------
81 // Finds which browser context (if any) to use.
82 virtual content::BrowserContext* GetBrowserContextToUse(
83 content::BrowserContext* context) const;
85 // By default, we create instances of a service lazily and wait until
86 // GetForBrowserContext() is called on our subclass. Some services need to be
87 // created as soon as the BrowserContext has been brought up.
88 virtual bool ServiceIsCreatedWithBrowserContext() const;
90 // By default, TestingBrowserContexts will be treated like normal contexts.
91 // You can override this so that by default, the service associated with the
92 // TestingBrowserContext is NULL. (This is just a shortcut around
93 // SetTestingFactory() to make sure our contexts don't directly refer to the
94 // services they use.)
95 bool ServiceIsNULLWhileTesting() const override;
97 // Interface for people building a type of BrowserContextKeyedFactory: -------
99 // All subclasses of BrowserContextKeyedServiceFactory must return a
100 // KeyedService instead of just a BrowserContextKeyedBase.
101 virtual scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor(
102 content::BrowserContext* context) const = 0;
104 // A helper object actually listens for notifications about BrowserContext
105 // destruction, calculates the order in which things are destroyed and then
106 // does a two pass shutdown.
108 // First, BrowserContextShutdown() is called on every ServiceFactory and will
109 // usually call KeyedService::Shutdown(), which gives each
110 // KeyedService a chance to remove dependencies on other
111 // services that it may be holding.
113 // Secondly, BrowserContextDestroyed() is called on every ServiceFactory
114 // and the default implementation removes it from |mapping_| and deletes
115 // the pointer.
116 virtual void BrowserContextShutdown(content::BrowserContext* context);
117 virtual void BrowserContextDestroyed(content::BrowserContext* context);
119 private:
120 friend class BrowserContextDependencyManagerUnittests;
122 // Registers any user preferences on this service. This is called by
123 // RegisterPrefsIfNecessaryForContext() and should be overriden by any service
124 // that wants to register profile-specific preferences.
125 virtual void RegisterProfilePrefs(
126 user_prefs::PrefRegistrySyncable* registry) {}
128 // RefcountedKeyedServiceFactory:
129 scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor(
130 base::SupportsUserData* context) const final;
131 bool IsOffTheRecord(base::SupportsUserData* context) const final;
133 // KeyedServiceBaseFactory:
134 base::SupportsUserData* GetContextToUse(
135 base::SupportsUserData* context) const final;
136 bool ServiceIsCreatedWithContext() const final;
137 void ContextShutdown(base::SupportsUserData* context) final;
138 void ContextDestroyed(base::SupportsUserData* context) final;
139 void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry) final;
141 DISALLOW_COPY_AND_ASSIGN(RefcountedBrowserContextKeyedServiceFactory);
144 #endif // COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_