| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 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/favicon_url_parser.h" |
| 6 |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "chrome/common/favicon/favicon_types.h" |
| 9 #include "net/url_request/url_request.h" |
| 10 #include "ui/base/layout.h" |
| 11 #include "ui/gfx/favicon_size.h" |
| 12 #include "ui/webui/web_ui_util.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Parameters which can be used in chrome://favicon path. See file |
| 17 // "chrome/browser/ui/webui/favicon_source.h" for a description of |
| 18 // what each does. |
| 19 const char kIconURLParameter[] = "iconurl/"; |
| 20 const char kLargestParameter[] = "largest/"; |
| 21 const char kOriginParameter[] = "origin/"; |
| 22 const char kSizeParameter[] = "size/"; |
| 23 |
| 24 // Returns true if |search| is a substring of |path| which starts at |
| 25 // |start_index|. |
| 26 bool HasSubstringAt(const std::string& path, |
| 27 size_t start_index, |
| 28 const std::string& search) { |
| 29 return path.compare(start_index, search.length(), search) == 0; |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
| 34 namespace chrome { |
| 35 |
| 36 bool ParseFaviconPath(const std::string& path, |
| 37 int icon_types, |
| 38 ParsedFaviconPath* parsed) { |
| 39 parsed->is_icon_url = false; |
| 40 parsed->url = ""; |
| 41 parsed->size_in_dip = gfx::kSizeInDip; |
| 42 parsed->scale_factor = ui::SCALE_FACTOR_100P; |
| 43 parsed->path_index = -1; |
| 44 |
| 45 if (path.empty()) |
| 46 return false; |
| 47 |
| 48 size_t parsed_index = 0; |
| 49 if (HasSubstringAt(path, parsed_index, kLargestParameter)) { |
| 50 parsed_index += strlen(kLargestParameter); |
| 51 parsed->size_in_dip = 0; |
| 52 } else if (HasSubstringAt(path, parsed_index, kSizeParameter)) { |
| 53 parsed_index += strlen(kSizeParameter); |
| 54 |
| 55 size_t slash = path.find("/", parsed_index); |
| 56 if (slash == std::string::npos) |
| 57 return false; |
| 58 |
| 59 size_t scale_delimiter = path.find("@", parsed_index); |
| 60 std::string size_str; |
| 61 std::string scale_str; |
| 62 if (scale_delimiter == std::string::npos) { |
| 63 // Support the legacy size format of 'size/aa/' where 'aa' is the desired |
| 64 // size in DIP for the sake of not regressing the extensions which use it. |
| 65 size_str = path.substr(parsed_index, slash - parsed_index); |
| 66 } else { |
| 67 size_str = path.substr(parsed_index, scale_delimiter - parsed_index); |
| 68 scale_str = path.substr(scale_delimiter + 1, |
| 69 slash - scale_delimiter - 1); |
| 70 } |
| 71 |
| 72 if (!base::StringToInt(size_str, &parsed->size_in_dip)) |
| 73 return false; |
| 74 |
| 75 if (parsed->size_in_dip != gfx::kSizeInDip4x && |
| 76 parsed->size_in_dip != gfx::kSizeInDip2x) { |
| 77 // Only 64x64, 32x32 and 16x16 icons are supported. |
| 78 parsed->size_in_dip = gfx::kSizeInDip; |
| 79 } |
| 80 |
| 81 if (!scale_str.empty()) |
| 82 webui::ParseScaleFactor(scale_str, &parsed->scale_factor); |
| 83 |
| 84 // Return the default favicon (as opposed to a resized favicon) for |
| 85 // favicon sizes which are not cached by the favicon service. |
| 86 // Currently the favicon service caches: |
| 87 // - favicons of sizes "gfx::kSizeInDip * scale factor" px of type FAVICON |
| 88 // where scale factor is one of FaviconUtil::GetFaviconScaleFactors(). |
| 89 // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON |
| 90 if (parsed->size_in_dip != gfx::kSizeInDip && icon_types == chrome::FAVICON) |
| 91 return false; |
| 92 |
| 93 parsed_index = slash + 1; |
| 94 } |
| 95 |
| 96 if (HasSubstringAt(path, parsed_index, kIconURLParameter)) { |
| 97 parsed_index += strlen(kIconURLParameter); |
| 98 parsed->is_icon_url = true; |
| 99 parsed->url = path.substr(parsed_index); |
| 100 } else { |
| 101 // URL requests prefixed with "origin/" are converted to a form with an |
| 102 // empty path and a valid scheme. (e.g., example.com --> |
| 103 // http://example.com/ or http://example.com/a --> http://example.com/) |
| 104 if (HasSubstringAt(path, parsed_index, kOriginParameter)) { |
| 105 parsed_index += strlen(kOriginParameter); |
| 106 std::string possibly_invalid_url = path.substr(parsed_index); |
| 107 |
| 108 // If the URL does not specify a scheme (e.g., example.com instead of |
| 109 // http://example.com), add "http://" as a default. |
| 110 if (!GURL(possibly_invalid_url).has_scheme()) |
| 111 possibly_invalid_url = "http://" + possibly_invalid_url; |
| 112 |
| 113 // Strip the path beyond the top-level domain. |
| 114 parsed->url = GURL(possibly_invalid_url).GetOrigin().spec(); |
| 115 } else { |
| 116 parsed->url = path.substr(parsed_index); |
| 117 } |
| 118 } |
| 119 |
| 120 // The parsed index needs to be returned in order to allow Instant Extended |
| 121 // to translate favicon URLs using advanced parameters. |
| 122 // Example: |
| 123 // "chrome-search://favicon/size/16@2x/<renderer-id>/<most-visited-id>" |
| 124 // would be translated to: |
| 125 // "chrome-search://favicon/size/16@2x/<most-visited-item-with-given-id>". |
| 126 parsed->path_index = parsed_index; |
| 127 return true; |
| 128 } |
| 129 |
| 130 } // namespace chrome |
| OLD | NEW |