| 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_impl.h" | 5 #include "chrome/browser/history/top_sites_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 TopSitesImpl::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 TopSitesImpl::GetMostVisitedURLs( | 272 void TopSitesImpl::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 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 | 547 |
| 513 bool TopSitesImpl::IsFull() { | 548 bool TopSitesImpl::IsFull() { |
| 514 return loaded_ && cache_->top_sites().size() >= kTopSitesNumber; | 549 return loaded_ && cache_->top_sites().size() >= kTopSitesNumber; |
| 515 } | 550 } |
| 516 | 551 |
| 517 TopSitesImpl::~TopSitesImpl() { | 552 TopSitesImpl::~TopSitesImpl() { |
| 518 } | 553 } |
| 519 | 554 |
| 520 bool TopSitesImpl::SetPageThumbnailNoDB( | 555 bool TopSitesImpl::SetPageThumbnailNoDB( |
| 521 const GURL& url, | 556 const GURL& url, |
| 522 const base::RefCountedBytes* thumbnail_data, | 557 const base::RefCountedMemory* thumbnail_data, |
| 523 const ThumbnailScore& score) { | 558 const ThumbnailScore& score) { |
| 524 // This should only be invoked when we know about the url. | 559 // This should only be invoked when we know about the url. |
| 525 DCHECK(cache_->IsKnownURL(url)); | 560 DCHECK(cache_->IsKnownURL(url)); |
| 526 | 561 |
| 527 const MostVisitedURL& most_visited = | 562 const MostVisitedURL& most_visited = |
| 528 cache_->top_sites()[cache_->GetURLIndex(url)]; | 563 cache_->top_sites()[cache_->GetURLIndex(url)]; |
| 529 Images* image = cache_->GetImage(url); | 564 Images* image = cache_->GetImage(url); |
| 530 | 565 |
| 531 // When comparing the thumbnail scores, we need to take into account the | 566 // When comparing the thumbnail scores, we need to take into account the |
| 532 // redirect hops, which are not generated when the thumbnail is because the | 567 // redirect hops, which are not generated when the thumbnail is because the |
| 533 // redirects weren't known. We fill that in here since we know the redirects. | 568 // redirects weren't known. We fill that in here since we know the redirects. |
| 534 ThumbnailScore new_score_with_redirects(score); | 569 ThumbnailScore new_score_with_redirects(score); |
| 535 new_score_with_redirects.redirect_hops_from_dest = | 570 new_score_with_redirects.redirect_hops_from_dest = |
| 536 GetRedirectDistanceForURL(most_visited, url); | 571 GetRedirectDistanceForURL(most_visited, url); |
| 537 | 572 |
| 538 if (!ShouldReplaceThumbnailWith(image->thumbnail_score, | 573 if (!ShouldReplaceThumbnailWith(image->thumbnail_score, |
| 539 new_score_with_redirects) && | 574 new_score_with_redirects) && |
| 540 image->thumbnail.get()) | 575 image->thumbnail.get()) |
| 541 return false; // The one we already have is better. | 576 return false; // The one we already have is better. |
| 542 | 577 |
| 543 image->thumbnail = const_cast<base::RefCountedBytes*>(thumbnail_data); | 578 image->thumbnail = const_cast<base::RefCountedMemory*>(thumbnail_data); |
| 544 image->thumbnail_score = new_score_with_redirects; | 579 image->thumbnail_score = new_score_with_redirects; |
| 545 | 580 |
| 546 ResetThreadSafeImageCache(); | 581 ResetThreadSafeImageCache(); |
| 547 return true; | 582 return true; |
| 548 } | 583 } |
| 549 | 584 |
| 550 bool TopSitesImpl::SetPageThumbnailEncoded( | 585 bool TopSitesImpl::SetPageThumbnailEncoded( |
| 551 const GURL& url, | 586 const GURL& url, |
| 552 const base::RefCountedBytes* thumbnail, | 587 const base::RefCountedMemory* thumbnail, |
| 553 const ThumbnailScore& score) { | 588 const ThumbnailScore& score) { |
| 554 if (!SetPageThumbnailNoDB(url, thumbnail, score)) | 589 if (!SetPageThumbnailNoDB(url, thumbnail, score)) |
| 555 return false; | 590 return false; |
| 556 | 591 |
| 557 // Update the database. | 592 // Update the database. |
| 558 if (!cache_->IsKnownURL(url)) | 593 if (!cache_->IsKnownURL(url)) |
| 559 return false; | 594 return false; |
| 560 | 595 |
| 561 size_t index = cache_->GetURLIndex(url); | 596 size_t index = cache_->GetURLIndex(url); |
| 562 const MostVisitedURL& most_visited = cache_->top_sites()[index]; | 597 const MostVisitedURL& most_visited = cache_->top_sites()[index]; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 586 void TopSitesImpl::RemoveTemporaryThumbnailByURL(const GURL& url) { | 621 void TopSitesImpl::RemoveTemporaryThumbnailByURL(const GURL& url) { |
| 587 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end(); | 622 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end(); |
| 588 ++i) { | 623 ++i) { |
| 589 if (i->first == url) { | 624 if (i->first == url) { |
| 590 temp_images_.erase(i); | 625 temp_images_.erase(i); |
| 591 return; | 626 return; |
| 592 } | 627 } |
| 593 } | 628 } |
| 594 } | 629 } |
| 595 | 630 |
| 596 void TopSitesImpl::AddTemporaryThumbnail(const GURL& url, | 631 void TopSitesImpl::AddTemporaryThumbnail( |
| 597 const base::RefCountedBytes* thumbnail, | 632 const GURL& url, |
| 598 const ThumbnailScore& score) { | 633 const base::RefCountedMemory* thumbnail, |
| 634 const ThumbnailScore& score) { |
| 599 if (temp_images_.size() == kMaxTempTopImages) | 635 if (temp_images_.size() == kMaxTempTopImages) |
| 600 temp_images_.erase(temp_images_.begin()); | 636 temp_images_.erase(temp_images_.begin()); |
| 601 | 637 |
| 602 TempImage image; | 638 TempImage image; |
| 603 image.first = url; | 639 image.first = url; |
| 604 image.second.thumbnail = const_cast<base::RefCountedBytes*>(thumbnail); | 640 image.second.thumbnail = const_cast<base::RefCountedMemory*>(thumbnail); |
| 605 image.second.thumbnail_score = score; | 641 image.second.thumbnail_score = score; |
| 606 temp_images_.push_back(image); | 642 temp_images_.push_back(image); |
| 607 } | 643 } |
| 608 | 644 |
| 609 void TopSitesImpl::TimerFired() { | 645 void TopSitesImpl::TimerFired() { |
| 610 StartQueryForMostVisited(); | 646 StartQueryForMostVisited(); |
| 611 } | 647 } |
| 612 | 648 |
| 613 // static | 649 // static |
| 614 int TopSitesImpl::GetRedirectDistanceForURL(const MostVisitedURL& most_visited, | 650 int TopSitesImpl::GetRedirectDistanceForURL(const MostVisitedURL& most_visited, |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 902 SetTopSites(pages); | 938 SetTopSites(pages); |
| 903 | 939 |
| 904 // Used only in testing. | 940 // Used only in testing. |
| 905 content::NotificationService::current()->Notify( | 941 content::NotificationService::current()->Notify( |
| 906 chrome::NOTIFICATION_TOP_SITES_UPDATED, | 942 chrome::NOTIFICATION_TOP_SITES_UPDATED, |
| 907 content::Source<TopSitesImpl>(this), | 943 content::Source<TopSitesImpl>(this), |
| 908 content::Details<CancelableRequestProvider::Handle>(&handle)); | 944 content::Details<CancelableRequestProvider::Handle>(&handle)); |
| 909 } | 945 } |
| 910 | 946 |
| 911 } // namespace history | 947 } // namespace history |
| OLD | NEW |