Make castv2 performance test work.
[chromium-blink-merge.git] / components / keyed_service / ios / refcounted_browser_state_keyed_service_factory.h
blobfae4b53325a6c8471d6c746512489e2b41303a4b
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_REFCOUNTED_BROWSER_STATE_KEYED_SERVICE_FACTORY_H_
6 #define COMPONENTS_KEYED_SERVICE_IOS_REFCOUNTED_BROWSER_STATE_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 BrowserStateDependencyManager;
15 class RefcountedKeyedService;
17 namespace web {
18 class BrowserState;
21 // A specialized BrowserStateKeyedServiceFactory 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 RefcountedBrowserStateKeyedServiceFactory
31 : public RefcountedKeyedServiceFactory {
32 public:
33 // A function that supplies the instance of a KeyedService for a given
34 // BrowserState. This is used primarily for testing, where we want to feed
35 // a specific mock into the BSKSF system.
36 typedef scoped_refptr<RefcountedKeyedService>(*TestingFactoryFunction)(
37 web::BrowserState* 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(web::BrowserState* 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 web::BrowserState* context,
51 TestingFactoryFunction factory);
53 protected:
54 // RefcountedBrowserStateKeyedServiceFactories must communicate with a
55 // BrowserStateDependencyManager. For all non-test code, write your subclass
56 // constructors like this:
58 // MyServiceFactory::MyServiceFactory()
59 // : RefcountedBrowserStateKeyedServiceFactory(
60 // "MyService",
61 // BrowserStateDependencyManager::GetInstance()) {
62 // }
63 RefcountedBrowserStateKeyedServiceFactory(
64 const char* name,
65 /*BrowserState*/DependencyManager* manager);
66 ~RefcountedBrowserStateKeyedServiceFactory() override;
68 // Common implementation that maps |context| to some service object. Deals
69 // with incognito and testing contexts per subclass instructions. If |create|
70 // is true, the service will be created using BuildServiceInstanceFor() if it
71 // doesn't already exist.
72 scoped_refptr<RefcountedKeyedService> GetServiceForBrowserState(
73 web::BrowserState* context,
74 bool create);
76 // Interface for people building a concrete FooServiceFactory: --------------
78 // Finds which browser state (if any) to use.
79 virtual web::BrowserState* GetBrowserStateToUse(
80 web::BrowserState* context) const;
82 // By default, we create instances of a service lazily and wait until
83 // GetForBrowserState() is called on our subclass. Some services need to be
84 // created as soon as the BrowserState has been brought up.
85 virtual bool ServiceIsCreatedWithBrowserState() const;
87 // By default, TestingBrowserStates will be treated like normal contexts.
88 // You can override this so that by default, the service associated with the
89 // TestingBrowserState is NULL. (This is just a shortcut around
90 // SetTestingFactory() to make sure our contexts don't directly refer to the
91 // services they use.)
92 bool ServiceIsNULLWhileTesting() const override;
94 // Interface for people building a type of BrowserStateKeyedFactory: -------
96 // All subclasses of BrowserStateKeyedServiceFactory must return a
97 // KeyedService instead of just a BrowserStateKeyedBase.
98 virtual scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor(
99 web::BrowserState* context) const = 0;
101 // A helper object actually listens for notifications about BrowserState
102 // destruction, calculates the order in which things are destroyed and then
103 // does a two pass shutdown.
105 // First, BrowserStateShutdown() is called on every ServiceFactory and will
106 // usually call KeyedService::Shutdown(), which gives each
107 // KeyedService a chance to remove dependencies on other
108 // services that it may be holding.
110 // Secondly, BrowserStateDestroyed() is called on every ServiceFactory
111 // and the default implementation removes it from |mapping_| and deletes
112 // the pointer.
113 virtual void BrowserStateShutdown(web::BrowserState* context);
114 virtual void BrowserStateDestroyed(web::BrowserState* context);
116 private:
117 // Registers any user preferences on this service. This is called by
118 // RegisterProfilePrefsIfNecessary() and should be overriden by any service
119 // that wants to register profile-specific preferences.
120 virtual void RegisterProfilePrefs(
121 user_prefs::PrefRegistrySyncable* registry) {}
123 // RefcountedKeyedServiceFactory:
124 scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor(
125 base::SupportsUserData* context) const final;
126 bool IsOffTheRecord(base::SupportsUserData* context) const final;
128 // KeyedServiceBaseFactory:
129 user_prefs::PrefRegistrySyncable* GetAssociatedPrefRegistry(
130 base::SupportsUserData* context) const final;
131 base::SupportsUserData* GetContextToUse(
132 base::SupportsUserData* context) const final;
133 bool ServiceIsCreatedWithContext() const final;
134 void ContextShutdown(base::SupportsUserData* context) final;
135 void ContextDestroyed(base::SupportsUserData* context) final;
136 void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry) final;
138 DISALLOW_COPY_AND_ASSIGN(RefcountedBrowserStateKeyedServiceFactory);
141 #endif // COMPONENTS_KEYED_SERVICE_IOS_REFCOUNTED_BROWSER_STATE_KEYED_SERVICE_FACTORY_H_