/components/keyed_service/core/keyed_service_base_factory.cc

https://gitlab.com/jonnialva90/iridium-browser · C++ · 136 lines · 90 code · 17 blank · 29 comment · 2 complexity · f21ddb255131f6e700e124b205383248 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/core/keyed_service_base_factory.h"
  5. #include "base/prefs/pref_service.h"
  6. #include "base/supports_user_data.h"
  7. #include "base/trace_event/trace_event.h"
  8. #include "components/keyed_service/core/dependency_manager.h"
  9. #include "components/pref_registry/pref_registry_syncable.h"
  10. #include "components/user_prefs/user_prefs.h"
  11. void KeyedServiceBaseFactory::RegisterUserPrefsOnContextForTest(
  12. base::SupportsUserData* context) {
  13. TRACE_EVENT0("browser,startup",
  14. "KeyedServiceBaseFactory::RegisterUserPrefsOnContextForTest");
  15. // Safe timing for pref registration is hard. Previously, we made
  16. // context responsible for all pref registration on every service
  17. // that used contexts. Now we don't and there are timing issues.
  18. //
  19. // With normal contexts, prefs can simply be registered at
  20. // DependencyManager::RegisterProfilePrefsForServices time.
  21. // With incognito contexts, we just never register since incognito
  22. // contexts share the same pref services with their parent contexts.
  23. //
  24. // Testing contexts throw a wrench into the mix, in that some tests will
  25. // swap out the PrefService after we've registered user prefs on the original
  26. // PrefService. Test code that does this is responsible for either manually
  27. // invoking RegisterProfilePrefs() on the appropriate
  28. // ContextKeyedServiceFactory associated with the prefs they need,
  29. // or they can use SetTestingFactory() and create a service (since service
  30. // creation with a factory method causes registration to happen at
  31. // TestingProfile creation time).
  32. //
  33. // Now that services are responsible for declaring their preferences, we have
  34. // to enforce a uniquenes check here because some tests create one context and
  35. // multiple services of the same type attached to that context (serially, not
  36. // parallel) and we don't want to register multiple times on the same context.
  37. // This is the purpose of RegisterPrefsIfNecessaryForContext() which could be
  38. // replaced directly by RegisterPrefs() if this method is ever phased out.
  39. RegisterPrefsIfNecessaryForContext(context,
  40. GetAssociatedPrefRegistry(context));
  41. }
  42. KeyedServiceBaseFactory::KeyedServiceBaseFactory(const char* service_name,
  43. DependencyManager* manager)
  44. : dependency_manager_(manager) {
  45. #ifndef NDEBUG
  46. service_name_ = service_name;
  47. #endif
  48. dependency_manager_->AddComponent(this);
  49. }
  50. KeyedServiceBaseFactory::~KeyedServiceBaseFactory() {
  51. dependency_manager_->RemoveComponent(this);
  52. }
  53. void KeyedServiceBaseFactory::DependsOn(KeyedServiceBaseFactory* rhs) {
  54. dependency_manager_->AddEdge(rhs, this);
  55. }
  56. void KeyedServiceBaseFactory::RegisterPrefsIfNecessaryForContext(
  57. base::SupportsUserData* context,
  58. user_prefs::PrefRegistrySyncable* registry) {
  59. if (!ArePreferencesSetOn(context)) {
  60. RegisterPrefs(registry);
  61. MarkPreferencesSetOn(context);
  62. }
  63. }
  64. user_prefs::PrefRegistrySyncable*
  65. KeyedServiceBaseFactory::GetAssociatedPrefRegistry(
  66. base::SupportsUserData* context) const {
  67. PrefService* prefs =
  68. user_prefs::UserPrefs::Get(GetContextForDependencyManager(context));
  69. user_prefs::PrefRegistrySyncable* registry =
  70. static_cast<user_prefs::PrefRegistrySyncable*>(
  71. prefs->DeprecatedGetPrefRegistry());
  72. return registry;
  73. }
  74. #ifndef NDEBUG
  75. void KeyedServiceBaseFactory::AssertContextWasntDestroyed(
  76. base::SupportsUserData* context) const {
  77. DCHECK(CalledOnValidThread());
  78. context = GetContextForDependencyManager(context);
  79. dependency_manager_->AssertContextWasntDestroyed(context);
  80. }
  81. void KeyedServiceBaseFactory::MarkContextLiveForTesting(
  82. base::SupportsUserData* context) {
  83. DCHECK(CalledOnValidThread());
  84. context = GetContextForDependencyManager(context);
  85. dependency_manager_->MarkContextLiveForTesting(context);
  86. }
  87. #endif
  88. bool KeyedServiceBaseFactory::ServiceIsCreatedWithContext() const {
  89. return false;
  90. }
  91. bool KeyedServiceBaseFactory::ServiceIsNULLWhileTesting() const {
  92. return false;
  93. }
  94. void KeyedServiceBaseFactory::ContextDestroyed(
  95. base::SupportsUserData* context) {
  96. // While object destruction can be customized in ways where the object is
  97. // only dereferenced, this still must run on the UI thread.
  98. DCHECK(CalledOnValidThread());
  99. registered_preferences_.erase(context);
  100. }
  101. bool KeyedServiceBaseFactory::ArePreferencesSetOn(
  102. base::SupportsUserData* context) const {
  103. return registered_preferences_.find(context) != registered_preferences_.end();
  104. }
  105. void KeyedServiceBaseFactory::MarkPreferencesSetOn(
  106. base::SupportsUserData* context) {
  107. DCHECK(!ArePreferencesSetOn(context));
  108. registered_preferences_.insert(context);
  109. }
  110. #if defined(OS_IOS)
  111. base::SupportsUserData* KeyedServiceBaseFactory::GetTypedContext(
  112. base::SupportsUserData* context) const {
  113. return context;
  114. }
  115. base::SupportsUserData* KeyedServiceBaseFactory::GetContextForDependencyManager(
  116. base::SupportsUserData* context) const {
  117. return context;
  118. }
  119. #endif // defined(OS_IOS)