| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/common/favicon/favicon_url_parser.h" | 5 #include "chrome/common/favicon/favicon_url_parser.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "components/favicon_base/favicon_types.h" | 8 #include "components/favicon_base/favicon_types.h" |
| 9 #include "net/url_request/url_request.h" | 9 #include "net/url_request/url_request.h" |
| 10 #include "ui/base/webui/web_ui_util.h" | 10 #include "ui/base/webui/web_ui_util.h" |
| 11 #include "ui/gfx/favicon_size.h" | 11 #include "ui/gfx/favicon_size.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 // Parameters which can be used in chrome://favicon path. See file | 15 // Parameters which can be used in chrome://favicon path. See file |
| 16 // "chrome/browser/ui/webui/favicon_source.h" for a description of | 16 // "chrome/browser/ui/webui/favicon_source.h" for a description of |
| 17 // what each does. | 17 // what each does. |
| 18 const char kFallbackParameter[] = "fallback/"; |
| 18 const char kIconURLParameter[] = "iconurl/"; | 19 const char kIconURLParameter[] = "iconurl/"; |
| 19 const char kLargestParameter[] = "largest/"; | 20 const char kLargestParameter[] = "largest/"; |
| 20 const char kOriginParameter[] = "origin/"; | 21 const char kOriginParameter[] = "origin/"; |
| 21 const char kSizeParameter[] = "size/"; | 22 const char kSizeParameter[] = "size/"; |
| 22 | 23 |
| 23 // Returns true if |search| is a substring of |path| which starts at | 24 // Returns true if |search| is a substring of |path| which starts at |
| 24 // |start_index|. | 25 // |start_index|. |
| 25 bool HasSubstringAt(const std::string& path, | 26 bool HasSubstringAt(const std::string& path, |
| 26 size_t start_index, | 27 size_t start_index, |
| 27 const std::string& search) { | 28 const std::string& search) { |
| 28 return path.compare(start_index, search.length(), search) == 0; | 29 return path.compare(start_index, search.length(), search) == 0; |
| 29 } | 30 } |
| 30 | 31 |
| 31 } // namespace | 32 } // namespace |
| 32 | 33 |
| 33 namespace chrome { | 34 namespace chrome { |
| 34 | 35 |
| 35 bool ParseFaviconPath(const std::string& path, | 36 bool ParseFaviconPath(const std::string& path, |
| 36 int icon_types, | 37 int icon_types, |
| 37 ParsedFaviconPath* parsed) { | 38 ParsedFaviconPath* parsed) { |
| 38 parsed->is_icon_url = false; | 39 parsed->is_icon_url = false; |
| 39 parsed->url = ""; | 40 parsed->url = ""; |
| 40 parsed->size_in_dip = gfx::kFaviconSize; | 41 parsed->size_in_dip = gfx::kFaviconSize; |
| 41 parsed->device_scale_factor = 1.0f; | 42 parsed->device_scale_factor = 1.0f; |
| 42 parsed->path_index = std::string::npos; | 43 parsed->path_index = std::string::npos; |
| 44 parsed->fallback_specs_builder.reset(nullptr); |
| 43 | 45 |
| 44 if (path.empty()) | 46 if (path.empty()) |
| 45 return false; | 47 return false; |
| 46 | 48 |
| 47 size_t parsed_index = 0; | 49 size_t parsed_index = 0; |
| 48 if (HasSubstringAt(path, parsed_index, kLargestParameter)) { | 50 if (HasSubstringAt(path, parsed_index, kLargestParameter)) { |
| 49 parsed_index += strlen(kLargestParameter); | 51 parsed_index += strlen(kLargestParameter); |
| 50 parsed->size_in_dip = 0; | 52 parsed->size_in_dip = 0; |
| 51 } else if (HasSubstringAt(path, parsed_index, kSizeParameter)) { | 53 } else if (HasSubstringAt(path, parsed_index, kSizeParameter)) { |
| 52 parsed_index += strlen(kSizeParameter); | 54 parsed_index += strlen(kSizeParameter); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 // - favicons of sizes "gfx::kFaviconSize * scale factor" px of type FAVICON | 87 // - favicons of sizes "gfx::kFaviconSize * scale factor" px of type FAVICON |
| 86 // where scale factor is one of FaviconUtil::GetFaviconScales(). | 88 // where scale factor is one of FaviconUtil::GetFaviconScales(). |
| 87 // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON | 89 // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON |
| 88 if (parsed->size_in_dip != gfx::kFaviconSize && | 90 if (parsed->size_in_dip != gfx::kFaviconSize && |
| 89 icon_types == favicon_base::FAVICON) | 91 icon_types == favicon_base::FAVICON) |
| 90 return false; | 92 return false; |
| 91 | 93 |
| 92 parsed_index = slash + 1; | 94 parsed_index = slash + 1; |
| 93 } | 95 } |
| 94 | 96 |
| 97 if (HasSubstringAt(path, parsed_index, kFallbackParameter)) { |
| 98 parsed_index += strlen(kFallbackParameter); |
| 99 size_t slash = path.find("/", parsed_index); |
| 100 if (slash == std::string::npos) |
| 101 return false; |
| 102 std::string fallback_str = path.substr(parsed_index, slash - parsed_index); |
| 103 parsed->fallback_specs_builder.reset( |
| 104 new favicon_base::FallbackIconSpecsBuilder()); |
| 105 if (!parsed->fallback_specs_builder->Parse(fallback_str)) |
| 106 return false; // Parse failed. |
| 107 parsed_index = slash + 1; |
| 108 } |
| 109 |
| 95 if (HasSubstringAt(path, parsed_index, kIconURLParameter)) { | 110 if (HasSubstringAt(path, parsed_index, kIconURLParameter)) { |
| 96 parsed_index += strlen(kIconURLParameter); | 111 parsed_index += strlen(kIconURLParameter); |
| 97 parsed->is_icon_url = true; | 112 parsed->is_icon_url = true; |
| 98 parsed->url = path.substr(parsed_index); | 113 parsed->url = path.substr(parsed_index); |
| 99 } else { | 114 } else if (HasSubstringAt(path, parsed_index, kOriginParameter)) { |
| 100 // URL requests prefixed with "origin/" are converted to a form with an | 115 // URL requests prefixed with "origin/" are converted to a form with an |
| 101 // empty path and a valid scheme. (e.g., example.com --> | 116 // empty path and a valid scheme. (e.g., example.com --> |
| 102 // http://example.com/ or http://example.com/a --> http://example.com/) | 117 // http://example.com/ or http://example.com/a --> http://example.com/) |
| 103 if (HasSubstringAt(path, parsed_index, kOriginParameter)) { | 118 parsed_index += strlen(kOriginParameter); |
| 104 parsed_index += strlen(kOriginParameter); | 119 std::string possibly_invalid_url = path.substr(parsed_index); |
| 105 std::string possibly_invalid_url = path.substr(parsed_index); | |
| 106 | 120 |
| 107 // If the URL does not specify a scheme (e.g., example.com instead of | 121 // If the URL does not specify a scheme (e.g., example.com instead of |
| 108 // http://example.com), add "http://" as a default. | 122 // http://example.com), add "http://" as a default. |
| 109 if (!GURL(possibly_invalid_url).has_scheme()) | 123 if (!GURL(possibly_invalid_url).has_scheme()) |
| 110 possibly_invalid_url = "http://" + possibly_invalid_url; | 124 possibly_invalid_url = "http://" + possibly_invalid_url; |
| 111 | 125 |
| 112 // Strip the path beyond the top-level domain. | 126 // Strip the path beyond the top-level domain. |
| 113 parsed->url = GURL(possibly_invalid_url).GetOrigin().spec(); | 127 parsed->url = GURL(possibly_invalid_url).GetOrigin().spec(); |
| 114 } else { | 128 } else { |
| 115 parsed->url = path.substr(parsed_index); | 129 parsed->url = path.substr(parsed_index); |
| 116 } | |
| 117 } | 130 } |
| 118 | 131 |
| 119 // The parsed index needs to be returned in order to allow Instant Extended | 132 // The parsed index needs to be returned in order to allow Instant Extended |
| 120 // to translate favicon URLs using advanced parameters. | 133 // to translate favicon URLs using advanced parameters. |
| 121 // Example: | 134 // Example: |
| 122 // "chrome-search://favicon/size/16@2x/<renderer-id>/<most-visited-id>" | 135 // "chrome-search://favicon/size/16@2x/<renderer-id>/<most-visited-id>" |
| 123 // would be translated to: | 136 // would be translated to: |
| 124 // "chrome-search://favicon/size/16@2x/<most-visited-item-with-given-id>". | 137 // "chrome-search://favicon/size/16@2x/<most-visited-item-with-given-id>". |
| 125 parsed->path_index = parsed_index; | 138 parsed->path_index = parsed_index; |
| 126 return true; | 139 return true; |
| 127 } | 140 } |
| 128 | 141 |
| 129 } // namespace chrome | 142 } // namespace chrome |
| OLD | NEW |