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

Side by Side 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: Created 7 years, 5 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/common/favicon_url_parser.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/common/favicon_types.h"
9 #include "net/url_request/url_request.h"
10 #include "ui/base/layout.h"
11 #include "ui/gfx/favicon_size.h"
12 #include "ui/webui/web_ui_util.h"
13
14 namespace {
15
16 // Parameters which can be used in chrome://favicon path. See file
17 // "chrome/browser/ui/webui/favicon_source.h" for a description of
18 // what each does.
19 const char kIconURLParameter[] = "iconurl/";
20 const char kLargestParameter[] = "largest/";
21 const char kOriginParameter[] = "origin/";
22 const char kSizeParameter[] = "size/";
23
24 // Returns true if |search| is a substring of |path| which starts at
25 // |start_index|.
26 bool HasSubstringAt(const std::string& path,
27 size_t start_index,
28 const std::string& search) {
29 if (search.empty())
30 return false;
31
32 if (start_index + search.size() >= path.size())
33 return false;
34
35 return path.compare(start_index, search.size(), search) == 0;
36 }
37
38 } // namespace
39
40 namespace chrome {
41
42 bool ParseFaviconPath(const std::string& path,
43 int icon_types,
44 ParsedFaviconPath* parsed) {
45 DCHECK_EQ(16, gfx::kFaviconSize);
46
47 parsed->is_icon_url = false;
48 parsed->url = "";
49 parsed->size_in_dip = 16;
50 parsed->scale_factor = ui::SCALE_FACTOR_100P;
51 parsed->path_index = -1;
52
53 if (path.empty())
54 return false;
55
56 size_t parsed_index = 0;
57 if (HasSubstringAt(path, parsed_index, kLargestParameter)) {
58 parsed_index += strlen(kLargestParameter);
59 parsed->size_in_dip = 0;
60 } else if (HasSubstringAt(path, parsed_index, kSizeParameter)) {
61 parsed_index += strlen(kSizeParameter);
62
63 size_t slash = path.find("/", parsed_index);
64 if (slash == std::string::npos)
65 return false;
66
67 size_t scale_delimiter = path.find("@", parsed_index);
68 std::string size_str;
69 std::string scale_str;
70 if (scale_delimiter == std::string::npos) {
71 // Support the legacy size format of 'size/aa/' where 'aa' is the desired
72 // size in DIP for the sake of not regressing the extensions which use it.
73 size_str = path.substr(parsed_index, slash - parsed_index);
74 } else {
75 size_str = path.substr(parsed_index, scale_delimiter - parsed_index);
76 scale_str = path.substr(scale_delimiter + 1,
77 slash - scale_delimiter - 1);
78 }
79
80 if (!base::StringToInt(size_str, &parsed->size_in_dip))
81 return false;
82
83 if (parsed->size_in_dip != 64 && parsed->size_in_dip != 32) {
84 // Only 64x64, 32x32 and 16x16 icons are supported.
85 parsed->size_in_dip = 16;
86 }
87
88 if (!scale_str.empty())
89 webui::ParseScaleFactor(scale_str, &parsed->scale_factor);
90
91 // Return the default favicon (as opposed to a resized favicon) for
92 // favicon sizes which are not cached by the favicon service.
93 // Currently the favicon service caches:
94 // - favicons of sizes "16 * scale factor" px of type FAVICON
95 // where scale factor is one of FaviconUtil::GetFaviconScaleFactors().
96 // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON
97 if (parsed->size_in_dip != 16 && icon_types == chrome::FAVICON)
98 return false;
99
100 parsed_index = slash + 1;
101 }
102
103 if (HasSubstringAt(path, parsed_index, kIconURLParameter)) {
104 parsed_index += strlen(kIconURLParameter);
105 parsed->is_icon_url = true;
106 parsed->url = path.substr(parsed_index);
107 } else {
108 // URL requests prefixed with "origin/" are converted to a form with an
109 // empty path and a valid scheme. (e.g., example.com -->
110 // http://example.com/ or http://example.com/a --> http://example.com/)
111 if (HasSubstringAt(path, parsed_index, kOriginParameter)) {
112 parsed_index += strlen(kOriginParameter);
113 std::string possibly_invalid_url = path.substr(parsed_index);
114
115 // If the URL does not specify a scheme (e.g., example.com instead of
116 // http://example.com), add "http://" as a default.
117 if (!GURL(possibly_invalid_url).has_scheme())
118 possibly_invalid_url = "http://" + possibly_invalid_url;
119
120 // Strip the path beyond the top-level domain.
121 parsed->url = GURL(possibly_invalid_url).GetOrigin().spec();
122 } else {
123 parsed->url = path.substr(parsed_index);
124 }
125 }
126
127 // The parsed index needs to be returned in order to allow Instant Extended
128 // to translate favicon URLs using advanced parameters.
129 // Example:
130 // "chrome-search://favicon/size/16@2x/<renderer-id>/<most-visited-id>"
131 // would be translated to:
132 // "chrome-search://favicon/size/16@2x/<most-visited-item-with-given-id>".
133 parsed->path_index = parsed_index;
134 return true;
135 }
136
137 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698