OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 // |hs| may be null during unit tests. | 525 // |hs| may be null during unit tests. |
526 if (!hs) | 526 if (!hs) |
527 return; | 527 return; |
528 HistoryService::Handle handle = | 528 HistoryService::Handle handle = |
529 hs->GetPageThumbnail(top_sites_[index].url, | 529 hs->GetPageThumbnail(top_sites_[index].url, |
530 &cancelable_consumer_, | 530 &cancelable_consumer_, |
531 NewCallback(this, &TopSites::OnThumbnailAvailable)); | 531 NewCallback(this, &TopSites::OnThumbnailAvailable)); |
532 cancelable_consumer_.SetClientData(hs, handle, index); | 532 cancelable_consumer_.SetClientData(hs, handle, index); |
533 } | 533 } |
534 | 534 |
| 535 void TopSites::GenerateCanonicalURLs() { |
| 536 canonical_urls_.clear(); |
| 537 for (size_t i = 0; i < top_sites_.size(); i++) { |
| 538 const MostVisitedURL& mv = top_sites_[i]; |
| 539 StoreRedirectChain(mv.redirects, i); |
| 540 } |
| 541 } |
| 542 |
535 void TopSites::StoreMostVisited(MostVisitedURLList* most_visited) { | 543 void TopSites::StoreMostVisited(MostVisitedURLList* most_visited) { |
536 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::DB)); | 544 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::DB)); |
537 // Take ownership of the most visited data. | 545 // Take ownership of the most visited data. |
538 top_sites_.clear(); | 546 top_sites_.clear(); |
539 top_sites_.swap(*most_visited); | 547 top_sites_.swap(*most_visited); |
540 waiting_for_results_ = false; | 548 waiting_for_results_ = false; |
541 | 549 |
542 // Save the redirect information for quickly mapping to the canonical URLs. | 550 // Save the redirect information for quickly mapping to the canonical URLs. |
543 canonical_urls_.clear(); | 551 GenerateCanonicalURLs(); |
| 552 |
544 for (size_t i = 0; i < top_sites_.size(); i++) { | 553 for (size_t i = 0; i < top_sites_.size(); i++) { |
545 const MostVisitedURL& mv = top_sites_[i]; | 554 const MostVisitedURL& mv = top_sites_[i]; |
546 StoreRedirectChain(mv.redirects, i); | |
547 | |
548 std::map<GURL, Images>::iterator it = temp_thumbnails_map_.begin(); | 555 std::map<GURL, Images>::iterator it = temp_thumbnails_map_.begin(); |
549 GURL canonical_url = GetCanonicalURL(mv.url); | 556 GURL canonical_url = GetCanonicalURL(mv.url); |
550 for (; it != temp_thumbnails_map_.end(); it++) { | 557 for (; it != temp_thumbnails_map_.end(); it++) { |
551 // Must map all temp URLs to canonical ones. | 558 // Must map all temp URLs to canonical ones. |
552 // temp_thumbnails_map_ contains non-canonical URLs, because | 559 // temp_thumbnails_map_ contains non-canonical URLs, because |
553 // when we add a temp thumbnail, redirect chain is not known. | 560 // when we add a temp thumbnail, redirect chain is not known. |
554 // This is slow, but temp_thumbnails_map_ should have very few URLs. | 561 // This is slow, but temp_thumbnails_map_ should have very few URLs. |
555 if (canonical_url == GetCanonicalURL(it->first)) { | 562 if (canonical_url == GetCanonicalURL(it->first)) { |
556 SetPageThumbnail(mv.url, it->second.thumbnail, | 563 SetPageThumbnail(mv.url, it->second.thumbnail, |
557 it->second.thumbnail_score); | 564 it->second.thumbnail_score); |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
795 void TopSites::Observe(NotificationType type, | 802 void TopSites::Observe(NotificationType type, |
796 const NotificationSource& source, | 803 const NotificationSource& source, |
797 const NotificationDetails& details) { | 804 const NotificationDetails& details) { |
798 if (type == NotificationType::HISTORY_URLS_DELETED) { | 805 if (type == NotificationType::HISTORY_URLS_DELETED) { |
799 Details<history::URLsDeletedDetails> deleted_details(details); | 806 Details<history::URLsDeletedDetails> deleted_details(details); |
800 if (deleted_details->all_history) { | 807 if (deleted_details->all_history) { |
801 top_sites_.clear(); | 808 top_sites_.clear(); |
802 ChromeThread::PostTask(ChromeThread::DB, FROM_HERE, | 809 ChromeThread::PostTask(ChromeThread::DB, FROM_HERE, |
803 NewRunnableMethod(this, &TopSites::ResetDatabase)); | 810 NewRunnableMethod(this, &TopSites::ResetDatabase)); |
804 } else { | 811 } else { |
| 812 std::set<size_t> indices_to_delete; // Indices into top_sites_. |
805 std::set<GURL>::iterator it; | 813 std::set<GURL>::iterator it; |
806 for (it = deleted_details->urls.begin(); | 814 for (it = deleted_details->urls.begin(); |
807 it != deleted_details->urls.end(); ++it) { | 815 it != deleted_details->urls.end(); ++it) { |
808 for (size_t i = 0; i < top_sites_.size(); i++) { | 816 std::map<GURL,size_t>::const_iterator found = canonical_urls_.find(*it); |
809 if (top_sites_[i].url == *it) { | 817 if (found != canonical_urls_.end()) |
810 top_sites_.erase(top_sites_.begin() + i); | 818 indices_to_delete.insert(found->second); |
811 break; | 819 } |
812 } | 820 |
813 } | 821 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); |
| 822 i != indices_to_delete.rend(); i++) { |
| 823 size_t index = *i; |
| 824 RemovePinnedURL(top_sites_[index].url); |
| 825 top_sites_.erase(top_sites_.begin() + index); |
814 } | 826 } |
815 } | 827 } |
| 828 // Canonical URLs are not valid any more. |
| 829 GenerateCanonicalURLs(); |
816 StartQueryForMostVisited(); | 830 StartQueryForMostVisited(); |
817 } else if (type == NotificationType::NAV_ENTRY_COMMITTED) { | 831 } else if (type == NotificationType::NAV_ENTRY_COMMITTED) { |
818 if (top_sites_.size() < kTopSitesNumber) { | 832 if (top_sites_.size() < kTopSitesNumber) { |
819 const NavigationController::LoadCommittedDetails& load_details = | 833 const NavigationController::LoadCommittedDetails& load_details = |
820 *Details<NavigationController::LoadCommittedDetails>(details).ptr(); | 834 *Details<NavigationController::LoadCommittedDetails>(details).ptr(); |
821 GURL url = load_details.entry->url(); | 835 GURL url = load_details.entry->url(); |
822 if (canonical_urls_.find(url) == canonical_urls_.end() && | 836 if (canonical_urls_.find(url) == canonical_urls_.end() && |
823 HistoryService::CanAddURL(url)) { | 837 HistoryService::CanAddURL(url)) { |
824 // Add this page to the known pages in case the thumbnail comes | 838 // Add this page to the known pages in case the thumbnail comes |
825 // in before we get the results. | 839 // in before we get the results. |
(...skipping 13 matching lines...) Expand all Loading... |
839 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::DB)); | 853 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::DB)); |
840 db_.reset(new TopSitesDatabaseImpl()); | 854 db_.reset(new TopSitesDatabaseImpl()); |
841 file_util::Delete(db_path_, false); | 855 file_util::Delete(db_path_, false); |
842 if (!db_->Init(db_path_)) { | 856 if (!db_->Init(db_path_)) { |
843 NOTREACHED() << "Failed to initialize database."; | 857 NOTREACHED() << "Failed to initialize database."; |
844 return; | 858 return; |
845 } | 859 } |
846 } | 860 } |
847 | 861 |
848 } // namespace history | 862 } // namespace history |
OLD | NEW |