1 // Copyright 2019 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_SIMPLE_KEYED_SERVICE_FACTORY_H_ 6 #define COMPONENTS_KEYED_SERVICE_CORE_SIMPLE_KEYED_SERVICE_FACTORY_H_ 7 8 #include <memory> 9 10 #include "base/compiler_specific.h" 11 #include "base/macros.h" 12 #include "components/keyed_service/core/keyed_service_export.h" 13 #include "components/keyed_service/core/keyed_service_factory.h" 14 15 class KeyedService; 16 class SimpleDependencyManager; 17 class SimpleFactoryKey; 18 19 // Base class for Factories that take a SimpleFactoryKey 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 on which services they depend. 26 // 27 // Note: 28 // The SimpleKeyedServiceFactory (SKSF) provides a way to create or get a 29 // SimpleKeyedService before BrowserContext is created. 30 // BrowserContextKeyedServiceFactories (BCKSFs) can only be converted to 31 // SKSFs as long as they access only part of Profile properties: 32 // path, PrefService, and is_off_the_record flag. 33 // 34 // An SKSF shouldn't declare DependsOn() of any BCKSF on creation (constructor). 35 // It is because an SKSF can't depend on any BCKSF when it is created. However, 36 // dependencies from SKSFs to BCKSFs may exist after full browser launches, 37 // since some SimpleKeyedServices move from "reduced mode" to "full browser 38 // mode" when the full browser starts up, which involves injection of 39 // BrowserContextKeyedService dependencies into the SimpleKeyedService. 40 // 41 // If such dependencies exist in a SimpleKeyedService, the service **MUST** 42 // explicitly reset/clean up the dependencies in KeyedService::Shutdown(). 43 // 44 // Once the dependencies are reset, the dependencies from the BCKSF dependency 45 // graph to the SKSF dependency graph are removed. Therefore, we adopt a 46 // two-phase shutdown: 47 // - Shutdown of all BCKSFactories 48 // - Shutdown of all SKSFactories 49 // - Destruction of all BCKSFactories 50 // - Destruction of all SKSFactories 51 52 // A SimpleKeyedService should *AVOID* full browser inflation whenever it is 53 // possible. A solution might be splitting the part of the service that 54 // depends on BrowserContextKeyedService or BrowserContext into a separate 55 // BrowserContextKeyedService. 56 // 57 // See 58 // https://docs.google.com/document/d/1caWonaPnBhMb6sk4syNe0BbdsQih13S6QmDW237Mcrg/edit?usp=sharing 59 // for more details. 60 class KEYED_SERVICE_EXPORT SimpleKeyedServiceFactory 61 : public KeyedServiceFactory { 62 public: 63 // A callback that supplies the instance of a KeyedService for a given 64 // SimpleFactoryKey. This is used primarily for testing, where we want to feed 65 // a specific test double into the SKSF system. 66 using TestingFactory = base::RepeatingCallback<std::unique_ptr<KeyedService>( 67 SimpleFactoryKey* key)>; 68 69 // Associates |testing_factory| with |key| so that |testing_factory| is 70 // used to create the KeyedService when requested. |testing_factory| can be 71 // empty to signal that KeyedService should be null. Multiple calls to 72 // SetTestingFactory() are allowed; previous services will be shut down. 73 void SetTestingFactory(SimpleFactoryKey* key, TestingFactory testing_factory); 74 75 // Associates |testing_factory| with |key| and immediately returns the 76 // created KeyedService. Since the factory will be used immediately, it may 77 // not be empty. 78 KeyedService* SetTestingFactoryAndUse(SimpleFactoryKey* key, 79 TestingFactory testing_factory); 80 81 protected: 82 SimpleKeyedServiceFactory(const char* name, SimpleDependencyManager* manager); 83 ~SimpleKeyedServiceFactory() override; 84 85 // Common implementation that maps |key| to some service object. Deals 86 // with incognito contexts per subclass instructions with 87 // GetBrowserContextRedirectedInIncognito() and 88 // GetBrowserContextOwnInstanceInIncognito() through the 89 // GetBrowserContextToUse() method on the base. If |create| is true, the 90 // service will be created using BuildServiceInstanceFor() if it doesn't 91 // already exist. 92 KeyedService* GetServiceForKey(SimpleFactoryKey* key, 93 bool create); 94 95 // Interface for people building a concrete FooServiceFactory: -------------- 96 97 // Finds which SimpleFactoryKey (if any) to use. 98 virtual SimpleFactoryKey* GetKeyToUse(SimpleFactoryKey* key) const; 99 100 // Interface for people building a type of SimpleKeyedFactory: ------- 101 102 // All subclasses of SimpleKeyedServiceFactory must return a 103 // KeyedService. 104 virtual std::unique_ptr<KeyedService> BuildServiceInstanceFor( 105 SimpleFactoryKey* key) const = 0; 106 107 // A helper object actually listens for notifications about BrowserContext 108 // destruction, calculates the order in which things are destroyed and then 109 // does a two pass shutdown. 110 // 111 // First, SimpleContextShutdown() is called on every ServiceFactory and will 112 // usually call KeyedService::Shutdown(), which gives each 113 // KeyedService a chance to remove dependencies on other 114 // services that it may be holding. 115 // 116 // Secondly, SimpleContextDestroyed() is called on every ServiceFactory 117 // and the default implementation removes it from |mapping_| and deletes 118 // the pointer. 119 virtual void SimpleContextShutdown(SimpleFactoryKey* key); 120 virtual void SimpleContextDestroyed(SimpleFactoryKey* key); 121 122 private: 123 // Registers any user preferences on this service. This is called by 124 // RegisterPrefsIfNecessaryForContext() and should be overriden by any service 125 // that wants to register profile-specific preferences. RegisterProfilePrefs(user_prefs::PrefRegistrySyncable * registry)126 virtual void RegisterProfilePrefs( 127 user_prefs::PrefRegistrySyncable* registry) {} 128 129 // KeyedServiceFactory: 130 std::unique_ptr<KeyedService> BuildServiceInstanceFor( 131 void* context) const final; 132 bool IsOffTheRecord(void* context) const final; 133 134 // KeyedServiceBaseFactory: 135 void* GetContextToUse(void* context) const final; 136 bool ServiceIsCreatedWithContext() const final; 137 void ContextShutdown(void* context) final; 138 void ContextDestroyed(void* context) final; 139 void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry) final; 140 void SetEmptyTestingFactory(void* context) final; 141 bool HasTestingFactory(void* context) final; 142 void CreateServiceNow(void* context) final; 143 144 DISALLOW_COPY_AND_ASSIGN(SimpleKeyedServiceFactory); 145 }; 146 147 #endif // COMPONENTS_KEYED_SERVICE_CORE_SIMPLE_KEYED_SERVICE_FACTORY_H_ 148