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