OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SERVIC
E_FACTORY_H_ | 5 #ifndef COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SERVIC
E_FACTORY_H_ |
6 #define COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SERVIC
E_FACTORY_H_ | 6 #define COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SERVIC
E_FACTORY_H_ |
7 | 7 |
8 #include <map> | |
9 | |
10 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
11 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
12 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
13 #include "components/keyed_service/content/browser_context_keyed_base_factory.h" | |
14 #include "components/keyed_service/core/keyed_service_export.h" | 11 #include "components/keyed_service/core/keyed_service_export.h" |
| 12 #include "components/keyed_service/core/refcounted_keyed_service_factory.h" |
15 | 13 |
| 14 class BrowserContextDependencyManager; |
16 class RefcountedKeyedService; | 15 class RefcountedKeyedService; |
17 | 16 |
18 namespace content { | 17 namespace content { |
19 class BrowserContext; | 18 class BrowserContext; |
20 } | 19 } |
21 | 20 |
22 // A specialized BrowserContextKeyedServiceFactory that manages a | 21 // A specialized BrowserContextKeyedServiceFactory that manages a |
23 // RefcountedThreadSafe<>. | 22 // RefcountedThreadSafe<>. |
24 // | 23 // |
25 // While the factory returns RefcountedThreadSafe<>s, the factory itself is a | 24 // While the factory returns RefcountedThreadSafe<>s, the factory itself is a |
26 // base::NotThreadSafe. Only call methods on this object on the UI thread. | 25 // base::NotThreadSafe. Only call methods on this object on the UI thread. |
27 // | 26 // |
28 // Implementers of RefcountedKeyedService should note that we guarantee that | 27 // Implementers of RefcountedKeyedService should note that we guarantee that |
29 // ShutdownOnUIThread() is called on the UI thread, but actual object | 28 // ShutdownOnUIThread() is called on the UI thread, but actual object |
30 // destruction can happen anywhere. | 29 // destruction can happen anywhere. |
31 class KEYED_SERVICE_EXPORT RefcountedBrowserContextKeyedServiceFactory | 30 class KEYED_SERVICE_EXPORT RefcountedBrowserContextKeyedServiceFactory |
32 : public BrowserContextKeyedBaseFactory { | 31 : public RefcountedKeyedServiceFactory { |
33 public: | 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 |
34 // A function that supplies the instance of a KeyedService for a given | 43 // A function that supplies the instance of a KeyedService for a given |
35 // BrowserContext. This is used primarily for testing, where we want to feed | 44 // BrowserContext. This is used primarily for testing, where we want to feed |
36 // a specific mock into the BCKSF system. | 45 // a specific mock into the BCKSF system. |
37 typedef scoped_refptr<RefcountedKeyedService>(*TestingFactoryFunction)( | 46 typedef scoped_refptr<RefcountedKeyedService>(*TestingFactoryFunction)( |
38 content::BrowserContext* context); | 47 content::BrowserContext* context); |
39 | 48 |
40 // Associates |factory| with |context| so that |factory| is used to create | 49 // Associates |factory| with |context| so that |factory| is used to create |
41 // the KeyedService when requested. |factory| can be NULL to signal that | 50 // the KeyedService when requested. |factory| can be NULL to signal that |
42 // KeyedService should be NULL. Multiple calls to SetTestingFactory() are | 51 // KeyedService should be NULL. Multiple calls to SetTestingFactory() are |
43 // allowed; previous services will be shut down. | 52 // allowed; previous services will be shut down. |
44 void SetTestingFactory(content::BrowserContext* context, | 53 void SetTestingFactory(content::BrowserContext* context, |
45 TestingFactoryFunction factory); | 54 TestingFactoryFunction factory); |
46 | 55 |
47 // Associates |factory| with |context| and immediately returns the created | 56 // Associates |factory| with |context| and immediately returns the created |
48 // KeyedService. Since the factory will be used immediately, it may not be | 57 // KeyedService. Since the factory will be used immediately, it may not be |
49 // NULL. | 58 // NULL. |
50 scoped_refptr<RefcountedKeyedService> SetTestingFactoryAndUse( | 59 scoped_refptr<RefcountedKeyedService> SetTestingFactoryAndUse( |
51 content::BrowserContext* context, | 60 content::BrowserContext* context, |
52 TestingFactoryFunction factory); | 61 TestingFactoryFunction factory); |
53 | 62 |
54 protected: | 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 // {} |
55 RefcountedBrowserContextKeyedServiceFactory( | 73 RefcountedBrowserContextKeyedServiceFactory( |
56 const char* name, | 74 const char* name, |
57 BrowserContextDependencyManager* manager); | 75 BrowserContextDependencyManager* manager); |
58 ~RefcountedBrowserContextKeyedServiceFactory() override; | 76 ~RefcountedBrowserContextKeyedServiceFactory() override; |
59 | 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. |
60 scoped_refptr<RefcountedKeyedService> GetServiceForBrowserContext( | 85 scoped_refptr<RefcountedKeyedService> GetServiceForBrowserContext( |
61 content::BrowserContext* context, | 86 content::BrowserContext* context, |
62 bool create); | 87 bool create); |
63 | 88 |
64 // Maps |context| to |service| with debug checks to prevent duplication. | 89 // Interface for people building a concrete FooServiceFactory: -------------- |
65 void Associate(content::BrowserContext* context, | |
66 const scoped_refptr<RefcountedKeyedService>& service); | |
67 | 90 |
68 // All subclasses of RefcountedBrowserContextKeyedServiceFactory must return | 91 // Finds which browser context (if any) to use. |
69 // a RefcountedKeyedService instead of just | 92 virtual content::BrowserContext* GetBrowserContextToUse( |
70 // a BrowserContextKeyedBase. | 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. |
71 virtual scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor( | 111 virtual scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor( |
72 content::BrowserContext* context) const = 0; | 112 content::BrowserContext* context) const = 0; |
73 | 113 |
74 void BrowserContextShutdown(content::BrowserContext* context) override; | 114 // A helper object actually listens for notifications about BrowserContext |
75 void BrowserContextDestroyed(content::BrowserContext* context) override; | 115 // destruction, calculates the order in which things are destroyed and then |
76 void SetEmptyTestingFactory(content::BrowserContext* context) override; | 116 // does a two pass shutdown. |
77 bool HasTestingFactory(content::BrowserContext* context) override; | 117 // |
78 void CreateServiceNow(content::BrowserContext* context) override; | 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); |
79 | 128 |
80 private: | 129 private: |
81 typedef std::map<content::BrowserContext*, | 130 friend class BrowserContextDependencyManagerUnittests; |
82 scoped_refptr<RefcountedKeyedService>> RefCountedStorage; | |
83 typedef std::map<content::BrowserContext*, TestingFactoryFunction> | |
84 BrowserContextOverriddenTestingFunctions; | |
85 | 131 |
86 // The mapping between a BrowserContext and its refcounted service. | 132 // Registers any user preferences on this service. This is called by |
87 RefCountedStorage mapping_; | 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) {} |
88 | 137 |
89 // The mapping between a BrowserContext and its overridden | 138 // RefcountedKeyedServiceFactory: |
90 // TestingFactoryFunction. | 139 scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor( |
91 BrowserContextOverriddenTestingFunctions testing_factories_; | 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; |
92 | 152 |
93 DISALLOW_COPY_AND_ASSIGN(RefcountedBrowserContextKeyedServiceFactory); | 153 DISALLOW_COPY_AND_ASSIGN(RefcountedBrowserContextKeyedServiceFactory); |
94 }; | 154 }; |
95 | 155 |
96 #endif // COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SER
VICE_FACTORY_H_ | 156 #endif // COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SER
VICE_FACTORY_H_ |
OLD | NEW |