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

Unified Diff: chrome/common/favicon/fallback_icon_url_parser.cc

Issue 924063003: [Favicon] Adding FallbackIconUrlParser. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unsigned int compare issue in test. Created 5 years, 10 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/common/favicon/fallback_icon_url_parser.cc
diff --git a/chrome/common/favicon/fallback_icon_url_parser.cc b/chrome/common/favicon/fallback_icon_url_parser.cc
new file mode 100644
index 0000000000000000000000000000000000000000..51e7d963fc4cc6d74b7fed812c52fadec74220ea
--- /dev/null
+++ b/chrome/common/favicon/fallback_icon_url_parser.cc
@@ -0,0 +1,112 @@
+// Copyright 2015 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/common/favicon/fallback_icon_url_parser.h"
+
+#include "base/logging.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_split.h"
+#include "ui/gfx/favicon_size.h"
+
+namespace {
+
+// Returns 0-15 for given hex char (case-insensitive), and -1 if invalid.
+int ParseHexChar(char ch) {
+ if (ch >= '0' && ch <= '9')
+ return ch - '0';
+ if (ch >= 'A' && ch <= 'F')
+ return (ch - 'A') + 10;
+ if (ch >= 'a' && ch <= 'f')
+ return (ch - 'a') + 10;
+ return -1;
+}
cpu_(ooo_6.6-7.5) 2015/02/19 03:49:18 We don't have such helper somewhere else in our co
huangs 2015/02/20 16:06:44 Moot if I use SkParse::FindColor(). Meanwhile, I
+
+} // namespace
+
+namespace chrome {
+
+bool ParseIconColor(const std::string& str, SkColor* color) {
+ size_t len = str.length();
+ if (len != 3 && len != 6 && len != 8)
+ return false;
+ // Translate and validate each digit.
+ int d[8];
+ for (size_t i = 0; i < len; ++i) {
+ d[i] = ParseHexChar(str[i]);
+ if (d[i] < 0)
+ return false;
+ }
+ if (len == 3) { // RGB.
+ *color = SkColorSetRGB(d[0] * 0x11, d[1] * 0x11, d[2] * 0x11);
+ } else if (len == 6) { // RRGGBB.
+ *color = SkColorSetRGB(
+ (d[0] << 4) | d[1], (d[2] << 4) | d[3], (d[4] << 4) | d[5]);
+ } else { // RRGGBBAA.
+ DCHECK(len == 8);
+ *color = SkColorSetARGB((d[6] << 4) | d[7], (d[0] << 4) | d[1],
+ (d[2] << 4) | d[3], (d[4] << 4) | d[5]);
+ }
+ return true;
+}
+
+bool ParseFallbackIconSpecs(const std::string& specs_str,
+ int *size,
+ favicon_base::FallbackIconStyle* style) {
+ DCHECK(size);
+ DCHECK(style);
+
+ std::vector<std::string> tokens;
+ base::SplitStringDontTrim(specs_str, ',', &tokens);
+ if (tokens.size() != 5) // Force "," for empty fields.
+ return false;
+
+ *size = gfx::kFaviconSize;
+ if (!tokens[0].empty() && !base::StringToInt(tokens[0], size))
+ return false;
+ if (*size <= 0)
+ return false;
+
+ if (!tokens[1].empty() &&
+ !ParseIconColor(tokens[1], &style->background_color))
+ return false;
+
+ if (tokens[2].empty())
+ favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(style);
+ else if (!ParseIconColor(tokens[2], &style->text_color))
+ return false;
+
+ if (!tokens[3].empty() &&
+ !base::StringToDouble(tokens[3], &style->font_size_ratio))
+ return false;
+
+ if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness))
+ return false;
+
+ return favicon_base::ValidateFallbackIconStyle(*style);
+}
+
+bool ParseFallbackIconPath(const std::string& path,
+ ParsedFallbackIconPath* parsed) {
+ DCHECK(parsed);
+ parsed->url = "";
+ parsed->size_in_pixels = gfx::kFaviconSize;
+ parsed->style = favicon_base::FallbackIconStyle();
+
+ if (path.empty())
+ return false;
+
+ size_t slash = path.find("/", 0);
+ if (slash == std::string::npos)
+ return false;
+ std::string spec_str = path.substr(0, slash);
+ if (!ParseFallbackIconSpecs(
+ spec_str, &parsed->size_in_pixels, &parsed->style))
+ return false; // Parse failed.
+
+ // Extract URL.
+ parsed->url = path.substr(slash + 1);
+ return true;
+}
+
+} // namespace chrome

Powered by Google App Engine
This is Rietveld 408576698