| 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 "components/ntp_tiles/most_visited_sites.h" | 5 #include "components/ntp_tiles/most_visited_sites.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 NONE, | 56 NONE, |
| 57 // The item displays a site's actual favicon or touch icon. | 57 // The item displays a site's actual favicon or touch icon. |
| 58 ICON_REAL, | 58 ICON_REAL, |
| 59 // The item displays a color derived from the site's favicon or touch icon. | 59 // The item displays a color derived from the site's favicon or touch icon. |
| 60 ICON_COLOR, | 60 ICON_COLOR, |
| 61 // The item displays a default gray box in place of an icon. | 61 // The item displays a default gray box in place of an icon. |
| 62 ICON_DEFAULT, | 62 ICON_DEFAULT, |
| 63 NUM_TILE_TYPES, | 63 NUM_TILE_TYPES, |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 // May only be called from blocking thread pool. | |
| 67 std::unique_ptr<SkBitmap> TryFetchLocalThumbnail( | |
| 68 const GURL& url, | |
| 69 const scoped_refptr<TopSites>& top_sites) { | |
| 70 scoped_refptr<base::RefCountedMemory> image; | |
| 71 std::unique_ptr<SkBitmap> bitmap; | |
| 72 if (top_sites->GetPageThumbnail(url, false, &image)) | |
| 73 bitmap = gfx::JPEGCodec::Decode(image->front(), image->size()); | |
| 74 return bitmap; | |
| 75 } | |
| 76 | |
| 77 // Log an event for a given |histogram| at a given element |position|. This | 66 // 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 | 67 // 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. | 68 // if the name of the histogram will change at a given call site. |
| 80 void LogHistogramEvent(const std::string& histogram, | 69 void LogHistogramEvent(const std::string& histogram, |
| 81 int position, | 70 int position, |
| 82 int num_sites) { | 71 int num_sites) { |
| 83 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( | 72 base::HistogramBase* counter = base::LinearHistogram::FactoryGet( |
| 84 histogram, | 73 histogram, |
| 85 1, | 74 1, |
| 86 num_sites, | 75 num_sites, |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 159 |
| 171 MostVisitedSites::MostVisitedSites( | 160 MostVisitedSites::MostVisitedSites( |
| 172 scoped_refptr<base::SequencedWorkerPool> blocking_pool, | 161 scoped_refptr<base::SequencedWorkerPool> blocking_pool, |
| 173 PrefService* prefs, | 162 PrefService* prefs, |
| 174 const TemplateURLService* template_url_service, | 163 const TemplateURLService* template_url_service, |
| 175 variations::VariationsService* variations_service, | 164 variations::VariationsService* variations_service, |
| 176 net::URLRequestContextGetter* download_context, | 165 net::URLRequestContextGetter* download_context, |
| 177 const base::FilePath& popular_sites_directory, | 166 const base::FilePath& popular_sites_directory, |
| 178 scoped_refptr<history::TopSites> top_sites, | 167 scoped_refptr<history::TopSites> top_sites, |
| 179 SuggestionsService* suggestions, | 168 SuggestionsService* suggestions, |
| 180 MostVisitedSitesSupervisor* supervisor) | 169 MostVisitedSitesSupervisor* supervisor, |
| 170 image_fetcher::ImageDecoder* image_decoder) |
| 181 : prefs_(prefs), | 171 : prefs_(prefs), |
| 182 template_url_service_(template_url_service), | 172 template_url_service_(template_url_service), |
| 183 variations_service_(variations_service), | 173 variations_service_(variations_service), |
| 184 download_context_(download_context), | 174 download_context_(download_context), |
| 185 popular_sites_directory_(popular_sites_directory), | 175 popular_sites_directory_(popular_sites_directory), |
| 186 top_sites_(top_sites), | 176 top_sites_(top_sites), |
| 187 suggestions_service_(suggestions), | 177 suggestions_service_(suggestions), |
| 188 supervisor_(supervisor), | 178 supervisor_(supervisor), |
| 179 image_decoder_(image_decoder), |
| 189 observer_(nullptr), | 180 observer_(nullptr), |
| 190 num_sites_(0), | 181 num_sites_(0), |
| 191 received_most_visited_sites_(false), | 182 received_most_visited_sites_(false), |
| 192 received_popular_sites_(false), | 183 received_popular_sites_(false), |
| 193 recorded_uma_(false), | 184 recorded_uma_(false), |
| 194 scoped_observer_(this), | 185 scoped_observer_(this), |
| 195 mv_source_(SUGGESTIONS_SERVICE), | 186 mv_source_(SUGGESTIONS_SERVICE), |
| 196 blocking_pool_(std::move(blocking_pool)), | 187 blocking_pool_(std::move(blocking_pool)), |
| 197 blocking_runner_(blocking_pool_->GetTaskRunnerWithShutdownBehavior( | 188 blocking_runner_(blocking_pool_->GetTaskRunnerWithShutdownBehavior( |
| 198 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), | 189 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, | 227 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, |
| 237 base::Unretained(this))); | 228 base::Unretained(this))); |
| 238 | 229 |
| 239 // Immediately build the current suggestions, getting personal suggestions | 230 // Immediately build the current suggestions, getting personal suggestions |
| 240 // from the SuggestionsService's cache or, if that is empty, from TopSites. | 231 // from the SuggestionsService's cache or, if that is empty, from TopSites. |
| 241 BuildCurrentSuggestions(); | 232 BuildCurrentSuggestions(); |
| 242 // Also start a request for fresh suggestions. | 233 // Also start a request for fresh suggestions. |
| 243 suggestions_service_->FetchSuggestionsData(); | 234 suggestions_service_->FetchSuggestionsData(); |
| 244 } | 235 } |
| 245 | 236 |
| 246 void MostVisitedSites::GetURLThumbnail(const GURL& url, | |
| 247 const ThumbnailCallback& callback) { | |
| 248 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 249 | |
| 250 base::PostTaskAndReplyWithResult( | |
| 251 blocking_runner_.get(), FROM_HERE, | |
| 252 base::Bind(&TryFetchLocalThumbnail, url, top_sites_), | |
| 253 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, | |
| 254 weak_ptr_factory_.GetWeakPtr(), url, callback)); | |
| 255 } | |
| 256 | |
| 257 void MostVisitedSites::OnLocalThumbnailFetched( | |
| 258 const GURL& url, | |
| 259 const ThumbnailCallback& callback, | |
| 260 std::unique_ptr<SkBitmap> bitmap) { | |
| 261 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 262 if (bitmap.get()) { | |
| 263 callback.Run(true /* is_local_thumbnail */, bitmap.get()); | |
| 264 return; | |
| 265 } | |
| 266 | |
| 267 // A thumbnail is not locally available for |url|. Make sure it is put in | |
| 268 // the list to be fetched at the next visit to this site. | |
| 269 top_sites_->AddForcedURL(url, base::Time::Now()); | |
| 270 // Also fetch a remote thumbnail if possible. PopularSites or the | |
| 271 // SuggestionsService can supply a thumbnail download URL. | |
| 272 if (popular_sites_) { | |
| 273 const std::vector<PopularSites::Site>& sites = popular_sites_->sites(); | |
| 274 auto it = std::find_if( | |
| 275 sites.begin(), sites.end(), | |
| 276 [&url](const PopularSites::Site& site) { return site.url == url; }); | |
| 277 if (it != sites.end() && it->thumbnail_url.is_valid()) { | |
| 278 return suggestions_service_->GetPageThumbnailWithURL( | |
| 279 url, it->thumbnail_url, | |
| 280 base::Bind(&MostVisitedSites::OnObtainedThumbnail, | |
| 281 weak_ptr_factory_.GetWeakPtr(), false, callback)); | |
| 282 } | |
| 283 } | |
| 284 if (mv_source_ == SUGGESTIONS_SERVICE) { | |
| 285 return suggestions_service_->GetPageThumbnail( | |
| 286 url, base::Bind(&MostVisitedSites::OnObtainedThumbnail, | |
| 287 weak_ptr_factory_.GetWeakPtr(), false, callback)); | |
| 288 } | |
| 289 // If no bitmap could be fetched and neither PopularSites nor the | |
| 290 // SuggestionsService is available then a nullptr is passed to the callback. | |
| 291 callback.Run(true /* is_local_thumbnail */, nullptr); | |
| 292 } | |
| 293 | |
| 294 void MostVisitedSites::OnObtainedThumbnail(bool is_local_thumbnail, | 237 void MostVisitedSites::OnObtainedThumbnail(bool is_local_thumbnail, |
| 295 const ThumbnailCallback& callback, | 238 const ThumbnailCallback& callback, |
| 296 const GURL& url, | 239 const GURL& url, |
| 297 const gfx::Image& image) { | 240 const gfx::Image& image) { |
| 298 DCHECK(thread_checker_.CalledOnValidThread()); | 241 DCHECK(thread_checker_.CalledOnValidThread()); |
| 242 // TODO(markusheintz): Move the code for converting to SkBitmap to the |
| 243 // callback. |
| 299 const SkBitmap* bitmap = nullptr; | 244 const SkBitmap* bitmap = nullptr; |
| 300 if (!image.IsEmpty()) | 245 if (!image.IsEmpty()) |
| 301 bitmap = image.ToSkBitmap(); | 246 bitmap = image.ToSkBitmap(); |
| 302 callback.Run(is_local_thumbnail, bitmap); | 247 callback.Run(is_local_thumbnail, bitmap); |
| 303 } | 248 } |
| 304 | 249 |
| 305 void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url, | 250 void MostVisitedSites::AddOrRemoveBlacklistedUrl(const GURL& url, |
| 306 bool add_url) { | 251 bool add_url) { |
| 307 // Always blacklist in the local TopSites. | 252 // Always blacklist in the local TopSites. |
| 308 if (add_url) | 253 if (add_url) |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 | 594 |
| 650 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} | 595 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} |
| 651 | 596 |
| 652 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 597 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
| 653 ChangeReason change_reason) { | 598 ChangeReason change_reason) { |
| 654 if (mv_source_ == TOP_SITES) { | 599 if (mv_source_ == TOP_SITES) { |
| 655 // The displayed suggestions are invalidated. | 600 // The displayed suggestions are invalidated. |
| 656 InitiateTopSitesQuery(); | 601 InitiateTopSitesQuery(); |
| 657 } | 602 } |
| 658 } | 603 } |
| OLD | NEW |