Chromium Code Reviews| Index: chrome/common/favicon_url_parser.cc |
| diff --git a/chrome/common/favicon_url_parser.cc b/chrome/common/favicon_url_parser.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9cb119428733fc448ae78054ace213a672cdb6de |
| --- /dev/null |
| +++ b/chrome/common/favicon_url_parser.cc |
| @@ -0,0 +1,148 @@ |
| +// 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.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/common/favicon_url_parser.h" |
| + |
| +#include "base/strings/string_number_conversions.h" |
| +#include "chrome/common/favicon_types.h" |
| +#include "net/url_request/url_request.h" |
| +#include "ui/base/layout.h" |
| +#include "ui/gfx/favicon_size.h" |
| +#include "ui/webui/web_ui_util.h" |
| + |
| +namespace { |
| + |
| +// Parameters which can be used in chrome://favicon path. See .h file for a |
| +// 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.
|
| +const char kIconURLParameter[] = "iconurl/"; |
| +const char kLargestParameter[] = "largest/"; |
| +const char kOriginParameter[] = "origin/"; |
| +const char kSizeParameter[] = "size/"; |
| + |
| +// Returns true if |search| is a substring of |path| which starts at |
| +// |start_index|. |
| +bool HasSubstringAt(const std::string& path, |
| + size_t start_index, |
| + const std::string& search) { |
| + if (search.empty()) |
| + return false; |
| + |
| + if (start_index + search.size() >= path.size()) |
| + return false; |
| + |
| + return (path.compare(start_index, search.size(), search) == 0); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace chrome { |
| + |
| +// TODO make url string |
|
sreeram
2013/06/04 22:01:56
Remove this?
pedro (no code reviews)
2013/06/07 23:34:21
Done.
|
| + |
| +bool ParseFaviconPath(const std::string& path, |
| + bool supports_origin_parameter, |
| + int icon_types, |
| + bool* is_icon_url, |
| + std::string* url, |
| + int* size_in_dip, |
| + ui::ScaleFactor* scale_factor, |
| + 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.
|
| + DCHECK_EQ(16, gfx::kFaviconSize); |
| + |
| + *is_icon_url = false; |
| + *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.
|
| + *url = ""; |
| + *size_in_dip = 16; |
| + *scale_factor = ui::SCALE_FACTOR_100P; |
| + |
| + if (path.empty()) |
| + return false; |
| + |
| + size_t parsed_index = 0; |
| + if (HasSubstringAt(path, parsed_index, kLargestParameter)) { |
| + parsed_index += strlen(kLargestParameter); |
| + *size_in_dip = 0; |
| + } else if (HasSubstringAt(path, parsed_index, kSizeParameter)) { |
| + parsed_index += strlen(kSizeParameter); |
| + |
| + size_t slash = path.find("/", parsed_index); |
| + if (slash == std::string::npos) |
| + return false; |
| + |
| + size_t scale_delimiter = path.find("@", parsed_index); |
| + std::string size_str; |
| + std::string scale_str; |
| + if (scale_delimiter == std::string::npos) { |
| + // Support the legacy size format of 'size/aa/' where 'aa' is the desired |
| + // size in DIP for the sake of not regressing the extensions which use it. |
| + size_str = path.substr(parsed_index, slash - parsed_index); |
| + } else { |
| + size_str = path.substr(parsed_index, scale_delimiter - parsed_index); |
| + scale_str = path.substr(scale_delimiter + 1, |
| + slash - scale_delimiter - 1); |
| + } |
| + |
| + if (!base::StringToInt(size_str, size_in_dip)) |
| + return false; |
| + |
| + if (*size_in_dip != 64 && *size_in_dip != 32) { |
| + // Only 64x64, 32x32 and 16x16 icons are supported. |
| + *size_in_dip = 16; |
| + } |
| + |
| + if (!scale_str.empty()) |
| + webui::ParseScaleFactor(scale_str, scale_factor); |
| + |
| + // Return the default favicon (as opposed to a resized favicon) for |
| + // favicon sizes which are not cached by the favicon service. |
| + // Currently the favicon service caches: |
| + // - favicons of sizes "16 * scale factor" px of type FAVICON |
| + // where scale factor is one of FaviconUtil::GetFaviconScaleFactors(). |
| + // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON |
| + if (*size_in_dip != 16 && icon_types == chrome::FAVICON) |
| + return false; |
| + |
| + parsed_index = slash + 1; |
| + } |
| + |
| + if (HasSubstringAt(path, parsed_index, kIconURLParameter)) { |
| + parsed_index += strlen(kIconURLParameter); |
| + *is_icon_url = true; |
| + *url = path.substr(parsed_index); |
| + } else { |
| + // URL requests prefixed with "origin/" are converted to a form with an |
| + // empty path and a valid scheme. (e.g., example.com --> |
| + // http://example.com/ or http://example.com/a --> http://example.com/) |
| + if (HasSubstringAt(path, parsed_index, kOriginParameter)) { |
| + // The format of favicon URLs used in Instant Extended does not support |
| + // the origin parameter. |
| + if (!supports_origin_parameter) |
| + return false; |
| + |
| + parsed_index += strlen(kOriginParameter); |
| + std::string possibly_invalid_url = path.substr(parsed_index); |
| + |
| + // If the URL does not specify a scheme (e.g., example.com instead of |
| + // http://example.com), add "http://" as a default. |
| + if (!GURL(possibly_invalid_url).has_scheme()) |
| + possibly_invalid_url = "http://" + possibly_invalid_url; |
| + |
| + // Strip the path beyond the top-level domain. |
| + *url = GURL(possibly_invalid_url).GetOrigin().spec(); |
| + } else { |
| + *url = path.substr(parsed_index); |
| + } |
| + } |
| + |
| + // The favicon parameters part of the path needs to be returned in order |
| + // to allow Instant Extended to translate favicon URLs using such parameters. |
| + // Example: "chrome-search://favicon/size/16@2x/1234" would be translated to |
| + // "chrome-search://favicon/size/16@2x/<most-visited-item-with-id-1234>". |
| + // The value of |params| in this case would be "size/16@2x/" (without the |
| + // leading slash). |
| + *params = path.substr(0, parsed_index); |
| + return true; |
| +} |
| + |
| +} // namespace chrome |