Chromium Code Reviews| 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 |