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

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

Powered by Google App Engine
This is Rietveld 408576698