simple_keyed_service_factory.h 6.6 KB

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