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 <algorithm> | |
| 8 | |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | |
| 8 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/string_util.h" | |
| 10 #include "third_party/skia/include/utils/SkParse.h" | 14 #include "third_party/skia/include/utils/SkParse.h" |
| 11 #include "ui/gfx/favicon_size.h" | 15 #include "ui/gfx/favicon_size.h" |
| 12 | 16 |
| 17 namespace { | |
| 18 | |
| 19 // List of sizes corresponding to RGB, ARGB, RRGGBB, AARRGGBB. | |
| 20 size_t kValidHexColorSizes[] = {3, 4, 6, 8}; | |
|
James Hawkins
2015/03/09 21:06:48
nit: const
huangs
2015/03/09 21:11:54
Done.
| |
| 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 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 (!IsHexDigit(ch)) | |
| 31 return false; | |
| 32 return true; | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 13 namespace chrome { | 37 namespace chrome { |
| 14 | 38 |
| 15 ParsedFallbackIconPath::ParsedFallbackIconPath() | 39 ParsedFallbackIconPath::ParsedFallbackIconPath() |
| 16 : size_in_pixels_(gfx::kFaviconSize) { | 40 : size_in_pixels_(gfx::kFaviconSize) { |
| 17 } | 41 } |
| 18 | 42 |
| 19 ParsedFallbackIconPath::~ParsedFallbackIconPath() { | 43 ParsedFallbackIconPath::~ParsedFallbackIconPath() { |
| 20 } | 44 } |
| 21 | 45 |
| 22 bool ParsedFallbackIconPath::Parse(const std::string& path) { | 46 bool ParsedFallbackIconPath::Parse(const std::string& path) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 | 93 |
| 70 if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness)) | 94 if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness)) |
| 71 return false; | 95 return false; |
| 72 | 96 |
| 73 return favicon_base::ValidateFallbackIconStyle(*style); | 97 return favicon_base::ValidateFallbackIconStyle(*style); |
| 74 } | 98 } |
| 75 | 99 |
| 76 // static | 100 // static |
| 77 bool ParsedFallbackIconPath::ParseColor(const std::string& color_str, | 101 bool ParsedFallbackIconPath::ParseColor(const std::string& color_str, |
| 78 SkColor* color) { | 102 SkColor* color) { |
| 79 const char* end = SkParse::FindColor(color_str.c_str(), color); | 103 DCHECK(color); |
| 80 // Return true if FindColor() succeeds and |color_str| is entirely consumed. | 104 // Exclude the empty case. Also disallow the '#' prefix, since we want color |
| 81 return end && !*end; | 105 // to be part of an URL, where '#' is used for ref fragment. |
| 106 if (color_str.empty() || color_str[0] == '#') | |
| 107 return false; | |
| 108 | |
| 109 // If a valid color hex string is given, prepend '#' and parse (always works). | |
| 110 // This is unambiguous since named color never only use leters 'a' to 'f'. | |
| 111 if (IsHexColorString(color_str)) { | |
| 112 // Force alpha to be 0xFF since FindColor() preserves unspecified alpha. | |
| 113 *color = SK_ColorWHITE; | |
| 114 // Need temp variable to avoid use-after-free of returned pointer. | |
| 115 std::string color_str_with_hash = "#" + color_str; | |
| 116 const char* end = SkParse::FindColor(color_str_with_hash.c_str(), color); | |
| 117 DCHECK(end && !*end); // Call should succeed and consume string. | |
| 118 return true; | |
| 119 } | |
| 120 | |
| 121 // Force alpha to be 0xFF. | |
| 122 SkColor temp_color = SK_ColorWHITE; | |
| 123 const char* end = SkParse::FindColor(color_str.c_str(), &temp_color); | |
| 124 if (end && !*end) { // Successful if call succeeds and string is consumed. | |
| 125 *color = temp_color; | |
| 126 return true; | |
| 127 } | |
| 128 return false; | |
| 82 } | 129 } |
| 83 | 130 |
| 84 } // namespace chrome | 131 } // namespace chrome |
| OLD | NEW |