| 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 "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/profiles/profile_dependency_manager.h" | 8 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 9 | 9 |
| 10 void ProfileKeyedBaseFactory::SetTestingFactory(Profile* profile, | |
| 11 FactoryFunction factory) { | |
| 12 // Destroying the profile may cause us to lose data about whether |profile| | |
| 13 // has our preferences registered on it (since the profile object itself | |
| 14 // isn't dead). See if we need to readd it once we've gone through normal | |
| 15 // destruction. | |
| 16 bool add_profile = registered_preferences_.find(profile) != | |
| 17 registered_preferences_.end(); | |
| 18 | |
| 19 // We have to go through the shutdown and destroy mechanisms because there | |
| 20 // are unit tests that create a service on a profile and then change the | |
| 21 // testing service mid-test. | |
| 22 ProfileShutdown(profile); | |
| 23 ProfileDestroyed(profile); | |
| 24 | |
| 25 if (add_profile) | |
| 26 registered_preferences_.insert(profile); | |
| 27 | |
| 28 factories_[profile] = factory; | |
| 29 } | |
| 30 | |
| 31 ProfileKeyedBase* ProfileKeyedBaseFactory::SetTestingFactoryAndUse( | |
| 32 Profile* profile, | |
| 33 FactoryFunction factory) { | |
| 34 DCHECK(factory); | |
| 35 SetTestingFactory(profile, factory); | |
| 36 return GetBaseForProfile(profile, true); | |
| 37 } | |
| 38 | |
| 39 ProfileKeyedBaseFactory::ProfileKeyedBaseFactory( | 10 ProfileKeyedBaseFactory::ProfileKeyedBaseFactory( |
| 40 const char* name, ProfileDependencyManager* manager) | 11 const char* name, ProfileDependencyManager* manager) |
| 41 : dependency_manager_(manager) | 12 : dependency_manager_(manager) |
| 42 #ifndef NDEBUG | 13 #ifndef NDEBUG |
| 43 , service_name_(name) | 14 , service_name_(name) |
| 44 #endif | 15 #endif |
| 45 { | 16 { |
| 46 dependency_manager_->AddComponent(this); | 17 dependency_manager_->AddComponent(this); |
| 47 } | 18 } |
| 48 | 19 |
| 49 ProfileKeyedBaseFactory::~ProfileKeyedBaseFactory() { | 20 ProfileKeyedBaseFactory::~ProfileKeyedBaseFactory() { |
| 50 dependency_manager_->RemoveComponent(this); | 21 dependency_manager_->RemoveComponent(this); |
| 51 } | 22 } |
| 52 | 23 |
| 53 void ProfileKeyedBaseFactory::DependsOn(ProfileKeyedBaseFactory* rhs) { | 24 void ProfileKeyedBaseFactory::DependsOn(ProfileKeyedBaseFactory* rhs) { |
| 54 dependency_manager_->AddEdge(rhs, this); | 25 dependency_manager_->AddEdge(rhs, this); |
| 55 } | 26 } |
| 56 | 27 |
| 28 Profile* ProfileKeyedBaseFactory::GetProfileToUse(Profile* profile) { |
| 29 DCHECK(CalledOnValidThread()); |
| 30 |
| 31 #ifndef NDEBUG |
| 32 dependency_manager_->AssertProfileWasntDestroyed(profile); |
| 33 #endif |
| 34 |
| 35 // Possibly handle Incognito mode. |
| 36 if (profile->IsOffTheRecord()) { |
| 37 if (ServiceRedirectedInIncognito()) { |
| 38 profile = profile->GetOriginalProfile(); |
| 39 |
| 40 #ifndef NDEBUG |
| 41 dependency_manager_->AssertProfileWasntDestroyed(profile); |
| 42 #endif |
| 43 } else if (ServiceHasOwnInstanceInIncognito()) { |
| 44 // No-op; the pointers are already set correctly. |
| 45 } else { |
| 46 return NULL; |
| 47 } |
| 48 } |
| 49 |
| 50 return profile; |
| 51 } |
| 52 |
| 57 void ProfileKeyedBaseFactory::RegisterUserPrefsOnProfile(Profile* profile) { | 53 void ProfileKeyedBaseFactory::RegisterUserPrefsOnProfile(Profile* profile) { |
| 58 // Safe timing for pref registration is hard. Previously, we made Profile | 54 // Safe timing for pref registration is hard. Previously, we made Profile |
| 59 // responsible for all pref registration on every service that used | 55 // responsible for all pref registration on every service that used |
| 60 // Profile. Now we don't and there are timing issues. | 56 // Profile. Now we don't and there are timing issues. |
| 61 // | 57 // |
| 62 // With normal profiles, prefs can simply be registered at | 58 // With normal profiles, prefs can simply be registered at |
| 63 // ProfileDependencyManager::CreateProfileServices time. With incognito | 59 // ProfileDependencyManager::CreateProfileServices time. With incognito |
| 64 // profiles, we just never register since incognito profiles share the same | 60 // profiles, we just never register since incognito profiles share the same |
| 65 // pref services with their parent profiles. | 61 // pref services with their parent profiles. |
| 66 // | 62 // |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 } | 94 } |
| 99 | 95 |
| 100 bool ProfileKeyedBaseFactory::ServiceIsCreatedWithProfile() { | 96 bool ProfileKeyedBaseFactory::ServiceIsCreatedWithProfile() { |
| 101 return false; | 97 return false; |
| 102 } | 98 } |
| 103 | 99 |
| 104 bool ProfileKeyedBaseFactory::ServiceIsNULLWhileTesting() { | 100 bool ProfileKeyedBaseFactory::ServiceIsNULLWhileTesting() { |
| 105 return false; | 101 return false; |
| 106 } | 102 } |
| 107 | 103 |
| 108 ProfileKeyedBase* ProfileKeyedBaseFactory::GetBaseForProfile( | |
| 109 Profile* profile, | |
| 110 bool create) { | |
| 111 DCHECK(CalledOnValidThread()); | |
| 112 | |
| 113 #ifndef NDEBUG | |
| 114 dependency_manager_->AssertProfileWasntDestroyed(profile); | |
| 115 #endif | |
| 116 | |
| 117 // Possibly handle Incognito mode. | |
| 118 if (profile->IsOffTheRecord()) { | |
| 119 if (ServiceRedirectedInIncognito()) { | |
| 120 profile = profile->GetOriginalProfile(); | |
| 121 | |
| 122 #ifndef NDEBUG | |
| 123 dependency_manager_->AssertProfileWasntDestroyed(profile); | |
| 124 #endif | |
| 125 } else if (ServiceHasOwnInstanceInIncognito()) { | |
| 126 // No-op; the pointers are already set correctly. | |
| 127 } else { | |
| 128 return NULL; | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 ProfileKeyedBase* service = NULL; | |
| 133 if (GetAssociation(profile, &service)) { | |
| 134 return service; | |
| 135 } else if (create) { | |
| 136 // not found but creation allowed | |
| 137 | |
| 138 // Check to see if we have a per-Profile factory | |
| 139 std::map<Profile*, FactoryFunction>::iterator jt = factories_.find(profile); | |
| 140 if (jt != factories_.end()) { | |
| 141 if (jt->second) { | |
| 142 if (!profile->IsOffTheRecord()) | |
| 143 RegisterUserPrefsOnProfile(profile); | |
| 144 service = jt->second(profile); | |
| 145 } else { | |
| 146 service = NULL; | |
| 147 } | |
| 148 } else { | |
| 149 service = BuildServiceInstanceFor(profile); | |
| 150 } | |
| 151 } else { | |
| 152 // not found, creation forbidden | |
| 153 return NULL; | |
| 154 } | |
| 155 | |
| 156 Associate(profile, service); | |
| 157 return service; | |
| 158 } | |
| 159 | |
| 160 void ProfileKeyedBaseFactory::ProfileDestroyed(Profile* profile) { | 104 void ProfileKeyedBaseFactory::ProfileDestroyed(Profile* profile) { |
| 161 // While object destruction can be customized in ways where the object is | 105 // While object destruction can be customized in ways where the object is |
| 162 // only dereferenced, this still must run on the UI thread. | 106 // only dereferenced, this still must run on the UI thread. |
| 163 DCHECK(CalledOnValidThread()); | 107 DCHECK(CalledOnValidThread()); |
| 164 | 108 |
| 165 // For unit tests, we also remove the factory function both so we don't | |
| 166 // maintain a big map of dead pointers, but also since we may have a second | |
| 167 // object that lives at the same address (see other comments about unit tests | |
| 168 // in this file). | |
| 169 factories_.erase(profile); | |
| 170 registered_preferences_.erase(profile); | 109 registered_preferences_.erase(profile); |
| 171 } | 110 } |
| 111 |
| 112 bool ProfileKeyedBaseFactory::ArePreferencesSetOn(Profile* profile) { |
| 113 return registered_preferences_.find(profile) != |
| 114 registered_preferences_.end(); |
| 115 } |
| 116 |
| 117 void ProfileKeyedBaseFactory::MarkPreferencesSetOn(Profile* profile) { |
| 118 DCHECK(!ArePreferencesSetOn(profile)); |
| 119 registered_preferences_.insert(profile); |
| 120 } |
| OLD | NEW |