OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/common/favicon/fallback_icon_url_parser.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_split.h" |
| 10 #include "third_party/skia/include/utils/SkParse.h" |
| 11 #include "ui/gfx/favicon_size.h" |
| 12 |
| 13 namespace chrome { |
| 14 |
| 15 ParsedFallbackIconPath::ParsedFallbackIconPath() |
| 16 : size_in_pixels_(gfx::kFaviconSize) { |
| 17 } |
| 18 |
| 19 ParsedFallbackIconPath::~ParsedFallbackIconPath() { |
| 20 } |
| 21 |
| 22 bool ParsedFallbackIconPath::Parse(const std::string& path) { |
| 23 if (path.empty()) |
| 24 return false; |
| 25 |
| 26 size_t slash = path.find("/", 0); |
| 27 if (slash == std::string::npos) |
| 28 return false; |
| 29 std::string spec_str = path.substr(0, slash); |
| 30 if (!ParseSpecs(spec_str, &size_in_pixels_, &style_)) |
| 31 return false; // Parse failed. |
| 32 |
| 33 // Extract URL, which may be empty (if first slash appears at the end). |
| 34 std::string url_str = path.substr(slash + 1); |
| 35 url_ = GURL(url_str); |
| 36 return url_str.empty() || url_.is_valid(); // Allow empty URL. |
| 37 } |
| 38 |
| 39 // static |
| 40 bool ParsedFallbackIconPath::ParseSpecs( |
| 41 const std::string& specs_str, |
| 42 int *size, |
| 43 favicon_base::FallbackIconStyle* style) { |
| 44 DCHECK(size); |
| 45 DCHECK(style); |
| 46 |
| 47 std::vector<std::string> tokens; |
| 48 base::SplitStringDontTrim(specs_str, ',', &tokens); |
| 49 if (tokens.size() != 5) // Force "," for empty fields. |
| 50 return false; |
| 51 |
| 52 *size = gfx::kFaviconSize; |
| 53 if (!tokens[0].empty() && !base::StringToInt(tokens[0], size)) |
| 54 return false; |
| 55 if (*size <= 0) |
| 56 return false; |
| 57 |
| 58 if (!tokens[1].empty() && !ParseColor(tokens[1], &style->background_color)) |
| 59 return false; |
| 60 |
| 61 if (tokens[2].empty()) |
| 62 favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(style); |
| 63 else if (!ParseColor(tokens[2], &style->text_color)) |
| 64 return false; |
| 65 |
| 66 if (!tokens[3].empty() && |
| 67 !base::StringToDouble(tokens[3], &style->font_size_ratio)) |
| 68 return false; |
| 69 |
| 70 if (!tokens[4].empty() && !base::StringToDouble(tokens[4], &style->roundness)) |
| 71 return false; |
| 72 |
| 73 return favicon_base::ValidateFallbackIconStyle(*style); |
| 74 } |
| 75 |
| 76 // static |
| 77 bool ParsedFallbackIconPath::ParseColor(const std::string& color_str, |
| 78 SkColor* color) { |
| 79 const char* end = SkParse::FindColor(color_str.c_str(), color); |
| 80 // Return true if FindColor() succeeds and |color_str| is entirely consumed. |
| 81 return end && !*end; |
| 82 } |
| 83 |
| 84 } // namespace chrome |
OLD | NEW |