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_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H
_ | 5 #ifndef COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H
_ |
6 #define COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H
_ | 6 #define COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_SERVICE_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 "components/keyed_service/content/browser_context_keyed_base_factory.h" | |
13 #include "components/keyed_service/core/keyed_service_export.h" | 10 #include "components/keyed_service/core/keyed_service_export.h" |
| 11 #include "components/keyed_service/core/keyed_service_factory.h" |
14 | 12 |
15 class BrowserContextDependencyManager; | 13 class BrowserContextDependencyManager; |
16 class KeyedService; | 14 class KeyedService; |
17 | 15 |
| 16 namespace content { |
| 17 class BrowserContext; |
| 18 } |
| 19 |
18 // Base class for Factories that take a BrowserContext object and return some | 20 // Base class for Factories that take a BrowserContext object and return some |
19 // service on a one-to-one mapping. Each factory that derives from this class | 21 // service on a one-to-one mapping. Each factory that derives from this class |
20 // *must* be a Singleton (only unit tests don't do that). | 22 // *must* be a Singleton (only unit tests don't do that). |
21 // | 23 // |
22 // We do this because services depend on each other and we need to control | 24 // We do this because services depend on each other and we need to control |
23 // shutdown/destruction order. In each derived classes' constructors, the | 25 // shutdown/destruction order. In each derived classes' constructors, the |
24 // implementors must explicitly state which services are depended on. | 26 // implementors must explicitly state which services are depended on. |
25 class KEYED_SERVICE_EXPORT BrowserContextKeyedServiceFactory | 27 class KEYED_SERVICE_EXPORT BrowserContextKeyedServiceFactory |
26 : public BrowserContextKeyedBaseFactory { | 28 : public KeyedServiceFactory { |
27 public: | 29 public: |
| 30 // Registers preferences used in this service on the pref service of |
| 31 // |context|. This is the public interface and is safe to be called multiple |
| 32 // times because testing code can have multiple services of the same type |
| 33 // attached to a single |context|. Only test code is allowed to call this |
| 34 // method. |
| 35 // TODO(gab): This method can be removed entirely when |
| 36 // PrefService::DeprecatedGetPrefRegistry() is phased out. |
| 37 void RegisterUserPrefsOnBrowserContextForTest( |
| 38 content::BrowserContext* context); |
| 39 |
28 // A function that supplies the instance of a KeyedService for a given | 40 // A function that supplies the instance of a KeyedService for a given |
29 // BrowserContext. This is used primarily for testing, where we want to feed | 41 // BrowserContext. This is used primarily for testing, where we want to feed |
30 // a specific mock into the BCKSF system. | 42 // a specific mock into the BCKSF system. |
31 typedef KeyedService* (*TestingFactoryFunction)( | 43 typedef KeyedService* (*TestingFactoryFunction)( |
32 content::BrowserContext* context); | 44 content::BrowserContext* context); |
33 | 45 |
34 // Associates |factory| with |context| so that |factory| is used to create | 46 // Associates |factory| with |context| so that |factory| is used to create |
35 // the KeyedService when requested. |factory| can be NULL to signal that | 47 // the KeyedService when requested. |factory| can be NULL to signal that |
36 // KeyedService should be NULL. Multiple calls to SetTestingFactory() are | 48 // KeyedService should be NULL. Multiple calls to SetTestingFactory() are |
37 // allowed; previous services will be shut down. | 49 // allowed; previous services will be shut down. |
(...skipping 23 matching lines...) Expand all Loading... |
61 // Common implementation that maps |context| to some service object. Deals | 73 // Common implementation that maps |context| to some service object. Deals |
62 // with incognito contexts per subclass instructions with | 74 // with incognito contexts per subclass instructions with |
63 // GetBrowserContextRedirectedInIncognito() and | 75 // GetBrowserContextRedirectedInIncognito() and |
64 // GetBrowserContextOwnInstanceInIncognito() through the | 76 // GetBrowserContextOwnInstanceInIncognito() through the |
65 // GetBrowserContextToUse() method on the base. If |create| is true, the | 77 // GetBrowserContextToUse() method on the base. If |create| is true, the |
66 // service will be created using BuildServiceInstanceFor() if it doesn't | 78 // service will be created using BuildServiceInstanceFor() if it doesn't |
67 // already exist. | 79 // already exist. |
68 KeyedService* GetServiceForBrowserContext(content::BrowserContext* context, | 80 KeyedService* GetServiceForBrowserContext(content::BrowserContext* context, |
69 bool create); | 81 bool create); |
70 | 82 |
71 // Maps |context| to |service| with debug checks to prevent duplication. | 83 // Interface for people building a concrete FooServiceFactory: -------------- |
72 void Associate(content::BrowserContext* context, KeyedService* service); | |
73 | 84 |
74 // Removes the mapping from |context| to a service. | 85 // Finds which browser context (if any) to use. |
75 void Disassociate(content::BrowserContext* context); | 86 virtual content::BrowserContext* GetBrowserContextToUse( |
| 87 content::BrowserContext* context) const; |
| 88 |
| 89 // By default, we create instances of a service lazily and wait until |
| 90 // GetForBrowserContext() is called on our subclass. Some services need to be |
| 91 // created as soon as the BrowserContext has been brought up. |
| 92 virtual bool ServiceIsCreatedWithBrowserContext() const; |
| 93 |
| 94 // By default, TestingBrowserContexts will be treated like normal contexts. |
| 95 // You can override this so that by default, the service associated with the |
| 96 // TestingBrowserContext is NULL. (This is just a shortcut around |
| 97 // SetTestingFactory() to make sure our contexts don't directly refer to the |
| 98 // services they use.) |
| 99 bool ServiceIsNULLWhileTesting() const override; |
| 100 |
| 101 // Interface for people building a type of BrowserContextKeyedFactory: ------- |
76 | 102 |
77 // All subclasses of BrowserContextKeyedServiceFactory must return a | 103 // All subclasses of BrowserContextKeyedServiceFactory must return a |
78 // KeyedService instead of just a BrowserContextKeyedBase. | 104 // KeyedService instead of just a BrowserContextKeyedBase. |
79 virtual KeyedService* BuildServiceInstanceFor( | 105 virtual KeyedService* BuildServiceInstanceFor( |
80 content::BrowserContext* context) const = 0; | 106 content::BrowserContext* context) const = 0; |
81 | 107 |
82 // A helper object actually listens for notifications about BrowserContext | 108 // A helper object actually listens for notifications about BrowserContext |
83 // destruction, calculates the order in which things are destroyed and then | 109 // destruction, calculates the order in which things are destroyed and then |
84 // does a two pass shutdown. | 110 // does a two pass shutdown. |
85 // | 111 // |
86 // First, BrowserContextShutdown() is called on every ServiceFactory and will | 112 // First, BrowserContextShutdown() is called on every ServiceFactory and will |
87 // usually call KeyedService::Shutdown(), which gives each | 113 // usually call KeyedService::Shutdown(), which gives each |
88 // KeyedService a chance to remove dependencies on other | 114 // KeyedService a chance to remove dependencies on other |
89 // services that it may be holding. | 115 // services that it may be holding. |
90 // | 116 // |
91 // Secondly, BrowserContextDestroyed() is called on every ServiceFactory | 117 // Secondly, BrowserContextDestroyed() is called on every ServiceFactory |
92 // and the default implementation removes it from |mapping_| and deletes | 118 // and the default implementation removes it from |mapping_| and deletes |
93 // the pointer. | 119 // the pointer. |
94 void BrowserContextShutdown(content::BrowserContext* context) override; | 120 virtual void BrowserContextShutdown(content::BrowserContext* context); |
95 void BrowserContextDestroyed(content::BrowserContext* context) override; | 121 virtual void BrowserContextDestroyed(content::BrowserContext* context); |
96 | |
97 void SetEmptyTestingFactory(content::BrowserContext* context) override; | |
98 bool HasTestingFactory(content::BrowserContext* context) override; | |
99 void CreateServiceNow(content::BrowserContext* context) override; | |
100 | 122 |
101 private: | 123 private: |
102 friend class BrowserContextDependencyManager; | |
103 friend class BrowserContextDependencyManagerUnittests; | 124 friend class BrowserContextDependencyManagerUnittests; |
104 | 125 |
105 typedef std::map<content::BrowserContext*, KeyedService*> | 126 // Registers any user preferences on this service. This is called by |
106 BrowserContextKeyedServices; | 127 // RegisterProfilePrefsIfNecessary() and should be overriden by any service |
107 typedef std::map<content::BrowserContext*, TestingFactoryFunction> | 128 // that wants to register profile-specific preferences. |
108 BrowserContextOverriddenTestingFunctions; | 129 virtual void RegisterProfilePrefs( |
| 130 user_prefs::PrefRegistrySyncable* registry) {} |
109 | 131 |
110 // The mapping between a BrowserContext and its service. | 132 // KeyedServiceFactory: |
111 BrowserContextKeyedServices mapping_; | 133 KeyedService* BuildServiceInstanceFor( |
| 134 base::SupportsUserData* context) const final; |
| 135 bool IsOffTheRecord(base::SupportsUserData* context) const final; |
112 | 136 |
113 // The mapping between a BrowserContext and its overridden | 137 // KeyedServiceBaseFactory: |
114 // TestingFactoryFunction. | 138 user_prefs::PrefRegistrySyncable* GetAssociatedPrefRegistry( |
115 BrowserContextOverriddenTestingFunctions testing_factories_; | 139 base::SupportsUserData* context) const final; |
| 140 base::SupportsUserData* GetContextToUse( |
| 141 base::SupportsUserData* context) const final; |
| 142 bool ServiceIsCreatedWithContext() const final; |
| 143 void ContextShutdown(base::SupportsUserData* context) final; |
| 144 void ContextDestroyed(base::SupportsUserData* context) final; |
| 145 void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry) final; |
116 | 146 |
117 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedServiceFactory); | 147 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedServiceFactory); |
118 }; | 148 }; |
119 | 149 |
120 #endif // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_SERVICE_FACTOR
Y_H_ | 150 #endif // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_KEYED_SERVICE_FACTOR
Y_H_ |
OLD | NEW |