OLD | NEW |
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 <algorithm> | 9 #include <algorithm> |
10 #include <vector> | 10 #include <vector> |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 : url(url), | 204 : url(url), |
205 image_url(image_url), | 205 image_url(image_url), |
206 image(image), | 206 image(image), |
207 score(score), | 207 score(score), |
208 icon_type(icon_type) { | 208 icon_type(icon_type) { |
209 } | 209 } |
210 | 210 |
211 //////////////////////////////////////////////////////////////////////////////// | 211 //////////////////////////////////////////////////////////////////////////////// |
212 | 212 |
213 FaviconHandler::FaviconHandler(Profile* profile, | 213 FaviconHandler::FaviconHandler(Profile* profile, |
| 214 FaviconClient* client, |
214 FaviconDriver* driver, | 215 FaviconDriver* driver, |
215 FaviconHandlerDelegate* delegate, | 216 FaviconHandlerDelegate* delegate, |
216 Type icon_type) | 217 Type icon_type) |
217 : got_favicon_from_history_(false), | 218 : got_favicon_from_history_(false), |
218 favicon_expired_or_incomplete_(false), | 219 favicon_expired_or_incomplete_(false), |
219 icon_types_(icon_type == FAVICON | 220 icon_types_(icon_type == FAVICON |
220 ? chrome::FAVICON | 221 ? chrome::FAVICON |
221 : chrome::TOUCH_ICON | chrome::TOUCH_PRECOMPOSED_ICON), | 222 : chrome::TOUCH_ICON | chrome::TOUCH_PRECOMPOSED_ICON), |
222 profile_(profile), | 223 profile_(profile), |
| 224 client_(client), |
223 driver_(driver), | 225 driver_(driver), |
224 delegate_(delegate) { | 226 delegate_(delegate) { |
225 DCHECK(profile_); | 227 DCHECK(profile_); |
226 DCHECK(driver_); | 228 DCHECK(driver_); |
227 DCHECK(delegate_); | 229 DCHECK(delegate_); |
228 } | 230 } |
229 | 231 |
230 FaviconHandler::~FaviconHandler() { | 232 FaviconHandler::~FaviconHandler() { |
231 } | 233 } |
232 | 234 |
233 void FaviconHandler::FetchFavicon(const GURL& url) { | 235 void FaviconHandler::FetchFavicon(const GURL& url) { |
234 cancelable_task_tracker_.TryCancelAll(); | 236 cancelable_task_tracker_.TryCancelAll(); |
235 | 237 |
236 url_ = url; | 238 url_ = url; |
237 | 239 |
238 favicon_expired_or_incomplete_ = got_favicon_from_history_ = false; | 240 favicon_expired_or_incomplete_ = got_favicon_from_history_ = false; |
239 image_urls_.clear(); | 241 image_urls_.clear(); |
240 | 242 |
241 // Request the favicon from the history service. In parallel to this the | 243 // Request the favicon from the history service. In parallel to this the |
242 // renderer is going to notify us (well WebContents) when the favicon url is | 244 // renderer is going to notify us (well WebContents) when the favicon url is |
243 // available. | 245 // available. |
244 if (GetFaviconService()) { | 246 if (client_->GetFaviconService()) { |
245 GetFaviconForURLFromFaviconService( | 247 GetFaviconForURLFromFaviconService( |
246 url_, | 248 url_, |
247 icon_types_, | 249 icon_types_, |
248 base::Bind( | 250 base::Bind( |
249 &FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, | 251 &FaviconHandler::OnFaviconDataForInitialURLFromFaviconService, |
250 base::Unretained(this)), | 252 base::Unretained(this)), |
251 &cancelable_task_tracker_); | 253 &cancelable_task_tracker_); |
252 } | 254 } |
253 } | 255 } |
254 | 256 |
255 FaviconService* FaviconHandler::GetFaviconService() { | |
256 return FaviconServiceFactory::GetForProfile( | |
257 profile_, Profile::EXPLICIT_ACCESS); | |
258 } | |
259 | |
260 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, | 257 bool FaviconHandler::UpdateFaviconCandidate(const GURL& url, |
261 const GURL& image_url, | 258 const GURL& image_url, |
262 const gfx::Image& image, | 259 const gfx::Image& image, |
263 float score, | 260 float score, |
264 chrome::IconType icon_type) { | 261 chrome::IconType icon_type) { |
265 const bool exact_match = score == 1 || preferred_icon_size() == 0; | 262 const bool exact_match = score == 1 || preferred_icon_size() == 0; |
266 if (exact_match || | 263 if (exact_match || |
267 best_favicon_candidate_.icon_type == chrome::INVALID_ICON || | 264 best_favicon_candidate_.icon_type == chrome::INVALID_ICON || |
268 score > best_favicon_candidate_.score) { | 265 score > best_favicon_candidate_.score) { |
269 best_favicon_candidate_ = FaviconCandidate( | 266 best_favicon_candidate_ = FaviconCandidate( |
270 url, image_url, image, score, icon_type); | 267 url, image_url, image, score, icon_type); |
271 } | 268 } |
272 return exact_match; | 269 return exact_match; |
273 } | 270 } |
274 | 271 |
275 void FaviconHandler::SetFavicon( | 272 void FaviconHandler::SetFavicon( |
276 const GURL& url, | 273 const GURL& url, |
277 const GURL& icon_url, | 274 const GURL& icon_url, |
278 const gfx::Image& image, | 275 const gfx::Image& image, |
279 chrome::IconType icon_type) { | 276 chrome::IconType icon_type) { |
280 if (GetFaviconService() && ShouldSaveFavicon(url)) | 277 if (client_->GetFaviconService() && ShouldSaveFavicon(url)) |
281 SetHistoryFavicons(url, icon_url, icon_type, image); | 278 SetHistoryFavicons(url, icon_url, icon_type, image); |
282 | 279 |
283 if (UrlMatches(url, url_) && icon_type == chrome::FAVICON) { | 280 if (UrlMatches(url, url_) && icon_type == chrome::FAVICON) { |
284 NavigationEntry* entry = GetEntry(); | 281 NavigationEntry* entry = GetEntry(); |
285 if (entry) | 282 if (entry) |
286 SetFaviconOnNavigationEntry(entry, icon_url, image); | 283 SetFaviconOnNavigationEntry(entry, icon_url, image); |
287 } | 284 } |
288 } | 285 } |
289 | 286 |
290 void FaviconHandler::SetFaviconOnNavigationEntry( | 287 void FaviconHandler::SetFaviconOnNavigationEntry( |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 i != candidates.end(); ++i) { | 327 i != candidates.end(); ++i) { |
331 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_)) | 328 if (!i->icon_url.is_empty() && (i->icon_type & icon_types_)) |
332 image_urls_.push_back(*i); | 329 image_urls_.push_back(*i); |
333 } | 330 } |
334 | 331 |
335 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 332 // TODO(davemoore) Should clear on empty url. Currently we ignore it. |
336 // This appears to be what FF does as well. | 333 // This appears to be what FF does as well. |
337 if (image_urls_.empty()) | 334 if (image_urls_.empty()) |
338 return; | 335 return; |
339 | 336 |
340 if (!GetFaviconService()) | 337 if (!client_->GetFaviconService()) |
341 return; | 338 return; |
342 | 339 |
343 ProcessCurrentUrl(); | 340 ProcessCurrentUrl(); |
344 } | 341 } |
345 | 342 |
346 void FaviconHandler::ProcessCurrentUrl() { | 343 void FaviconHandler::ProcessCurrentUrl() { |
347 DCHECK(!image_urls_.empty()); | 344 DCHECK(!image_urls_.empty()); |
348 | 345 |
349 NavigationEntry* entry = GetEntry(); | 346 NavigationEntry* entry = GetEntry(); |
350 if (!entry) | 347 if (!entry) |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 void FaviconHandler::UpdateFaviconMappingAndFetch( | 434 void FaviconHandler::UpdateFaviconMappingAndFetch( |
438 const GURL& page_url, | 435 const GURL& page_url, |
439 const GURL& icon_url, | 436 const GURL& icon_url, |
440 chrome::IconType icon_type, | 437 chrome::IconType icon_type, |
441 const FaviconService::FaviconResultsCallback& callback, | 438 const FaviconService::FaviconResultsCallback& callback, |
442 base::CancelableTaskTracker* tracker) { | 439 base::CancelableTaskTracker* tracker) { |
443 // TODO(pkotwicz): pass in all of |image_urls_| to | 440 // TODO(pkotwicz): pass in all of |image_urls_| to |
444 // UpdateFaviconMappingsAndFetch(). | 441 // UpdateFaviconMappingsAndFetch(). |
445 std::vector<GURL> icon_urls; | 442 std::vector<GURL> icon_urls; |
446 icon_urls.push_back(icon_url); | 443 icon_urls.push_back(icon_url); |
447 GetFaviconService()->UpdateFaviconMappingsAndFetch( | 444 client_->GetFaviconService()->UpdateFaviconMappingsAndFetch( |
448 page_url, icon_urls, icon_type, preferred_icon_size(), callback, tracker); | 445 page_url, icon_urls, icon_type, preferred_icon_size(), callback, tracker); |
449 } | 446 } |
450 | 447 |
451 void FaviconHandler::GetFaviconFromFaviconService( | 448 void FaviconHandler::GetFaviconFromFaviconService( |
452 const GURL& icon_url, | 449 const GURL& icon_url, |
453 chrome::IconType icon_type, | 450 chrome::IconType icon_type, |
454 const FaviconService::FaviconResultsCallback& callback, | 451 const FaviconService::FaviconResultsCallback& callback, |
455 base::CancelableTaskTracker* tracker) { | 452 base::CancelableTaskTracker* tracker) { |
456 GetFaviconService()->GetFavicon( | 453 client_->GetFaviconService()->GetFavicon( |
457 icon_url, icon_type, preferred_icon_size(), callback, tracker); | 454 icon_url, icon_type, preferred_icon_size(), callback, tracker); |
458 } | 455 } |
459 | 456 |
460 void FaviconHandler::GetFaviconForURLFromFaviconService( | 457 void FaviconHandler::GetFaviconForURLFromFaviconService( |
461 const GURL& page_url, | 458 const GURL& page_url, |
462 int icon_types, | 459 int icon_types, |
463 const FaviconService::FaviconResultsCallback& callback, | 460 const FaviconService::FaviconResultsCallback& callback, |
464 base::CancelableTaskTracker* tracker) { | 461 base::CancelableTaskTracker* tracker) { |
465 GetFaviconService()->GetFaviconForURL( | 462 client_->GetFaviconService()->GetFaviconForURL( |
466 FaviconService::FaviconForURLParams(page_url, icon_types, | 463 FaviconService::FaviconForURLParams( |
467 preferred_icon_size()), | 464 page_url, icon_types, preferred_icon_size()), |
468 callback, | 465 callback, |
469 tracker); | 466 tracker); |
470 } | 467 } |
471 | 468 |
472 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, | 469 void FaviconHandler::SetHistoryFavicons(const GURL& page_url, |
473 const GURL& icon_url, | 470 const GURL& icon_url, |
474 chrome::IconType icon_type, | 471 chrome::IconType icon_type, |
475 const gfx::Image& image) { | 472 const gfx::Image& image) { |
476 GetFaviconService()->SetFavicons(page_url, icon_url, icon_type, image); | 473 client_->GetFaviconService()->SetFavicons( |
| 474 page_url, icon_url, icon_type, image); |
477 } | 475 } |
478 | 476 |
479 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { | 477 bool FaviconHandler::ShouldSaveFavicon(const GURL& url) { |
480 if (!driver_->IsOffTheRecord()) | 478 if (!driver_->IsOffTheRecord()) |
481 return true; | 479 return true; |
482 | 480 |
483 // Otherwise store the favicon if the page is bookmarked. | 481 // Otherwise store the favicon if the page is bookmarked. |
484 BookmarkService* bookmark_service = | 482 BookmarkService* bookmark_service = |
485 BookmarkService::FromBrowserContext(profile_); | 483 BookmarkService::FromBrowserContext(profile_); |
486 return bookmark_service && bookmark_service->IsBookmarked(url); | 484 return bookmark_service && bookmark_service->IsBookmarked(url); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 // renderer to download the icon. | 541 // renderer to download the icon. |
544 } | 542 } |
545 | 543 |
546 void FaviconHandler::DownloadFaviconOrAskFaviconService( | 544 void FaviconHandler::DownloadFaviconOrAskFaviconService( |
547 const GURL& page_url, | 545 const GURL& page_url, |
548 const GURL& icon_url, | 546 const GURL& icon_url, |
549 chrome::IconType icon_type) { | 547 chrome::IconType icon_type) { |
550 if (favicon_expired_or_incomplete_) { | 548 if (favicon_expired_or_incomplete_) { |
551 // We have the mapping, but the favicon is out of date. Download it now. | 549 // We have the mapping, but the favicon is out of date. Download it now. |
552 ScheduleDownload(page_url, icon_url, icon_type); | 550 ScheduleDownload(page_url, icon_url, icon_type); |
553 } else if (GetFaviconService()) { | 551 } else if (client_->GetFaviconService()) { |
554 // We don't know the favicon, but we may have previously downloaded the | 552 // We don't know the favicon, but we may have previously downloaded the |
555 // favicon for another page that shares the same favicon. Ask for the | 553 // favicon for another page that shares the same favicon. Ask for the |
556 // favicon given the favicon URL. | 554 // favicon given the favicon URL. |
557 if (driver_->IsOffTheRecord()) { | 555 if (driver_->IsOffTheRecord()) { |
558 GetFaviconFromFaviconService( | 556 GetFaviconFromFaviconService( |
559 icon_url, icon_type, | 557 icon_url, icon_type, |
560 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), | 558 base::Bind(&FaviconHandler::OnFaviconData, base::Unretained(this)), |
561 &cancelable_task_tracker_); | 559 &cancelable_task_tracker_); |
562 } else { | 560 } else { |
563 // Ask the history service for the icon. This does two things: | 561 // Ask the history service for the icon. This does two things: |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 GetMaximalIconSize(icon_type)); | 615 GetMaximalIconSize(icon_type)); |
618 if (download_id) { | 616 if (download_id) { |
619 // Download ids should be unique. | 617 // Download ids should be unique. |
620 DCHECK(download_requests_.find(download_id) == download_requests_.end()); | 618 DCHECK(download_requests_.find(download_id) == download_requests_.end()); |
621 download_requests_[download_id] = | 619 download_requests_[download_id] = |
622 DownloadRequest(url, image_url, icon_type); | 620 DownloadRequest(url, image_url, icon_type); |
623 } | 621 } |
624 | 622 |
625 return download_id; | 623 return download_id; |
626 } | 624 } |
OLD | NEW |