Chromium Code Reviews| 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) | |
|
James Hawkins
2015/03/09 20:27:38
This could be cleaner as:
std::vector<int> length
huangs
2015/03/09 20:44:56
I prefer to list all the numbers, but I guess havi
| |
| 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 DCHECK(color); |
| 80 // Return true if FindColor() succeeds and |color_str| is entirely consumed. | 97 // Exclude the empty case. Also disallow the '#' prefix, since we want color |
| 81 return end && !*end; | 98 // to be part of an URL, where '#' is reserved for ref fragment. |
| 99 if (color_str.empty() || color_str[0] == '#') | |
| 100 return false; | |
| 101 | |
| 102 // If a valid color hex string is given, prepend '#' and parse (always works). | |
| 103 // This is unambiguous since named color never only use leters 'a' to 'f'. | |
| 104 if (IsHexColorString(color_str)) { | |
| 105 // Force alpha = 0xFF since FindColor() preserves unspecified alpha. | |
| 106 *color = SK_ColorWHITE; | |
| 107 // Need temp variable to avoid use-after-free of returned pointer. | |
| 108 std::string color_str_with_hash = "#" + color_str; | |
| 109 const char* end = SkParse::FindColor(color_str_with_hash.c_str(), color); | |
| 110 DCHECK(end && !*end); // Ensure call succees and string is consumed. | |
|
James Hawkins
2015/03/09 20:27:38
nit: s/succees/succeeds/
huangs
2015/03/09 20:44:56
Done, also updated comments.
| |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 // Force alpha = 0xFF. | |
| 115 SkColor temp_color = SK_ColorWHITE; | |
| 116 const char* end = SkParse::FindColor(color_str.c_str(), &temp_color); | |
| 117 if (end && !*end) { // Successful if call succeeds and string is consumed. | |
| 118 *color = temp_color; | |
| 119 return true; | |
| 120 } | |
| 121 return false; | |
| 82 } | 122 } |
| 83 | 123 |
| 84 } // namespace chrome | 124 } // namespace chrome |
| OLD | NEW |