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

Side by Side Diff: chrome/browser/android/ntp/most_visited_sites.cc

Issue 2012473002: Remove NTP dependency on //content/... (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sync Created 4 years, 6 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 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 "chrome/browser/android/ntp/most_visited_sites.h" 5 #include "chrome/browser/android/ntp/most_visited_sites.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/metrics/field_trial.h" 11 #include "base/metrics/field_trial.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/metrics/sparse_histogram.h" 13 #include "base/metrics/sparse_histogram.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/time/time.h" 18 #include "base/time/time.h"
19 #include "components/history/core/browser/top_sites.h" 19 #include "components/history/core/browser/top_sites.h"
20 #include "components/ntp_tiles/pref_names.h" 20 #include "components/ntp_tiles/pref_names.h"
21 #include "components/ntp_tiles/switches.h" 21 #include "components/ntp_tiles/switches.h"
22 #include "components/pref_registry/pref_registry_syncable.h" 22 #include "components/pref_registry/pref_registry_syncable.h"
23 #include "components/prefs/pref_service.h" 23 #include "components/prefs/pref_service.h"
24 #include "components/variations/variations_associated_data.h" 24 #include "components/variations/variations_associated_data.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "third_party/skia/include/core/SkBitmap.h" 25 #include "third_party/skia/include/core/SkBitmap.h"
27 #include "ui/gfx/codec/jpeg_codec.h" 26 #include "ui/gfx/codec/jpeg_codec.h"
28 #include "url/gurl.h" 27 #include "url/gurl.h"
29 28
30 using content::BrowserThread;
31 using history::TopSites; 29 using history::TopSites;
32 using suggestions::ChromeSuggestion; 30 using suggestions::ChromeSuggestion;
33 using suggestions::SuggestionsProfile; 31 using suggestions::SuggestionsProfile;
34 using suggestions::SuggestionsService; 32 using suggestions::SuggestionsService;
35 33
36 namespace { 34 namespace {
37 35
38 // Identifiers for the various tile sources. 36 // Identifiers for the various tile sources.
39 const char kHistogramClientName[] = "client"; 37 const char kHistogramClientName[] = "client";
40 const char kHistogramServerName[] = "server"; 38 const char kHistogramServerName[] = "server";
(...skipping 15 matching lines...) Expand all
56 NONE, 54 NONE,
57 // The item displays a site's actual favicon or touch icon. 55 // The item displays a site's actual favicon or touch icon.
58 ICON_REAL, 56 ICON_REAL,
59 // The item displays a color derived from the site's favicon or touch icon. 57 // The item displays a color derived from the site's favicon or touch icon.
60 ICON_COLOR, 58 ICON_COLOR,
61 // The item displays a default gray box in place of an icon. 59 // The item displays a default gray box in place of an icon.
62 ICON_DEFAULT, 60 ICON_DEFAULT,
63 NUM_TILE_TYPES, 61 NUM_TILE_TYPES,
64 }; 62 };
65 63
64 // May only be called from blocking thread pool.
66 std::unique_ptr<SkBitmap> MaybeFetchLocalThumbnail( 65 std::unique_ptr<SkBitmap> MaybeFetchLocalThumbnail(
67 const GURL& url, 66 const GURL& url,
68 const scoped_refptr<TopSites>& top_sites) { 67 const scoped_refptr<TopSites>& top_sites) {
69 DCHECK_CURRENTLY_ON(BrowserThread::DB);
70 scoped_refptr<base::RefCountedMemory> image; 68 scoped_refptr<base::RefCountedMemory> image;
71 std::unique_ptr<SkBitmap> bitmap; 69 std::unique_ptr<SkBitmap> bitmap;
72 if (top_sites && top_sites->GetPageThumbnail(url, false, &image)) 70 if (top_sites && top_sites->GetPageThumbnail(url, false, &image))
73 bitmap = gfx::JPEGCodec::Decode(image->front(), image->size()); 71 bitmap = gfx::JPEGCodec::Decode(image->front(), image->size());
74 return bitmap; 72 return bitmap;
75 } 73 }
76 74
77 // Log an event for a given |histogram| at a given element |position|. This 75 // Log an event for a given |histogram| at a given element |position|. This
78 // routine exists because regular histogram macros are cached thus can't be used 76 // routine exists because regular histogram macros are cached thus can't be used
79 // if the name of the histogram will change at a given call site. 77 // if the name of the histogram will change at a given call site.
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 160
163 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {} 161 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {}
164 162
165 MostVisitedSites::Suggestion::~Suggestion() {} 163 MostVisitedSites::Suggestion::~Suggestion() {}
166 164
167 MostVisitedSites::Suggestion::Suggestion(Suggestion&&) = default; 165 MostVisitedSites::Suggestion::Suggestion(Suggestion&&) = default;
168 MostVisitedSites::Suggestion& 166 MostVisitedSites::Suggestion&
169 MostVisitedSites::Suggestion::operator=(Suggestion&&) = default; 167 MostVisitedSites::Suggestion::operator=(Suggestion&&) = default;
170 168
171 MostVisitedSites::MostVisitedSites( 169 MostVisitedSites::MostVisitedSites(
170 scoped_refptr<base::SequencedWorkerPool> blocking_pool,
172 PrefService* prefs, 171 PrefService* prefs,
173 const TemplateURLService* template_url_service, 172 const TemplateURLService* template_url_service,
174 variations::VariationsService* variations_service, 173 variations::VariationsService* variations_service,
175 net::URLRequestContextGetter* download_context, 174 net::URLRequestContextGetter* download_context,
176 const base::FilePath& popular_sites_directory, 175 const base::FilePath& popular_sites_directory,
177 scoped_refptr<history::TopSites> top_sites, 176 scoped_refptr<history::TopSites> top_sites,
178 SuggestionsService* suggestions, 177 SuggestionsService* suggestions,
179 MostVisitedSitesSupervisor* supervisor) 178 MostVisitedSitesSupervisor* supervisor)
180 : prefs_(prefs), 179 : prefs_(prefs),
181 template_url_service_(template_url_service), 180 template_url_service_(template_url_service),
182 variations_service_(variations_service), 181 variations_service_(variations_service),
183 download_context_(download_context), 182 download_context_(download_context),
184 popular_sites_directory_(popular_sites_directory), 183 popular_sites_directory_(popular_sites_directory),
185 top_sites_(top_sites), 184 top_sites_(top_sites),
186 suggestions_service_(suggestions), 185 suggestions_service_(suggestions),
187 supervisor_(supervisor), 186 supervisor_(supervisor),
188 observer_(nullptr), 187 observer_(nullptr),
189 num_sites_(0), 188 num_sites_(0),
190 received_most_visited_sites_(false), 189 received_most_visited_sites_(false),
191 received_popular_sites_(false), 190 received_popular_sites_(false),
192 recorded_uma_(false), 191 recorded_uma_(false),
193 scoped_observer_(this), 192 scoped_observer_(this),
194 mv_source_(SUGGESTIONS_SERVICE), 193 mv_source_(SUGGESTIONS_SERVICE),
194 blocking_pool_(std::move(blocking_pool)),
195 blocking_runner_(blocking_pool_->GetTaskRunnerWithShutdownBehavior(
196 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)),
195 weak_ptr_factory_(this) { 197 weak_ptr_factory_(this) {
196 supervisor_->SetObserver(this); 198 supervisor_->SetObserver(this);
197 } 199 }
198 200
199 MostVisitedSites::~MostVisitedSites() { 201 MostVisitedSites::~MostVisitedSites() {
200 supervisor_->SetObserver(nullptr); 202 supervisor_->SetObserver(nullptr);
201 } 203 }
202 204
203 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer, 205 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer,
204 int num_sites) { 206 int num_sites) {
205 DCHECK(observer); 207 DCHECK(observer);
206 observer_ = observer; 208 observer_ = observer;
207 num_sites_ = num_sites; 209 num_sites_ = num_sites;
208 210
209 if (ShouldShowPopularSites() && 211 if (ShouldShowPopularSites() &&
210 NeedPopularSites(prefs_, num_sites_)) { 212 NeedPopularSites(prefs_, num_sites_)) {
211 popular_sites_.reset(new PopularSites( 213 popular_sites_.reset(new PopularSites(
212 prefs_, template_url_service_, variations_service_, download_context_, 214 blocking_pool_, prefs_, template_url_service_, variations_service_,
213 popular_sites_directory_, GetPopularSitesCountry(), 215 download_context_, popular_sites_directory_, GetPopularSitesCountry(),
214 GetPopularSitesVersion(), false, 216 GetPopularSitesVersion(), false,
215 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, 217 base::Bind(&MostVisitedSites::OnPopularSitesAvailable,
216 base::Unretained(this)))); 218 base::Unretained(this))));
217 } else { 219 } else {
218 received_popular_sites_ = true; 220 received_popular_sites_ = true;
219 } 221 }
220 222
221 // TODO(treib): Can |top_sites_| ever be null? If not, remove these checks. 223 // TODO(treib): Can |top_sites_| ever be null? If not, remove these checks.
222 if (top_sites_) { 224 if (top_sites_) {
223 // TopSites updates itself after a delay. To ensure up-to-date results, 225 // TopSites updates itself after a delay. To ensure up-to-date results,
(...skipping 11 matching lines...) Expand all
235 237
236 // Immediately build the current suggestions, getting personal suggestions 238 // Immediately build the current suggestions, getting personal suggestions
237 // from the SuggestionsService's cache or, if that is empty, from TopSites. 239 // from the SuggestionsService's cache or, if that is empty, from TopSites.
238 BuildCurrentSuggestions(); 240 BuildCurrentSuggestions();
239 // Also start a request for fresh suggestions. 241 // Also start a request for fresh suggestions.
240 suggestions_service_->FetchSuggestionsData(); 242 suggestions_service_->FetchSuggestionsData();
241 } 243 }
242 244
243 void MostVisitedSites::GetURLThumbnail(const GURL& url, 245 void MostVisitedSites::GetURLThumbnail(const GURL& url,
244 const ThumbnailCallback& callback) { 246 const ThumbnailCallback& callback) {
245 DCHECK_CURRENTLY_ON(BrowserThread::UI); 247 DCHECK(thread_checker_.CalledOnValidThread());
246 248
247 // TODO(treib): Move this to the blocking pool? Doesn't seem related to DB. 249 base::PostTaskAndReplyWithResult(
248 BrowserThread::PostTaskAndReplyWithResult( 250 blocking_runner_.get(), FROM_HERE,
249 BrowserThread::DB, FROM_HERE,
250 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites_), 251 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites_),
251 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, 252 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched,
252 weak_ptr_factory_.GetWeakPtr(), url, callback)); 253 weak_ptr_factory_.GetWeakPtr(), url, callback));
253 } 254 }
254 255
255 void MostVisitedSites::OnLocalThumbnailFetched( 256 void MostVisitedSites::OnLocalThumbnailFetched(
256 const GURL& url, 257 const GURL& url,
257 const ThumbnailCallback& callback, 258 const ThumbnailCallback& callback,
258 std::unique_ptr<SkBitmap> bitmap) { 259 std::unique_ptr<SkBitmap> bitmap) {
259 DCHECK_CURRENTLY_ON(BrowserThread::UI); 260 DCHECK(thread_checker_.CalledOnValidThread());
260 if (bitmap.get()) { 261 if (bitmap.get()) {
261 callback.Run(true /* is_local_thumbnail */, bitmap.get()); 262 callback.Run(true /* is_local_thumbnail */, bitmap.get());
262 return; 263 return;
263 } 264 }
264 265
265 // A thumbnail is not locally available for |url|. Make sure it is put in 266 // A thumbnail is not locally available for |url|. Make sure it is put in
266 // the list to be fetched at the next visit to this site. 267 // the list to be fetched at the next visit to this site.
267 if (top_sites_) 268 if (top_sites_)
268 top_sites_->AddForcedURL(url, base::Time::Now()); 269 top_sites_->AddForcedURL(url, base::Time::Now());
269 // Also fetch a remote thumbnail if possible. PopularSites or the 270 // Also fetch a remote thumbnail if possible. PopularSites or the
(...skipping 17 matching lines...) Expand all
287 } 288 }
288 // If no bitmap could be fetched and neither PopularSites nor the 289 // If no bitmap could be fetched and neither PopularSites nor the
289 // SuggestionsService is available then a nullptr is passed to the callback. 290 // SuggestionsService is available then a nullptr is passed to the callback.
290 callback.Run(true /* is_local_thumbnail */, nullptr); 291 callback.Run(true /* is_local_thumbnail */, nullptr);
291 } 292 }
292 293
293 void MostVisitedSites::OnObtainedThumbnail(bool is_local_thumbnail, 294 void MostVisitedSites::OnObtainedThumbnail(bool is_local_thumbnail,
294 const ThumbnailCallback& callback, 295 const ThumbnailCallback& callback,
295 const GURL& url, 296 const GURL& url,
296 const gfx::Image& image) { 297 const gfx::Image& image) {
297 DCHECK_CURRENTLY_ON(BrowserThread::UI); 298 DCHECK(thread_checker_.CalledOnValidThread());
298 const SkBitmap* bitmap = nullptr; 299 const SkBitmap* bitmap = nullptr;
299 if (!image.IsEmpty()) 300 if (!image.IsEmpty())
300 bitmap = image.ToSkBitmap(); 301 bitmap = image.ToSkBitmap();
301 callback.Run(is_local_thumbnail, bitmap); 302 callback.Run(is_local_thumbnail, bitmap);
302 } 303 }
303 304
304 void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url, 305 void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url,
305 bool add_url) { 306 bool add_url) {
306 // Always blacklist in the local TopSites. 307 // Always blacklist in the local TopSites.
307 if (top_sites_) { 308 if (top_sites_) {
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 654
654 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} 655 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {}
655 656
656 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, 657 void MostVisitedSites::TopSitesChanged(TopSites* top_sites,
657 ChangeReason change_reason) { 658 ChangeReason change_reason) {
658 if (mv_source_ == TOP_SITES) { 659 if (mv_source_ == TOP_SITES) {
659 // The displayed suggestions are invalidated. 660 // The displayed suggestions are invalidated.
660 InitiateTopSitesQuery(); 661 InitiateTopSitesQuery();
661 } 662 }
662 } 663 }
OLDNEW
« no previous file with comments | « chrome/browser/android/ntp/most_visited_sites.h ('k') | chrome/browser/android/ntp/most_visited_sites_bridge.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698