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" |
10 #include "third_party/skia/include/utils/SkParse.h" | 11 #include "third_party/skia/include/utils/SkParse.h" |
11 #include "ui/gfx/favicon_size.h" | 12 #include "ui/gfx/favicon_size.h" |
12 | 13 |
| 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 |
13 namespace chrome { | 30 namespace chrome { |
14 | 31 |
15 ParsedFallbackIconPath::ParsedFallbackIconPath() | 32 ParsedFallbackIconPath::ParsedFallbackIconPath() |
16 : size_in_pixels_(gfx::kFaviconSize) { | 33 : size_in_pixels_(gfx::kFaviconSize) { |
17 } | 34 } |
18 | 35 |
19 ParsedFallbackIconPath::~ParsedFallbackIconPath() { | 36 ParsedFallbackIconPath::~ParsedFallbackIconPath() { |
20 } | 37 } |
21 | 38 |
22 bool ParsedFallbackIconPath::Parse(const std::string& path) { | 39 bool ParsedFallbackIconPath::Parse(const std::string& path) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 86 |
70 if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness)) | 87 if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness)) |
71 return false; | 88 return false; |
72 | 89 |
73 return favicon_base::ValidateFallbackIconStyle(*style); | 90 return favicon_base::ValidateFallbackIconStyle(*style); |
74 } | 91 } |
75 | 92 |
76 // static | 93 // static |
77 bool ParsedFallbackIconPath::ParseColor(const std::string& color_str, | 94 bool ParsedFallbackIconPath::ParseColor(const std::string& color_str, |
78 SkColor* color) { | 95 SkColor* color) { |
79 const char* end = SkParse::FindColor(color_str.c_str(), color); | 96 // Exclude empty case. Also disallow '#' prefix since '#' is used to specify |
80 // Return true if FindColor() succeeds and |color_str| is entirely consumed. | 97 // ref fragment in an URL, and so we want to avoid using it. |
81 return end && !*end; | 98 if (color_str.empty() || color_str[0] == '#') |
| 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; |
82 } | 113 } |
83 | 114 |
84 } // namespace chrome | 115 } // namespace chrome |
OLD | NEW |