keyed_service_base_factory.h 5.9 KB

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