Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
|
Marc Treib
2016/10/12 09:11:28
2016
sfiera
2016/10/13 09:06:45
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/ntp_tiles/icon_cacher.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
|
Marc Treib
2016/10/12 09:11:28
No need to repeat all the includes from the header
sfiera
2016/10/13 09:06:45
Took this as an opportunity to trim the includes i
| |
| 8 #include "base/memory/weak_ptr.h" | |
| 9 #include "components/favicon/core/favicon_service.h" | |
| 10 #include "components/favicon/core/favicon_util.h" | |
| 11 #include "components/favicon_base/favicon_types.h" | |
| 12 #include "components/favicon_base/favicon_util.h" | |
| 13 #include "components/image_fetcher/image_fetcher.h" | |
| 14 #include "components/ntp_tiles/popular_sites.h" | |
| 15 #include "ui/gfx/image/image_skia.h" // DO_NOT_SUBMIT | |
|
Marc Treib
2016/10/12 09:11:28
:)
sfiera
2016/10/13 09:06:45
:X
| |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace ntp_tiles { | |
| 19 | |
| 20 IconCacher::IconCacher( | |
| 21 favicon::FaviconService* favicon_service, | |
| 22 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) | |
| 23 : favicon_service_(favicon_service), | |
| 24 image_fetcher_(std::move(image_fetcher)), | |
| 25 weak_ptr_factory_(this) { | |
| 26 image_fetcher_->SetDataUseServiceName( | |
| 27 data_use_measurement::DataUseUserData::NTP_TILES); | |
|
Marc Treib
2016/10/12 09:11:28
:)
There's a TODO to add just this in popular_site
sfiera
2016/10/13 09:06:45
Done.
| |
| 28 } | |
| 29 | |
| 30 IconCacher::~IconCacher() = default; | |
| 31 | |
| 32 void IconCacher::StartFetch(PopularSites::Site site, | |
| 33 base::Callback<void(bool)> done) { | |
| 34 favicon::GetFaviconImageForPageURL( | |
| 35 favicon_service_, site.url, IconType(site), | |
| 36 base::Bind(&IconCacher::OnGetFaviconImageForPageURLFinished, | |
| 37 weak_ptr_factory_.GetWeakPtr(), std::move(site), done), | |
|
Marc Treib
2016/10/12 09:11:28
WeakPtr isn't necessary I think: the CancelableTas
sfiera
2016/10/13 09:06:45
Done.
| |
| 38 &tracker_); | |
| 39 } | |
| 40 | |
| 41 favicon_base::IconType IconCacher::IconType(const PopularSites::Site& site) { | |
| 42 return site.large_icon_url.is_valid() ? favicon_base::TOUCH_ICON | |
| 43 : favicon_base::FAVICON; | |
| 44 } | |
| 45 | |
| 46 const GURL& IconCacher::IconURL(const PopularSites::Site& site) { | |
| 47 return site.large_icon_url.is_valid() ? site.large_icon_url | |
| 48 : site.favicon_url; | |
| 49 } | |
| 50 | |
| 51 void IconCacher::OnGetFaviconImageForPageURLFinished( | |
| 52 PopularSites::Site site, | |
| 53 base::Callback<void(bool)> done, | |
| 54 const favicon_base::FaviconImageResult& result) { | |
| 55 if (!result.image.IsEmpty()) { | |
| 56 done.Run(false); | |
| 57 return; | |
| 58 } | |
| 59 | |
| 60 image_fetcher_->StartOrQueueNetworkRequest( | |
| 61 "", IconURL(site), | |
|
Marc Treib
2016/10/12 09:11:28
nit: s/""/std::string()/
sfiera
2016/10/13 09:06:45
Done.
| |
| 62 base::Bind(&IconCacher::OnFaviconDownloaded, | |
| 63 weak_ptr_factory_.GetWeakPtr(), site, done)); | |
|
Marc Treib
2016/10/12 09:11:28
WeakPtr isn't necessary I think (we own the ImageF
sfiera
2016/10/13 09:06:45
Done.
| |
| 64 } | |
| 65 | |
| 66 void IconCacher::OnFaviconDownloaded(PopularSites::Site site, | |
| 67 base::Callback<void(bool)> done, | |
| 68 const std::string& id, | |
| 69 const gfx::Image& fetched_image) { | |
| 70 if (fetched_image.IsEmpty()) { | |
| 71 done.Run(false); | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 gfx::Image image = fetched_image; | |
| 76 favicon_base::SetFaviconColorSpace(&image); | |
| 77 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image); | |
| 78 done.Run(true); | |
| 79 } | |
| 80 | |
| 81 } // namespace ntp_tiles | |
| OLD | NEW |