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" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/md5.h" | 13 #include "base/md5.h" |
14 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
15 #include "base/message_loop/message_loop_proxy.h" | 15 #include "base/message_loop/message_loop_proxy.h" |
16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
17 #include "base/prefs/pref_service.h" | 17 #include "base/prefs/pref_service.h" |
18 #include "base/prefs/scoped_user_pref_update.h" | 18 #include "base/prefs/scoped_user_pref_update.h" |
19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
21 #include "base/task_runner.h" | 21 #include "base/task_runner.h" |
22 #include "base/values.h" | 22 #include "base/values.h" |
23 #include "chrome/browser/chrome_notification_types.h" | 23 #include "chrome/browser/chrome_notification_types.h" |
24 #include "chrome/browser/history/history_backend.h" | 24 #include "chrome/browser/history/history_backend.h" |
25 #include "chrome/browser/history/history_db_task.h" | 25 #include "chrome/browser/history/history_db_task.h" |
26 #include "chrome/browser/history/history_notifications.h" | |
27 #include "chrome/browser/history/history_service_factory.h" | 26 #include "chrome/browser/history/history_service_factory.h" |
28 #include "chrome/browser/history/top_sites_cache.h" | 27 #include "chrome/browser/history/top_sites_cache.h" |
29 #include "chrome/browser/history/url_utils.h" | 28 #include "chrome/browser/history/url_utils.h" |
30 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
31 #include "chrome/common/pref_names.h" | 30 #include "chrome/common/pref_names.h" |
32 #include "components/history/core/browser/page_usage_data.h" | 31 #include "components/history/core/browser/page_usage_data.h" |
33 #include "components/history/core/common/thumbnail_score.h" | 32 #include "components/history/core/common/thumbnail_score.h" |
34 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
35 #include "content/public/browser/navigation_controller.h" | 34 #include "content/public/browser/navigation_controller.h" |
36 #include "content/public/browser/navigation_details.h" | 35 #include "content/public/browser/navigation_details.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 // Use 100 quality (highest quality) because we're very sensitive to | 93 // Use 100 quality (highest quality) because we're very sensitive to |
95 // artifacts for these small sized, highly detailed images. | 94 // artifacts for these small sized, highly detailed images. |
96 static const int kTopSitesImageQuality = 100; | 95 static const int kTopSitesImageQuality = 100; |
97 | 96 |
98 TopSitesImpl::TopSitesImpl(Profile* profile) | 97 TopSitesImpl::TopSitesImpl(Profile* profile) |
99 : backend_(NULL), | 98 : backend_(NULL), |
100 cache_(new TopSitesCache()), | 99 cache_(new TopSitesCache()), |
101 thread_safe_cache_(new TopSitesCache()), | 100 thread_safe_cache_(new TopSitesCache()), |
102 profile_(profile), | 101 profile_(profile), |
103 last_num_urls_changed_(0), | 102 last_num_urls_changed_(0), |
104 loaded_(false) { | 103 loaded_(false), |
| 104 history_service_observer_(this) { |
105 if (!profile_) | 105 if (!profile_) |
106 return; | 106 return; |
107 | 107 |
108 if (content::NotificationService::current()) { | 108 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 | 109 // Listen for any nav commits. We'll ignore those not related to this |
112 // profile when we get the notification. | 110 // profile when we get the notification. |
113 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 111 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
114 content::NotificationService::AllSources()); | 112 content::NotificationService::AllSources()); |
115 } | 113 } |
116 for (int i = 0; i < kPrepopulatedPagesCount; i++) { | 114 for (int i = 0; i < kPrepopulatedPagesCount; i++) { |
117 int url_id = kPrepopulatedPages[i].url_id; | 115 int url_id = kPrepopulatedPages[i].url_id; |
118 prepopulated_page_urls_.push_back( | 116 prepopulated_page_urls_.push_back( |
119 GURL(l10n_util::GetStringUTF8(url_id))); | 117 GURL(l10n_util::GetStringUTF8(url_id))); |
120 } | 118 } |
(...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 | 730 |
733 int64 range = kMaxUpdateIntervalMinutes - kMinUpdateIntervalMinutes; | 731 int64 range = kMaxUpdateIntervalMinutes - kMinUpdateIntervalMinutes; |
734 int64 minutes = kMaxUpdateIntervalMinutes - | 732 int64 minutes = kMaxUpdateIntervalMinutes - |
735 last_num_urls_changed_ * range / cache_->top_sites().size(); | 733 last_num_urls_changed_ * range / cache_->top_sites().size(); |
736 return base::TimeDelta::FromMinutes(minutes); | 734 return base::TimeDelta::FromMinutes(minutes); |
737 } | 735 } |
738 | 736 |
739 void TopSitesImpl::Observe(int type, | 737 void TopSitesImpl::Observe(int type, |
740 const content::NotificationSource& source, | 738 const content::NotificationSource& source, |
741 const content::NotificationDetails& details) { | 739 const content::NotificationDetails& details) { |
| 740 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); |
742 if (!loaded_) | 741 if (!loaded_) |
743 return; | 742 return; |
744 | 743 |
745 if (type == chrome::NOTIFICATION_HISTORY_URLS_DELETED) { | |
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 = | 744 NavigationController* controller = |
771 content::Source<NavigationController>(source).ptr(); | 745 content::Source<NavigationController>(source).ptr(); |
772 Profile* profile = Profile::FromBrowserContext( | 746 Profile* profile = Profile::FromBrowserContext( |
773 controller->GetWebContents()->GetBrowserContext()); | 747 controller->GetWebContents()->GetBrowserContext()); |
774 if (profile == profile_ && !IsNonForcedFull()) { | 748 if (profile == profile_ && !IsNonForcedFull()) { |
775 content::LoadCommittedDetails* load_details = | 749 content::LoadCommittedDetails* load_details = |
776 content::Details<content::LoadCommittedDetails>(details).ptr(); | 750 content::Details<content::LoadCommittedDetails>(details).ptr(); |
777 if (!load_details) | 751 if (!load_details) |
778 return; | 752 return; |
779 const GURL& url = load_details->entry->GetURL(); | 753 const GURL& url = load_details->entry->GetURL(); |
780 if (!cache_->IsKnownURL(url) && HistoryService::CanAddURL(url)) { | 754 if (!cache_->IsKnownURL(url) && HistoryService::CanAddURL(url)) { |
781 // To avoid slamming history we throttle requests when the url updates. | 755 // To avoid slamming history we throttle requests when the url updates. |
782 // To do otherwise negatively impacts perf tests. | 756 // To do otherwise negatively impacts perf tests. |
783 RestartQueryForTopSitesTimer(GetUpdateDelay()); | 757 RestartQueryForTopSitesTimer(GetUpdateDelay()); |
784 } | 758 } |
785 } | 759 } |
786 } | |
787 } | 760 } |
788 | 761 |
789 void TopSitesImpl::SetTopSites(const MostVisitedURLList& new_top_sites) { | 762 void TopSitesImpl::SetTopSites(const MostVisitedURLList& new_top_sites) { |
790 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 763 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
791 | 764 |
792 MostVisitedURLList top_sites(new_top_sites); | 765 MostVisitedURLList top_sites(new_top_sites); |
793 size_t num_forced_urls = MergeCachedForcedURLs(&top_sites); | 766 size_t num_forced_urls = MergeCachedForcedURLs(&top_sites); |
794 AddPrepopulatedPages(&top_sites, num_forced_urls); | 767 AddPrepopulatedPages(&top_sites, num_forced_urls); |
795 | 768 |
796 TopSitesDelta delta; | 769 TopSitesDelta delta; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
867 filtered_urls_nonforced.assign(thread_safe_cache_->top_sites().begin() + | 840 filtered_urls_nonforced.assign(thread_safe_cache_->top_sites().begin() + |
868 thread_safe_cache_->GetNumForcedURLs(), | 841 thread_safe_cache_->GetNumForcedURLs(), |
869 thread_safe_cache_->top_sites().end()); | 842 thread_safe_cache_->top_sites().end()); |
870 pending_callbacks.swap(pending_callbacks_); | 843 pending_callbacks.swap(pending_callbacks_); |
871 } | 844 } |
872 } | 845 } |
873 | 846 |
874 for (size_t i = 0; i < pending_callbacks.size(); i++) | 847 for (size_t i = 0; i < pending_callbacks.size(); i++) |
875 pending_callbacks[i].Run(filtered_urls_all, filtered_urls_nonforced); | 848 pending_callbacks[i].Run(filtered_urls_all, filtered_urls_nonforced); |
876 | 849 |
| 850 HistoryService* hs = |
| 851 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 852 // |hs| may be null during unit tests. |
| 853 if (hs) |
| 854 history_service_observer_.Add(hs); |
| 855 |
877 NotifyTopSitesLoaded(); | 856 NotifyTopSitesLoaded(); |
878 } | 857 } |
879 | 858 |
880 void TopSitesImpl::ResetThreadSafeCache() { | 859 void TopSitesImpl::ResetThreadSafeCache() { |
881 base::AutoLock lock(lock_); | 860 base::AutoLock lock(lock_); |
882 MostVisitedURLList cached; | 861 MostVisitedURLList cached; |
883 ApplyBlacklist(cache_->top_sites(), &cached); | 862 ApplyBlacklist(cache_->top_sites(), &cached); |
884 thread_safe_cache_->SetTopSites(cached); | 863 thread_safe_cache_->SetTopSites(cached); |
885 } | 864 } |
886 | 865 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
918 RestartQueryForTopSitesTimer( | 897 RestartQueryForTopSitesTimer( |
919 base::TimeDelta::FromSeconds(kUpdateIntervalSecs)); | 898 base::TimeDelta::FromSeconds(kUpdateIntervalSecs)); |
920 } | 899 } |
921 | 900 |
922 void TopSitesImpl::OnTopSitesAvailableFromHistory( | 901 void TopSitesImpl::OnTopSitesAvailableFromHistory( |
923 const MostVisitedURLList* pages) { | 902 const MostVisitedURLList* pages) { |
924 DCHECK(pages); | 903 DCHECK(pages); |
925 SetTopSites(*pages); | 904 SetTopSites(*pages); |
926 } | 905 } |
927 | 906 |
| 907 void TopSitesImpl::OnURLsDeleted(HistoryService* history_service, |
| 908 bool all_history, |
| 909 bool expired, |
| 910 const URLRows& deleted_rows, |
| 911 const std::set<GURL>& favicon_urls) { |
| 912 if (!loaded_) |
| 913 return; |
| 914 |
| 915 if (all_history) { |
| 916 SetTopSites(MostVisitedURLList()); |
| 917 backend_->ResetDatabase(); |
| 918 } else { |
| 919 std::set<size_t> indices_to_delete; // Indices into top_sites_. |
| 920 for (const auto& row : deleted_rows) { |
| 921 if (cache_->IsKnownURL(row.url())) |
| 922 indices_to_delete.insert(cache_->GetURLIndex(row.url())); |
| 923 } |
| 924 |
| 925 if (indices_to_delete.empty()) |
| 926 return; |
| 927 |
| 928 MostVisitedURLList new_top_sites(cache_->top_sites()); |
| 929 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); |
| 930 i != indices_to_delete.rend(); i++) { |
| 931 new_top_sites.erase(new_top_sites.begin() + *i); |
| 932 } |
| 933 SetTopSites(new_top_sites); |
| 934 } |
| 935 StartQueryForMostVisited(); |
| 936 } |
| 937 |
| 938 void TopSitesImpl::HistoryServiceBeingDeleted(HistoryService* history_service) { |
| 939 history_service_observer_.Remove(history_service); |
| 940 } |
| 941 |
928 } // namespace history | 942 } // namespace history |
OLD | NEW |