OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/web_applications/web_app_ui.h" | 5 #include "chrome/browser/ui/web_applications/web_app_ui.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/browser/notification_details.h" | 23 #include "content/public/browser/notification_details.h" |
24 #include "content/public/browser/notification_registrar.h" | 24 #include "content/public/browser/notification_registrar.h" |
25 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
26 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
27 #include "googleurl/src/gurl.h" | 27 #include "googleurl/src/gurl.h" |
28 #include "grit/theme_resources.h" | 28 #include "grit/theme_resources.h" |
29 #include "skia/ext/image_operations.h" | 29 #include "skia/ext/image_operations.h" |
30 #include "third_party/skia/include/core/SkBitmap.h" | 30 #include "third_party/skia/include/core/SkBitmap.h" |
31 #include "ui/base/resource/resource_bundle.h" | 31 #include "ui/base/resource/resource_bundle.h" |
| 32 #include "ui/gfx/image/image.h" |
| 33 #include "ui/gfx/image/image_family.h" |
| 34 #include "ui/gfx/image/image_skia.h" |
32 | 35 |
33 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 36 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
34 #include "base/environment.h" | 37 #include "base/environment.h" |
35 #endif | 38 #endif |
36 | 39 |
37 #if defined(OS_WIN) | 40 #if defined(OS_WIN) |
38 #include "base/win/shortcut.h" | 41 #include "base/win/shortcut.h" |
39 #include "base/win/windows_version.h" | 42 #include "base/win/windows_version.h" |
40 #endif | 43 #endif |
41 | 44 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 std::vector<ui::ScaleFactor> scale_factors; | 191 std::vector<ui::ScaleFactor> scale_factors; |
189 scale_factors.push_back(ui::SCALE_FACTOR_100P); | 192 scale_factors.push_back(ui::SCALE_FACTOR_100P); |
190 | 193 |
191 size_t closest_index = | 194 size_t closest_index = |
192 FaviconUtil::SelectBestFaviconFromBitmaps(bitmaps, | 195 FaviconUtil::SelectBestFaviconFromBitmaps(bitmaps, |
193 scale_factors, | 196 scale_factors, |
194 requested_size); | 197 requested_size); |
195 | 198 |
196 if (!bitmaps.empty() && !bitmaps[closest_index].isNull()) { | 199 if (!bitmaps.empty() && !bitmaps[closest_index].isNull()) { |
197 // Update icon with download image and update shortcut. | 200 // Update icon with download image and update shortcut. |
198 shortcut_info_.favicon = | 201 shortcut_info_.favicon.Add( |
199 gfx::Image::CreateFrom1xBitmap(bitmaps[closest_index]); | 202 gfx::Image::CreateFrom1xBitmap(bitmaps[closest_index])); |
200 extensions::TabHelper* extensions_tab_helper = | 203 extensions::TabHelper* extensions_tab_helper = |
201 extensions::TabHelper::FromWebContents(web_contents_); | 204 extensions::TabHelper::FromWebContents(web_contents_); |
202 extensions_tab_helper->SetAppIcon(bitmaps[closest_index]); | 205 extensions_tab_helper->SetAppIcon(bitmaps[closest_index]); |
203 UpdateShortcuts(); | 206 UpdateShortcuts(); |
204 } else { | 207 } else { |
205 // Try the next icon otherwise. | 208 // Try the next icon otherwise. |
206 DownloadIcon(); | 209 DownloadIcon(); |
207 } | 210 } |
208 } | 211 } |
209 | 212 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 // Ensure web_app_path exists. web_app_path could be missing for a legacy | 266 // Ensure web_app_path exists. web_app_path could be missing for a legacy |
264 // shortcut created by Gears. | 267 // shortcut created by Gears. |
265 if (!file_util::PathExists(web_app_path) && | 268 if (!file_util::PathExists(web_app_path) && |
266 !file_util::CreateDirectory(web_app_path)) { | 269 !file_util::CreateDirectory(web_app_path)) { |
267 NOTREACHED(); | 270 NOTREACHED(); |
268 return; | 271 return; |
269 } | 272 } |
270 | 273 |
271 base::FilePath icon_file = web_app_path.Append(file_name_).ReplaceExtension( | 274 base::FilePath icon_file = web_app_path.Append(file_name_).ReplaceExtension( |
272 FILE_PATH_LITERAL(".ico")); | 275 FILE_PATH_LITERAL(".ico")); |
273 web_app::internals::CheckAndSaveIcon(icon_file, | 276 web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info_.favicon); |
274 *shortcut_info_.favicon.ToSkBitmap()); | |
275 | 277 |
276 // Update existing shortcuts' description, icon and app id. | 278 // Update existing shortcuts' description, icon and app id. |
277 CheckExistingShortcuts(); | 279 CheckExistingShortcuts(); |
278 if (!shortcut_files_.empty()) { | 280 if (!shortcut_files_.empty()) { |
279 // Generates app id from web app url and profile path. | 281 // Generates app id from web app url and profile path. |
280 string16 app_id = ShellIntegration::GetAppModelIdForProfile( | 282 string16 app_id = ShellIntegration::GetAppModelIdForProfile( |
281 UTF8ToWide(web_app::GenerateApplicationNameFromURL(shortcut_info_.url)), | 283 UTF8ToWide(web_app::GenerateApplicationNameFromURL(shortcut_info_.url)), |
282 profile_path_); | 284 profile_path_); |
283 | 285 |
284 // Sanitize description | 286 // Sanitize description |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 const gfx::Image& image) { | 327 const gfx::Image& image) { |
326 // If the image failed to load (e.g. if the resource being loaded was empty) | 328 // If the image failed to load (e.g. if the resource being loaded was empty) |
327 // use the standard application icon. | 329 // use the standard application icon. |
328 if (image.IsEmpty()) { | 330 if (image.IsEmpty()) { |
329 gfx::Image default_icon = | 331 gfx::Image default_icon = |
330 ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); | 332 ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); |
331 int size = kDesiredSizes[arraysize(kDesiredSizes) - 1]; | 333 int size = kDesiredSizes[arraysize(kDesiredSizes) - 1]; |
332 SkBitmap bmp = skia::ImageOperations::Resize( | 334 SkBitmap bmp = skia::ImageOperations::Resize( |
333 *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, | 335 *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, |
334 size, size); | 336 size, size); |
335 shortcut_info.favicon = gfx::Image::CreateFrom1xBitmap(bmp); | 337 shortcut_info.favicon.Add(gfx::Image::CreateFrom1xBitmap(bmp)); |
336 } else { | 338 } else { |
337 shortcut_info.favicon = image; | 339 // As described in UpdateShortcutInfoAndIconForApp, image contains all of |
| 340 // the icons, hackily put into a single ImageSkia. Separate them out into |
| 341 // individual ImageSkias and insert them into the icon family. |
| 342 const gfx::ImageSkia& multires_image_skia = image.AsImageSkia(); |
| 343 // NOTE: We do not call ImageSkia::EnsureRepsForSupportedScaleFactors here. |
| 344 // The image reps here are not really for different scale factors (ImageSkia |
| 345 // is just being used as a handy container for multiple images). |
| 346 std::vector<gfx::ImageSkiaRep> image_reps = |
| 347 multires_image_skia.image_reps(); |
| 348 for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin(); |
| 349 it != image_reps.end(); ++it) { |
| 350 gfx::ImageSkia image_skia(*it); |
| 351 image_skia.MakeThreadSafe(); |
| 352 shortcut_info.favicon.Add(image_skia); |
| 353 } |
338 } | 354 } |
339 | 355 |
340 callback.Run(shortcut_info); | 356 callback.Run(shortcut_info); |
341 } | 357 } |
342 | 358 |
343 } // namespace | 359 } // namespace |
344 | 360 |
345 namespace web_app { | 361 namespace web_app { |
346 | 362 |
347 ShellIntegration::ShortcutInfo ShortcutInfoForExtensionAndProfile( | 363 ShellIntegration::ShortcutInfo ShortcutInfoForExtensionAndProfile( |
(...skipping 13 matching lines...) Expand all Loading... |
361 extensions::TabHelper::FromWebContents(web_contents); | 377 extensions::TabHelper::FromWebContents(web_contents); |
362 const WebApplicationInfo& app_info = extensions_tab_helper->web_app_info(); | 378 const WebApplicationInfo& app_info = extensions_tab_helper->web_app_info(); |
363 | 379 |
364 info->url = app_info.app_url.is_empty() ? web_contents->GetURL() : | 380 info->url = app_info.app_url.is_empty() ? web_contents->GetURL() : |
365 app_info.app_url; | 381 app_info.app_url; |
366 info->title = app_info.title.empty() ? | 382 info->title = app_info.title.empty() ? |
367 (web_contents->GetTitle().empty() ? UTF8ToUTF16(info->url.spec()) : | 383 (web_contents->GetTitle().empty() ? UTF8ToUTF16(info->url.spec()) : |
368 web_contents->GetTitle()) : | 384 web_contents->GetTitle()) : |
369 app_info.title; | 385 app_info.title; |
370 info->description = app_info.description; | 386 info->description = app_info.description; |
371 info->favicon = gfx::Image(favicon_tab_helper->GetFavicon()); | 387 info->favicon.Add(favicon_tab_helper->GetFavicon()); |
372 | 388 |
373 Profile* profile = | 389 Profile* profile = |
374 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 390 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
375 info->profile_path = profile->GetPath(); | 391 info->profile_path = profile->GetPath(); |
376 } | 392 } |
377 | 393 |
378 void UpdateShortcutForTabContents(WebContents* web_contents) { | 394 void UpdateShortcutForTabContents(WebContents* web_contents) { |
379 #if defined(OS_WIN) | 395 #if defined(OS_WIN) |
380 // UpdateShortcutWorker will delete itself when it's done. | 396 // UpdateShortcutWorker will delete itself when it's done. |
381 UpdateShortcutWorker* worker = new UpdateShortcutWorker(web_contents); | 397 UpdateShortcutWorker* worker = new UpdateShortcutWorker(web_contents); |
(...skipping 13 matching lines...) Expand all Loading... |
395 shortcut_info->profile_path = profile->GetPath(); | 411 shortcut_info->profile_path = profile->GetPath(); |
396 } | 412 } |
397 | 413 |
398 void UpdateShortcutInfoAndIconForApp( | 414 void UpdateShortcutInfoAndIconForApp( |
399 const extensions::Extension& extension, | 415 const extensions::Extension& extension, |
400 Profile* profile, | 416 Profile* profile, |
401 const web_app::ShortcutInfoCallback& callback) { | 417 const web_app::ShortcutInfoCallback& callback) { |
402 ShellIntegration::ShortcutInfo shortcut_info = | 418 ShellIntegration::ShortcutInfo shortcut_info = |
403 ShortcutInfoForExtensionAndProfile(&extension, profile); | 419 ShortcutInfoForExtensionAndProfile(&extension, profile); |
404 | 420 |
| 421 // We want to load each icon into a separate ImageSkia to insert into an |
| 422 // ImageFamily, but LoadImagesAsync currently only builds a single ImageSkia. |
| 423 // Hack around this by loading all images into the ImageSkia as 100% |
| 424 // representations, and later (in OnImageLoaded), pulling them out and |
| 425 // individually inserting them into an ImageFamily. |
405 std::vector<extensions::ImageLoader::ImageRepresentation> info_list; | 426 std::vector<extensions::ImageLoader::ImageRepresentation> info_list; |
406 for (size_t i = 0; i < arraysize(kDesiredSizes); ++i) { | 427 for (size_t i = 0; i < arraysize(kDesiredSizes); ++i) { |
407 int size = kDesiredSizes[i]; | 428 int size = kDesiredSizes[i]; |
408 extensions::ExtensionResource resource = | 429 extensions::ExtensionResource resource = |
409 extensions::IconsInfo::GetIconResource( | 430 extensions::IconsInfo::GetIconResource( |
410 &extension, size, ExtensionIconSet::MATCH_EXACTLY); | 431 &extension, size, ExtensionIconSet::MATCH_EXACTLY); |
411 if (!resource.empty()) { | 432 if (!resource.empty()) { |
412 info_list.push_back(extensions::ImageLoader::ImageRepresentation( | 433 info_list.push_back(extensions::ImageLoader::ImageRepresentation( |
413 resource, | 434 resource, |
414 extensions::ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, | 435 extensions::ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, |
(...skipping 24 matching lines...) Expand all Loading... |
439 } | 460 } |
440 | 461 |
441 // |info_list| may still be empty at this point, in which case LoadImage | 462 // |info_list| may still be empty at this point, in which case LoadImage |
442 // will call the OnImageLoaded callback with an empty image and exit | 463 // will call the OnImageLoaded callback with an empty image and exit |
443 // immediately. | 464 // immediately. |
444 extensions::ImageLoader::Get(profile)->LoadImagesAsync(&extension, info_list, | 465 extensions::ImageLoader::Get(profile)->LoadImagesAsync(&extension, info_list, |
445 base::Bind(&OnImageLoaded, shortcut_info, callback)); | 466 base::Bind(&OnImageLoaded, shortcut_info, callback)); |
446 } | 467 } |
447 | 468 |
448 } // namespace web_app | 469 } // namespace web_app |
OLD | NEW |