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
3 // found in the LICENSE file.
5 #include "chrome/browser/history/top_sites_impl.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/logging.h"
14 #include "base/memory/ref_counted_memory.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/metrics/histogram.h"
17 #include "base/prefs/pref_service.h"
18 #include "base/prefs/scoped_user_pref_update.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/task_runner.h"
22 #include "base/values.h"
23 #include "chrome/browser/chrome_notification_types.h"
24 #include "chrome/browser/history/history_backend.h"
25 #include "chrome/browser/history/history_db_task.h"
26 #include "chrome/browser/history/history_notifications.h"
27 #include "chrome/browser/history/history_service_factory.h"
28 #include "chrome/browser/history/page_usage_data.h"
29 #include "chrome/browser/history/top_sites_cache.h"
30 #include "chrome/browser/history/url_utils.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h"
33 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
34 #include "chrome/common/pref_names.h"
35 #include "chrome/common/thumbnail_score.h"
36 #include "content/public/browser/browser_thread.h"
37 #include "content/public/browser/navigation_controller.h"
38 #include "content/public/browser/navigation_details.h"
39 #include "content/public/browser/navigation_entry.h"
40 #include "content/public/browser/notification_service.h"
41 #include "content/public/browser/web_contents.h"
42 #include "grit/locale_settings.h"
43 #include "ui/base/l10n/l10n_util.h"
44 #include "ui/base/layout.h"
45 #include "ui/base/resource/resource_bundle.h"
46 #include "ui/gfx/image/image_util.h"
48 using base::DictionaryValue;
49 using content::BrowserThread;
50 using content::NavigationController;
56 void RunOrPostGetMostVisitedURLsCallback(
57 base::TaskRunner* task_runner,
58 bool include_forced_urls,
59 const TopSitesImpl::GetMostVisitedURLsCallback& callback,
60 const MostVisitedURLList& all_urls,
61 const MostVisitedURLList& nonforced_urls) {
62 const MostVisitedURLList* urls =
63 include_forced_urls ? &all_urls : &nonforced_urls;
64 if (task_runner->RunsTasksOnCurrentThread())
67 task_runner->PostTask(FROM_HERE, base::Bind(callback, *urls));
70 // Compares two MostVisitedURL having a non-null |last_forced_time|.
71 bool ForcedURLComparator(const MostVisitedURL& first,
72 const MostVisitedURL& second) {
73 DCHECK(!first.last_forced_time.is_null() &&
74 !second.last_forced_time.is_null());
75 return first.last_forced_time < second.last_forced_time;
80 // How many non-forced top sites to store in the cache.
81 static const size_t kNonForcedTopSitesNumber = 20;
83 // How many forced top sites to store in the cache.
84 static const size_t kForcedTopSitesNumber = 20;
86 // Max number of temporary images we'll cache. See comment above
87 // temp_images_ for details.
88 static const size_t kMaxTempTopImages = 8;
90 static const int kDaysOfHistory = 90;
91 // Time from startup to first HistoryService query.
92 static const int64 kUpdateIntervalSecs = 15;
93 // Intervals between requests to HistoryService.
94 static const int64 kMinUpdateIntervalMinutes = 1;
95 static const int64 kMaxUpdateIntervalMinutes = 60;
97 // Use 100 quality (highest quality) because we're very sensitive to
98 // artifacts for these small sized, highly detailed images.
99 static const int kTopSitesImageQuality = 100;
101 TopSitesImpl::TopSitesImpl(Profile* profile)
103 cache_(new TopSitesCache()),
104 thread_safe_cache_(new TopSitesCache()),
106 last_num_urls_changed_(0),
111 if (content::NotificationService::current()) {
112 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED,
113 content::Source<Profile>(profile_));
114 // Listen for any nav commits. We'll ignore those not related to this
115 // profile when we get the notification.
116 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
117 content::NotificationService::AllSources());
119 for (size_t i = 0; i < arraysize(kPrepopulatedPages); i++) {
120 int url_id = kPrepopulatedPages[i].url_id;
121 prepopulated_page_urls_.push_back(
122 GURL(l10n_util::GetStringUTF8(url_id)));
126 void TopSitesImpl::Init(const base::FilePath& db_name) {
127 // Create the backend here, rather than in the constructor, so that
128 // unit tests that do not need the backend can run without a problem.
129 backend_ = new TopSitesBackend;
130 backend_->Init(db_name);
131 backend_->GetMostVisitedThumbnails(
132 base::Bind(&TopSitesImpl::OnGotMostVisitedThumbnails,
133 base::Unretained(this)),
134 &cancelable_task_tracker_);
137 bool TopSitesImpl::SetPageThumbnail(const GURL& url,
138 const gfx::Image& thumbnail,
139 const ThumbnailScore& score) {
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
143 // TODO(sky): I need to cache these and apply them after the load
148 bool add_temp_thumbnail = false;
149 if (!IsKnownURL(url)) {
150 if (!IsNonForcedFull()) {
151 add_temp_thumbnail = true;
153 return false; // This URL is not known to us.
157 if (!HistoryService::CanAddURL(url))
158 return false; // It's not a real webpage.
160 scoped_refptr<base::RefCountedBytes> thumbnail_data;
161 if (!EncodeBitmap(thumbnail, &thumbnail_data))
164 if (add_temp_thumbnail) {
165 // Always remove the existing entry and then add it back. That way if we end
166 // up with too many temp thumbnails we'll prune the oldest first.
167 RemoveTemporaryThumbnailByURL(url);
168 AddTemporaryThumbnail(url, thumbnail_data.get(), score);
172 return SetPageThumbnailEncoded(url, thumbnail_data.get(), score);
175 bool TopSitesImpl::SetPageThumbnailToJPEGBytes(
177 const base::RefCountedMemory* memory,
178 const ThumbnailScore& score) {
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
182 // TODO(sky): I need to cache these and apply them after the load
187 bool add_temp_thumbnail = false;
188 if (!IsKnownURL(url)) {
189 if (!IsNonForcedFull()) {
190 add_temp_thumbnail = true;
192 return false; // This URL is not known to us.
196 if (!HistoryService::CanAddURL(url))
197 return false; // It's not a real webpage.
199 if (add_temp_thumbnail) {
200 // Always remove the existing entry and then add it back. That way if we end
201 // up with too many temp thumbnails we'll prune the oldest first.
202 RemoveTemporaryThumbnailByURL(url);
203 AddTemporaryThumbnail(url, memory, score);
207 return SetPageThumbnailEncoded(url, memory, score);
210 // WARNING: this function may be invoked on any thread.
211 void TopSitesImpl::GetMostVisitedURLs(
212 const GetMostVisitedURLsCallback& callback,
213 bool include_forced_urls) {
214 MostVisitedURLList filtered_urls;
216 base::AutoLock lock(lock_);
218 // A request came in before we finished loading. Store the callback and
219 // we'll run it on current thread when we finish loading.
220 pending_callbacks_.push_back(
221 base::Bind(&RunOrPostGetMostVisitedURLsCallback,
222 base::MessageLoopProxy::current(),
227 if (include_forced_urls) {
228 filtered_urls = thread_safe_cache_->top_sites();
230 filtered_urls.assign(thread_safe_cache_->top_sites().begin() +
231 thread_safe_cache_->GetNumForcedURLs(),
232 thread_safe_cache_->top_sites().end());
235 callback.Run(filtered_urls);
238 bool TopSitesImpl::GetPageThumbnail(
241 scoped_refptr<base::RefCountedMemory>* bytes) {
242 // WARNING: this may be invoked on any thread.
243 // Perform exact match.
245 base::AutoLock lock(lock_);
246 if (thread_safe_cache_->GetPageThumbnail(url, bytes))
250 // Resource bundle is thread safe.
251 for (size_t i = 0; i < arraysize(kPrepopulatedPages); i++) {
252 if (url == prepopulated_page_urls_[i]) {
253 *bytes = ResourceBundle::GetSharedInstance().
254 LoadDataResourceBytesForScale(
255 kPrepopulatedPages[i].thumbnail_id,
256 ui::SCALE_FACTOR_100P);
262 // If http or https, search with |url| first, then try the other one.
263 std::vector<GURL> url_list;
264 url_list.push_back(url);
265 if (url.SchemeIsHTTPOrHTTPS())
266 url_list.push_back(ToggleHTTPAndHTTPS(url));
268 for (std::vector<GURL>::iterator it = url_list.begin();
269 it != url_list.end(); ++it) {
270 base::AutoLock lock(lock_);
273 // Test whether any stored URL is a prefix of |url|.
274 canonical_url = thread_safe_cache_->GetGeneralizedCanonicalURL(*it);
275 if (!canonical_url.is_empty() &&
276 thread_safe_cache_->GetPageThumbnail(canonical_url, bytes)) {
285 bool TopSitesImpl::GetPageThumbnailScore(const GURL& url,
286 ThumbnailScore* score) {
287 // WARNING: this may be invoked on any thread.
288 base::AutoLock lock(lock_);
289 return thread_safe_cache_->GetPageThumbnailScore(url, score);
292 bool TopSitesImpl::GetTemporaryPageThumbnailScore(const GURL& url,
293 ThumbnailScore* score) {
294 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end();
296 if (i->first == url) {
297 *score = i->second.thumbnail_score;
305 // Returns the index of |url| in |urls|, or -1 if not found.
306 static int IndexOf(const MostVisitedURLList& urls, const GURL& url) {
307 for (size_t i = 0; i < urls.size(); i++) {
308 if (urls[i].url == url)
314 void TopSitesImpl::SyncWithHistory() {
315 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
316 if (loaded_ && temp_images_.size()) {
317 // If we have temporary thumbnails it means there isn't much data, and most
318 // likely the user is first running Chrome. During this time we throttle
319 // updating from history by 30 seconds. If the user creates a new tab page
320 // during this window of time we force updating from history so that the new
321 // tab page isn't so far out of date.
323 StartQueryForMostVisited();
327 bool TopSitesImpl::HasBlacklistedItems() const {
328 const base::DictionaryValue* blacklist =
329 profile_->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist);
330 return blacklist && !blacklist->empty();
333 void TopSitesImpl::AddBlacklistedURL(const GURL& url) {
334 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
336 base::Value* dummy = base::Value::CreateNullValue();
338 DictionaryPrefUpdate update(profile_->GetPrefs(),
339 prefs::kNtpMostVisitedURLsBlacklist);
340 base::DictionaryValue* blacklist = update.Get();
341 blacklist->SetWithoutPathExpansion(GetURLHash(url), dummy);
344 ResetThreadSafeCache();
345 NotifyTopSitesChanged();
348 void TopSitesImpl::RemoveBlacklistedURL(const GURL& url) {
349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
351 DictionaryPrefUpdate update(profile_->GetPrefs(),
352 prefs::kNtpMostVisitedURLsBlacklist);
353 base::DictionaryValue* blacklist = update.Get();
354 blacklist->RemoveWithoutPathExpansion(GetURLHash(url), NULL);
356 ResetThreadSafeCache();
357 NotifyTopSitesChanged();
360 bool TopSitesImpl::IsBlacklisted(const GURL& url) {
361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
362 const base::DictionaryValue* blacklist =
363 profile_->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist);
364 return blacklist && blacklist->HasKey(GetURLHash(url));
367 void TopSitesImpl::ClearBlacklistedURLs() {
368 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
370 DictionaryPrefUpdate update(profile_->GetPrefs(),
371 prefs::kNtpMostVisitedURLsBlacklist);
372 base::DictionaryValue* blacklist = update.Get();
375 ResetThreadSafeCache();
376 NotifyTopSitesChanged();
379 void TopSitesImpl::Shutdown() {
381 // Cancel all requests so that the service doesn't callback to us after we've
382 // invoked Shutdown (this could happen if we have a pending request and
383 // Shutdown is invoked).
384 history_consumer_.CancelAllRequests();
385 backend_->Shutdown();
389 void TopSitesImpl::DiffMostVisited(const MostVisitedURLList& old_list,
390 const MostVisitedURLList& new_list,
391 TopSitesDelta* delta) {
393 // Add all the old URLs for quick lookup. This maps URLs to the corresponding
394 // index in the input.
395 std::map<GURL, size_t> all_old_urls;
396 size_t num_old_forced = 0;
397 for (size_t i = 0; i < old_list.size(); i++) {
398 if (!old_list[i].last_forced_time.is_null())
400 DCHECK(old_list[i].last_forced_time.is_null() || i < num_old_forced)
401 << "Forced URLs must all appear before non-forced URLs.";
402 all_old_urls[old_list[i].url] = i;
405 // Check all the URLs in the new set to see which ones are new or just moved.
406 // When we find a match in the old set, we'll reset its index to our special
407 // marker. This allows us to quickly identify the deleted ones in a later
409 const size_t kAlreadyFoundMarker = static_cast<size_t>(-1);
410 int rank = -1; // Forced URLs have a rank of -1.
411 for (size_t i = 0; i < new_list.size(); i++) {
412 // Increase the rank if we're going through forced URLs. This works because
413 // non-forced URLs all come after forced URLs.
414 if (new_list[i].last_forced_time.is_null())
416 DCHECK(new_list[i].last_forced_time.is_null() == (rank != -1))
417 << "Forced URLs must all appear before non-forced URLs.";
418 std::map<GURL, size_t>::iterator found = all_old_urls.find(new_list[i].url);
419 if (found == all_old_urls.end()) {
420 MostVisitedURLWithRank added;
421 added.url = new_list[i];
423 delta->added.push_back(added);
425 DCHECK(found->second != kAlreadyFoundMarker)
426 << "Same URL appears twice in the new list.";
427 int old_rank = found->second >= num_old_forced ?
428 found->second - num_old_forced : -1;
429 if (old_rank != rank ||
430 old_list[found->second].last_forced_time !=
431 new_list[i].last_forced_time) {
432 MostVisitedURLWithRank moved;
433 moved.url = new_list[i];
435 delta->moved.push_back(moved);
437 found->second = kAlreadyFoundMarker;
441 // Any member without the special marker in the all_old_urls list means that
442 // there wasn't a "new" URL that mapped to it, so it was deleted.
443 for (std::map<GURL, size_t>::const_iterator i = all_old_urls.begin();
444 i != all_old_urls.end(); ++i) {
445 if (i->second != kAlreadyFoundMarker)
446 delta->deleted.push_back(old_list[i->second]);
450 CancelableRequestProvider::Handle TopSitesImpl::StartQueryForMostVisited() {
455 HistoryService* hs = HistoryServiceFactory::GetForProfile(
456 profile_, Profile::EXPLICIT_ACCESS);
457 // |hs| may be null during unit tests.
459 return hs->QueryMostVisitedURLs(
460 num_results_to_request_from_history(),
463 base::Bind(&TopSitesImpl::OnTopSitesAvailableFromHistory,
464 base::Unretained(this)));
469 bool TopSitesImpl::IsKnownURL(const GURL& url) {
470 return loaded_ && cache_->IsKnownURL(url);
473 const std::string& TopSitesImpl::GetCanonicalURLString(const GURL& url) const {
474 return cache_->GetCanonicalURL(url).spec();
477 bool TopSitesImpl::IsNonForcedFull() {
478 return loaded_ && cache_->GetNumNonForcedURLs() >= kNonForcedTopSitesNumber;
481 bool TopSitesImpl::IsForcedFull() {
482 return loaded_ && cache_->GetNumForcedURLs() >= kForcedTopSitesNumber;
485 TopSitesImpl::~TopSitesImpl() {
488 bool TopSitesImpl::SetPageThumbnailNoDB(
490 const base::RefCountedMemory* thumbnail_data,
491 const ThumbnailScore& score) {
492 // This should only be invoked when we know about the url.
493 DCHECK(cache_->IsKnownURL(url));
495 const MostVisitedURL& most_visited =
496 cache_->top_sites()[cache_->GetURLIndex(url)];
497 Images* image = cache_->GetImage(url);
499 // When comparing the thumbnail scores, we need to take into account the
500 // redirect hops, which are not generated when the thumbnail is because the
501 // redirects weren't known. We fill that in here since we know the redirects.
502 ThumbnailScore new_score_with_redirects(score);
503 new_score_with_redirects.redirect_hops_from_dest =
504 GetRedirectDistanceForURL(most_visited, url);
506 if (!ShouldReplaceThumbnailWith(image->thumbnail_score,
507 new_score_with_redirects) &&
508 image->thumbnail.get())
509 return false; // The one we already have is better.
511 image->thumbnail = const_cast<base::RefCountedMemory*>(thumbnail_data);
512 image->thumbnail_score = new_score_with_redirects;
514 ResetThreadSafeImageCache();
518 bool TopSitesImpl::SetPageThumbnailEncoded(
520 const base::RefCountedMemory* thumbnail,
521 const ThumbnailScore& score) {
522 if (!SetPageThumbnailNoDB(url, thumbnail, score))
525 // Update the database.
526 if (!cache_->IsKnownURL(url))
529 size_t index = cache_->GetURLIndex(url);
530 int url_rank = index - cache_->GetNumForcedURLs();
531 const MostVisitedURL& most_visited = cache_->top_sites()[index];
532 backend_->SetPageThumbnail(most_visited,
533 url_rank < 0 ? -1 : url_rank,
534 *(cache_->GetImage(most_visited.url)));
539 bool TopSitesImpl::EncodeBitmap(const gfx::Image& bitmap,
540 scoped_refptr<base::RefCountedBytes>* bytes) {
541 if (bitmap.IsEmpty())
543 *bytes = new base::RefCountedBytes();
544 std::vector<unsigned char> data;
545 if (!gfx::JPEG1xEncodedDataFromImage(bitmap, kTopSitesImageQuality, &data))
548 // As we're going to cache this data, make sure the vector is only as big as
549 // it needs to be, as JPEGCodec::Encode() over-allocates data.capacity().
550 // (In a C++0x future, we can just call shrink_to_fit() in Encode())
551 (*bytes)->data() = data;
555 void TopSitesImpl::RemoveTemporaryThumbnailByURL(const GURL& url) {
556 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end();
558 if (i->first == url) {
559 temp_images_.erase(i);
565 void TopSitesImpl::AddTemporaryThumbnail(
567 const base::RefCountedMemory* thumbnail,
568 const ThumbnailScore& score) {
569 if (temp_images_.size() == kMaxTempTopImages)
570 temp_images_.erase(temp_images_.begin());
574 image.second.thumbnail = const_cast<base::RefCountedMemory*>(thumbnail);
575 image.second.thumbnail_score = score;
576 temp_images_.push_back(image);
579 void TopSitesImpl::TimerFired() {
580 StartQueryForMostVisited();
584 int TopSitesImpl::GetRedirectDistanceForURL(const MostVisitedURL& most_visited,
586 for (size_t i = 0; i < most_visited.redirects.size(); i++) {
587 if (most_visited.redirects[i] == url)
588 return static_cast<int>(most_visited.redirects.size() - i - 1);
590 NOTREACHED() << "URL should always be found.";
594 MostVisitedURLList TopSitesImpl::GetPrepopulatePages() {
595 MostVisitedURLList urls;
596 urls.resize(arraysize(kPrepopulatedPages));
597 for (size_t i = 0; i < urls.size(); ++i) {
598 MostVisitedURL& url = urls[i];
599 url.url = GURL(prepopulated_page_urls_[i]);
600 url.redirects.push_back(url.url);
601 url.title = l10n_util::GetStringUTF16(kPrepopulatedPages[i].title_id);
606 bool TopSitesImpl::loaded() const {
610 bool TopSitesImpl::AddForcedURL(const GURL& url, const base::Time& time) {
611 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
612 size_t num_forced = cache_->GetNumForcedURLs();
613 MostVisitedURLList new_list(cache_->top_sites());
614 MostVisitedURL new_url;
616 if (cache_->IsKnownURL(url)) {
617 size_t index = cache_->GetURLIndex(url);
618 // Do nothing if we currently have that URL as non-forced.
619 if (new_list[index].last_forced_time.is_null())
622 // Update the |last_forced_time| of the already existing URL. Delete it and
623 // reinsert it at the right location.
624 new_url = new_list[index];
625 new_list.erase(new_list.begin() + index);
629 new_url.redirects.push_back(url);
631 new_url.last_forced_time = time;
632 // Add forced URLs and sort. Added to the end of the list of forced URLs
633 // since this is almost always where it needs to go, unless the user's local
634 // clock is fiddled with.
635 MostVisitedURLList::iterator mid = new_list.begin() + num_forced;
636 new_list.insert(mid, new_url);
637 mid = new_list.begin() + num_forced; // Mid was invalidated.
638 std::inplace_merge(new_list.begin(), mid, mid + 1, ForcedURLComparator);
639 SetTopSites(new_list);
643 bool TopSitesImpl::AddPrepopulatedPages(MostVisitedURLList* urls,
644 size_t num_forced_urls) {
646 MostVisitedURLList prepopulate_urls = GetPrepopulatePages();
647 for (size_t i = 0; i < prepopulate_urls.size(); ++i) {
648 if (urls->size() - num_forced_urls < kNonForcedTopSitesNumber &&
649 IndexOf(*urls, prepopulate_urls[i].url) == -1) {
650 urls->push_back(prepopulate_urls[i]);
657 size_t TopSitesImpl::MergeCachedForcedURLs(MostVisitedURLList* new_list) {
658 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
659 // Add all the new URLs for quick lookup. Take that opportunity to count the
660 // number of forced URLs in |new_list|.
661 std::set<GURL> all_new_urls;
662 size_t num_forced = 0;
663 for (size_t i = 0; i < new_list->size(); ++i) {
664 all_new_urls.insert((*new_list)[i].url);
665 if (!(*new_list)[i].last_forced_time.is_null())
669 // Keep the forced URLs from |cache_| that are not found in |new_list|.
670 MostVisitedURLList filtered_forced_urls;
671 for (size_t i = 0; i < cache_->GetNumForcedURLs(); ++i) {
672 if (all_new_urls.find(cache_->top_sites()[i].url) == all_new_urls.end())
673 filtered_forced_urls.push_back(cache_->top_sites()[i]);
675 num_forced += filtered_forced_urls.size();
677 // Prepend forced URLs and sort in order of ascending |last_forced_time|.
678 new_list->insert(new_list->begin(), filtered_forced_urls.begin(),
679 filtered_forced_urls.end());
681 new_list->begin(), new_list->begin() + filtered_forced_urls.size(),
682 new_list->begin() + num_forced, ForcedURLComparator);
684 // Drop older forced URLs if the list overflows. Since forced URLs are always
685 // sort in increasing order of |last_forced_time|, drop the first ones.
686 if (num_forced > kForcedTopSitesNumber) {
687 new_list->erase(new_list->begin(),
688 new_list->begin() + (num_forced - kForcedTopSitesNumber));
689 num_forced = kForcedTopSitesNumber;
695 void TopSitesImpl::ApplyBlacklist(const MostVisitedURLList& urls,
696 MostVisitedURLList* out) {
697 // Log the number of times ApplyBlacklist is called so we can compute the
698 // average number of blacklisted items per user.
699 const base::DictionaryValue* blacklist =
700 profile_->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist);
701 UMA_HISTOGRAM_BOOLEAN("TopSites.NumberOfApplyBlacklist", true);
702 UMA_HISTOGRAM_COUNTS_100("TopSites.NumberOfBlacklistedItems",
703 (blacklist ? blacklist->size() : 0));
704 size_t num_non_forced_urls = 0;
705 size_t num_forced_urls = 0;
706 for (size_t i = 0; i < urls.size(); ++i) {
707 if (!IsBlacklisted(urls[i].url)) {
708 if (urls[i].last_forced_time.is_null()) {
710 if (num_non_forced_urls >= kNonForcedTopSitesNumber)
712 num_non_forced_urls++;
715 if (num_forced_urls >= kForcedTopSitesNumber)
719 out->push_back(urls[i]);
724 std::string TopSitesImpl::GetURLHash(const GURL& url) {
725 // We don't use canonical URLs here to be able to blacklist only one of
726 // the two 'duplicate' sites, e.g. 'gmail.com' and 'mail.google.com'.
727 return base::MD5String(url.spec());
730 base::TimeDelta TopSitesImpl::GetUpdateDelay() {
731 if (cache_->top_sites().size() <= arraysize(kPrepopulatedPages))
732 return base::TimeDelta::FromSeconds(30);
734 int64 range = kMaxUpdateIntervalMinutes - kMinUpdateIntervalMinutes;
735 int64 minutes = kMaxUpdateIntervalMinutes -
736 last_num_urls_changed_ * range / cache_->top_sites().size();
737 return base::TimeDelta::FromMinutes(minutes);
740 void TopSitesImpl::Observe(int type,
741 const content::NotificationSource& source,
742 const content::NotificationDetails& details) {
746 if (type == chrome::NOTIFICATION_HISTORY_URLS_DELETED) {
747 content::Details<history::URLsDeletedDetails> deleted_details(details);
748 if (deleted_details->all_history) {
749 SetTopSites(MostVisitedURLList());
750 backend_->ResetDatabase();
752 std::set<size_t> indices_to_delete; // Indices into top_sites_.
753 for (URLRows::const_iterator i = deleted_details->rows.begin();
754 i != deleted_details->rows.end(); ++i) {
755 if (cache_->IsKnownURL(i->url()))
756 indices_to_delete.insert(cache_->GetURLIndex(i->url()));
759 if (indices_to_delete.empty())
762 MostVisitedURLList new_top_sites(cache_->top_sites());
763 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin();
764 i != indices_to_delete.rend(); i++) {
765 new_top_sites.erase(new_top_sites.begin() + *i);
767 SetTopSites(new_top_sites);
769 StartQueryForMostVisited();
770 } else if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) {
771 NavigationController* controller =
772 content::Source<NavigationController>(source).ptr();
773 Profile* profile = Profile::FromBrowserContext(
774 controller->GetWebContents()->GetBrowserContext());
775 if (profile == profile_ && !IsNonForcedFull()) {
776 content::LoadCommittedDetails* load_details =
777 content::Details<content::LoadCommittedDetails>(details).ptr();
780 const GURL& url = load_details->entry->GetURL();
781 if (!cache_->IsKnownURL(url) && HistoryService::CanAddURL(url)) {
782 // To avoid slamming history we throttle requests when the url updates.
783 // To do otherwise negatively impacts perf tests.
784 RestartQueryForTopSitesTimer(GetUpdateDelay());
790 void TopSitesImpl::SetTopSites(const MostVisitedURLList& new_top_sites) {
791 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
793 MostVisitedURLList top_sites(new_top_sites);
794 size_t num_forced_urls = MergeCachedForcedURLs(&top_sites);
795 AddPrepopulatedPages(&top_sites, num_forced_urls);
798 DiffMostVisited(cache_->top_sites(), top_sites, &delta);
799 if (!delta.deleted.empty() || !delta.added.empty() || !delta.moved.empty()) {
800 backend_->UpdateTopSites(delta);
803 last_num_urls_changed_ = delta.added.size() + delta.moved.size();
805 // We always do the following steps (setting top sites in cache, and resetting
806 // thread safe cache ...) as this method is invoked during startup at which
807 // point the caches haven't been updated yet.
808 cache_->SetTopSites(top_sites);
810 // See if we have any tmp thumbnails for the new sites.
811 if (!temp_images_.empty()) {
812 for (size_t i = 0; i < top_sites.size(); ++i) {
813 const MostVisitedURL& mv = top_sites[i];
814 GURL canonical_url = cache_->GetCanonicalURL(mv.url);
815 // At the time we get the thumbnail redirects aren't known, so we have to
816 // iterate through all the images.
817 for (TempImages::iterator it = temp_images_.begin();
818 it != temp_images_.end(); ++it) {
819 if (canonical_url == cache_->GetCanonicalURL(it->first)) {
820 SetPageThumbnailEncoded(
821 mv.url, it->second.thumbnail.get(), it->second.thumbnail_score);
822 temp_images_.erase(it);
829 if (top_sites.size() - num_forced_urls >= kNonForcedTopSitesNumber)
830 temp_images_.clear();
832 ResetThreadSafeCache();
833 ResetThreadSafeImageCache();
834 NotifyTopSitesChanged();
836 // Restart the timer that queries history for top sites. This is done to
837 // ensure we stay in sync with history.
838 RestartQueryForTopSitesTimer(GetUpdateDelay());
841 int TopSitesImpl::num_results_to_request_from_history() const {
842 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
844 const base::DictionaryValue* blacklist =
845 profile_->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist);
846 return kNonForcedTopSitesNumber + (blacklist ? blacklist->size() : 0);
849 void TopSitesImpl::MoveStateToLoaded() {
850 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
852 MostVisitedURLList filtered_urls_all;
853 MostVisitedURLList filtered_urls_nonforced;
854 PendingCallbacks pending_callbacks;
856 base::AutoLock lock(lock_);
859 return; // Don't do anything if we're already loaded.
862 // Now that we're loaded we can service the queued up callbacks. Copy them
863 // here and service them outside the lock.
864 if (!pending_callbacks_.empty()) {
865 // We always filter out forced URLs because callers of GetMostVisitedURLs
866 // are not interested in them.
867 filtered_urls_all = thread_safe_cache_->top_sites();
868 filtered_urls_nonforced.assign(thread_safe_cache_->top_sites().begin() +
869 thread_safe_cache_->GetNumForcedURLs(),
870 thread_safe_cache_->top_sites().end());
871 pending_callbacks.swap(pending_callbacks_);
875 for (size_t i = 0; i < pending_callbacks.size(); i++)
876 pending_callbacks[i].Run(filtered_urls_all, filtered_urls_nonforced);
878 content::NotificationService::current()->Notify(
879 chrome::NOTIFICATION_TOP_SITES_LOADED,
880 content::Source<Profile>(profile_),
881 content::Details<TopSites>(this));
884 void TopSitesImpl::ResetThreadSafeCache() {
885 base::AutoLock lock(lock_);
886 MostVisitedURLList cached;
887 ApplyBlacklist(cache_->top_sites(), &cached);
888 thread_safe_cache_->SetTopSites(cached);
891 void TopSitesImpl::ResetThreadSafeImageCache() {
892 base::AutoLock lock(lock_);
893 thread_safe_cache_->SetThumbnails(cache_->images());
896 void TopSitesImpl::NotifyTopSitesChanged() {
897 content::NotificationService::current()->Notify(
898 chrome::NOTIFICATION_TOP_SITES_CHANGED,
899 content::Source<TopSites>(this),
900 content::NotificationService::NoDetails());
903 void TopSitesImpl::RestartQueryForTopSitesTimer(base::TimeDelta delta) {
904 if (timer_.IsRunning() && ((timer_start_time_ + timer_.GetCurrentDelay()) <
905 (base::TimeTicks::Now() + delta))) {
909 timer_start_time_ = base::TimeTicks::Now();
911 timer_.Start(FROM_HERE, delta, this, &TopSitesImpl::TimerFired);
914 void TopSitesImpl::OnGotMostVisitedThumbnails(
915 const scoped_refptr<MostVisitedThumbnails>& thumbnails) {
916 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
918 // Set the top sites directly in the cache so that SetTopSites diffs
920 cache_->SetTopSites(thumbnails->most_visited);
921 SetTopSites(thumbnails->most_visited);
922 cache_->SetThumbnails(thumbnails->url_to_images_map);
924 ResetThreadSafeImageCache();
928 // Start a timer that refreshes top sites from history.
929 RestartQueryForTopSitesTimer(
930 base::TimeDelta::FromSeconds(kUpdateIntervalSecs));
933 void TopSitesImpl::OnTopSitesAvailableFromHistory(
934 CancelableRequestProvider::Handle handle,
935 MostVisitedURLList pages) {
938 // Used only in testing.
939 content::NotificationService::current()->Notify(
940 chrome::NOTIFICATION_TOP_SITES_UPDATED,
941 content::Source<TopSitesImpl>(this),
942 content::Details<CancelableRequestProvider::Handle>(&handle));
945 } // namespace history