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_BASE_FACTORY_H_ 6 #define COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_ 7 8 #include <set> 9 10 #include "base/threading/non_thread_safe.h" 11 #include "components/browser_context_keyed_service/browser_context_keyed_service_export.h" 12 #include "components/browser_context_keyed_service/dependency_node.h" 13 14 class BrowserContextDependencyManager; 15 class PrefService; 16 17 namespace content { 18 class BrowserContext; 19 } 20 21 namespace user_prefs { 22 class PrefRegistrySyncable; 23 } 24 25 // Base class for Factories that take a BrowserContext object and return some 26 // service. 27 // 28 // Unless you're trying to make a new type of Factory, you probably don't want 29 // this class, but its subclasses: BrowserContextKeyedServiceFactory and 30 // RefcountedBrowserContextKeyedServiceFactory. This object describes general 31 // dependency management between Factories; subclasses react to lifecycle 32 // events and implement memory management. 33 class BROWSER_CONTEXT_KEYED_SERVICE_EXPORT 34 BrowserContextKeyedBaseFactory 35 : public base::NonThreadSafe, 36 NON_EXPORTED_BASE(public DependencyNode) { 37 public: 38 // Registers preferences used in this service on the pref service of 39 // |context|. This is the public interface and is safe to be called multiple 40 // times because testing code can have multiple services of the same type 41 // attached to a single |context|. 42 void RegisterUserPrefsOnBrowserContext(content::BrowserContext* context); 43 44 #ifndef NDEBUG 45 // Returns our name. We don't keep track of this in release mode. 46 const char* name() const { return service_name_; } 47 #endif 48 49 protected: 50 BrowserContextKeyedBaseFactory(const char* name, 51 BrowserContextDependencyManager* manager); 52 virtual ~BrowserContextKeyedBaseFactory(); 53 54 // The main public interface for declaring dependencies between services 55 // created by factories. 56 void DependsOn(BrowserContextKeyedBaseFactory* rhs); 57 58 // Interface for people building a concrete FooServiceFactory: -------------- 59 60 // Finds which browser context (if any) to use. 61 virtual content::BrowserContext* GetBrowserContextToUse( 62 content::BrowserContext* context) const; 63 64 // Register any user preferences on this service. This is called during 65 // CreateBrowserContextService() since preferences are registered on a per 66 // BrowserContext basis. 67 virtual void RegisterProfilePrefs( 68 user_prefs::PrefRegistrySyncable* registry) {} 69 70 // By default, we create instances of a service lazily and wait until 71 // GetForBrowserContext() is called on our subclass. Some services need to be 72 // created as soon as the BrowserContext has been brought up. 73 virtual bool ServiceIsCreatedWithBrowserContext() const; 74 75 // By default, TestingBrowserContexts will be treated like normal contexts. 76 // You can override this so that by default, the service associated with the 77 // TestingBrowserContext is NULL. (This is just a shortcut around 78 // SetTestingFactory() to make sure our contexts don't directly refer to the 79 // services they use.) 80 virtual bool ServiceIsNULLWhileTesting() const; 81 82 // Interface for people building a type of BrowserContextKeyedFactory: ------- 83 84 // A helper object actually listens for notifications about BrowserContext 85 // destruction, calculates the order in which things are destroyed and then 86 // does a two pass shutdown. 87 // 88 // It is up to the individual factory types to determine what this two pass 89 // shutdown means. The general framework guarantees the following: 90 // 91 // - Each BrowserContextShutdown() is called in dependency order (and you may 92 // reach out to other services during this phase). 93 // 94 // - Each BrowserContextDestroyed() is called in dependency order. We will 95 // NOTREACHED() if you attempt to GetForBrowserContext() any other service. 96 // You should delete/deref/do other final memory management things during 97 // this phase. You must also call the base class method as the last thing 98 // you do. 99 virtual void BrowserContextShutdown(content::BrowserContext* context) = 0; 100 virtual void BrowserContextDestroyed(content::BrowserContext* context); 101 102 // Returns whether we've registered the preferences on this context. 103 bool ArePreferencesSetOn(content::BrowserContext* context) const; 104 105 // Mark context as Preferences set. 106 void MarkPreferencesSetOn(content::BrowserContext* context); 107 108 private: 109 friend class BrowserContextDependencyManager; 110 friend class BrowserContextDependencyManagerUnittests; 111 112 // These two methods are for tight integration with the 113 // BrowserContextDependencyManager. 114 115 // Because of ServiceIsNULLWhileTesting(), we need a way to tell different 116 // subclasses that they should disable testing. 117 virtual void SetEmptyTestingFactory(content::BrowserContext* context) = 0; 118 119 // We also need a generalized, non-returning method that generates the object 120 // now for when we're creating the context. 121 virtual void CreateServiceNow(content::BrowserContext* context) = 0; 122 123 // Which BrowserContextDependencyManager we should communicate with. 124 // In real code, this will always be 125 // BrowserContextDependencyManager::GetInstance(), but unit tests will want 126 // to use their own copy. 127 BrowserContextDependencyManager* dependency_manager_; 128 129 // BrowserContexts that have this service's preferences registered on them. 130 std::set<content::BrowserContext*> registered_preferences_; 131 132 #if !defined(NDEBUG) 133 // A static string passed in to our constructor. Should be unique across all 134 // services. This is used only for debugging in debug mode. (We can print 135 // pretty graphs with GraphViz with this information.) 136 const char* service_name_; 137 #endif 138 }; 139 140 #endif // COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_KEYED_BASE_FACTORY_H_ 141