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

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/favicon_service.h"
15 #include "components/favicon/core/large_icon_service.h"
13 #include "components/favicon_base/fallback_icon_style.h" 16 #include "components/favicon_base/fallback_icon_style.h"
14 #include "net/url_request/url_request.h" 17 #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 18
19 namespace { 19 namespace {
20 20
21 const int kDefaultLargeIconSize = 96;
22 const int kMaxLargeIconSize = 192; // Arbitrary bound to safeguard endpoint. 21 const int kMaxLargeIconSize = 192; // Arbitrary bound to safeguard endpoint.
23 22
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 23 } // namespace
30 24
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( 25 LargeIconSource::LargeIconSource(
47 favicon::FaviconService* favicon_service, 26 favicon::FaviconService* favicon_service,
48 favicon::FallbackIconService* fallback_icon_service) 27 favicon::FallbackIconService* fallback_icon_service,
28 favicon::LargeIconService* large_icon_service)
49 : favicon_service_(favicon_service), 29 : favicon_service_(favicon_service),
50 fallback_icon_service_(fallback_icon_service) { 30 fallback_icon_service_(fallback_icon_service),
51 large_icon_types_.push_back(favicon_base::IconType::FAVICON); 31 large_icon_service_(large_icon_service) {
52 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON);
53 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON);
54 } 32 }
55 33
56 LargeIconSource::~LargeIconSource() { 34 LargeIconSource::~LargeIconSource() {
57 } 35 }
58 36
59 std::string LargeIconSource::GetSource() const { 37 std::string LargeIconSource::GetSource() const {
60 return chrome::kChromeUILargeIconHost; 38 return chrome::kChromeUILargeIconHost;
61 } 39 }
62 40
63 void LargeIconSource::StartDataRequest( 41 void LargeIconSource::StartDataRequest(
(...skipping 14 matching lines...) Expand all
78 SendNotFoundResponse(callback); 56 SendNotFoundResponse(callback);
79 return; 57 return;
80 } 58 }
81 59
82 GURL url(parser.url_string()); 60 GURL url(parser.url_string());
83 if (!url.is_valid()) { 61 if (!url.is_valid()) {
84 SendNotFoundResponse(callback); 62 SendNotFoundResponse(callback);
85 return; 63 return;
86 } 64 }
87 65
88 favicon_service_->GetLargestRawFaviconForPageURL( 66 large_icon_service_->GetLargeIconOrFallbackStyle(
89 url, 67 url,
90 large_icon_types_,
91 parser.size_in_pixels(), 68 parser.size_in_pixels(),
92 base::Bind( 69 base::Bind(&LargeIconSource::OnIconDataAvailable, base::Unretained(this),
93 &LargeIconSource::OnIconDataAvailable, 70 callback, url, parser.size_in_pixels()),
94 base::Unretained(this),
95 IconRequest(callback, url, parser.size_in_pixels())),
96 &cancelable_task_tracker_); 71 &cancelable_task_tracker_);
97 } 72 }
98 73
99 std::string LargeIconSource::GetMimeType(const std::string&) const { 74 std::string LargeIconSource::GetMimeType(const std::string&) const {
100 // We need to explicitly return a mime type, otherwise if the user tries to 75 // We need to explicitly return a mime type, otherwise if the user tries to
101 // drag the image they get no extension. 76 // drag the image they get no extension.
102 return "image/png"; 77 return "image/png";
103 } 78 }
104 79
105 bool LargeIconSource::ShouldReplaceExistingSource() const { 80 bool LargeIconSource::ShouldReplaceExistingSource() const {
106 // Leave the existing DataSource in place, otherwise we'll drop any pending 81 // Leave the existing DataSource in place, otherwise we'll drop any pending
107 // requests on the floor. 82 // requests on the floor.
108 return false; 83 return false;
109 } 84 }
110 85
111 bool LargeIconSource::ShouldServiceRequest( 86 bool LargeIconSource::ShouldServiceRequest(
112 const net::URLRequest* request) const { 87 const net::URLRequest* request) const {
113 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) 88 if (request->url().SchemeIs(chrome::kChromeSearchScheme))
114 return InstantIOContext::ShouldServiceRequest(request); 89 return InstantIOContext::ShouldServiceRequest(request);
115 return URLDataSource::ShouldServiceRequest(request); 90 return URLDataSource::ShouldServiceRequest(request);
116 } 91 }
117 92
118 void LargeIconSource::OnIconDataAvailable( 93 void LargeIconSource::OnIconDataAvailable(
119 const IconRequest& request, 94 const content::URLDataSource::GotDataCallback& callback,
120 const favicon_base::FaviconRawBitmapResult& bitmap_result) { 95 const GURL& url,
121 if (!bitmap_result.is_valid()) { 96 int size,
122 SendDefaultFallbackIcon(request); 97 const favicon_base::LargeIconResult& result) {
98 if (result.bitmap.is_valid()) {
99 callback.Run(result.bitmap.bitmap_data.get());
123 return; 100 return;
124 } 101 }
125 102
126 // If we found a bitmap, but it's smaller than the requested size, we 103 // Bitmap is invalid, use the fallback if service is available.
127 // generate a fallback using the dominant color from the too-small bitmap. 104 if (!fallback_icon_service_) {
128 // We adjust the luminance of the background so we can put light text over it. 105 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; 106 return;
141 } 107 }
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 = 108 std::vector<unsigned char> bitmap_data =
163 fallback_icon_service_->RenderFallbackIconBitmap( 109 fallback_icon_service_->RenderFallbackIconBitmap(
164 request.url, request.size, style); 110 url, size, result.fallback_icon_style);
165 request.callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); 111 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data));
166 } 112 }
167 113
168 void LargeIconSource::SendNotFoundResponse( 114 void LargeIconSource::SendNotFoundResponse(
169 const content::URLDataSource::GotDataCallback& callback) { 115 const content::URLDataSource::GotDataCallback& callback) {
170 callback.Run(nullptr); 116 callback.Run(nullptr);
171 } 117 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698