| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/history/top_sites_likely_impl.h" | 5 #include "chrome/browser/history/top_sites_likely_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 // Always remove the existing entry and then add it back. That way if we end | 226 // Always remove the existing entry and then add it back. That way if we end |
| 227 // up with too many temp thumbnails we'll prune the oldest first. | 227 // up with too many temp thumbnails we'll prune the oldest first. |
| 228 RemoveTemporaryThumbnailByURL(url); | 228 RemoveTemporaryThumbnailByURL(url); |
| 229 AddTemporaryThumbnail(url, thumbnail_data, score); | 229 AddTemporaryThumbnail(url, thumbnail_data, score); |
| 230 return true; | 230 return true; |
| 231 } | 231 } |
| 232 | 232 |
| 233 return SetPageThumbnailEncoded(url, thumbnail_data, score); | 233 return SetPageThumbnailEncoded(url, thumbnail_data, score); |
| 234 } | 234 } |
| 235 | 235 |
| 236 bool TopSitesLikelyImpl::SetPageThumbnailToJPEGBytes( |
| 237 const GURL& url, |
| 238 const base::RefCountedMemory* memory, |
| 239 const ThumbnailScore& score) { |
| 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 241 |
| 242 if (!loaded_) { |
| 243 // TODO(sky): I need to cache these and apply them after the load |
| 244 // completes. |
| 245 return false; |
| 246 } |
| 247 |
| 248 bool add_temp_thumbnail = false; |
| 249 if (!IsKnownURL(url)) { |
| 250 if (!IsFull()) { |
| 251 add_temp_thumbnail = true; |
| 252 } else { |
| 253 return false; // This URL is not known to us. |
| 254 } |
| 255 } |
| 256 |
| 257 if (!HistoryService::CanAddURL(url)) |
| 258 return false; // It's not a real webpage. |
| 259 |
| 260 if (add_temp_thumbnail) { |
| 261 // Always remove the existing entry and then add it back. That way if we end |
| 262 // up with too many temp thumbnails we'll prune the oldest first. |
| 263 RemoveTemporaryThumbnailByURL(url); |
| 264 AddTemporaryThumbnail(url, memory, score); |
| 265 return true; |
| 266 } |
| 267 |
| 268 return SetPageThumbnailEncoded(url, memory, score); |
| 269 } |
| 270 |
| 236 // WARNING: this function may be invoked on any thread. | 271 // WARNING: this function may be invoked on any thread. |
| 237 void TopSitesLikelyImpl::GetMostVisitedURLs( | 272 void TopSitesLikelyImpl::GetMostVisitedURLs( |
| 238 const GetMostVisitedURLsCallback& callback) { | 273 const GetMostVisitedURLsCallback& callback) { |
| 239 MostVisitedURLList filtered_urls; | 274 MostVisitedURLList filtered_urls; |
| 240 { | 275 { |
| 241 base::AutoLock lock(lock_); | 276 base::AutoLock lock(lock_); |
| 242 if (!loaded_) { | 277 if (!loaded_) { |
| 243 // A request came in before we finished loading. Store the callback and | 278 // A request came in before we finished loading. Store the callback and |
| 244 // we'll run it on current thread when we finish loading. | 279 // we'll run it on current thread when we finish loading. |
| 245 pending_callbacks_.push_back( | 280 pending_callbacks_.push_back( |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 | 549 |
| 515 bool TopSitesLikelyImpl::IsFull() { | 550 bool TopSitesLikelyImpl::IsFull() { |
| 516 return loaded_ && cache_->top_sites().size() >= kTopSitesNumber; | 551 return loaded_ && cache_->top_sites().size() >= kTopSitesNumber; |
| 517 } | 552 } |
| 518 | 553 |
| 519 TopSitesLikelyImpl::~TopSitesLikelyImpl() { | 554 TopSitesLikelyImpl::~TopSitesLikelyImpl() { |
| 520 } | 555 } |
| 521 | 556 |
| 522 bool TopSitesLikelyImpl::SetPageThumbnailNoDB( | 557 bool TopSitesLikelyImpl::SetPageThumbnailNoDB( |
| 523 const GURL& url, | 558 const GURL& url, |
| 524 const base::RefCountedBytes* thumbnail_data, | 559 const base::RefCountedMemory* thumbnail_data, |
| 525 const ThumbnailScore& score) { | 560 const ThumbnailScore& score) { |
| 526 // This should only be invoked when we know about the url. | 561 // This should only be invoked when we know about the url. |
| 527 DCHECK(cache_->IsKnownURL(url)); | 562 DCHECK(cache_->IsKnownURL(url)); |
| 528 | 563 |
| 529 const MostVisitedURL& most_visited = | 564 const MostVisitedURL& most_visited = |
| 530 cache_->top_sites()[cache_->GetURLIndex(url)]; | 565 cache_->top_sites()[cache_->GetURLIndex(url)]; |
| 531 Images* image = cache_->GetImage(url); | 566 Images* image = cache_->GetImage(url); |
| 532 | 567 |
| 533 // When comparing the thumbnail scores, we need to take into account the | 568 // When comparing the thumbnail scores, we need to take into account the |
| 534 // redirect hops, which are not generated when the thumbnail is because the | 569 // redirect hops, which are not generated when the thumbnail is because the |
| 535 // redirects weren't known. We fill that in here since we know the redirects. | 570 // redirects weren't known. We fill that in here since we know the redirects. |
| 536 ThumbnailScore new_score_with_redirects(score); | 571 ThumbnailScore new_score_with_redirects(score); |
| 537 new_score_with_redirects.redirect_hops_from_dest = | 572 new_score_with_redirects.redirect_hops_from_dest = |
| 538 GetRedirectDistanceForURL(most_visited, url); | 573 GetRedirectDistanceForURL(most_visited, url); |
| 539 | 574 |
| 540 if (!ShouldReplaceThumbnailWith(image->thumbnail_score, | 575 if (!ShouldReplaceThumbnailWith(image->thumbnail_score, |
| 541 new_score_with_redirects) && | 576 new_score_with_redirects) && |
| 542 image->thumbnail.get()) | 577 image->thumbnail.get()) |
| 543 return false; // The one we already have is better. | 578 return false; // The one we already have is better. |
| 544 | 579 |
| 545 image->thumbnail = const_cast<base::RefCountedBytes*>(thumbnail_data); | 580 image->thumbnail = const_cast<base::RefCountedMemory*>(thumbnail_data); |
| 546 image->thumbnail_score = new_score_with_redirects; | 581 image->thumbnail_score = new_score_with_redirects; |
| 547 | 582 |
| 548 ResetThreadSafeImageCache(); | 583 ResetThreadSafeImageCache(); |
| 549 return true; | 584 return true; |
| 550 } | 585 } |
| 551 | 586 |
| 552 bool TopSitesLikelyImpl::SetPageThumbnailEncoded( | 587 bool TopSitesLikelyImpl::SetPageThumbnailEncoded( |
| 553 const GURL& url, | 588 const GURL& url, |
| 554 const base::RefCountedBytes* thumbnail, | 589 const base::RefCountedMemory* thumbnail, |
| 555 const ThumbnailScore& score) { | 590 const ThumbnailScore& score) { |
| 556 if (!SetPageThumbnailNoDB(url, thumbnail, score)) | 591 if (!SetPageThumbnailNoDB(url, thumbnail, score)) |
| 557 return false; | 592 return false; |
| 558 | 593 |
| 559 // Update the database. | 594 // Update the database. |
| 560 if (!cache_->IsKnownURL(url)) | 595 if (!cache_->IsKnownURL(url)) |
| 561 return false; | 596 return false; |
| 562 | 597 |
| 563 size_t index = cache_->GetURLIndex(url); | 598 size_t index = cache_->GetURLIndex(url); |
| 564 const MostVisitedURL& most_visited = cache_->top_sites()[index]; | 599 const MostVisitedURL& most_visited = cache_->top_sites()[index]; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 588 void TopSitesLikelyImpl::RemoveTemporaryThumbnailByURL(const GURL& url) { | 623 void TopSitesLikelyImpl::RemoveTemporaryThumbnailByURL(const GURL& url) { |
| 589 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end(); | 624 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end(); |
| 590 ++i) { | 625 ++i) { |
| 591 if (i->first == url) { | 626 if (i->first == url) { |
| 592 temp_images_.erase(i); | 627 temp_images_.erase(i); |
| 593 return; | 628 return; |
| 594 } | 629 } |
| 595 } | 630 } |
| 596 } | 631 } |
| 597 | 632 |
| 598 void TopSitesLikelyImpl::AddTemporaryThumbnail(const GURL& url, | 633 void TopSitesLikelyImpl::AddTemporaryThumbnail( |
| 599 const base::RefCountedBytes* thumbnail, | 634 const GURL& url, |
| 600 const ThumbnailScore& score) { | 635 const base::RefCountedMemory* thumbnail, |
| 636 const ThumbnailScore& score) { |
| 601 if (temp_images_.size() == kMaxTempTopImages) | 637 if (temp_images_.size() == kMaxTempTopImages) |
| 602 temp_images_.erase(temp_images_.begin()); | 638 temp_images_.erase(temp_images_.begin()); |
| 603 | 639 |
| 604 TempImage image; | 640 TempImage image; |
| 605 image.first = url; | 641 image.first = url; |
| 606 image.second.thumbnail = const_cast<base::RefCountedBytes*>(thumbnail); | 642 image.second.thumbnail = const_cast<base::RefCountedMemory*>(thumbnail); |
| 607 image.second.thumbnail_score = score; | 643 image.second.thumbnail_score = score; |
| 608 temp_images_.push_back(image); | 644 temp_images_.push_back(image); |
| 609 } | 645 } |
| 610 | 646 |
| 611 void TopSitesLikelyImpl::TimerFired() { | 647 void TopSitesLikelyImpl::TimerFired() { |
| 612 StartQueryForMostVisited(); | 648 StartQueryForMostVisited(); |
| 613 } | 649 } |
| 614 | 650 |
| 615 // static | 651 // static |
| 616 int TopSitesLikelyImpl::GetRedirectDistanceForURL( | 652 int TopSitesLikelyImpl::GetRedirectDistanceForURL( |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 905 SetTopSites(pages); | 941 SetTopSites(pages); |
| 906 | 942 |
| 907 // Used only in testing. | 943 // Used only in testing. |
| 908 content::NotificationService::current()->Notify( | 944 content::NotificationService::current()->Notify( |
| 909 chrome::NOTIFICATION_TOP_SITES_UPDATED, | 945 chrome::NOTIFICATION_TOP_SITES_UPDATED, |
| 910 content::Source<TopSitesLikelyImpl>(this), | 946 content::Source<TopSitesLikelyImpl>(this), |
| 911 content::Details<CancelableRequestProvider::Handle>(&handle)); | 947 content::Details<CancelableRequestProvider::Handle>(&handle)); |
| 912 } | 948 } |
| 913 | 949 |
| 914 } // namespace history | 950 } // namespace history |
| OLD | NEW |