1 // Copyright (c) 2012 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_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_ 6 #define COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_ 7 8 #include <map> 9 10 #include "base/basictypes.h" 11 #include "base/compiler_specific.h" 12 #include "components/browser_context_keyed_service/browser_context_keyed_base_factory.h" 13 #include "components/browser_context_keyed_service/browser_context_keyed_service.h" 14 #include "components/browser_context_keyed_service/browser_context_keyed_service_export.h" 15 16 class BrowserContextDependencyManager; 17 class BrowserContextKeyedService; 18 19 // Base class for Factories that take a BrowserContext object and return some 20 // service on a one-to-one mapping. Each factory that derives from this class 21 // *must* be a Singleton (only unit tests don't do that). 22 // 23 // We do this because services depend on each other and we need to control 24 // shutdown/destruction order. In each derived classes' constructors, the 25 // implementors must explicitly state which services are depended on. 26 class BROWSER_CONTEXT_KEYED_SERVICE_EXPORT BrowserContextKeyedServiceFactory 27 : public BrowserContextKeyedBaseFactory { 28 public: 29 // A function that supplies the instance of a BrowserContextKeyedService 30 // for a given BrowserContext. This is used primarily for testing, where 31 // we want to feed a specific mock into the BCKSF system. 32 typedef BrowserContextKeyedService* 33 (*FactoryFunction)(content::BrowserContext* context); 34 35 // Associates |factory| with |context| so that |factory| is used to create 36 // the BrowserContextKeyedService when requested. |factory| can be NULL 37 // to signal that BrowserContextKeyedService should be NULL. Multiple calls to 38 // SetTestingFactory() are allowed; previous services will be shut down. 39 void SetTestingFactory(content::BrowserContext* context, 40 FactoryFunction factory); 41 42 // Associates |factory| with |context| and immediately returns the created 43 // BrowserContextKeyedService. Since the factory will be used immediately, 44 // it may not be NULL. 45 BrowserContextKeyedService* SetTestingFactoryAndUse( 46 content::BrowserContext* context, 47 FactoryFunction factory); 48 49 protected: 50 // BrowserContextKeyedServiceFactories must communicate with a 51 // BrowserContextDependencyManager. For all non-test code, write your subclass 52 // constructors like this: 53 // 54 // MyServiceFactory::MyServiceFactory() 55 // : BrowserContextKeyedServiceFactory( 56 // "MyService", 57 // BrowserContextDependencyManager::GetInstance()) 58 // {} 59 BrowserContextKeyedServiceFactory(const char* name, 60 BrowserContextDependencyManager* manager); 61 virtual ~BrowserContextKeyedServiceFactory(); 62 63 // Common implementation that maps |context| to some service object. Deals 64 // with incognito contexts per subclass instructions with 65 // GetBrowserContextRedirectedInIncognito() and 66 // GetBrowserContextOwnInstanceInIncognito() through the 67 // GetBrowserContextToUse() method on the base. If |create| is true, the 68 // service will be created using BuildServiceInstanceFor() if it doesn't 69 // already exist. 70 BrowserContextKeyedService* GetServiceForBrowserContext( 71 content::BrowserContext* context, 72 bool create); 73 74 // Maps |context| to |service| with debug checks to prevent duplication. 75 void Associate(content::BrowserContext* context, 76 BrowserContextKeyedService* service); 77 78 // All subclasses of BrowserContextKeyedServiceFactory must return a 79 // BrowserContextKeyedService instead of just a BrowserContextKeyedBase. 80 virtual BrowserContextKeyedService* BuildServiceInstanceFor( 81 content::BrowserContext* context) const = 0; 82 83 // A helper object actually listens for notifications about BrowserContext 84 // destruction, calculates the order in which things are destroyed and then 85 // does a two pass shutdown. 86 // 87 // First, BrowserContextShutdown() is called on every ServiceFactory and will 88 // usually call BrowserContextKeyedService::Shutdown(), which gives each 89 // BrowserContextKeyedService a chance to remove dependencies on other 90 // services that it may be holding. 91 // 92 // Secondly, BrowserContextDestroyed() is called on every ServiceFactory 93 // and the default implementation removes it from |mapping_| and deletes 94 // the pointer. 95 virtual void BrowserContextShutdown( 96 content::BrowserContext* context) OVERRIDE; 97 virtual void BrowserContextDestroyed( 98 content::BrowserContext* context) OVERRIDE; 99 100 virtual void SetEmptyTestingFactory( 101 content::BrowserContext* context) OVERRIDE; 102 virtual void CreateServiceNow(content::BrowserContext* context) OVERRIDE; 103 104 private: 105 friend class BrowserContextDependencyManager; 106 friend class BrowserContextDependencyManagerUnittests; 107 108 typedef std::map<content::BrowserContext*, BrowserContextKeyedService*> 109 BrowserContextKeyedServices; 110 typedef std::map<content::BrowserContext*, FactoryFunction> 111 BrowserContextOverriddenFunctions; 112 113 // The mapping between a BrowserContext and its service. 114 std::map<content::BrowserContext*, BrowserContextKeyedService*> mapping_; 115 116 // The mapping between a BrowserContext and its overridden FactoryFunction. 117 std::map<content::BrowserContext*, FactoryFunction> factories_; 118 119 DISALLOW_COPY_AND_ASSIGN(BrowserContextKeyedServiceFactory); 120 }; 121 122 #endif // COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_ 123