Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8419)

Unified Diff: chrome/common/favicon_url_parser.cc

Issue 15388002: Supporting high dpi favicons in Instant Extended. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressing Samarth's comments Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..9d6bc1b1d8a5cdf2427a8c48dcf399dcdb190e71
--- /dev/null
+++ b/chrome/common/favicon_url_parser.cc
@@ -0,0 +1,137 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// 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 file
+// "chrome/browser/ui/webui/favicon_source.h" for a description of
+// what each does.
+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 {
+
+bool ParseFaviconPath(const std::string& path,
+ int icon_types,
+ ParsedFaviconPath* parsed) {
+ DCHECK_EQ(16, gfx::kFaviconSize);
+
+ parsed->is_icon_url = false;
+ parsed->url = "";
+ parsed->size_in_dip = 16;
+ parsed->scale_factor = ui::SCALE_FACTOR_100P;
+ parsed->path_index = -1;
+
+ if (path.empty())
+ return false;
+
+ size_t parsed_index = 0;
+ if (HasSubstringAt(path, parsed_index, kLargestParameter)) {
+ parsed_index += strlen(kLargestParameter);
+ parsed->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, &parsed->size_in_dip))
+ return false;
+
+ if (parsed->size_in_dip != 64 && parsed->size_in_dip != 32) {
+ // Only 64x64, 32x32 and 16x16 icons are supported.
+ parsed->size_in_dip = 16;
+ }
+
+ if (!scale_str.empty())
+ webui::ParseScaleFactor(scale_str, &parsed->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 (parsed->size_in_dip != 16 && icon_types == chrome::FAVICON)
+ return false;
+
+ parsed_index = slash + 1;
+ }
+
+ if (HasSubstringAt(path, parsed_index, kIconURLParameter)) {
+ parsed_index += strlen(kIconURLParameter);
+ parsed->is_icon_url = true;
+ parsed->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)) {
+ 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.
+ parsed->url = GURL(possibly_invalid_url).GetOrigin().spec();
+ } else {
+ parsed->url = path.substr(parsed_index);
+ }
+ }
+
+ // The parsed index needs to be returned in order to allow Instant Extended
+ // to translate favicon URLs using advanced parameters.
+ // Example:
+ // "chrome-search://favicon/size/16@2x/<renderer-id>/<most-visited-id>"
+ // would be translated to:
+ // "chrome-search://favicon/size/16@2x/<most-visited-item-with-given-id>".
+ parsed->path_index = parsed_index;
+ return true;
+}
+
+} // namespace chrome

Powered by Google App Engine
This is Rietveld 408576698