| Index: chrome/browser/android/ntp/most_visited_sites.cc
|
| diff --git a/components/ntp_tiles/most_visited_sites.cc b/chrome/browser/android/ntp/most_visited_sites.cc
|
| similarity index 93%
|
| copy from components/ntp_tiles/most_visited_sites.cc
|
| copy to chrome/browser/android/ntp/most_visited_sites.cc
|
| index 4396851b671f055f47d1c05808daeb6a5a511a51..2cff04d7dfaa9b34c5269bf1f7ec8e3f54d0c804 100644
|
| --- a/components/ntp_tiles/most_visited_sites.cc
|
| +++ b/chrome/browser/android/ntp/most_visited_sites.cc
|
| @@ -2,10 +2,8 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "components/ntp_tiles/most_visited_sites.h"
|
| +#include "chrome/browser/android/ntp/most_visited_sites.h"
|
|
|
| -#include <algorithm>
|
| -#include <set>
|
| #include <utility>
|
|
|
| #include "base/callback.h"
|
| @@ -24,10 +22,12 @@
|
| #include "components/pref_registry/pref_registry_syncable.h"
|
| #include "components/prefs/pref_service.h"
|
| #include "components/variations/variations_associated_data.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| #include "third_party/skia/include/core/SkBitmap.h"
|
| #include "ui/gfx/codec/jpeg_codec.h"
|
| #include "url/gurl.h"
|
|
|
| +using content::BrowserThread;
|
| using history::TopSites;
|
| using suggestions::ChromeSuggestion;
|
| using suggestions::SuggestionsProfile;
|
| @@ -63,13 +63,13 @@ enum MostVisitedTileType {
|
| NUM_TILE_TYPES,
|
| };
|
|
|
| -// May only be called from blocking thread pool.
|
| -std::unique_ptr<SkBitmap> TryFetchLocalThumbnail(
|
| +std::unique_ptr<SkBitmap> MaybeFetchLocalThumbnail(
|
| const GURL& url,
|
| const scoped_refptr<TopSites>& top_sites) {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::DB);
|
| scoped_refptr<base::RefCountedMemory> image;
|
| std::unique_ptr<SkBitmap> bitmap;
|
| - if (top_sites->GetPageThumbnail(url, false, &image))
|
| + if (top_sites && top_sites->GetPageThumbnail(url, false, &image))
|
| bitmap = gfx::JPEGCodec::Decode(image->front(), image->size());
|
| return bitmap;
|
| }
|
| @@ -169,7 +169,6 @@ MostVisitedSites::Suggestion&
|
| MostVisitedSites::Suggestion::operator=(Suggestion&&) = default;
|
|
|
| MostVisitedSites::MostVisitedSites(
|
| - scoped_refptr<base::SequencedWorkerPool> blocking_pool,
|
| PrefService* prefs,
|
| const TemplateURLService* template_url_service,
|
| variations::VariationsService* variations_service,
|
| @@ -193,12 +192,7 @@ MostVisitedSites::MostVisitedSites(
|
| recorded_uma_(false),
|
| scoped_observer_(this),
|
| mv_source_(SUGGESTIONS_SERVICE),
|
| - blocking_pool_(std::move(blocking_pool)),
|
| - blocking_runner_(blocking_pool_->GetTaskRunnerWithShutdownBehavior(
|
| - base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)),
|
| weak_ptr_factory_(this) {
|
| - DCHECK(top_sites_);
|
| - DCHECK(suggestions_service_);
|
| supervisor_->SetObserver(this);
|
| }
|
|
|
| @@ -215,8 +209,8 @@ void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer,
|
| if (ShouldShowPopularSites() &&
|
| NeedPopularSites(prefs_, num_sites_)) {
|
| popular_sites_.reset(new PopularSites(
|
| - blocking_pool_, prefs_, template_url_service_, variations_service_,
|
| - download_context_, popular_sites_directory_, GetPopularSitesCountry(),
|
| + prefs_, template_url_service_, variations_service_, download_context_,
|
| + popular_sites_directory_, GetPopularSitesCountry(),
|
| GetPopularSitesVersion(), false,
|
| base::Bind(&MostVisitedSites::OnPopularSitesAvailable,
|
| base::Unretained(this))));
|
| @@ -224,13 +218,16 @@ void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer,
|
| received_popular_sites_ = true;
|
| }
|
|
|
| - // TopSites updates itself after a delay. To ensure up-to-date results,
|
| - // force an update now.
|
| - top_sites_->SyncWithHistory();
|
| + // TODO(treib): Can |top_sites_| ever be null? If not, remove these checks.
|
| + if (top_sites_) {
|
| + // TopSites updates itself after a delay. To ensure up-to-date results,
|
| + // force an update now.
|
| + top_sites_->SyncWithHistory();
|
|
|
| - // Register as TopSitesObserver so that we can update ourselves when the
|
| - // TopSites changes.
|
| - scoped_observer_.Add(top_sites_.get());
|
| + // Register as TopSitesObserver so that we can update ourselves when the
|
| + // TopSites changes.
|
| + scoped_observer_.Add(top_sites_.get());
|
| + }
|
|
|
| suggestions_subscription_ = suggestions_service_->AddCallback(
|
| base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable,
|
| @@ -245,11 +242,12 @@ void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer,
|
|
|
| void MostVisitedSites::GetURLThumbnail(const GURL& url,
|
| const ThumbnailCallback& callback) {
|
| - DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
| - base::PostTaskAndReplyWithResult(
|
| - blocking_runner_.get(), FROM_HERE,
|
| - base::Bind(&TryFetchLocalThumbnail, url, top_sites_),
|
| + // TODO(treib): Move this to the blocking pool? Doesn't seem related to DB.
|
| + BrowserThread::PostTaskAndReplyWithResult(
|
| + BrowserThread::DB, FROM_HERE,
|
| + base::Bind(&MaybeFetchLocalThumbnail, url, top_sites_),
|
| base::Bind(&MostVisitedSites::OnLocalThumbnailFetched,
|
| weak_ptr_factory_.GetWeakPtr(), url, callback));
|
| }
|
| @@ -258,7 +256,7 @@ void MostVisitedSites::OnLocalThumbnailFetched(
|
| const GURL& url,
|
| const ThumbnailCallback& callback,
|
| std::unique_ptr<SkBitmap> bitmap) {
|
| - DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
| if (bitmap.get()) {
|
| callback.Run(true /* is_local_thumbnail */, bitmap.get());
|
| return;
|
| @@ -266,7 +264,8 @@ void MostVisitedSites::OnLocalThumbnailFetched(
|
|
|
| // A thumbnail is not locally available for |url|. Make sure it is put in
|
| // the list to be fetched at the next visit to this site.
|
| - top_sites_->AddForcedURL(url, base::Time::Now());
|
| + if (top_sites_)
|
| + top_sites_->AddForcedURL(url, base::Time::Now());
|
| // Also fetch a remote thumbnail if possible. PopularSites or the
|
| // SuggestionsService can supply a thumbnail download URL.
|
| if (popular_sites_) {
|
| @@ -295,7 +294,7 @@ void MostVisitedSites::OnObtainedThumbnail(bool is_local_thumbnail,
|
| const ThumbnailCallback& callback,
|
| const GURL& url,
|
| const gfx::Image& image) {
|
| - DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
| const SkBitmap* bitmap = nullptr;
|
| if (!image.IsEmpty())
|
| bitmap = image.ToSkBitmap();
|
| @@ -305,10 +304,12 @@ void MostVisitedSites::OnObtainedThumbnail(bool is_local_thumbnail,
|
| void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url,
|
| bool add_url) {
|
| // Always blacklist in the local TopSites.
|
| - if (add_url)
|
| - top_sites_->AddBlacklistedURL(url);
|
| - else
|
| - top_sites_->RemoveBlacklistedURL(url);
|
| + if (top_sites_) {
|
| + if (add_url)
|
| + top_sites_->AddBlacklistedURL(url);
|
| + else
|
| + top_sites_->RemoveBlacklistedURL(url);
|
| + }
|
|
|
| // Only blacklist in the server-side suggestions service if it's active.
|
| if (mv_source_ == SUGGESTIONS_SERVICE) {
|
| @@ -378,6 +379,9 @@ void MostVisitedSites::BuildCurrentSuggestions() {
|
| }
|
|
|
| void MostVisitedSites::InitiateTopSitesQuery() {
|
| + if (!top_sites_)
|
| + return;
|
| +
|
| top_sites_->GetMostVisitedURLs(
|
| base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable,
|
| weak_ptr_factory_.GetWeakPtr()),
|
| @@ -471,7 +475,7 @@ MostVisitedSites::CreateWhitelistEntryPointSuggestions(
|
|
|
| for (const auto& whitelist : supervisor_->whitelists()) {
|
| // Skip blacklisted sites.
|
| - if (top_sites_->IsBlacklisted(whitelist.entry_point))
|
| + if (top_sites_ && top_sites_->IsBlacklisted(whitelist.entry_point))
|
| continue;
|
|
|
| // Skip suggestions already present.
|
| @@ -522,7 +526,7 @@ MostVisitedSites::CreatePopularSitesSuggestions(
|
| hosts.insert(suggestion->url.host());
|
| for (const PopularSites::Site& popular_site : popular_sites_->sites()) {
|
| // Skip blacklisted sites.
|
| - if (top_sites_->IsBlacklisted(popular_site.url))
|
| + if (top_sites_ && top_sites_->IsBlacklisted(popular_site.url))
|
| continue;
|
| std::string host = popular_site.url.host();
|
| // Skip suggestions already present in personal or whitelists.
|
|
|