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

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

Issue 10909236: Add support for favicon scale factor in WebUI (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use high-DPI icons for search engine dialog. Created 8 years, 3 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 "chrome/browser/favicon/favicon_service_factory.h" 9 #include "chrome/browser/favicon/favicon_service_factory.h"
10 #include "chrome/browser/history/top_sites.h" 10 #include "chrome/browser/history/top_sites.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/webui/web_ui_util.h"
12 #include "chrome/common/url_constants.h" 13 #include "chrome/common/url_constants.h"
13 #include "grit/locale_settings.h" 14 #include "grit/locale_settings.h"
14 #include "grit/ui_resources.h" 15 #include "grit/ui_resources.h"
15 #include "ui/base/l10n/l10n_util.h" 16 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/layout.h" 17 #include "ui/base/layout.h"
17 #include "ui/base/resource/resource_bundle.h" 18 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/gfx/favicon_size.h" 19 #include "ui/gfx/favicon_size.h"
19 20
20 FaviconSource::FaviconSource(Profile* profile, IconType type) 21 FaviconSource::FaviconSource(Profile* profile, IconType type)
21 : DataSource(type == FAVICON ? chrome::kChromeUIFaviconHost : 22 : DataSource(type == FAVICON ? chrome::kChromeUIFaviconHost :
(...skipping 22 matching lines...) Expand all
44 void FaviconSource::StartDataRequest(const std::string& path, 45 void FaviconSource::StartDataRequest(const std::string& path,
45 bool is_incognito, 46 bool is_incognito,
46 int request_id) { 47 int request_id) {
47 FaviconService* favicon_service = 48 FaviconService* favicon_service =
48 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); 49 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
49 if (!favicon_service || path.empty()) { 50 if (!favicon_service || path.empty()) {
50 SendDefaultResponse(request_id); 51 SendDefaultResponse(request_id);
51 return; 52 return;
52 } 53 }
53 54
55 ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_NONE;
56
54 FaviconService::Handle handle; 57 FaviconService::Handle handle;
55 if (path.size() > 8 && path.substr(0, 8) == "iconurl/") { 58 if (path.size() > 8 && (path.substr(0, 8) == "iconurl/" ||
59 path.substr(0, 8) == "iconurl@")) {
pkotwicz 2012/09/17 19:32:06 Can you put " (path.substr(0, 8) == iconurl/ ||" o
kevers 2012/09/17 21:13:02 Done.
60 size_t prefix_length = 8;
61 // Optional scale factor appended to iconurl, which may be @1x or @2x.
62 if (path.at(7) == '@') {
63 size_t slash = path.find("/");
64 std::string scale_str = path.substr(8, slash - 8);
65 web_ui_util::ParseScaleFactor(scale_str, &scale_factor);
66 prefix_length = slash + 1;
67 }
56 // TODO : Change GetFavicon to support combination of IconType. 68 // TODO : Change GetFavicon to support combination of IconType.
57 handle = favicon_service->GetRawFavicon( 69 handle = favicon_service->GetRawFavicon(
58 GURL(path.substr(8)), 70 GURL(path.substr(prefix_length)),
59 history::FAVICON, 71 history::FAVICON,
60 gfx::kFaviconSize, 72 gfx::kFaviconSize,
61 ui::SCALE_FACTOR_100P, 73 scale_factor,
62 &cancelable_consumer_, 74 &cancelable_consumer_,
63 base::Bind(&FaviconSource::OnFaviconDataAvailable, 75 base::Bind(&FaviconSource::OnFaviconDataAvailable,
64 base::Unretained(this))); 76 base::Unretained(this)));
65 } else { 77 } else {
66 GURL url; 78 GURL url;
67
68 if (path.size() > 5 && path.substr(0, 5) == "size/") { 79 if (path.size() > 5 && path.substr(0, 5) == "size/") {
69 size_t slash = path.find("/", 5); 80 size_t slash = path.find("/", 5);
81 size_t scale_delimiter = path.find("@", 5);
70 std::string size = path.substr(5, slash - 5); 82 std::string size = path.substr(5, slash - 5);
71 int pixel_size = atoi(size.c_str()); 83 int pixel_size = atoi(size.c_str());
72 CHECK(pixel_size == 32 || pixel_size == 16) << 84 CHECK(pixel_size == 32 || pixel_size == 16) <<
73 "only 32x32 and 16x16 icons are supported"; 85 "only 32x32 and 16x16 icons are supported";
74 request_size_map_[request_id] = pixel_size; 86 // Optional scale factor.
87 if (scale_delimiter != path.npos && scale_delimiter < slash) {
pkotwicz 2012/09/17 19:32:06 Can you use std::string::npos instead?
kevers 2012/09/17 21:13:02 Done.
88 std::string scale_str = path.substr(scale_delimiter + 1,
89 slash - scale_delimiter - 1);
90 web_ui_util::ParseScaleFactor(scale_str, &scale_factor);
91 }
92 request_size_map_[request_id] = IconSize(pixel_size, scale_factor);
pkotwicz 2012/09/17 19:32:06 Can you use std::map::insert() and thus avoid need
kevers 2012/09/17 21:13:02 Using insert does not appear to eliminate the need
Evan Stade 2012/09/18 13:54:05 is it possible to delete the map (therefore fixing
kevers 2012/09/18 17:31:03 Done.
75 url = GURL(path.substr(slash + 1)); 93 url = GURL(path.substr(slash + 1));
76 } else { 94 } else {
77 // URL requests prefixed with "origin/" are converted to a form with an 95 // URL requests prefixed with "origin/" are converted to a form with an
78 // empty path and a valid scheme. (e.g., example.com --> 96 // empty path and a valid scheme. (e.g., example.com -->
79 // http://example.com/ or http://example.com/a --> http://example.com/) 97 // http://example.com/ or http://example.com/a --> http://example.com/)
80 if (path.size() > 7 && path.substr(0, 7) == "origin/") { 98 if (path.size() > 7 && path.substr(0, 7) == "origin/") {
81 std::string originalUrl = path.substr(7); 99 std::string originalUrl = path.substr(7);
82 100
83 // If the original URL does not specify a scheme (e.g., example.com 101 // If the original URL does not specify a scheme (e.g., example.com
84 // instead of http://example.com), add "http://" as a default. 102 // instead of http://example.com), add "http://" as a default.
85 if (!GURL(originalUrl).has_scheme()) 103 if (!GURL(originalUrl).has_scheme())
86 originalUrl = "http://" + originalUrl; 104 originalUrl = "http://" + originalUrl;
87 105
88 // Strip the path beyond the top-level domain. 106 // Strip the path beyond the top-level domain.
89 url = GURL(originalUrl).GetOrigin(); 107 url = GURL(originalUrl).GetOrigin();
90 } else { 108 } else {
91 url = GURL(path); 109 url = GURL(path);
92 } 110 }
93 111
94 request_size_map_[request_id] = 16; 112 request_size_map_[request_id] = IconSize(16, scale_factor);
95 } 113 }
96 114
97 // Intercept requests for prepopulated pages. 115 // Intercept requests for prepopulated pages.
98 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) { 116 for (size_t i = 0; i < arraysize(history::kPrepopulatedPages); i++) {
99 if (url.spec() == 117 if (url.spec() ==
100 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) { 118 l10n_util::GetStringUTF8(history::kPrepopulatedPages[i].url_id)) {
101 request_size_map_.erase(request_id); 119 request_size_map_.erase(request_id);
102 SendResponse(request_id, 120 SendResponse(request_id,
103 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( 121 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
104 history::kPrepopulatedPages[i].favicon_id, 122 history::kPrepopulatedPages[i].favicon_id,
105 ui::SCALE_FACTOR_100P)); 123 scale_factor));
106 return; 124 return;
107 } 125 }
108 } 126 }
109 127
110 // TODO(estade): fetch the requested size. 128 // TODO(estade): fetch the requested size.
111 handle = favicon_service->GetRawFaviconForURL( 129 handle = favicon_service->GetRawFaviconForURL(
112 FaviconService::FaviconForURLParams( 130 FaviconService::FaviconForURLParams(
113 profile_, 131 profile_,
114 url, 132 url,
115 icon_types_, 133 icon_types_,
116 gfx::kFaviconSize, 134 gfx::kFaviconSize,
117 &cancelable_consumer_), 135 &cancelable_consumer_),
118 ui::SCALE_FACTOR_100P, 136 scale_factor,
119 base::Bind(&FaviconSource::OnFaviconDataAvailable, 137 base::Bind(&FaviconSource::OnFaviconDataAvailable,
120 base::Unretained(this))); 138 base::Unretained(this)));
121 } 139 }
122 140
123 // Attach the ChromeURLDataManager request ID to the history request. 141 // Attach the ChromeURLDataManager request ID to the history request.
124 cancelable_consumer_.SetClientData(favicon_service, handle, request_id); 142 cancelable_consumer_.SetClientData(favicon_service, handle, request_id);
125 } 143 }
126 144
127 std::string FaviconSource::GetMimeType(const std::string&) const { 145 std::string FaviconSource::GetMimeType(const std::string&) const {
128 // We need to explicitly return a mime type, otherwise if the user tries to 146 // We need to explicitly return a mime type, otherwise if the user tries to
(...skipping 18 matching lines...) Expand all
147 if (bitmap_result.is_valid()) { 165 if (bitmap_result.is_valid()) {
148 // Forward the data along to the networking system. 166 // Forward the data along to the networking system.
149 SendResponse(request_id, bitmap_result.bitmap_data); 167 SendResponse(request_id, bitmap_result.bitmap_data);
150 } else { 168 } else {
151 SendDefaultResponse(request_id); 169 SendDefaultResponse(request_id);
152 } 170 }
153 } 171 }
154 172
155 void FaviconSource::SendDefaultResponse(int request_id) { 173 void FaviconSource::SendDefaultResponse(int request_id) {
156 base::RefCountedMemory* bytes = NULL; 174 base::RefCountedMemory* bytes = NULL;
157 if (request_size_map_[request_id] == 32) { 175 ui::ScaleFactor scale = request_size_map_[request_id].scale_factor;
pkotwicz 2012/09/17 19:32:06 Nit: can you call this variable scale_factor? In g
kevers 2012/09/17 21:13:02 Done.
176
177 if (request_size_map_[request_id].pixel_size == 32) {
158 if (!default_favicon_large_.get()) { 178 if (!default_favicon_large_.get()) {
159 default_favicon_large_ = 179 default_favicon_large_ =
160 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( 180 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
161 IDR_DEFAULT_LARGE_FAVICON, ui::SCALE_FACTOR_100P); 181 IDR_DEFAULT_LARGE_FAVICON, scale);
162 } 182 }
163 bytes = default_favicon_large_; 183 bytes = default_favicon_large_;
164 } else { 184 } else {
165 if (!default_favicon_.get()) { 185 if (!default_favicon_.get()) {
166 default_favicon_ = 186 default_favicon_ =
167 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( 187 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
168 IDR_DEFAULT_FAVICON, ui::SCALE_FACTOR_100P); 188 IDR_DEFAULT_FAVICON, scale);
169 } 189 }
170 bytes = default_favicon_; 190 bytes = default_favicon_;
171 } 191 }
172 request_size_map_.erase(request_id); 192 request_size_map_.erase(request_id);
173 193
174 SendResponse(request_id, bytes); 194 SendResponse(request_id, bytes);
175 } 195 }
OLDNEW
« chrome/browser/ui/webui/favicon_source.h ('K') | « chrome/browser/ui/webui/favicon_source.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698