/components/keyed_service/content/browser_context_keyed_service_factory.cc

https://github.com/chromium/chromium · C++ · 127 lines · 99 code · 22 blank · 6 comment · 2 complexity · f0637690dca8ec4da0f67e2992a4831d 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. #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/check_op.h"
  8. #include "base/memory/ptr_util.h"
  9. #include "components/keyed_service/content/browser_context_dependency_manager.h"
  10. #include "components/keyed_service/core/keyed_service.h"
  11. #include "components/pref_registry/pref_registry_syncable.h"
  12. #include "content/public/browser/browser_context.h"
  13. void BrowserContextKeyedServiceFactory::SetTestingFactory(
  14. content::BrowserContext* context,
  15. TestingFactory testing_factory) {
  16. KeyedServiceFactory::TestingFactory wrapped_factory;
  17. if (testing_factory) {
  18. wrapped_factory = base::BindRepeating(
  19. [](const TestingFactory& testing_factory, void* context) {
  20. return testing_factory.Run(
  21. static_cast<content::BrowserContext*>(context));
  22. },
  23. std::move(testing_factory));
  24. }
  25. KeyedServiceFactory::SetTestingFactory(context, std::move(wrapped_factory));
  26. }
  27. KeyedService* BrowserContextKeyedServiceFactory::SetTestingFactoryAndUse(
  28. content::BrowserContext* context,
  29. TestingFactory testing_factory) {
  30. DCHECK(testing_factory);
  31. return KeyedServiceFactory::SetTestingFactoryAndUse(
  32. context,
  33. base::BindRepeating(
  34. [](const TestingFactory& testing_factory, void* context) {
  35. return testing_factory.Run(
  36. static_cast<content::BrowserContext*>(context));
  37. },
  38. std::move(testing_factory)));
  39. }
  40. BrowserContextKeyedServiceFactory::BrowserContextKeyedServiceFactory(
  41. const char* name,
  42. BrowserContextDependencyManager* manager)
  43. : KeyedServiceFactory(name, manager, BROWSER_CONTEXT) {}
  44. BrowserContextKeyedServiceFactory::~BrowserContextKeyedServiceFactory() {
  45. }
  46. KeyedService* BrowserContextKeyedServiceFactory::GetServiceForBrowserContext(
  47. content::BrowserContext* context,
  48. bool create) {
  49. return KeyedServiceFactory::GetServiceForContext(context, create);
  50. }
  51. content::BrowserContext*
  52. BrowserContextKeyedServiceFactory::GetBrowserContextToUse(
  53. content::BrowserContext* context) const {
  54. // Safe default for Incognito mode: no service.
  55. if (context->IsOffTheRecord())
  56. return nullptr;
  57. return context;
  58. }
  59. bool BrowserContextKeyedServiceFactory::ServiceIsCreatedWithBrowserContext()
  60. const {
  61. return KeyedServiceBaseFactory::ServiceIsCreatedWithContext();
  62. }
  63. bool BrowserContextKeyedServiceFactory::ServiceIsNULLWhileTesting() const {
  64. return KeyedServiceBaseFactory::ServiceIsNULLWhileTesting();
  65. }
  66. void BrowserContextKeyedServiceFactory::BrowserContextShutdown(
  67. content::BrowserContext* context) {
  68. KeyedServiceFactory::ContextShutdown(context);
  69. }
  70. void BrowserContextKeyedServiceFactory::BrowserContextDestroyed(
  71. content::BrowserContext* context) {
  72. KeyedServiceFactory::ContextDestroyed(context);
  73. }
  74. std::unique_ptr<KeyedService>
  75. BrowserContextKeyedServiceFactory::BuildServiceInstanceFor(
  76. void* context) const {
  77. // TODO(isherman): The wrapped BuildServiceInstanceFor() should return a
  78. // scoped_ptr as well.
  79. return base::WrapUnique(
  80. BuildServiceInstanceFor(static_cast<content::BrowserContext*>(context)));
  81. }
  82. bool BrowserContextKeyedServiceFactory::IsOffTheRecord(void* context) const {
  83. return static_cast<content::BrowserContext*>(context)->IsOffTheRecord();
  84. }
  85. void* BrowserContextKeyedServiceFactory::GetContextToUse(void* context) const {
  86. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  87. AssertContextWasntDestroyed(context);
  88. return GetBrowserContextToUse(static_cast<content::BrowserContext*>(context));
  89. }
  90. bool BrowserContextKeyedServiceFactory::ServiceIsCreatedWithContext() const {
  91. return ServiceIsCreatedWithBrowserContext();
  92. }
  93. void BrowserContextKeyedServiceFactory::ContextShutdown(void* context) {
  94. BrowserContextShutdown(static_cast<content::BrowserContext*>(context));
  95. }
  96. void BrowserContextKeyedServiceFactory::ContextDestroyed(void* context) {
  97. BrowserContextDestroyed(static_cast<content::BrowserContext*>(context));
  98. }
  99. void BrowserContextKeyedServiceFactory::RegisterPrefs(
  100. user_prefs::PrefRegistrySyncable* registry) {
  101. RegisterProfilePrefs(registry);
  102. }
  103. void BrowserContextKeyedServiceFactory::CreateServiceNow(void* context) {
  104. KeyedServiceFactory::GetServiceForContext(context, true);
  105. }