OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/extensions/bookmark_app_helper.h" | 5 #include "chrome/browser/extensions/bookmark_app_helper.h" |
6 | 6 |
7 #include <cctype> | 7 #include <cctype> |
8 | 8 |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 bitmaps_it->second, | 146 bitmaps_it->second, |
147 skia::ImageOperations::RESIZE_LANCZOS3, | 147 skia::ImageOperations::RESIZE_LANCZOS3, |
148 size, | 148 size, |
149 size); | 149 size); |
150 } | 150 } |
151 } | 151 } |
152 return output_bitmaps; | 152 return output_bitmaps; |
153 } | 153 } |
154 | 154 |
155 // static | 155 // static |
| 156 char BookmarkAppHelper::GetIconLetterForGeneratedIcon(const GURL& url) { |
| 157 char icon_letter = ' '; |
| 158 std::string domain_and_registry( |
| 159 net::registry_controlled_domains::GetDomainAndRegistry( |
| 160 url, |
| 161 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)); |
| 162 if (!domain_and_registry.empty()) { |
| 163 icon_letter = domain_and_registry[0]; |
| 164 } else if (!url.host().empty()) { |
| 165 icon_letter = url.host()[0]; |
| 166 } |
| 167 return icon_letter; |
| 168 } |
| 169 |
| 170 // static |
| 171 SkColor BookmarkAppHelper::GetIconColorForGeneratedIcon( |
| 172 const SkBitmap& base_icon) { |
| 173 color_utils::GridSampler sampler; |
| 174 SkColor background_color = color_utils::CalculateKMeanColorOfPNG( |
| 175 gfx::Image::CreateFrom1xBitmap(base_icon).As1xPNGBytes(), |
| 176 100, |
| 177 568, |
| 178 &sampler); |
| 179 return background_color; |
| 180 } |
| 181 |
| 182 // static |
156 void BookmarkAppHelper::GenerateIcon(std::map<int, SkBitmap>* bitmaps, | 183 void BookmarkAppHelper::GenerateIcon(std::map<int, SkBitmap>* bitmaps, |
157 int output_size, | 184 int output_size, |
158 SkColor color, | 185 SkColor color, |
159 char letter) { | 186 char letter) { |
160 // Do nothing if there is already an icon of |output_size|. | 187 // Do nothing if there is already an icon of |output_size|. |
161 if (bitmaps->count(output_size)) | 188 if (bitmaps->count(output_size)) |
162 return; | 189 return; |
163 | 190 |
164 gfx::ImageSkia icon_image( | 191 gfx::ImageSkia icon_image( |
165 new GeneratedIconImageSource(letter, color, output_size), | 192 new GeneratedIconImageSource(letter, color, output_size), |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 kIconSizesToGenerate, | 278 kIconSizesToGenerate, |
252 kIconSizesToGenerate + arraysize(kIconSizesToGenerate)); | 279 kIconSizesToGenerate + arraysize(kIconSizesToGenerate)); |
253 | 280 |
254 // Only generate icons if larger icons don't exist. This means the app | 281 // Only generate icons if larger icons don't exist. This means the app |
255 // launcher and the taskbar will do their best downsizing large icons and | 282 // launcher and the taskbar will do their best downsizing large icons and |
256 // these icons are only generated as a last resort against upscaling a smaller | 283 // these icons are only generated as a last resort against upscaling a smaller |
257 // icon. | 284 // icon. |
258 if (resized_bitmaps.lower_bound(*generate_sizes.rbegin()) == | 285 if (resized_bitmaps.lower_bound(*generate_sizes.rbegin()) == |
259 resized_bitmaps.end()) { | 286 resized_bitmaps.end()) { |
260 GURL app_url = web_app_info_.app_url; | 287 GURL app_url = web_app_info_.app_url; |
| 288 char icon_letter = GetIconLetterForGeneratedIcon(app_url); |
| 289 SkColor background_color = SK_ColorBLACK; |
261 | 290 |
262 // The letter that will be painted on the generated icon. | |
263 char icon_letter = ' '; | |
264 std::string domain_and_registry( | |
265 net::registry_controlled_domains::GetDomainAndRegistry( | |
266 app_url, | |
267 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)); | |
268 if (!domain_and_registry.empty()) { | |
269 icon_letter = domain_and_registry[0]; | |
270 } else if (!app_url.host().empty()) { | |
271 icon_letter = app_url.host()[0]; | |
272 } | |
273 | |
274 // The color that will be used for the icon's background. | |
275 SkColor background_color = SK_ColorBLACK; | |
276 if (resized_bitmaps.size()) { | 291 if (resized_bitmaps.size()) { |
277 color_utils::GridSampler sampler; | 292 background_color = GetIconColorForGeneratedIcon( |
278 background_color = color_utils::CalculateKMeanColorOfPNG( | 293 resized_bitmaps.begin()->second); |
279 gfx::Image::CreateFrom1xBitmap(resized_bitmaps.begin()->second) | |
280 .As1xPNGBytes(), | |
281 100, | |
282 568, | |
283 &sampler); | |
284 } | 294 } |
285 | 295 |
286 for (std::set<int>::const_iterator it = generate_sizes.begin(); | 296 for (std::set<int>::const_iterator it = generate_sizes.begin(); |
287 it != generate_sizes.end(); | 297 it != generate_sizes.end(); |
288 ++it) { | 298 ++it) { |
289 GenerateIcon(&resized_bitmaps, *it, background_color, icon_letter); | 299 GenerateIcon(&resized_bitmaps, *it, background_color, icon_letter); |
290 // Also generate the 2x resource for this size. | 300 // Also generate the 2x resource for this size. |
291 GenerateIcon(&resized_bitmaps, *it * 2, background_color, icon_letter); | 301 GenerateIcon(&resized_bitmaps, *it * 2, background_color, icon_letter); |
292 } | 302 } |
293 } | 303 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 extension, info_list, base::Bind(&OnIconsLoaded, web_app_info, callback)); | 383 extension, info_list, base::Bind(&OnIconsLoaded, web_app_info, callback)); |
374 } | 384 } |
375 | 385 |
376 bool IsValidBookmarkAppUrl(const GURL& url) { | 386 bool IsValidBookmarkAppUrl(const GURL& url) { |
377 URLPattern origin_only_pattern(Extension::kValidWebExtentSchemes); | 387 URLPattern origin_only_pattern(Extension::kValidWebExtentSchemes); |
378 origin_only_pattern.SetMatchAllURLs(true); | 388 origin_only_pattern.SetMatchAllURLs(true); |
379 return url.is_valid() && origin_only_pattern.MatchesURL(url); | 389 return url.is_valid() && origin_only_pattern.MatchesURL(url); |
380 } | 390 } |
381 | 391 |
382 } // namespace extensions | 392 } // namespace extensions |
OLD | NEW |