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

Side by Side Diff: components/favicon_base/fallback_icon_url_parser.cc

Issue 2883053003: Delete unused chrome://fallback-icon/ handling (Closed)
Patch Set: Merge branch 'master' into searchbox3 Created 3 years, 7 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 "components/favicon_base/fallback_icon_url_parser.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "base/macros.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "third_party/skia/include/utils/SkParse.h"
15 #include "ui/gfx/favicon_size.h"
16
17 namespace {
18
19 // List of sizes corresponding to RGB, ARGB, RRGGBB, AARRGGBB.
20 const size_t kValidHexColorSizes[] = {3, 4, 6, 8};
21
22 // Returns whether |color_str| is a valid CSS color in hex format if we prepend
23 // '#', i.e., whether |color_str| matches /^[0-9A-Fa-f]{3,4,6,8}$/.
24 bool IsHexColorString(const std::string& color_str) {
25 size_t len = color_str.length();
26 const size_t* end = kValidHexColorSizes + arraysize(kValidHexColorSizes);
27 if (std::find(kValidHexColorSizes, end, len) == end)
28 return false;
29 for (auto ch : color_str) {
30 if (!base::IsHexDigit(ch))
31 return false;
32 }
33 return true;
34 }
35
36 } // namespace
37
38 namespace chrome {
39
40 ParsedFallbackIconPath::ParsedFallbackIconPath()
41 : size_in_pixels_(gfx::kFaviconSize) {
42 }
43
44 ParsedFallbackIconPath::~ParsedFallbackIconPath() {
45 }
46
47 bool ParsedFallbackIconPath::Parse(const std::string& path) {
48 if (path.empty())
49 return false;
50
51 size_t slash = path.find("/", 0);
52 if (slash == std::string::npos)
53 return false;
54 std::string spec_str = path.substr(0, slash);
55 if (!ParseSpecs(spec_str, &size_in_pixels_, &style_))
56 return false; // Parse failed.
57
58 // Need to store the index of the URL field, so Instant Extended can translate
59 // fallback icon URLs using advanced parameters.
60 // Example:
61 // "chrome-search://fallback-icon/48/<renderer-id>/<most-visited-id>"
62 // would be translated to:
63 // "chrome-search://fallback-icon/48/<most-visited-item-with-given-id>".
64 path_index_ = slash + 1;
65 url_string_ = path.substr(path_index_);
66 return true;
67 }
68
69 // static
70 bool ParsedFallbackIconPath::ParseSpecs(
71 const std::string& specs_str,
72 int *size,
73 favicon_base::FallbackIconStyle* style) {
74 DCHECK(size);
75 DCHECK(style);
76
77 std::vector<std::string> tokens = base::SplitString(
78 specs_str, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
79 if (tokens.size() != 5) // Force "," for empty fields.
80 return false;
81
82 *size = gfx::kFaviconSize;
83 if (!tokens[0].empty() && !base::StringToInt(tokens[0], size))
84 return false;
85 if (*size <= 0)
86 return false;
87
88 *style = favicon_base::FallbackIconStyle();
89
90 if (!tokens[1].empty()) {
91 style->is_default_background_color = false;
92 if (!ParseColor(tokens[1], &style->background_color))
93 return false;
94 }
95
96 if (tokens[2].empty())
97 favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(style);
98 else if (!ParseColor(tokens[2], &style->text_color))
99 return false;
100
101 if (!tokens[3].empty() &&
102 !base::StringToDouble(tokens[3], &style->font_size_ratio))
103 return false;
104
105 if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness))
106 return false;
107
108 return favicon_base::ValidateFallbackIconStyle(*style);
109 }
110
111 // static
112 bool ParsedFallbackIconPath::ParseColor(const std::string& color_str,
113 SkColor* color) {
114 DCHECK(color);
115 // Exclude the empty case. Also disallow the '#' prefix, since we want color
116 // to be part of an URL, but in URL '#' is used for ref fragment.
117 if (color_str.empty() || color_str[0] == '#')
118 return false;
119
120 // If a valid color hex string is given, prepend '#' and parse (always works).
121 // This is unambiguous since named color never only use leters 'a' to 'f'.
122 if (IsHexColorString(color_str)) {
123 // Default alpha to 0xFF since FindColor() preserves unspecified alpha.
124 *color = SK_ColorWHITE;
125 // Need temp variable to avoid use-after-free of returned pointer.
126 std::string color_str_with_hash = "#" + color_str;
127 const char* end = SkParse::FindColor(color_str_with_hash.c_str(), color);
128 DCHECK(end && !*end); // Call should succeed and consume string.
129 return true;
130 }
131
132 // Default alpha to 0xFF.
133 SkColor temp_color = SK_ColorWHITE;
134 const char* end = SkParse::FindColor(color_str.c_str(), &temp_color);
135 if (end && !*end) { // Successful if call succeeds and string is consumed.
136 *color = temp_color;
137 return true;
138 }
139 return false;
140 }
141
142 } // namespace chrome
OLDNEW
« no previous file with comments | « components/favicon_base/fallback_icon_url_parser.h ('k') | components/favicon_base/fallback_icon_url_parser_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698