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; |
24 } | 25 } |
25 | 26 |
26 const GURL& IconURL(const PopularSites::Site& site) { | 27 const GURL& IconURL(const PopularSites::Site& site) { |
27 return site.large_icon_url.is_valid() ? site.large_icon_url | 28 return site.large_icon_url.is_valid() ? site.large_icon_url |
28 : site.favicon_url; | 29 : site.favicon_url; |
29 } | 30 } |
30 | 31 |
31 } // namespace | 32 } // namespace |
32 | 33 |
| 34 struct IconCacherImpl::DefaultIcon { |
| 35 DefaultIcon(const PopularSites::Site& popular_site, int image_resource_id) |
| 36 : site(popular_site), image_resource(image_resource_id) {} |
| 37 |
| 38 PopularSites::Site site; |
| 39 int image_resource; |
| 40 }; |
| 41 |
33 IconCacherImpl::IconCacherImpl( | 42 IconCacherImpl::IconCacherImpl( |
34 favicon::FaviconService* favicon_service, | 43 favicon::FaviconService* favicon_service, |
35 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) | 44 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) |
36 : favicon_service_(favicon_service), | 45 : favicon_service_(favicon_service), |
37 image_fetcher_(std::move(image_fetcher)) { | 46 image_fetcher_(std::move(image_fetcher)) { |
38 image_fetcher_->SetDataUseServiceName( | 47 image_fetcher_->SetDataUseServiceName( |
39 data_use_measurement::DataUseUserData::NTP_TILES); | 48 data_use_measurement::DataUseUserData::NTP_TILES); |
40 } | 49 } |
41 | 50 |
42 IconCacherImpl::~IconCacherImpl() = default; | 51 IconCacherImpl::~IconCacherImpl() = default; |
43 | 52 |
44 void IconCacherImpl::StartFetch(PopularSites::Site site, | 53 void IconCacherImpl::StartFetch(PopularSites::Site site, |
45 const base::Callback<void(bool)>& done) { | 54 const base::Callback<void(bool)>& done) { |
46 favicon::GetFaviconImageForPageURL( | 55 favicon::GetFaviconImageForPageURL( |
47 favicon_service_, site.url, IconType(site), | 56 favicon_service_, site.url, IconType(site), |
48 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, | 57 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, |
49 base::Unretained(this), std::move(site), done), | 58 base::Unretained(this), std::move(site), done), |
50 &tracker_); | 59 &tracker_); |
51 } | 60 } |
52 | 61 |
53 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( | 62 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( |
54 PopularSites::Site site, | 63 PopularSites::Site site, |
55 const base::Callback<void(bool)>& done, | 64 const base::Callback<void(bool)>& done, |
56 const favicon_base::FaviconImageResult& result) { | 65 const favicon_base::FaviconImageResult& result) { |
57 if (!result.image.IsEmpty()) { | 66 if (!result.image.IsEmpty()) { |
58 done.Run(false); | 67 done.Run(false); |
59 return; | 68 return; |
60 } | 69 } |
61 | 70 |
| 71 if (ProvideDefaultIcon(site)) { |
| 72 done.Run(true); |
| 73 // Don't return. We want to fetch to update possibly stale images. |
| 74 } |
| 75 |
62 image_fetcher_->StartOrQueueNetworkRequest( | 76 image_fetcher_->StartOrQueueNetworkRequest( |
63 std::string(), IconURL(site), | 77 std::string(), IconURL(site), |
64 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this), | 78 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this), |
65 site, done)); | 79 site, done)); |
66 } | 80 } |
67 | 81 |
68 void IconCacherImpl::OnFaviconDownloaded(PopularSites::Site site, | 82 void IconCacherImpl::OnFaviconDownloaded(PopularSites::Site site, |
69 const base::Callback<void(bool)>& done, | 83 const base::Callback<void(bool)>& done, |
70 const std::string& id, | 84 const std::string& id, |
71 const gfx::Image& fetched_image) { | 85 const gfx::Image& fetched_image) { |
72 if (fetched_image.IsEmpty()) { | 86 if (fetched_image.IsEmpty()) { |
73 done.Run(false); | 87 done.Run(false); |
74 return; | 88 return; |
75 } | 89 } |
76 | 90 |
77 gfx::Image image = fetched_image; | 91 SaveIconForSite(site, fetched_image); |
78 favicon_base::SetFaviconColorSpace(&image); | |
79 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image); | |
80 done.Run(true); | 92 done.Run(true); |
81 } | 93 } |
82 | 94 |
| 95 void IconCacherImpl::SaveIconForSite(const PopularSites::Site& site, |
| 96 gfx::Image image) { |
| 97 favicon_base::SetFaviconColorSpace(&image); |
| 98 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image); |
| 99 } |
| 100 |
| 101 bool IconCacherImpl::ProvideDefaultIcon(const PopularSites::Site& site) { |
| 102 auto it = default_icons_.find(site.url.spec()); |
| 103 if (it == default_icons_.end()) { |
| 104 return false; |
| 105 } |
| 106 const DefaultIcon& icon = it->second; |
| 107 SaveIconForSite(icon.site, ResourceBundle::GetSharedInstance().GetImageNamed( |
| 108 icon.image_resource)); |
| 109 return true; |
| 110 } |
| 111 |
| 112 void IconCacherImpl::AddDefaultImage(const PopularSites::Site& site, |
| 113 int image_resource_id) { |
| 114 default_icons_.insert( |
| 115 std::make_pair(site.url.spec(), DefaultIcon(site, image_resource_id))); |
| 116 } |
| 117 |
83 } // namespace ntp_tiles | 118 } // namespace ntp_tiles |
OLD | NEW |