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 backend_ = new TopSitesBackend; | |
satorux1
2011/01/26 10:53:59
Moved instantiation here.
brettw
2011/01/26 16:40:32
Can you add a comment why it's here rather than in
satorux1
2011/01/27 02:02:11
Good point. Added a comment.
| |
157 backend_->Init(db_name); | 158 backend_->Init(db_name); |
158 backend_->GetMostVisitedThumbnails( | 159 backend_->GetMostVisitedThumbnails( |
159 &cancelable_consumer_, | 160 &cancelable_consumer_, |
160 NewCallback(this, &TopSites::OnGotMostVisitedThumbnails)); | 161 NewCallback(this, &TopSites::OnGotMostVisitedThumbnails)); |
161 | 162 |
162 // History may have already finished loading by the time we're created. | 163 // History may have already finished loading by the time we're created. |
163 HistoryService* history = profile_->GetHistoryServiceWithoutCreating(); | 164 HistoryService* history = profile_->GetHistoryServiceWithoutCreating(); |
164 if (history && history->backend_loaded()) { | 165 if (history && history->backend_loaded()) { |
165 if (history->needs_top_sites_migration()) | 166 if (history->needs_top_sites_migration()) |
166 MigrateFromHistory(); | 167 MigrateFromHistory(); |
167 else | 168 else |
168 history_state_ = HISTORY_LOADED; | 169 history_state_ = HISTORY_LOADED; |
169 } | 170 } |
170 } | 171 } |
171 | 172 |
172 bool TopSites::SetPageThumbnail(const GURL& url, | 173 bool TopSites::SetPageThumbnail(const GURL& url, |
173 const SkBitmap& thumbnail, | 174 const SkBitmap& thumbnail, |
174 const ThumbnailScore& score) { | 175 const ThumbnailScore& score) { |
175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
176 | 177 |
177 if (!loaded_) { | 178 if (!loaded_) { |
178 // TODO(sky): I need to cache these and apply them after the load | 179 // TODO(sky): I need to cache these and apply them after the load |
179 // completes. | 180 // completes. |
180 return false; | 181 return false; |
181 } | 182 } |
182 | 183 |
183 bool add_temp_thumbnail = false; | 184 bool add_temp_thumbnail = false; |
184 if (!cache_->IsKnownURL(url)) { | 185 if (!IsKnownURL(url)) { |
185 if (cache_->top_sites().size() < kTopSitesNumber) { | 186 if (!IsFull()) { |
186 add_temp_thumbnail = true; | 187 add_temp_thumbnail = true; |
187 } else { | 188 } else { |
188 return false; // This URL is not known to us. | 189 return false; // This URL is not known to us. |
189 } | 190 } |
190 } | 191 } |
191 | 192 |
192 if (!HistoryService::CanAddURL(url)) | 193 if (!HistoryService::CanAddURL(url)) |
193 return false; // It's not a real webpage. | 194 return false; // It's not a real webpage. |
194 | 195 |
195 scoped_refptr<RefCountedBytes> thumbnail_data; | 196 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)); | 231 request->ForwardResult(GetTopSitesCallback::TupleType(filtered_urls)); |
231 } | 232 } |
232 | 233 |
233 bool TopSites::GetPageThumbnail(const GURL& url, | 234 bool TopSites::GetPageThumbnail(const GURL& url, |
234 scoped_refptr<RefCountedBytes>* bytes) { | 235 scoped_refptr<RefCountedBytes>* bytes) { |
235 // WARNING: this may be invoked on any thread. | 236 // WARNING: this may be invoked on any thread. |
236 base::AutoLock lock(lock_); | 237 base::AutoLock lock(lock_); |
237 return thread_safe_cache_->GetPageThumbnail(url, bytes); | 238 return thread_safe_cache_->GetPageThumbnail(url, bytes); |
238 } | 239 } |
239 | 240 |
241 bool TopSites::GetPageThumbnailScore(const GURL& url, | |
242 ThumbnailScore* score) { | |
243 // WARNING: this may be invoked on any thread. | |
244 base::AutoLock lock(lock_); | |
245 return thread_safe_cache_->GetPageThumbnailScore(url, score); | |
246 } | |
247 | |
240 // Returns the index of |url| in |urls|, or -1 if not found. | 248 // Returns the index of |url| in |urls|, or -1 if not found. |
241 static int IndexOf(const MostVisitedURLList& urls, const GURL& url) { | 249 static int IndexOf(const MostVisitedURLList& urls, const GURL& url) { |
242 for (size_t i = 0; i < urls.size(); i++) { | 250 for (size_t i = 0; i < urls.size(); i++) { |
243 if (urls[i].url == url) | 251 if (urls[i].url == url) |
244 return i; | 252 return i; |
245 } | 253 } |
246 return -1; | 254 return -1; |
247 } | 255 } |
248 | 256 |
249 void TopSites::MigrateFromHistory() { | 257 void TopSites::MigrateFromHistory() { |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
442 if (hs) { | 450 if (hs) { |
443 return hs->QueryMostVisitedURLs( | 451 return hs->QueryMostVisitedURLs( |
444 num_results_to_request_from_history(), | 452 num_results_to_request_from_history(), |
445 kDaysOfHistory, | 453 kDaysOfHistory, |
446 &cancelable_consumer_, | 454 &cancelable_consumer_, |
447 NewCallback(this, &TopSites::OnTopSitesAvailableFromHistory)); | 455 NewCallback(this, &TopSites::OnTopSitesAvailableFromHistory)); |
448 } | 456 } |
449 return 0; | 457 return 0; |
450 } | 458 } |
451 | 459 |
460 bool TopSites::IsKnownURL(const GURL& url) { | |
461 return loaded_ && cache_->IsKnownURL(url); | |
462 } | |
463 | |
464 bool TopSites::IsFull() { | |
465 return loaded_ && cache_->top_sites().size() >= kTopSitesNumber; | |
466 } | |
467 | |
452 TopSites::~TopSites() { | 468 TopSites::~TopSites() { |
453 } | 469 } |
454 | 470 |
455 bool TopSites::SetPageThumbnailNoDB(const GURL& url, | 471 bool TopSites::SetPageThumbnailNoDB(const GURL& url, |
456 const RefCountedBytes* thumbnail_data, | 472 const RefCountedBytes* thumbnail_data, |
457 const ThumbnailScore& score) { | 473 const ThumbnailScore& score) { |
458 // This should only be invoked when we know about the url. | 474 // This should only be invoked when we know about the url. |
459 DCHECK(cache_->IsKnownURL(url)); | 475 DCHECK(cache_->IsKnownURL(url)); |
460 | 476 |
461 const MostVisitedURL& most_visited = | 477 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(); | 733 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); |
718 i != indices_to_delete.rend(); i++) { | 734 i != indices_to_delete.rend(); i++) { |
719 size_t index = *i; | 735 size_t index = *i; |
720 RemovePinnedURL(new_top_sites[index].url); | 736 RemovePinnedURL(new_top_sites[index].url); |
721 new_top_sites.erase(new_top_sites.begin() + index); | 737 new_top_sites.erase(new_top_sites.begin() + index); |
722 } | 738 } |
723 SetTopSites(new_top_sites); | 739 SetTopSites(new_top_sites); |
724 } | 740 } |
725 StartQueryForMostVisited(); | 741 StartQueryForMostVisited(); |
726 } else if (type == NotificationType::NAV_ENTRY_COMMITTED) { | 742 } else if (type == NotificationType::NAV_ENTRY_COMMITTED) { |
727 if (cache_->top_sites().size() < kTopSitesNumber) { | 743 if (!IsFull()) { |
728 NavigationController::LoadCommittedDetails* load_details = | 744 NavigationController::LoadCommittedDetails* load_details = |
729 Details<NavigationController::LoadCommittedDetails>(details).ptr(); | 745 Details<NavigationController::LoadCommittedDetails>(details).ptr(); |
730 if (!load_details) | 746 if (!load_details) |
731 return; | 747 return; |
732 const GURL& url = load_details->entry->url(); | 748 const GURL& url = load_details->entry->url(); |
733 if (!cache_->IsKnownURL(url) && HistoryService::CanAddURL(url)) { | 749 if (!cache_->IsKnownURL(url) && HistoryService::CanAddURL(url)) { |
734 // To avoid slamming history we throttle requests when the url updates. | 750 // To avoid slamming history we throttle requests when the url updates. |
735 // To do otherwise negatively impacts perf tests. | 751 // To do otherwise negatively impacts perf tests. |
736 RestartQueryForTopSitesTimer(GetUpdateDelay()); | 752 RestartQueryForTopSitesTimer(GetUpdateDelay()); |
737 } | 753 } |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
902 SetTopSites(pages); | 918 SetTopSites(pages); |
903 | 919 |
904 // Used only in testing. | 920 // Used only in testing. |
905 NotificationService::current()->Notify( | 921 NotificationService::current()->Notify( |
906 NotificationType::TOP_SITES_UPDATED, | 922 NotificationType::TOP_SITES_UPDATED, |
907 Source<TopSites>(this), | 923 Source<TopSites>(this), |
908 Details<CancelableRequestProvider::Handle>(&handle)); | 924 Details<CancelableRequestProvider::Handle>(&handle)); |
909 } | 925 } |
910 | 926 |
911 } // namespace history | 927 } // namespace history |
OLD | NEW |