Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1908)

Unified Diff: chrome/browser/profiles/profile_info_cache.cc

Issue 7155015: Store profile avatar to preferences (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/profiles/profile_info_cache.cc
diff --git a/chrome/browser/profiles/profile_info_cache.cc b/chrome/browser/profiles/profile_info_cache.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f6ec25defe9dd9319a34af773f7a8a399872d3d2
--- /dev/null
+++ b/chrome/browser/profiles/profile_info_cache.cc
@@ -0,0 +1,207 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/profiles/profile_info_cache.h"
+
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/string_number_conversions.h"
+#include "base/stringprintf.h"
+#include "base/utf_string_conversions.h"
+#include "base/values.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/prefs/pref_service.h"
+#include "chrome/browser/prefs/scoped_user_pref_update.h"
+#include "chrome/common/pref_names.h"
+#include "grit/theme_resources.h"
+#include "ui/base/resource/resource_bundle.h"
+
+namespace {
+
+std::string kNameKey = "name";
willchan no longer on Chromium 2011/06/15 11:22:12 No global non-PODs. Just use const char kNameKey[]
sail 2011/06/21 03:26:53 Done.
+std::string kAvatarIconKey = "avatar_icon";
+std::string kDefaultUrlPrefix = "chrome://theme/IDR_PROFILE_AVATAR_";
+
+const int kDefaultAvatarIconResources[] = {
+ IDR_PROFILE_AVATAR_0,
+ IDR_PROFILE_AVATAR_1,
+ IDR_PROFILE_AVATAR_2,
+ IDR_PROFILE_AVATAR_3,
+};
+
+const int kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources);
+
+// Checks if the given URL points to one of the default avatar icons. if it is,
+// returns true and its index through |icon_index|. If not, returns false.
Miranda Callahan 2011/06/15 15:16:08 Ah, I see, you have comments in the ".cc" file --
sail 2011/06/21 03:26:53 Done.
+bool IsDefaultAvatarIconUrl(const std::string url, size_t* icon_index) {
willchan no longer on Chromium 2011/06/15 11:22:12 const std::string&
sail 2011/06/21 03:26:53 Done.
+ DCHECK(icon_index);
+ if (url.find(kDefaultUrlPrefix) != 0)
+ return false;
+
+ int int_value = -1;
+ if (base::StringToInt(url.begin() + url.size(),
+ url.end(),
+ &int_value)) {
+ if (int_value < 0 || int_value >= kDefaultAvatarIconsCount)
+ return false;
+ *icon_index = int_value;
+ return true;
+ }
+
+ return false;
+}
+
+// Returns URL to default avatar icon with specifided index.
Miranda Callahan 2011/06/15 15:16:08 I actually like the word "specifided," but others
sail 2011/06/21 03:26:53 Done.
+std::string GetDefaultAvatarIconUrl(int icon_index) {
+ DCHECK(icon_index < kDefaultAvatarIconsCount);
willchan no longer on Chromium 2011/06/15 11:22:12 DCHECK_LT
sail 2011/06/21 03:26:53 Done.
+ return StringPrintf("%s%d", kDefaultUrlPrefix.c_str(), icon_index);
+}
+
+} // namespace
+
+ProfileInfoCache::ProfileInfoCache(const FilePath& user_data_dir)
+ : user_data_dir_(user_data_dir) {
+ g_browser_process->local_state()->RegisterDictionaryPref(
+ prefs::kProfileInfoCache);
+
+ // Popuplate the cache
Miranda Callahan 2011/06/15 15:16:08 same w/"Popuplate"
sail 2011/06/21 03:26:53 Done.
+ const DictionaryValue* cache =
+ g_browser_process->local_state()->GetDictionary(prefs::kProfileInfoCache);
+ for (DictionaryValue::key_iterator it = cache->begin_keys();
+ it != cache->end_keys(); ++it) {
+ std::string key = *it;
+ DictionaryValue* info = NULL;
+ cache->GetDictionary(key, &info);
+ string16 name;
+ info->GetString(kNameKey, &name);
+ sorted_keys_.insert(FindPositionForProfile(key, name), key);
+ }
+}
+
+void ProfileInfoCache::AddProfileToCache(const FilePath& profile_path,
+ string16 name,
+ size_t icon_index) {
+ std::string key = CacheKeyFromProfilePath(profile_path);
+ DictionaryPrefUpdate update(g_browser_process->local_state(),
+ prefs::kProfileInfoCache);
+ DictionaryValue* cache = update.Get();
+
+ scoped_ptr<DictionaryValue> info(new DictionaryValue);
+ info->SetString(kNameKey, name);
+ info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index));
+ cache->Set(key, info.release());
+
+ sorted_keys_.insert(FindPositionForProfile(key, name), key);
+}
+
+void ProfileInfoCache::DeleteProfileFromCache(const FilePath& profile_path) {
+ DictionaryPrefUpdate update(g_browser_process->local_state(),
+ prefs::kProfileInfoCache);
+ DictionaryValue* cache = update.Get();
+
+ std::string key = CacheKeyFromProfilePath(profile_path);
+ cache->Remove(key, NULL);
+ sorted_keys_.erase(std::find(sorted_keys_.begin(), sorted_keys_.end(), key));
+}
+
+size_t ProfileInfoCache::GetNumberOfProfiles() const {
+ return sorted_keys_.size();
+}
+
+string16 ProfileInfoCache::GetNameOfProfileAtIndex(size_t index) const {
+ string16 name;
+ GetInfoForProfileAtIndex(index)->GetString(kNameKey, &name);
+ return name;
+}
+
+FilePath ProfileInfoCache::GetPathOfProfileAtIndex(size_t index) const {
+ FilePath::StringType base_name;
+#if defined(OS_POSIX)
+ base_name = sorted_keys_[index];
+#elif defined(OS_WIN)
+ base_name = ASCIIToWide(sorted_keys_[index]);
+#endif
+ return user_data_dir_.Append(base_name);
+}
+
+const gfx::Image& ProfileInfoCache::GetAvatarIconOfProfileAtIndex(
+ size_t index) const {
+ std::string icon_url;
+ GetInfoForProfileAtIndex(index)->GetString(kAvatarIconKey, &icon_url);
+ size_t icon_index = 0;
+ if (IsDefaultAvatarIconUrl(icon_url, &icon_index)) {
+ int resource_id = GetDefaultAvatarIconResourceIDAtIndex(icon_index);
+ return ResourceBundle::GetSharedInstance().GetImageNamed(resource_id);
+ }
+
+ DLOG(WARNING) << "Unkown avatar icon: " << icon_url;
willchan no longer on Chromium 2011/06/15 11:22:12 Unknown
sail 2011/06/21 03:26:53 Done.
+ return ResourceBundle::GetSharedInstance().GetImageNamed(
+ GetDefaultAvatarIconResourceIDAtIndex(0));
+}
+
+void ProfileInfoCache::SetNameOfProfileAtIndex(size_t index, string16 name) {
willchan no longer on Chromium 2011/06/15 11:22:12 const string16&
sail 2011/06/21 03:26:53 Done.
+ scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy());
+ info->SetString(kNameKey, name);
+ // This takes ownership of |info|.
+ SetInfoForProfileAtIndex(index, info.release());
+}
+
+void ProfileInfoCache::SetAvatarIconOfProfileAtIndex(size_t index,
+ size_t icon_index) {
+ scoped_ptr<DictionaryValue> info(GetInfoForProfileAtIndex(index)->DeepCopy());
+ info->SetString(kAvatarIconKey, GetDefaultAvatarIconUrl(icon_index));
+ // This takes ownership of |info|.
+ SetInfoForProfileAtIndex(index, info.release());
+}
+
+size_t ProfileInfoCache::GetDefaultAvatarIconCount() {
+ return kDefaultAvatarIconsCount;
+}
+
+int ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(size_t index) {
+ DCHECK(index < GetDefaultAvatarIconCount());
willchan no longer on Chromium 2011/06/15 11:22:12 DCHECK_LT
sail 2011/06/21 03:26:53 Done.
+ return kDefaultAvatarIconResources[index];
+}
+
+const DictionaryValue* ProfileInfoCache::GetInfoForProfileAtIndex(
+ size_t index) const {
+ DCHECK(index < GetNumberOfProfiles());
willchan no longer on Chromium 2011/06/15 11:22:12 DCHECK_LT
sail 2011/06/21 03:26:53 Done.
+ const DictionaryValue* cache =
+ g_browser_process->local_state()->GetDictionary(prefs::kProfileInfoCache);
+ DictionaryValue* info = NULL;
+ cache->GetDictionary(sorted_keys_[index], &info);
+ return info;
+}
+
+// Saves the profile info to the cache and takes ownership of |info|.
+void ProfileInfoCache::SetInfoForProfileAtIndex(size_t index,
+ DictionaryValue* info) {
+ DictionaryPrefUpdate update(g_browser_process->local_state(),
+ prefs::kProfileInfoCache);
+ DictionaryValue* cache = update.Get();
+ cache->Set(sorted_keys_[index], info);
+}
+
+std::string ProfileInfoCache::CacheKeyFromProfilePath(
+ const FilePath& profile_path) const {
+ DCHECK(user_data_dir_ == profile_path.DirName());
willchan no longer on Chromium 2011/06/15 11:22:12 DCHECK_EQ
sail 2011/06/21 03:26:53 Using DCHECK_EQ didn't work because the macro need
+ FilePath base_name = profile_path.BaseName();
+ return base_name.MaybeAsASCII();
+}
+
+std::vector<std::string>::iterator ProfileInfoCache::FindPositionForProfile(
+ std::string search_key,
+ string16 search_name) {
+ for (size_t i = 0; i < GetNumberOfProfiles(); ++i) {
+ int name_compare = search_name.compare(GetNameOfProfileAtIndex(i));
+ if (name_compare < 0)
+ return sorted_keys_.begin() + i;
+ if (name_compare == 0) {
+ int key_compare = search_key.compare(sorted_keys_[i]);
+ if (key_compare < 0)
+ return sorted_keys_.begin() + i;
+ }
+ }
+ return sorted_keys_.end();
+}

Powered by Google App Engine
This is Rietveld 408576698