/components/keyed_service/ios/browser_state_keyed_service_factory.h

https://github.com/chromium/chromium · C Header · 136 lines · 52 code · 22 blank · 62 comment · 0 complexity · b6fc2cb13e3973ab87b254a5c05d8823 MD5 · raw file

  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. #ifndef COMPONENTS_KEYED_SERVICE_IOS_BROWSER_STATE_KEYED_SERVICE_FACTORY_H_
  5. #define COMPONENTS_KEYED_SERVICE_IOS_BROWSER_STATE_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 BrowserStateDependencyManager;
  11. class KeyedService;
  12. namespace web {
  13. class BrowserState;
  14. }
  15. // Base class for Factories that take a BrowserState object and return some
  16. // service on a one-to-one mapping. Each factory that derives from this class
  17. // *must* be a Singleton (only unit tests don't do that).
  18. //
  19. // We do this because services depend on each other and we need to control
  20. // shutdown/destruction order. In each derived classes' constructors, the
  21. // implementors must explicitly state which services are depended on.
  22. class KEYED_SERVICE_EXPORT BrowserStateKeyedServiceFactory
  23. : public KeyedServiceFactory {
  24. public:
  25. // A callback that supplies the instance of a KeyedService for a given
  26. // BrowserState. This is used primarily for testing, where we want to feed
  27. // a specific test double into the BSKSF system.
  28. using TestingFactory = base::RepeatingCallback<std::unique_ptr<KeyedService>(
  29. web::BrowserState* context)>;
  30. BrowserStateKeyedServiceFactory(const BrowserStateKeyedServiceFactory&) =
  31. delete;
  32. BrowserStateKeyedServiceFactory& operator=(
  33. const BrowserStateKeyedServiceFactory&) = delete;
  34. // Associates |testing_factory| with |context| so that |testing_factory| is
  35. // used to create the KeyedService when requested. |testing_factory| can be
  36. // empty to signal that KeyedService should be null. Multiple calls to
  37. // SetTestingFactory() are allowed; previous services will be shut down.
  38. void SetTestingFactory(web::BrowserState* context,
  39. TestingFactory testing_factory);
  40. // Associates |testing_factory| with |context| and immediately returns the
  41. // created KeyedService. Since the factory will be used immediately, it may
  42. // not be empty.
  43. KeyedService* SetTestingFactoryAndUse(web::BrowserState* context,
  44. TestingFactory testing_factory);
  45. protected:
  46. // BrowserStateKeyedServiceFactories must communicate with a
  47. // BrowserStateDependencyManager. For all non-test code, write your subclass
  48. // constructors like this:
  49. //
  50. // MyServiceFactory::MyServiceFactory()
  51. // : BrowserStateKeyedServiceFactory(
  52. // "MyService",
  53. // BrowserStateDependencyManager::GetInstance()) {
  54. // }
  55. BrowserStateKeyedServiceFactory(const char* name,
  56. BrowserStateDependencyManager* manager);
  57. ~BrowserStateKeyedServiceFactory() override;
  58. // Common implementation that maps |context| to some service object. Deals
  59. // with incognito and testing contexts per subclass instructions. If |create|
  60. // is true, the service will be created using BuildServiceInstanceFor() if it
  61. // doesn't already exist.
  62. KeyedService* GetServiceForBrowserState(web::BrowserState* context,
  63. bool create);
  64. // Interface for people building a concrete FooServiceFactory: --------------
  65. // Finds which browser state (if any) to use.
  66. virtual web::BrowserState* GetBrowserStateToUse(
  67. web::BrowserState* context) const;
  68. // By default, we create instances of a service lazily and wait until
  69. // GetForBrowserState() is called on our subclass. Some services need to be
  70. // created as soon as the BrowserState has been brought up.
  71. virtual bool ServiceIsCreatedWithBrowserState() const;
  72. // By default, TestingBrowserStates will be treated like normal contexts.
  73. // You can override this so that by default, the service associated with the
  74. // TestingBrowserState is NULL. (This is just a shortcut around
  75. // SetTestingFactory() to make sure our contexts don't directly refer to the
  76. // services they use.)
  77. bool ServiceIsNULLWhileTesting() const override;
  78. // Interface for people building a type of BrowserStateKeyedFactory: -------
  79. // All subclasses of BrowserStateKeyedServiceFactory must return a
  80. // KeyedService instead of just a BrowserStateKeyedBase.
  81. virtual std::unique_ptr<KeyedService> BuildServiceInstanceFor(
  82. web::BrowserState* context) const = 0;
  83. // A helper object actually listens for notifications about BrowserState
  84. // destruction, calculates the order in which things are destroyed and then
  85. // does a two pass shutdown.
  86. //
  87. // First, BrowserStateShutdown() is called on every ServiceFactory and will
  88. // usually call KeyedService::Shutdown(), which gives each
  89. // KeyedService a chance to remove dependencies on other
  90. // services that it may be holding.
  91. //
  92. // Secondly, BrowserStateDestroyed() is called on every ServiceFactory
  93. // and the default implementation removes it from |mapping_| and deletes
  94. // the pointer.
  95. virtual void BrowserStateShutdown(web::BrowserState* context);
  96. virtual void BrowserStateDestroyed(web::BrowserState* context);
  97. private:
  98. // Registers any user preferences on this service. This should be overriden by
  99. // any service that wants to register profile-specific preferences.
  100. virtual void RegisterBrowserStatePrefs(
  101. user_prefs::PrefRegistrySyncable* registry) {}
  102. // KeyedServiceFactory:
  103. std::unique_ptr<KeyedService> BuildServiceInstanceFor(
  104. void* context) const final;
  105. bool IsOffTheRecord(void* context) const final;
  106. // KeyedServiceBaseFactory:
  107. void* GetContextToUse(void* context) const final;
  108. bool ServiceIsCreatedWithContext() const final;
  109. void ContextShutdown(void* context) final;
  110. void ContextDestroyed(void* context) final;
  111. void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry) final;
  112. void CreateServiceNow(void* context) final;
  113. };
  114. #endif // COMPONENTS_KEYED_SERVICE_IOS_BROWSER_STATE_KEYED_SERVICE_FACTORY_H_