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

Side by Side Diff: components/history/core/browser/top_sites_impl.cc

Issue 2881753002: TopSites: various small cleanups (Closed)
Patch Set: Created 3 years, 7 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 "components/history/core/browser/top_sites_impl.h" 5 #include "components/history/core/browser/top_sites_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <set> 9 #include <set>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
14 #include "base/location.h" 14 #include "base/location.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/md5.h" 16 #include "base/md5.h"
17 #include "base/memory/ptr_util.h" 17 #include "base/memory/ptr_util.h"
18 #include "base/memory/ref_counted_memory.h" 18 #include "base/memory/ref_counted_memory.h"
19 #include "base/metrics/histogram_macros.h" 19 #include "base/metrics/histogram_macros.h"
20 #include "base/single_thread_task_runner.h" 20 #include "base/single_thread_task_runner.h"
21 #include "base/strings/string_util.h" 21 #include "base/strings/string_util.h"
22 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
23 #include "base/task_runner.h" 23 #include "base/task_runner.h"
24 #include "base/threading/thread_task_runner_handle.h" 24 #include "base/threading/thread_task_runner_handle.h"
25 #include "base/values.h" 25 #include "base/values.h"
26 #include "build/build_config.h" 26 #include "build/build_config.h"
27 #include "components/history/core/browser/history_backend.h" 27 #include "components/history/core/browser/history_backend.h"
28 #include "components/history/core/browser/history_constants.h" 28 #include "components/history/core/browser/history_constants.h"
29 #include "components/history/core/browser/history_db_task.h" 29 #include "components/history/core/browser/history_db_task.h"
30 #include "components/history/core/browser/history_service.h"
30 #include "components/history/core/browser/page_usage_data.h" 31 #include "components/history/core/browser/page_usage_data.h"
31 #include "components/history/core/browser/top_sites_cache.h" 32 #include "components/history/core/browser/top_sites_cache.h"
32 #include "components/history/core/browser/top_sites_observer.h" 33 #include "components/history/core/browser/top_sites_observer.h"
33 #include "components/history/core/browser/url_utils.h" 34 #include "components/history/core/browser/url_utils.h"
34 #include "components/history/core/common/thumbnail_score.h" 35 #include "components/history/core/common/thumbnail_score.h"
35 #include "components/prefs/pref_registry_simple.h" 36 #include "components/prefs/pref_registry_simple.h"
36 #include "components/prefs/pref_service.h" 37 #include "components/prefs/pref_service.h"
37 #include "components/prefs/scoped_user_pref_update.h" 38 #include "components/prefs/scoped_user_pref_update.h"
38 #include "ui/base/l10n/l10n_util.h"
39 #include "ui/base/layout.h" 39 #include "ui/base/layout.h"
40 #include "ui/base/resource/resource_bundle.h" 40 #include "ui/base/resource/resource_bundle.h"
41 #include "ui/gfx/image/image_util.h" 41 #include "ui/gfx/image/image_util.h"
42 42
43 using base::DictionaryValue;
44
45 namespace history { 43 namespace history {
46 namespace { 44 namespace {
47 45
48 void RunOrPostGetMostVisitedURLsCallback( 46 void RunOrPostGetMostVisitedURLsCallback(
49 base::TaskRunner* task_runner, 47 base::TaskRunner* task_runner,
50 bool include_forced_urls, 48 bool include_forced_urls,
51 const TopSitesImpl::GetMostVisitedURLsCallback& callback, 49 const TopSitesImpl::GetMostVisitedURLsCallback& callback,
52 const MostVisitedURLList& all_urls, 50 const MostVisitedURLList& all_urls,
53 const MostVisitedURLList& nonforced_urls) { 51 const MostVisitedURLList& nonforced_urls) {
54 const MostVisitedURLList* urls = 52 const MostVisitedURLList& urls =
55 include_forced_urls ? &all_urls : &nonforced_urls; 53 include_forced_urls ? all_urls : nonforced_urls;
56 if (task_runner->RunsTasksOnCurrentThread()) 54 if (task_runner->RunsTasksOnCurrentThread())
57 callback.Run(*urls); 55 callback.Run(urls);
58 else 56 else
59 task_runner->PostTask(FROM_HERE, base::Bind(callback, *urls)); 57 task_runner->PostTask(FROM_HERE, base::Bind(callback, urls));
60 } 58 }
61 59
62 // Compares two MostVisitedURL having a non-null |last_forced_time|. 60 // Compares two MostVisitedURL having a non-null |last_forced_time|.
63 bool ForcedURLComparator(const MostVisitedURL& first, 61 bool ForcedURLComparator(const MostVisitedURL& first,
64 const MostVisitedURL& second) { 62 const MostVisitedURL& second) {
65 DCHECK(!first.last_forced_time.is_null() && 63 DCHECK(!first.last_forced_time.is_null() &&
66 !second.last_forced_time.is_null()); 64 !second.last_forced_time.is_null());
67 return first.last_forced_time < second.last_forced_time; 65 return first.last_forced_time < second.last_forced_time;
68 } 66 }
69 67
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 } // namespace 100 } // namespace
103 101
104 // Initially, histogram is not recorded. 102 // Initially, histogram is not recorded.
105 bool TopSitesImpl::histogram_recorded_ = false; 103 bool TopSitesImpl::histogram_recorded_ = false;
106 104
107 TopSitesImpl::TopSitesImpl(PrefService* pref_service, 105 TopSitesImpl::TopSitesImpl(PrefService* pref_service,
108 HistoryService* history_service, 106 HistoryService* history_service,
109 const PrepopulatedPageList& prepopulated_pages, 107 const PrepopulatedPageList& prepopulated_pages,
110 const CanAddURLToHistoryFn& can_add_url_to_history) 108 const CanAddURLToHistoryFn& can_add_url_to_history)
111 : backend_(nullptr), 109 : backend_(nullptr),
112 cache_(new TopSitesCache()), 110 cache_(base::MakeUnique<TopSitesCache>()),
113 thread_safe_cache_(new TopSitesCache()), 111 thread_safe_cache_(base::MakeUnique<TopSitesCache>()),
114 last_num_urls_changed_(0), 112 last_num_urls_changed_(0),
115 prepopulated_pages_(prepopulated_pages), 113 prepopulated_pages_(prepopulated_pages),
116 pref_service_(pref_service), 114 pref_service_(pref_service),
117 history_service_(history_service), 115 history_service_(history_service),
118 can_add_url_to_history_(can_add_url_to_history), 116 can_add_url_to_history_(can_add_url_to_history),
119 loaded_(false), 117 loaded_(false),
120 history_service_observer_(this) { 118 history_service_observer_(this) {
121 DCHECK(pref_service_); 119 DCHECK(pref_service_);
122 DCHECK(!can_add_url_to_history_.is_null()); 120 DCHECK(!can_add_url_to_history_.is_null());
123 } 121 }
(...skipping 17 matching lines...) Expand all
141 DCHECK(thread_checker_.CalledOnValidThread()); 139 DCHECK(thread_checker_.CalledOnValidThread());
142 140
143 if (!loaded_) { 141 if (!loaded_) {
144 // TODO(sky): I need to cache these and apply them after the load 142 // TODO(sky): I need to cache these and apply them after the load
145 // completes. 143 // completes.
146 return false; 144 return false;
147 } 145 }
148 146
149 bool add_temp_thumbnail = false; 147 bool add_temp_thumbnail = false;
150 if (!IsKnownURL(url)) { 148 if (!IsKnownURL(url)) {
151 if (!IsNonForcedFull()) { 149 if (IsNonForcedFull())
152 add_temp_thumbnail = true; 150 return false; // We're full, and this URL is not known to us.
153 } else { 151
154 return false; // This URL is not known to us. 152 add_temp_thumbnail = true;
155 }
156 } 153 }
157 154
158 if (!can_add_url_to_history_.Run(url)) 155 if (!can_add_url_to_history_.Run(url))
159 return false; // It's not a real webpage. 156 return false; // It's not a real webpage.
160 157
161 scoped_refptr<base::RefCountedBytes> thumbnail_data; 158 scoped_refptr<base::RefCountedBytes> thumbnail_data;
162 if (!EncodeBitmap(thumbnail, &thumbnail_data)) 159 if (!EncodeBitmap(thumbnail, &thumbnail_data))
163 return false; 160 return false;
164 161
165 if (add_temp_thumbnail) { 162 if (add_temp_thumbnail) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 } 219 }
223 } 220 }
224 221
225 if (prefix_match) { 222 if (prefix_match) {
226 // If http or https, search with |url| first, then try the other one. 223 // If http or https, search with |url| first, then try the other one.
227 std::vector<GURL> url_list; 224 std::vector<GURL> url_list;
228 url_list.push_back(url); 225 url_list.push_back(url);
229 if (url.SchemeIsHTTPOrHTTPS()) 226 if (url.SchemeIsHTTPOrHTTPS())
230 url_list.push_back(ToggleHTTPAndHTTPS(url)); 227 url_list.push_back(ToggleHTTPAndHTTPS(url));
231 228
232 for (std::vector<GURL>::iterator it = url_list.begin(); 229 for (const GURL& url : url_list) {
233 it != url_list.end(); ++it) {
234 base::AutoLock lock(lock_); 230 base::AutoLock lock(lock_);
235 231
236 GURL canonical_url;
237 // Test whether any stored URL is a prefix of |url|. 232 // Test whether any stored URL is a prefix of |url|.
238 canonical_url = thread_safe_cache_->GetGeneralizedCanonicalURL(*it); 233 GURL canonical_url = thread_safe_cache_->GetGeneralizedCanonicalURL(url);
239 if (!canonical_url.is_empty() && 234 if (!canonical_url.is_empty() &&
240 thread_safe_cache_->GetPageThumbnail(canonical_url, bytes)) { 235 thread_safe_cache_->GetPageThumbnail(canonical_url, bytes)) {
241 return true; 236 return true;
242 } 237 }
243 } 238 }
244 } 239 }
245 240
246 return false; 241 return false;
247 } 242 }
248 243
249 bool TopSitesImpl::GetPageThumbnailScore(const GURL& url, 244 bool TopSitesImpl::GetPageThumbnailScore(const GURL& url,
250 ThumbnailScore* score) { 245 ThumbnailScore* score) {
251 // WARNING: this may be invoked on any thread. 246 // WARNING: this may be invoked on any thread.
252 base::AutoLock lock(lock_); 247 base::AutoLock lock(lock_);
253 return thread_safe_cache_->GetPageThumbnailScore(url, score); 248 return thread_safe_cache_->GetPageThumbnailScore(url, score);
254 } 249 }
255 250
256 bool TopSitesImpl::GetTemporaryPageThumbnailScore(const GURL& url, 251 bool TopSitesImpl::GetTemporaryPageThumbnailScore(const GURL& url,
257 ThumbnailScore* score) { 252 ThumbnailScore* score) {
258 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end(); 253 for (const TempImage& temp_image : temp_images_) {
259 ++i) { 254 if (temp_image.first == url) {
260 if (i->first == url) { 255 *score = temp_image.second.thumbnail_score;
261 *score = i->second.thumbnail_score;
262 return true; 256 return true;
263 } 257 }
264 } 258 }
265 return false; 259 return false;
266 } 260 }
267 261
268 262
269 // Returns the index of |url| in |urls|, or -1 if not found. 263 // Returns the index of |url| in |urls|, or -1 if not found.
270 static int IndexOf(const MostVisitedURLList& urls, const GURL& url) { 264 static int IndexOf(const MostVisitedURLList& urls, const GURL& url) {
271 for (size_t i = 0; i < urls.size(); i++) { 265 for (size_t i = 0; i < urls.size(); i++) {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 num_forced--; 373 num_forced--;
380 } else { 374 } else {
381 new_url.url = url; 375 new_url.url = url;
382 new_url.redirects.push_back(url); 376 new_url.redirects.push_back(url);
383 } 377 }
384 new_url.last_forced_time = time; 378 new_url.last_forced_time = time;
385 // Add forced URLs and sort. Added to the end of the list of forced URLs 379 // Add forced URLs and sort. Added to the end of the list of forced URLs
386 // since this is almost always where it needs to go, unless the user's local 380 // since this is almost always where it needs to go, unless the user's local
387 // clock is fiddled with. 381 // clock is fiddled with.
388 MostVisitedURLList::iterator mid = new_list.begin() + num_forced; 382 MostVisitedURLList::iterator mid = new_list.begin() + num_forced;
389 new_list.insert(mid, new_url); 383 mid = new_list.insert(mid, new_url);
390 mid = new_list.begin() + num_forced; // Mid was invalidated.
391 std::inplace_merge(new_list.begin(), mid, mid + 1, ForcedURLComparator); 384 std::inplace_merge(new_list.begin(), mid, mid + 1, ForcedURLComparator);
392 SetTopSites(new_list, CALL_LOCATION_FROM_FORCED_URLS); 385 SetTopSites(new_list, CALL_LOCATION_FROM_FORCED_URLS);
393 return true; 386 return true;
394 } 387 }
395 388
396 void TopSitesImpl::OnNavigationCommitted(const GURL& url) { 389 void TopSitesImpl::OnNavigationCommitted(const GURL& url) {
397 DCHECK(thread_checker_.CalledOnValidThread()); 390 DCHECK(thread_checker_.CalledOnValidThread());
398 if (!loaded_ || IsNonForcedFull()) 391 if (!loaded_ || IsNonForcedFull())
399 return; 392 return;
400 393
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 moved.url = new_list[i]; 476 moved.url = new_list[i];
484 moved.rank = rank; 477 moved.rank = rank;
485 delta->moved.push_back(moved); 478 delta->moved.push_back(moved);
486 } 479 }
487 found->second = kAlreadyFoundMarker; 480 found->second = kAlreadyFoundMarker;
488 } 481 }
489 } 482 }
490 483
491 // Any member without the special marker in the all_old_urls list means that 484 // Any member without the special marker in the all_old_urls list means that
492 // there wasn't a "new" URL that mapped to it, so it was deleted. 485 // there wasn't a "new" URL that mapped to it, so it was deleted.
493 for (std::map<GURL, size_t>::const_iterator i = all_old_urls.begin(); 486 for (const std::pair<GURL, size_t>& old_url : all_old_urls) {
494 i != all_old_urls.end(); ++i) { 487 if (old_url.second != kAlreadyFoundMarker)
495 if (i->second != kAlreadyFoundMarker) 488 delta->deleted.push_back(old_list[old_url.second]);
496 delta->deleted.push_back(old_list[i->second]);
497 } 489 }
498 } 490 }
499 491
500 bool TopSitesImpl::SetPageThumbnailNoDB( 492 bool TopSitesImpl::SetPageThumbnailNoDB(
501 const GURL& url, 493 const GURL& url,
502 const base::RefCountedMemory* thumbnail_data, 494 const base::RefCountedMemory* thumbnail_data,
503 const ThumbnailScore& score) { 495 const ThumbnailScore& score) {
504 // This should only be invoked when we know about the url. 496 // This should only be invoked when we know about the url.
505 DCHECK(cache_->IsKnownURL(url)); 497 DCHECK(cache_->IsKnownURL(url));
506 498
507 const MostVisitedURL& most_visited = 499 const MostVisitedURL& most_visited =
508 cache_->top_sites()[cache_->GetURLIndex(url)]; 500 cache_->top_sites()[cache_->GetURLIndex(url)];
509 Images* image = cache_->GetImage(url); 501 Images* image = cache_->GetImage(url);
510 502
511 // When comparing the thumbnail scores, we need to take into account the 503 // When comparing the thumbnail scores, we need to take into account the
512 // redirect hops, which are not generated when the thumbnail is because the 504 // redirect hops, which are not generated when the thumbnail is because the
513 // redirects weren't known. We fill that in here since we know the redirects. 505 // redirects weren't known. We fill that in here since we know the redirects.
514 ThumbnailScore new_score_with_redirects(score); 506 ThumbnailScore new_score_with_redirects(score);
515 new_score_with_redirects.redirect_hops_from_dest = 507 new_score_with_redirects.redirect_hops_from_dest =
516 GetRedirectDistanceForURL(most_visited, url); 508 GetRedirectDistanceForURL(most_visited, url);
517 509
518 if (!ShouldReplaceThumbnailWith(image->thumbnail_score, 510 if (image->thumbnail.get() &&
519 new_score_with_redirects) && 511 !ShouldReplaceThumbnailWith(image->thumbnail_score,
520 image->thumbnail.get()) 512 new_score_with_redirects)) {
521 return false; // The one we already have is better. 513 return false; // The one we already have is better.
514 }
522 515
523 image->thumbnail = const_cast<base::RefCountedMemory*>(thumbnail_data); 516 image->thumbnail = const_cast<base::RefCountedMemory*>(thumbnail_data);
524 image->thumbnail_score = new_score_with_redirects; 517 image->thumbnail_score = new_score_with_redirects;
525 518
526 ResetThreadSafeImageCache(); 519 ResetThreadSafeImageCache();
527 return true; 520 return true;
528 } 521 }
529 522
530 bool TopSitesImpl::SetPageThumbnailEncoded( 523 bool TopSitesImpl::SetPageThumbnailEncoded(
531 const GURL& url, 524 const GURL& url,
(...skipping 14 matching lines...) Expand all
546 *(cache_->GetImage(most_visited.url))); 539 *(cache_->GetImage(most_visited.url)));
547 return true; 540 return true;
548 } 541 }
549 542
550 // static 543 // static
551 bool TopSitesImpl::EncodeBitmap(const gfx::Image& bitmap, 544 bool TopSitesImpl::EncodeBitmap(const gfx::Image& bitmap,
552 scoped_refptr<base::RefCountedBytes>* bytes) { 545 scoped_refptr<base::RefCountedBytes>* bytes) {
553 if (bitmap.IsEmpty()) 546 if (bitmap.IsEmpty())
554 return false; 547 return false;
555 *bytes = new base::RefCountedBytes(); 548 *bytes = new base::RefCountedBytes();
556 std::vector<unsigned char> data; 549 if (!gfx::JPEG1xEncodedDataFromImage(bitmap, kTopSitesImageQuality,
557 if (!gfx::JPEG1xEncodedDataFromImage(bitmap, kTopSitesImageQuality, &data)) 550 &(*bytes)->data())) {
558 return false; 551 return false;
552 }
559 553
560 // As we're going to cache this data, make sure the vector is only as big as 554 // As we're going to cache this data, make sure the vector is only as big as
561 // it needs to be, as JPEGCodec::Encode() over-allocates data.capacity(). 555 // it needs to be, as JPEGCodec::Encode() over-allocates data.capacity().
562 // (In a C++0x future, we can just call shrink_to_fit() in Encode()) 556 (*bytes)->data().shrink_to_fit();
563 (*bytes)->data() = data;
564 return true; 557 return true;
565 } 558 }
566 559
567 void TopSitesImpl::RemoveTemporaryThumbnailByURL(const GURL& url) { 560 void TopSitesImpl::RemoveTemporaryThumbnailByURL(const GURL& url) {
568 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end(); 561 for (TempImages::iterator i = temp_images_.begin(); i != temp_images_.end();
569 ++i) { 562 ++i) {
570 if (i->first == url) { 563 if (i->first == url) {
571 temp_images_.erase(i); 564 temp_images_.erase(i);
572 return; 565 return;
573 } 566 }
574 } 567 }
575 } 568 }
576 569
577 void TopSitesImpl::AddTemporaryThumbnail( 570 void TopSitesImpl::AddTemporaryThumbnail(const GURL& url,
578 const GURL& url, 571 base::RefCountedMemory* thumbnail,
579 const base::RefCountedMemory* thumbnail, 572 const ThumbnailScore& score) {
580 const ThumbnailScore& score) {
581 if (temp_images_.size() == kMaxTempTopImages) 573 if (temp_images_.size() == kMaxTempTopImages)
582 temp_images_.erase(temp_images_.begin()); 574 temp_images_.pop_front();
583 575
584 TempImage image; 576 TempImage image;
585 image.first = url; 577 image.first = url;
586 image.second.thumbnail = const_cast<base::RefCountedMemory*>(thumbnail); 578 image.second.thumbnail = thumbnail;
587 image.second.thumbnail_score = score; 579 image.second.thumbnail_score = score;
588 temp_images_.push_back(image); 580 temp_images_.push_back(image);
589 } 581 }
590 582
591 void TopSitesImpl::TimerFired() { 583 void TopSitesImpl::TimerFired() {
592 StartQueryForMostVisited(); 584 StartQueryForMostVisited();
593 } 585 }
594 586
595 // static 587 // static
596 int TopSitesImpl::GetRedirectDistanceForURL(const MostVisitedURL& most_visited, 588 int TopSitesImpl::GetRedirectDistanceForURL(const MostVisitedURL& most_visited,
597 const GURL& url) { 589 const GURL& url) {
598 for (size_t i = 0; i < most_visited.redirects.size(); i++) { 590 for (size_t i = 0; i < most_visited.redirects.size(); i++) {
599 if (most_visited.redirects[i] == url) 591 if (most_visited.redirects[i] == url)
600 return static_cast<int>(most_visited.redirects.size() - i - 1); 592 return static_cast<int>(most_visited.redirects.size() - i - 1);
601 } 593 }
602 NOTREACHED() << "URL should always be found."; 594 NOTREACHED() << "URL should always be found.";
603 return 0; 595 return 0;
604 } 596 }
605 597
606 bool TopSitesImpl::AddPrepopulatedPages(MostVisitedURLList* urls, 598 bool TopSitesImpl::AddPrepopulatedPages(MostVisitedURLList* urls,
607 size_t num_forced_urls) { 599 size_t num_forced_urls) const {
608 bool added = false; 600 bool added = false;
609 for (const auto& prepopulated_page : prepopulated_pages_) { 601 for (const auto& prepopulated_page : prepopulated_pages_) {
610 if (urls->size() - num_forced_urls < kNonForcedTopSitesNumber && 602 if (urls->size() - num_forced_urls < kNonForcedTopSitesNumber &&
611 IndexOf(*urls, prepopulated_page.most_visited.url) == -1) { 603 IndexOf(*urls, prepopulated_page.most_visited.url) == -1) {
612 urls->push_back(prepopulated_page.most_visited); 604 urls->push_back(prepopulated_page.most_visited);
613 added = true; 605 added = true;
614 } 606 }
615 } 607 }
616 return added; 608 return added;
617 } 609 }
618 610
619 size_t TopSitesImpl::MergeCachedForcedURLs(MostVisitedURLList* new_list) { 611 size_t TopSitesImpl::MergeCachedForcedURLs(MostVisitedURLList* new_list) const {
620 DCHECK(thread_checker_.CalledOnValidThread()); 612 DCHECK(thread_checker_.CalledOnValidThread());
621 // Add all the new URLs for quick lookup. Take that opportunity to count the 613 // Add all the new URLs for quick lookup. Take that opportunity to count the
622 // number of forced URLs in |new_list|. 614 // number of forced URLs in |new_list|.
623 std::set<GURL> all_new_urls; 615 std::set<GURL> all_new_urls;
624 size_t num_forced = 0; 616 size_t num_forced = 0;
625 for (size_t i = 0; i < new_list->size(); ++i) { 617 for (size_t i = 0; i < new_list->size(); ++i) {
626 for (size_t j = 0; j < (*new_list)[i].redirects.size(); j++) { 618 for (size_t j = 0; j < (*new_list)[i].redirects.size(); j++) {
627 all_new_urls.insert((*new_list)[i].redirects[j]); 619 all_new_urls.insert((*new_list)[i].redirects[j]);
628 } 620 }
629 if (!(*new_list)[i].last_forced_time.is_null()) 621 if (!(*new_list)[i].last_forced_time.is_null())
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 // Forced URL. 670 // Forced URL.
679 if (num_forced_urls >= kForcedTopSitesNumber) 671 if (num_forced_urls >= kForcedTopSitesNumber)
680 continue; 672 continue;
681 num_forced_urls++; 673 num_forced_urls++;
682 } 674 }
683 out->push_back(urls[i]); 675 out->push_back(urls[i]);
684 } 676 }
685 } 677 }
686 } 678 }
687 679
680 // static
688 std::string TopSitesImpl::GetURLHash(const GURL& url) { 681 std::string TopSitesImpl::GetURLHash(const GURL& url) {
689 // We don't use canonical URLs here to be able to blacklist only one of 682 // We don't use canonical URLs here to be able to blacklist only one of
690 // the two 'duplicate' sites, e.g. 'gmail.com' and 'mail.google.com'. 683 // the two 'duplicate' sites, e.g. 'gmail.com' and 'mail.google.com'.
691 return base::MD5String(url.spec()); 684 return base::MD5String(url.spec());
692 } 685 }
693 686
694 base::TimeDelta TopSitesImpl::GetUpdateDelay() { 687 base::TimeDelta TopSitesImpl::GetUpdateDelay() const {
695 if (cache_->top_sites().size() <= prepopulated_pages_.size()) 688 if (cache_->top_sites().size() <= prepopulated_pages_.size())
696 return base::TimeDelta::FromSeconds(30); 689 return base::TimeDelta::FromSeconds(30);
697 690
698 int64_t range = kMaxUpdateIntervalMinutes - kMinUpdateIntervalMinutes; 691 int64_t range = kMaxUpdateIntervalMinutes - kMinUpdateIntervalMinutes;
699 int64_t minutes = kMaxUpdateIntervalMinutes - 692 int64_t minutes = kMaxUpdateIntervalMinutes -
700 last_num_urls_changed_ * range / cache_->top_sites().size(); 693 last_num_urls_changed_ * range / cache_->top_sites().size();
701 return base::TimeDelta::FromMinutes(minutes); 694 return base::TimeDelta::FromMinutes(minutes);
702 } 695 }
703 696
704 void TopSitesImpl::SetTopSites(const MostVisitedURLList& new_top_sites, 697 void TopSitesImpl::SetTopSites(const MostVisitedURLList& new_top_sites,
(...skipping 28 matching lines...) Expand all
733 backend_->UpdateTopSites(delta, record_or_not); 726 backend_->UpdateTopSites(delta, record_or_not);
734 } 727 }
735 728
736 last_num_urls_changed_ = delta.added.size() + delta.moved.size(); 729 last_num_urls_changed_ = delta.added.size() + delta.moved.size();
737 730
738 // We always do the following steps (setting top sites in cache, and resetting 731 // We always do the following steps (setting top sites in cache, and resetting
739 // thread safe cache ...) as this method is invoked during startup at which 732 // thread safe cache ...) as this method is invoked during startup at which
740 // point the caches haven't been updated yet. 733 // point the caches haven't been updated yet.
741 cache_->SetTopSites(top_sites); 734 cache_->SetTopSites(top_sites);
742 735
743 // See if we have any tmp thumbnails for the new sites. 736 // See if we have any temp thumbnails for the new sites, and promote them to
737 // proper thumbnails.
744 if (!temp_images_.empty()) { 738 if (!temp_images_.empty()) {
745 for (size_t i = 0; i < top_sites.size(); ++i) { 739 for (const MostVisitedURL& mv : top_sites) {
746 const MostVisitedURL& mv = top_sites[i]; 740 const GURL& canonical_url = cache_->GetCanonicalURL(mv.url);
747 GURL canonical_url = cache_->GetCanonicalURL(mv.url);
748 // At the time we get the thumbnail redirects aren't known, so we have to 741 // At the time we get the thumbnail redirects aren't known, so we have to
749 // iterate through all the images. 742 // iterate through all the images.
750 for (TempImages::iterator it = temp_images_.begin(); 743 for (TempImages::iterator it = temp_images_.begin();
751 it != temp_images_.end(); ++it) { 744 it != temp_images_.end(); ++it) {
752 if (canonical_url == cache_->GetCanonicalURL(it->first)) { 745 if (canonical_url == cache_->GetCanonicalURL(it->first)) {
753 SetPageThumbnailEncoded( 746 SetPageThumbnailEncoded(
754 mv.url, it->second.thumbnail.get(), it->second.thumbnail_score); 747 mv.url, it->second.thumbnail.get(), it->second.thumbnail_score);
755 temp_images_.erase(it); 748 temp_images_.erase(it);
756 break; 749 break;
757 } 750 }
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin(); 884 for (std::set<size_t>::reverse_iterator i = indices_to_delete.rbegin();
892 i != indices_to_delete.rend(); i++) { 885 i != indices_to_delete.rend(); i++) {
893 new_top_sites.erase(new_top_sites.begin() + *i); 886 new_top_sites.erase(new_top_sites.begin() + *i);
894 } 887 }
895 SetTopSites(new_top_sites, CALL_LOCATION_FROM_OTHER_PLACES); 888 SetTopSites(new_top_sites, CALL_LOCATION_FROM_OTHER_PLACES);
896 } 889 }
897 StartQueryForMostVisited(); 890 StartQueryForMostVisited();
898 } 891 }
899 892
900 } // namespace history 893 } // namespace history
OLDNEW
« no previous file with comments | « components/history/core/browser/top_sites_impl.h ('k') | components/history/core/browser/top_sites_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698