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/content/browser_context_keyed_service_factory.h" 12 #include "components/keyed_service/content/refcounted_browser_context_keyed_service.h" 13 #include "components/keyed_service/core/keyed_service_export.h" 14 15 class RefcountedBrowserContextKeyedService; 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 RefcountedBrowserContextKeyedService should note that 28 // we guarantee that ShutdownOnUIThread() is called on the UI thread, but actual 29 // object destruction can happen anywhere. 30 class KEYED_SERVICE_EXPORT RefcountedBrowserContextKeyedServiceFactory 31 : public BrowserContextKeyedBaseFactory { 32 public: 33 // A function that supplies the instance of a KeyedService for a given 34 // BrowserContext. This is used primarily for testing, where we want to feed 35 // a specific mock into the BCKSF system. 36 typedef scoped_refptr<RefcountedBrowserContextKeyedService>( 37 *TestingFactoryFunction)(content::BrowserContext* context); 38 39 // Associates |factory| with |context| so that |factory| is used to create 40 // the KeyedService when requested. |factory| can be NULL to signal that 41 // KeyedService should be NULL. Multiple calls to SetTestingFactory() are 42 // allowed; previous services will be shut down. 43 void SetTestingFactory(content::BrowserContext* context, 44 TestingFactoryFunction factory); 45 46 // Associates |factory| with |context| and immediately returns the created 47 // KeyedService. Since the factory will be used immediately, it may not be 48 // NULL. 49 scoped_refptr<RefcountedBrowserContextKeyedService> SetTestingFactoryAndUse( 50 content::BrowserContext* context, 51 TestingFactoryFunction factory); 52 53 protected: 54 RefcountedBrowserContextKeyedServiceFactory( 55 const char* name, 56 BrowserContextDependencyManager* manager); 57 virtual ~RefcountedBrowserContextKeyedServiceFactory(); 58 59 scoped_refptr<RefcountedBrowserContextKeyedService> 60 GetServiceForBrowserContext(content::BrowserContext* context, 61 bool create); 62 63 // Maps |context| to |service| with debug checks to prevent duplication. 64 void Associate( 65 content::BrowserContext* context, 66 const scoped_refptr<RefcountedBrowserContextKeyedService>& service); 67 68 // All subclasses of RefcountedBrowserContextKeyedServiceFactory must return 69 // a RefcountedBrowserContextKeyedService instead of just 70 // a BrowserContextKeyedBase. 71 virtual scoped_refptr<RefcountedBrowserContextKeyedService> 72 BuildServiceInstanceFor(content::BrowserContext* context) const = 0; 73 74 virtual void BrowserContextShutdown(content::BrowserContext* context) 75 OVERRIDE; 76 virtual void BrowserContextDestroyed(content::BrowserContext* context) 77 OVERRIDE; 78 virtual void SetEmptyTestingFactory(content::BrowserContext* context) 79 OVERRIDE; 80 virtual bool HasTestingFactory(content::BrowserContext* context) OVERRIDE; 81 virtual void CreateServiceNow(content::BrowserContext* context) OVERRIDE; 82 83 private: 84 typedef std::map<content::BrowserContext*, 85 scoped_refptr<RefcountedBrowserContextKeyedService> > 86 RefCountedStorage; 87 typedef std::map<content::BrowserContext*, TestingFactoryFunction> 88 BrowserContextOverriddenTestingFunctions; 89 90 // The mapping between a BrowserContext and its refcounted service. 91 RefCountedStorage mapping_; 92 93 // The mapping between a BrowserContext and its overridden 94 // TestingFactoryFunction. 95 BrowserContextOverriddenTestingFunctions testing_factories_; 96 97 DISALLOW_COPY_AND_ASSIGN(RefcountedBrowserContextKeyedServiceFactory); 98 }; 99 100 #endif // COMPONENTS_KEYED_SERVICE_CONTENT_REFCOUNTED_BROWSER_CONTEXT_KEYED_SERVICE_FACTORY_H_ 101