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 11 matching lines...) Expand all Loading... |
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(PopularSites::Site site, |
45 const base::Callback<void(bool)>& done) { | 46 const base::Callback<void()>& icon_available) { |
46 favicon::GetFaviconImageForPageURL( | 47 favicon::GetFaviconImageForPageURL( |
47 favicon_service_, site.url, IconType(site), | 48 favicon_service_, site.url, IconType(site), |
48 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, | 49 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, |
49 base::Unretained(this), std::move(site), done), | 50 base::Unretained(this), std::move(site), icon_available), |
50 &tracker_); | 51 &tracker_); |
51 } | 52 } |
52 | 53 |
53 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( | 54 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( |
54 PopularSites::Site site, | 55 PopularSites::Site site, |
55 const base::Callback<void(bool)>& done, | 56 const base::Callback<void()>& icon_available, |
56 const favicon_base::FaviconImageResult& result) { | 57 const favicon_base::FaviconImageResult& result) { |
57 if (!result.image.IsEmpty()) { | 58 if (!result.image.IsEmpty()) { |
58 done.Run(false); | |
59 return; | 59 return; |
60 } | 60 } |
61 | 61 |
| 62 if (ProvideDefaultIcon(site)) { |
| 63 icon_available.Run(); |
| 64 // Don't return. We want to fetch to update possibly stale images. |
| 65 } |
| 66 |
62 image_fetcher_->StartOrQueueNetworkRequest( | 67 image_fetcher_->StartOrQueueNetworkRequest( |
63 std::string(), IconURL(site), | 68 std::string(), IconURL(site), |
64 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this), | 69 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this), |
65 site, done)); | 70 site, icon_available)); |
66 } | 71 } |
67 | 72 |
68 void IconCacherImpl::OnFaviconDownloaded(PopularSites::Site site, | 73 void IconCacherImpl::OnFaviconDownloaded( |
69 const base::Callback<void(bool)>& done, | 74 PopularSites::Site site, |
70 const std::string& id, | 75 const base::Callback<void()>& icon_available, |
71 const gfx::Image& fetched_image) { | 76 const std::string& id, |
| 77 const gfx::Image& fetched_image) { |
72 if (fetched_image.IsEmpty()) { | 78 if (fetched_image.IsEmpty()) { |
73 done.Run(false); | |
74 return; | 79 return; |
75 } | 80 } |
76 | 81 |
77 gfx::Image image = fetched_image; | 82 SaveIconForSite(site, fetched_image); |
| 83 icon_available.Run(); |
| 84 } |
| 85 |
| 86 void IconCacherImpl::SaveIconForSite(const PopularSites::Site& site, |
| 87 gfx::Image image) { |
78 favicon_base::SetFaviconColorSpace(&image); | 88 favicon_base::SetFaviconColorSpace(&image); |
79 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image); | 89 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image); |
80 done.Run(true); | 90 } |
| 91 |
| 92 bool IconCacherImpl::ProvideDefaultIcon(const PopularSites::Site& site) { |
| 93 if (site.default_resource_id < 0) { |
| 94 return false; |
| 95 } |
| 96 SaveIconForSite(site, |
| 97 ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
| 98 site.default_resource_id)); |
| 99 return true; |
81 } | 100 } |
82 | 101 |
83 } // namespace ntp_tiles | 102 } // namespace ntp_tiles |
OLD | NEW |