Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: components/ntp_tiles/icon_cacher_impl.cc

Issue 2695713004: Add baked-in favicons for default popular sites on NTP (Closed)
Patch Set: Add default resource attribute to Site Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 28 matching lines...) Expand all
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(bool)>& done,
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 done.Run(false);
59 return; 60 return;
60 } 61 }
61 62
63 if (ProvideDefaultIcon(site)) {
64 done.Run(true);
65 // Don't return. We want to fetch to update possibly stale images.
66 }
67
62 image_fetcher_->StartOrQueueNetworkRequest( 68 image_fetcher_->StartOrQueueNetworkRequest(
63 std::string(), IconURL(site), 69 std::string(), IconURL(site),
64 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this), 70 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this),
65 site, done)); 71 site, done));
66 } 72 }
67 73
68 void IconCacherImpl::OnFaviconDownloaded(PopularSites::Site site, 74 void IconCacherImpl::OnFaviconDownloaded(PopularSites::Site site,
69 const base::Callback<void(bool)>& done, 75 const base::Callback<void(bool)>& done,
70 const std::string& id, 76 const std::string& id,
71 const gfx::Image& fetched_image) { 77 const gfx::Image& fetched_image) {
72 if (fetched_image.IsEmpty()) { 78 if (fetched_image.IsEmpty()) {
73 done.Run(false); 79 done.Run(false);
74 return; 80 return;
75 } 81 }
76 82
77 gfx::Image image = fetched_image; 83 SaveIconForSite(site, fetched_image);
78 favicon_base::SetFaviconColorSpace(&image);
79 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image);
80 done.Run(true); 84 done.Run(true);
81 } 85 }
82 86
87 void IconCacherImpl::SaveIconForSite(const PopularSites::Site& site,
88 gfx::Image image) {
89 favicon_base::SetFaviconColorSpace(&image);
90 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image);
91 }
92
93 bool IconCacherImpl::ProvideDefaultIcon(const PopularSites::Site& site) {
94 if (site.default_resource_id < 0) {
95 return false;
96 }
97 SaveIconForSite(site, ResourceBundle::GetSharedInstance().GetImageNamed(
98 site.default_resource_id));
99 return true;
100 }
101
83 } // namespace ntp_tiles 102 } // namespace ntp_tiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698