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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 // Time from startup to first HistoryService query. | 89 // Time from startup to first HistoryService query. |
| 90 static const int64 kUpdateIntervalSecs = 15; | 90 static const int64 kUpdateIntervalSecs = 15; |
| 91 // Intervals between requests to HistoryService. | 91 // Intervals between requests to HistoryService. |
| 92 static const int64 kMinUpdateIntervalMinutes = 1; | 92 static const int64 kMinUpdateIntervalMinutes = 1; |
| 93 static const int64 kMaxUpdateIntervalMinutes = 60; | 93 static const int64 kMaxUpdateIntervalMinutes = 60; |
| 94 | 94 |
| 95 // Use 100 quality (highest quality) because we're very sensitive to | 95 // Use 100 quality (highest quality) because we're very sensitive to |
| 96 // artifacts for these small sized, highly detailed images. | 96 // artifacts for these small sized, highly detailed images. |
| 97 static const int kTopSitesImageQuality = 100; | 97 static const int kTopSitesImageQuality = 100; |
| 98 | 98 |
| 99 // Initially, histogram is not recorded. | |
| 100 bool TopSitesImpl::histogram_recorded_ = false; | |
| 101 | |
| 99 TopSitesImpl::TopSitesImpl(Profile* profile, | 102 TopSitesImpl::TopSitesImpl(Profile* profile, |
| 100 const PrepopulatedPageList& prepopulated_pages) | 103 const PrepopulatedPageList& prepopulated_pages) |
| 101 : backend_(NULL), | 104 : backend_(NULL), |
| 102 cache_(new TopSitesCache()), | 105 cache_(new TopSitesCache()), |
| 103 thread_safe_cache_(new TopSitesCache()), | 106 thread_safe_cache_(new TopSitesCache()), |
| 104 profile_(profile), | 107 profile_(profile), |
| 105 last_num_urls_changed_(0), | 108 last_num_urls_changed_(0), |
| 106 prepopulated_pages_(prepopulated_pages), | 109 prepopulated_pages_(prepopulated_pages), |
| 107 loaded_(false), | 110 loaded_(false), |
| 108 history_service_observer_(this) { | 111 history_service_observer_(this) { |
| (...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 618 new_url.redirects.push_back(url); | 621 new_url.redirects.push_back(url); |
| 619 } | 622 } |
| 620 new_url.last_forced_time = time; | 623 new_url.last_forced_time = time; |
| 621 // Add forced URLs and sort. Added to the end of the list of forced URLs | 624 // 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 | 625 // since this is almost always where it needs to go, unless the user's local |
| 623 // clock is fiddled with. | 626 // clock is fiddled with. |
| 624 MostVisitedURLList::iterator mid = new_list.begin() + num_forced; | 627 MostVisitedURLList::iterator mid = new_list.begin() + num_forced; |
| 625 new_list.insert(mid, new_url); | 628 new_list.insert(mid, new_url); |
| 626 mid = new_list.begin() + num_forced; // Mid was invalidated. | 629 mid = new_list.begin() + num_forced; // Mid was invalidated. |
| 627 std::inplace_merge(new_list.begin(), mid, mid + 1, ForcedURLComparator); | 630 std::inplace_merge(new_list.begin(), mid, mid + 1, ForcedURLComparator); |
| 628 SetTopSites(new_list); | 631 SetTopSites(new_list, CALL_LOCATION_FROM_OTHER_PLACES); |
| 629 return true; | 632 return true; |
| 630 } | 633 } |
| 631 | 634 |
| 632 bool TopSitesImpl::AddPrepopulatedPages(MostVisitedURLList* urls, | 635 bool TopSitesImpl::AddPrepopulatedPages(MostVisitedURLList* urls, |
| 633 size_t num_forced_urls) { | 636 size_t num_forced_urls) { |
| 634 bool added = false; | 637 bool added = false; |
| 635 for (const auto& prepopulated_page : prepopulated_pages_) { | 638 for (const auto& prepopulated_page : prepopulated_pages_) { |
| 636 if (urls->size() - num_forced_urls < kNonForcedTopSitesNumber && | 639 if (urls->size() - num_forced_urls < kNonForcedTopSitesNumber && |
| 637 IndexOf(*urls, prepopulated_page.most_visited.url) == -1) { | 640 IndexOf(*urls, prepopulated_page.most_visited.url) == -1) { |
| 638 urls->push_back(prepopulated_page.most_visited); | 641 urls->push_back(prepopulated_page.most_visited); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 745 return; | 748 return; |
| 746 const GURL& url = load_details->entry->GetURL(); | 749 const GURL& url = load_details->entry->GetURL(); |
| 747 if (!cache_->IsKnownURL(url) && CanAddURLToHistory(url)) { | 750 if (!cache_->IsKnownURL(url) && CanAddURLToHistory(url)) { |
| 748 // To avoid slamming history we throttle requests when the url updates. | 751 // To avoid slamming history we throttle requests when the url updates. |
| 749 // To do otherwise negatively impacts perf tests. | 752 // To do otherwise negatively impacts perf tests. |
| 750 RestartQueryForTopSitesTimer(GetUpdateDelay()); | 753 RestartQueryForTopSitesTimer(GetUpdateDelay()); |
| 751 } | 754 } |
| 752 } | 755 } |
| 753 } | 756 } |
| 754 | 757 |
| 755 void TopSitesImpl::SetTopSites(const MostVisitedURLList& new_top_sites) { | 758 void TopSitesImpl::SetTopSites(const MostVisitedURLList& new_top_sites, |
| 759 const CallLocation location) { | |
| 756 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 760 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 757 | 761 |
| 758 MostVisitedURLList top_sites(new_top_sites); | 762 MostVisitedURLList top_sites(new_top_sites); |
| 759 size_t num_forced_urls = MergeCachedForcedURLs(&top_sites); | 763 size_t num_forced_urls = MergeCachedForcedURLs(&top_sites); |
| 760 AddPrepopulatedPages(&top_sites, num_forced_urls); | 764 AddPrepopulatedPages(&top_sites, num_forced_urls); |
| 761 | 765 |
| 762 TopSitesDelta delta; | 766 TopSitesDelta delta; |
| 763 DiffMostVisited(cache_->top_sites(), top_sites, &delta); | 767 DiffMostVisited(cache_->top_sites(), top_sites, &delta); |
| 768 | |
| 769 TopSitesBackend::RecordHistogram record_or_not = | |
| 770 TopSitesBackend::RECORD_HISTOGRAM_NO; | |
| 771 | |
| 772 // Record the delta size into a histogram if this function is called from | |
| 773 // function OnGotMostVisitedThumbnails and no histogram value has been | |
| 774 // recorded before. | |
| 775 if (location == CALL_LOCATION_FROM_ON_GOT_MOST_VISITED_THUMBNAILS && | |
| 776 !histogram_recorded_) { | |
| 777 size_t delta_size = | |
| 778 delta.deleted.size() + delta.added.size() + delta.moved.size(); | |
| 779 UMA_HISTOGRAM_COUNTS_100( | |
| 780 "History.UpdateTopSitesOnDBThread_Startup_DeltaSize", delta_size); | |
|
sky
2015/04/17 15:07:02
Should this be named differently? This isn't on th
yao
2015/04/17 15:42:50
Done.
| |
| 781 // Will be passed to TopSitesBackend to let it record the histogram too. | |
| 782 record_or_not = TopSitesBackend::RECORD_HISTOGRAM_YES; | |
| 783 // Change it to true so that the histogram will not be recorded any more. | |
| 784 histogram_recorded_ = true; | |
| 785 } | |
| 786 | |
| 764 if (!delta.deleted.empty() || !delta.added.empty() || !delta.moved.empty()) { | 787 if (!delta.deleted.empty() || !delta.added.empty() || !delta.moved.empty()) { |
| 765 backend_->UpdateTopSites(delta); | 788 backend_->UpdateTopSites(delta, record_or_not); |
| 766 } | 789 } |
| 767 | 790 |
| 768 last_num_urls_changed_ = delta.added.size() + delta.moved.size(); | 791 last_num_urls_changed_ = delta.added.size() + delta.moved.size(); |
| 769 | 792 |
| 770 // We always do the following steps (setting top sites in cache, and resetting | 793 // 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 | 794 // thread safe cache ...) as this method is invoked during startup at which |
| 772 // point the caches haven't been updated yet. | 795 // point the caches haven't been updated yet. |
| 773 cache_->SetTopSites(top_sites); | 796 cache_->SetTopSites(top_sites); |
| 774 | 797 |
| 775 // See if we have any tmp thumbnails for the new sites. | 798 // See if we have any tmp thumbnails for the new sites. |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 872 timer_.Start(FROM_HERE, delta, this, &TopSitesImpl::TimerFired); | 895 timer_.Start(FROM_HERE, delta, this, &TopSitesImpl::TimerFired); |
| 873 } | 896 } |
| 874 | 897 |
| 875 void TopSitesImpl::OnGotMostVisitedThumbnails( | 898 void TopSitesImpl::OnGotMostVisitedThumbnails( |
| 876 const scoped_refptr<MostVisitedThumbnails>& thumbnails) { | 899 const scoped_refptr<MostVisitedThumbnails>& thumbnails) { |
| 877 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 900 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 878 | 901 |
| 879 // Set the top sites directly in the cache so that SetTopSites diffs | 902 // Set the top sites directly in the cache so that SetTopSites diffs |
| 880 // correctly. | 903 // correctly. |
| 881 cache_->SetTopSites(thumbnails->most_visited); | 904 cache_->SetTopSites(thumbnails->most_visited); |
| 882 SetTopSites(thumbnails->most_visited); | 905 SetTopSites(thumbnails->most_visited, |
| 906 CALL_LOCATION_FROM_ON_GOT_MOST_VISITED_THUMBNAILS); | |
| 883 cache_->SetThumbnails(thumbnails->url_to_images_map); | 907 cache_->SetThumbnails(thumbnails->url_to_images_map); |
| 884 | 908 |
| 885 ResetThreadSafeImageCache(); | 909 ResetThreadSafeImageCache(); |
| 886 | 910 |
| 887 MoveStateToLoaded(); | 911 MoveStateToLoaded(); |
| 888 | 912 |
| 889 // Start a timer that refreshes top sites from history. | 913 // Start a timer that refreshes top sites from history. |
| 890 RestartQueryForTopSitesTimer( | 914 RestartQueryForTopSitesTimer( |
| 891 base::TimeDelta::FromSeconds(kUpdateIntervalSecs)); | 915 base::TimeDelta::FromSeconds(kUpdateIntervalSecs)); |
| 892 } | 916 } |
| 893 | 917 |
| 894 void TopSitesImpl::OnTopSitesAvailableFromHistory( | 918 void TopSitesImpl::OnTopSitesAvailableFromHistory( |
| 895 const MostVisitedURLList* pages) { | 919 const MostVisitedURLList* pages) { |
| 896 DCHECK(pages); | 920 DCHECK(pages); |
| 897 SetTopSites(*pages); | 921 SetTopSites(*pages, CALL_LOCATION_FROM_OTHER_PLACES); |
| 898 } | 922 } |
| 899 | 923 |
| 900 void TopSitesImpl::OnURLsDeleted(HistoryService* history_service, | 924 void TopSitesImpl::OnURLsDeleted(HistoryService* history_service, |
| 901 bool all_history, | 925 bool all_history, |
| 902 bool expired, | 926 bool expired, |
| 903 const URLRows& deleted_rows, | 927 const URLRows& deleted_rows, |
| 904 const std::set<GURL>& favicon_urls) { | 928 const std::set<GURL>& favicon_urls) { |
| 905 if (!loaded_) | 929 if (!loaded_) |
| 906 return; | 930 return; |
| 907 | 931 |
| 908 if (all_history) { | 932 if (all_history) { |
| 909 SetTopSites(MostVisitedURLList()); | 933 SetTopSites(MostVisitedURLList(), CALL_LOCATION_FROM_OTHER_PLACES); |
| 910 backend_->ResetDatabase(); | 934 backend_->ResetDatabase(); |
| 911 } else { | 935 } else { |
| 912 std::set<size_t> indices_to_delete; // Indices into top_sites_. | 936 std::set<size_t> indices_to_delete; // Indices into top_sites_. |
| 913 for (const auto& row : deleted_rows) { | 937 for (const auto& row : deleted_rows) { |
| 914 if (cache_->IsKnownURL(row.url())) | 938 if (cache_->IsKnownURL(row.url())) |
| 915 indices_to_delete.insert(cache_->GetURLIndex(row.url())); | 939 indices_to_delete.insert(cache_->GetURLIndex(row.url())); |
| 916 } | 940 } |
| 917 | 941 |
| 918 if (indices_to_delete.empty()) | 942 if (indices_to_delete.empty()) |
| 919 return; | 943 return; |
| 920 | 944 |
| 921 MostVisitedURLList new_top_sites(cache_->top_sites()); | 945 MostVisitedURLList new_top_sites(cache_->top_sites()); |
| 922 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); | 946 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); |
| 923 i != indices_to_delete.rend(); i++) { | 947 i != indices_to_delete.rend(); i++) { |
| 924 new_top_sites.erase(new_top_sites.begin() + *i); | 948 new_top_sites.erase(new_top_sites.begin() + *i); |
| 925 } | 949 } |
| 926 SetTopSites(new_top_sites); | 950 SetTopSites(new_top_sites, CALL_LOCATION_FROM_OTHER_PLACES); |
| 927 } | 951 } |
| 928 StartQueryForMostVisited(); | 952 StartQueryForMostVisited(); |
| 929 } | 953 } |
| 930 | 954 |
| 931 void TopSitesImpl::HistoryServiceBeingDeleted(HistoryService* history_service) { | 955 void TopSitesImpl::HistoryServiceBeingDeleted(HistoryService* history_service) { |
| 932 history_service_observer_.Remove(history_service); | 956 history_service_observer_.Remove(history_service); |
| 933 } | 957 } |
| 934 | 958 |
| 935 } // namespace history | 959 } // namespace history |
| OLD | NEW |