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 "components/history/core/browser/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/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "base/task_runner.h" | 22 #include "base/task_runner.h" |
23 #include "base/values.h" | 23 #include "base/values.h" |
24 #include "chrome/browser/history/history_utils.h" | |
25 #include "components/history/core/browser/history_backend.h" | 24 #include "components/history/core/browser/history_backend.h" |
26 #include "components/history/core/browser/history_db_task.h" | 25 #include "components/history/core/browser/history_db_task.h" |
27 #include "components/history/core/browser/page_usage_data.h" | 26 #include "components/history/core/browser/page_usage_data.h" |
28 #include "components/history/core/browser/top_sites_cache.h" | 27 #include "components/history/core/browser/top_sites_cache.h" |
29 #include "components/history/core/browser/url_utils.h" | 28 #include "components/history/core/browser/url_utils.h" |
30 #include "components/history/core/common/thumbnail_score.h" | 29 #include "components/history/core/common/thumbnail_score.h" |
31 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
32 #include "ui/base/layout.h" | 31 #include "ui/base/layout.h" |
33 #include "ui/base/resource/resource_bundle.h" | 32 #include "ui/base/resource/resource_bundle.h" |
34 #include "ui/gfx/image/image_util.h" | 33 #include "ui/gfx/image/image_util.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 const int kTopSitesImageQuality = 100; | 81 const int kTopSitesImageQuality = 100; |
83 | 82 |
84 } // namespace | 83 } // namespace |
85 | 84 |
86 // Initially, histogram is not recorded. | 85 // Initially, histogram is not recorded. |
87 bool TopSitesImpl::histogram_recorded_ = false; | 86 bool TopSitesImpl::histogram_recorded_ = false; |
88 | 87 |
89 TopSitesImpl::TopSitesImpl(PrefService* pref_service, | 88 TopSitesImpl::TopSitesImpl(PrefService* pref_service, |
90 HistoryService* history_service, | 89 HistoryService* history_service, |
91 const char* blacklist_pref_name, | 90 const char* blacklist_pref_name, |
92 const PrepopulatedPageList& prepopulated_pages) | 91 const PrepopulatedPageList& prepopulated_pages, |
| 92 const CanAddURLToHistoryFn& can_add_url_to_history) |
93 : backend_(nullptr), | 93 : backend_(nullptr), |
94 cache_(new TopSitesCache()), | 94 cache_(new TopSitesCache()), |
95 thread_safe_cache_(new TopSitesCache()), | 95 thread_safe_cache_(new TopSitesCache()), |
96 last_num_urls_changed_(0), | 96 last_num_urls_changed_(0), |
97 prepopulated_pages_(prepopulated_pages), | 97 prepopulated_pages_(prepopulated_pages), |
98 pref_service_(pref_service), | 98 pref_service_(pref_service), |
99 blacklist_pref_name_(blacklist_pref_name), | 99 blacklist_pref_name_(blacklist_pref_name), |
100 history_service_(history_service), | 100 history_service_(history_service), |
| 101 can_add_url_to_history_(can_add_url_to_history), |
101 loaded_(false), | 102 loaded_(false), |
102 history_service_observer_(this) { | 103 history_service_observer_(this) { |
103 DCHECK(pref_service_); | 104 DCHECK(pref_service_); |
104 DCHECK(blacklist_pref_name_); | 105 DCHECK(blacklist_pref_name_); |
| 106 DCHECK(!can_add_url_to_history_.is_null()); |
105 } | 107 } |
106 | 108 |
107 void TopSitesImpl::Init( | 109 void TopSitesImpl::Init( |
108 const base::FilePath& db_name, | 110 const base::FilePath& db_name, |
109 const scoped_refptr<base::SingleThreadTaskRunner>& db_task_runner) { | 111 const scoped_refptr<base::SingleThreadTaskRunner>& db_task_runner) { |
110 // Create the backend here, rather than in the constructor, so that | 112 // Create the backend here, rather than in the constructor, so that |
111 // unit tests that do not need the backend can run without a problem. | 113 // unit tests that do not need the backend can run without a problem. |
112 backend_ = new TopSitesBackend(db_task_runner); | 114 backend_ = new TopSitesBackend(db_task_runner); |
113 backend_->Init(db_name); | 115 backend_->Init(db_name); |
114 backend_->GetMostVisitedThumbnails( | 116 backend_->GetMostVisitedThumbnails( |
(...skipping 15 matching lines...) Expand all Loading... |
130 | 132 |
131 bool add_temp_thumbnail = false; | 133 bool add_temp_thumbnail = false; |
132 if (!IsKnownURL(url)) { | 134 if (!IsKnownURL(url)) { |
133 if (!IsNonForcedFull()) { | 135 if (!IsNonForcedFull()) { |
134 add_temp_thumbnail = true; | 136 add_temp_thumbnail = true; |
135 } else { | 137 } else { |
136 return false; // This URL is not known to us. | 138 return false; // This URL is not known to us. |
137 } | 139 } |
138 } | 140 } |
139 | 141 |
140 if (!CanAddURLToHistory(url)) | 142 if (!can_add_url_to_history_.Run(url)) |
141 return false; // It's not a real webpage. | 143 return false; // It's not a real webpage. |
142 | 144 |
143 scoped_refptr<base::RefCountedBytes> thumbnail_data; | 145 scoped_refptr<base::RefCountedBytes> thumbnail_data; |
144 if (!EncodeBitmap(thumbnail, &thumbnail_data)) | 146 if (!EncodeBitmap(thumbnail, &thumbnail_data)) |
145 return false; | 147 return false; |
146 | 148 |
147 if (add_temp_thumbnail) { | 149 if (add_temp_thumbnail) { |
148 // Always remove the existing entry and then add it back. That way if we end | 150 // Always remove the existing entry and then add it back. That way if we end |
149 // up with too many temp thumbnails we'll prune the oldest first. | 151 // up with too many temp thumbnails we'll prune the oldest first. |
150 RemoveTemporaryThumbnailByURL(url); | 152 RemoveTemporaryThumbnailByURL(url); |
(...skipping 18 matching lines...) Expand all Loading... |
169 | 171 |
170 bool add_temp_thumbnail = false; | 172 bool add_temp_thumbnail = false; |
171 if (!IsKnownURL(url)) { | 173 if (!IsKnownURL(url)) { |
172 if (!IsNonForcedFull()) { | 174 if (!IsNonForcedFull()) { |
173 add_temp_thumbnail = true; | 175 add_temp_thumbnail = true; |
174 } else { | 176 } else { |
175 return false; // This URL is not known to us. | 177 return false; // This URL is not known to us. |
176 } | 178 } |
177 } | 179 } |
178 | 180 |
179 if (!CanAddURLToHistory(url)) | 181 if (!can_add_url_to_history_.Run(url)) |
180 return false; // It's not a real webpage. | 182 return false; // It's not a real webpage. |
181 | 183 |
182 if (add_temp_thumbnail) { | 184 if (add_temp_thumbnail) { |
183 // Always remove the existing entry and then add it back. That way if we end | 185 // Always remove the existing entry and then add it back. That way if we end |
184 // up with too many temp thumbnails we'll prune the oldest first. | 186 // up with too many temp thumbnails we'll prune the oldest first. |
185 RemoveTemporaryThumbnailByURL(url); | 187 RemoveTemporaryThumbnailByURL(url); |
186 AddTemporaryThumbnail(url, memory, score); | 188 AddTemporaryThumbnail(url, memory, score); |
187 return true; | 189 return true; |
188 } | 190 } |
189 | 191 |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 std::inplace_merge(new_list.begin(), mid, mid + 1, ForcedURLComparator); | 606 std::inplace_merge(new_list.begin(), mid, mid + 1, ForcedURLComparator); |
605 SetTopSites(new_list, CALL_LOCATION_FROM_OTHER_PLACES); | 607 SetTopSites(new_list, CALL_LOCATION_FROM_OTHER_PLACES); |
606 return true; | 608 return true; |
607 } | 609 } |
608 | 610 |
609 void TopSitesImpl::OnNavigationCommitted(const GURL& url) { | 611 void TopSitesImpl::OnNavigationCommitted(const GURL& url) { |
610 DCHECK(thread_checker_.CalledOnValidThread()); | 612 DCHECK(thread_checker_.CalledOnValidThread()); |
611 if (!loaded_ || IsNonForcedFull()) | 613 if (!loaded_ || IsNonForcedFull()) |
612 return; | 614 return; |
613 | 615 |
614 if (!cache_->IsKnownURL(url) && CanAddURLToHistory(url)) { | 616 if (!cache_->IsKnownURL(url) && can_add_url_to_history_.Run(url)) { |
615 // To avoid slamming history we throttle requests when the url updates. To | 617 // To avoid slamming history we throttle requests when the url updates. To |
616 // do otherwise negatively impacts perf tests. | 618 // do otherwise negatively impacts perf tests. |
617 RestartQueryForTopSitesTimer(GetUpdateDelay()); | 619 RestartQueryForTopSitesTimer(GetUpdateDelay()); |
618 } | 620 } |
619 } | 621 } |
620 | 622 |
621 bool TopSitesImpl::AddPrepopulatedPages(MostVisitedURLList* urls, | 623 bool TopSitesImpl::AddPrepopulatedPages(MostVisitedURLList* urls, |
622 size_t num_forced_urls) { | 624 size_t num_forced_urls) { |
623 bool added = false; | 625 bool added = false; |
624 for (const auto& prepopulated_page : prepopulated_pages_) { | 626 for (const auto& prepopulated_page : prepopulated_pages_) { |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
903 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); | 905 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); |
904 i != indices_to_delete.rend(); i++) { | 906 i != indices_to_delete.rend(); i++) { |
905 new_top_sites.erase(new_top_sites.begin() + *i); | 907 new_top_sites.erase(new_top_sites.begin() + *i); |
906 } | 908 } |
907 SetTopSites(new_top_sites, CALL_LOCATION_FROM_OTHER_PLACES); | 909 SetTopSites(new_top_sites, CALL_LOCATION_FROM_OTHER_PLACES); |
908 } | 910 } |
909 StartQueryForMostVisited(); | 911 StartQueryForMostVisited(); |
910 } | 912 } |
911 | 913 |
912 } // namespace history | 914 } // namespace history |
OLD | NEW |