| 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..cec7cf3a659857d0741f300db874740caa543ea9
|
| --- /dev/null
|
| +++ b/chrome/common/favicon/fallback_icon_url_parser.cc
|
| @@ -0,0 +1,111 @@
|
| +// 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/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 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())
|
| + 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();
|
| +}
|
| +
|
| +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
|
|
|