Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / keyed_service / content / refcounted_browser_context_keyed_service_factory.h
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.
4
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_
7
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"
13
14 class BrowserContextDependencyManager;
15 class RefcountedKeyedService;
16
17 namespace content {
18 class BrowserContext;
19 }
20
21 // A specialized BrowserContextKeyedServiceFactory that manages a
22 // RefcountedThreadSafe<>.
23 //
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.
26 //
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   // Registers preferences used in this service on the pref service of
34   // |context|. This is the public interface and is safe to be called multiple
35   // times because testing code can have multiple services of the same type
36   // attached to a single |context|. Only test code is allowed to call this
37   // method.
38   // TODO(gab): This method can be removed entirely when
39   // PrefService::DeprecatedGetPrefRegistry() is phased out.
40   void RegisterUserPrefsOnBrowserContextForTest(
41       content::BrowserContext* context);
42
43   // A function that supplies the instance of a KeyedService for a given
44   // BrowserContext. This is used primarily for testing, where we want to feed
45   // a specific mock into the BCKSF system.
46   typedef scoped_refptr<RefcountedKeyedService>(*TestingFactoryFunction)(
47       content::BrowserContext* context);
48
49   // Associates |factory| with |context| so that |factory| is used to create
50   // the KeyedService when requested.  |factory| can be NULL to signal that
51   // KeyedService should be NULL. Multiple calls to SetTestingFactory() are
52   // allowed; previous services will be shut down.
53   void SetTestingFactory(content::BrowserContext* context,
54                          TestingFactoryFunction factory);
55
56   // Associates |factory| with |context| and immediately returns the created
57   // KeyedService. Since the factory will be used immediately, it may not be
58   // NULL.
59   scoped_refptr<RefcountedKeyedService> SetTestingFactoryAndUse(
60       content::BrowserContext* context,
61       TestingFactoryFunction factory);
62
63  protected:
64   // RefcountedBrowserContextKeyedServiceFactories must communicate with a
65   // BrowserContextDependencyManager. For all non-test code, write your subclass
66   // constructors like this:
67   //
68   //   MyServiceFactory::MyServiceFactory()
69   //     : RefcountedBrowserContextKeyedServiceFactory(
70   //         "MyService",
71   //         BrowserContextDependencyManager::GetInstance())
72   //   {}
73   RefcountedBrowserContextKeyedServiceFactory(
74       const char* name,
75       BrowserContextDependencyManager* manager);
76   ~RefcountedBrowserContextKeyedServiceFactory() override;
77
78   // Common implementation that maps |context| to some service object. Deals
79   // with incognito contexts per subclass instructions with
80   // GetBrowserContextRedirectedInIncognito() and
81   // GetBrowserContextOwnInstanceInIncognito() through the
82   // GetBrowserContextToUse() method on the base.  If |create| is true, the
83   // service will be created using BuildServiceInstanceFor() if it doesn't
84   // already exist.
85   scoped_refptr<RefcountedKeyedService> GetServiceForBrowserContext(
86       content::BrowserContext* context,
87       bool create);
88
89   // Interface for people building a concrete FooServiceFactory: --------------
90
91   // Finds which browser context (if any) to use.
92   virtual content::BrowserContext* GetBrowserContextToUse(
93       content::BrowserContext* context) const;
94
95   // By default, we create instances of a service lazily and wait until
96   // GetForBrowserContext() is called on our subclass. Some services need to be
97   // created as soon as the BrowserContext has been brought up.
98   virtual bool ServiceIsCreatedWithBrowserContext() const;
99
100   // By default, TestingBrowserContexts will be treated like normal contexts.
101   // You can override this so that by default, the service associated with the
102   // TestingBrowserContext is NULL. (This is just a shortcut around
103   // SetTestingFactory() to make sure our contexts don't directly refer to the
104   // services they use.)
105   bool ServiceIsNULLWhileTesting() const override;
106
107   // Interface for people building a type of BrowserContextKeyedFactory: -------
108
109   // All subclasses of BrowserContextKeyedServiceFactory must return a
110   // KeyedService instead of just a BrowserContextKeyedBase.
111   virtual scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor(
112       content::BrowserContext* context) const = 0;
113
114   // A helper object actually listens for notifications about BrowserContext
115   // destruction, calculates the order in which things are destroyed and then
116   // does a two pass shutdown.
117   //
118   // First, BrowserContextShutdown() is called on every ServiceFactory and will
119   // usually call KeyedService::Shutdown(), which gives each
120   // KeyedService a chance to remove dependencies on other
121   // services that it may be holding.
122   //
123   // Secondly, BrowserContextDestroyed() is called on every ServiceFactory
124   // and the default implementation removes it from |mapping_| and deletes
125   // the pointer.
126   virtual void BrowserContextShutdown(content::BrowserContext* context);
127   virtual void BrowserContextDestroyed(content::BrowserContext* context);
128
129  private:
130   friend class BrowserContextDependencyManagerUnittests;
131
132   // Registers any user preferences on this service. This is called by
133   // RegisterProfilePrefsIfNecessary() and should be overriden by any service
134   // that wants to register profile-specific preferences.
135   virtual void RegisterProfilePrefs(
136       user_prefs::PrefRegistrySyncable* registry) {}
137
138   // RefcountedKeyedServiceFactory:
139   scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor(
140       base::SupportsUserData* context) const final;
141   bool IsOffTheRecord(base::SupportsUserData* context) const final;
142
143   // KeyedServiceBaseFactory:
144   user_prefs::PrefRegistrySyncable* GetAssociatedPrefRegistry(
145       base::SupportsUserData* context) const final;
146   base::SupportsUserData* GetContextToUse(
147       base::SupportsUserData* context) const final;
148   bool ServiceIsCreatedWithContext() const final;
149   void ContextShutdown(base::SupportsUserData* context) final;
150   void ContextDestroyed(base::SupportsUserData* context) final;
151   void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry) final;
152
153   DISALLOW_COPY_AND_ASSIGN(RefcountedBrowserContextKeyedServiceFactory);
154 };
155
156 #endif  // COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_