refcounted_keyed_service_factory.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_REFCOUNTED_KEYED_SERVICE_FACTORY_H_
  5. #define COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_FACTORY_H_
  6. #include <map>
  7. #include "base/callback.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "components/keyed_service/core/keyed_service_base_factory.h"
  11. #include "components/keyed_service/core/keyed_service_export.h"
  12. class RefcountedKeyedService;
  13. // A specialized KeyedServiceBaseFactory that manages a RefcountedThreadSafe<>.
  14. //
  15. // While the factory returns RefcountedThreadSafe<>s, the factory itself is a
  16. // base::NotThreadSafe. Only call methods on this object on the UI thread.
  17. //
  18. // Implementers of RefcountedKeyedService should note that we guarantee that
  19. // ShutdownOnUIThread() is called on the UI thread, but actual object
  20. // destruction can happen anywhere.
  21. class KEYED_SERVICE_EXPORT RefcountedKeyedServiceFactory
  22. : public KeyedServiceBaseFactory {
  23. public:
  24. RefcountedKeyedServiceFactory(const RefcountedKeyedServiceFactory&) = delete;
  25. RefcountedKeyedServiceFactory& operator=(
  26. const RefcountedKeyedServiceFactory&) = delete;
  27. // Returns the number of RefCountedKeyedServices that are currently active for
  28. // a given context.
  29. static int GetServicesCount(void* context);
  30. protected:
  31. RefcountedKeyedServiceFactory(const char* name,
  32. DependencyManager* manager,
  33. Type type);
  34. ~RefcountedKeyedServiceFactory() override;
  35. // A callback that supplies the instance of a KeyedService for a given
  36. // |context|. This is used primarily for testing, where we want to feed
  37. // a specific test double into the KeyedServiceFactory system.
  38. using TestingFactory =
  39. base::RepeatingCallback<scoped_refptr<RefcountedKeyedService>(
  40. void* context)>;
  41. // Associates |testing_factory| with |context| so that |testing_factory| is
  42. // used to create the KeyedService when requested. |testing_factory| can be
  43. // empty to signal that KeyedService should be null. Multiple calls to
  44. // SetTestingFactory() are allowed; previous services will be shut down.
  45. void SetTestingFactory(void* context, TestingFactory testing_factory);
  46. // Associates |testing_factory| with |context| and immediately returns the
  47. // created KeyedService. Since the factory will be used immediately, it may
  48. // not be empty.
  49. scoped_refptr<RefcountedKeyedService> SetTestingFactoryAndUse(
  50. void* context,
  51. TestingFactory testing_factory);
  52. // Common implementation that maps |context| to some service object. Deals
  53. // with incognito contexts per subclass instructions with GetContextToUse()
  54. // method on the base. If |create| is true, the service will be created
  55. // using BuildServiceInstanceFor() if it doesn't already exist.
  56. scoped_refptr<RefcountedKeyedService> GetServiceForContext(void* context,
  57. bool create);
  58. // Maps |context| to |service| with debug checks to prevent duplication and
  59. // returns |service|.
  60. scoped_refptr<RefcountedKeyedService> Associate(
  61. void* context,
  62. scoped_refptr<RefcountedKeyedService> service);
  63. // Removes the mapping from |context| to a service.
  64. void Disassociate(void* context);
  65. // Returns a new RefcountedKeyedService that will be associated with
  66. // |context|.
  67. virtual scoped_refptr<RefcountedKeyedService> BuildServiceInstanceFor(
  68. void* context) const = 0;
  69. // Returns whether the |context| is off-the-record or not.
  70. virtual bool IsOffTheRecord(void* context) const = 0;
  71. // KeyedServiceBaseFactory:
  72. void ContextShutdown(void* context) override;
  73. void ContextDestroyed(void* context) override;
  74. void SetEmptyTestingFactory(void* context) override;
  75. bool HasTestingFactory(void* context) override;
  76. void CreateServiceNow(void* context) override;
  77. bool IsServiceCreated(void* context) const override;
  78. private:
  79. // The mapping between a context and its refcounted service.
  80. std::map<void*, scoped_refptr<RefcountedKeyedService>> mapping_;
  81. // The mapping between a context and its overridden TestingFactory.
  82. std::map<void*, TestingFactory> testing_factories_;
  83. };
  84. #endif // COMPONENTS_KEYED_SERVICE_CORE_REFCOUNTED_KEYED_SERVICE_FACTORY_H_