Index: components/keyed_service/content/refcounted_browser_context_keyed_service_factory.h |
diff --git a/components/keyed_service/content/refcounted_browser_context_keyed_service_factory.h b/components/keyed_service/content/refcounted_browser_context_keyed_service_factory.h |
index f6f49ca87bfd1d27e737e937022c75f0059079fd..ca005c287089ba83985465b99d60edb71c55a132 100644 |
--- a/components/keyed_service/content/refcounted_browser_context_keyed_service_factory.h |
+++ b/components/keyed_service/content/refcounted_browser_context_keyed_service_factory.h |
@@ -5,14 +5,13 @@ |
#ifndef COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_ |
#define COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_ |
-#include <map> |
- |
#include "base/basictypes.h" |
#include "base/compiler_specific.h" |
#include "base/memory/ref_counted.h" |
-#include "components/keyed_service/content/browser_context_keyed_base_factory.h" |
#include "components/keyed_service/core/keyed_service_export.h" |
+#include "components/keyed_service/core/refcounted_keyed_service_factory.h" |
+class BrowserContextDependencyManager; |
class RefcountedKeyedService; |
namespace content { |
@@ -29,8 +28,18 @@ class BrowserContext; |
// ShutdownOnUIThread() is called on the UI thread, but actual object |
// destruction can happen anywhere. |
class KEYED_SERVICE_EXPORT RefcountedBrowserContextKeyedServiceFactory |
- : public BrowserContextKeyedBaseFactory { |
+ : public RefcountedKeyedServiceFactory { |
public: |
+ // Registers preferences used in this service on the pref service of |
+ // |context|. This is the public interface and is safe to be called multiple |
+ // times because testing code can have multiple services of the same type |
+ // attached to a single |context|. Only test code is allowed to call this |
+ // method. |
+ // TODO(gab): This method can be removed entirely when |
+ // PrefService::DeprecatedGetPrefRegistry() is phased out. |
+ void RegisterUserPrefsOnBrowserContextForTest( |
+ content::BrowserContext* context); |
+ |
// A function that supplies the instance of a KeyedService for a given |
// BrowserContext. This is used primarily for testing, where we want to feed |
// a specific mock into the BCKSF system. |
@@ -52,43 +61,94 @@ class KEYED_SERVICE_EXPORT RefcountedBrowserContextKeyedServiceFactory |
TestingFactoryFunction factory); |
protected: |
+ // RefcountedBrowserContextKeyedServiceFactories must communicate with a |
+ // BrowserContextDependencyManager. For all non-test code, write your subclass |
+ // constructors like this: |
+ // |
+ // MyServiceFactory::MyServiceFactory() |
+ // : RefcountedBrowserContextKeyedServiceFactory( |
+ // "MyService", |
+ // BrowserContextDependencyManager::GetInstance()) |
+ // {} |
RefcountedBrowserContextKeyedServiceFactory( |
const char* name, |
BrowserContextDependencyManager* manager); |
~RefcountedBrowserContextKeyedServiceFactory() override; |
+ // Common implementation that maps |context| to some service object. Deals |
+ // with incognito contexts per subclass instructions with |
+ // GetBrowserContextRedirectedInIncognito() and |
+ // GetBrowserContextOwnInstanceInIncognito() through the |
+ // GetBrowserContextToUse() method on the base. If |create| is true, the |
+ // service will be created using BuildServiceInstanceFor() if it doesn't |
+ // already exist. |
scoped_refptr<RefcountedKeyedService> GetServiceForBrowserContext( |
content::BrowserContext* context, |
bool create); |
- // Maps |context| to |service| with debug checks to prevent duplication. |
- void Associate(content::BrowserContext* context, |
- const scoped_refptr<RefcountedKeyedService>& service); |
+ // Interface for people building a concrete FooServiceFactory: -------------- |
+ |
+ // Finds which browser context (if any) to use. |
+ virtual content::BrowserContext* GetBrowserContextToUse( |
+ content::BrowserContext* context) const; |
+ |
+ // By default, we create instances of a service lazily and wait until |
+ // GetForBrowserContext() is called on our subclass. Some services need to be |
+ // created as soon as the BrowserContext has been brought up. |
+ virtual bool ServiceIsCreatedWithBrowserContext() const; |
- // All subclasses of RefcountedBrowserContextKeyedServiceFactory must return |
- // a RefcountedKeyedService instead of just |
- // a BrowserContextKeyedBase. |
+ // By default, TestingBrowserContexts will be treated like normal contexts. |
+ // You can override this so that by default, the service associated with the |
+ // TestingBrowserContext is NULL. (This is just a shortcut around |
+ // SetTestingFactory() to make sure our contexts don't directly refer to the |
+ // services they use.) |
+ bool ServiceIsNULLWhileTesting() const override; |
+ |
+ // Interface for people building a type of BrowserContextKeyedFactory: ------- |
+ |
+ // All subclasses of BrowserContextKeyedServiceFactory must return a |
+ // KeyedService instead of just a BrowserContextKeyedBase. |
virtual scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor( |
content::BrowserContext* context) const = 0; |
- void BrowserContextShutdown(content::BrowserContext* context) override; |
- void BrowserContextDestroyed(content::BrowserContext* context) override; |
- void SetEmptyTestingFactory(content::BrowserContext* context) override; |
- bool HasTestingFactory(content::BrowserContext* context) override; |
- void CreateServiceNow(content::BrowserContext* context) override; |
+ // A helper object actually listens for notifications about BrowserContext |
+ // destruction, calculates the order in which things are destroyed and then |
+ // does a two pass shutdown. |
+ // |
+ // First, BrowserContextShutdown() is called on every ServiceFactory and will |
+ // usually call KeyedService::Shutdown(), which gives each |
+ // KeyedService a chance to remove dependencies on other |
+ // services that it may be holding. |
+ // |
+ // Secondly, BrowserContextDestroyed() is called on every ServiceFactory |
+ // and the default implementation removes it from |mapping_| and deletes |
+ // the pointer. |
+ virtual void BrowserContextShutdown(content::BrowserContext* context); |
+ virtual void BrowserContextDestroyed(content::BrowserContext* context); |
private: |
- typedef std::map<content::BrowserContext*, |
- scoped_refptr<RefcountedKeyedService>> RefCountedStorage; |
- typedef std::map<content::BrowserContext*, TestingFactoryFunction> |
- BrowserContextOverriddenTestingFunctions; |
- |
- // The mapping between a BrowserContext and its refcounted service. |
- RefCountedStorage mapping_; |
- |
- // The mapping between a BrowserContext and its overridden |
- // TestingFactoryFunction. |
- BrowserContextOverriddenTestingFunctions testing_factories_; |
+ friend class BrowserContextDependencyManagerUnittests; |
+ |
+ // Registers any user preferences on this service. This is called by |
+ // RegisterProfilePrefsIfNecessary() and should be overriden by any service |
+ // that wants to register profile-specific preferences. |
+ virtual void RegisterProfilePrefs( |
+ user_prefs::PrefRegistrySyncable* registry) {} |
+ |
+ // RefcountedKeyedServiceFactory: |
+ scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor( |
+ base::SupportsUserData* context) const final; |
+ bool IsOffTheRecord(base::SupportsUserData* context) const final; |
+ |
+ // KeyedServiceBaseFactory: |
+ user_prefs::PrefRegistrySyncable* GetAssociatedPrefRegistry( |
+ base::SupportsUserData* context) const final; |
+ base::SupportsUserData* GetContextToUse( |
+ base::SupportsUserData* context) const final; |
+ bool ServiceIsCreatedWithContext() const final; |
+ void ContextShutdown(base::SupportsUserData* context) final; |
+ void ContextDestroyed(base::SupportsUserData* context) final; |
+ void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry) final; |
DISALLOW_COPY_AND_ASSIGN(RefcountedBrowserContextKeyedServiceFactory); |
}; |