Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: chrome/browser/history/top_sites_impl.cc

Issue 815983002: Topsites become keyedService based. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix comments Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 if (content::NotificationService::current()) { 108 if (content::NotificationService::current()) {
109 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, 109 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED,
110 content::Source<Profile>(profile_)); 110 content::Source<Profile>(profile_));
111 // Listen for any nav commits. We'll ignore those not related to this 111 // Listen for any nav commits. We'll ignore those not related to this
112 // profile when we get the notification. 112 // profile when we get the notification.
113 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 113 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
114 content::NotificationService::AllSources()); 114 content::NotificationService::AllSources());
115 } 115 }
116 for (int i = 0; i < kPrepopulatedPagesCount; i++) { 116 for (int i = 0; i < kPrepopulatedPagesCount; i++) {
117 int url_id = kPrepopulatedPages[i].url_id; 117 int url_id = kPrepopulatedPages[i].url_id;
118 prepopulated_page_urls_.push_back( 118 prepopulated_page_urls_.push_back(GURL(l10n_util::GetStringUTF8(url_id)));
119 GURL(l10n_util::GetStringUTF8(url_id)));
120 } 119 }
121 } 120 }
122 121
123 void TopSitesImpl::Init(const base::FilePath& db_name) { 122 void TopSitesImpl::Init(const base::FilePath& db_name) {
124 // Create the backend here, rather than in the constructor, so that 123 // Create the backend here, rather than in the constructor, so that
125 // unit tests that do not need the backend can run without a problem. 124 // unit tests that do not need the backend can run without a problem.
126 backend_ = new TopSitesBackend; 125 backend_ = new TopSitesBackend;
127 backend_->Init(db_name); 126 backend_->Init(db_name);
128 backend_->GetMostVisitedThumbnails( 127 backend_->GetMostVisitedThumbnails(
129 base::Bind(&TopSitesImpl::OnGotMostVisitedThumbnails, 128 base::Bind(&TopSitesImpl::OnGotMostVisitedThumbnails,
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 // WARNING: this function may be invoked on any thread. 206 // WARNING: this function may be invoked on any thread.
208 void TopSitesImpl::GetMostVisitedURLs( 207 void TopSitesImpl::GetMostVisitedURLs(
209 const GetMostVisitedURLsCallback& callback, 208 const GetMostVisitedURLsCallback& callback,
210 bool include_forced_urls) { 209 bool include_forced_urls) {
211 MostVisitedURLList filtered_urls; 210 MostVisitedURLList filtered_urls;
212 { 211 {
213 base::AutoLock lock(lock_); 212 base::AutoLock lock(lock_);
214 if (!loaded_) { 213 if (!loaded_) {
215 // A request came in before we finished loading. Store the callback and 214 // A request came in before we finished loading. Store the callback and
216 // we'll run it on current thread when we finish loading. 215 // we'll run it on current thread when we finish loading.
217 pending_callbacks_.push_back( 216 pending_callbacks_.push_back(base::Bind(
218 base::Bind(&RunOrPostGetMostVisitedURLsCallback, 217 &RunOrPostGetMostVisitedURLsCallback,
219 base::MessageLoopProxy::current(), 218 base::MessageLoopProxy::current(), include_forced_urls, callback));
220 include_forced_urls,
221 callback));
222 return; 219 return;
223 } 220 }
224 if (include_forced_urls) { 221 if (include_forced_urls) {
225 filtered_urls = thread_safe_cache_->top_sites(); 222 filtered_urls = thread_safe_cache_->top_sites();
226 } else { 223 } else {
227 filtered_urls.assign(thread_safe_cache_->top_sites().begin() + 224 filtered_urls.assign(thread_safe_cache_->top_sites().begin() +
228 thread_safe_cache_->GetNumForcedURLs(), 225 thread_safe_cache_->GetNumForcedURLs(),
229 thread_safe_cache_->top_sites().end()); 226 thread_safe_cache_->top_sites().end());
230 } 227 }
231 } 228 }
232 callback.Run(filtered_urls); 229 callback.Run(filtered_urls);
233 } 230 }
234 231
235 bool TopSitesImpl::GetPageThumbnail( 232 bool TopSitesImpl::GetPageThumbnail(
236 const GURL& url, 233 const GURL& url,
237 bool prefix_match, 234 bool prefix_match,
238 scoped_refptr<base::RefCountedMemory>* bytes) { 235 scoped_refptr<base::RefCountedMemory>* bytes) {
239 // WARNING: this may be invoked on any thread. 236 // WARNING: this may be invoked on any thread.
240 // Perform exact match. 237 // Perform exact match.
241 { 238 {
242 base::AutoLock lock(lock_); 239 base::AutoLock lock(lock_);
243 if (thread_safe_cache_->GetPageThumbnail(url, bytes)) 240 if (thread_safe_cache_->GetPageThumbnail(url, bytes))
244 return true; 241 return true;
245 } 242 }
246 243
247 // Resource bundle is thread safe. 244 // Resource bundle is thread safe.
248 for (int i = 0; i < kPrepopulatedPagesCount; i++) { 245 for (int i = 0; i < kPrepopulatedPagesCount; i++) {
249 if (url == prepopulated_page_urls_[i]) { 246 if (url == prepopulated_page_urls_[i]) {
250 *bytes = ResourceBundle::GetSharedInstance(). 247 *bytes =
251 LoadDataResourceBytesForScale( 248 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
252 kPrepopulatedPages[i].thumbnail_id, 249 kPrepopulatedPages[i].thumbnail_id, ui::SCALE_FACTOR_100P);
253 ui::SCALE_FACTOR_100P);
254 return true; 250 return true;
255 } 251 }
256 } 252 }
257 253
258 if (prefix_match) { 254 if (prefix_match) {
259 // If http or https, search with |url| first, then try the other one. 255 // If http or https, search with |url| first, then try the other one.
260 std::vector<GURL> url_list; 256 std::vector<GURL> url_list;
261 url_list.push_back(url); 257 url_list.push_back(url);
262 if (url.SchemeIsHTTPOrHTTPS()) 258 if (url.SchemeIsHTTPOrHTTPS())
263 url_list.push_back(ToggleHTTPAndHTTPS(url)); 259 url_list.push_back(ToggleHTTPAndHTTPS(url));
(...skipping 27 matching lines...) Expand all
291 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end(); 287 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end();
292 ++i) { 288 ++i) {
293 if (i->first == url) { 289 if (i->first == url) {
294 *score = i->second.thumbnail_score; 290 *score = i->second.thumbnail_score;
295 return true; 291 return true;
296 } 292 }
297 } 293 }
298 return false; 294 return false;
299 } 295 }
300 296
301
302 // Returns the index of |url| in |urls|, or -1 if not found. 297 // Returns the index of |url| in |urls|, or -1 if not found.
303 static int IndexOf(const MostVisitedURLList& urls, const GURL& url) { 298 static int IndexOf(const MostVisitedURLList& urls, const GURL& url) {
304 for (size_t i = 0; i < urls.size(); i++) { 299 for (size_t i = 0; i < urls.size(); i++) {
305 if (urls[i].url == url) 300 if (urls[i].url == url)
306 return i; 301 return i;
307 } 302 }
308 return -1; 303 return -1;
309 } 304 }
310 305
311 void TopSitesImpl::SyncWithHistory() { 306 void TopSitesImpl::SyncWithHistory() {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 // invoked Shutdown (this could happen if we have a pending request and 374 // invoked Shutdown (this could happen if we have a pending request and
380 // Shutdown is invoked). 375 // Shutdown is invoked).
381 cancelable_task_tracker_.TryCancelAll(); 376 cancelable_task_tracker_.TryCancelAll();
382 backend_->Shutdown(); 377 backend_->Shutdown();
383 } 378 }
384 379
385 // static 380 // static
386 void TopSitesImpl::DiffMostVisited(const MostVisitedURLList& old_list, 381 void TopSitesImpl::DiffMostVisited(const MostVisitedURLList& old_list,
387 const MostVisitedURLList& new_list, 382 const MostVisitedURLList& new_list,
388 TopSitesDelta* delta) { 383 TopSitesDelta* delta) {
389
390 // Add all the old URLs for quick lookup. This maps URLs to the corresponding 384 // Add all the old URLs for quick lookup. This maps URLs to the corresponding
391 // index in the input. 385 // index in the input.
392 std::map<GURL, size_t> all_old_urls; 386 std::map<GURL, size_t> all_old_urls;
393 size_t num_old_forced = 0; 387 size_t num_old_forced = 0;
394 for (size_t i = 0; i < old_list.size(); i++) { 388 for (size_t i = 0; i < old_list.size(); i++) {
395 if (!old_list[i].last_forced_time.is_null()) 389 if (!old_list[i].last_forced_time.is_null())
396 num_old_forced++; 390 num_old_forced++;
397 DCHECK(old_list[i].last_forced_time.is_null() || i < num_old_forced) 391 DCHECK(old_list[i].last_forced_time.is_null() || i < num_old_forced)
398 << "Forced URLs must all appear before non-forced URLs."; 392 << "Forced URLs must all appear before non-forced URLs.";
399 all_old_urls[old_list[i].url] = i; 393 all_old_urls[old_list[i].url] = i;
(...skipping 14 matching lines...) Expand all
414 << "Forced URLs must all appear before non-forced URLs."; 408 << "Forced URLs must all appear before non-forced URLs.";
415 std::map<GURL, size_t>::iterator found = all_old_urls.find(new_list[i].url); 409 std::map<GURL, size_t>::iterator found = all_old_urls.find(new_list[i].url);
416 if (found == all_old_urls.end()) { 410 if (found == all_old_urls.end()) {
417 MostVisitedURLWithRank added; 411 MostVisitedURLWithRank added;
418 added.url = new_list[i]; 412 added.url = new_list[i];
419 added.rank = rank; 413 added.rank = rank;
420 delta->added.push_back(added); 414 delta->added.push_back(added);
421 } else { 415 } else {
422 DCHECK(found->second != kAlreadyFoundMarker) 416 DCHECK(found->second != kAlreadyFoundMarker)
423 << "Same URL appears twice in the new list."; 417 << "Same URL appears twice in the new list.";
424 int old_rank = found->second >= num_old_forced ? 418 int old_rank =
425 found->second - num_old_forced : -1; 419 found->second >= num_old_forced ? found->second - num_old_forced : -1;
426 if (old_rank != rank || 420 if (old_rank != rank ||
427 old_list[found->second].last_forced_time != 421 old_list[found->second].last_forced_time !=
428 new_list[i].last_forced_time) { 422 new_list[i].last_forced_time) {
429 MostVisitedURLWithRank moved; 423 MostVisitedURLWithRank moved;
430 moved.url = new_list[i]; 424 moved.url = new_list[i];
431 moved.rank = rank; 425 moved.rank = rank;
432 delta->moved.push_back(moved); 426 delta->moved.push_back(moved);
433 } 427 }
434 found->second = kAlreadyFoundMarker; 428 found->second = kAlreadyFoundMarker;
435 } 429 }
436 } 430 }
437 431
438 // Any member without the special marker in the all_old_urls list means that 432 // Any member without the special marker in the all_old_urls list means that
439 // there wasn't a "new" URL that mapped to it, so it was deleted. 433 // there wasn't a "new" URL that mapped to it, so it was deleted.
440 for (std::map<GURL, size_t>::const_iterator i = all_old_urls.begin(); 434 for (std::map<GURL, size_t>::const_iterator i = all_old_urls.begin();
441 i != all_old_urls.end(); ++i) { 435 i != all_old_urls.end(); ++i) {
442 if (i->second != kAlreadyFoundMarker) 436 if (i->second != kAlreadyFoundMarker)
443 delta->deleted.push_back(old_list[i->second]); 437 delta->deleted.push_back(old_list[i->second]);
444 } 438 }
445 } 439 }
446 440
447 base::CancelableTaskTracker::TaskId TopSitesImpl::StartQueryForMostVisited() { 441 base::CancelableTaskTracker::TaskId TopSitesImpl::StartQueryForMostVisited() {
448 DCHECK(loaded_); 442 DCHECK(loaded_);
449 if (!profile_) 443 if (!profile_)
450 return base::CancelableTaskTracker::kBadTaskId; 444 return base::CancelableTaskTracker::kBadTaskId;
451 445
452 HistoryService* hs = HistoryServiceFactory::GetForProfile( 446 HistoryService* hs =
453 profile_, Profile::EXPLICIT_ACCESS); 447 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
sdefresne 2015/01/12 17:24:24 nit: Profile::EXPLICIT access has been removed wit
454 // |hs| may be null during unit tests. 448 // |hs| may be null during unit tests.
455 if (hs) { 449 if (hs) {
456 return hs->QueryMostVisitedURLs( 450 return hs->QueryMostVisitedURLs(
457 num_results_to_request_from_history(), 451 num_results_to_request_from_history(), kDaysOfHistory,
458 kDaysOfHistory,
459 base::Bind(&TopSitesImpl::OnTopSitesAvailableFromHistory, 452 base::Bind(&TopSitesImpl::OnTopSitesAvailableFromHistory,
460 base::Unretained(this)), 453 base::Unretained(this)),
461 &cancelable_task_tracker_); 454 &cancelable_task_tracker_);
462 } 455 }
463 return base::CancelableTaskTracker::kBadTaskId; 456 return base::CancelableTaskTracker::kBadTaskId;
464 } 457 }
465 458
466 bool TopSitesImpl::IsKnownURL(const GURL& url) { 459 bool TopSitesImpl::IsKnownURL(const GURL& url) {
467 return loaded_ && cache_->IsKnownURL(url); 460 return loaded_ && cache_->IsKnownURL(url);
468 } 461 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 if (!SetPageThumbnailNoDB(url, thumbnail, score)) 512 if (!SetPageThumbnailNoDB(url, thumbnail, score))
520 return false; 513 return false;
521 514
522 // Update the database. 515 // Update the database.
523 if (!cache_->IsKnownURL(url)) 516 if (!cache_->IsKnownURL(url))
524 return false; 517 return false;
525 518
526 size_t index = cache_->GetURLIndex(url); 519 size_t index = cache_->GetURLIndex(url);
527 int url_rank = index - cache_->GetNumForcedURLs(); 520 int url_rank = index - cache_->GetNumForcedURLs();
528 const MostVisitedURL& most_visited = cache_->top_sites()[index]; 521 const MostVisitedURL& most_visited = cache_->top_sites()[index];
529 backend_->SetPageThumbnail(most_visited, 522 backend_->SetPageThumbnail(most_visited, url_rank < 0 ? -1 : url_rank,
530 url_rank < 0 ? -1 : url_rank,
531 *(cache_->GetImage(most_visited.url))); 523 *(cache_->GetImage(most_visited.url)));
532 return true; 524 return true;
533 } 525 }
534 526
535 // static 527 // static
536 bool TopSitesImpl::EncodeBitmap(const gfx::Image& bitmap, 528 bool TopSitesImpl::EncodeBitmap(const gfx::Image& bitmap,
537 scoped_refptr<base::RefCountedBytes>* bytes) { 529 scoped_refptr<base::RefCountedBytes>* bytes) {
538 if (bitmap.IsEmpty()) 530 if (bitmap.IsEmpty())
539 return false; 531 return false;
540 *bytes = new base::RefCountedBytes(); 532 *bytes = new base::RefCountedBytes();
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 // 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
631 // clock is fiddled with. 623 // clock is fiddled with.
632 MostVisitedURLList::iterator mid = new_list.begin() + num_forced; 624 MostVisitedURLList::iterator mid = new_list.begin() + num_forced;
633 new_list.insert(mid, new_url); 625 new_list.insert(mid, new_url);
634 mid = new_list.begin() + num_forced; // Mid was invalidated. 626 mid = new_list.begin() + num_forced; // Mid was invalidated.
635 std::inplace_merge(new_list.begin(), mid, mid + 1, ForcedURLComparator); 627 std::inplace_merge(new_list.begin(), mid, mid + 1, ForcedURLComparator);
636 SetTopSites(new_list); 628 SetTopSites(new_list);
637 return true; 629 return true;
638 } 630 }
639 631
632 void TopSitesImpl::ShutdownOnUIThread() {
633 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
sdefresne 2015/01/12 17:24:24 nit: can you removed this DCHECK? This is a guaran
634 BrowserThread::CurrentlyOn(BrowserThread::UI));
635 registrar_.RemoveAll();
636 }
637
640 bool TopSitesImpl::AddPrepopulatedPages(MostVisitedURLList* urls, 638 bool TopSitesImpl::AddPrepopulatedPages(MostVisitedURLList* urls,
641 size_t num_forced_urls) { 639 size_t num_forced_urls) {
642 bool added = false; 640 bool added = false;
643 MostVisitedURLList prepopulate_urls = GetPrepopulatePages(); 641 MostVisitedURLList prepopulate_urls = GetPrepopulatePages();
644 for (size_t i = 0; i < prepopulate_urls.size(); ++i) { 642 for (size_t i = 0; i < prepopulate_urls.size(); ++i) {
645 if (urls->size() - num_forced_urls < kNonForcedTopSitesNumber && 643 if (urls->size() - num_forced_urls < kNonForcedTopSitesNumber &&
646 IndexOf(*urls, prepopulate_urls[i].url) == -1) { 644 IndexOf(*urls, prepopulate_urls[i].url) == -1) {
647 urls->push_back(prepopulate_urls[i]); 645 urls->push_back(prepopulate_urls[i]);
648 added = true; 646 added = true;
649 } 647 }
(...skipping 19 matching lines...) Expand all
669 MostVisitedURLList filtered_forced_urls; 667 MostVisitedURLList filtered_forced_urls;
670 for (size_t i = 0; i < cache_->GetNumForcedURLs(); ++i) { 668 for (size_t i = 0; i < cache_->GetNumForcedURLs(); ++i) {
671 if (all_new_urls.find(cache_->top_sites()[i].url) == all_new_urls.end()) 669 if (all_new_urls.find(cache_->top_sites()[i].url) == all_new_urls.end())
672 filtered_forced_urls.push_back(cache_->top_sites()[i]); 670 filtered_forced_urls.push_back(cache_->top_sites()[i]);
673 } 671 }
674 num_forced += filtered_forced_urls.size(); 672 num_forced += filtered_forced_urls.size();
675 673
676 // Prepend forced URLs and sort in order of ascending |last_forced_time|. 674 // Prepend forced URLs and sort in order of ascending |last_forced_time|.
677 new_list->insert(new_list->begin(), filtered_forced_urls.begin(), 675 new_list->insert(new_list->begin(), filtered_forced_urls.begin(),
678 filtered_forced_urls.end()); 676 filtered_forced_urls.end());
679 std::inplace_merge( 677 std::inplace_merge(new_list->begin(),
680 new_list->begin(), new_list->begin() + filtered_forced_urls.size(), 678 new_list->begin() + filtered_forced_urls.size(),
681 new_list->begin() + num_forced, ForcedURLComparator); 679 new_list->begin() + num_forced, ForcedURLComparator);
682 680
683 // Drop older forced URLs if the list overflows. Since forced URLs are always 681 // Drop older forced URLs if the list overflows. Since forced URLs are always
684 // sort in increasing order of |last_forced_time|, drop the first ones. 682 // sort in increasing order of |last_forced_time|, drop the first ones.
685 if (num_forced > kForcedTopSitesNumber) { 683 if (num_forced > kForcedTopSitesNumber) {
686 new_list->erase(new_list->begin(), 684 new_list->erase(new_list->begin(),
687 new_list->begin() + (num_forced - kForcedTopSitesNumber)); 685 new_list->begin() + (num_forced - kForcedTopSitesNumber));
688 num_forced = kForcedTopSitesNumber; 686 num_forced = kForcedTopSitesNumber;
689 } 687 }
690 688
691 return num_forced; 689 return num_forced;
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 // See if we have any tmp thumbnails for the new sites. 807 // See if we have any tmp thumbnails for the new sites.
810 if (!temp_images_.empty()) { 808 if (!temp_images_.empty()) {
811 for (size_t i = 0; i < top_sites.size(); ++i) { 809 for (size_t i = 0; i < top_sites.size(); ++i) {
812 const MostVisitedURL& mv = top_sites[i]; 810 const MostVisitedURL& mv = top_sites[i];
813 GURL canonical_url = cache_->GetCanonicalURL(mv.url); 811 GURL canonical_url = cache_->GetCanonicalURL(mv.url);
814 // At the time we get the thumbnail redirects aren't known, so we have to 812 // At the time we get the thumbnail redirects aren't known, so we have to
815 // iterate through all the images. 813 // iterate through all the images.
816 for (TempImages::iterator it = temp_images_.begin(); 814 for (TempImages::iterator it = temp_images_.begin();
817 it != temp_images_.end(); ++it) { 815 it != temp_images_.end(); ++it) {
818 if (canonical_url == cache_->GetCanonicalURL(it->first)) { 816 if (canonical_url == cache_->GetCanonicalURL(it->first)) {
819 SetPageThumbnailEncoded( 817 SetPageThumbnailEncoded(mv.url, it->second.thumbnail.get(),
820 mv.url, it->second.thumbnail.get(), it->second.thumbnail_score); 818 it->second.thumbnail_score);
821 temp_images_.erase(it); 819 temp_images_.erase(it);
822 break; 820 break;
823 } 821 }
824 } 822 }
825 } 823 }
826 } 824 }
827 825
828 if (top_sites.size() - num_forced_urls >= kNonForcedTopSitesNumber) 826 if (top_sites.size() - num_forced_urls >= kNonForcedTopSitesNumber)
829 temp_images_.clear(); 827 temp_images_.clear();
830 828
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
919 base::TimeDelta::FromSeconds(kUpdateIntervalSecs)); 917 base::TimeDelta::FromSeconds(kUpdateIntervalSecs));
920 } 918 }
921 919
922 void TopSitesImpl::OnTopSitesAvailableFromHistory( 920 void TopSitesImpl::OnTopSitesAvailableFromHistory(
923 const MostVisitedURLList* pages) { 921 const MostVisitedURLList* pages) {
924 DCHECK(pages); 922 DCHECK(pages);
925 SetTopSites(*pages); 923 SetTopSites(*pages);
926 } 924 }
927 925
928 } // namespace history 926 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698