OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/favicon/fallback_icon_url_parser.h" | 5 #include "chrome/common/favicon/fallback_icon_url_parser.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/string_util.h" | |
11 #include "third_party/skia/include/utils/SkParse.h" | 10 #include "third_party/skia/include/utils/SkParse.h" |
12 #include "ui/gfx/favicon_size.h" | 11 #include "ui/gfx/favicon_size.h" |
13 | 12 |
14 namespace { | |
15 | |
16 // Returns whether |color_str| is a valid CSS color in hex format if we prepend | |
17 // '#', i.e., whether |color_str| matches /^[0-9A-Fa-f]{3,4,6,8}$/. | |
18 bool IsHexColorString(const std::string& color_str) { | |
19 size_t len = color_str.length(); | |
20 if (len != 3 && len != 4 && len != 6 && len != 8) | |
21 return false; | |
22 for (auto ch : color_str) | |
23 if (!IsHexDigit(ch)) | |
24 return false; | |
25 return true; | |
26 } | |
27 | |
28 } // namespace | |
29 | |
30 namespace chrome { | 13 namespace chrome { |
31 | 14 |
32 ParsedFallbackIconPath::ParsedFallbackIconPath() | 15 ParsedFallbackIconPath::ParsedFallbackIconPath() |
33 : size_in_pixels_(gfx::kFaviconSize) { | 16 : size_in_pixels_(gfx::kFaviconSize) { |
34 } | 17 } |
35 | 18 |
36 ParsedFallbackIconPath::~ParsedFallbackIconPath() { | 19 ParsedFallbackIconPath::~ParsedFallbackIconPath() { |
37 } | 20 } |
38 | 21 |
39 bool ParsedFallbackIconPath::Parse(const std::string& path) { | 22 bool ParsedFallbackIconPath::Parse(const std::string& path) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 69 |
87 if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness)) | 70 if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness)) |
88 return false; | 71 return false; |
89 | 72 |
90 return favicon_base::ValidateFallbackIconStyle(*style); | 73 return favicon_base::ValidateFallbackIconStyle(*style); |
91 } | 74 } |
92 | 75 |
93 // static | 76 // static |
94 bool ParsedFallbackIconPath::ParseColor(const std::string& color_str, | 77 bool ParsedFallbackIconPath::ParseColor(const std::string& color_str, |
95 SkColor* color) { | 78 SkColor* color) { |
96 // Exclude empty case. Also disallow '#' prefix since '#' is used to specify | 79 const char* end = SkParse::FindColor(color_str.c_str(), color); |
97 // ref fragment in an URL, and so we want to avoid using it. | 80 // Return true if FindColor() succeeds and |color_str| is entirely consumed. |
98 if (color_str.empty() || color_str[0] == '#') | 81 return end && !*end; |
99 return false; | |
100 // Force alpha = 0xFF since SkParse::FindColor() preserves unspecified alpha. | |
101 SkColor temp_color = SK_ColorWHITE; | |
102 // Prepend '#' if color hex string is given. This is unambiguous because no | |
103 // named color consists of leters 'a' to 'f' only. | |
104 const char* end = IsHexColorString(color_str) ? | |
105 SkParse::FindColor(("#" + color_str).c_str(), &temp_color) : | |
106 SkParse::FindColor(color_str.c_str(), &temp_color); | |
107 // Successful if FindColor() succeeds and |color_str| is completely consumed. | |
108 if (end && !*end) { | |
109 *color = temp_color; | |
110 return true; | |
111 } | |
112 return false; | |
113 } | 82 } |
114 | 83 |
115 } // namespace chrome | 84 } // namespace chrome |
OLD | NEW |