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" |
19 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
20 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
21 #include "grit/theme_resources.h" | 22 #include "grit/theme_resources.h" |
22 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/base/resource/resource_bundle.h" | 24 #include "ui/base/resource/resource_bundle.h" |
24 | 25 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 IDS_DEFAULT_AVATAR_NAME_18, | 80 IDS_DEFAULT_AVATAR_NAME_18, |
80 IDS_DEFAULT_AVATAR_NAME_19, | 81 IDS_DEFAULT_AVATAR_NAME_19, |
81 IDS_DEFAULT_AVATAR_NAME_20, | 82 IDS_DEFAULT_AVATAR_NAME_20, |
82 IDS_DEFAULT_AVATAR_NAME_21, | 83 IDS_DEFAULT_AVATAR_NAME_21, |
83 IDS_DEFAULT_AVATAR_NAME_22, | 84 IDS_DEFAULT_AVATAR_NAME_22, |
84 IDS_DEFAULT_AVATAR_NAME_23, | 85 IDS_DEFAULT_AVATAR_NAME_23, |
85 IDS_DEFAULT_AVATAR_NAME_24, | 86 IDS_DEFAULT_AVATAR_NAME_24, |
86 IDS_DEFAULT_AVATAR_NAME_25 | 87 IDS_DEFAULT_AVATAR_NAME_25 |
87 }; | 88 }; |
88 | 89 |
89 } // namespace | 90 } // namespace |
90 | 91 |
91 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, | 92 ProfileInfoCache::ProfileInfoCache(PrefService* prefs, |
92 const FilePath& user_data_dir) | 93 const FilePath& user_data_dir) |
93 : prefs_(prefs), | 94 : prefs_(prefs), |
94 user_data_dir_(user_data_dir) { | 95 user_data_dir_(user_data_dir) { |
95 // Populate the cache | 96 // Populate the cache |
96 const DictionaryValue* cache = | 97 const DictionaryValue* cache = |
97 prefs_->GetDictionary(prefs::kProfileInfoCache); | 98 prefs_->GetDictionary(prefs::kProfileInfoCache); |
98 for (DictionaryValue::key_iterator it = cache->begin_keys(); | 99 for (DictionaryValue::key_iterator it = cache->begin_keys(); |
99 it != cache->end_keys(); ++it) { | 100 it != cache->end_keys(); ++it) { |
(...skipping 20 matching lines...) Expand all Loading... |
120 scoped_ptr<DictionaryValue> info(new DictionaryValue); | 121 scoped_ptr<DictionaryValue> info(new DictionaryValue); |
121 info->SetString(kNameKey, name); | 122 info->SetString(kNameKey, name); |
122 info->SetString(kUserNameKey, username); | 123 info->SetString(kUserNameKey, username); |
123 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | 124 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); |
124 // Default value for whether background apps are running is false. | 125 // Default value for whether background apps are running is false. |
125 info->SetBoolean(kBackgroundAppsKey, false); | 126 info->SetBoolean(kBackgroundAppsKey, false); |
126 cache->Set(key, info.release()); | 127 cache->Set(key, info.release()); |
127 | 128 |
128 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 129 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
129 | 130 |
| 131 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 132 observer_list_, |
| 133 OnProfileAdded(name, UTF8ToUTF16(key))); |
| 134 |
130 content::NotificationService::current()->Notify( | 135 content::NotificationService::current()->Notify( |
131 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 136 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
132 content::NotificationService::AllSources(), | 137 content::NotificationService::AllSources(), |
133 content::NotificationService::NoDetails()); | 138 content::NotificationService::NoDetails()); |
| 139 } |
| 140 |
| 141 void ProfileInfoCache::AddObserver(ProfileInfoCacheObserver* obs) { |
| 142 observer_list_.AddObserver(obs); |
| 143 } |
| 144 |
| 145 void ProfileInfoCache::RemoveObserver(ProfileInfoCacheObserver* obs) { |
| 146 observer_list_.RemoveObserver(obs); |
134 } | 147 } |
135 | 148 |
136 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { | 149 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { |
137 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 150 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
138 DictionaryValue* cache = update.Get(); | 151 DictionaryValue* cache = update.Get(); |
139 | 152 |
140 std::string key = CacheKeyFromProfilePath(profile_path); | 153 std::string key = CacheKeyFromProfilePath(profile_path); |
| 154 DictionaryValue* info = NULL; |
| 155 cache->GetDictionary(key, &info); |
| 156 string16 name; |
| 157 info->GetString(kNameKey, &name); |
| 158 |
| 159 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 160 observer_list_, |
| 161 OnProfileRemoved(name)); |
| 162 |
141 cache->Remove(key, NULL); | 163 cache->Remove(key, NULL); |
142 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); | 164 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); |
143 | 165 |
144 content::NotificationService::current()->Notify( | 166 content::NotificationService::current()->Notify( |
145 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 167 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
146 content::NotificationService::AllSources(), | 168 content::NotificationService::AllSources(), |
147 content::NotificationService::NoDetails()); | 169 content::NotificationService::NoDetails()); |
148 } | 170 } |
149 | 171 |
150 size_t ProfileInfoCache::GetNumberOfProfiles() const { | 172 size_t ProfileInfoCache::GetNumberOfProfiles() const { |
151 return sorted_keys_.size(); | 173 return sorted_keys_.size(); |
152 } | 174 } |
153 | 175 |
154 size_t ProfileInfoCache::GetIndexOfProfileWithPath( | 176 size_t ProfileInfoCache::GetIndexOfProfileWithPath( |
155 const FilePath& profile_path) const { | 177 const FilePath& profile_path) const { |
156 if (profile_path.DirName() != user_data_dir_) | 178 if (profile_path.DirName() != user_data_dir_) |
157 return std::string::npos; | 179 return std::string::npos; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 if (IsDefaultAvatarIconUrl(icon_url, &icon_index)) | 230 if (IsDefaultAvatarIconUrl(icon_url, &icon_index)) |
209 return icon_index; | 231 return icon_index; |
210 | 232 |
211 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; | 233 DLOG(WARNING) << "Unknown avatar icon: " << icon_url; |
212 return GetDefaultAvatarIconResourceIDAtIndex(0); | 234 return GetDefaultAvatarIconResourceIDAtIndex(0); |
213 } | 235 } |
214 | 236 |
215 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, | 237 void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, |
216 const string16& name) { | 238 const string16& name) { |
217 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 239 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 240 string16 old_name; |
| 241 info->GetString(kNameKey, &old_name); |
218 info->SetString(kNameKey, name); | 242 info->SetString(kNameKey, name); |
219 // This takes ownership of |info|. | 243 // This takes ownership of |info|. |
220 SetInfoForProfileAtIndex(index, info.release()); | 244 SetInfoForProfileAtIndex(index, info.release()); |
221 | 245 |
222 // Remove and reinsert key in |sorted_keys_| to alphasort. | 246 // Remove and reinsert key in |sorted_keys_| to alphasort. |
223 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); | 247 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); |
224 std::vector<std::string>::iterator key_it = | 248 std::vector<std::string>::iterator key_it = |
225 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); | 249 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); |
226 DCHECK(key_it != sorted_keys_.end()); | 250 DCHECK(key_it != sorted_keys_.end()); |
227 sorted_keys_.erase(key_it); | 251 sorted_keys_.erase(key_it); |
228 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 252 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
229 | 253 |
| 254 FOR_EACH_OBSERVER(ProfileInfoCacheObserver, |
| 255 observer_list_, |
| 256 OnProfileNameChanged(old_name, name)); |
| 257 |
230 content::NotificationService::current()->Notify( | 258 content::NotificationService::current()->Notify( |
231 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 259 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
232 content::NotificationService::AllSources(), | 260 content::NotificationService::AllSources(), |
233 content::NotificationService::NoDetails()); | 261 content::NotificationService::NoDetails()); |
234 } | 262 } |
235 | 263 |
236 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, | 264 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, |
237 const string16& user_name) { | 265 const string16& user_name) { |
238 string16 old_user_name; | 266 string16 old_user_name; |
239 const base::DictionaryValue* old_info = GetInfoForProfileAtIndex(index); | 267 const base::DictionaryValue* old_info = GetInfoForProfileAtIndex(index); |
240 old_info->GetString(kUserNameKey, &old_user_name); | 268 old_info->GetString(kUserNameKey, &old_user_name); |
241 if (old_user_name == user_name) | 269 if (old_user_name == user_name) |
242 return; | 270 return; |
243 | 271 |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 return info; | 416 return info; |
389 } | 417 } |
390 | 418 |
391 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index, | 419 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index, |
392 DictionaryValue* info) { | 420 DictionaryValue* info) { |
393 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 421 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
394 DictionaryValue* cache = update.Get(); | 422 DictionaryValue* cache = update.Get(); |
395 cache->Set(sorted_keys_[index], info); | 423 cache->Set(sorted_keys_[index], info); |
396 | 424 |
397 content::NotificationService::current()->Notify( | 425 content::NotificationService::current()->Notify( |
398 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 426 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
399 content::NotificationService::AllSources(), | 427 content::NotificationService::AllSources(), |
400 content::NotificationService::NoDetails()); | 428 content::NotificationService::NoDetails()); |
401 } | 429 } |
402 | 430 |
403 std::string ProfileInfoCache::CacheKeyFromProfilePath( | 431 std::string ProfileInfoCache::CacheKeyFromProfilePath( |
404 const FilePath& profile_path) const { | 432 const FilePath& profile_path) const { |
405 DCHECK(user_data_dir_ == profile_path.DirName()); | 433 DCHECK(user_data_dir_ == profile_path.DirName()); |
406 FilePath base_name = profile_path.BaseName(); | 434 FilePath base_name = profile_path.BaseName(); |
407 return base_name.MaybeAsASCII(); | 435 return base_name.MaybeAsASCII(); |
408 } | 436 } |
409 | 437 |
410 std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile( | 438 std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile( |
411 std::string search_key, | 439 std::string search_key, |
412 const string16& search_name) { | 440 const string16& search_name) { |
413 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { | 441 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { |
414 int name_compare = search_name.compare(GetNameOfProfileAtIndex(i)); | 442 int name_compare = search_name.compare(GetNameOfProfileAtIndex(i)); |
415 if (name_compare < 0) | 443 if (name_compare < 0) |
416 return sorted_keys_.begin() + i; | 444 return sorted_keys_.begin() + i; |
417 if (name_compare == 0) { | 445 if (name_compare == 0) { |
418 int key_compare = search_key.compare(sorted_keys_[i]); | 446 int key_compare = search_key.compare(sorted_keys_[i]); |
419 if (key_compare < 0) | 447 if (key_compare < 0) |
420 return sorted_keys_.begin() + i; | 448 return sorted_keys_.begin() + i; |
421 } | 449 } |
422 } | 450 } |
423 return sorted_keys_.end(); | 451 return sorted_keys_.end(); |
424 } | 452 } |
425 | 453 |
| 454 // static |
| 455 std::vector<string16> ProfileInfoCache::GetProfileNames() { |
| 456 std::vector<string16> names; |
| 457 PrefService* local_state = g_browser_process->local_state(); |
| 458 const DictionaryValue* cache = local_state->GetDictionary( |
| 459 prefs::kProfileInfoCache); |
| 460 string16 name; |
| 461 for (base::DictionaryValue::key_iterator it = cache->begin_keys(); |
| 462 it != cache->end_keys(); |
| 463 ++it) { |
| 464 base::DictionaryValue* info = NULL; |
| 465 cache->GetDictionary(*it, &info); |
| 466 info->GetString(kNameKey, &name); |
| 467 names.push_back(name); |
| 468 } |
| 469 return names; |
| 470 } |
| 471 |
| 472 // static |
426 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { | 473 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { |
427 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); | 474 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); |
428 } | 475 } |
OLD | NEW |