| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 10 #include "base/bind.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "content/public/browser/notification_service.h" | 34 #include "content/public/browser/notification_service.h" |
| 35 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
| 36 #include "grit/chromium_strings.h" | 36 #include "grit/chromium_strings.h" |
| 37 #include "grit/generated_resources.h" | 37 #include "grit/generated_resources.h" |
| 38 #include "grit/locale_settings.h" | 38 #include "grit/locale_settings.h" |
| 39 #include "grit/theme_resources.h" | 39 #include "grit/theme_resources.h" |
| 40 #include "ui/base/l10n/l10n_util.h" | 40 #include "ui/base/l10n/l10n_util.h" |
| 41 #include "ui/base/resource/resource_bundle.h" | 41 #include "ui/base/resource/resource_bundle.h" |
| 42 #include "ui/gfx/image/image_util.h" | 42 #include "ui/gfx/image/image_util.h" |
| 43 | 43 |
| 44 using base::DictionaryValue; |
| 44 using content::BrowserThread; | 45 using content::BrowserThread; |
| 45 using content::NavigationController; | 46 using content::NavigationController; |
| 46 | 47 |
| 47 namespace history { | 48 namespace history { |
| 48 | 49 |
| 49 // How many top sites to store in the cache. | 50 // How many top sites to store in the cache. |
| 50 static const size_t kTopSitesNumber = 20; | 51 static const size_t kTopSitesNumber = 20; |
| 51 | 52 |
| 52 // Max number of temporary images we'll cache. See comment above | 53 // Max number of temporary images we'll cache. See comment above |
| 53 // temp_images_ for details. | 54 // temp_images_ for details. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 }; | 134 }; |
| 134 | 135 |
| 135 } // namespace | 136 } // namespace |
| 136 | 137 |
| 137 TopSites::TopSites(Profile* profile) | 138 TopSites::TopSites(Profile* profile) |
| 138 : backend_(NULL), | 139 : backend_(NULL), |
| 139 cache_(new TopSitesCache()), | 140 cache_(new TopSitesCache()), |
| 140 thread_safe_cache_(new TopSitesCache()), | 141 thread_safe_cache_(new TopSitesCache()), |
| 141 profile_(profile), | 142 profile_(profile), |
| 142 last_num_urls_changed_(0), | 143 last_num_urls_changed_(0), |
| 143 blacklist_(NULL), | |
| 144 pinned_urls_(NULL), | 144 pinned_urls_(NULL), |
| 145 history_state_(HISTORY_LOADING), | 145 history_state_(HISTORY_LOADING), |
| 146 top_sites_state_(TOP_SITES_LOADING), | 146 top_sites_state_(TOP_SITES_LOADING), |
| 147 loaded_(false) { | 147 loaded_(false) { |
| 148 if (!profile_) | 148 if (!profile_) |
| 149 return; | 149 return; |
| 150 | 150 |
| 151 if (content::NotificationService::current()) { | 151 if (content::NotificationService::current()) { |
| 152 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 152 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
| 153 content::Source<Profile>(profile_)); | 153 content::Source<Profile>(profile_)); |
| 154 // Listen for any nav commits. We'll ignore those not related to this | 154 // Listen for any nav commits. We'll ignore those not related to this |
| 155 // profile when we get the notification. | 155 // profile when we get the notification. |
| 156 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 156 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 157 content::NotificationService::AllSources()); | 157 content::NotificationService::AllSources()); |
| 158 } | 158 } |
| 159 | 159 |
| 160 // We create update objects here to be sure that dictionaries are created | 160 // We create update objects here to be sure that dictionaries are created |
| 161 // in the user preferences. | 161 // in the user preferences. |
| 162 DictionaryPrefUpdate(profile_->GetPrefs(), | 162 DictionaryPrefUpdate(profile_->GetPrefs(), |
| 163 prefs::kNtpMostVisitedURLsBlacklist).Get(); | |
| 164 DictionaryPrefUpdate(profile_->GetPrefs(), | |
| 165 prefs::kNtpMostVisitedPinnedURLs).Get(); | 163 prefs::kNtpMostVisitedPinnedURLs).Get(); |
| 166 | 164 |
| 167 // Now the dictionaries are guaranteed to exist and we can cache pointers | 165 // Now the dictionaries are guaranteed to exist and we can cache pointers |
| 168 // to them. | 166 // to them. |
| 169 blacklist_ = | |
| 170 profile_->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); | |
| 171 pinned_urls_ = | 167 pinned_urls_ = |
| 172 profile_->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedPinnedURLs); | 168 profile_->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedPinnedURLs); |
| 173 CHECK(blacklist_ != NULL); | 169 DCHECK(pinned_urls_ != NULL); |
| 174 CHECK(pinned_urls_ != NULL); | |
| 175 } | 170 } |
| 176 | 171 |
| 177 void TopSites::Init(const FilePath& db_name) { | 172 void TopSites::Init(const FilePath& db_name) { |
| 178 // Create the backend here, rather than in the constructor, so that | 173 // Create the backend here, rather than in the constructor, so that |
| 179 // unit tests that do not need the backend can run without a problem. | 174 // unit tests that do not need the backend can run without a problem. |
| 180 backend_ = new TopSitesBackend; | 175 backend_ = new TopSitesBackend; |
| 181 backend_->Init(db_name); | 176 backend_->Init(db_name); |
| 182 backend_->GetMostVisitedThumbnails( | 177 backend_->GetMostVisitedThumbnails( |
| 183 &top_sites_consumer_, | 178 &top_sites_consumer_, |
| 184 base::Bind(&TopSites::OnGotMostVisitedThumbnails, | 179 base::Bind(&TopSites::OnGotMostVisitedThumbnails, |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 // likely the user is first running Chrome. During this time we throttle | 373 // likely the user is first running Chrome. During this time we throttle |
| 379 // updating from history by 30 seconds. If the user creates a new tab page | 374 // updating from history by 30 seconds. If the user creates a new tab page |
| 380 // during this window of time we force updating from history so that the new | 375 // during this window of time we force updating from history so that the new |
| 381 // tab page isn't so far out of date. | 376 // tab page isn't so far out of date. |
| 382 timer_.Stop(); | 377 timer_.Stop(); |
| 383 StartQueryForMostVisited(); | 378 StartQueryForMostVisited(); |
| 384 } | 379 } |
| 385 } | 380 } |
| 386 | 381 |
| 387 bool TopSites::HasBlacklistedItems() const { | 382 bool TopSites::HasBlacklistedItems() const { |
| 388 return !blacklist_->empty(); | 383 const DictionaryValue* blacklist = |
| 384 profile_->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); |
| 385 return blacklist && !blacklist->empty(); |
| 389 } | 386 } |
| 390 | 387 |
| 391 void TopSites::AddBlacklistedURL(const GURL& url) { | 388 void TopSites::AddBlacklistedURL(const GURL& url) { |
| 392 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 393 | 390 |
| 394 RemovePinnedURL(url); | 391 RemovePinnedURL(url); |
| 395 Value* dummy = Value::CreateNullValue(); | 392 Value* dummy = Value::CreateNullValue(); |
| 396 { | 393 { |
| 397 DictionaryPrefUpdate update(profile_->GetPrefs(), | 394 DictionaryPrefUpdate update(profile_->GetPrefs(), |
| 398 prefs::kNtpMostVisitedURLsBlacklist); | 395 prefs::kNtpMostVisitedURLsBlacklist); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 411 prefs::kNtpMostVisitedURLsBlacklist); | 408 prefs::kNtpMostVisitedURLsBlacklist); |
| 412 DictionaryValue* blacklist = update.Get(); | 409 DictionaryValue* blacklist = update.Get(); |
| 413 blacklist->RemoveWithoutPathExpansion(GetURLHash(url), NULL); | 410 blacklist->RemoveWithoutPathExpansion(GetURLHash(url), NULL); |
| 414 } | 411 } |
| 415 ResetThreadSafeCache(); | 412 ResetThreadSafeCache(); |
| 416 NotifyTopSitesChanged(); | 413 NotifyTopSitesChanged(); |
| 417 } | 414 } |
| 418 | 415 |
| 419 bool TopSites::IsBlacklisted(const GURL& url) { | 416 bool TopSites::IsBlacklisted(const GURL& url) { |
| 420 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 421 return blacklist_->HasKey(GetURLHash(url)); | 418 const DictionaryValue* blacklist = |
| 419 profile_->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); |
| 420 return blacklist && blacklist->HasKey(GetURLHash(url)); |
| 422 } | 421 } |
| 423 | 422 |
| 424 void TopSites::ClearBlacklistedURLs() { | 423 void TopSites::ClearBlacklistedURLs() { |
| 425 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 426 { | 425 { |
| 427 DictionaryPrefUpdate update(profile_->GetPrefs(), | 426 DictionaryPrefUpdate update(profile_->GetPrefs(), |
| 428 prefs::kNtpMostVisitedURLsBlacklist); | 427 prefs::kNtpMostVisitedURLsBlacklist); |
| 429 DictionaryValue* blacklist = update.Get(); | 428 DictionaryValue* blacklist = update.Get(); |
| 430 blacklist->Clear(); | 429 blacklist->Clear(); |
| 431 } | 430 } |
| (...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 912 NotifyTopSitesChanged(); | 911 NotifyTopSitesChanged(); |
| 913 | 912 |
| 914 // Restart the timer that queries history for top sites. This is done to | 913 // Restart the timer that queries history for top sites. This is done to |
| 915 // ensure we stay in sync with history. | 914 // ensure we stay in sync with history. |
| 916 RestartQueryForTopSitesTimer(GetUpdateDelay()); | 915 RestartQueryForTopSitesTimer(GetUpdateDelay()); |
| 917 } | 916 } |
| 918 | 917 |
| 919 int TopSites::num_results_to_request_from_history() const { | 918 int TopSites::num_results_to_request_from_history() const { |
| 920 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 919 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 921 | 920 |
| 922 return kTopSitesNumber + blacklist_->size(); | 921 const DictionaryValue* blacklist = |
| 922 profile_->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); |
| 923 return kTopSitesNumber + (blacklist ? blacklist->size() : 0); |
| 923 } | 924 } |
| 924 | 925 |
| 925 void TopSites::MoveStateToLoaded() { | 926 void TopSites::MoveStateToLoaded() { |
| 926 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 927 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 927 | 928 |
| 928 MostVisitedURLList filtered_urls; | 929 MostVisitedURLList filtered_urls; |
| 929 PendingCallbackSet pending_callbacks; | 930 PendingCallbackSet pending_callbacks; |
| 930 { | 931 { |
| 931 base::AutoLock lock(lock_); | 932 base::AutoLock lock(lock_); |
| 932 | 933 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1037 SetTopSites(pages); | 1038 SetTopSites(pages); |
| 1038 | 1039 |
| 1039 // Used only in testing. | 1040 // Used only in testing. |
| 1040 content::NotificationService::current()->Notify( | 1041 content::NotificationService::current()->Notify( |
| 1041 chrome::NOTIFICATION_TOP_SITES_UPDATED, | 1042 chrome::NOTIFICATION_TOP_SITES_UPDATED, |
| 1042 content::Source<TopSites>(this), | 1043 content::Source<TopSites>(this), |
| 1043 content::Details<CancelableRequestProvider::Handle>(&handle)); | 1044 content::Details<CancelableRequestProvider::Handle>(&handle)); |
| 1044 } | 1045 } |
| 1045 | 1046 |
| 1046 } // namespace history | 1047 } // namespace history |
| OLD | NEW |