| 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/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
| 16 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 16 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 17 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 19 #include "content/common/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
| 20 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
| 21 #include "grit/theme_resources.h" | 21 #include "grit/theme_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const char kNameKey[] = "name"; | 27 const char kNameKey[] = "name"; |
| 28 const char kUserNameKey[] = "user_name"; | 28 const char kUserNameKey[] = "user_name"; |
| 29 const char kAvatarIconKey[] = "avatar_icon"; | 29 const char kAvatarIconKey[] = "avatar_icon"; |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 scoped_ptr<DictionaryValue> info(new DictionaryValue); | 120 scoped_ptr<DictionaryValue> info(new DictionaryValue); |
| 121 info->SetString(kNameKey, name); | 121 info->SetString(kNameKey, name); |
| 122 info->SetString(kUserNameKey, username); | 122 info->SetString(kUserNameKey, username); |
| 123 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); | 123 info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index)); |
| 124 // Default value for whether background apps are running is false. | 124 // Default value for whether background apps are running is false. |
| 125 info->SetBoolean(kBackgroundAppsKey, false); | 125 info->SetBoolean(kBackgroundAppsKey, false); |
| 126 cache->Set(key, info.release()); | 126 cache->Set(key, info.release()); |
| 127 | 127 |
| 128 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 128 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
| 129 | 129 |
| 130 NotificationService::current()->Notify( | 130 content::NotificationService::current()->Notify( |
| 131 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 131 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 132 NotificationService::AllSources(), | 132 content::NotificationService::AllSources(), |
| 133 NotificationService::NoDetails()); | 133 content::NotificationService::NoDetails()); |
| 134 } | 134 } |
| 135 | 135 |
| 136 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { | 136 void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) { |
| 137 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 137 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
| 138 DictionaryValue* cache = update.Get(); | 138 DictionaryValue* cache = update.Get(); |
| 139 | 139 |
| 140 std::string key = CacheKeyFromProfilePath(profile_path); | 140 std::string key = CacheKeyFromProfilePath(profile_path); |
| 141 cache->Remove(key, NULL); | 141 cache->Remove(key, NULL); |
| 142 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); | 142 sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key)); |
| 143 | 143 |
| 144 NotificationService::current()->Notify( | 144 content::NotificationService::current()->Notify( |
| 145 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 145 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 146 NotificationService::AllSources(), | 146 content::NotificationService::AllSources(), |
| 147 NotificationService::NoDetails()); | 147 content::NotificationService::NoDetails()); |
| 148 } | 148 } |
| 149 | 149 |
| 150 size_t ProfileInfoCache::GetNumberOfProfiles() const { | 150 size_t ProfileInfoCache::GetNumberOfProfiles() const { |
| 151 return sorted_keys_.size(); | 151 return sorted_keys_.size(); |
| 152 } | 152 } |
| 153 | 153 |
| 154 size_t ProfileInfoCache::GetIndexOfProfileWithPath( | 154 size_t ProfileInfoCache::GetIndexOfProfileWithPath( |
| 155 const FilePath& profile_path) const { | 155 const FilePath& profile_path) const { |
| 156 if (profile_path.DirName() != user_data_dir_) | 156 if (profile_path.DirName() != user_data_dir_) |
| 157 return std::string::npos; | 157 return std::string::npos; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 SetInfoForProfileAtIndex(index, info.release()); | 220 SetInfoForProfileAtIndex(index, info.release()); |
| 221 | 221 |
| 222 // Remove and reinsert key in |sorted_keys_| to alphasort. | 222 // Remove and reinsert key in |sorted_keys_| to alphasort. |
| 223 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); | 223 std::string key = CacheKeyFromProfilePath(GetPathOfProfileAtIndex(index)); |
| 224 std::vector<std::string>::iterator key_it = | 224 std::vector<std::string>::iterator key_it = |
| 225 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); | 225 std::find(sorted_keys_.begin(), sorted_keys_.end(), key); |
| 226 DCHECK(key_it != sorted_keys_.end()); | 226 DCHECK(key_it != sorted_keys_.end()); |
| 227 sorted_keys_.erase(key_it); | 227 sorted_keys_.erase(key_it); |
| 228 sorted_keys_.insert(FindPositionForProfile(key, name), key); | 228 sorted_keys_.insert(FindPositionForProfile(key, name), key); |
| 229 | 229 |
| 230 NotificationService::current()->Notify( | 230 content::NotificationService::current()->Notify( |
| 231 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 231 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 232 NotificationService::AllSources(), | 232 content::NotificationService::AllSources(), |
| 233 NotificationService::NoDetails()); | 233 content::NotificationService::NoDetails()); |
| 234 } | 234 } |
| 235 | 235 |
| 236 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, | 236 void ProfileInfoCache::SetUserNameOfProfileAtIndex(size_t index, |
| 237 const string16& user_name) { | 237 const string16& user_name) { |
| 238 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); | 238 scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy()); |
| 239 info->SetString(kUserNameKey, user_name); | 239 info->SetString(kUserNameKey, user_name); |
| 240 // This takes ownership of |info|. | 240 // This takes ownership of |info|. |
| 241 SetInfoForProfileAtIndex(index, info.release()); | 241 SetInfoForProfileAtIndex(index, info.release()); |
| 242 } | 242 } |
| 243 | 243 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 cache->GetDictionary(sorted_keys_[index], &info); | 381 cache->GetDictionary(sorted_keys_[index], &info); |
| 382 return info; | 382 return info; |
| 383 } | 383 } |
| 384 | 384 |
| 385 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index, | 385 void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index, |
| 386 DictionaryValue* info) { | 386 DictionaryValue* info) { |
| 387 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); | 387 DictionaryPrefUpdate update(prefs_, prefs::kProfileInfoCache); |
| 388 DictionaryValue* cache = update.Get(); | 388 DictionaryValue* cache = update.Get(); |
| 389 cache->Set(sorted_keys_[index], info); | 389 cache->Set(sorted_keys_[index], info); |
| 390 | 390 |
| 391 NotificationService::current()->Notify( | 391 content::NotificationService::current()->Notify( |
| 392 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 392 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 393 NotificationService::AllSources(), | 393 content::NotificationService::AllSources(), |
| 394 NotificationService::NoDetails()); | 394 content::NotificationService::NoDetails()); |
| 395 } | 395 } |
| 396 | 396 |
| 397 std::string ProfileInfoCache::CacheKeyFromProfilePath( | 397 std::string ProfileInfoCache::CacheKeyFromProfilePath( |
| 398 const FilePath& profile_path) const { | 398 const FilePath& profile_path) const { |
| 399 DCHECK(user_data_dir_ == profile_path.DirName()); | 399 DCHECK(user_data_dir_ == profile_path.DirName()); |
| 400 FilePath base_name = profile_path.BaseName(); | 400 FilePath base_name = profile_path.BaseName(); |
| 401 return base_name.MaybeAsASCII(); | 401 return base_name.MaybeAsASCII(); |
| 402 } | 402 } |
| 403 | 403 |
| 404 std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile( | 404 std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile( |
| 405 std::string search_key, | 405 std::string search_key, |
| 406 const string16& search_name) { | 406 const string16& search_name) { |
| 407 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { | 407 for (size_t i = 0; i < GetNumberOfProfiles(); ++i) { |
| 408 int name_compare = search_name.compare(GetNameOfProfileAtIndex(i)); | 408 int name_compare = search_name.compare(GetNameOfProfileAtIndex(i)); |
| 409 if (name_compare < 0) | 409 if (name_compare < 0) |
| 410 return sorted_keys_.begin() + i; | 410 return sorted_keys_.begin() + i; |
| 411 if (name_compare == 0) { | 411 if (name_compare == 0) { |
| 412 int key_compare = search_key.compare(sorted_keys_[i]); | 412 int key_compare = search_key.compare(sorted_keys_[i]); |
| 413 if (key_compare < 0) | 413 if (key_compare < 0) |
| 414 return sorted_keys_.begin() + i; | 414 return sorted_keys_.begin() + i; |
| 415 } | 415 } |
| 416 } | 416 } |
| 417 return sorted_keys_.end(); | 417 return sorted_keys_.end(); |
| 418 } | 418 } |
| 419 | 419 |
| 420 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { | 420 void ProfileInfoCache::RegisterPrefs(PrefService* prefs) { |
| 421 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); | 421 prefs->RegisterDictionaryPref(prefs::kProfileInfoCache); |
| 422 } | 422 } |
| OLD | NEW |