| 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/history/top_sites.h" | 5 #include "chrome/browser/history/top_sites.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 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 // Always remove the existing entry and then add it back. That way if we end | 246 // Always remove the existing entry and then add it back. That way if we end |
| 247 // up with too many temp thumbnails we'll prune the oldest first. | 247 // up with too many temp thumbnails we'll prune the oldest first. |
| 248 RemoveTemporaryThumbnailByURL(url); | 248 RemoveTemporaryThumbnailByURL(url); |
| 249 AddTemporaryThumbnail(url, thumbnail_data, score); | 249 AddTemporaryThumbnail(url, thumbnail_data, score); |
| 250 return true; | 250 return true; |
| 251 } | 251 } |
| 252 | 252 |
| 253 return SetPageThumbnailEncoded(url, thumbnail_data, score); | 253 return SetPageThumbnailEncoded(url, thumbnail_data, score); |
| 254 } | 254 } |
| 255 | 255 |
| 256 bool TopSites::SetPageThumbnailToJPEGBytes( |
| 257 const GURL& url, |
| 258 const base::RefCountedMemory* memory, |
| 259 const ThumbnailScore& score) { |
| 260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 261 |
| 262 if (!loaded_) { |
| 263 // TODO(sky): I need to cache these and apply them after the load |
| 264 // completes. |
| 265 return false; |
| 266 } |
| 267 |
| 268 bool add_temp_thumbnail = false; |
| 269 if (!IsKnownURL(url)) { |
| 270 if (!IsFull()) { |
| 271 add_temp_thumbnail = true; |
| 272 } else { |
| 273 return false; // This URL is not known to us. |
| 274 } |
| 275 } |
| 276 |
| 277 if (!HistoryService::CanAddURL(url)) |
| 278 return false; // It's not a real webpage. |
| 279 |
| 280 if (add_temp_thumbnail) { |
| 281 // Always remove the existing entry and then add it back. That way if we end |
| 282 // up with too many temp thumbnails we'll prune the oldest first. |
| 283 RemoveTemporaryThumbnailByURL(url); |
| 284 AddTemporaryThumbnail(url, memory, score); |
| 285 return true; |
| 286 } |
| 287 |
| 288 return SetPageThumbnailEncoded(url, memory, score); |
| 289 } |
| 290 |
| 256 // WARNING: this function may be invoked on any thread. | 291 // WARNING: this function may be invoked on any thread. |
| 257 void TopSites::GetMostVisitedURLs(const GetMostVisitedURLsCallback& callback) { | 292 void TopSites::GetMostVisitedURLs(const GetMostVisitedURLsCallback& callback) { |
| 258 MostVisitedURLList filtered_urls; | 293 MostVisitedURLList filtered_urls; |
| 259 { | 294 { |
| 260 base::AutoLock lock(lock_); | 295 base::AutoLock lock(lock_); |
| 261 if (!loaded_) { | 296 if (!loaded_) { |
| 262 // A request came in before we finished loading. Store the callback and | 297 // A request came in before we finished loading. Store the callback and |
| 263 // we'll run it on current thread when we finish loading. | 298 // we'll run it on current thread when we finish loading. |
| 264 pending_callbacks_.push_back( | 299 pending_callbacks_.push_back( |
| 265 base::Bind(&RunOrPostGetMostVisitedURLsCallback, | 300 base::Bind(&RunOrPostGetMostVisitedURLsCallback, |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 529 return loaded_ && cache_->IsKnownURL(url); | 564 return loaded_ && cache_->IsKnownURL(url); |
| 530 } | 565 } |
| 531 | 566 |
| 532 bool TopSites::IsFull() { | 567 bool TopSites::IsFull() { |
| 533 return loaded_ && cache_->top_sites().size() >= kTopSitesNumber; | 568 return loaded_ && cache_->top_sites().size() >= kTopSitesNumber; |
| 534 } | 569 } |
| 535 | 570 |
| 536 TopSites::~TopSites() { | 571 TopSites::~TopSites() { |
| 537 } | 572 } |
| 538 | 573 |
| 539 bool TopSites::SetPageThumbnailNoDB(const GURL& url, | 574 bool TopSites::SetPageThumbnailNoDB( |
| 540 const base::RefCountedBytes* thumbnail_data, | 575 const GURL& url, |
| 541 const ThumbnailScore& score) { | 576 const base::RefCountedMemory* thumbnail_data, |
| 577 const ThumbnailScore& score) { |
| 542 // This should only be invoked when we know about the url. | 578 // This should only be invoked when we know about the url. |
| 543 DCHECK(cache_->IsKnownURL(url)); | 579 DCHECK(cache_->IsKnownURL(url)); |
| 544 | 580 |
| 545 const MostVisitedURL& most_visited = | 581 const MostVisitedURL& most_visited = |
| 546 cache_->top_sites()[cache_->GetURLIndex(url)]; | 582 cache_->top_sites()[cache_->GetURLIndex(url)]; |
| 547 Images* image = cache_->GetImage(url); | 583 Images* image = cache_->GetImage(url); |
| 548 | 584 |
| 549 // When comparing the thumbnail scores, we need to take into account the | 585 // When comparing the thumbnail scores, we need to take into account the |
| 550 // redirect hops, which are not generated when the thumbnail is because the | 586 // redirect hops, which are not generated when the thumbnail is because the |
| 551 // redirects weren't known. We fill that in here since we know the redirects. | 587 // redirects weren't known. We fill that in here since we know the redirects. |
| 552 ThumbnailScore new_score_with_redirects(score); | 588 ThumbnailScore new_score_with_redirects(score); |
| 553 new_score_with_redirects.redirect_hops_from_dest = | 589 new_score_with_redirects.redirect_hops_from_dest = |
| 554 GetRedirectDistanceForURL(most_visited, url); | 590 GetRedirectDistanceForURL(most_visited, url); |
| 555 | 591 |
| 556 if (!ShouldReplaceThumbnailWith(image->thumbnail_score, | 592 if (!ShouldReplaceThumbnailWith(image->thumbnail_score, |
| 557 new_score_with_redirects) && | 593 new_score_with_redirects) && |
| 558 image->thumbnail.get()) | 594 image->thumbnail.get()) |
| 559 return false; // The one we already have is better. | 595 return false; // The one we already have is better. |
| 560 | 596 |
| 561 image->thumbnail = const_cast<base::RefCountedBytes*>(thumbnail_data); | 597 image->thumbnail = const_cast<base::RefCountedMemory*>(thumbnail_data); |
| 562 image->thumbnail_score = new_score_with_redirects; | 598 image->thumbnail_score = new_score_with_redirects; |
| 563 | 599 |
| 564 ResetThreadSafeImageCache(); | 600 ResetThreadSafeImageCache(); |
| 565 return true; | 601 return true; |
| 566 } | 602 } |
| 567 | 603 |
| 568 bool TopSites::SetPageThumbnailEncoded(const GURL& url, | 604 bool TopSites::SetPageThumbnailEncoded(const GURL& url, |
| 569 const base::RefCountedBytes* thumbnail, | 605 const base::RefCountedMemory* thumbnail, |
| 570 const ThumbnailScore& score) { | 606 const ThumbnailScore& score) { |
| 571 if (!SetPageThumbnailNoDB(url, thumbnail, score)) | 607 if (!SetPageThumbnailNoDB(url, thumbnail, score)) |
| 572 return false; | 608 return false; |
| 573 | 609 |
| 574 // Update the database. | 610 // Update the database. |
| 575 if (!cache_->IsKnownURL(url)) | 611 if (!cache_->IsKnownURL(url)) |
| 576 return false; | 612 return false; |
| 577 | 613 |
| 578 size_t index = cache_->GetURLIndex(url); | 614 size_t index = cache_->GetURLIndex(url); |
| 579 const MostVisitedURL& most_visited = cache_->top_sites()[index]; | 615 const MostVisitedURL& most_visited = cache_->top_sites()[index]; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 604 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end(); | 640 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end(); |
| 605 ++i) { | 641 ++i) { |
| 606 if (i->first == url) { | 642 if (i->first == url) { |
| 607 temp_images_.erase(i); | 643 temp_images_.erase(i); |
| 608 return; | 644 return; |
| 609 } | 645 } |
| 610 } | 646 } |
| 611 } | 647 } |
| 612 | 648 |
| 613 void TopSites::AddTemporaryThumbnail(const GURL& url, | 649 void TopSites::AddTemporaryThumbnail(const GURL& url, |
| 614 const base::RefCountedBytes* thumbnail, | 650 const base::RefCountedMemory* thumbnail, |
| 615 const ThumbnailScore& score) { | 651 const ThumbnailScore& score) { |
| 616 if (temp_images_.size() == kMaxTempTopImages) | 652 if (temp_images_.size() == kMaxTempTopImages) |
| 617 temp_images_.erase(temp_images_.begin()); | 653 temp_images_.erase(temp_images_.begin()); |
| 618 | 654 |
| 619 TempImage image; | 655 TempImage image; |
| 620 image.first = url; | 656 image.first = url; |
| 621 image.second.thumbnail = const_cast<base::RefCountedBytes*>(thumbnail); | 657 image.second.thumbnail = const_cast<base::RefCountedMemory*>(thumbnail); |
| 622 image.second.thumbnail_score = score; | 658 image.second.thumbnail_score = score; |
| 623 temp_images_.push_back(image); | 659 temp_images_.push_back(image); |
| 624 } | 660 } |
| 625 | 661 |
| 626 void TopSites::TimerFired() { | 662 void TopSites::TimerFired() { |
| 627 StartQueryForMostVisited(); | 663 StartQueryForMostVisited(); |
| 628 } | 664 } |
| 629 | 665 |
| 630 // static | 666 // static |
| 631 int TopSites::GetRedirectDistanceForURL(const MostVisitedURL& most_visited, | 667 int TopSites::GetRedirectDistanceForURL(const MostVisitedURL& most_visited, |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 915 SetTopSites(pages); | 951 SetTopSites(pages); |
| 916 | 952 |
| 917 // Used only in testing. | 953 // Used only in testing. |
| 918 content::NotificationService::current()->Notify( | 954 content::NotificationService::current()->Notify( |
| 919 chrome::NOTIFICATION_TOP_SITES_UPDATED, | 955 chrome::NOTIFICATION_TOP_SITES_UPDATED, |
| 920 content::Source<TopSites>(this), | 956 content::Source<TopSites>(this), |
| 921 content::Details<CancelableRequestProvider::Handle>(&handle)); | 957 content::Details<CancelableRequestProvider::Handle>(&handle)); |
| 922 } | 958 } |
| 923 | 959 |
| 924 } // namespace history | 960 } // namespace history |
| OLD | NEW |