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

Side by Side Diff: chrome/browser/ui/webui/favicon_source.cc

Issue 15388002: Supporting high dpi favicons in Instant Extended. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Refactoring after Kausalya's change 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/favicon_source.h" 5 #include "chrome/browser/ui/webui/favicon_source.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/favicon/favicon_service_factory.h" 10 #include "chrome/browser/favicon/favicon_service_factory.h"
11 #include "chrome/browser/history/top_sites.h" 11 #include "chrome/browser/history/top_sites.h"
12 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/search/instant_io_context.h" 13 #include "chrome/browser/search/instant_io_context.h"
14 #include "chrome/browser/search/instant_service.h" 14 #include "chrome/browser/search/instant_service.h"
15 #include "chrome/browser/search/instant_service_factory.h" 15 #include "chrome/browser/search/instant_service_factory.h"
16 #include "chrome/common/favicon_types.h"
17 #include "chrome/common/favicon_url_parser.h"
16 #include "chrome/common/url_constants.h" 18 #include "chrome/common/url_constants.h"
17 #include "grit/locale_settings.h" 19 #include "grit/locale_settings.h"
18 #include "grit/ui_resources.h" 20 #include "grit/ui_resources.h"
19 #include "net/url_request/url_request.h" 21 #include "net/url_request/url_request.h"
20 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/layout.h" 23 #include "ui/base/layout.h"
22 #include "ui/base/resource/resource_bundle.h" 24 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/webui/web_ui_util.h" 25 #include "ui/webui/web_ui_util.h"
24 26
25 namespace {
26
27 // Parameters which can be used in chrome://favicon path. See .h file for a
28 // description of what each does.
29 const char kIconURLParameter[] = "iconurl/";
30 const char kLargestParameter[] = "largest/";
31 const char kOriginParameter[] = "origin/";
32 const char kSizeParameter[] = "size/";
33
34 // Returns true if |search| is a substring of |path| which starts at
35 // |start_index|.
36 bool HasSubstringAt(const std::string& path,
37 size_t start_index,
38 const std::string& search) {
39 if (search.empty())
40 return false;
41
42 if (start_index + search.size() >= path.size())
43 return false;
44
45 return (path.compare(start_index, search.size(), search) == 0);
46 }
47
48 } // namespace
49
50 FaviconSource::IconRequest::IconRequest() 27 FaviconSource::IconRequest::IconRequest()
51 : size_in_dip(gfx::kFaviconSize), 28 : size_in_dip(gfx::kFaviconSize),
52 scale_factor(ui::SCALE_FACTOR_NONE) { 29 scale_factor(ui::SCALE_FACTOR_NONE) {
53 } 30 }
54 31
55 FaviconSource::IconRequest::IconRequest( 32 FaviconSource::IconRequest::IconRequest(
56 const content::URLDataSource::GotDataCallback& cb, 33 const content::URLDataSource::GotDataCallback& cb,
57 const GURL& path, 34 const GURL& path,
58 int size, 35 int size,
59 ui::ScaleFactor scale) 36 ui::ScaleFactor scale)
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 return InstantIOContext::ShouldServiceRequest(request); 141 return InstantIOContext::ShouldServiceRequest(request);
165 return URLDataSource::ShouldServiceRequest(request); 142 return URLDataSource::ShouldServiceRequest(request);
166 } 143 }
167 144
168 bool FaviconSource::HandleMissingResource(const IconRequest& request) { 145 bool FaviconSource::HandleMissingResource(const IconRequest& request) {
169 // No additional checks to locate the favicon resource in the base 146 // No additional checks to locate the favicon resource in the base
170 // implementation. 147 // implementation.
171 return false; 148 return false;
172 } 149 }
173 150
174 bool FaviconSource::ParsePath(const std::string& path, 151 bool FaviconSource::ParsePath(const std::string& path,
samarth 2013/06/14 21:54:46 Doesn't seem like much point for this function to
pedro (no code reviews) 2013/06/18 22:35:13 Good point. Removed.
175 bool* is_icon_url, 152 bool* is_icon_url,
176 GURL* url, 153 GURL* url,
177 int* size_in_dip, 154 int* size_in_dip,
178 ui::ScaleFactor* scale_factor) const { 155 ui::ScaleFactor* scale_factor) const {
179 DCHECK_EQ(16, gfx::kFaviconSize); 156 DCHECK_EQ(16, gfx::kFaviconSize);
180 157
181 *is_icon_url = false; 158 std::string url_string;
182 *url = GURL(); 159 std::string params;
183 *size_in_dip = 16; 160 bool success = chrome::ParseFaviconPath(path, true, icon_types_,
184 *scale_factor = ui::SCALE_FACTOR_100P; 161 is_icon_url, &url_string, size_in_dip, scale_factor, &params);
185 162
186 if (path.empty()) 163 *url = GURL(url_string);
187 return false;
188 164
189 size_t parsed_index = 0; 165 return success;
190 if (HasSubstringAt(path, parsed_index, kLargestParameter)) {
191 parsed_index += strlen(kLargestParameter);
192 *size_in_dip = 0;
193 } else if (HasSubstringAt(path, parsed_index, kSizeParameter)) {
194 parsed_index += strlen(kSizeParameter);
195
196 size_t slash = path.find("/", parsed_index);
197 if (slash == std::string::npos)
198 return false;
199
200 size_t scale_delimiter = path.find("@", parsed_index);
201 std::string size_str;
202 std::string scale_str;
203 if (scale_delimiter == std::string::npos) {
204 // Support the legacy size format of 'size/aa/' where 'aa' is the desired
205 // size in DIP for the sake of not regressing the extensions which use it.
206 size_str = path.substr(parsed_index, slash - parsed_index);
207 } else {
208 size_str = path.substr(parsed_index, scale_delimiter - parsed_index);
209 scale_str = path.substr(scale_delimiter + 1, slash - scale_delimiter - 1);
210 }
211
212 if (!base::StringToInt(size_str, size_in_dip))
213 return false;
214
215 if (*size_in_dip != 64 && *size_in_dip != 32) {
216 // Only 64x64, 32x32 and 16x16 icons are supported.
217 *size_in_dip = 16;
218 }
219
220 if (!scale_str.empty())
221 webui::ParseScaleFactor(scale_str, scale_factor);
222
223 // Return the default favicon (as opposed to a resized favicon) for
224 // favicon sizes which are not cached by the favicon service.
225 // Currently the favicon service caches:
226 // - favicons of sizes "16 * scale factor" px of type FAVICON
227 // where scale factor is one of FaviconUtil::GetFaviconScaleFactors().
228 // - the largest TOUCH_ICON / TOUCH_PRECOMPOSED_ICON
229 if (*size_in_dip != 16 && icon_types_ == chrome::FAVICON)
230 return false;
231
232 parsed_index = slash + 1;
233 }
234
235 if (HasSubstringAt(path, parsed_index, kIconURLParameter)) {
236 parsed_index += strlen(kIconURLParameter);
237 *is_icon_url = true;
238 *url = GURL(path.substr(parsed_index));
239 } else {
240 // URL requests prefixed with "origin/" are converted to a form with an
241 // empty path and a valid scheme. (e.g., example.com -->
242 // http://example.com/ or http://example.com/a --> http://example.com/)
243 if (HasSubstringAt(path, parsed_index, kOriginParameter)) {
244 parsed_index += strlen(kOriginParameter);
245 std::string possibly_invalid_url = path.substr(parsed_index);
246
247 // If the URL does not specify a scheme (e.g., example.com instead of
248 // http://example.com), add "http://" as a default.
249 if (!GURL(possibly_invalid_url).has_scheme())
250 possibly_invalid_url = "http://" + possibly_invalid_url;
251
252 // Strip the path beyond the top-level domain.
253 *url = GURL(possibly_invalid_url).GetOrigin();
254 } else {
255 *url = GURL(path.substr(parsed_index));
256 }
257 }
258 return true;
259 } 166 }
260 167
261 void FaviconSource::OnFaviconDataAvailable( 168 void FaviconSource::OnFaviconDataAvailable(
262 const IconRequest& request, 169 const IconRequest& request,
263 const chrome::FaviconBitmapResult& bitmap_result) { 170 const chrome::FaviconBitmapResult& bitmap_result) {
264 if (bitmap_result.is_valid()) { 171 if (bitmap_result.is_valid()) {
265 // Forward the data along to the networking system. 172 // Forward the data along to the networking system.
266 request.callback.Run(bitmap_result.bitmap_data.get()); 173 request.callback.Run(bitmap_result.bitmap_data.get());
267 } else if (!HandleMissingResource(request)) { 174 } else if (!HandleMissingResource(request)) {
268 SendDefaultResponse(request); 175 SendDefaultResponse(request);
(...skipping 28 matching lines...) Expand all
297 204
298 if (!default_favicon) { 205 if (!default_favicon) {
299 ui::ScaleFactor scale_factor = icon_request.scale_factor; 206 ui::ScaleFactor scale_factor = icon_request.scale_factor;
300 default_favicon = ResourceBundle::GetSharedInstance() 207 default_favicon = ResourceBundle::GetSharedInstance()
301 .LoadDataResourceBytesForScale(resource_id, scale_factor); 208 .LoadDataResourceBytesForScale(resource_id, scale_factor);
302 default_favicons_[favicon_index] = default_favicon; 209 default_favicons_[favicon_index] = default_favicon;
303 } 210 }
304 211
305 icon_request.callback.Run(default_favicon); 212 icon_request.callback.Run(default_favicon);
306 } 213 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698