Chromium Code Reviews| 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.ToImageSkia()); | |
| 343 // NOTE: Do not call ImageSkia::EnsureRepsForSupportedScaleFactors here. | |
| 344 // The image reps here are not really scale factors (ImageSkia is just being | |
| 345 // used as a handy container for multiple images), so it doesn't make sense | |
| 346 // to get other supported scale factors. | |
| 347 std::vector<gfx::ImageSkiaRep> image_reps = | |
| 348 multires_image_skia.image_reps(); | |
| 349 for (std::vector<gfx::ImageSkiaRep>::const_iterator it = image_reps.begin(); | |
| 350 it != image_reps.end(); ++it) { | |
| 351 const gfx::ImageSkiaRep& image = *it; | |
| 352 gfx::ImageSkia image_skia(image); | |
| 353 image_skia.MakeThreadSafe(); | |
| 354 shortcut_info.favicon.Add(image_skia); | |
| 355 } | |
| 338 } | 356 } |
| 339 | 357 |
| 340 callback.Run(shortcut_info); | 358 callback.Run(shortcut_info); |
| 341 } | 359 } |
| 342 | 360 |
| 343 } // namespace | 361 } // namespace |
| 344 | 362 |
| 345 namespace web_app { | 363 namespace web_app { |
| 346 | 364 |
| 347 ShellIntegration::ShortcutInfo ShortcutInfoForExtensionAndProfile( | 365 ShellIntegration::ShortcutInfo ShortcutInfoForExtensionAndProfile( |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 361 extensions::TabHelper::FromWebContents(web_contents); | 379 extensions::TabHelper::FromWebContents(web_contents); |
| 362 const WebApplicationInfo& app_info = extensions_tab_helper->web_app_info(); | 380 const WebApplicationInfo& app_info = extensions_tab_helper->web_app_info(); |
| 363 | 381 |
| 364 info->url = app_info.app_url.is_empty() ? web_contents->GetURL() : | 382 info->url = app_info.app_url.is_empty() ? web_contents->GetURL() : |
| 365 app_info.app_url; | 383 app_info.app_url; |
| 366 info->title = app_info.title.empty() ? | 384 info->title = app_info.title.empty() ? |
| 367 (web_contents->GetTitle().empty() ? UTF8ToUTF16(info->url.spec()) : | 385 (web_contents->GetTitle().empty() ? UTF8ToUTF16(info->url.spec()) : |
| 368 web_contents->GetTitle()) : | 386 web_contents->GetTitle()) : |
| 369 app_info.title; | 387 app_info.title; |
| 370 info->description = app_info.description; | 388 info->description = app_info.description; |
| 371 info->favicon = gfx::Image(favicon_tab_helper->GetFavicon()); | 389 info->favicon.Add(favicon_tab_helper->GetFavicon()); |
| 372 | 390 |
| 373 Profile* profile = | 391 Profile* profile = |
| 374 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 392 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 375 info->profile_path = profile->GetPath(); | 393 info->profile_path = profile->GetPath(); |
| 376 } | 394 } |
| 377 | 395 |
| 378 void UpdateShortcutForTabContents(WebContents* web_contents) { | 396 void UpdateShortcutForTabContents(WebContents* web_contents) { |
| 379 #if defined(OS_WIN) | 397 #if defined(OS_WIN) |
| 380 // UpdateShortcutWorker will delete itself when it's done. | 398 // UpdateShortcutWorker will delete itself when it's done. |
| 381 UpdateShortcutWorker* worker = new UpdateShortcutWorker(web_contents); | 399 UpdateShortcutWorker* worker = new UpdateShortcutWorker(web_contents); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 395 shortcut_info->profile_path = profile->GetPath(); | 413 shortcut_info->profile_path = profile->GetPath(); |
| 396 } | 414 } |
| 397 | 415 |
| 398 void UpdateShortcutInfoAndIconForApp( | 416 void UpdateShortcutInfoAndIconForApp( |
| 399 const extensions::Extension& extension, | 417 const extensions::Extension& extension, |
| 400 Profile* profile, | 418 Profile* profile, |
| 401 const web_app::ShortcutInfoCallback& callback) { | 419 const web_app::ShortcutInfoCallback& callback) { |
| 402 ShellIntegration::ShortcutInfo shortcut_info = | 420 ShellIntegration::ShortcutInfo shortcut_info = |
| 403 ShortcutInfoForExtensionAndProfile(&extension, profile); | 421 ShortcutInfoForExtensionAndProfile(&extension, profile); |
| 404 | 422 |
| 423 // We want to load each icon into a separate ImageSkia to insert into an | |
| 424 // ImageFamily, but LoadImagesAsync currently only builds a single ImageSkia. | |
| 425 // Hack around this by loading all images into the ImageSkia as 100% | |
| 426 // representations, and later (in OnImageLoaded), pulling them out and | |
| 427 // individually inserting them into an ImageFamily. | |
|
benwells
2013/04/04 04:58:30
Should we change LoadImagesAsync to use ImageFamil
Matt Giuca
2013/04/04 05:15:36
Yes, I intend to do this in a separate CL.
| |
| 405 std::vector<extensions::ImageLoader::ImageRepresentation> info_list; | 428 std::vector<extensions::ImageLoader::ImageRepresentation> info_list; |
| 406 for (size_t i = 0; i < arraysize(kDesiredSizes); ++i) { | 429 for (size_t i = 0; i < arraysize(kDesiredSizes); ++i) { |
| 407 int size = kDesiredSizes[i]; | 430 int size = kDesiredSizes[i]; |
| 408 extensions::ExtensionResource resource = | 431 extensions::ExtensionResource resource = |
| 409 extensions::IconsInfo::GetIconResource( | 432 extensions::IconsInfo::GetIconResource( |
| 410 &extension, size, ExtensionIconSet::MATCH_EXACTLY); | 433 &extension, size, ExtensionIconSet::MATCH_EXACTLY); |
| 411 if (!resource.empty()) { | 434 if (!resource.empty()) { |
| 412 info_list.push_back(extensions::ImageLoader::ImageRepresentation( | 435 info_list.push_back(extensions::ImageLoader::ImageRepresentation( |
| 413 resource, | 436 resource, |
| 414 extensions::ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, | 437 extensions::ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER, |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 439 } | 462 } |
| 440 | 463 |
| 441 // |info_list| may still be empty at this point, in which case LoadImage | 464 // |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 | 465 // will call the OnImageLoaded callback with an empty image and exit |
| 443 // immediately. | 466 // immediately. |
| 444 extensions::ImageLoader::Get(profile)->LoadImagesAsync(&extension, info_list, | 467 extensions::ImageLoader::Get(profile)->LoadImagesAsync(&extension, info_list, |
| 445 base::Bind(&OnImageLoaded, shortcut_info, callback)); | 468 base::Bind(&OnImageLoaded, shortcut_info, callback)); |
| 446 } | 469 } |
| 447 | 470 |
| 448 } // namespace web_app | 471 } // namespace web_app |
| OLD | NEW |