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