Chromium Code Reviews| 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..dc0f64cb0f3b1739fff1ce8ca629d5e5c3d987e9 |
| --- /dev/null |
| +++ b/chrome/common/favicon/fallback_icon_url_parser.cc |
| @@ -0,0 +1,114 @@ |
| +// 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/strings/string_number_conversions.h" |
| +#include "base/strings/string_split.h" |
| +#include "ui/base/webui/web_ui_util.h" |
|
pkotwicz
2015/01/23 16:00:47
You no longer need the above include. Remove it.
huangs
2015/01/23 19:47:51
Done.
|
| +#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; |
| +} |
| + |
| +} // 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 digits. |
|
pkotwicz
2015/01/23 16:00:47
Nit: digits -> digit
huangs
2015/01/23 19:47:51
Done.
|
| + 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()) |
| + style->MatchTextColorWithBackgroundColor(); |
| + 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 style->is_valid();; |
|
pkotwicz
2015/01/23 16:00:47
Nit: Remove extra semicolon
huangs
2015/01/23 19:47:50
Done.
|
| +} |
| + |
| +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 parsed_index = 0; |
|
pkotwicz
2015/01/23 16:00:47
Nit: Can you replace the places where |parsed_inde
huangs
2015/01/23 19:47:51
Done.
|
| + size_t slash = path.find("/", parsed_index); |
| + if (slash == std::string::npos) |
| + return false; |
| + std::string spec_str = path.substr(parsed_index, slash - parsed_index); |
| + if (!ParseFallbackIconSpecs( |
| + spec_str, &parsed->size_in_pixels, &parsed->style)) |
| + return false; // Parse failed. |
| + parsed_index = slash + 1; |
| + |
| + // Extract URL. |
| + parsed->url = path.substr(parsed_index); |
| + return true; |
| +} |
| + |
| +} // namespace chrome |