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

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: thread checker Created 4 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 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 weak_ptr_factory_(this) { 195 weak_ptr_factory_(this) {
196 supervisor_->SetObserver(this); 196 supervisor_->SetObserver(this);
197 } 197 }
198 198
199 MostVisitedSites::~MostVisitedSites() { 199 MostVisitedSites::~MostVisitedSites() {
200 supervisor_->SetObserver(nullptr); 200 supervisor_->SetObserver(nullptr);
201 } 201 }
202 202
203 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer, 203 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer,
204 int num_sites) { 204 int num_sites) {
205 DCHECK(observer); 205 DCHECK(observer);
206 observer_ = observer; 206 observer_ = observer;
207 num_sites_ = num_sites; 207 num_sites_ = num_sites;
208 208
209 if (ShouldShowPopularSites() && 209 if (ShouldShowPopularSites() &&
210 NeedPopularSites(prefs_, num_sites_)) { 210 NeedPopularSites(prefs_, num_sites_)) {
211 popular_sites_.reset(new PopularSites( 211 popular_sites_.reset(new PopularSites(
212 prefs_, template_url_service_, variations_service_, download_context_, 212 blocking_pool_, prefs_, template_url_service_, variations_service_,
213 popular_sites_directory_, GetPopularSitesCountry(), 213 download_context_, popular_sites_directory_, GetPopularSitesCountry(),
214 GetPopularSitesVersion(), false, 214 GetPopularSitesVersion(), false,
215 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, 215 base::Bind(&MostVisitedSites::OnPopularSitesAvailable,
216 base::Unretained(this)))); 216 base::Unretained(this))));
217 } else { 217 } else {
218 received_popular_sites_ = true; 218 received_popular_sites_ = true;
219 } 219 }
220 220
221 // TODO(treib): Can |top_sites_| ever be null? If not, remove these checks. 221 // TODO(treib): Can |top_sites_| ever be null? If not, remove these checks.
222 if (top_sites_) { 222 if (top_sites_) {
223 // TopSites updates itself after a delay. To ensure up-to-date results, 223 // TopSites updates itself after a delay. To ensure up-to-date results,
(...skipping 11 matching lines...) Expand all
235 235
236 // Immediately build the current suggestions, getting personal suggestions 236 // Immediately build the current suggestions, getting personal suggestions
237 // from the SuggestionsService's cache or, if that is empty, from TopSites. 237 // from the SuggestionsService's cache or, if that is empty, from TopSites.
238 BuildCurrentSuggestions(); 238 BuildCurrentSuggestions();
239 // Also start a request for fresh suggestions. 239 // Also start a request for fresh suggestions.
240 suggestions_service_->FetchSuggestionsData(); 240 suggestions_service_->FetchSuggestionsData();
241 } 241 }
242 242
243 void MostVisitedSites::GetURLThumbnail(const GURL& url, 243 void MostVisitedSites::GetURLThumbnail(const GURL& url,
244 const ThumbnailCallback& callback) { 244 const ThumbnailCallback& callback) {
245 DCHECK_CURRENTLY_ON(BrowserThread::UI); 245 DCHECK(thread_checker_.CalledOnValidThread());
246 246
247 // TODO(treib): Move this to the blocking pool? Doesn't seem related to DB. 247 base::PostTaskAndReplyWithResult(
248 BrowserThread::PostTaskAndReplyWithResult( 248 blocking_pool_
blundell 2016/05/25 09:00:24 Usually I would suggest making behavioral changes
249 BrowserThread::DB, FROM_HERE, 249 ->GetTaskRunnerWithShutdownBehavior(
Bernhard Bauer 2016/05/25 17:02:49 Sorry to barge in again here -- this creates a new
sfiera 2016/05/27 10:37:42 OK, done. Though that's in addition to the SWP, wh
250 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites_), 250 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)
251 .get(),
252 FROM_HERE, base::Bind(&MaybeFetchLocalThumbnail, url, top_sites_),
251 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, 253 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched,
252 weak_ptr_factory_.GetWeakPtr(), url, callback)); 254 weak_ptr_factory_.GetWeakPtr(), url, callback));
253 } 255 }
254 256
255 void MostVisitedSites::OnLocalThumbnailFetched( 257 void MostVisitedSites::OnLocalThumbnailFetched(
256 const GURL& url, 258 const GURL& url,
257 const ThumbnailCallback& callback, 259 const ThumbnailCallback& callback,
258 std::unique_ptr<SkBitmap> bitmap) { 260 std::unique_ptr<SkBitmap> bitmap) {
259 DCHECK_CURRENTLY_ON(BrowserThread::UI); 261 DCHECK(thread_checker_.CalledOnValidThread());
260 if (!bitmap.get()) { 262 if (!bitmap.get()) {
261 // A thumbnail is not locally available for |url|. Make sure it is put in 263 // A thumbnail is not locally available for |url|. Make sure it is put in
262 // the list to be fetched at the next visit to this site. 264 // the list to be fetched at the next visit to this site.
263 if (top_sites_) 265 if (top_sites_)
264 top_sites_->AddForcedURL(url, base::Time::Now()); 266 top_sites_->AddForcedURL(url, base::Time::Now());
265 // Also fetch a remote thumbnail if possible. PopularSites or the 267 // Also fetch a remote thumbnail if possible. PopularSites or the
266 // SuggestionsService can supply a thumbnail download URL. 268 // SuggestionsService can supply a thumbnail download URL.
267 if (popular_sites_) { 269 if (popular_sites_) {
268 const std::vector<PopularSites::Site>& sites = popular_sites_->sites(); 270 const std::vector<PopularSites::Site>& sites = popular_sites_->sites();
269 auto it = std::find_if( 271 auto it = std::find_if(
(...skipping 12 matching lines...) Expand all
282 weak_ptr_factory_.GetWeakPtr(), false, callback)); 284 weak_ptr_factory_.GetWeakPtr(), false, callback));
283 } 285 }
284 } 286 }
285 OnObtainedThumbnail(true, callback, url, bitmap.get()); 287 OnObtainedThumbnail(true, callback, url, bitmap.get());
286 } 288 }
287 289
288 void MostVisitedSites::OnObtainedThumbnail(bool is_local_thumbnail, 290 void MostVisitedSites::OnObtainedThumbnail(bool is_local_thumbnail,
289 const ThumbnailCallback& callback, 291 const ThumbnailCallback& callback,
290 const GURL& url, 292 const GURL& url,
291 const SkBitmap* bitmap) { 293 const SkBitmap* bitmap) {
292 DCHECK_CURRENTLY_ON(BrowserThread::UI); 294 DCHECK(thread_checker_.CalledOnValidThread());
293 callback.Run(is_local_thumbnail, bitmap); 295 callback.Run(is_local_thumbnail, bitmap);
294 } 296 }
295 297
296 void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url, 298 void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url,
297 bool add_url) { 299 bool add_url) {
298 // Always blacklist in the local TopSites. 300 // Always blacklist in the local TopSites.
299 if (top_sites_) { 301 if (top_sites_) {
300 if (add_url) 302 if (add_url)
301 top_sites_->AddBlacklistedURL(url); 303 top_sites_->AddBlacklistedURL(url);
302 else 304 else
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
645 647
646 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} 648 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {}
647 649
648 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, 650 void MostVisitedSites::TopSitesChanged(TopSites* top_sites,
649 ChangeReason change_reason) { 651 ChangeReason change_reason) {
650 if (mv_source_ == TOP_SITES) { 652 if (mv_source_ == TOP_SITES) {
651 // The displayed suggestions are invalidated. 653 // The displayed suggestions are invalidated.
652 InitiateTopSitesQuery(); 654 InitiateTopSitesQuery();
653 } 655 }
654 } 656 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698