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