OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/icon_cacher_impl.h" | 5 #include "components/ntp_tiles/icon_cacher_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "components/favicon/core/favicon_service.h" | 9 #include "components/favicon/core/favicon_service.h" |
10 #include "components/favicon/core/favicon_util.h" | 10 #include "components/favicon/core/favicon_util.h" |
11 #include "components/favicon_base/favicon_types.h" | 11 #include "components/favicon_base/favicon_types.h" |
12 #include "components/favicon_base/favicon_util.h" | 12 #include "components/favicon_base/favicon_util.h" |
13 #include "components/image_fetcher/image_fetcher.h" | 13 #include "components/image_fetcher/image_fetcher.h" |
| 14 #include "ui/base/resource/resource_bundle.h" |
14 #include "ui/gfx/image/image.h" | 15 #include "ui/gfx/image/image.h" |
15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
16 | 17 |
17 namespace ntp_tiles { | 18 namespace ntp_tiles { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 favicon_base::IconType IconType(const PopularSites::Site& site) { | 22 favicon_base::IconType IconType(const PopularSites::Site& site) { |
22 return site.large_icon_url.is_valid() ? favicon_base::TOUCH_ICON | 23 return site.large_icon_url.is_valid() ? favicon_base::TOUCH_ICON |
23 : favicon_base::FAVICON; | 24 : favicon_base::FAVICON; |
(...skipping 10 matching lines...) Expand all Loading... |
34 favicon::FaviconService* favicon_service, | 35 favicon::FaviconService* favicon_service, |
35 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) | 36 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) |
36 : favicon_service_(favicon_service), | 37 : favicon_service_(favicon_service), |
37 image_fetcher_(std::move(image_fetcher)) { | 38 image_fetcher_(std::move(image_fetcher)) { |
38 image_fetcher_->SetDataUseServiceName( | 39 image_fetcher_->SetDataUseServiceName( |
39 data_use_measurement::DataUseUserData::NTP_TILES); | 40 data_use_measurement::DataUseUserData::NTP_TILES); |
40 } | 41 } |
41 | 42 |
42 IconCacherImpl::~IconCacherImpl() = default; | 43 IconCacherImpl::~IconCacherImpl() = default; |
43 | 44 |
44 void IconCacherImpl::StartFetch(PopularSites::Site site, | 45 void IconCacherImpl::StartFetch( |
45 const base::Callback<void(bool)>& done) { | 46 PopularSites::Site site, |
| 47 const base::Callback<void(bool)>& icon_available) { |
46 favicon::GetFaviconImageForPageURL( | 48 favicon::GetFaviconImageForPageURL( |
47 favicon_service_, site.url, IconType(site), | 49 favicon_service_, site.url, IconType(site), |
48 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, | 50 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, |
49 base::Unretained(this), std::move(site), done), | 51 base::Unretained(this), std::move(site), icon_available), |
50 &tracker_); | 52 &tracker_); |
51 } | 53 } |
52 | 54 |
53 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( | 55 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( |
54 PopularSites::Site site, | 56 PopularSites::Site site, |
55 const base::Callback<void(bool)>& done, | 57 const base::Callback<void(bool)>& icon_available, |
56 const favicon_base::FaviconImageResult& result) { | 58 const favicon_base::FaviconImageResult& result) { |
57 if (!result.image.IsEmpty()) { | 59 if (!result.image.IsEmpty()) { |
58 done.Run(false); | 60 icon_available.Run(false); |
59 return; | 61 return; |
60 } | 62 } |
61 | 63 |
| 64 if (ProvideDefaultIcon(site)) { |
| 65 icon_available.Run(true); |
| 66 // Don't return. We want to fetch to update possibly stale images. |
| 67 } |
| 68 |
62 image_fetcher_->StartOrQueueNetworkRequest( | 69 image_fetcher_->StartOrQueueNetworkRequest( |
63 std::string(), IconURL(site), | 70 std::string(), IconURL(site), |
64 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this), | 71 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this), |
65 site, done)); | 72 site, icon_available)); |
66 } | 73 } |
67 | 74 |
68 void IconCacherImpl::OnFaviconDownloaded(PopularSites::Site site, | 75 void IconCacherImpl::OnFaviconDownloaded( |
69 const base::Callback<void(bool)>& done, | 76 PopularSites::Site site, |
70 const std::string& id, | 77 const base::Callback<void(bool)>& icon_available, |
71 const gfx::Image& fetched_image) { | 78 const std::string& id, |
| 79 const gfx::Image& fetched_image) { |
72 if (fetched_image.IsEmpty()) { | 80 if (fetched_image.IsEmpty()) { |
73 done.Run(false); | 81 icon_available.Run(false); |
74 return; | 82 return; |
75 } | 83 } |
76 | 84 |
77 gfx::Image image = fetched_image; | 85 SaveIconForSite(site, fetched_image); |
| 86 icon_available.Run(true); |
| 87 } |
| 88 |
| 89 void IconCacherImpl::SaveIconForSite(const PopularSites::Site& site, |
| 90 gfx::Image image) { |
78 favicon_base::SetFaviconColorSpace(&image); | 91 favicon_base::SetFaviconColorSpace(&image); |
79 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image); | 92 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image); |
80 done.Run(true); | 93 } |
| 94 |
| 95 bool IconCacherImpl::ProvideDefaultIcon(const PopularSites::Site& site) { |
| 96 if (site.default_resource_id < 0) { |
| 97 return false; |
| 98 } |
| 99 SaveIconForSite(site, ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
| 100 site.default_resource_id)); |
| 101 return true; |
81 } | 102 } |
82 | 103 |
83 } // namespace ntp_tiles | 104 } // namespace ntp_tiles |
OLD | NEW |