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

Side by Side Diff: chrome/browser/favicon/favicon_handler.cc

Issue 9696057: Prioritize smaller favicons over larger ones for tabs, etc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Store single FavionCandidate Created 8 years, 9 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/favicon/favicon_handler.h" 5 #include "chrome/browser/favicon/favicon_handler.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #include <vector> 9 #include <vector>
10 10
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 bool DoUrlAndIconMatch(const FaviconURL& favicon_url, 45 bool DoUrlAndIconMatch(const FaviconURL& favicon_url,
46 const GURL& url, 46 const GURL& url,
47 history::IconType icon_type) { 47 history::IconType icon_type) {
48 return favicon_url.icon_url == url && 48 return favicon_url.icon_url == url &&
49 favicon_url.icon_type == static_cast<FaviconURL::IconType>(icon_type); 49 favicon_url.icon_type == static_cast<FaviconURL::IconType>(icon_type);
50 } 50 }
51 51
52 } // namespace 52 } // namespace
53 53
54 ////////////////////////////////////////////////////////////////////////////////
55
54 FaviconHandler::DownloadRequest::DownloadRequest() 56 FaviconHandler::DownloadRequest::DownloadRequest()
55 : icon_type(history::INVALID_ICON) { 57 : icon_type(history::INVALID_ICON) {
56 } 58 }
57 59
58 FaviconHandler::DownloadRequest::~DownloadRequest() { 60 FaviconHandler::DownloadRequest::~DownloadRequest() {
59 } 61 }
60 62
61 FaviconHandler::DownloadRequest::DownloadRequest( 63 FaviconHandler::DownloadRequest::DownloadRequest(
62 const GURL& url, 64 const GURL& url,
63 const GURL& image_url, 65 const GURL& image_url,
64 const FaviconTabHelper::ImageDownloadCallback& callback, 66 const FaviconTabHelper::ImageDownloadCallback& callback,
65 history::IconType icon_type) 67 history::IconType icon_type)
66 : url(url), 68 : url(url),
67 image_url(image_url), 69 image_url(image_url),
68 callback(callback), 70 callback(callback),
69 icon_type(icon_type) { 71 icon_type(icon_type) {
70 } 72 }
71 73
74 ////////////////////////////////////////////////////////////////////////////////
75
76 FaviconHandler::FaviconCandidate::FaviconCandidate()
77 : icon_type(history::INVALID_ICON) {
78 }
79
80 FaviconHandler::FaviconCandidate::~FaviconCandidate() {
81 }
82
83 FaviconHandler::FaviconCandidate::FaviconCandidate(
84 const GURL& url,
85 const GURL& image_url,
86 const gfx::Image& image,
87 const SkBitmap& bitmap,
88 history::IconType icon_type)
89 : url(url),
90 image_url(image_url),
91 image(image),
92 bitmap(bitmap),
93 icon_type(icon_type) {
94 }
95
96 ////////////////////////////////////////////////////////////////////////////////
97
72 FaviconHandler::FaviconHandler(Profile* profile, 98 FaviconHandler::FaviconHandler(Profile* profile,
73 FaviconHandlerDelegate* delegate, 99 FaviconHandlerDelegate* delegate,
74 Type icon_type) 100 Type icon_type)
75 : got_favicon_from_history_(false), 101 : got_favicon_from_history_(false),
76 favicon_expired_(false), 102 favicon_expired_(false),
77 icon_types_(icon_type == FAVICON ? history::FAVICON : 103 icon_types_(icon_type == FAVICON ? history::FAVICON :
78 history::TOUCH_ICON | history::TOUCH_PRECOMPOSED_ICON), 104 history::TOUCH_ICON | history::TOUCH_PRECOMPOSED_ICON),
79 current_url_index_(0), 105 current_url_index_(0),
80 profile_(profile), 106 profile_(profile),
81 delegate_(delegate) { 107 delegate_(delegate) {
(...skipping 12 matching lines...) Expand all
94 } 120 }
95 121
96 void FaviconHandler::FetchFavicon(const GURL& url) { 122 void FaviconHandler::FetchFavicon(const GURL& url) {
97 cancelable_consumer_.CancelAllRequests(); 123 cancelable_consumer_.CancelAllRequests();
98 124
99 url_ = url; 125 url_ = url;
100 126
101 favicon_expired_ = got_favicon_from_history_ = false; 127 favicon_expired_ = got_favicon_from_history_ = false;
102 current_url_index_ = 0; 128 current_url_index_ = 0;
103 urls_.clear(); 129 urls_.clear();
130 favicon_candidate_.icon_type = history::INVALID_ICON;
michaelbai 2012/03/15 22:39:53 It seemed that you should store the best matched c
stevenjb 2012/03/15 23:29:06 I believe that the call to CancelAllRequests() sho
sky 2012/03/16 22:15:09 CancelAllRequests only cancels requests to the fav
stevenjb 2012/03/16 22:31:29 My understanding is that FetchFavicon() will alway
104 131
105 // Request the favicon from the history service. In parallel to this the 132 // Request the favicon from the history service. In parallel to this the
106 // renderer is going to notify us (well TabContents) when the favicon url is 133 // renderer is going to notify us (well TabContents) when the favicon url is
107 // available. 134 // available.
108 if (GetFaviconService()) { 135 if (GetFaviconService()) {
109 GetFaviconForURL(url_, icon_types_, &cancelable_consumer_, 136 GetFaviconForURL(url_, icon_types_, &cancelable_consumer_,
110 base::Bind(&FaviconHandler::OnFaviconDataForInitialURL, 137 base::Bind(&FaviconHandler::OnFaviconDataForInitialURL,
111 base::Unretained(this))); 138 base::Unretained(this)));
112 } 139 }
113 } 140 }
114 141
115 int FaviconHandler::DownloadImage( 142 int FaviconHandler::DownloadImage(
116 const GURL& image_url, 143 const GURL& image_url,
117 int image_size, 144 int image_size,
118 history::IconType icon_type, 145 history::IconType icon_type,
119 const FaviconTabHelper::ImageDownloadCallback& callback) { 146 const FaviconTabHelper::ImageDownloadCallback& callback) {
120 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback); 147 return ScheduleDownload(GURL(), image_url, image_size, icon_type, callback);
121 } 148 }
122 149
123 FaviconService* FaviconHandler::GetFaviconService() { 150 FaviconService* FaviconHandler::GetFaviconService() {
124 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); 151 return profile_->GetFaviconService(Profile::EXPLICIT_ACCESS);
125 } 152 }
126 153
154 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url,
155 const GURL& image_url,
156 const gfx::Image& image,
157 history::IconType icon_type) {
158 bool update_candidate = false;
159 bool exact_match = false;
160 SkBitmap bitmap = *(image.ToSkBitmap());
161 int preferred_size = preferred_icon_size();
162 if (preferred_size == 0) {
163 update_candidate = true;
164 exact_match = true;
165 } else if (favicon_candidate_.icon_type == history::INVALID_ICON) {
166 // No current candidate, use this.
167 update_candidate = true;
168 } else {
169 int bitmap_size = std::max(bitmap.width(), bitmap.height());
170 if (bitmap_size == preferred_size) {
171 // Exact match, use this.
172 update_candidate = true;
173 exact_match = true;
174 } else {
175 // Compare against current candidate.
176 int cur_size = std::max(favicon_candidate_.bitmap.width(),
177 favicon_candidate_.bitmap.height());
178 if ((bitmap_size >= preferred_size && bitmap_size < cur_size) ||
179 (cur_size < preferred_size && bitmap_size > cur_size)) {
180 update_candidate = true;
181 }
182 }
183 }
184 if (update_candidate) {
185 favicon_candidate_ = FaviconCandidate(
186 url, image_url, image, bitmap, icon_type);
187 }
188 return exact_match;
189 }
190
127 void FaviconHandler::SetFavicon( 191 void FaviconHandler::SetFavicon(
128 const GURL& url, 192 const GURL& url,
129 const GURL& image_url, 193 const GURL& image_url,
130 const gfx::Image& image, 194 const gfx::Image& image,
195 const SkBitmap& bitmap,
131 history::IconType icon_type) { 196 history::IconType icon_type) {
132 const SkBitmap& bitmap = image;
133 const gfx::Image& sized_image = (preferred_icon_size() == 0 || 197 const gfx::Image& sized_image = (preferred_icon_size() == 0 ||
134 (preferred_icon_size() == bitmap.width() && 198 (preferred_icon_size() == bitmap.width() &&
135 preferred_icon_size() == bitmap.height())) ? 199 preferred_icon_size() == bitmap.height())) ?
136 image : ResizeFaviconIfNeeded(image); 200 image : ResizeFaviconIfNeeded(image);
137 201
138 if (GetFaviconService() && ShouldSaveFavicon(url)) { 202 if (GetFaviconService() && ShouldSaveFavicon(url)) {
139 std::vector<unsigned char> image_data; 203 std::vector<unsigned char> image_data;
140 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data)) 204 if (gfx::PNGEncodedDataFromImage(sized_image, &image_data))
141 SetHistoryFavicon(url, image_url, image_data, icon_type); 205 SetHistoryFavicon(url, image_url, image_data, icon_type);
142 } 206 }
143 207
144 if (url == url_ && icon_type == history::FAVICON) { 208 if (url == url_ && icon_type == history::FAVICON) {
145 NavigationEntry* entry = GetEntry(); 209 NavigationEntry* entry = GetEntry();
146 if (entry) 210 if (entry) {
211 entry->GetFavicon().url = image_url;
147 UpdateFavicon(entry, &sized_image); 212 UpdateFavicon(entry, &sized_image);
213 }
148 } 214 }
149 } 215 }
150 216
151 void FaviconHandler::UpdateFavicon(NavigationEntry* entry, 217 void FaviconHandler::UpdateFavicon(NavigationEntry* entry,
152 scoped_refptr<RefCountedMemory> data) { 218 scoped_refptr<RefCountedMemory> data) {
153 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(), 219 scoped_ptr<gfx::Image> image(gfx::ImageFromPNGEncodedData(data->front(),
154 data->size())); 220 data->size()));
155 UpdateFavicon(entry, image.get()); 221 UpdateFavicon(entry, image.get());
156 } 222 }
157 223
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 return; 294 return;
229 } 295 }
230 296
231 if (!i->second.callback.is_null()) { 297 if (!i->second.callback.is_null()) {
232 i->second.callback.Run(id, errored, *(&image)); 298 i->second.callback.Run(id, errored, *(&image));
233 } else if (current_candidate() && 299 } else if (current_candidate() &&
234 DoUrlAndIconMatch(*current_candidate(), image_url, 300 DoUrlAndIconMatch(*current_candidate(), image_url,
235 i->second.icon_type)) { 301 i->second.icon_type)) {
236 // The downloaded icon is still valid when there is no FaviconURL update 302 // The downloaded icon is still valid when there is no FaviconURL update
237 // during the downloading. 303 // during the downloading.
304 bool request_next_icon = true;
238 if (!errored) { 305 if (!errored) {
239 SetFavicon(i->second.url, image_url, image, i->second.icon_type); 306 bool exact_match = UpdateFaviconCandidate(
michaelbai 2012/03/15 22:39:53 nit: You don't need exact_match here.
stevenjb 2012/03/15 23:29:06 I thought the code would be more readable this way
240 } else if (GetEntry() && ++current_url_index_ < urls_.size()) { 307 i->second.url, image_url, image, i->second.icon_type);
241 // Copies all candidate except first one and notifies the FaviconHandler, 308 if (exact_match)
309 request_next_icon = false;
310 }
michaelbai 2012/03/15 22:39:53 You changed the logical here, previously, we didn'
stevenjb 2012/03/15 23:29:06 Previously we tried the remaining icons only if th
311 if (request_next_icon &&
312 (GetEntry() && ++current_url_index_ < urls_.size())) {
313 // Copies all candidates except the first and notifies the FaviconHandler,
242 // so the next candidate can be processed. 314 // so the next candidate can be processed.
243 std::vector<FaviconURL> new_candidates(urls_.begin() + 1, urls_.end()); 315 std::vector<FaviconURL> new_candidates(urls_.begin() + 1, urls_.end());
244 OnUpdateFaviconURL(0, new_candidates); 316 OnUpdateFaviconURL(0, new_candidates);
317 } else if (favicon_candidate_.icon_type != history::INVALID_ICON) {
318 // No more icons to request, set the favicon from the candidate.
319 SetFavicon(favicon_candidate_.url, favicon_candidate_.image_url,
320 favicon_candidate_.image, favicon_candidate_.bitmap,
321 favicon_candidate_.icon_type);
322 // Reset candidate.
323 favicon_candidate_.icon_type = history::INVALID_ICON;
michaelbai 2012/03/15 22:39:53 Could you reset all by favicon_candidate = Favicon
stevenjb 2012/03/15 23:29:06 Done.
245 } 324 }
246 } 325 }
247 download_requests_.erase(i); 326 download_requests_.erase(i);
248 } 327 }
249 328
250 NavigationEntry* FaviconHandler::GetEntry() { 329 NavigationEntry* FaviconHandler::GetEntry() {
251 NavigationEntry* entry = delegate_->GetActiveEntry(); 330 NavigationEntry* entry = delegate_->GetActiveEntry();
252 if (entry && entry->GetURL() == url_) 331 if (entry && entry->GetURL() == url_)
253 return entry; 332 return entry;
254 333
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 if (width > 0 && height > 0) { 530 if (width > 0 && height > 0) {
452 gfx::CalculateFaviconTargetSize(&width, &height); 531 gfx::CalculateFaviconTargetSize(&width, &height);
453 return gfx::Image(new SkBitmap( 532 return gfx::Image(new SkBitmap(
454 skia::ImageOperations::Resize( 533 skia::ImageOperations::Resize(
455 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, 534 bitmap, skia::ImageOperations::RESIZE_LANCZOS3,
456 width, height))); 535 width, height)));
457 } 536 }
458 537
459 return image; 538 return image;
460 } 539 }
OLDNEW
« no previous file with comments | « chrome/browser/favicon/favicon_handler.h ('k') | chrome/browser/favicon/favicon_handler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698