keyed_service_factory.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_FACTORY_H_
  5. #define COMPONENTS_KEYED_SERVICE_CORE_KEYED_SERVICE_FACTORY_H_
  6. #include <map>
  7. #include <memory>
  8. #include "base/callback_forward.h"
  9. #include "base/compiler_specific.h"
  10. #include "components/keyed_service/core/keyed_service_base_factory.h"
  11. #include "components/keyed_service/core/keyed_service_export.h"
  12. class DependencyManager;
  13. class KeyedService;
  14. // Base class for Factories that take an opaque pointer and return some service
  15. // on a one-to-one mapping. Each concrete factory that derives from this class
  16. // *must* be a Singleton (only unit tests don't do that).
  17. //
  18. // We do this because services depend on each other and we need to control
  19. // shutdown/destruction order. In each derived classes' constructors, the
  20. // implementors must explicitly state which services are depended on.
  21. class KEYED_SERVICE_EXPORT KeyedServiceFactory
  22. : public KeyedServiceBaseFactory {
  23. public:
  24. KeyedServiceFactory(const KeyedServiceFactory&) = delete;
  25. KeyedServiceFactory& operator=(const KeyedServiceFactory&) = delete;
  26. // Returns the number of KeyedServices that are currently active for a given
  27. // context.
  28. static int GetServicesCount(void* context);
  29. protected:
  30. KeyedServiceFactory(const char* name, DependencyManager* manager, Type type);
  31. ~KeyedServiceFactory() override;
  32. // A callback that supplies the instance of a KeyedService for a given
  33. // |context|. This is used primarily for testing, where we want to feed
  34. // a specific test double into the KeyedServiceFactory system.
  35. using TestingFactory =
  36. base::RepeatingCallback<std::unique_ptr<KeyedService>(void* context)>;
  37. // Associates |testing_factory| with |context| so that |testing_factory| is
  38. // used to create the KeyedService when requested. |testing_factory| can be
  39. // empty to signal that KeyedService should be null. Multiple calls to
  40. // SetTestingFactory() are allowed; previous services will be shut down.
  41. void SetTestingFactory(void* context, TestingFactory testing_factory);
  42. // Associates |testing_factory| with |context| and immediately returns the
  43. // created KeyedService. Since the factory will be used immediately, it may
  44. // not be empty.
  45. KeyedService* SetTestingFactoryAndUse(void* context,
  46. TestingFactory testing_factory);
  47. // Common implementation that maps |context| to some service object. Deals
  48. // with incognito contexts per subclass instructions with GetContextToUse()
  49. // method on the base. If |create| is true, the service will be created
  50. // using BuildServiceInstanceFor() if it doesn't already exist.
  51. KeyedService* GetServiceForContext(void* context,
  52. bool create);
  53. // Maps |context| to |service| with debug checks to prevent duplication and
  54. // returns a raw pointer to |service|.
  55. KeyedService* Associate(void* context, std::unique_ptr<KeyedService> service);
  56. // Removes the mapping from |context| to a service.
  57. void Disassociate(void* context);
  58. // Returns a new KeyedService that will be associated with |context|. The
  59. // |side_parameter| could be nullptr or some object required to create a
  60. // service instance.
  61. virtual std::unique_ptr<KeyedService> BuildServiceInstanceFor(
  62. void* context) const = 0;
  63. // Returns whether the |context| is off-the-record or not.
  64. virtual bool IsOffTheRecord(void* context) const = 0;
  65. // KeyedServiceBaseFactory:
  66. void ContextShutdown(void* context) override;
  67. void ContextDestroyed(void* context) override;
  68. void SetEmptyTestingFactory(void* context) override;
  69. bool HasTestingFactory(void* context) override;
  70. bool IsServiceCreated(void* context) const override;
  71. private:
  72. friend class DependencyManager;
  73. friend class DependencyManagerUnittests;
  74. // The mapping between a context and its service.
  75. std::map<void*, std::unique_ptr<KeyedService>> mapping_;
  76. // The mapping between a context and its overridden TestingFactory.
  77. std::map<void*, TestingFactory> testing_factories_;
  78. };
  79. #endif // COMPONENTS_KEYED_SERVICE_CORE_KEYED_SERVICE_FACTORY_H_