Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
|
sreeram
2013/06/04 22:01:56
No "(c)".
pedro (no code reviews)
2013/06/07 23:34:21
Done.
| |
| 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 .h file for a | |
| 17 // description of what each does. | |
|
sreeram
2013/06/04 22:01:56
The .h file doesn't actually give any description.
pedro (no code reviews)
2013/06/07 23:34:21
Done.
| |
| 18 const char kIconURLParameter[] = "iconurl/"; | |
| 19 const char kLargestParameter[] = "largest/"; | |
| 20 const char kOriginParameter[] = "origin/"; | |
| 21 const char kSizeParameter[] = "size/"; | |
| 22 | |
| 23 // Returns true if |search| is a substring of |path| which starts at | |
| 24 // |start_index|. | |
| 25 bool HasSubstringAt(const std::string& path, | |
| 26 size_t start_index, | |
| 27 const std::string& search) { | |
| 28 if (search.empty()) | |
| 29 return false; | |
| 30 | |
| 31 if (start_index + search.size() >= path.size()) | |
| 32 return false; | |
| 33 | |
| 34 return (path.compare(start_index, search.size(), search) == 0); | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 namespace chrome { | |
| 40 | |
| 41 // TODO make url string | |
|
sreeram
2013/06/04 22:01:56
Remove this?
pedro (no code reviews)
2013/06/07 23:34:21
Done.
| |
| 42 | |
| 43 bool ParseFaviconPath(const std::string& path, | |
| 44 bool supports_origin_parameter, | |
| 45 int icon_types, | |
| 46 bool* is_icon_url, | |
| 47 std::string* url, | |
| 48 int* size_in_dip, | |
| 49 ui::ScaleFactor* scale_factor, | |
| 50 std::string* params) { | |
|
sreeram
2013/06/04 22:01:56
Same alignment issue as in the .h file.
pedro (no code reviews)
2013/06/07 23:34:21
Done.
| |
| 51 DCHECK_EQ(16, gfx::kFaviconSize); | |
| 52 | |
| 53 *is_icon_url = false; | |
| 54 *params = ""; | |
|
sreeram
2013/06/04 22:01:56
Move this to below line 57, to keep the same order
pedro (no code reviews)
2013/06/07 23:34:21
Done.
| |
| 55 *url = ""; | |
| 56 *size_in_dip = 16; | |
| 57 *scale_factor = ui::SCALE_FACTOR_100P; | |
| 58 | |
| 59 if (path.empty()) | |
| 60 return false; | |
| 61 | |
| 62 size_t parsed_index = 0; | |
| 63 if (HasSubstringAt(path, parsed_index, kLargestParameter)) { | |
| 64 parsed_index += strlen(kLargestParameter); | |
| 65 *size_in_dip = 0; | |
| 66 } else if (HasSubstringAt(path, parsed_index, kSizeParameter)) { | |
| 67 parsed_index += strlen(kSizeParameter); | |
| 68 | |
| 69 size_t slash = path.find("/", parsed_index); | |
| 70 if (slash == std::string::npos) | |
| 71 return false; | |
| 72 | |
| 73 size_t scale_delimiter = path.find("@", parsed_index); | |
| 74 std::string size_str; | |
| 75 std::string scale_str; | |
| 76 if (scale_delimiter == std::string::npos) { | |
| 77 // Support the legacy size format of 'size/aa/' where 'aa' is the desired | |
| 78 // size in DIP for the sake of not regressing the extensions which use it. | |
| 79 size_str = path.substr(parsed_index, slash - parsed_index); | |
| 80 } else { | |
| 81 size_str = path.substr(parsed_index, scale_delimiter - parsed_index); | |
| 82 scale_str = path.substr(scale_delimiter + 1, | |
| 83 slash - scale_delimiter - 1); | |
| 84 } | |
| 85 | |
| 86 if (!base::StringToInt(size_str, size_in_dip)) | |
| 87 return false; | |
| 88 | |
| 89 if (*size_in_dip != 64 && *size_in_dip != 32) { | |
| 90 // Only 64x64, 32x32 and 16x16 icons are supported. | |
| 91 *size_in_dip = 16; | |
| 92 } | |
| 93 | |
| 94 if (!scale_str.empty()) | |
| 95 webui::ParseScaleFactor(scale_str, scale_factor); | |
| 96 | |
| 97 // Return the default favicon (as opposed to a resized favicon) for | |
| 98 // favicon sizes which are not cached by the favicon service. | |
| 99 // Currently the favicon service caches: | |
| 100 // - favicons of sizes "16 * scale factor" px of type FAVICON | |
| 101 // where scale factor is one of FaviconUtil::GetFaviconScaleFactors(). | |
| 102 // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON | |
| 103 if (*size_in_dip != 16 && icon_types == chrome::FAVICON) | |
| 104 return false; | |
| 105 | |
| 106 parsed_index = slash + 1; | |
| 107 } | |
| 108 | |
| 109 if (HasSubstringAt(path, parsed_index, kIconURLParameter)) { | |
| 110 parsed_index += strlen(kIconURLParameter); | |
| 111 *is_icon_url = true; | |
| 112 *url = path.substr(parsed_index); | |
| 113 } else { | |
| 114 // URL requests prefixed with "origin/" are converted to a form with an | |
| 115 // empty path and a valid scheme. (e.g., example.com --> | |
| 116 // http://example.com/ or http://example.com/a --> http://example.com/) | |
| 117 if (HasSubstringAt(path, parsed_index, kOriginParameter)) { | |
| 118 // The format of favicon URLs used in Instant Extended does not support | |
| 119 // the origin parameter. | |
| 120 if (!supports_origin_parameter) | |
| 121 return false; | |
| 122 | |
| 123 parsed_index += strlen(kOriginParameter); | |
| 124 std::string possibly_invalid_url = path.substr(parsed_index); | |
| 125 | |
| 126 // If the URL does not specify a scheme (e.g., example.com instead of | |
| 127 // http://example.com), add "http://" as a default. | |
| 128 if (!GURL(possibly_invalid_url).has_scheme()) | |
| 129 possibly_invalid_url = "http://" + possibly_invalid_url; | |
| 130 | |
| 131 // Strip the path beyond the top-level domain. | |
| 132 *url = GURL(possibly_invalid_url).GetOrigin().spec(); | |
| 133 } else { | |
| 134 *url = path.substr(parsed_index); | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 // The favicon parameters part of the path needs to be returned in order | |
| 139 // to allow Instant Extended to translate favicon URLs using such parameters. | |
| 140 // Example: "chrome-search://favicon/size/16@2x/1234" would be translated to | |
| 141 // "chrome-search://favicon/size/16@2x/<most-visited-item-with-id-1234>". | |
| 142 // The value of |params| in this case would be "size/16@2x/" (without the | |
| 143 // leading slash). | |
| 144 *params = path.substr(0, parsed_index); | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 148 } // namespace chrome | |
| OLD | NEW |