Chromium Code Reviews| OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/l10n_util.h" | 10 #include "app/l10n_util.h" | 
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 TopSites::TopSites(Profile* profile) | 129 TopSites::TopSites(Profile* profile) | 
| 130 : backend_(new TopSitesBackend()), | 130 : backend_(new TopSitesBackend()), | 
| 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 handle_to_wait_for_(NULL) { | |
| 140 if (!profile_) | 141 if (!profile_) | 
| 141 return; | 142 return; | 
| 142 | 143 | 
| 143 if (NotificationService::current()) { | 144 if (NotificationService::current()) { | 
| 144 registrar_.Add(this, NotificationType::HISTORY_URLS_DELETED, | 145 registrar_.Add(this, NotificationType::HISTORY_URLS_DELETED, | 
| 145 Source<Profile>(profile_)); | 146 Source<Profile>(profile_)); | 
| 146 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 147 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 
| 147 NotificationService::AllSources()); | 148 NotificationService::AllSources()); | 
| 148 } | 149 } | 
| 149 | 150 | 
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 433 | 434 | 
| 434 // Any member without the special marker in the all_old_urls list means that | 435 // Any member without the special marker in the all_old_urls list means that | 
| 435 // there wasn't a "new" URL that mapped to it, so it was deleted. | 436 // there wasn't a "new" URL that mapped to it, so it was deleted. | 
| 436 for (std::map<GURL, size_t>::const_iterator i = all_old_urls.begin(); | 437 for (std::map<GURL, size_t>::const_iterator i = all_old_urls.begin(); | 
| 437 i != all_old_urls.end(); ++i) { | 438 i != all_old_urls.end(); ++i) { | 
| 438 if (i->second != kAlreadyFoundMarker) | 439 if (i->second != kAlreadyFoundMarker) | 
| 439 delta->deleted.push_back(old_list[i->second]); | 440 delta->deleted.push_back(old_list[i->second]); | 
| 440 } | 441 } | 
| 441 } | 442 } | 
| 442 | 443 | 
| 444 void TopSites::RefreshAndCallback(Callback0::Type* callback) { | |
| 
 
sky
2010/11/16 21:43:08
Instead of exposing this method, maintaining handl
 
kkania
2010/11/17 01:11:34
Done.
 
 | |
| 445 if (refresh_callback_.get()) { | |
| 446 DLOG(ERROR) << "Waiting for refresh before previous refresh finished"; | |
| 447 return; | |
| 448 } | |
| 449 refresh_callback_.reset(callback); | |
| 450 handle_to_wait_for_ = StartQueryForMostVisited(); | |
| 451 } | |
| 452 | |
| 443 TopSites::~TopSites() { | 453 TopSites::~TopSites() { | 
| 444 } | 454 } | 
| 445 | 455 | 
| 446 bool TopSites::SetPageThumbnailNoDB(const GURL& url, | 456 bool TopSites::SetPageThumbnailNoDB(const GURL& url, | 
| 447 const RefCountedBytes* thumbnail_data, | 457 const RefCountedBytes* thumbnail_data, | 
| 448 const ThumbnailScore& score) { | 458 const ThumbnailScore& score) { | 
| 449 // This should only be invoked when we know about the url. | 459 // This should only be invoked when we know about the url. | 
| 450 DCHECK(cache_->IsKnownURL(url)); | 460 DCHECK(cache_->IsKnownURL(url)); | 
| 451 | 461 | 
| 452 const MostVisitedURL& most_visited = | 462 const MostVisitedURL& most_visited = | 
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 526 if (temp_images_.size() == kMaxTempTopImages) | 536 if (temp_images_.size() == kMaxTempTopImages) | 
| 527 temp_images_.erase(temp_images_.begin()); | 537 temp_images_.erase(temp_images_.begin()); | 
| 528 | 538 | 
| 529 TempImage image; | 539 TempImage image; | 
| 530 image.first = url; | 540 image.first = url; | 
| 531 image.second.thumbnail = const_cast<RefCountedBytes*>(thumbnail); | 541 image.second.thumbnail = const_cast<RefCountedBytes*>(thumbnail); | 
| 532 image.second.thumbnail_score = score; | 542 image.second.thumbnail_score = score; | 
| 533 temp_images_.push_back(image); | 543 temp_images_.push_back(image); | 
| 534 } | 544 } | 
| 535 | 545 | 
| 536 void TopSites::StartQueryForMostVisited() { | 546 CancelableRequestProvider::Handle TopSites::StartQueryForMostVisited() { | 
| 537 if (!profile_) | 547 if (!profile_) | 
| 538 return; | 548 return NULL; | 
| 539 | 549 | 
| 550 CancelableRequestProvider::Handle request_handle = NULL; | |
| 540 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 551 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 
| 541 // |hs| may be null during unit tests. | 552 // |hs| may be null during unit tests. | 
| 542 if (hs) { | 553 if (hs) { | 
| 543 hs->QueryMostVisitedURLs( | 554 request_handle = hs->QueryMostVisitedURLs( | 
| 544 num_results_to_request_from_history(), | 555 num_results_to_request_from_history(), | 
| 545 kDaysOfHistory, | 556 kDaysOfHistory, | 
| 546 &cancelable_consumer_, | 557 &cancelable_consumer_, | 
| 547 NewCallback(this, &TopSites::OnTopSitesAvailableFromHistory)); | 558 NewCallback(this, &TopSites::OnTopSitesAvailableFromHistory)); | 
| 548 } | 559 } | 
| 560 return request_handle; | |
| 561 } | |
| 562 | |
| 563 void TopSites::TimerFired() { | |
| 564 StartQueryForMostVisited(); | |
| 
 
sky
2010/11/16 21:43:08
Why do we need TimerFired?
 
kkania
2010/11/17 01:11:34
OneShotTimer expects a void method.
 
 | |
| 549 } | 565 } | 
| 550 | 566 | 
| 551 // static | 567 // static | 
| 552 int TopSites::GetRedirectDistanceForURL(const MostVisitedURL& most_visited, | 568 int TopSites::GetRedirectDistanceForURL(const MostVisitedURL& most_visited, | 
| 553 const GURL& url) { | 569 const GURL& url) { | 
| 554 for (size_t i = 0; i < most_visited.redirects.size(); i++) { | 570 for (size_t i = 0; i < most_visited.redirects.size(); i++) { | 
| 555 if (most_visited.redirects[i] == url) | 571 if (most_visited.redirects[i] == url) | 
| 556 return static_cast<int>(most_visited.redirects.size() - i - 1); | 572 return static_cast<int>(most_visited.redirects.size() - i - 1); | 
| 557 } | 573 } | 
| 558 NOTREACHED() << "URL should always be found."; | 574 NOTREACHED() << "URL should always be found."; | 
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 837 } | 853 } | 
| 838 | 854 | 
| 839 void TopSites::RestartQueryForTopSitesTimer(base::TimeDelta delta) { | 855 void TopSites::RestartQueryForTopSitesTimer(base::TimeDelta delta) { | 
| 840 if (timer_.IsRunning() && ((timer_start_time_ + timer_.GetCurrentDelay()) < | 856 if (timer_.IsRunning() && ((timer_start_time_ + timer_.GetCurrentDelay()) < | 
| 841 (base::TimeTicks::Now() + delta))) { | 857 (base::TimeTicks::Now() + delta))) { | 
| 842 return; | 858 return; | 
| 843 } | 859 } | 
| 844 | 860 | 
| 845 timer_start_time_ = base::TimeTicks::Now(); | 861 timer_start_time_ = base::TimeTicks::Now(); | 
| 846 timer_.Stop(); | 862 timer_.Stop(); | 
| 847 timer_.Start(delta, this, &TopSites::StartQueryForMostVisited); | 863 timer_.Start(delta, this, &TopSites::TimerFired); | 
| 848 } | 864 } | 
| 849 | 865 | 
| 850 void TopSites::OnHistoryMigrationWrittenToDisk(TopSitesBackend::Handle handle) { | 866 void TopSites::OnHistoryMigrationWrittenToDisk(TopSitesBackend::Handle handle) { | 
| 851 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 867 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 852 | 868 | 
| 853 if (!profile_) | 869 if (!profile_) | 
| 854 return; | 870 return; | 
| 855 | 871 | 
| 856 HistoryService* history = | 872 HistoryService* history = | 
| 857 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 873 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 895 // finishes loading we'll do migration and/or move to loaded. | 911 // finishes loading we'll do migration and/or move to loaded. | 
| 896 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 912 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 
| 897 } | 913 } | 
| 898 } | 914 } | 
| 899 } | 915 } | 
| 900 | 916 | 
| 901 void TopSites::OnTopSitesAvailableFromHistory( | 917 void TopSites::OnTopSitesAvailableFromHistory( | 
| 902 CancelableRequestProvider::Handle handle, | 918 CancelableRequestProvider::Handle handle, | 
| 903 MostVisitedURLList pages) { | 919 MostVisitedURLList pages) { | 
| 904 SetTopSites(pages); | 920 SetTopSites(pages); | 
| 921 | |
| 922 // For testing only. Invoke the testing callback if this is the handle to the | |
| 923 // request that is being waited upon. | |
| 924 if (handle == handle_to_wait_for_) { | |
| 925 handle_to_wait_for_ = NULL; | |
| 926 // Invoke the callback after this empty request goes through the backend, to | |
| 927 // ensure that the database is updated. | |
| 928 backend_->DoEmptyRequest( | |
| 
 
sky
2010/11/16 21:43:08
Why does this need to wait until the backend has b
 
kkania
2010/11/17 01:11:34
After thinking about it again, I think this is not
 
 | |
| 929 &cancelable_consumer_, | |
| 930 NewCallback(this, &TopSites::InvokeRefreshCallback)); | |
| 931 } | |
| 932 } | |
| 933 | |
| 934 void TopSites::InvokeRefreshCallback(CancelableRequestProvider::Handle handle) { | |
| 935 if (refresh_callback_.get()) { | |
| 936 refresh_callback_->Run(); | |
| 937 refresh_callback_.reset(); | |
| 938 } else { | |
| 939 LOG(DFATAL) << "refresh callback was not set"; | |
| 940 } | |
| 905 } | 941 } | 
| 906 | 942 | 
| 907 } // namespace history | 943 } // namespace history | 
| OLD | NEW |