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

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

Issue 1092873002: [Icons NTP] Refactor large_icon_source to extract the logic shared between desktop and Android to f… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 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 2015 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/ui/webui/large_icon_source.h" 5 #include "chrome/browser/ui/webui/large_icon_source.h"
6 6
7 #include <vector>
8
7 #include "base/memory/ref_counted_memory.h" 9 #include "base/memory/ref_counted_memory.h"
8 #include "chrome/browser/search/instant_io_context.h" 10 #include "chrome/browser/search/instant_io_context.h"
9 #include "chrome/common/favicon/large_icon_url_parser.h" 11 #include "chrome/common/favicon/large_icon_url_parser.h"
10 #include "chrome/common/url_constants.h" 12 #include "chrome/common/url_constants.h"
11 #include "components/favicon/core/fallback_icon_service.h" 13 #include "components/favicon/core/fallback_icon_service.h"
12 #include "components/favicon/core/favicon_service.h" 14 #include "components/favicon/core/large_icon_service.h"
13 #include "components/favicon_base/fallback_icon_style.h" 15 #include "components/favicon_base/fallback_icon_style.h"
14 #include "net/url_request/url_request.h" 16 #include "net/url_request/url_request.h"
15 #include "third_party/skia/include/core/SkColor.h"
16 #include "ui/gfx/color_analysis.h"
17 #include "ui/gfx/color_utils.h"
18 17
19 namespace { 18 namespace {
20 19
21 const int kDefaultLargeIconSize = 96;
22 const int kMaxLargeIconSize = 192; // Arbitrary bound to safeguard endpoint. 20 const int kMaxLargeIconSize = 192; // Arbitrary bound to safeguard endpoint.
23 21
24 const double kMaxBackgroundLuminance = 0.67;
25 const SkColor kDarkGray = SkColorSetRGB(0x78, 0x78, 0x78);
26 const SkColor kTextColor = SK_ColorWHITE;
27 const SkColor kDefaultBackgroundColor = kDarkGray;
28
29 } // namespace 22 } // namespace
30 23
31 LargeIconSource::IconRequest::IconRequest() : size(kDefaultLargeIconSize) {
32 }
33
34 LargeIconSource::IconRequest::IconRequest(
35 const content::URLDataSource::GotDataCallback& callback_in,
36 const GURL& url_in,
37 int size_in)
38 : callback(callback_in),
39 url(url_in),
40 size(size_in) {
41 }
42
43 LargeIconSource::IconRequest::~IconRequest() {
44 }
45
46 LargeIconSource::LargeIconSource( 24 LargeIconSource::LargeIconSource(
47 favicon::FaviconService* favicon_service, 25 favicon::FallbackIconService* fallback_icon_service,
48 favicon::FallbackIconService* fallback_icon_service) 26 favicon::LargeIconService* large_icon_service)
49 : favicon_service_(favicon_service), 27 : fallback_icon_service_(fallback_icon_service),
50 fallback_icon_service_(fallback_icon_service) { 28 large_icon_service_(large_icon_service) {
51 large_icon_types_.push_back(favicon_base::IconType::FAVICON);
52 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON);
53 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON);
54 } 29 }
55 30
56 LargeIconSource::~LargeIconSource() { 31 LargeIconSource::~LargeIconSource() {
57 } 32 }
58 33
59 std::string LargeIconSource::GetSource() const { 34 std::string LargeIconSource::GetSource() const {
60 return chrome::kChromeUILargeIconHost; 35 return chrome::kChromeUILargeIconHost;
61 } 36 }
62 37
63 void LargeIconSource::StartDataRequest( 38 void LargeIconSource::StartDataRequest(
64 const std::string& path, 39 const std::string& path,
65 int render_process_id, 40 int render_process_id,
66 int render_frame_id, 41 int render_frame_id,
67 const content::URLDataSource::GotDataCallback& callback) { 42 const content::URLDataSource::GotDataCallback& callback) {
68 if (!favicon_service_) { 43 if (!large_icon_service_) {
69 SendNotFoundResponse(callback); 44 SendNotFoundResponse(callback);
70 return; 45 return;
71 } 46 }
72 47
73 LargeIconUrlParser parser; 48 LargeIconUrlParser parser;
74 bool success = parser.Parse(path); 49 bool success = parser.Parse(path);
75 if (!success || 50 if (!success ||
76 parser.size_in_pixels() <= 0 || 51 parser.size_in_pixels() <= 0 ||
77 parser.size_in_pixels() > kMaxLargeIconSize) { 52 parser.size_in_pixels() > kMaxLargeIconSize) {
78 SendNotFoundResponse(callback); 53 SendNotFoundResponse(callback);
79 return; 54 return;
80 } 55 }
81 56
82 GURL url(parser.url_string()); 57 GURL url(parser.url_string());
83 if (!url.is_valid()) { 58 if (!url.is_valid()) {
84 SendNotFoundResponse(callback); 59 SendNotFoundResponse(callback);
85 return; 60 return;
86 } 61 }
87 62
88 favicon_service_->GetLargestRawFaviconForPageURL( 63 large_icon_service_->GetLargeIconOrFallbackStyle(
89 url, 64 url,
90 large_icon_types_,
91 parser.size_in_pixels(), 65 parser.size_in_pixels(),
92 base::Bind( 66 base::Bind(&LargeIconSource::OnLargeIconDataAvailable,
93 &LargeIconSource::OnIconDataAvailable, 67 base::Unretained(this), callback, url,
94 base::Unretained(this), 68 parser.size_in_pixels()),
95 IconRequest(callback, url, parser.size_in_pixels())),
96 &cancelable_task_tracker_); 69 &cancelable_task_tracker_);
97 } 70 }
98 71
99 std::string LargeIconSource::GetMimeType(const std::string&) const { 72 std::string LargeIconSource::GetMimeType(const std::string&) const {
100 // We need to explicitly return a mime type, otherwise if the user tries to 73 // We need to explicitly return a mime type, otherwise if the user tries to
101 // drag the image they get no extension. 74 // drag the image they get no extension.
102 return "image/png"; 75 return "image/png";
103 } 76 }
104 77
105 bool LargeIconSource::ShouldReplaceExistingSource() const { 78 bool LargeIconSource::ShouldReplaceExistingSource() const {
106 // Leave the existing DataSource in place, otherwise we'll drop any pending 79 // Leave the existing DataSource in place, otherwise we'll drop any pending
107 // requests on the floor. 80 // requests on the floor.
108 return false; 81 return false;
109 } 82 }
110 83
111 bool LargeIconSource::ShouldServiceRequest( 84 bool LargeIconSource::ShouldServiceRequest(
112 const net::URLRequest* request) const { 85 const net::URLRequest* request) const {
113 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) 86 if (request->url().SchemeIs(chrome::kChromeSearchScheme))
114 return InstantIOContext::ShouldServiceRequest(request); 87 return InstantIOContext::ShouldServiceRequest(request);
115 return URLDataSource::ShouldServiceRequest(request); 88 return URLDataSource::ShouldServiceRequest(request);
116 } 89 }
117 90
118 void LargeIconSource::OnIconDataAvailable( 91 void LargeIconSource::OnLargeIconDataAvailable(
119 const IconRequest& request, 92 const content::URLDataSource::GotDataCallback& callback,
120 const favicon_base::FaviconRawBitmapResult& bitmap_result) { 93 const GURL& url,
121 if (!bitmap_result.is_valid()) { 94 int size,
122 SendDefaultFallbackIcon(request); 95 const favicon_base::LargeIconResult& result) {
96 if (result.bitmap.is_valid()) {
97 callback.Run(result.bitmap.bitmap_data.get());
123 return; 98 return;
124 } 99 }
125 100
126 // If we found a bitmap, but it's smaller than the requested size, we 101 // Bitmap is invalid, use the fallback if service is available.
127 // generate a fallback using the dominant color from the too-small bitmap. 102 if (!fallback_icon_service_ || !result.fallback_icon_style) {
128 // We adjust the luminance of the background so we can put light text over it. 103 SendNotFoundResponse(callback);
129 if (bitmap_result.pixel_size.width() < request.size ||
130 bitmap_result.pixel_size.height() < request.size) {
131 SkColor background =
132 color_utils::CalculateKMeanColorOfPNG(bitmap_result.bitmap_data);
133 color_utils::HSL background_hsl;
134 color_utils::SkColorToHSL(background, &background_hsl);
135 background_hsl.l = std::min(background_hsl.l, kMaxBackgroundLuminance);
136 background = color_utils::HSLToSkColor(background_hsl, SK_AlphaOPAQUE);
137
138 // Now we can construct the fallback icon.
139 SendFallbackIcon(request, kTextColor, background);
140 return; 104 return;
141 } 105 }
142
143 request.callback.Run(bitmap_result.bitmap_data.get());
144 }
145
146 void LargeIconSource::SendDefaultFallbackIcon(const IconRequest& request) {
147 SendFallbackIcon(request, kTextColor, kDefaultBackgroundColor);
148 }
149
150 void LargeIconSource::SendFallbackIcon(const IconRequest& request,
151 SkColor text_color,
152 SkColor background_color) {
153 if (!fallback_icon_service_) {
154 SendNotFoundResponse(request.callback);
155 return;
156 }
157 favicon_base::FallbackIconStyle style;
158 style.background_color = background_color;
159 style.text_color = text_color;
160 style.font_size_ratio = 0.44;
161 style.roundness = 0; // Square. Round corners can be applied by JavaScript.
162 std::vector<unsigned char> bitmap_data = 106 std::vector<unsigned char> bitmap_data =
163 fallback_icon_service_->RenderFallbackIconBitmap( 107 fallback_icon_service_->RenderFallbackIconBitmap(
164 request.url, request.size, style); 108 url, size, *result.fallback_icon_style);
165 request.callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); 109 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data));
166 } 110 }
167 111
168 void LargeIconSource::SendNotFoundResponse( 112 void LargeIconSource::SendNotFoundResponse(
169 const content::URLDataSource::GotDataCallback& callback) { 113 const content::URLDataSource::GotDataCallback& callback) {
170 callback.Run(nullptr); 114 callback.Run(nullptr);
171 } 115 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698