simple_keyed_service_factory.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "components/keyed_service/core/simple_keyed_service_factory.h"
  5. #include "base/bind.h"
  6. #include "base/check_op.h"
  7. #include "base/memory/ptr_util.h"
  8. #include "components/keyed_service/core/keyed_service.h"
  9. #include "components/keyed_service/core/simple_dependency_manager.h"
  10. #include "components/keyed_service/core/simple_factory_key.h"
  11. void SimpleKeyedServiceFactory::SetTestingFactory(
  12. SimpleFactoryKey* key,
  13. TestingFactory testing_factory) {
  14. KeyedServiceFactory::TestingFactory wrapped_factory;
  15. if (testing_factory) {
  16. wrapped_factory = base::BindRepeating(
  17. [](const TestingFactory& testing_factory, void* context) {
  18. return testing_factory.Run(static_cast<SimpleFactoryKey*>(context));
  19. },
  20. std::move(testing_factory));
  21. }
  22. KeyedServiceFactory::SetTestingFactory(key, std::move(wrapped_factory));
  23. }
  24. KeyedService* SimpleKeyedServiceFactory::SetTestingFactoryAndUse(
  25. SimpleFactoryKey* key,
  26. TestingFactory testing_factory) {
  27. DCHECK(testing_factory);
  28. return KeyedServiceFactory::SetTestingFactoryAndUse(
  29. key,
  30. base::BindRepeating(
  31. [](const TestingFactory& testing_factory, void* context) {
  32. return testing_factory.Run(static_cast<SimpleFactoryKey*>(context));
  33. },
  34. std::move(testing_factory)));
  35. }
  36. SimpleKeyedServiceFactory::SimpleKeyedServiceFactory(
  37. const char* name,
  38. SimpleDependencyManager* manager)
  39. : KeyedServiceFactory(name, manager, SIMPLE) {}
  40. SimpleKeyedServiceFactory::~SimpleKeyedServiceFactory() {}
  41. KeyedService* SimpleKeyedServiceFactory::GetServiceForKey(SimpleFactoryKey* key,
  42. bool create) {
  43. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  44. return KeyedServiceFactory::GetServiceForContext(key, create);
  45. }
  46. SimpleFactoryKey* SimpleKeyedServiceFactory::GetKeyToUse(
  47. SimpleFactoryKey* key) const {
  48. // Safe default for Incognito mode: no service.
  49. if (key->IsOffTheRecord())
  50. return nullptr;
  51. return key;
  52. }
  53. void SimpleKeyedServiceFactory::SimpleContextShutdown(SimpleFactoryKey* key) {
  54. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  55. KeyedServiceFactory::ContextShutdown(key);
  56. }
  57. void SimpleKeyedServiceFactory::SimpleContextDestroyed(SimpleFactoryKey* key) {
  58. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  59. KeyedServiceFactory::ContextDestroyed(key);
  60. }
  61. std::unique_ptr<KeyedService>
  62. SimpleKeyedServiceFactory::BuildServiceInstanceFor(void* context) const {
  63. return BuildServiceInstanceFor(static_cast<SimpleFactoryKey*>(context));
  64. }
  65. bool SimpleKeyedServiceFactory::IsOffTheRecord(void* context) const {
  66. return static_cast<SimpleFactoryKey*>(context)->IsOffTheRecord();
  67. }
  68. void* SimpleKeyedServiceFactory::GetContextToUse(void* context) const {
  69. AssertContextWasntDestroyed(context);
  70. return GetKeyToUse(static_cast<SimpleFactoryKey*>(context));
  71. }
  72. bool SimpleKeyedServiceFactory::ServiceIsCreatedWithContext() const {
  73. return false;
  74. }
  75. void SimpleKeyedServiceFactory::ContextShutdown(void* context) {
  76. SimpleContextShutdown(static_cast<SimpleFactoryKey*>(context));
  77. }
  78. void SimpleKeyedServiceFactory::ContextDestroyed(void* context) {
  79. SimpleContextDestroyed(static_cast<SimpleFactoryKey*>(context));
  80. }
  81. void SimpleKeyedServiceFactory::RegisterPrefs(
  82. user_prefs::PrefRegistrySyncable* registry) {
  83. RegisterProfilePrefs(registry);
  84. }
  85. void SimpleKeyedServiceFactory::SetEmptyTestingFactory(void* context) {}
  86. bool SimpleKeyedServiceFactory::HasTestingFactory(void* context) {
  87. return false;
  88. }
  89. void SimpleKeyedServiceFactory::CreateServiceNow(void* context) {}