OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_info_cache.h" | 5 #include "chrome/browser/profiles/profile_info_cache.h" |
6 | 6 |
7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/browser_process.h" | |
15 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
16 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 17 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
17 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
18 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
20 #include "chrome/installer/util/browser_distribution.h" | |
21 #include "chrome/installer/util/shell_util.h" | |
19 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
20 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
21 #include "grit/theme_resources.h" | 24 #include "grit/theme_resources.h" |
22 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/base/resource/resource_bundle.h" | 26 #include "ui/base/resource/resource_bundle.h" |
24 | 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 const char kNameKey[] = "name"; | 30 const char kNameKey[] = "name"; |
28 const char kUserNameKey[] = "user_name"; | 31 const char kUserNameKey[] = "user_name"; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 IDS_DEFAULT_AVATAR_NAME_18, | 82 IDS_DEFAULT_AVATAR_NAME_18, |
80 IDS_DEFAULT_AVATAR_NAME_19, | 83 IDS_DEFAULT_AVATAR_NAME_19, |
81 IDS_DEFAULT_AVATAR_NAME_20, | 84 IDS_DEFAULT_AVATAR_NAME_20, |
82 IDS_DEFAULT_AVATAR_NAME_21, | 85 IDS_DEFAULT_AVATAR_NAME_21, |
83 IDS_DEFAULT_AVATAR_NAME_22, | 86 IDS_DEFAULT_AVATAR_NAME_22, |
84 IDS_DEFAULT_AVATAR_NAME_23, | 87 IDS_DEFAULT_AVATAR_NAME_23, |
85 IDS_DEFAULT_AVATAR_NAME_24, | 88 IDS_DEFAULT_AVATAR_NAME_24, |
86 IDS_DEFAULT_AVATAR_NAME_25 | 89 IDS_DEFAULT_AVATAR_NAME_25 |
87 }; | 90 }; |
88 | 91 |
89 } // namespace | 92 } // namespace |
90 | 93 |
91 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, | 94 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, |
92 const FilePath& user_data_dir) | 95 const FilePath& user_data_dir) |
93 : prefs_(prefs), | 96 : prefs_(prefs), |
94 user_data_dir_(user_data_dir) { | 97 user_data_dir_(user_data_dir) { |
95 // Populate the cache | 98 // Populate the cache |
96 const DictionaryValue* cache = | 99 const DictionaryValue* cache = |
97 prefs_->GetDictionary(prefs::kProfileInfoCache); | 100 prefs_->GetDictionary(prefs::kProfileInfoCache); |
98 for (DictionaryValue::key_iterator it = cache->begin_keys(); | 101 for (DictionaryValue::key_iterator it = cache->begin_keys(); |
99 it != cache->end_keys(); ++it) { | 102 it != cache->end_keys(); ++it) { |
(...skipping 20 matching lines...) Expand all Loading... | |
120 scoped_ptr<DictionaryValue> info(new DictionaryValue); | 123 scoped_ptr<DictionaryValue> info(new DictionaryValue); |
121 info->SetString(kNameKey, name); | 124 info->SetString(kNameKey, name); |
122 info->SetString(kUserNameKey, username); | 125 info->SetString(kUserNameKey, username); |
123 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | 126 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); |
124 // Default value for whether background apps are running is false. | 127 // Default value for whether background apps are running is false. |
125 info->SetBoolean(kBackgroundAppsKey, false); | 128 info->SetBoolean(kBackgroundAppsKey, false); |
126 cache->Set(key, info.release()); | 129 cache->Set(key, info.release()); |
127 | 130 |
128 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 131 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
129 | 132 |
133 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | |
134 observer_list_, | |
135 OnProfileAdded(name, UTF8ToUTF16(key))); | |
136 | |
130 content::NotificationService::current()->Notify( | 137 content::NotificationService::current()->Notify( |
131 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 138 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
132 content::NotificationService::AllSources(), | 139 content::NotificationService::AllSources(), |
133 content::NotificationService::NoDetails()); | 140 content::NotificationService::NoDetails()); |
134 } | 141 } |
135 | 142 |
143 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { | |
144 observer_list_.AddObserver(obs); | |
145 } | |
146 | |
147 void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver* obs) { | |
148 observer_list_.RemoveObserver(obs); | |
149 } | |
150 | |
136 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { | 151 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { |
137 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 152 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
138 DictionaryValue* cache = update.Get(); | 153 DictionaryValue* cache = update.Get(); |
154 std::string key = CacheKeyFromProfilePath(profile_path); | |
155 DictionaryValue* info = NULL; | |
156 cache->GetDictionary(key, &info); | |
robertshield
2011/11/18 03:58:57
check that cache is non null?
Miranda Callahan
2011/11/18 19:00:36
Done.
| |
157 string16 name; | |
158 info->GetString(kNameKey, &name); | |
robertshield
2011/11/18 03:58:57
check that info is non null? I notice that most of
Miranda Callahan
2011/11/18 19:00:36
Done.
| |
139 | 159 |
140 std::string key = CacheKeyFromProfilePath(profile_path); | 160 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
161 observer_list_, | |
162 OnProfileRemoved(name)); | |
163 | |
141 cache->Remove(key, NULL); | 164 cache->Remove(key, NULL); |
142 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); | 165 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); |
143 | 166 |
144 content::NotificationService::current()->Notify( | 167 content::NotificationService::current()->Notify( |
145 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 168 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
146 content::NotificationService::AllSources(), | 169 content::NotificationService::AllSources(), |
147 content::NotificationService::NoDetails()); | 170 content::NotificationService::NoDetails()); |
148 } | 171 } |
149 | 172 |
150 size_t ProfileInfoCache::GetNumberOfProfiles() const { | 173 size_t ProfileInfoCache::GetNumberOfProfiles() const { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 if (IsDefaultAvatarIconUrl(icon_url, &icon_index)) | 231 if (IsDefaultAvatarIconUrl(icon_url, &icon_index)) |
209 return icon_index; | 232 return icon_index; |
210 | 233 |
211 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; | 234 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; |
212 return GetDefaultAvatarIconResourceIDAtIndex(0); | 235 return GetDefaultAvatarIconResourceIDAtIndex(0); |
213 } | 236 } |
214 | 237 |
215 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, | 238 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, |
216 const string16& name) { | 239 const string16& name) { |
217 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 240 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
241 string16 old_name; | |
242 info->GetString(kNameKey, &old_name); | |
robertshield
2011/11/18 03:58:57
same null check grumbling as above, ignore me if n
Miranda Callahan
2011/11/18 19:00:36
Added null DCHECK directly to GetInfoForProfileAtI
| |
218 info->SetString(kNameKey, name); | 243 info->SetString(kNameKey, name); |
219 // This takes ownership of |info|. | 244 // This takes ownership of |info|. |
220 SetInfoForProfileAtIndex(index, info.release()); | 245 SetInfoForProfileAtIndex(index, info.release()); |
221 | 246 |
222 // Remove and reinsert key in |sorted_keys_| to alphasort. | 247 // Remove and reinsert key in |sorted_keys_| to alphasort. |
223 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); | 248 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); |
224 std::vector<std::string>::iterator key_it = | 249 std::vector<std::string>::iterator key_it = |
225 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); | 250 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); |
226 DCHECK(key_it != sorted_keys_.end()); | 251 DCHECK(key_it != sorted_keys_.end()); |
227 sorted_keys_.erase(key_it); | 252 sorted_keys_.erase(key_it); |
228 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 253 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
229 | 254 |
255 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, | |
256 observer_list_, | |
257 OnProfileNameChanged(old_name, name)); | |
258 | |
230 content::NotificationService::current()->Notify( | 259 content::NotificationService::current()->Notify( |
231 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 260 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
232 content::NotificationService::AllSources(), | 261 content::NotificationService::AllSources(), |
233 content::NotificationService::NoDetails()); | 262 content::NotificationService::NoDetails()); |
234 } | 263 } |
235 | 264 |
236 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, | 265 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, |
237 const string16& user_name) { | 266 const string16& user_name) { |
238 string16 old_user_name; | 267 string16 old_user_name; |
239 const base::DictionaryValue* old_info = GetInfoForProfileAtIndex(index); | 268 const base::DictionaryValue* old_info = GetInfoForProfileAtIndex(index); |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
416 return sorted_keys_.begin() + i; | 445 return sorted_keys_.begin() + i; |
417 if (name_compare == 0) { | 446 if (name_compare == 0) { |
418 int key_compare = search_key.compare(sorted_keys_[i]); | 447 int key_compare = search_key.compare(sorted_keys_[i]); |
419 if (key_compare < 0) | 448 if (key_compare < 0) |
420 return sorted_keys_.begin() + i; | 449 return sorted_keys_.begin() + i; |
421 } | 450 } |
422 } | 451 } |
423 return sorted_keys_.end(); | 452 return sorted_keys_.end(); |
424 } | 453 } |
425 | 454 |
455 // static | |
456 std::vector<string16> ProfileInfoCache::GetProfileNames() { | |
457 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | |
458 std::vector<string16> names; | |
459 PrefService* local_state = g_browser_process->local_state(); | |
460 const DictionaryValue* cache = local_state->GetDictionary( | |
461 prefs::kProfileInfoCache); | |
462 for (base::DictionaryValue::key_iterator it = cache->begin_keys(); | |
463 it != cache->end_keys(); | |
464 ++it) { | |
465 base::DictionaryValue* info = NULL; | |
466 cache->GetDictionary(*it, &info); | |
467 string16 name; | |
468 info->GetString(kNameKey, &name); | |
469 string16 shortcut; | |
470 if (ShellUtil::GetChromeShortcutName(dist, false, name, &shortcut)) | |
471 names.push_back(shortcut); | |
472 } | |
473 return names; | |
474 } | |
475 | |
476 // static | |
426 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { | 477 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { |
427 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); | 478 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); |
428 } | 479 } |
OLD | NEW |