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
3 // found in the LICENSE file.
5 #include "components/favicon_base/favicon_url_parser.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "components/favicon_base/favicon_types.h"
9 #include "net/url_request/url_request.h"
10 #include "ui/base/webui/web_ui_util.h"
11 #include "ui/gfx/favicon_size.h"
15 // Parameters which can be used in chrome://favicon path. See file
16 // "chrome/browser/ui/webui/favicon_source.h" for a description of
18 const char kIconURLParameter[] = "iconurl/";
19 const char kSizeParameter[] = "size/";
21 // Returns true if |search| is a substring of |path| which starts at
23 bool HasSubstringAt(const std::string& path,
25 const std::string& search) {
26 return path.compare(start_index, search.length(), search) == 0;
33 bool ParseFaviconPath(const std::string& path,
34 ParsedFaviconPath* parsed) {
35 parsed->is_icon_url = false;
37 parsed->size_in_dip = gfx::kFaviconSize;
38 parsed->device_scale_factor = 1.0f;
39 parsed->path_index = std::string::npos;
44 size_t parsed_index = 0;
45 if (HasSubstringAt(path, parsed_index, kSizeParameter)) {
46 parsed_index += strlen(kSizeParameter);
48 size_t slash = path.find("/", parsed_index);
49 if (slash == std::string::npos)
52 size_t scale_delimiter = path.find("@", parsed_index);
54 std::string scale_str;
55 if (scale_delimiter == std::string::npos) {
56 // Support the legacy size format of 'size/aa/' where 'aa' is the desired
57 // size in DIP for the sake of not regressing the extensions which use it.
58 size_str = path.substr(parsed_index, slash - parsed_index);
60 size_str = path.substr(parsed_index, scale_delimiter - parsed_index);
61 scale_str = path.substr(scale_delimiter + 1,
62 slash - scale_delimiter - 1);
65 if (!base::StringToInt(size_str, &parsed->size_in_dip))
68 if (!scale_str.empty())
69 webui::ParseScaleFactor(scale_str, &parsed->device_scale_factor);
71 parsed_index = slash + 1;
74 if (HasSubstringAt(path, parsed_index, kIconURLParameter)) {
75 parsed_index += strlen(kIconURLParameter);
76 parsed->is_icon_url = true;
77 parsed->url = path.substr(parsed_index);
79 parsed->url = path.substr(parsed_index);
82 // The parsed index needs to be returned in order to allow Instant Extended
83 // to translate favicon URLs using advanced parameters.
85 // "chrome-search://favicon/size/16@2x/<renderer-id>/<most-visited-id>"
86 // would be translated to:
87 // "chrome-search://favicon/size/16@2x/<most-visited-item-with-given-id>".
88 parsed->path_index = parsed_index;