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_CORE_KEYED_SERVICE_BASE_FACTORY_H_ 6 #define COMPONENTS_KEYED_SERVICE_CORE_KEYED_SERVICE_BASE_FACTORY_H_ 7 8 #include <set> 9 10 #include "base/sequence_checker.h" 11 #include "components/keyed_service/core/dependency_node.h" 12 #include "components/keyed_service/core/keyed_service_export.h" 13 14 class DependencyManager; 15 16 namespace user_prefs { 17 class PrefRegistrySyncable; 18 } 19 20 // Base class for factories that take an opaque pointer and return some service. 21 // Not for direct usage, instead use descendent classes that deal with more 22 // specific context objects. 23 // 24 // This object describes general dependency management between factories while 25 // direct subclasses react to lifecycle events and implement memory management. 26 class KEYED_SERVICE_EXPORT KeyedServiceBaseFactory : public DependencyNode { 27 public: 28 // The type is used to determine whether a service can depend on another. 29 // Each type can only depend on other services that are of the same type. 30 // TODO(crbug.com/944906): Remove once there are no dependencies between 31 // factories with different type of context, or dependencies are safe to have. 32 enum Type { BROWSER_CONTEXT, BROWSER_STATE, SIMPLE }; 33 34 // Returns our name. name()35 const char* name() const { return service_name_; } 36 37 // Returns the type of this service factory. 38 // TODO(crbug.com/944906): Remove once there are no dependencies between 39 // factories with different type of context, or dependencies are safe to have. type()40 Type type() { return type_; } 41 42 protected: 43 KeyedServiceBaseFactory(const char* service_name, 44 DependencyManager* manager, 45 Type type); 46 virtual ~KeyedServiceBaseFactory(); 47 48 // The main public interface for declaring dependencies between services 49 // created by factories. 50 void DependsOn(KeyedServiceBaseFactory* rhs); 51 52 // Runtime assertion to check if |context| is considered stale. Should be used 53 // by subclasses when accessing |context|. 54 void AssertContextWasntDestroyed(void* context) const; 55 56 // Marks |context| as live (i.e., not stale). This method can be called as a 57 // safeguard against |AssertContextWasntDestroyed()| checks going off due to 58 // |context| aliasing an instance from a prior construction (i.e., 0xWhatever 59 // might be created, be destroyed, and then a new object might be created at 60 // 0xWhatever). 61 void MarkContextLive(void* context); 62 63 // Finds which context (if any) to use. 64 virtual void* GetContextToUse(void* context) const = 0; 65 66 // By default, instance of a service are created lazily when GetForContext() 67 // is called by the subclass. Some services need to be created as soon as the 68 // context is created and should override this method to return true. 69 virtual bool ServiceIsCreatedWithContext() const; 70 71 // By default, testing contexts will be treated like normal contexts. If this 72 // method is overriden to return true, then the service associated with the 73 // testing context will be null. 74 virtual bool ServiceIsNULLWhileTesting() const; 75 76 // The service build by the factories goes through a two phase shutdown. 77 // It is up to the individual factory types to determine what this two pass 78 // shutdown means. The general framework guarantees the following: 79 // 80 // - Each ContextShutdown() is called in dependency order (and you may 81 // reach out to other services during this phase). 82 // 83 // - Each ContextDestroyed() is called in dependency order. Accessing a 84 // service with GetForContext() will NOTREACHED() and code should delete/ 85 // deref/do other final memory management during this phase. The base class 86 // method *must* be called as the last thing. 87 virtual void ContextShutdown(void* context) = 0; 88 virtual void ContextDestroyed(void* context); 89 90 SEQUENCE_CHECKER(sequence_checker_); 91 92 private: 93 friend class DependencyManager; 94 95 // The DependencyManager used. In real code, this will be a singleton used 96 // by all the factories of a given type. Unit tests will use their own copy. 97 DependencyManager* dependency_manager_; 98 99 // Registers any preferences used by this service. This should be overriden by 100 // any services that want to register context-specific preferences. RegisterPrefs(user_prefs::PrefRegistrySyncable * registry)101 virtual void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry) {} 102 103 // Used by DependencyManager to disable creation of the service when the 104 // method ServiceIsNULLWhileTesting() returns true. 105 virtual void SetEmptyTestingFactory(void* context) = 0; 106 107 // Returns true if a testing factory function has been set for |context|. 108 virtual bool HasTestingFactory(void* context) = 0; 109 110 // Create the service associated with |context|. 111 virtual void CreateServiceNow(void* context) = 0; 112 113 // A static string passed in to the constructor. Should be unique across all 114 // services. 115 const char* service_name_; 116 117 // The type of this service. 118 // TODO(crbug.com/944906): Remove once there are no dependencies between 119 // factories with different type of context, or dependencies are safe to have. 120 Type type_; 121 122 DISALLOW_COPY_AND_ASSIGN(KeyedServiceBaseFactory); 123 }; 124 125 #endif // COMPONENTS_KEYED_SERVICE_CORE_KEYED_SERVICE_BASE_FACTORY_H_ 126