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 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 TopSitesImpl::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 TopSitesImpl::GetMostVisitedURLs( | 292 void TopSitesImpl::GetMostVisitedURLs( |
258 const GetMostVisitedURLsCallback& callback) { | 293 const GetMostVisitedURLsCallback& callback) { |
259 MostVisitedURLList filtered_urls; | 294 MostVisitedURLList filtered_urls; |
260 { | 295 { |
261 base::AutoLock lock(lock_); | 296 base::AutoLock lock(lock_); |
262 if (!loaded_) { | 297 if (!loaded_) { |
263 // A request came in before we finished loading. Store the callback and | 298 // A request came in before we finished loading. Store the callback and |
264 // we'll run it on current thread when we finish loading. | 299 // we'll run it on current thread when we finish loading. |
265 pending_callbacks_.push_back( | 300 pending_callbacks_.push_back( |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 | 567 |
533 bool TopSitesImpl::IsFull() { | 568 bool TopSitesImpl::IsFull() { |
534 return loaded_ && cache_->top_sites().size() >= kTopSitesNumber; | 569 return loaded_ && cache_->top_sites().size() >= kTopSitesNumber; |
535 } | 570 } |
536 | 571 |
537 TopSitesImpl::~TopSitesImpl() { | 572 TopSitesImpl::~TopSitesImpl() { |
538 } | 573 } |
539 | 574 |
540 bool TopSitesImpl::SetPageThumbnailNoDB( | 575 bool TopSitesImpl::SetPageThumbnailNoDB( |
541 const GURL& url, | 576 const GURL& url, |
542 const base::RefCountedBytes* thumbnail_data, | 577 const base::RefCountedMemory* thumbnail_data, |
543 const ThumbnailScore& score) { | 578 const ThumbnailScore& score) { |
544 // This should only be invoked when we know about the url. | 579 // This should only be invoked when we know about the url. |
545 DCHECK(cache_->IsKnownURL(url)); | 580 DCHECK(cache_->IsKnownURL(url)); |
546 | 581 |
547 const MostVisitedURL& most_visited = | 582 const MostVisitedURL& most_visited = |
548 cache_->top_sites()[cache_->GetURLIndex(url)]; | 583 cache_->top_sites()[cache_->GetURLIndex(url)]; |
549 Images* image = cache_->GetImage(url); | 584 Images* image = cache_->GetImage(url); |
550 | 585 |
551 // When comparing the thumbnail scores, we need to take into account the | 586 // When comparing the thumbnail scores, we need to take into account the |
552 // redirect hops, which are not generated when the thumbnail is because the | 587 // redirect hops, which are not generated when the thumbnail is because the |
553 // redirects weren't known. We fill that in here since we know the redirects. | 588 // redirects weren't known. We fill that in here since we know the redirects. |
554 ThumbnailScore new_score_with_redirects(score); | 589 ThumbnailScore new_score_with_redirects(score); |
555 new_score_with_redirects.redirect_hops_from_dest = | 590 new_score_with_redirects.redirect_hops_from_dest = |
556 GetRedirectDistanceForURL(most_visited, url); | 591 GetRedirectDistanceForURL(most_visited, url); |
557 | 592 |
558 if (!ShouldReplaceThumbnailWith(image->thumbnail_score, | 593 if (!ShouldReplaceThumbnailWith(image->thumbnail_score, |
559 new_score_with_redirects) && | 594 new_score_with_redirects) && |
560 image->thumbnail.get()) | 595 image->thumbnail.get()) |
561 return false; // The one we already have is better. | 596 return false; // The one we already have is better. |
562 | 597 |
563 image->thumbnail = const_cast<base::RefCountedBytes*>(thumbnail_data); | 598 image->thumbnail = const_cast<base::RefCountedMemory*>(thumbnail_data); |
564 image->thumbnail_score = new_score_with_redirects; | 599 image->thumbnail_score = new_score_with_redirects; |
565 | 600 |
566 ResetThreadSafeImageCache(); | 601 ResetThreadSafeImageCache(); |
567 return true; | 602 return true; |
568 } | 603 } |
569 | 604 |
570 bool TopSitesImpl::SetPageThumbnailEncoded( | 605 bool TopSitesImpl::SetPageThumbnailEncoded( |
571 const GURL& url, | 606 const GURL& url, |
572 const base::RefCountedBytes* thumbnail, | 607 const base::RefCountedMemory* thumbnail, |
573 const ThumbnailScore& score) { | 608 const ThumbnailScore& score) { |
574 if (!SetPageThumbnailNoDB(url, thumbnail, score)) | 609 if (!SetPageThumbnailNoDB(url, thumbnail, score)) |
575 return false; | 610 return false; |
576 | 611 |
577 // Update the database. | 612 // Update the database. |
578 if (!cache_->IsKnownURL(url)) | 613 if (!cache_->IsKnownURL(url)) |
579 return false; | 614 return false; |
580 | 615 |
581 size_t index = cache_->GetURLIndex(url); | 616 size_t index = cache_->GetURLIndex(url); |
582 const MostVisitedURL& most_visited = cache_->top_sites()[index]; | 617 const MostVisitedURL& most_visited = cache_->top_sites()[index]; |
(...skipping 23 matching lines...) Expand all Loading... |
606 void TopSitesImpl::RemoveTemporaryThumbnailByURL(const GURL& url) { | 641 void TopSitesImpl::RemoveTemporaryThumbnailByURL(const GURL& url) { |
607 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end(); | 642 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end(); |
608 ++i) { | 643 ++i) { |
609 if (i->first == url) { | 644 if (i->first == url) { |
610 temp_images_.erase(i); | 645 temp_images_.erase(i); |
611 return; | 646 return; |
612 } | 647 } |
613 } | 648 } |
614 } | 649 } |
615 | 650 |
616 void TopSitesImpl::AddTemporaryThumbnail(const GURL& url, | 651 void TopSitesImpl::AddTemporaryThumbnail( |
617 const base::RefCountedBytes* thumbnail, | 652 const GURL& url, |
618 const ThumbnailScore& score) { | 653 const base::RefCountedMemory* thumbnail, |
| 654 const ThumbnailScore& score) { |
619 if (temp_images_.size() == kMaxTempTopImages) | 655 if (temp_images_.size() == kMaxTempTopImages) |
620 temp_images_.erase(temp_images_.begin()); | 656 temp_images_.erase(temp_images_.begin()); |
621 | 657 |
622 TempImage image; | 658 TempImage image; |
623 image.first = url; | 659 image.first = url; |
624 image.second.thumbnail = const_cast<base::RefCountedBytes*>(thumbnail); | 660 image.second.thumbnail = const_cast<base::RefCountedMemory*>(thumbnail); |
625 image.second.thumbnail_score = score; | 661 image.second.thumbnail_score = score; |
626 temp_images_.push_back(image); | 662 temp_images_.push_back(image); |
627 } | 663 } |
628 | 664 |
629 void TopSitesImpl::TimerFired() { | 665 void TopSitesImpl::TimerFired() { |
630 StartQueryForMostVisited(); | 666 StartQueryForMostVisited(); |
631 } | 667 } |
632 | 668 |
633 // static | 669 // static |
634 int TopSitesImpl::GetRedirectDistanceForURL(const MostVisitedURL& most_visited, | 670 int TopSitesImpl::GetRedirectDistanceForURL(const MostVisitedURL& most_visited, |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
922 SetTopSites(pages); | 958 SetTopSites(pages); |
923 | 959 |
924 // Used only in testing. | 960 // Used only in testing. |
925 content::NotificationService::current()->Notify( | 961 content::NotificationService::current()->Notify( |
926 chrome::NOTIFICATION_TOP_SITES_UPDATED, | 962 chrome::NOTIFICATION_TOP_SITES_UPDATED, |
927 content::Source<TopSitesImpl>(this), | 963 content::Source<TopSitesImpl>(this), |
928 content::Details<CancelableRequestProvider::Handle>(&handle)); | 964 content::Details<CancelableRequestProvider::Handle>(&handle)); |
929 } | 965 } |
930 | 966 |
931 } // namespace history | 967 } // namespace history |
OLD | NEW |