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

Side by Side Diff: chrome/common/favicon/fallback_icon_url_parser.cc

Issue 835903005: [Favicon] Add new fallback icon rendering flow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleanup and simplification. Created 5 years, 11 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/common/favicon/fallback_icon_url_parser.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/string_split.h"
9 #include "ui/gfx/favicon_size.h"
10
11 namespace {
12
13 // Returns 0-15 for given hex char (case-insensitive), and -1 if invalid.
14 int ParseHexChar(char ch) {
15 if (ch >= '0' && ch <= '9')
16 return ch - '0';
17 if (ch >= 'A' && ch <= 'F')
18 return (ch - 'A') + 10;
19 if (ch >= 'a' && ch <= 'f')
20 return (ch - 'a') + 10;
21 return -1;
22 }
23
24 } // namespace
25
26 namespace chrome {
27
28 bool ParseIconColor(const std::string& str, SkColor* color) {
29 size_t len = str.length();
30 if (len != 3 && len != 6 && len != 8)
31 return false;
32 // Translate and validate each digit.
33 int d[8];
34 for (size_t i = 0; i < len; ++i) {
35 d[i] = ParseHexChar(str[i]);
36 if (d[i] < 0)
37 return false;
38 }
39 if (len == 3) { // RGB.
40 *color = SkColorSetRGB(d[0] * 0x11, d[1] * 0x11, d[2] * 0x11);
41 } else if (len == 6) { // RRGGBB.
42 *color = SkColorSetRGB(
43 (d[0] << 4) | d[1], (d[2] << 4) | d[3], (d[4] << 4) | d[5]);
44 } else { // RRGGBBAA.
45 DCHECK(len == 8);
46 *color = SkColorSetARGB((d[6] << 4) | d[7], (d[0] << 4) | d[1],
47 (d[2] << 4) | d[3], (d[4] << 4) | d[5]);
48 }
49 return true;
50 }
51
52 bool ParseFallbackIconSpecs(const std::string& specs_str,
53 int *size,
54 favicon_base::FallbackIconStyle* style) {
55 DCHECK(size);
56 DCHECK(style);
57
58 std::vector<std::string> tokens;
59 base::SplitStringDontTrim(specs_str, ',', &tokens);
60 if (tokens.size() != 5) // Force "," for empty fields.
61 return false;
62
63 *size = gfx::kFaviconSize;
64 if (!tokens[0].empty() && !base::StringToInt(tokens[0], size))
65 return false;
66 if (*size <= 0)
67 return false;
68
69 if (!tokens[1].empty() &&
70 !ParseIconColor(tokens[1], &style->background_color))
71 return false;
72
73 if (tokens[2].empty())
74 style->MatchTextColorWithBackgroundColor();
75 else if (!ParseIconColor(tokens[2], &style->text_color))
76 return false;
77
78 if (!tokens[3].empty() &&
79 !base::StringToDouble(tokens[3], &style->font_size_ratio))
80 return false;
81
82 if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness))
83 return false;
84
85 return style->is_valid();
86 }
87
88 bool ParseFallbackIconPath(const std::string& path,
89 ParsedFallbackIconPath* parsed) {
90 DCHECK(parsed);
91 parsed->url = "";
92 parsed->size_in_pixels = gfx::kFaviconSize;
93 parsed->style = favicon_base::FallbackIconStyle();
94
95 if (path.empty())
96 return false;
97
98 size_t slash = path.find("/", 0);
99 if (slash == std::string::npos)
100 return false;
101 std::string spec_str = path.substr(0, slash);
102 if (!ParseFallbackIconSpecs(
103 spec_str, &parsed->size_in_pixels, &parsed->style))
104 return false; // Parse failed.
105
106 // Extract URL.
107 parsed->url = path.substr(slash + 1);
108 return true;
109 }
110
111 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698