/chromium-webcl/src/chrome/browser/profiles/profile_keyed_base_factory.cc

https://bitbucket.org/peixuan/chromium_r197479_base · C++ · 130 lines · 81 code · 21 blank · 28 comment · 7 complexity · fbf5a9a5512218997b311fbd1fc2d62e 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 "chrome/browser/profiles/profile_keyed_base_factory.h"
  5. #include "base/prefs/pref_service.h"
  6. #include "chrome/browser/profiles/profile.h"
  7. #include "chrome/browser/profiles/profile_dependency_manager.h"
  8. #include "components/user_prefs/pref_registry_syncable.h"
  9. #include "components/user_prefs/user_prefs.h"
  10. ProfileKeyedBaseFactory::ProfileKeyedBaseFactory(
  11. const char* name, ProfileDependencyManager* manager)
  12. : dependency_manager_(manager)
  13. #ifndef NDEBUG
  14. , service_name_(name)
  15. #endif
  16. {
  17. dependency_manager_->AddComponent(this);
  18. }
  19. ProfileKeyedBaseFactory::~ProfileKeyedBaseFactory() {
  20. dependency_manager_->RemoveComponent(this);
  21. }
  22. void ProfileKeyedBaseFactory::DependsOn(ProfileKeyedBaseFactory* rhs) {
  23. dependency_manager_->AddEdge(rhs, this);
  24. }
  25. content::BrowserContext* ProfileKeyedBaseFactory::GetProfileToUse(
  26. content::BrowserContext* context) {
  27. DCHECK(CalledOnValidThread());
  28. Profile* profile = static_cast<Profile*>(context);
  29. #ifndef NDEBUG
  30. dependency_manager_->AssertProfileWasntDestroyed(profile);
  31. #endif
  32. // Possibly handle Incognito mode.
  33. if (profile->IsOffTheRecord()) {
  34. if (ServiceRedirectedInIncognito()) {
  35. profile = profile->GetOriginalProfile();
  36. #ifndef NDEBUG
  37. dependency_manager_->AssertProfileWasntDestroyed(profile);
  38. #endif
  39. } else if (ServiceHasOwnInstanceInIncognito()) {
  40. // No-op; the pointers are already set correctly.
  41. } else {
  42. return NULL;
  43. }
  44. }
  45. return profile;
  46. }
  47. void ProfileKeyedBaseFactory::RegisterUserPrefsOnProfile(
  48. content::BrowserContext* profile) {
  49. // Safe timing for pref registration is hard. Previously, we made Profile
  50. // responsible for all pref registration on every service that used
  51. // Profile. Now we don't and there are timing issues.
  52. //
  53. // With normal profiles, prefs can simply be registered at
  54. // ProfileDependencyManager::CreateProfileServices time. With incognito
  55. // profiles, we just never register since incognito profiles share the same
  56. // pref services with their parent profiles.
  57. //
  58. // TestingProfiles 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 RegisterUserPrefs() on the appropriate ProfileKeyedServiceFactory
  62. // associated with the prefs they need, or they can use SetTestingFactory()
  63. // and create a service (since service creation with a factory method causes
  64. // registration to happen at service creation time).
  65. //
  66. // Now that services are responsible for declaring their preferences, we have
  67. // to enforce a uniquenes check here because some tests create one profile and
  68. // multiple services of the same type attached to that profile (serially, not
  69. // parallel) and we don't want to register multiple times on the same profile.
  70. DCHECK(!profile->IsOffTheRecord());
  71. std::set<content::BrowserContext*>::iterator it =
  72. registered_preferences_.find(profile);
  73. if (it == registered_preferences_.end()) {
  74. PrefService* prefs = components::UserPrefs::Get(profile);
  75. PrefRegistrySyncable* registry = static_cast<PrefRegistrySyncable*>(
  76. prefs->DeprecatedGetPrefRegistry());
  77. RegisterUserPrefs(registry);
  78. registered_preferences_.insert(profile);
  79. }
  80. }
  81. bool ProfileKeyedBaseFactory::ServiceRedirectedInIncognito() const {
  82. return false;
  83. }
  84. bool ProfileKeyedBaseFactory::ServiceHasOwnInstanceInIncognito() const {
  85. return false;
  86. }
  87. bool ProfileKeyedBaseFactory::ServiceIsCreatedWithProfile() const {
  88. return false;
  89. }
  90. bool ProfileKeyedBaseFactory::ServiceIsNULLWhileTesting() const {
  91. return false;
  92. }
  93. void ProfileKeyedBaseFactory::ProfileDestroyed(
  94. content::BrowserContext* profile) {
  95. // While object destruction can be customized in ways where the object is
  96. // only dereferenced, this still must run on the UI thread.
  97. DCHECK(CalledOnValidThread());
  98. registered_preferences_.erase(profile);
  99. }
  100. bool ProfileKeyedBaseFactory::ArePreferencesSetOn(
  101. content::BrowserContext* profile) const {
  102. return registered_preferences_.find(profile) !=
  103. registered_preferences_.end();
  104. }
  105. void ProfileKeyedBaseFactory::MarkPreferencesSetOn(
  106. content::BrowserContext* profile) {
  107. DCHECK(!ArePreferencesSetOn(profile));
  108. registered_preferences_.insert(profile);
  109. }