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/geometry/size.h" | 15 #include "ui/gfx/geometry/size.h" |
15 #include "ui/gfx/image/image.h" | 16 #include "ui/gfx/image/image.h" |
16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
17 | 18 |
18 namespace ntp_tiles { | 19 namespace ntp_tiles { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 favicon_base::IconType IconType(const PopularSites::Site& site) { | 23 favicon_base::IconType IconType(const PopularSites::Site& site) { |
23 return site.large_icon_url.is_valid() ? favicon_base::TOUCH_ICON | 24 return site.large_icon_url.is_valid() ? favicon_base::TOUCH_ICON |
(...skipping 13 matching lines...) Expand all Loading... |
37 : favicon_service_(favicon_service), | 38 : favicon_service_(favicon_service), |
38 image_fetcher_(std::move(image_fetcher)) { | 39 image_fetcher_(std::move(image_fetcher)) { |
39 image_fetcher_->SetDataUseServiceName( | 40 image_fetcher_->SetDataUseServiceName( |
40 data_use_measurement::DataUseUserData::NTP_TILES); | 41 data_use_measurement::DataUseUserData::NTP_TILES); |
41 // For images with multiple frames, prefer one of size 128x128px. | 42 // For images with multiple frames, prefer one of size 128x128px. |
42 image_fetcher_->SetDesiredImageFrameSize(gfx::Size(128, 128)); | 43 image_fetcher_->SetDesiredImageFrameSize(gfx::Size(128, 128)); |
43 } | 44 } |
44 | 45 |
45 IconCacherImpl::~IconCacherImpl() = default; | 46 IconCacherImpl::~IconCacherImpl() = default; |
46 | 47 |
47 void IconCacherImpl::StartFetch(PopularSites::Site site, | 48 void IconCacherImpl::StartFetch( |
48 const base::Callback<void(bool)>& done) { | 49 PopularSites::Site site, |
| 50 const base::Closure& icon_available, |
| 51 const base::Closure& preliminary_icon_available) { |
| 52 DCHECK(!icon_available.is_null()); |
49 favicon::GetFaviconImageForPageURL( | 53 favicon::GetFaviconImageForPageURL( |
50 favicon_service_, site.url, IconType(site), | 54 favicon_service_, site.url, IconType(site), |
51 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, | 55 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, |
52 base::Unretained(this), std::move(site), done), | 56 base::Unretained(this), std::move(site), icon_available, |
| 57 preliminary_icon_available), |
53 &tracker_); | 58 &tracker_); |
54 } | 59 } |
55 | 60 |
56 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( | 61 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( |
57 PopularSites::Site site, | 62 PopularSites::Site site, |
58 const base::Callback<void(bool)>& done, | 63 const base::Closure& icon_available, |
| 64 const base::Closure& preliminary_icon_available, |
59 const favicon_base::FaviconImageResult& result) { | 65 const favicon_base::FaviconImageResult& result) { |
60 if (!result.image.IsEmpty()) { | 66 if (!result.image.IsEmpty()) { |
61 done.Run(false); | |
62 return; | 67 return; |
63 } | 68 } |
| 69 if (ProvideDefaultIcon(site) && !preliminary_icon_available.is_null()) { |
| 70 preliminary_icon_available.Run(); |
| 71 } |
64 | 72 |
65 image_fetcher_->StartOrQueueNetworkRequest( | 73 image_fetcher_->StartOrQueueNetworkRequest( |
66 std::string(), IconURL(site), | 74 std::string(), IconURL(site), |
67 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this), | 75 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this), |
68 site, done)); | 76 site, icon_available)); |
69 } | 77 } |
70 | 78 |
71 void IconCacherImpl::OnFaviconDownloaded(PopularSites::Site site, | 79 void IconCacherImpl::OnFaviconDownloaded(PopularSites::Site site, |
72 const base::Callback<void(bool)>& done, | 80 const base::Closure& icon_available, |
73 const std::string& id, | 81 const std::string& id, |
74 const gfx::Image& fetched_image) { | 82 const gfx::Image& fetched_image) { |
75 if (fetched_image.IsEmpty()) { | 83 if (fetched_image.IsEmpty()) { |
76 done.Run(false); | |
77 return; | 84 return; |
78 } | 85 } |
79 | 86 |
80 gfx::Image image = fetched_image; | 87 SaveIconForSite(site, fetched_image); |
| 88 icon_available.Run(); |
| 89 } |
| 90 |
| 91 void IconCacherImpl::SaveIconForSite(const PopularSites::Site& site, |
| 92 gfx::Image image) { |
81 favicon_base::SetFaviconColorSpace(&image); | 93 favicon_base::SetFaviconColorSpace(&image); |
82 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image); | 94 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image); |
83 done.Run(true); | 95 } |
| 96 |
| 97 bool IconCacherImpl::ProvideDefaultIcon(const PopularSites::Site& site) { |
| 98 if (site.default_icon_resource < 0) { |
| 99 return false; |
| 100 } |
| 101 SaveIconForSite(site, ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
| 102 site.default_icon_resource)); |
| 103 return true; |
84 } | 104 } |
85 | 105 |
86 } // namespace ntp_tiles | 106 } // namespace ntp_tiles |
OLD | NEW |