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/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 // Default size of the favicon. | |
| 25 const int kFaviconSizeInDip = gfx::kFaviconSize; | |
| 26 | |
| 27 // 2x size of the favicon. | |
| 28 const int kFaviconSizeInDip2x = gfx::kFaviconSize * 2; | |
| 29 | |
| 30 // 4x size of the favicon. | |
| 31 const int kFaviconSizeInDip4x = gfx::kFaviconSize * 4; | |
| 32 | |
| 33 // Returns true if |search| is a substring of |path| which starts at | |
| 34 // |start_index|. | |
| 35 bool HasSubstringAt(const std::string& path, | |
|
James Hawkins
2013/07/02 23:23:04
This isn't available somewhere in base already?
pedro (no code reviews)
2013/07/03 00:16:56
Does it have a different name? I would be glad to
James Hawkins
2013/07/03 16:44:45
I don't know; I'm asking if you've looked. base/s
pedro (no code reviews)
2013/07/03 21:28:14
As I stated in a previous comment, I've looked for
| |
| 36 size_t start_index, | |
| 37 const std::string& search) { | |
| 38 return path.compare(start_index, search.length(), search) == 0; | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 namespace chrome { | |
| 44 | |
| 45 bool ParseFaviconPath(const std::string& path, | |
| 46 int icon_types, | |
| 47 ParsedFaviconPath* parsed) { | |
| 48 parsed->is_icon_url = false; | |
| 49 parsed->url = ""; | |
| 50 parsed->size_in_dip = kFaviconSizeInDip; | |
| 51 parsed->scale_factor = ui::SCALE_FACTOR_100P; | |
| 52 parsed->path_index = -1; | |
| 53 | |
| 54 if (path.empty()) | |
| 55 return false; | |
| 56 | |
| 57 size_t parsed_index = 0; | |
| 58 if (HasSubstringAt(path, parsed_index, kLargestParameter)) { | |
| 59 parsed_index += strlen(kLargestParameter); | |
| 60 parsed->size_in_dip = 0; | |
| 61 } else if (HasSubstringAt(path, parsed_index, kSizeParameter)) { | |
| 62 parsed_index += strlen(kSizeParameter); | |
| 63 | |
| 64 size_t slash = path.find("/", parsed_index); | |
| 65 if (slash == std::string::npos) | |
| 66 return false; | |
| 67 | |
| 68 size_t scale_delimiter = path.find("@", parsed_index); | |
| 69 std::string size_str; | |
| 70 std::string scale_str; | |
| 71 if (scale_delimiter == std::string::npos) { | |
| 72 // Support the legacy size format of 'size/aa/' where 'aa' is the desired | |
| 73 // size in DIP for the sake of not regressing the extensions which use it. | |
| 74 size_str = path.substr(parsed_index, slash - parsed_index); | |
| 75 } else { | |
| 76 size_str = path.substr(parsed_index, scale_delimiter - parsed_index); | |
| 77 scale_str = path.substr(scale_delimiter + 1, | |
| 78 slash - scale_delimiter - 1); | |
| 79 } | |
| 80 | |
| 81 if (!base::StringToInt(size_str, &parsed->size_in_dip)) | |
| 82 return false; | |
| 83 | |
| 84 if (parsed->size_in_dip != kFaviconSizeInDip4x && | |
| 85 parsed->size_in_dip != kFaviconSizeInDip2x) { | |
| 86 // Only 64x64, 32x32 and 16x16 icons are supported. | |
| 87 parsed->size_in_dip = kFaviconSizeInDip; | |
| 88 } | |
| 89 | |
| 90 if (!scale_str.empty()) | |
| 91 webui::ParseScaleFactor(scale_str, &parsed->scale_factor); | |
| 92 | |
| 93 // Return the default favicon (as opposed to a resized favicon) for | |
| 94 // favicon sizes which are not cached by the favicon service. | |
| 95 // Currently the favicon service caches: | |
| 96 // - favicons of sizes "kFaviconSizeInDip * scale factor" px of type FAVICON | |
| 97 // where scale factor is one of FaviconUtil::GetFaviconScaleFactors(). | |
| 98 // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON | |
| 99 if (parsed->size_in_dip != kFaviconSizeInDip && | |
| 100 icon_types == chrome::FAVICON) | |
| 101 return false; | |
| 102 | |
| 103 parsed_index = slash + 1; | |
| 104 } | |
| 105 | |
| 106 if (HasSubstringAt(path, parsed_index, kIconURLParameter)) { | |
| 107 parsed_index += strlen(kIconURLParameter); | |
| 108 parsed->is_icon_url = true; | |
| 109 parsed->url = path.substr(parsed_index); | |
| 110 } else { | |
| 111 // URL requests prefixed with "origin/" are converted to a form with an | |
| 112 // empty path and a valid scheme. (e.g., example.com --> | |
| 113 // http://example.com/ or http://example.com/a --> http://example.com/) | |
| 114 if (HasSubstringAt(path, parsed_index, kOriginParameter)) { | |
| 115 parsed_index += strlen(kOriginParameter); | |
| 116 std::string possibly_invalid_url = path.substr(parsed_index); | |
| 117 | |
| 118 // If the URL does not specify a scheme (e.g., example.com instead of | |
| 119 // http://example.com), add "http://" as a default. | |
| 120 if (!GURL(possibly_invalid_url).has_scheme()) | |
| 121 possibly_invalid_url = "http://" + possibly_invalid_url; | |
| 122 | |
| 123 // Strip the path beyond the top-level domain. | |
| 124 parsed->url = GURL(possibly_invalid_url).GetOrigin().spec(); | |
| 125 } else { | |
| 126 parsed->url = path.substr(parsed_index); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 // The parsed index needs to be returned in order to allow Instant Extended | |
| 131 // to translate favicon URLs using advanced parameters. | |
| 132 // Example: | |
| 133 // "chrome-search://favicon/size/16@2x/<renderer-id>/<most-visited-id>" | |
| 134 // would be translated to: | |
| 135 // "chrome-search://favicon/size/16@2x/<most-visited-item-with-given-id>". | |
| 136 parsed->path_index = parsed_index; | |
| 137 return true; | |
| 138 } | |
| 139 | |
| 140 } // namespace chrome | |
| OLD | NEW |