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 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
618 new_url.redirects.push_back(url); | 618 new_url.redirects.push_back(url); |
619 } | 619 } |
620 new_url.last_forced_time = time; | 620 new_url.last_forced_time = time; |
621 // Add forced URLs and sort. Added to the end of the list of forced URLs | 621 // Add forced URLs and sort. Added to the end of the list of forced URLs |
622 // since this is almost always where it needs to go, unless the user's local | 622 // since this is almost always where it needs to go, unless the user's local |
623 // clock is fiddled with. | 623 // clock is fiddled with. |
624 MostVisitedURLList::iterator mid = new_list.begin() + num_forced; | 624 MostVisitedURLList::iterator mid = new_list.begin() + num_forced; |
625 new_list.insert(mid, new_url); | 625 new_list.insert(mid, new_url); |
626 mid = new_list.begin() + num_forced; // Mid was invalidated. | 626 mid = new_list.begin() + num_forced; // Mid was invalidated. |
627 std::inplace_merge(new_list.begin(), mid, mid + 1, ForcedURLComparator); | 627 std::inplace_merge(new_list.begin(), mid, mid + 1, ForcedURLComparator); |
628 SetTopSites(new_list); | 628 SetTopSites(new_list, false); |
629 return true; | 629 return true; |
630 } | 630 } |
631 | 631 |
632 bool TopSitesImpl::AddPrepopulatedPages(MostVisitedURLList* urls, | 632 bool TopSitesImpl::AddPrepopulatedPages(MostVisitedURLList* urls, |
633 size_t num_forced_urls) { | 633 size_t num_forced_urls) { |
634 bool added = false; | 634 bool added = false; |
635 for (const auto& prepopulated_page : prepopulated_pages_) { | 635 for (const auto& prepopulated_page : prepopulated_pages_) { |
636 if (urls->size() - num_forced_urls < kNonForcedTopSitesNumber && | 636 if (urls->size() - num_forced_urls < kNonForcedTopSitesNumber && |
637 IndexOf(*urls, prepopulated_page.most_visited.url) == -1) { | 637 IndexOf(*urls, prepopulated_page.most_visited.url) == -1) { |
638 urls->push_back(prepopulated_page.most_visited); | 638 urls->push_back(prepopulated_page.most_visited); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
745 return; | 745 return; |
746 const GURL& url = load_details->entry->GetURL(); | 746 const GURL& url = load_details->entry->GetURL(); |
747 if (!cache_->IsKnownURL(url) && CanAddURLToHistory(url)) { | 747 if (!cache_->IsKnownURL(url) && CanAddURLToHistory(url)) { |
748 // To avoid slamming history we throttle requests when the url updates. | 748 // To avoid slamming history we throttle requests when the url updates. |
749 // To do otherwise negatively impacts perf tests. | 749 // To do otherwise negatively impacts perf tests. |
750 RestartQueryForTopSitesTimer(GetUpdateDelay()); | 750 RestartQueryForTopSitesTimer(GetUpdateDelay()); |
751 } | 751 } |
752 } | 752 } |
753 } | 753 } |
754 | 754 |
755 void TopSitesImpl::SetTopSites(const MostVisitedURLList& new_top_sites) { | 755 void TopSitesImpl::SetTopSites( |
756 const MostVisitedURLList& new_top_sites, bool startup) { | |
756 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 757 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
757 | 758 |
758 MostVisitedURLList top_sites(new_top_sites); | 759 MostVisitedURLList top_sites(new_top_sites); |
759 size_t num_forced_urls = MergeCachedForcedURLs(&top_sites); | 760 size_t num_forced_urls = MergeCachedForcedURLs(&top_sites); |
760 AddPrepopulatedPages(&top_sites, num_forced_urls); | 761 AddPrepopulatedPages(&top_sites, num_forced_urls); |
761 | 762 |
762 TopSitesDelta delta; | 763 TopSitesDelta delta; |
763 DiffMostVisited(cache_->top_sites(), top_sites, &delta); | 764 DiffMostVisited(cache_->top_sites(), top_sites, &delta); |
764 if (!delta.deleted.empty() || !delta.added.empty() || !delta.moved.empty()) { | 765 if (!delta.deleted.empty() || !delta.added.empty() || !delta.moved.empty()) { |
765 backend_->UpdateTopSites(delta); | 766 backend_->UpdateTopSites(delta, startup); |
767 } | |
768 | |
769 // Record the delta size into a histogram for startup. | |
770 if (startup) { | |
771 size_t delta_size = | |
772 delta.deleted.size() + delta.added.size() + delta.moved.size(); | |
773 UMA_HISTOGRAM_COUNTS_100( | |
774 "History.UpdateTopSitesOnDBThread_Startup_DeltaSize", delta_size); | |
766 } | 775 } |
767 | 776 |
768 last_num_urls_changed_ = delta.added.size() + delta.moved.size(); | 777 last_num_urls_changed_ = delta.added.size() + delta.moved.size(); |
769 | 778 |
770 // We always do the following steps (setting top sites in cache, and resetting | 779 // We always do the following steps (setting top sites in cache, and resetting |
771 // thread safe cache ...) as this method is invoked during startup at which | 780 // thread safe cache ...) as this method is invoked during startup at which |
772 // point the caches haven't been updated yet. | 781 // point the caches haven't been updated yet. |
773 cache_->SetTopSites(top_sites); | 782 cache_->SetTopSites(top_sites); |
774 | 783 |
775 // See if we have any tmp thumbnails for the new sites. | 784 // See if we have any tmp thumbnails for the new sites. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
865 if (timer_.IsRunning() && ((timer_start_time_ + timer_.GetCurrentDelay()) < | 874 if (timer_.IsRunning() && ((timer_start_time_ + timer_.GetCurrentDelay()) < |
866 (base::TimeTicks::Now() + delta))) { | 875 (base::TimeTicks::Now() + delta))) { |
867 return; | 876 return; |
868 } | 877 } |
869 | 878 |
870 timer_start_time_ = base::TimeTicks::Now(); | 879 timer_start_time_ = base::TimeTicks::Now(); |
871 timer_.Stop(); | 880 timer_.Stop(); |
872 timer_.Start(FROM_HERE, delta, this, &TopSitesImpl::TimerFired); | 881 timer_.Start(FROM_HERE, delta, this, &TopSitesImpl::TimerFired); |
873 } | 882 } |
874 | 883 |
875 void TopSitesImpl::OnGotMostVisitedThumbnails( | 884 void TopSitesImpl::OnGotMostVisitedThumbnails( |
jwd
2015/03/30 16:06:47
I'm a bit concerned that this *could* be called du
yao
2015/03/30 22:16:18
But OnGotMostVisitedThumbnails is only called from
yao
2015/03/30 22:18:35
Ah, ic what you are saying, but OnGotMostVisitedTh
jwd
2015/03/31 19:26:16
As per offline conversation, I'd be comfortable wi
| |
876 const scoped_refptr<MostVisitedThumbnails>& thumbnails) { | 885 const scoped_refptr<MostVisitedThumbnails>& thumbnails) { |
877 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 886 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
878 | 887 |
879 // Set the top sites directly in the cache so that SetTopSites diffs | 888 // Set the top sites directly in the cache so that SetTopSites diffs |
880 // correctly. | 889 // correctly. |
881 cache_->SetTopSites(thumbnails->most_visited); | 890 cache_->SetTopSites(thumbnails->most_visited); |
882 SetTopSites(thumbnails->most_visited); | 891 SetTopSites(thumbnails->most_visited, true); |
883 cache_->SetThumbnails(thumbnails->url_to_images_map); | 892 cache_->SetThumbnails(thumbnails->url_to_images_map); |
884 | 893 |
885 ResetThreadSafeImageCache(); | 894 ResetThreadSafeImageCache(); |
886 | 895 |
887 MoveStateToLoaded(); | 896 MoveStateToLoaded(); |
888 | 897 |
889 // Start a timer that refreshes top sites from history. | 898 // Start a timer that refreshes top sites from history. |
890 RestartQueryForTopSitesTimer( | 899 RestartQueryForTopSitesTimer( |
891 base::TimeDelta::FromSeconds(kUpdateIntervalSecs)); | 900 base::TimeDelta::FromSeconds(kUpdateIntervalSecs)); |
892 } | 901 } |
893 | 902 |
894 void TopSitesImpl::OnTopSitesAvailableFromHistory( | 903 void TopSitesImpl::OnTopSitesAvailableFromHistory( |
895 const MostVisitedURLList* pages) { | 904 const MostVisitedURLList* pages) { |
896 DCHECK(pages); | 905 DCHECK(pages); |
897 SetTopSites(*pages); | 906 SetTopSites(*pages, false); |
898 } | 907 } |
899 | 908 |
900 void TopSitesImpl::OnURLsDeleted(HistoryService* history_service, | 909 void TopSitesImpl::OnURLsDeleted(HistoryService* history_service, |
901 bool all_history, | 910 bool all_history, |
902 bool expired, | 911 bool expired, |
903 const URLRows& deleted_rows, | 912 const URLRows& deleted_rows, |
904 const std::set<GURL>& favicon_urls) { | 913 const std::set<GURL>& favicon_urls) { |
905 if (!loaded_) | 914 if (!loaded_) |
906 return; | 915 return; |
907 | 916 |
908 if (all_history) { | 917 if (all_history) { |
909 SetTopSites(MostVisitedURLList()); | 918 SetTopSites(MostVisitedURLList(), false); |
910 backend_->ResetDatabase(); | 919 backend_->ResetDatabase(); |
911 } else { | 920 } else { |
912 std::set<size_t> indices_to_delete; // Indices into top_sites_. | 921 std::set<size_t> indices_to_delete; // Indices into top_sites_. |
913 for (const auto& row : deleted_rows) { | 922 for (const auto& row : deleted_rows) { |
914 if (cache_->IsKnownURL(row.url())) | 923 if (cache_->IsKnownURL(row.url())) |
915 indices_to_delete.insert(cache_->GetURLIndex(row.url())); | 924 indices_to_delete.insert(cache_->GetURLIndex(row.url())); |
916 } | 925 } |
917 | 926 |
918 if (indices_to_delete.empty()) | 927 if (indices_to_delete.empty()) |
919 return; | 928 return; |
920 | 929 |
921 MostVisitedURLList new_top_sites(cache_->top_sites()); | 930 MostVisitedURLList new_top_sites(cache_->top_sites()); |
922 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); | 931 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); |
923 i != indices_to_delete.rend(); i++) { | 932 i != indices_to_delete.rend(); i++) { |
924 new_top_sites.erase(new_top_sites.begin() + *i); | 933 new_top_sites.erase(new_top_sites.begin() + *i); |
925 } | 934 } |
926 SetTopSites(new_top_sites); | 935 SetTopSites(new_top_sites, false); |
927 } | 936 } |
928 StartQueryForMostVisited(); | 937 StartQueryForMostVisited(); |
929 } | 938 } |
930 | 939 |
931 void TopSitesImpl::HistoryServiceBeingDeleted(HistoryService* history_service) { | 940 void TopSitesImpl::HistoryServiceBeingDeleted(HistoryService* history_service) { |
932 history_service_observer_.Remove(history_service); | 941 history_service_observer_.Remove(history_service); |
933 } | 942 } |
934 | 943 |
935 } // namespace history | 944 } // namespace history |
OLD | NEW |