/chromium/components/browser_context_keyed_service/browser_context_keyed_base_factory.cc

https://gitlab.com/f3822/qtwebengine-chromium · C++ · 120 lines · 72 code · 17 blank · 31 comment · 3 complexity · abdd0f3fe7b81283c9b1d9aa398ef9dd MD5 · raw file

  1. // Copyright (c) 2012 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/browser_context_keyed_service/browser_context_keyed_base_factory.h"
  5. #include "base/prefs/pref_service.h"
  6. #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
  7. #include "components/user_prefs/pref_registry_syncable.h"
  8. #include "components/user_prefs/user_prefs.h"
  9. #include "content/public/browser/browser_context.h"
  10. BrowserContextKeyedBaseFactory::BrowserContextKeyedBaseFactory(
  11. const char* name, BrowserContextDependencyManager* manager)
  12. : dependency_manager_(manager)
  13. #ifndef NDEBUG
  14. , service_name_(name)
  15. #endif
  16. {
  17. dependency_manager_->AddComponent(this);
  18. }
  19. BrowserContextKeyedBaseFactory::~BrowserContextKeyedBaseFactory() {
  20. dependency_manager_->RemoveComponent(this);
  21. }
  22. void BrowserContextKeyedBaseFactory::DependsOn(
  23. BrowserContextKeyedBaseFactory* rhs) {
  24. dependency_manager_->AddEdge(rhs, this);
  25. }
  26. void BrowserContextKeyedBaseFactory::RegisterProfilePrefsIfNecessaryForContext(
  27. const content::BrowserContext* context,
  28. user_prefs::PrefRegistrySyncable* registry) {
  29. std::set<const content::BrowserContext*>::iterator it =
  30. registered_preferences_.find(context);
  31. if (it == registered_preferences_.end()) {
  32. RegisterProfilePrefs(registry);
  33. registered_preferences_.insert(context);
  34. }
  35. }
  36. content::BrowserContext* BrowserContextKeyedBaseFactory::GetBrowserContextToUse(
  37. content::BrowserContext* context) const {
  38. DCHECK(CalledOnValidThread());
  39. #ifndef NDEBUG
  40. dependency_manager_->AssertBrowserContextWasntDestroyed(context);
  41. #endif
  42. // Safe default for the Incognito mode: no service.
  43. if (context->IsOffTheRecord())
  44. return NULL;
  45. return context;
  46. }
  47. void BrowserContextKeyedBaseFactory::RegisterUserPrefsOnBrowserContextForTest(
  48. content::BrowserContext* context) {
  49. // Safe timing for pref registration is hard. Previously, we made
  50. // BrowserContext responsible for all pref registration on every service
  51. // that used BrowserContext. Now we don't and there are timing issues.
  52. //
  53. // With normal contexts, prefs can simply be registered at
  54. // BrowserContextDependencyManager::RegisterProfilePrefsForServices time.
  55. // With incognito contexts, we just never register since incognito contexts
  56. // share the same pref services with their parent contexts.
  57. //
  58. // TestingBrowserContexts throw a wrench into the mix, in that some tests will
  59. // swap out the PrefService after we've registered user prefs on the original
  60. // PrefService. Test code that does this is responsible for either manually
  61. // invoking RegisterProfilePrefs() on the appropriate
  62. // BrowserContextKeyedServiceFactory associated with the prefs they need,
  63. // or they can use SetTestingFactory() and create a service (since service
  64. // creation with a factory method causes registration to happen at
  65. // TestingProfile creation time).
  66. //
  67. // Now that services are responsible for declaring their preferences, we have
  68. // to enforce a uniquenes check here because some tests create one context and
  69. // multiple services of the same type attached to that context (serially, not
  70. // parallel) and we don't want to register multiple times on the same context.
  71. // This is the purpose of RegisterProfilePrefsIfNecessary() which could be
  72. // replaced directly by RegisterProfilePrefs() if this method is ever phased
  73. // out.
  74. PrefService* prefs = user_prefs::UserPrefs::Get(context);
  75. user_prefs::PrefRegistrySyncable* registry =
  76. static_cast<user_prefs::PrefRegistrySyncable*>(
  77. prefs->DeprecatedGetPrefRegistry());
  78. RegisterProfilePrefsIfNecessaryForContext(context, registry);
  79. }
  80. bool
  81. BrowserContextKeyedBaseFactory::ServiceIsCreatedWithBrowserContext() const {
  82. return false;
  83. }
  84. bool BrowserContextKeyedBaseFactory::ServiceIsNULLWhileTesting() const {
  85. return false;
  86. }
  87. void BrowserContextKeyedBaseFactory::BrowserContextDestroyed(
  88. content::BrowserContext* context) {
  89. // While object destruction can be customized in ways where the object is
  90. // only dereferenced, this still must run on the UI thread.
  91. DCHECK(CalledOnValidThread());
  92. registered_preferences_.erase(context);
  93. }
  94. bool BrowserContextKeyedBaseFactory::ArePreferencesSetOn(
  95. content::BrowserContext* context) const {
  96. return registered_preferences_.find(context) !=
  97. registered_preferences_.end();
  98. }
  99. void BrowserContextKeyedBaseFactory::MarkPreferencesSetOn(
  100. content::BrowserContext* context) {
  101. DCHECK(!ArePreferencesSetOn(context));
  102. registered_preferences_.insert(context);
  103. }