Chromium Code Reviews| 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_url_parser.h" | |
| 6 | |
| 7 #include "base/strings/string_number_conversions.h" | |
| 8 #include "chrome/common/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 if (search.empty()) | |
| 30 return false; | |
| 31 | |
| 32 if (start_index + search.size() >= path.size()) | |
| 33 return false; | |
| 34 | |
| 35 return (path.compare(start_index, search.size(), search) == 0); | |
| 36 } | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 namespace chrome { | |
| 41 | |
| 42 bool ParseFaviconPath(const std::string& path, | |
|
samarth
2013/06/14 21:54:46
Yikes! Are there no tests for this? Please add so
pedro (no code reviews)
2013/06/18 22:35:13
Done.
| |
| 43 bool supports_origin_parameter, | |
| 44 int icon_types, | |
| 45 bool* is_icon_url, | |
|
samarth
2013/06/14 21:54:46
Rather than taking in a slew of output params, how
pedro (no code reviews)
2013/06/18 22:35:13
I wasn't happy with it too. Thanks for the suggest
| |
| 46 std::string* url, | |
| 47 int* size_in_dip, | |
| 48 ui::ScaleFactor* scale_factor, | |
| 49 std::string* params) { | |
| 50 DCHECK_EQ(16, gfx::kFaviconSize); | |
| 51 | |
| 52 *is_icon_url = false; | |
| 53 *url = ""; | |
| 54 *size_in_dip = 16; | |
| 55 *scale_factor = ui::SCALE_FACTOR_100P; | |
| 56 *params = ""; | |
| 57 | |
| 58 if (path.empty()) | |
| 59 return false; | |
| 60 | |
| 61 size_t parsed_index = 0; | |
| 62 if (HasSubstringAt(path, parsed_index, kLargestParameter)) { | |
| 63 parsed_index += strlen(kLargestParameter); | |
| 64 *size_in_dip = 0; | |
| 65 } else if (HasSubstringAt(path, parsed_index, kSizeParameter)) { | |
| 66 parsed_index += strlen(kSizeParameter); | |
| 67 | |
| 68 size_t slash = path.find("/", parsed_index); | |
| 69 if (slash == std::string::npos) | |
| 70 return false; | |
| 71 | |
| 72 size_t scale_delimiter = path.find("@", parsed_index); | |
| 73 std::string size_str; | |
| 74 std::string scale_str; | |
| 75 if (scale_delimiter == std::string::npos) { | |
| 76 // Support the legacy size format of 'size/aa/' where 'aa' is the desired | |
| 77 // size in DIP for the sake of not regressing the extensions which use it. | |
| 78 size_str = path.substr(parsed_index, slash - parsed_index); | |
| 79 } else { | |
| 80 size_str = path.substr(parsed_index, scale_delimiter - parsed_index); | |
| 81 scale_str = path.substr(scale_delimiter + 1, | |
| 82 slash - scale_delimiter - 1); | |
| 83 } | |
| 84 | |
| 85 if (!base::StringToInt(size_str, size_in_dip)) | |
| 86 return false; | |
| 87 | |
| 88 if (*size_in_dip != 64 && *size_in_dip != 32) { | |
| 89 // Only 64x64, 32x32 and 16x16 icons are supported. | |
| 90 *size_in_dip = 16; | |
| 91 } | |
| 92 | |
| 93 if (!scale_str.empty()) | |
| 94 webui::ParseScaleFactor(scale_str, scale_factor); | |
| 95 | |
| 96 // Return the default favicon (as opposed to a resized favicon) for | |
| 97 // favicon sizes which are not cached by the favicon service. | |
| 98 // Currently the favicon service caches: | |
| 99 // - favicons of sizes "16 * scale factor" px of type FAVICON | |
| 100 // where scale factor is one of FaviconUtil::GetFaviconScaleFactors(). | |
| 101 // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON | |
| 102 if (*size_in_dip != 16 && icon_types == chrome::FAVICON) | |
| 103 return false; | |
| 104 | |
| 105 parsed_index = slash + 1; | |
| 106 } | |
| 107 | |
| 108 if (HasSubstringAt(path, parsed_index, kIconURLParameter)) { | |
| 109 parsed_index += strlen(kIconURLParameter); | |
| 110 *is_icon_url = true; | |
| 111 *url = path.substr(parsed_index); | |
| 112 } else { | |
| 113 // URL requests prefixed with "origin/" are converted to a form with an | |
| 114 // empty path and a valid scheme. (e.g., example.com --> | |
| 115 // http://example.com/ or http://example.com/a --> http://example.com/) | |
| 116 if (HasSubstringAt(path, parsed_index, kOriginParameter)) { | |
| 117 // The format of favicon URLs used in Instant Extended does not support | |
|
samarth
2013/06/14 21:54:46
We don't use it today. What's wrong with leaving s
pedro (no code reviews)
2013/06/18 22:35:13
Removed the |supports_origin_parameter|.
| |
| 118 // the origin parameter. | |
| 119 if (!supports_origin_parameter) | |
| 120 return false; | |
| 121 | |
| 122 parsed_index += strlen(kOriginParameter); | |
| 123 std::string possibly_invalid_url = path.substr(parsed_index); | |
| 124 | |
| 125 // If the URL does not specify a scheme (e.g., example.com instead of | |
| 126 // http://example.com), add "http://" as a default. | |
| 127 if (!GURL(possibly_invalid_url).has_scheme()) | |
| 128 possibly_invalid_url = "http://" + possibly_invalid_url; | |
| 129 | |
| 130 // Strip the path beyond the top-level domain. | |
| 131 *url = GURL(possibly_invalid_url).GetOrigin().spec(); | |
| 132 } else { | |
| 133 *url = path.substr(parsed_index); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 // The favicon parameters need to be returned in order to allow Instant | |
|
samarth
2013/06/14 21:54:46
I find this confusing. I think it would be more na
pedro (no code reviews)
2013/06/18 22:35:13
Done.
| |
| 138 // Extended to translate favicon URLs using such parameters. | |
| 139 // Example: "chrome-search://favicon/size/16@2x/1234" would be translated to | |
| 140 // "chrome-search://favicon/size/16@2x/<most-visited-item-with-id-1234>". | |
| 141 // The value of |params| in this case would be "size/16@2x/" (without the | |
| 142 // leading slash). | |
| 143 *params = path.substr(0, parsed_index); | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 147 } // namespace chrome | |
| OLD | NEW |