OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "chrome/browser/history/top_sites.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 const int result_count_; | 120 const int result_count_; |
121 | 121 |
122 ThumbnailMigration data_; | 122 ThumbnailMigration data_; |
123 | 123 |
124 DISALLOW_COPY_AND_ASSIGN(LoadThumbnailsFromHistoryTask); | 124 DISALLOW_COPY_AND_ASSIGN(LoadThumbnailsFromHistoryTask); |
125 }; | 125 }; |
126 | 126 |
127 } // namespace | 127 } // namespace |
128 | 128 |
129 TopSites::TopSites(Profile* profile) | 129 TopSites::TopSites(Profile* profile) |
130 : backend_(new TopSitesBackend()), | 130 : backend_(NULL), |
131 cache_(new TopSitesCache()), | 131 cache_(new TopSitesCache()), |
132 thread_safe_cache_(new TopSitesCache()), | 132 thread_safe_cache_(new TopSitesCache()), |
133 profile_(profile), | 133 profile_(profile), |
134 last_num_urls_changed_(0), | 134 last_num_urls_changed_(0), |
135 blacklist_(NULL), | 135 blacklist_(NULL), |
136 pinned_urls_(NULL), | 136 pinned_urls_(NULL), |
137 history_state_(HISTORY_LOADING), | 137 history_state_(HISTORY_LOADING), |
138 top_sites_state_(TOP_SITES_LOADING), | 138 top_sites_state_(TOP_SITES_LOADING), |
139 loaded_(false) { | 139 loaded_(false) { |
140 if (!profile_) | 140 if (!profile_) |
141 return; | 141 return; |
142 | 142 |
143 if (NotificationService::current()) { | 143 if (NotificationService::current()) { |
144 registrar_.Add(this, NotificationType::HISTORY_URLS_DELETED, | 144 registrar_.Add(this, NotificationType::HISTORY_URLS_DELETED, |
145 Source<Profile>(profile_)); | 145 Source<Profile>(profile_)); |
146 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 146 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
147 NotificationService::AllSources()); | 147 NotificationService::AllSources()); |
148 } | 148 } |
149 | 149 |
150 blacklist_ = profile_->GetPrefs()-> | 150 blacklist_ = profile_->GetPrefs()-> |
151 GetMutableDictionary(prefs::kNTPMostVisitedURLsBlacklist); | 151 GetMutableDictionary(prefs::kNTPMostVisitedURLsBlacklist); |
152 pinned_urls_ = profile_->GetPrefs()-> | 152 pinned_urls_ = profile_->GetPrefs()-> |
153 GetMutableDictionary(prefs::kNTPMostVisitedPinnedURLs); | 153 GetMutableDictionary(prefs::kNTPMostVisitedPinnedURLs); |
154 } | 154 } |
155 | 155 |
156 void TopSites::Init(const FilePath& db_name) { | 156 void TopSites::Init(const FilePath& db_name) { |
| 157 // Create the backend here, rather than in the constructor, so that |
| 158 // unit tests that do not need the backend can run without a problem. |
| 159 backend_ = new TopSitesBackend; |
157 backend_->Init(db_name); | 160 backend_->Init(db_name); |
158 backend_->GetMostVisitedThumbnails( | 161 backend_->GetMostVisitedThumbnails( |
159 &cancelable_consumer_, | 162 &cancelable_consumer_, |
160 NewCallback(this, &TopSites::OnGotMostVisitedThumbnails)); | 163 NewCallback(this, &TopSites::OnGotMostVisitedThumbnails)); |
161 | 164 |
162 // History may have already finished loading by the time we're created. | 165 // History may have already finished loading by the time we're created. |
163 HistoryService* history = profile_->GetHistoryServiceWithoutCreating(); | 166 HistoryService* history = profile_->GetHistoryServiceWithoutCreating(); |
164 if (history && history->backend_loaded()) { | 167 if (history && history->backend_loaded()) { |
165 if (history->needs_top_sites_migration()) | 168 if (history->needs_top_sites_migration()) |
166 MigrateFromHistory(); | 169 MigrateFromHistory(); |
167 else | 170 else |
168 history_state_ = HISTORY_LOADED; | 171 history_state_ = HISTORY_LOADED; |
169 } | 172 } |
170 } | 173 } |
171 | 174 |
172 bool TopSites::SetPageThumbnail(const GURL& url, | 175 bool TopSites::SetPageThumbnail(const GURL& url, |
173 const SkBitmap& thumbnail, | 176 const SkBitmap& thumbnail, |
174 const ThumbnailScore& score) { | 177 const ThumbnailScore& score) { |
175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
176 | 179 |
177 if (!loaded_) { | 180 if (!loaded_) { |
178 // TODO(sky): I need to cache these and apply them after the load | 181 // TODO(sky): I need to cache these and apply them after the load |
179 // completes. | 182 // completes. |
180 return false; | 183 return false; |
181 } | 184 } |
182 | 185 |
183 bool add_temp_thumbnail = false; | 186 bool add_temp_thumbnail = false; |
184 if (!cache_->IsKnownURL(url)) { | 187 if (!IsKnownURL(url)) { |
185 if (cache_->top_sites().size() < kTopSitesNumber) { | 188 if (!IsFull()) { |
186 add_temp_thumbnail = true; | 189 add_temp_thumbnail = true; |
187 } else { | 190 } else { |
188 return false; // This URL is not known to us. | 191 return false; // This URL is not known to us. |
189 } | 192 } |
190 } | 193 } |
191 | 194 |
192 if (!HistoryService::CanAddURL(url)) | 195 if (!HistoryService::CanAddURL(url)) |
193 return false; // It's not a real webpage. | 196 return false; // It's not a real webpage. |
194 | 197 |
195 scoped_refptr<RefCountedBytes> thumbnail_data; | 198 scoped_refptr<RefCountedBytes> thumbnail_data; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 request->ForwardResult(GetTopSitesCallback::TupleType(filtered_urls)); | 233 request->ForwardResult(GetTopSitesCallback::TupleType(filtered_urls)); |
231 } | 234 } |
232 | 235 |
233 bool TopSites::GetPageThumbnail(const GURL& url, | 236 bool TopSites::GetPageThumbnail(const GURL& url, |
234 scoped_refptr<RefCountedBytes>* bytes) { | 237 scoped_refptr<RefCountedBytes>* bytes) { |
235 // WARNING: this may be invoked on any thread. | 238 // WARNING: this may be invoked on any thread. |
236 base::AutoLock lock(lock_); | 239 base::AutoLock lock(lock_); |
237 return thread_safe_cache_->GetPageThumbnail(url, bytes); | 240 return thread_safe_cache_->GetPageThumbnail(url, bytes); |
238 } | 241 } |
239 | 242 |
| 243 bool TopSites::GetPageThumbnailScore(const GURL& url, |
| 244 ThumbnailScore* score) { |
| 245 // WARNING: this may be invoked on any thread. |
| 246 base::AutoLock lock(lock_); |
| 247 return thread_safe_cache_->GetPageThumbnailScore(url, score); |
| 248 } |
| 249 |
240 // Returns the index of |url| in |urls|, or -1 if not found. | 250 // Returns the index of |url| in |urls|, or -1 if not found. |
241 static int IndexOf(const MostVisitedURLList& urls, const GURL& url) { | 251 static int IndexOf(const MostVisitedURLList& urls, const GURL& url) { |
242 for (size_t i = 0; i < urls.size(); i++) { | 252 for (size_t i = 0; i < urls.size(); i++) { |
243 if (urls[i].url == url) | 253 if (urls[i].url == url) |
244 return i; | 254 return i; |
245 } | 255 } |
246 return -1; | 256 return -1; |
247 } | 257 } |
248 | 258 |
249 void TopSites::MigrateFromHistory() { | 259 void TopSites::MigrateFromHistory() { |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 if (hs) { | 452 if (hs) { |
443 return hs->QueryMostVisitedURLs( | 453 return hs->QueryMostVisitedURLs( |
444 num_results_to_request_from_history(), | 454 num_results_to_request_from_history(), |
445 kDaysOfHistory, | 455 kDaysOfHistory, |
446 &cancelable_consumer_, | 456 &cancelable_consumer_, |
447 NewCallback(this, &TopSites::OnTopSitesAvailableFromHistory)); | 457 NewCallback(this, &TopSites::OnTopSitesAvailableFromHistory)); |
448 } | 458 } |
449 return 0; | 459 return 0; |
450 } | 460 } |
451 | 461 |
| 462 bool TopSites::IsKnownURL(const GURL& url) { |
| 463 return loaded_ && cache_->IsKnownURL(url); |
| 464 } |
| 465 |
| 466 bool TopSites::IsFull() { |
| 467 return loaded_ && cache_->top_sites().size() >= kTopSitesNumber; |
| 468 } |
| 469 |
452 TopSites::~TopSites() { | 470 TopSites::~TopSites() { |
453 } | 471 } |
454 | 472 |
455 bool TopSites::SetPageThumbnailNoDB(const GURL& url, | 473 bool TopSites::SetPageThumbnailNoDB(const GURL& url, |
456 const RefCountedBytes* thumbnail_data, | 474 const RefCountedBytes* thumbnail_data, |
457 const ThumbnailScore& score) { | 475 const ThumbnailScore& score) { |
458 // This should only be invoked when we know about the url. | 476 // This should only be invoked when we know about the url. |
459 DCHECK(cache_->IsKnownURL(url)); | 477 DCHECK(cache_->IsKnownURL(url)); |
460 | 478 |
461 const MostVisitedURL& most_visited = | 479 const MostVisitedURL& most_visited = |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
717 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); | 735 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); |
718 i != indices_to_delete.rend(); i++) { | 736 i != indices_to_delete.rend(); i++) { |
719 size_t index = *i; | 737 size_t index = *i; |
720 RemovePinnedURL(new_top_sites[index].url); | 738 RemovePinnedURL(new_top_sites[index].url); |
721 new_top_sites.erase(new_top_sites.begin() + index); | 739 new_top_sites.erase(new_top_sites.begin() + index); |
722 } | 740 } |
723 SetTopSites(new_top_sites); | 741 SetTopSites(new_top_sites); |
724 } | 742 } |
725 StartQueryForMostVisited(); | 743 StartQueryForMostVisited(); |
726 } else if (type == NotificationType::NAV_ENTRY_COMMITTED) { | 744 } else if (type == NotificationType::NAV_ENTRY_COMMITTED) { |
727 if (cache_->top_sites().size() < kTopSitesNumber) { | 745 if (!IsFull()) { |
728 NavigationController::LoadCommittedDetails* load_details = | 746 NavigationController::LoadCommittedDetails* load_details = |
729 Details<NavigationController::LoadCommittedDetails>(details).ptr(); | 747 Details<NavigationController::LoadCommittedDetails>(details).ptr(); |
730 if (!load_details) | 748 if (!load_details) |
731 return; | 749 return; |
732 const GURL& url = load_details->entry->url(); | 750 const GURL& url = load_details->entry->url(); |
733 if (!cache_->IsKnownURL(url) && HistoryService::CanAddURL(url)) { | 751 if (!cache_->IsKnownURL(url) && HistoryService::CanAddURL(url)) { |
734 // To avoid slamming history we throttle requests when the url updates. | 752 // To avoid slamming history we throttle requests when the url updates. |
735 // To do otherwise negatively impacts perf tests. | 753 // To do otherwise negatively impacts perf tests. |
736 RestartQueryForTopSitesTimer(GetUpdateDelay()); | 754 RestartQueryForTopSitesTimer(GetUpdateDelay()); |
737 } | 755 } |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
902 SetTopSites(pages); | 920 SetTopSites(pages); |
903 | 921 |
904 // Used only in testing. | 922 // Used only in testing. |
905 NotificationService::current()->Notify( | 923 NotificationService::current()->Notify( |
906 NotificationType::TOP_SITES_UPDATED, | 924 NotificationType::TOP_SITES_UPDATED, |
907 Source<TopSites>(this), | 925 Source<TopSites>(this), |
908 Details<CancelableRequestProvider::Handle>(&handle)); | 926 Details<CancelableRequestProvider::Handle>(&handle)); |
909 } | 927 } |
910 | 928 |
911 } // namespace history | 929 } // namespace history |
OLD | NEW |