OLD | NEW |
---|---|
(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/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.
| |
10 #include "ui/gfx/favicon_size.h" | |
11 | |
12 namespace { | |
13 | |
14 // Returns 0-15 for given hex char (case-insensitive), and -1 if invalid. | |
15 int ParseHexChar(char ch) { | |
16 if (ch >= '0' && ch <= '9') | |
17 return ch - '0'; | |
18 if (ch >= 'A' && ch <= 'F') | |
19 return (ch - 'A') + 10; | |
20 if (ch >= 'a' && ch <= 'f') | |
21 return (ch - 'a') + 10; | |
22 return -1; | |
23 } | |
24 | |
25 } // namespace | |
26 | |
27 namespace chrome { | |
28 | |
29 bool ParseIconColor(const std::string& str, SkColor* color) { | |
30 size_t len = str.length(); | |
31 if (len != 3 && len != 6 && len != 8) | |
32 return false; | |
33 // Translate and validate each digits. | |
pkotwicz
2015/01/23 16:00:47
Nit: digits -> digit
huangs
2015/01/23 19:47:51
Done.
| |
34 int d[8]; | |
35 for (size_t i = 0; i < len; ++i) { | |
36 d[i] = ParseHexChar(str[i]); | |
37 if (d[i] < 0) | |
38 return false; | |
39 } | |
40 if (len == 3) { // RGB. | |
41 *color = SkColorSetRGB(d[0] * 0x11, d[1] * 0x11, d[2] * 0x11); | |
42 } else if (len == 6) { // RRGGBB. | |
43 *color = SkColorSetRGB( | |
44 (d[0] << 4) | d[1], (d[2] << 4) | d[3], (d[4] << 4) | d[5]); | |
45 } else { // RRGGBBAA. | |
46 DCHECK(len == 8); | |
47 *color = SkColorSetARGB((d[6] << 4) | d[7], (d[0] << 4) | d[1], | |
48 (d[2] << 4) | d[3], (d[4] << 4) | d[5]); | |
49 } | |
50 return true; | |
51 } | |
52 | |
53 bool ParseFallbackIconSpecs(const std::string& specs_str, | |
54 int *size, | |
55 favicon_base::FallbackIconStyle* style) { | |
56 DCHECK(size); | |
57 DCHECK(style); | |
58 | |
59 std::vector<std::string> tokens; | |
60 base::SplitStringDontTrim(specs_str, ',', &tokens); | |
61 if (tokens.size() != 5) // Force "," for empty fields. | |
62 return false; | |
63 | |
64 *size = gfx::kFaviconSize; | |
65 if (!tokens[0].empty() && !base::StringToInt(tokens[0], size)) | |
66 return false; | |
67 if (*size <= 0) | |
68 return false; | |
69 | |
70 if (!tokens[1].empty() && | |
71 !ParseIconColor(tokens[1], &style->background_color)) | |
72 return false; | |
73 | |
74 if (tokens[2].empty()) | |
75 style->MatchTextColorWithBackgroundColor(); | |
76 else if (!ParseIconColor(tokens[2], &style->text_color)) | |
77 return false; | |
78 | |
79 if (!tokens[3].empty() && | |
80 !base::StringToDouble(tokens[3], &style->font_size_ratio)) | |
81 return false; | |
82 | |
83 if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness)) | |
84 return false; | |
85 | |
86 return style->is_valid();; | |
pkotwicz
2015/01/23 16:00:47
Nit: Remove extra semicolon
huangs
2015/01/23 19:47:50
Done.
| |
87 } | |
88 | |
89 bool ParseFallbackIconPath(const std::string& path, | |
90 ParsedFallbackIconPath* parsed) { | |
91 DCHECK(parsed); | |
92 parsed->url = ""; | |
93 parsed->size_in_pixels = gfx::kFaviconSize; | |
94 parsed->style = favicon_base::FallbackIconStyle(); | |
95 | |
96 if (path.empty()) | |
97 return false; | |
98 | |
99 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.
| |
100 size_t slash = path.find("/", parsed_index); | |
101 if (slash == std::string::npos) | |
102 return false; | |
103 std::string spec_str = path.substr(parsed_index, slash - parsed_index); | |
104 if (!ParseFallbackIconSpecs( | |
105 spec_str, &parsed->size_in_pixels, &parsed->style)) | |
106 return false; // Parse failed. | |
107 parsed_index = slash + 1; | |
108 | |
109 // Extract URL. | |
110 parsed->url = path.substr(parsed_index); | |
111 return true; | |
112 } | |
113 | |
114 } // namespace chrome | |
OLD | NEW |