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 bool IsHexColorString(const std::string& color_str) { | |
James Hawkins
2015/03/06 22:24:52
Please add documentation about the conditions whic
huangs
2015/03/06 22:39:48
Done.
| |
17 size_t len = color_str.length(); | |
18 if (len != 3 && len != 4 && len != 6 && len != 8) | |
19 return false; | |
20 for (auto ch : color_str) | |
21 if (!IsHexDigit(ch)) | |
22 return false; | |
23 return true; | |
24 } | |
25 | |
26 } // namespace | |
27 | |
13 namespace chrome { | 28 namespace chrome { |
14 | 29 |
15 ParsedFallbackIconPath::ParsedFallbackIconPath() | 30 ParsedFallbackIconPath::ParsedFallbackIconPath() |
16 : size_in_pixels_(gfx::kFaviconSize) { | 31 : size_in_pixels_(gfx::kFaviconSize) { |
17 } | 32 } |
18 | 33 |
19 ParsedFallbackIconPath::~ParsedFallbackIconPath() { | 34 ParsedFallbackIconPath::~ParsedFallbackIconPath() { |
20 } | 35 } |
21 | 36 |
22 bool ParsedFallbackIconPath::Parse(const std::string& path) { | 37 bool ParsedFallbackIconPath::Parse(const std::string& path) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 | 84 |
70 if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness)) | 85 if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness)) |
71 return false; | 86 return false; |
72 | 87 |
73 return favicon_base::ValidateFallbackIconStyle(*style); | 88 return favicon_base::ValidateFallbackIconStyle(*style); |
74 } | 89 } |
75 | 90 |
76 // static | 91 // static |
77 bool ParsedFallbackIconPath::ParseColor(const std::string& color_str, | 92 bool ParsedFallbackIconPath::ParseColor(const std::string& color_str, |
78 SkColor* color) { | 93 SkColor* color) { |
79 const char* end = SkParse::FindColor(color_str.c_str(), color); | 94 // Exclude empty case and disallow '#' prefix since it messes up URL. |
James Hawkins
2015/03/06 22:24:52
Please clarify 'messes up URL'.
huangs
2015/03/06 22:39:48
Done. Also note that escaping '#' as "%23" would b
| |
80 // Return true if FindColor() succeeds and |color_str| is entirely consumed. | 95 if (color_str.empty() || color_str[0] == '#') |
81 return end && !*end; | 96 return false; |
97 // Force alpha = 0xFF since SkParse::FindColor() preserves unspecified alpha. | |
98 SkColor temp_color = SK_ColorWHITE; | |
99 // Prepend '#' if color hex string is given. This is unambiguous because no | |
100 // named color consists of leters 'a' to 'f' only. | |
101 const char* end = IsHexColorString(color_str) ? | |
102 SkParse::FindColor(("#" + color_str).c_str(), &temp_color) : | |
103 SkParse::FindColor(color_str.c_str(), &temp_color); | |
104 // Successful if FindColor() succeeds and |color_str| is completely consumed. | |
105 if (end && !*end) { | |
106 *color = temp_color; | |
107 return true; | |
108 } | |
109 return false; | |
82 } | 110 } |
83 | 111 |
84 } // namespace chrome | 112 } // namespace chrome |
OLD | NEW |