Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 DB thread. | |
| 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 Loading... | |
| 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 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, | |
| 171 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread, | |
| 172 const scoped_refptr<base::TaskRunner>& blocking_runner, | |
| 172 PrefService* prefs, | 173 PrefService* prefs, |
| 173 const TemplateURLService* template_url_service, | 174 const TemplateURLService* template_url_service, |
| 174 variations::VariationsService* variations_service, | 175 variations::VariationsService* variations_service, |
| 175 net::URLRequestContextGetter* download_context, | 176 net::URLRequestContextGetter* download_context, |
| 176 const base::FilePath& popular_sites_directory, | 177 const base::FilePath& popular_sites_directory, |
| 177 scoped_refptr<history::TopSites> top_sites, | 178 scoped_refptr<history::TopSites> top_sites, |
| 178 SuggestionsService* suggestions, | 179 SuggestionsService* suggestions, |
| 179 MostVisitedSitesSupervisor* supervisor) | 180 MostVisitedSitesSupervisor* supervisor) |
| 180 : prefs_(prefs), | 181 : prefs_(prefs), |
| 181 template_url_service_(template_url_service), | 182 template_url_service_(template_url_service), |
| 182 variations_service_(variations_service), | 183 variations_service_(variations_service), |
| 183 download_context_(download_context), | 184 download_context_(download_context), |
| 184 popular_sites_directory_(popular_sites_directory), | 185 popular_sites_directory_(popular_sites_directory), |
| 185 top_sites_(top_sites), | 186 top_sites_(top_sites), |
| 186 suggestions_service_(suggestions), | 187 suggestions_service_(suggestions), |
| 187 supervisor_(supervisor), | 188 supervisor_(supervisor), |
| 188 observer_(nullptr), | 189 observer_(nullptr), |
| 189 num_sites_(0), | 190 num_sites_(0), |
| 190 received_most_visited_sites_(false), | 191 received_most_visited_sites_(false), |
| 191 received_popular_sites_(false), | 192 received_popular_sites_(false), |
| 192 recorded_uma_(false), | 193 recorded_uma_(false), |
| 193 scoped_observer_(this), | 194 scoped_observer_(this), |
| 194 mv_source_(SUGGESTIONS_SERVICE), | 195 mv_source_(SUGGESTIONS_SERVICE), |
| 196 ui_thread_(ui_thread), | |
| 197 db_thread_(db_thread), | |
| 198 blocking_runner_(blocking_runner), | |
| 195 weak_ptr_factory_(this) { | 199 weak_ptr_factory_(this) { |
| 196 supervisor_->SetObserver(this); | 200 supervisor_->SetObserver(this); |
| 197 } | 201 } |
| 198 | 202 |
| 199 MostVisitedSites::~MostVisitedSites() { | 203 MostVisitedSites::~MostVisitedSites() { |
| 200 supervisor_->SetObserver(nullptr); | 204 supervisor_->SetObserver(nullptr); |
| 201 } | 205 } |
| 202 | 206 |
| 203 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer, | 207 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer, |
| 204 int num_sites) { | 208 int num_sites) { |
| 205 DCHECK(observer); | 209 DCHECK(observer); |
| 206 observer_ = observer; | 210 observer_ = observer; |
| 207 num_sites_ = num_sites; | 211 num_sites_ = num_sites; |
| 208 | 212 |
| 209 if (ShouldShowPopularSites() && | 213 if (ShouldShowPopularSites() && |
| 210 NeedPopularSites(prefs_, num_sites_)) { | 214 NeedPopularSites(prefs_, num_sites_)) { |
| 211 popular_sites_.reset(new PopularSites( | 215 popular_sites_.reset(new PopularSites( |
| 212 prefs_, template_url_service_, variations_service_, download_context_, | 216 ui_thread_, blocking_runner_, prefs_, template_url_service_, |
| 213 popular_sites_directory_, GetPopularSitesCountry(), | 217 variations_service_, download_context_, popular_sites_directory_, |
| 214 GetPopularSitesVersion(), false, | 218 GetPopularSitesCountry(), GetPopularSitesVersion(), false, |
| 215 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, | 219 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, |
| 216 base::Unretained(this)))); | 220 base::Unretained(this)))); |
| 217 } else { | 221 } else { |
| 218 received_popular_sites_ = true; | 222 received_popular_sites_ = true; |
| 219 } | 223 } |
| 220 | 224 |
| 221 // TODO(treib): Can |top_sites_| ever be null? If not, remove these checks. | 225 // TODO(treib): Can |top_sites_| ever be null? If not, remove these checks. |
| 222 if (top_sites_) { | 226 if (top_sites_) { |
| 223 // TopSites updates itself after a delay. To ensure up-to-date results, | 227 // TopSites updates itself after a delay. To ensure up-to-date results, |
| 224 // force an update now. | 228 // force an update now. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 235 | 239 |
| 236 // Immediately build the current suggestions, getting personal suggestions | 240 // Immediately build the current suggestions, getting personal suggestions |
| 237 // from the SuggestionsService's cache or, if that is empty, from TopSites. | 241 // from the SuggestionsService's cache or, if that is empty, from TopSites. |
| 238 BuildCurrentSuggestions(); | 242 BuildCurrentSuggestions(); |
| 239 // Also start a request for fresh suggestions. | 243 // Also start a request for fresh suggestions. |
| 240 suggestions_service_->FetchSuggestionsData(); | 244 suggestions_service_->FetchSuggestionsData(); |
| 241 } | 245 } |
| 242 | 246 |
| 243 void MostVisitedSites::GetURLThumbnail(const GURL& url, | 247 void MostVisitedSites::GetURLThumbnail(const GURL& url, |
| 244 const ThumbnailCallback& callback) { | 248 const ThumbnailCallback& callback) { |
| 245 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 249 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 246 | 250 |
| 247 // TODO(treib): Move this to the blocking pool? Doesn't seem related to DB. | 251 // TODO(treib): Move this to the blocking pool? Doesn't seem related to DB. |
|
Marc Treib
2016/05/24 15:16:11
Hm, maybe now is the time to resolve this TODO, so
sfiera
2016/05/24 16:59:20
Done.
| |
| 248 BrowserThread::PostTaskAndReplyWithResult( | 252 base::PostTaskAndReplyWithResult( |
| 249 BrowserThread::DB, FROM_HERE, | 253 db_thread_.get(), FROM_HERE, |
| 250 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites_), | 254 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites_), |
| 251 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, | 255 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, |
| 252 weak_ptr_factory_.GetWeakPtr(), url, callback)); | 256 weak_ptr_factory_.GetWeakPtr(), url, callback)); |
| 253 } | 257 } |
| 254 | 258 |
| 255 void MostVisitedSites::OnLocalThumbnailFetched( | 259 void MostVisitedSites::OnLocalThumbnailFetched( |
| 256 const GURL& url, | 260 const GURL& url, |
| 257 const ThumbnailCallback& callback, | 261 const ThumbnailCallback& callback, |
| 258 std::unique_ptr<SkBitmap> bitmap) { | 262 std::unique_ptr<SkBitmap> bitmap) { |
| 259 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 263 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 260 if (!bitmap.get()) { | 264 if (!bitmap.get()) { |
| 261 // A thumbnail is not locally available for |url|. Make sure it is put in | 265 // 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. | 266 // the list to be fetched at the next visit to this site. |
| 263 if (top_sites_) | 267 if (top_sites_) |
| 264 top_sites_->AddForcedURL(url, base::Time::Now()); | 268 top_sites_->AddForcedURL(url, base::Time::Now()); |
| 265 // Also fetch a remote thumbnail if possible. PopularSites or the | 269 // Also fetch a remote thumbnail if possible. PopularSites or the |
| 266 // SuggestionsService can supply a thumbnail download URL. | 270 // SuggestionsService can supply a thumbnail download URL. |
| 267 if (popular_sites_) { | 271 if (popular_sites_) { |
| 268 const std::vector<PopularSites::Site>& sites = popular_sites_->sites(); | 272 const std::vector<PopularSites::Site>& sites = popular_sites_->sites(); |
| 269 auto it = std::find_if( | 273 auto it = std::find_if( |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 282 weak_ptr_factory_.GetWeakPtr(), false, callback)); | 286 weak_ptr_factory_.GetWeakPtr(), false, callback)); |
| 283 } | 287 } |
| 284 } | 288 } |
| 285 OnObtainedThumbnail(true, callback, url, bitmap.get()); | 289 OnObtainedThumbnail(true, callback, url, bitmap.get()); |
| 286 } | 290 } |
| 287 | 291 |
| 288 void MostVisitedSites::OnObtainedThumbnail(bool is_local_thumbnail, | 292 void MostVisitedSites::OnObtainedThumbnail(bool is_local_thumbnail, |
| 289 const ThumbnailCallback& callback, | 293 const ThumbnailCallback& callback, |
| 290 const GURL& url, | 294 const GURL& url, |
| 291 const SkBitmap* bitmap) { | 295 const SkBitmap* bitmap) { |
| 292 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 296 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 293 callback.Run(is_local_thumbnail, bitmap); | 297 callback.Run(is_local_thumbnail, bitmap); |
| 294 } | 298 } |
| 295 | 299 |
| 296 void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url, | 300 void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url, |
| 297 bool add_url) { | 301 bool add_url) { |
| 298 // Always blacklist in the local TopSites. | 302 // Always blacklist in the local TopSites. |
| 299 if (top_sites_) { | 303 if (top_sites_) { |
| 300 if (add_url) | 304 if (add_url) |
| 301 top_sites_->AddBlacklistedURL(url); | 305 top_sites_->AddBlacklistedURL(url); |
| 302 else | 306 else |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 645 | 649 |
| 646 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} | 650 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} |
| 647 | 651 |
| 648 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 652 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
| 649 ChangeReason change_reason) { | 653 ChangeReason change_reason) { |
| 650 if (mv_source_ == TOP_SITES) { | 654 if (mv_source_ == TOP_SITES) { |
| 651 // The displayed suggestions are invalidated. | 655 // The displayed suggestions are invalidated. |
| 652 InitiateTopSitesQuery(); | 656 InitiateTopSitesQuery(); |
| 653 } | 657 } |
| 654 } | 658 } |
| OLD | NEW |