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

Side by Side Diff: chrome/browser/ui/webui/large_icon_source.cc

Issue 1016833003: [Icons NTP] Add chrome://large-icon host, which resizes icon and renders fallback. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 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
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/webui/large_icon_source.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/memory/ref_counted_memory.h"
11 #include "chrome/browser/favicon/favicon_service_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/search/instant_io_context.h"
14 #include "chrome/common/favicon/large_icon_url_parser.h"
15 #include "chrome/common/url_constants.h"
16 #include "components/favicon/core/browser/favicon_service.h"
17 #include "components/favicon_base/fallback_icon_style.h"
18 #include "grit/platform_locale_settings.h"
19 #include "net/url_request/url_request.h"
20 #include "skia/ext/image_operations.h"
21 #include "third_party/skia/include/core/SkColor.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/gfx/codec/png_codec.h"
24 #include "ui/gfx/geometry/size.h"
25 #include "ui/gfx/image/image_png_rep.h"
26 #include "ui/gfx/image/image_skia.h"
27
28 namespace {
29
30 int kDefaultLargeIconSize = 96;
31 int kMaxLargeIconSize = 512; // Arbitrary bound to safegard endpoint.
beaudoin 2015/03/17 23:08:36 Looks really big. I'd be fine limiting it more. Ac
huangs 2015/03/18 22:54:11 Done.
32
33 } // namespace
34
35 LargeIconSource::IconRequest::IconRequest() : size(kDefaultLargeIconSize) {
36 }
37
38 LargeIconSource::IconRequest::IconRequest(
39 const content::URLDataSource::GotDataCallback& callback_in,
40 const GURL& url_in,
41 int size_in)
42 : callback(callback_in),
43 url(url_in),
44 size(size_in) {
45 }
46
47 LargeIconSource::IconRequest::~IconRequest() {
48 }
49
50 LargeIconSource::LargeIconSource(Profile* profile)
51 : profile_(profile),
52 render_fallback_on_failure_(true) {
beaudoin 2015/03/17 23:08:36 Overdesign? All constructors set it to true: take
huangs 2015/03/18 22:54:11 Done.
53 std::vector<std::string> font_list;
54 #if defined(OS_CHROMEOS)
55 font_list.push_back("Noto Sans");
56 #elif defined(OS_IOS)
57 font_list.push_back("Helvetica Neue");
beaudoin 2015/03/17 23:08:36 Isn't there a better way to access the platform's
huangs 2015/03/18 22:54:11 That's the purpose of my refactoring CL https://ch
beaudoin 2015/03/19 01:50:18 Acknowledged.
58 #else
59 font_list.push_back(l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY));
60 #endif
61 fallback_icon_service_.reset(
62 new favicon_base::FallbackIconService(font_list));
63 }
64
65 LargeIconSource::~LargeIconSource() {
66 }
67
68 std::string LargeIconSource::GetSource() const {
69 return chrome::kChromeUILargeIconHost;
70 }
71
72 void LargeIconSource::StartDataRequest(
73 const std::string& path,
74 int render_process_id,
75 int render_frame_id,
76 const content::URLDataSource::GotDataCallback& callback) {
77 chrome::LargeIconUrlParser parser;
78 bool success = parser.Parse(path);
79 if (!success || parser.size_in_pixels() <= 0
80 || parser.size_in_pixels() > kMaxLargeIconSize) {
beaudoin 2015/03/17 23:08:36 More conventional in Chrome to have the || on the
huangs 2015/03/18 22:54:11 Done.
81 SendNotFoundResponse(callback);
82 return;
83 }
84
85 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile(
86 profile_, ServiceAccessType::EXPLICIT_ACCESS);
87 if (!favicon_service) {
88 SendNotFoundResponse(callback);
beaudoin 2015/03/17 23:08:36 Do we want to send a fallback icon in this case? (
huangs 2015/03/18 22:54:11 |favicon_service| this is an exceptional case. Be
beaudoin 2015/03/19 01:50:18 Acknowledged.
89 return;
90 }
91
92 GURL url(parser.url_string());
93 if (!url.is_valid()) {
94 SendNotFoundResponse(callback);
95 return;
96 }
97
98 favicon_service->GetGenericLargeIconForPageURL(
99 url,
100 parser.size_in_pixels(),
101 base::Bind(
102 &LargeIconSource::OnIconDataAvailable,
103 base::Unretained(this),
104 IconRequest(callback, url, parser.size_in_pixels())),
105 &cancelable_task_tracker_);
106 }
107
108 std::string LargeIconSource::GetMimeType(const std::string&) const {
109 // We need to explicitly return a mime type, otherwise if the user tries to
110 // drag the image they get no extension.
111 return "image/png";
112 }
113
114 bool LargeIconSource::ShouldReplaceExistingSource() const {
115 // Leave the existing DataSource in place, otherwise we'll drop any pending
116 // requests on the floor.
117 return false;
118 }
119
120 bool LargeIconSource::ShouldServiceRequest(
121 const net::URLRequest* request) const {
122 if (request->url().SchemeIs(chrome::kChromeSearchScheme))
123 return InstantIOContext::ShouldServiceRequest(request);
124 return URLDataSource::ShouldServiceRequest(request);
125 }
126
127 void LargeIconSource::OnIconDataAvailable(
128 const IconRequest& request,
129 const favicon_base::FaviconRawBitmapResult& bitmap_result) {
130 if (!bitmap_result.is_valid()) {
131 OnIconDataError(request);
132 return;
133 }
134 // Resize the retrieved large icon.
beaudoin 2015/03/17 23:08:36 Indentation.
huangs 2015/03/18 22:54:11 Can remove resizing code thanks to your insight in
beaudoin 2015/03/19 01:50:18 Acknowledged.
135 std::vector<gfx::ImagePNGRep> png_reps;
136 png_reps.push_back(gfx::ImagePNGRep(bitmap_result.bitmap_data, 1.0f));
137 gfx::Image image(png_reps);
138 if (image.IsEmpty()) {
139 OnIconDataError(request);
140 return;
141 }
142
143 SkBitmap resized_bitmap =
144 skia::ImageOperations::Resize(*image.ToSkBitmap(),
145 skia::ImageOperations::RESIZE_LANCZOS3,
146 request.size,
147 request.size);
148
149 // Send resized bitmap to the networking system.
150 std::vector<unsigned char> bitmap_data;
151 if (!gfx::PNGCodec::EncodeBGRASkBitmap(
152 resized_bitmap, false, &bitmap_data)) {
153 OnIconDataError(request);
154 return;
155 }
156
157 request.callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data));
158 }
159
160 void LargeIconSource::OnIconDataError(const IconRequest& request) {
161 if (render_fallback_on_failure_)
162 SendFallbackIcon(request);
163 else
164 SendNotFoundResponse(request.callback);
165 }
166
167
168 void LargeIconSource::SendFallbackIcon(const IconRequest& request) {
169 favicon_base::FallbackIconStyle style;
170 style.background_color = SkColorSetRGB(0xcc, 0xcc, 0xcc);
171 favicon_base::MatchFallbackIconTextColorAgainstBackgroundColor(&style);
172 style.roundness = 1.0;
173 std::vector<unsigned char> bitmap_data =
174 fallback_icon_service_->RenderFallbackIconBitmap(
175 request.url, request.size, style);
176 request.callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data));
177 }
178
179 void LargeIconSource::SendNotFoundResponse(
180 const content::URLDataSource::GotDataCallback& callback) {
181 callback.Run(nullptr);
182 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698