Chromium Code Reviews| 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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 // Use 100 quality (highest quality) because we're very sensitive to | 94 // Use 100 quality (highest quality) because we're very sensitive to |
| 95 // artifacts for these small sized, highly detailed images. | 95 // artifacts for these small sized, highly detailed images. |
| 96 static const int kTopSitesImageQuality = 100; | 96 static const int kTopSitesImageQuality = 100; |
| 97 | 97 |
| 98 TopSitesImpl::TopSitesImpl(Profile* profile) | 98 TopSitesImpl::TopSitesImpl(Profile* profile) |
| 99 : backend_(NULL), | 99 : backend_(NULL), |
| 100 cache_(new TopSitesCache()), | 100 cache_(new TopSitesCache()), |
| 101 thread_safe_cache_(new TopSitesCache()), | 101 thread_safe_cache_(new TopSitesCache()), |
| 102 profile_(profile), | 102 profile_(profile), |
| 103 last_num_urls_changed_(0), | 103 last_num_urls_changed_(0), |
| 104 loaded_(false) { | 104 loaded_(false), |
| 105 history_service_observer_(this) { | |
| 105 if (!profile_) | 106 if (!profile_) |
| 106 return; | 107 return; |
| 107 | 108 |
| 108 if (content::NotificationService::current()) { | 109 if (content::NotificationService::current()) { |
| 109 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | |
| 110 content::Source<Profile>(profile_)); | |
| 111 // Listen for any nav commits. We'll ignore those not related to this | 110 // Listen for any nav commits. We'll ignore those not related to this |
| 112 // profile when we get the notification. | 111 // profile when we get the notification. |
| 113 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 112 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 114 content::NotificationService::AllSources()); | 113 content::NotificationService::AllSources()); |
| 115 } | 114 } |
| 116 for (int i = 0; i < kPrepopulatedPagesCount; i++) { | 115 for (int i = 0; i < kPrepopulatedPagesCount; i++) { |
| 117 int url_id = kPrepopulatedPages[i].url_id; | 116 int url_id = kPrepopulatedPages[i].url_id; |
| 118 prepopulated_page_urls_.push_back( | 117 prepopulated_page_urls_.push_back( |
| 119 GURL(l10n_util::GetStringUTF8(url_id))); | 118 GURL(l10n_util::GetStringUTF8(url_id))); |
| 120 } | 119 } |
| (...skipping 614 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 735 last_num_urls_changed_ * range / cache_->top_sites().size(); | 734 last_num_urls_changed_ * range / cache_->top_sites().size(); |
| 736 return base::TimeDelta::FromMinutes(minutes); | 735 return base::TimeDelta::FromMinutes(minutes); |
| 737 } | 736 } |
| 738 | 737 |
| 739 void TopSitesImpl::Observe(int type, | 738 void TopSitesImpl::Observe(int type, |
| 740 const content::NotificationSource& source, | 739 const content::NotificationSource& source, |
| 741 const content::NotificationDetails& details) { | 740 const content::NotificationDetails& details) { |
| 742 if (!loaded_) | 741 if (!loaded_) |
| 743 return; | 742 return; |
| 744 | 743 |
| 745 if (type == chrome::NOTIFICATION_HISTORY_URLS_DELETED) { | 744 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { |
|
sdefresne
2014/12/04 17:21:13
This is the only notification listened for, so DCH
nshaik
2014/12/07 09:34:50
Done.
| |
| 746 content::Details<history::URLsDeletedDetails> deleted_details(details); | |
| 747 if (deleted_details->all_history) { | |
| 748 SetTopSites(MostVisitedURLList()); | |
| 749 backend_->ResetDatabase(); | |
| 750 } else { | |
| 751 std::set<size_t> indices_to_delete; // Indices into top_sites_. | |
| 752 for (URLRows::const_iterator i = deleted_details->rows.begin(); | |
| 753 i != deleted_details->rows.end(); ++i) { | |
| 754 if (cache_->IsKnownURL(i->url())) | |
| 755 indices_to_delete.insert(cache_->GetURLIndex(i->url())); | |
| 756 } | |
| 757 | |
| 758 if (indices_to_delete.empty()) | |
| 759 return; | |
| 760 | |
| 761 MostVisitedURLList new_top_sites(cache_->top_sites()); | |
| 762 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); | |
| 763 i != indices_to_delete.rend(); i++) { | |
| 764 new_top_sites.erase(new_top_sites.begin() + *i); | |
| 765 } | |
| 766 SetTopSites(new_top_sites); | |
| 767 } | |
| 768 StartQueryForMostVisited(); | |
| 769 } else if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { | |
| 770 NavigationController* controller = | 745 NavigationController* controller = |
| 771 content::Source<NavigationController>(source).ptr(); | 746 content::Source<NavigationController>(source).ptr(); |
| 772 Profile* profile = Profile::FromBrowserContext( | 747 Profile* profile = Profile::FromBrowserContext( |
| 773 controller->GetWebContents()->GetBrowserContext()); | 748 controller->GetWebContents()->GetBrowserContext()); |
| 774 if (profile == profile_ && !IsNonForcedFull()) { | 749 if (profile == profile_ && !IsNonForcedFull()) { |
| 775 content::LoadCommittedDetails* load_details = | 750 content::LoadCommittedDetails* load_details = |
| 776 content::Details<content::LoadCommittedDetails>(details).ptr(); | 751 content::Details<content::LoadCommittedDetails>(details).ptr(); |
| 777 if (!load_details) | 752 if (!load_details) |
| 778 return; | 753 return; |
| 779 const GURL& url = load_details->entry->GetURL(); | 754 const GURL& url = load_details->entry->GetURL(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 867 filtered_urls_nonforced.assign(thread_safe_cache_->top_sites().begin() + | 842 filtered_urls_nonforced.assign(thread_safe_cache_->top_sites().begin() + |
| 868 thread_safe_cache_->GetNumForcedURLs(), | 843 thread_safe_cache_->GetNumForcedURLs(), |
| 869 thread_safe_cache_->top_sites().end()); | 844 thread_safe_cache_->top_sites().end()); |
| 870 pending_callbacks.swap(pending_callbacks_); | 845 pending_callbacks.swap(pending_callbacks_); |
| 871 } | 846 } |
| 872 } | 847 } |
| 873 | 848 |
| 874 for (size_t i = 0; i < pending_callbacks.size(); i++) | 849 for (size_t i = 0; i < pending_callbacks.size(); i++) |
| 875 pending_callbacks[i].Run(filtered_urls_all, filtered_urls_nonforced); | 850 pending_callbacks[i].Run(filtered_urls_all, filtered_urls_nonforced); |
| 876 | 851 |
| 852 HistoryService* hs = | |
| 853 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | |
| 854 // |hs| may be null during unit tests. | |
| 855 if (hs) | |
| 856 history_service_observer_.Add(hs); | |
| 857 | |
| 877 NotifyTopSitesLoaded(); | 858 NotifyTopSitesLoaded(); |
| 878 } | 859 } |
| 879 | 860 |
| 880 void TopSitesImpl::ResetThreadSafeCache() { | 861 void TopSitesImpl::ResetThreadSafeCache() { |
| 881 base::AutoLock lock(lock_); | 862 base::AutoLock lock(lock_); |
| 882 MostVisitedURLList cached; | 863 MostVisitedURLList cached; |
| 883 ApplyBlacklist(cache_->top_sites(), &cached); | 864 ApplyBlacklist(cache_->top_sites(), &cached); |
| 884 thread_safe_cache_->SetTopSites(cached); | 865 thread_safe_cache_->SetTopSites(cached); |
| 885 } | 866 } |
| 886 | 867 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 918 RestartQueryForTopSitesTimer( | 899 RestartQueryForTopSitesTimer( |
| 919 base::TimeDelta::FromSeconds(kUpdateIntervalSecs)); | 900 base::TimeDelta::FromSeconds(kUpdateIntervalSecs)); |
| 920 } | 901 } |
| 921 | 902 |
| 922 void TopSitesImpl::OnTopSitesAvailableFromHistory( | 903 void TopSitesImpl::OnTopSitesAvailableFromHistory( |
| 923 const MostVisitedURLList* pages) { | 904 const MostVisitedURLList* pages) { |
| 924 DCHECK(pages); | 905 DCHECK(pages); |
| 925 SetTopSites(*pages); | 906 SetTopSites(*pages); |
| 926 } | 907 } |
| 927 | 908 |
| 909 void TopSitesImpl::OnURLsDeleted( | |
| 910 HistoryService* history_service, | |
| 911 const history::URLsDeletedDetails& deleted_details) { | |
| 912 if (deleted_details.all_history) { | |
| 913 SetTopSites(MostVisitedURLList()); | |
| 914 backend_->ResetDatabase(); | |
| 915 } else { | |
| 916 std::set<size_t> indices_to_delete; // Indices into top_sites_. | |
| 917 for (URLRows::const_iterator i = deleted_details.rows.begin(); | |
| 918 i != deleted_details.rows.end(); ++i) { | |
| 919 if (cache_->IsKnownURL(i->url())) | |
| 920 indices_to_delete.insert(cache_->GetURLIndex(i->url())); | |
| 921 } | |
| 922 | |
| 923 if (indices_to_delete.empty()) | |
| 924 return; | |
| 925 | |
| 926 MostVisitedURLList new_top_sites(cache_->top_sites()); | |
| 927 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); | |
| 928 i != indices_to_delete.rend(); i++) { | |
| 929 new_top_sites.erase(new_top_sites.begin() + *i); | |
| 930 } | |
| 931 SetTopSites(new_top_sites); | |
| 932 } | |
| 933 StartQueryForMostVisited(); | |
| 934 } | |
| 935 | |
| 936 void TopSitesImpl::HistoryServiceBeingDeleted(HistoryService* history_service) { | |
| 937 history_service_observer_.Remove(history_service); | |
| 938 } | |
| 939 | |
| 928 } // namespace history | 940 } // namespace history |
| OLD | NEW |