| 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 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 RemovePinnedURL(url); | 363 RemovePinnedURL(url); |
| 364 Value* dummy = Value::CreateNullValue(); | 364 Value* dummy = Value::CreateNullValue(); |
| 365 { | 365 { |
| 366 DictionaryPrefUpdate update(profile_->GetPrefs(), | 366 DictionaryPrefUpdate update(profile_->GetPrefs(), |
| 367 prefs::kNTPMostVisitedURLsBlacklist); | 367 prefs::kNTPMostVisitedURLsBlacklist); |
| 368 DictionaryValue* blacklist = update.Get(); | 368 DictionaryValue* blacklist = update.Get(); |
| 369 blacklist->SetWithoutPathExpansion(GetURLHash(url), dummy); | 369 blacklist->SetWithoutPathExpansion(GetURLHash(url), dummy); |
| 370 } | 370 } |
| 371 | 371 |
| 372 ResetThreadSafeCache(); | 372 ResetThreadSafeCache(); |
| 373 NotifyTopSitesChanged(); |
| 373 } | 374 } |
| 374 | 375 |
| 375 void TopSites::RemoveBlacklistedURL(const GURL& url) { | 376 void TopSites::RemoveBlacklistedURL(const GURL& url) { |
| 376 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 377 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 377 { | 378 { |
| 378 DictionaryPrefUpdate update(profile_->GetPrefs(), | 379 DictionaryPrefUpdate update(profile_->GetPrefs(), |
| 379 prefs::kNTPMostVisitedURLsBlacklist); | 380 prefs::kNTPMostVisitedURLsBlacklist); |
| 380 DictionaryValue* blacklist = update.Get(); | 381 DictionaryValue* blacklist = update.Get(); |
| 381 blacklist->RemoveWithoutPathExpansion(GetURLHash(url), NULL); | 382 blacklist->RemoveWithoutPathExpansion(GetURLHash(url), NULL); |
| 382 } | 383 } |
| 383 ResetThreadSafeCache(); | 384 ResetThreadSafeCache(); |
| 385 NotifyTopSitesChanged(); |
| 384 } | 386 } |
| 385 | 387 |
| 386 bool TopSites::IsBlacklisted(const GURL& url) { | 388 bool TopSites::IsBlacklisted(const GURL& url) { |
| 387 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 388 return blacklist_->HasKey(GetURLHash(url)); | 390 return blacklist_->HasKey(GetURLHash(url)); |
| 389 } | 391 } |
| 390 | 392 |
| 391 void TopSites::ClearBlacklistedURLs() { | 393 void TopSites::ClearBlacklistedURLs() { |
| 392 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 394 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 393 { | 395 { |
| 394 DictionaryPrefUpdate update(profile_->GetPrefs(), | 396 DictionaryPrefUpdate update(profile_->GetPrefs(), |
| 395 prefs::kNTPMostVisitedURLsBlacklist); | 397 prefs::kNTPMostVisitedURLsBlacklist); |
| 396 DictionaryValue* blacklist = update.Get(); | 398 DictionaryValue* blacklist = update.Get(); |
| 397 blacklist->Clear(); | 399 blacklist->Clear(); |
| 398 } | 400 } |
| 399 ResetThreadSafeCache(); | 401 ResetThreadSafeCache(); |
| 402 NotifyTopSitesChanged(); |
| 400 } | 403 } |
| 401 | 404 |
| 402 void TopSites::AddPinnedURL(const GURL& url, size_t pinned_index) { | 405 void TopSites::AddPinnedURL(const GURL& url, size_t pinned_index) { |
| 403 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 406 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 404 | 407 |
| 405 GURL old; | 408 GURL old; |
| 406 if (GetPinnedURLAtIndex(pinned_index, &old)) | 409 if (GetPinnedURLAtIndex(pinned_index, &old)) |
| 407 RemovePinnedURL(old); | 410 RemovePinnedURL(old); |
| 408 | 411 |
| 409 if (IsURLPinned(url)) | 412 if (IsURLPinned(url)) |
| 410 RemovePinnedURL(url); | 413 RemovePinnedURL(url); |
| 411 | 414 |
| 412 Value* index = Value::CreateIntegerValue(pinned_index); | 415 Value* index = Value::CreateIntegerValue(pinned_index); |
| 413 | 416 |
| 414 { | 417 { |
| 415 DictionaryPrefUpdate update(profile_->GetPrefs(), | 418 DictionaryPrefUpdate update(profile_->GetPrefs(), |
| 416 prefs::kNTPMostVisitedPinnedURLs); | 419 prefs::kNTPMostVisitedPinnedURLs); |
| 417 DictionaryValue* pinned_urls = update.Get(); | 420 DictionaryValue* pinned_urls = update.Get(); |
| 418 pinned_urls->SetWithoutPathExpansion(GetURLString(url), index); | 421 pinned_urls->SetWithoutPathExpansion(GetURLString(url), index); |
| 419 } | 422 } |
| 420 | 423 |
| 421 ResetThreadSafeCache(); | 424 ResetThreadSafeCache(); |
| 425 NotifyTopSitesChanged(); |
| 422 } | 426 } |
| 423 | 427 |
| 424 bool TopSites::IsURLPinned(const GURL& url) { | 428 bool TopSites::IsURLPinned(const GURL& url) { |
| 425 int tmp; | 429 int tmp; |
| 426 return pinned_urls_->GetIntegerWithoutPathExpansion(GetURLString(url), &tmp); | 430 return pinned_urls_->GetIntegerWithoutPathExpansion(GetURLString(url), &tmp); |
| 427 } | 431 } |
| 428 | 432 |
| 429 void TopSites::RemovePinnedURL(const GURL& url) { | 433 void TopSites::RemovePinnedURL(const GURL& url) { |
| 430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 434 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 431 | 435 |
| 432 { | 436 { |
| 433 DictionaryPrefUpdate update(profile_->GetPrefs(), | 437 DictionaryPrefUpdate update(profile_->GetPrefs(), |
| 434 prefs::kNTPMostVisitedPinnedURLs); | 438 prefs::kNTPMostVisitedPinnedURLs); |
| 435 DictionaryValue* pinned_urls = update.Get(); | 439 DictionaryValue* pinned_urls = update.Get(); |
| 436 pinned_urls->RemoveWithoutPathExpansion(GetURLString(url), NULL); | 440 pinned_urls->RemoveWithoutPathExpansion(GetURLString(url), NULL); |
| 437 } | 441 } |
| 438 | 442 |
| 439 ResetThreadSafeCache(); | 443 ResetThreadSafeCache(); |
| 444 NotifyTopSitesChanged(); |
| 440 } | 445 } |
| 441 | 446 |
| 442 bool TopSites::GetPinnedURLAtIndex(size_t index, GURL* url) { | 447 bool TopSites::GetPinnedURLAtIndex(size_t index, GURL* url) { |
| 443 for (DictionaryValue::key_iterator it = pinned_urls_->begin_keys(); | 448 for (DictionaryValue::key_iterator it = pinned_urls_->begin_keys(); |
| 444 it != pinned_urls_->end_keys(); ++it) { | 449 it != pinned_urls_->end_keys(); ++it) { |
| 445 int current_index; | 450 int current_index; |
| 446 if (pinned_urls_->GetIntegerWithoutPathExpansion(*it, ¤t_index)) { | 451 if (pinned_urls_->GetIntegerWithoutPathExpansion(*it, ¤t_index)) { |
| 447 if (static_cast<size_t>(current_index) == index) { | 452 if (static_cast<size_t>(current_index) == index) { |
| 448 *url = GURL(*it); | 453 *url = GURL(*it); |
| 449 return true; | 454 return true; |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 825 } | 830 } |
| 826 } | 831 } |
| 827 } | 832 } |
| 828 | 833 |
| 829 void TopSites::SetTopSites(const MostVisitedURLList& new_top_sites) { | 834 void TopSites::SetTopSites(const MostVisitedURLList& new_top_sites) { |
| 830 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 835 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 831 | 836 |
| 832 MostVisitedURLList top_sites(new_top_sites); | 837 MostVisitedURLList top_sites(new_top_sites); |
| 833 AddPrepopulatedPages(&top_sites); | 838 AddPrepopulatedPages(&top_sites); |
| 834 | 839 |
| 835 bool changed = false; | |
| 836 TopSitesDelta delta; | 840 TopSitesDelta delta; |
| 837 DiffMostVisited(cache_->top_sites(), top_sites, &delta); | 841 DiffMostVisited(cache_->top_sites(), top_sites, &delta); |
| 838 if (!delta.deleted.empty() || !delta.added.empty() || !delta.moved.empty()) { | 842 if (!delta.deleted.empty() || !delta.added.empty() || !delta.moved.empty()) { |
| 839 changed = true; | |
| 840 backend_->UpdateTopSites(delta); | 843 backend_->UpdateTopSites(delta); |
| 841 } | 844 } |
| 842 | 845 |
| 843 last_num_urls_changed_ = delta.added.size() + delta.moved.size(); | 846 last_num_urls_changed_ = delta.added.size() + delta.moved.size(); |
| 844 | 847 |
| 845 // We always do the following steps (setting top sites in cache, and resetting | 848 // We always do the following steps (setting top sites in cache, and resetting |
| 846 // thread safe cache ...) as this method is invoked during startup at which | 849 // thread safe cache ...) as this method is invoked during startup at which |
| 847 // point the caches haven't been updated yet. | 850 // point the caches haven't been updated yet. |
| 848 cache_->SetTopSites(top_sites); | 851 cache_->SetTopSites(top_sites); |
| 849 | 852 |
| 850 // See if we have any tmp thumbnails for the new sites. | 853 // See if we have any tmp thumbnails for the new sites. |
| 851 if (!temp_images_.empty()) { | 854 if (!temp_images_.empty()) { |
| 852 for (size_t i = 0; i < top_sites.size(); ++i) { | 855 for (size_t i = 0; i < top_sites.size(); ++i) { |
| 853 const MostVisitedURL& mv = top_sites[i]; | 856 const MostVisitedURL& mv = top_sites[i]; |
| 854 GURL canonical_url = cache_->GetCanonicalURL(mv.url); | 857 GURL canonical_url = cache_->GetCanonicalURL(mv.url); |
| 855 // At the time we get the thumbnail redirects aren't known, so we have to | 858 // At the time we get the thumbnail redirects aren't known, so we have to |
| 856 // iterate through all the images. | 859 // iterate through all the images. |
| 857 for (TempImages::iterator it = temp_images_.begin(); | 860 for (TempImages::iterator it = temp_images_.begin(); |
| 858 it != temp_images_.end(); ++it) { | 861 it != temp_images_.end(); ++it) { |
| 859 if (canonical_url == cache_->GetCanonicalURL(it->first)) { | 862 if (canonical_url == cache_->GetCanonicalURL(it->first)) { |
| 860 SetPageThumbnailEncoded(mv.url, | 863 SetPageThumbnailEncoded(mv.url, |
| 861 it->second.thumbnail, | 864 it->second.thumbnail, |
| 862 it->second.thumbnail_score); | 865 it->second.thumbnail_score); |
| 863 changed = true; | |
| 864 temp_images_.erase(it); | 866 temp_images_.erase(it); |
| 865 break; | 867 break; |
| 866 } | 868 } |
| 867 } | 869 } |
| 868 } | 870 } |
| 869 } | 871 } |
| 870 | 872 |
| 871 if (top_sites.size() >= kTopSitesNumber) | 873 if (top_sites.size() >= kTopSitesNumber) |
| 872 temp_images_.clear(); | 874 temp_images_.clear(); |
| 873 | 875 |
| 874 ResetThreadSafeCache(); | 876 ResetThreadSafeCache(); |
| 875 ResetThreadSafeImageCache(); | 877 ResetThreadSafeImageCache(); |
| 876 | 878 NotifyTopSitesChanged(); |
| 877 if (changed) { | |
| 878 NotificationService::current()->Notify( | |
| 879 NotificationType::TOP_SITES_CHANGED, | |
| 880 Source<TopSites>(this), | |
| 881 NotificationService::NoDetails()); | |
| 882 } | |
| 883 | 879 |
| 884 // Restart the timer that queries history for top sites. This is done to | 880 // Restart the timer that queries history for top sites. This is done to |
| 885 // ensure we stay in sync with history. | 881 // ensure we stay in sync with history. |
| 886 RestartQueryForTopSitesTimer(GetUpdateDelay()); | 882 RestartQueryForTopSitesTimer(GetUpdateDelay()); |
| 887 } | 883 } |
| 888 | 884 |
| 889 int TopSites::num_results_to_request_from_history() const { | 885 int TopSites::num_results_to_request_from_history() const { |
| 890 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 886 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 891 | 887 |
| 892 return kTopSitesNumber + blacklist_->size(); | 888 return kTopSitesNumber + blacklist_->size(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 925 ApplyBlacklistAndPinnedURLs(cache_->top_sites(), &cached); | 921 ApplyBlacklistAndPinnedURLs(cache_->top_sites(), &cached); |
| 926 thread_safe_cache_->SetTopSites(cached); | 922 thread_safe_cache_->SetTopSites(cached); |
| 927 } | 923 } |
| 928 | 924 |
| 929 void TopSites::ResetThreadSafeImageCache() { | 925 void TopSites::ResetThreadSafeImageCache() { |
| 930 base::AutoLock lock(lock_); | 926 base::AutoLock lock(lock_); |
| 931 thread_safe_cache_->SetThumbnails(cache_->images()); | 927 thread_safe_cache_->SetThumbnails(cache_->images()); |
| 932 thread_safe_cache_->RemoveUnreferencedThumbnails(); | 928 thread_safe_cache_->RemoveUnreferencedThumbnails(); |
| 933 } | 929 } |
| 934 | 930 |
| 931 void TopSites::NotifyTopSitesChanged() { |
| 932 NotificationService::current()->Notify( |
| 933 NotificationType::TOP_SITES_CHANGED, |
| 934 Source<TopSites>(this), |
| 935 NotificationService::NoDetails()); |
| 936 } |
| 937 |
| 935 void TopSites::RestartQueryForTopSitesTimer(base::TimeDelta delta) { | 938 void TopSites::RestartQueryForTopSitesTimer(base::TimeDelta delta) { |
| 936 if (timer_.IsRunning() && ((timer_start_time_ + timer_.GetCurrentDelay()) < | 939 if (timer_.IsRunning() && ((timer_start_time_ + timer_.GetCurrentDelay()) < |
| 937 (base::TimeTicks::Now() + delta))) { | 940 (base::TimeTicks::Now() + delta))) { |
| 938 return; | 941 return; |
| 939 } | 942 } |
| 940 | 943 |
| 941 timer_start_time_ = base::TimeTicks::Now(); | 944 timer_start_time_ = base::TimeTicks::Now(); |
| 942 timer_.Stop(); | 945 timer_.Stop(); |
| 943 timer_.Start(delta, this, &TopSites::TimerFired); | 946 timer_.Start(delta, this, &TopSites::TimerFired); |
| 944 } | 947 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1000 SetTopSites(pages); | 1003 SetTopSites(pages); |
| 1001 | 1004 |
| 1002 // Used only in testing. | 1005 // Used only in testing. |
| 1003 NotificationService::current()->Notify( | 1006 NotificationService::current()->Notify( |
| 1004 NotificationType::TOP_SITES_UPDATED, | 1007 NotificationType::TOP_SITES_UPDATED, |
| 1005 Source<TopSites>(this), | 1008 Source<TopSites>(this), |
| 1006 Details<CancelableRequestProvider::Handle>(&handle)); | 1009 Details<CancelableRequestProvider::Handle>(&handle)); |
| 1007 } | 1010 } |
| 1008 | 1011 |
| 1009 } // namespace history | 1012 } // namespace history |
| OLD | NEW |