| 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 "components/favicon_base/large_icon_url_parser.h" | |
| 6 | 5 |
| 7 #include "base/logging.h" | 6 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "components/favicon_base/fallback_icon_url_parser.h" |
| 11 #include "components/favicon_base/large_icon_url_parser.h" |
| 11 #include "third_party/skia/include/utils/SkParse.h" | 12 #include "third_party/skia/include/utils/SkParse.h" |
| 12 #include "ui/gfx/favicon_size.h" | 13 #include "ui/gfx/favicon_size.h" |
| 13 | 14 |
| 15 using chrome::ParsedFallbackIconPath; |
| 16 |
| 17 const char kFallbackIconParameter[] = "fallback/"; |
| 18 |
| 14 LargeIconUrlParser::LargeIconUrlParser() : size_in_pixels_(48) { | 19 LargeIconUrlParser::LargeIconUrlParser() : size_in_pixels_(48) { |
| 15 } | 20 } |
| 16 | 21 |
| 17 LargeIconUrlParser::~LargeIconUrlParser() { | 22 LargeIconUrlParser::~LargeIconUrlParser() { |
| 18 } | 23 } |
| 19 | 24 |
| 20 bool LargeIconUrlParser::Parse(base::StringPiece path) { | 25 bool LargeIconUrlParser::Parse(std::string path) { |
| 21 if (path.empty()) | 26 if (path.empty()) |
| 22 return false; | 27 return false; |
| 23 | 28 |
| 24 size_t slash = path.find("/", 0); // |path| does not start with '/'. | 29 size_t slash = path.find("/", 0); // |path| does not start with '/'. |
| 25 if (slash == base::StringPiece::npos) | 30 if (slash == std::string::npos) |
| 26 return false; | 31 return false; |
| 27 base::StringPiece size_str = path.substr(0, slash); | 32 |
| 33 std::string size_str = path.substr(0, slash); |
| 28 // Disallow empty, non-numeric, or non-positive sizes. | 34 // Disallow empty, non-numeric, or non-positive sizes. |
| 29 if (size_str.empty() || | 35 if (size_str.empty() || !base::StringToInt(size_str, &size_in_pixels_) || |
| 30 !base::StringToInt(size_str, &size_in_pixels_) || | |
| 31 size_in_pixels_ <= 0) | 36 size_in_pixels_ <= 0) |
| 32 return false; | 37 return false; |
| 33 | 38 |
| 39 path_index_ = slash + 1; |
| 40 // If the content of |kFallbackIconParameter| goes after the size, parse the |
| 41 // parameters of an fallback icon. Example: |
| 42 // 'chrome://large-icon/16/fallback/,777,FFF,0.625,0.4/http://google.com'. |
| 43 if (path.compare(path_index_, strlen(kFallbackIconParameter), |
| 44 kFallbackIconParameter) == 0) { |
| 45 path_index_ += strlen(kFallbackIconParameter); |
| 46 slash = path.find("/", path_index_); |
| 47 // The size of icon was set before. Ignore output of ParseSpecs. |
| 48 int ignored_icon_size; |
| 49 if (slash == std::string::npos || |
| 50 !ParsedFallbackIconPath::ParseSpecs( |
| 51 path.substr(path_index_, slash - path_index_), &ignored_icon_size, |
| 52 &fallback_icon_style_)) { |
| 53 return false; |
| 54 } |
| 55 path_index_ = slash + 1; |
| 56 } |
| 57 |
| 34 // Need to store the index of the URL field, so Instant Extended can translate | 58 // Need to store the index of the URL field, so Instant Extended can translate |
| 35 // large icon URLs using advanced parameters. | 59 // large icon URLs using advanced parameters. |
| 36 // Example: | 60 // Example: |
| 37 // "chrome-search://large-icon/48/<renderer-id>/<most-visited-id>" | 61 // "chrome-search://large-icon/48/<renderer-id>/<most-visited-id>" |
| 38 // would be translated to: | 62 // would be translated to: |
| 39 // "chrome-search://large-icon/48/<most-visited-item-with-given-id>". | 63 // "chrome-search://large-icon/48/<most-visited-item-with-given-id>". |
| 40 path_index_ = slash + 1; | 64 url_string_ = path.substr(path_index_); |
| 41 url_string_ = path.substr(path_index_).as_string(); | |
| 42 return true; | 65 return true; |
| 43 } | 66 } |
| OLD | NEW |