| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/profiles/profile_keyed_base_factory.h" | 5 #include "chrome/browser/profiles/profile_keyed_base_factory.h" |
| 6 | 6 |
| 7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/profiles/profile_dependency_manager.h" | 9 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 10 #include "components/user_prefs/pref_registry_syncable.h" | 10 #include "components/user_prefs/pref_registry_syncable.h" |
| 11 #include "components/user_prefs/user_prefs.h" |
| 11 | 12 |
| 12 ProfileKeyedBaseFactory::ProfileKeyedBaseFactory( | 13 ProfileKeyedBaseFactory::ProfileKeyedBaseFactory( |
| 13 const char* name, ProfileDependencyManager* manager) | 14 const char* name, ProfileDependencyManager* manager) |
| 14 : dependency_manager_(manager) | 15 : dependency_manager_(manager) |
| 15 #ifndef NDEBUG | 16 #ifndef NDEBUG |
| 16 , service_name_(name) | 17 , service_name_(name) |
| 17 #endif | 18 #endif |
| 18 { | 19 { |
| 19 dependency_manager_->AddComponent(this); | 20 dependency_manager_->AddComponent(this); |
| 20 } | 21 } |
| 21 | 22 |
| 22 ProfileKeyedBaseFactory::~ProfileKeyedBaseFactory() { | 23 ProfileKeyedBaseFactory::~ProfileKeyedBaseFactory() { |
| 23 dependency_manager_->RemoveComponent(this); | 24 dependency_manager_->RemoveComponent(this); |
| 24 } | 25 } |
| 25 | 26 |
| 26 void ProfileKeyedBaseFactory::DependsOn(ProfileKeyedBaseFactory* rhs) { | 27 void ProfileKeyedBaseFactory::DependsOn(ProfileKeyedBaseFactory* rhs) { |
| 27 dependency_manager_->AddEdge(rhs, this); | 28 dependency_manager_->AddEdge(rhs, this); |
| 28 } | 29 } |
| 29 | 30 |
| 30 Profile* ProfileKeyedBaseFactory::GetProfileToUse(Profile* profile) { | 31 content::BrowserContext* ProfileKeyedBaseFactory::GetProfileToUse( |
| 32 content::BrowserContext* context) { |
| 31 DCHECK(CalledOnValidThread()); | 33 DCHECK(CalledOnValidThread()); |
| 32 | 34 |
| 35 Profile* profile = static_cast<Profile*>(context); |
| 36 |
| 33 #ifndef NDEBUG | 37 #ifndef NDEBUG |
| 34 dependency_manager_->AssertProfileWasntDestroyed(profile); | 38 dependency_manager_->AssertProfileWasntDestroyed(profile); |
| 35 #endif | 39 #endif |
| 36 | 40 |
| 37 // Possibly handle Incognito mode. | 41 // Possibly handle Incognito mode. |
| 38 if (profile->IsOffTheRecord()) { | 42 if (profile->IsOffTheRecord()) { |
| 39 if (ServiceRedirectedInIncognito()) { | 43 if (ServiceRedirectedInIncognito()) { |
| 40 profile = profile->GetOriginalProfile(); | 44 profile = profile->GetOriginalProfile(); |
| 41 | 45 |
| 42 #ifndef NDEBUG | 46 #ifndef NDEBUG |
| 43 dependency_manager_->AssertProfileWasntDestroyed(profile); | 47 dependency_manager_->AssertProfileWasntDestroyed(profile); |
| 44 #endif | 48 #endif |
| 45 } else if (ServiceHasOwnInstanceInIncognito()) { | 49 } else if (ServiceHasOwnInstanceInIncognito()) { |
| 46 // No-op; the pointers are already set correctly. | 50 // No-op; the pointers are already set correctly. |
| 47 } else { | 51 } else { |
| 48 return NULL; | 52 return NULL; |
| 49 } | 53 } |
| 50 } | 54 } |
| 51 | 55 |
| 52 return profile; | 56 return profile; |
| 53 } | 57 } |
| 54 | 58 |
| 55 void ProfileKeyedBaseFactory::RegisterUserPrefsOnProfile(Profile* profile) { | 59 void ProfileKeyedBaseFactory::RegisterUserPrefsOnProfile( |
| 60 content::BrowserContext* profile) { |
| 56 // Safe timing for pref registration is hard. Previously, we made Profile | 61 // Safe timing for pref registration is hard. Previously, we made Profile |
| 57 // responsible for all pref registration on every service that used | 62 // responsible for all pref registration on every service that used |
| 58 // Profile. Now we don't and there are timing issues. | 63 // Profile. Now we don't and there are timing issues. |
| 59 // | 64 // |
| 60 // With normal profiles, prefs can simply be registered at | 65 // With normal profiles, prefs can simply be registered at |
| 61 // ProfileDependencyManager::CreateProfileServices time. With incognito | 66 // ProfileDependencyManager::CreateProfileServices time. With incognito |
| 62 // profiles, we just never register since incognito profiles share the same | 67 // profiles, we just never register since incognito profiles share the same |
| 63 // pref services with their parent profiles. | 68 // pref services with their parent profiles. |
| 64 // | 69 // |
| 65 // TestingProfiles throw a wrench into the mix, in that some tests will | 70 // TestingProfiles throw a wrench into the mix, in that some tests will |
| 66 // swap out the PrefService after we've registered user prefs on the original | 71 // swap out the PrefService after we've registered user prefs on the original |
| 67 // PrefService. Test code that does this is responsible for either manually | 72 // PrefService. Test code that does this is responsible for either manually |
| 68 // invoking RegisterUserPrefs() on the appropriate ProfileKeyedServiceFactory | 73 // invoking RegisterUserPrefs() on the appropriate ProfileKeyedServiceFactory |
| 69 // associated with the prefs they need, or they can use SetTestingFactory() | 74 // associated with the prefs they need, or they can use SetTestingFactory() |
| 70 // and create a service (since service creation with a factory method causes | 75 // and create a service (since service creation with a factory method causes |
| 71 // registration to happen at service creation time). | 76 // registration to happen at service creation time). |
| 72 // | 77 // |
| 73 // Now that services are responsible for declaring their preferences, we have | 78 // Now that services are responsible for declaring their preferences, we have |
| 74 // to enforce a uniquenes check here because some tests create one profile and | 79 // to enforce a uniquenes check here because some tests create one profile and |
| 75 // multiple services of the same type attached to that profile (serially, not | 80 // multiple services of the same type attached to that profile (serially, not |
| 76 // parallel) and we don't want to register multiple times on the same profile. | 81 // parallel) and we don't want to register multiple times on the same profile. |
| 77 DCHECK(!profile->IsOffTheRecord()); | 82 DCHECK(!profile->IsOffTheRecord()); |
| 78 | 83 |
| 79 std::set<Profile*>::iterator it = registered_preferences_.find(profile); | 84 std::set<content::BrowserContext*>::iterator it = |
| 85 registered_preferences_.find(profile); |
| 80 if (it == registered_preferences_.end()) { | 86 if (it == registered_preferences_.end()) { |
| 81 PrefService* prefs = profile->GetPrefs(); | 87 PrefService* prefs = components::UserPrefs::Get(profile); |
| 82 PrefRegistrySyncable* registry = static_cast<PrefRegistrySyncable*>( | 88 PrefRegistrySyncable* registry = static_cast<PrefRegistrySyncable*>( |
| 83 prefs->DeprecatedGetPrefRegistry()); | 89 prefs->DeprecatedGetPrefRegistry()); |
| 84 RegisterUserPrefs(registry); | 90 RegisterUserPrefs(registry); |
| 85 registered_preferences_.insert(profile); | 91 registered_preferences_.insert(profile); |
| 86 } | 92 } |
| 87 } | 93 } |
| 88 | 94 |
| 89 bool ProfileKeyedBaseFactory::ServiceRedirectedInIncognito() const { | 95 bool ProfileKeyedBaseFactory::ServiceRedirectedInIncognito() const { |
| 90 return false; | 96 return false; |
| 91 } | 97 } |
| 92 | 98 |
| 93 bool ProfileKeyedBaseFactory::ServiceHasOwnInstanceInIncognito() const { | 99 bool ProfileKeyedBaseFactory::ServiceHasOwnInstanceInIncognito() const { |
| 94 return false; | 100 return false; |
| 95 } | 101 } |
| 96 | 102 |
| 97 bool ProfileKeyedBaseFactory::ServiceIsCreatedWithProfile() const { | 103 bool ProfileKeyedBaseFactory::ServiceIsCreatedWithProfile() const { |
| 98 return false; | 104 return false; |
| 99 } | 105 } |
| 100 | 106 |
| 101 bool ProfileKeyedBaseFactory::ServiceIsNULLWhileTesting() const { | 107 bool ProfileKeyedBaseFactory::ServiceIsNULLWhileTesting() const { |
| 102 return false; | 108 return false; |
| 103 } | 109 } |
| 104 | 110 |
| 105 void ProfileKeyedBaseFactory::ProfileDestroyed(Profile* profile) { | 111 void ProfileKeyedBaseFactory::ProfileDestroyed( |
| 112 content::BrowserContext* profile) { |
| 106 // While object destruction can be customized in ways where the object is | 113 // While object destruction can be customized in ways where the object is |
| 107 // only dereferenced, this still must run on the UI thread. | 114 // only dereferenced, this still must run on the UI thread. |
| 108 DCHECK(CalledOnValidThread()); | 115 DCHECK(CalledOnValidThread()); |
| 109 | 116 |
| 110 registered_preferences_.erase(profile); | 117 registered_preferences_.erase(profile); |
| 111 } | 118 } |
| 112 | 119 |
| 113 bool ProfileKeyedBaseFactory::ArePreferencesSetOn(Profile* profile) const { | 120 bool ProfileKeyedBaseFactory::ArePreferencesSetOn( |
| 121 content::BrowserContext* profile) const { |
| 114 return registered_preferences_.find(profile) != | 122 return registered_preferences_.find(profile) != |
| 115 registered_preferences_.end(); | 123 registered_preferences_.end(); |
| 116 } | 124 } |
| 117 | 125 |
| 118 void ProfileKeyedBaseFactory::MarkPreferencesSetOn(Profile* profile) { | 126 void ProfileKeyedBaseFactory::MarkPreferencesSetOn( |
| 127 content::BrowserContext* profile) { |
| 119 DCHECK(!ArePreferencesSetOn(profile)); | 128 DCHECK(!ArePreferencesSetOn(profile)); |
| 120 registered_preferences_.insert(profile); | 129 registered_preferences_.insert(profile); |
| 121 } | 130 } |
| OLD | NEW |