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

Unified Diff: chrome/common/favicon/fallback_icon_url_parser.cc

Issue 835903005: [Favicon] Add new fallback icon rendering flow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding new host chrome://fallback-icon. Created 5 years, 11 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/fallback_icon_url_parser.cc
diff --git a/chrome/common/favicon/fallback_icon_url_parser.cc b/chrome/common/favicon/fallback_icon_url_parser.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f7dd2ba2aa364fd7864371e0a59d91d8d0a0381a
--- /dev/null
+++ b/chrome/common/favicon/fallback_icon_url_parser.cc
@@ -0,0 +1,83 @@
+// Copyright 2015 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/fallback_icon_url_parser.h"
+
+#include "base/strings/string_number_conversions.h"
+#include "ui/base/webui/web_ui_util.h"
+#include "ui/gfx/favicon_size.h"
+
+namespace {
+
+// Parameters which can be used in chrome://favicon path. See file
+// "fallback_icon_url_parser.h" for a description of what each does.
+const char kSizeParameter[] = "size/";
+const char kStyleParameter[] = "style/";
+
+// 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) {
+ return path.compare(start_index, search.length(), search) == 0;
+}
+
+} // namespace
+
+namespace chrome {
+
+bool ParseFallbackIconPath(const std::string& path,
+ ParsedFallbackIconPath* parsed) {
pkotwicz 2015/01/21 19:44:15 Can you move the parsing logic to FallbackIconSour
huangs 2015/01/22 01:13:27 Per other comment, I think size should be a separa
huangs 2015/01/22 22:51:39 Moved to ParseFallbackIconPath(), per discussion.
+ DCHECK(parsed);
+ parsed->url = "";
+ parsed->size_in_dip = gfx::kFaviconSize;
+ parsed->device_scale_factor = 1.0f;
+ parsed->style_builder.Reset();
+
+ if (path.empty())
+ return false;
+
+ size_t parsed_index = 0;
+ 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 (!scale_str.empty())
+ webui::ParseScaleFactor(scale_str, &parsed->device_scale_factor);
+ parsed_index = slash + 1;
+ }
+
+ if (HasSubstringAt(path, parsed_index, kStyleParameter)) {
+ parsed_index += strlen(kStyleParameter);
+ size_t slash = path.find("/", parsed_index);
+ if (slash == std::string::npos)
+ return false;
+ std::string fallback_str = path.substr(parsed_index, slash - parsed_index);
+ if (!parsed->style_builder.Parse(fallback_str))
+ return false; // Parse failed.
+ parsed_index = slash + 1;
+ }
+
+ // Extract URL.
+ parsed->url = path.substr(parsed_index);
+ return true;
+}
+
+} // namespace chrome

Powered by Google App Engine
This is Rietveld 408576698