Index: chrome/browser/profiles/profile_info_util.cc |
diff --git a/chrome/browser/profiles/profile_info_util.cc b/chrome/browser/profiles/profile_info_util.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ed4c831b151e2a1f387edb54354804092d353d51 |
--- /dev/null |
+++ b/chrome/browser/profiles/profile_info_util.cc |
@@ -0,0 +1,195 @@ |
+// 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_util.h" |
+ |
+#include "base/format_macros.h" |
+#include "base/rand_util.h" |
+#include "base/string_number_conversions.h" |
+#include "base/stringprintf.h" |
+#include "base/utf_string_conversions.h" |
+#include "grit/generated_resources.h" |
+#include "grit/theme_resources.h" |
+#include "ui/base/l10n/l10n_util.h" |
+ |
+namespace { |
+ |
+const int kDefaultAvatarIconResources[] = { |
+ IDR_PROFILE_AVATAR_0, |
+ IDR_PROFILE_AVATAR_1, |
+ IDR_PROFILE_AVATAR_2, |
+ IDR_PROFILE_AVATAR_3, |
+ IDR_PROFILE_AVATAR_4, |
+ IDR_PROFILE_AVATAR_5, |
+ IDR_PROFILE_AVATAR_6, |
+ IDR_PROFILE_AVATAR_7, |
+ IDR_PROFILE_AVATAR_8, |
+ IDR_PROFILE_AVATAR_9, |
+ IDR_PROFILE_AVATAR_10, |
+ IDR_PROFILE_AVATAR_11, |
+ IDR_PROFILE_AVATAR_12, |
+ IDR_PROFILE_AVATAR_13, |
+ IDR_PROFILE_AVATAR_14, |
+ IDR_PROFILE_AVATAR_15, |
+ IDR_PROFILE_AVATAR_16, |
+ IDR_PROFILE_AVATAR_17, |
+ IDR_PROFILE_AVATAR_18, |
+ IDR_PROFILE_AVATAR_19, |
+ IDR_PROFILE_AVATAR_20, |
+ IDR_PROFILE_AVATAR_21, |
+ IDR_PROFILE_AVATAR_22, |
+ IDR_PROFILE_AVATAR_23, |
+ IDR_PROFILE_AVATAR_24, |
+ IDR_PROFILE_AVATAR_25, |
+}; |
+ |
+const size_t kDefaultAvatarIconsCount = arraysize(kDefaultAvatarIconResources); |
+ |
+// The first 8 icons are generic. |
+const size_t kGenericIconCount = 8; |
+ |
+// First eight are generic icons, which use IDS_NUMBERED_PROFILE_NAME. |
+const int kDefaultNames[] = { |
+ IDS_DEFAULT_AVATAR_NAME_8, |
+ IDS_DEFAULT_AVATAR_NAME_9, |
+ IDS_DEFAULT_AVATAR_NAME_10, |
+ IDS_DEFAULT_AVATAR_NAME_11, |
+ IDS_DEFAULT_AVATAR_NAME_12, |
+ IDS_DEFAULT_AVATAR_NAME_13, |
+ IDS_DEFAULT_AVATAR_NAME_14, |
+ IDS_DEFAULT_AVATAR_NAME_15, |
+ IDS_DEFAULT_AVATAR_NAME_16, |
+ IDS_DEFAULT_AVATAR_NAME_17, |
+ IDS_DEFAULT_AVATAR_NAME_18, |
+ IDS_DEFAULT_AVATAR_NAME_19, |
+ IDS_DEFAULT_AVATAR_NAME_20, |
+ IDS_DEFAULT_AVATAR_NAME_21, |
+ IDS_DEFAULT_AVATAR_NAME_22, |
+ IDS_DEFAULT_AVATAR_NAME_23, |
+ IDS_DEFAULT_AVATAR_NAME_24, |
+ IDS_DEFAULT_AVATAR_NAME_25 |
+}; |
+ |
+const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_"; |
+ |
+ |
+bool IconIndexIsUnique( |
+ const std::vector<ProfileInfoEntry>& entries, |
+ size_t icon_index) { |
+ for (std::vector<ProfileInfoEntry>::const_iterator it = entries.begin(); |
+ it != entries.end(); ++it) { |
+ if (it->icon_index() == icon_index) |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+// Tries to find an icon index that satisfies all the given conditions. |
+// Returns true if an icon was found, false otherwise. |
+bool ChooseAvatarIconIndexForNewProfile( |
+ const std::vector<ProfileInfoEntry>& entries, |
+ bool allow_generic_icon, |
+ bool must_be_unique, |
+ size_t* out_icon_index) { |
+ size_t start = allow_generic_icon ? 0 : kGenericIconCount; |
+ size_t end = profiles::GetDefaultAvatarIconCount(); |
+ size_t count = end - start; |
+ |
+ int rand = base::RandInt(0, count); |
+ for (size_t i = 0; i < count; ++i) { |
+ size_t icon_index = start + (rand + i) % count; |
+ if (!must_be_unique || IconIndexIsUnique(entries, icon_index)) { |
+ *out_icon_index = icon_index; |
+ return true; |
+ } |
+ } |
+ |
+ return false; |
+} |
+ |
+} // namespace |
+ |
+namespace profiles { |
+ |
+// Gets the number of default avatar icons that exist. |
+size_t GetDefaultAvatarIconCount() { |
+ return kDefaultAvatarIconsCount; |
+} |
+ |
+int GetDefaultAvatarIconResourceIDAtIndex(size_t index) { |
+ DCHECK_LT(index, GetDefaultAvatarIconCount()); |
+ return kDefaultAvatarIconResources[index]; |
+} |
+ |
+std::string GetDefaultAvatarIconUrl(size_t index) { |
+ DCHECK_LT(index, kDefaultAvatarIconsCount); |
+ return StringPrintf("%s%" PRIuS, kDefaultUrlPrefix, index); |
+} |
+ |
+bool IsDefaultAvatarIconUrl(const std::string& url, |
+ size_t* icon_index) { |
+ DCHECK(icon_index); |
+ if (url.find(kDefaultUrlPrefix) != 0) |
+ return false; |
+ |
+ int int_value = -1; |
+ if (base::StringToInt(url.begin() + strlen(kDefaultUrlPrefix), |
+ url.end(), |
+ &int_value)) { |
+ if (int_value < 0 || |
+ int_value >= static_cast<int>(kDefaultAvatarIconsCount)) |
+ return false; |
+ *icon_index = int_value; |
+ return true; |
+ } |
+ |
+ return false; |
+} |
+ |
+string16 ChooseNameForNewProfile(const std::vector<ProfileInfoEntry>& entries, |
+ size_t icon_index) { |
+ for (int name_index = 1; ; ++name_index) { |
+ string16 name; |
+ if (icon_index < kGenericIconCount) { |
+ name = l10n_util::GetStringFUTF16Int(IDS_NUMBERED_PROFILE_NAME, |
+ name_index); |
+ } else { |
+ name = l10n_util::GetStringUTF16( |
+ kDefaultNames[icon_index - kGenericIconCount]); |
+ if (name_index > 1) |
+ name.append(UTF8ToUTF16(base::IntToString(name_index))); |
+ } |
+ |
+ // Loop through previously named profiles to ensure we're not duplicating. |
+ bool name_found = false; |
+ for (std::vector<ProfileInfoEntry>::const_iterator it = entries.begin(); |
+ it != entries.end(); ++it) { |
+ if (it->name() == name) { |
+ name_found = true; |
+ break; |
+ } |
+ } |
+ if (!name_found) |
+ return name; |
+ } |
+} |
+ |
+size_t ChooseAvatarIconIndexForNewProfile( |
+ const std::vector<ProfileInfoEntry>& entries) { |
+ size_t icon_index = 0; |
+ // Try to find a unique, non-generic icon. |
+ if (::ChooseAvatarIconIndexForNewProfile(entries, false, true, &icon_index)) |
+ return icon_index; |
+ // Try to find any unique icon. |
+ if (::ChooseAvatarIconIndexForNewProfile(entries, true, true, &icon_index)) |
+ return icon_index; |
+ // Settle for any random icon, even if it's not unique. |
+ if (::ChooseAvatarIconIndexForNewProfile(entries, true, false, &icon_index)) |
+ return icon_index; |
+ |
+ NOTREACHED(); |
+ return 0; |
+} |
+ |
+} // namespace profile_icon_util |