| 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/web_applications/web_app.h" | 5 #include "chrome/browser/web_applications/web_app.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/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 #endif | 76 #endif |
| 77 | 77 |
| 78 base::FilePath GetShortcutDataDir(const web_app::ShortcutInfo& shortcut_info) { | 78 base::FilePath GetShortcutDataDir(const web_app::ShortcutInfo& shortcut_info) { |
| 79 return web_app::GetWebAppDataDirectory(shortcut_info.profile_path, | 79 return web_app::GetWebAppDataDirectory(shortcut_info.profile_path, |
| 80 shortcut_info.extension_id, | 80 shortcut_info.extension_id, |
| 81 shortcut_info.url); | 81 shortcut_info.url); |
| 82 } | 82 } |
| 83 | 83 |
| 84 void UpdateAllShortcutsForShortcutInfo( | 84 void UpdateAllShortcutsForShortcutInfo( |
| 85 const base::string16& old_app_title, | 85 const base::string16& old_app_title, |
| 86 const web_app::ShortcutInfo& shortcut_info, | 86 scoped_ptr<web_app::ShortcutInfo> shortcut_info, |
| 87 const extensions::FileHandlersInfo& file_handlers_info) { | 87 const extensions::FileHandlersInfo& file_handlers_info) { |
| 88 base::FilePath shortcut_data_dir = GetShortcutDataDir(*shortcut_info); |
| 88 BrowserThread::PostTask( | 89 BrowserThread::PostTask( |
| 89 BrowserThread::FILE, | 90 BrowserThread::FILE, FROM_HERE, |
| 90 FROM_HERE, | |
| 91 base::Bind(&web_app::internals::UpdatePlatformShortcuts, | 91 base::Bind(&web_app::internals::UpdatePlatformShortcuts, |
| 92 GetShortcutDataDir(shortcut_info), | 92 shortcut_data_dir, old_app_title, base::Passed(&shortcut_info), |
| 93 old_app_title, shortcut_info, file_handlers_info)); | 93 file_handlers_info)); |
| 94 } | 94 } |
| 95 | 95 |
| 96 void OnImageLoaded(web_app::ShortcutInfo shortcut_info, | 96 void OnImageLoaded(scoped_ptr<web_app::ShortcutInfo> shortcut_info, |
| 97 extensions::FileHandlersInfo file_handlers_info, | 97 extensions::FileHandlersInfo file_handlers_info, |
| 98 web_app::InfoCallback callback, | 98 web_app::InfoCallback callback, |
| 99 const gfx::ImageFamily& image_family) { | 99 const gfx::ImageFamily& image_family) { |
| 100 // If the image failed to load (e.g. if the resource being loaded was empty) | 100 // If the image failed to load (e.g. if the resource being loaded was empty) |
| 101 // use the standard application icon. | 101 // use the standard application icon. |
| 102 if (image_family.empty()) { | 102 if (image_family.empty()) { |
| 103 gfx::Image default_icon = | 103 gfx::Image default_icon = |
| 104 ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); | 104 ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); |
| 105 int size = kDesiredSizes[kNumDesiredSizes - 1]; | 105 int size = kDesiredSizes[kNumDesiredSizes - 1]; |
| 106 SkBitmap bmp = skia::ImageOperations::Resize( | 106 SkBitmap bmp = skia::ImageOperations::Resize( |
| 107 *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, | 107 *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, |
| 108 size, size); | 108 size, size); |
| 109 gfx::ImageSkia image_skia = gfx::ImageSkia::CreateFrom1xBitmap(bmp); | 109 gfx::ImageSkia image_skia = gfx::ImageSkia::CreateFrom1xBitmap(bmp); |
| 110 // We are on the UI thread, and this image is needed from the FILE thread, | 110 // We are on the UI thread, and this image is needed from the FILE thread, |
| 111 // for creating shortcut icon files. | 111 // for creating shortcut icon files. |
| 112 image_skia.MakeThreadSafe(); | 112 image_skia.MakeThreadSafe(); |
| 113 shortcut_info.favicon.Add(gfx::Image(image_skia)); | 113 shortcut_info->favicon.Add(gfx::Image(image_skia)); |
| 114 } else { | 114 } else { |
| 115 shortcut_info.favicon = image_family; | 115 shortcut_info->favicon = image_family; |
| 116 } | 116 } |
| 117 | 117 |
| 118 callback.Run(shortcut_info, file_handlers_info); | 118 callback.Run(shortcut_info.Pass(), file_handlers_info); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void IgnoreFileHandlersInfo( | 121 void IgnoreFileHandlersInfo( |
| 122 const web_app::ShortcutInfoCallback& shortcut_info_callback, | 122 const web_app::ShortcutInfoCallback& shortcut_info_callback, |
| 123 const web_app::ShortcutInfo& shortcut_info, | 123 scoped_ptr<web_app::ShortcutInfo> shortcut_info, |
| 124 const extensions::FileHandlersInfo& file_handlers_info) { | 124 const extensions::FileHandlersInfo& file_handlers_info) { |
| 125 shortcut_info_callback.Run(shortcut_info); | 125 shortcut_info_callback.Run(shortcut_info.Pass()); |
| 126 } | 126 } |
| 127 | 127 |
| 128 void ScheduleCreatePlatformShortcut( | 128 void ScheduleCreatePlatformShortcut( |
| 129 web_app::ShortcutCreationReason reason, | 129 web_app::ShortcutCreationReason reason, |
| 130 const web_app::ShortcutLocations& locations, | 130 const web_app::ShortcutLocations& locations, |
| 131 const web_app::ShortcutInfo& shortcut_info, | 131 scoped_ptr<web_app::ShortcutInfo> shortcut_info, |
| 132 const extensions::FileHandlersInfo& file_handlers_info) { | 132 const extensions::FileHandlersInfo& file_handlers_info) { |
| 133 base::FilePath shortcut_data_dir = GetShortcutDataDir(*shortcut_info); |
| 133 BrowserThread::PostTask( | 134 BrowserThread::PostTask( |
| 134 BrowserThread::FILE, FROM_HERE, | 135 BrowserThread::FILE, FROM_HERE, |
| 135 base::Bind( | 136 base::Bind( |
| 136 base::IgnoreResult(&web_app::internals::CreatePlatformShortcuts), | 137 base::IgnoreResult(&web_app::internals::CreatePlatformShortcuts), |
| 137 GetShortcutDataDir(shortcut_info), shortcut_info, file_handlers_info, | 138 shortcut_data_dir, base::Passed(&shortcut_info), file_handlers_info, |
| 138 locations, reason)); | 139 locations, reason)); |
| 139 } | 140 } |
| 140 | 141 |
| 141 } // namespace | 142 } // namespace |
| 142 | 143 |
| 143 namespace web_app { | 144 namespace web_app { |
| 144 | 145 |
| 145 // The following string is used to build the directory name for | 146 // The following string is used to build the directory name for |
| 146 // shortcuts to chrome applications (the kind which are installed | 147 // shortcuts to chrome applications (the kind which are installed |
| 147 // from a CRX). Application shortcuts to URLs use the {host}_{path} | 148 // from a CRX). Application shortcuts to URLs use the {host}_{path} |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 Profile* profile = | 200 Profile* profile = |
| 200 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 201 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 201 info->profile_path = profile->GetPath(); | 202 info->profile_path = profile->GetPath(); |
| 202 } | 203 } |
| 203 #endif | 204 #endif |
| 204 | 205 |
| 205 #if !defined(OS_WIN) | 206 #if !defined(OS_WIN) |
| 206 void UpdateShortcutForTabContents(content::WebContents* web_contents) {} | 207 void UpdateShortcutForTabContents(content::WebContents* web_contents) {} |
| 207 #endif | 208 #endif |
| 208 | 209 |
| 209 ShortcutInfo ShortcutInfoForExtensionAndProfile( | 210 scoped_ptr<ShortcutInfo> ShortcutInfoForExtensionAndProfile( |
| 210 const extensions::Extension* app, Profile* profile) { | 211 const extensions::Extension* app, Profile* profile) { |
| 211 ShortcutInfo shortcut_info; | 212 scoped_ptr<ShortcutInfo> shortcut_info(new ShortcutInfo); |
| 212 shortcut_info.extension_id = app->id(); | 213 shortcut_info->extension_id = app->id(); |
| 213 shortcut_info.is_platform_app = app->is_platform_app(); | 214 shortcut_info->is_platform_app = app->is_platform_app(); |
| 214 shortcut_info.url = extensions::AppLaunchInfo::GetLaunchWebURL(app); | 215 shortcut_info->url = extensions::AppLaunchInfo::GetLaunchWebURL(app); |
| 215 shortcut_info.title = base::UTF8ToUTF16(app->name()); | 216 shortcut_info->title = base::UTF8ToUTF16(app->name()); |
| 216 shortcut_info.description = base::UTF8ToUTF16(app->description()); | 217 shortcut_info->description = base::UTF8ToUTF16(app->description()); |
| 217 shortcut_info.extension_path = app->path(); | 218 shortcut_info->extension_path = app->path(); |
| 218 shortcut_info.profile_path = profile->GetPath(); | 219 shortcut_info->profile_path = profile->GetPath(); |
| 219 shortcut_info.profile_name = | 220 shortcut_info->profile_name = |
| 220 profile->GetPrefs()->GetString(prefs::kProfileName); | 221 profile->GetPrefs()->GetString(prefs::kProfileName); |
| 221 return shortcut_info; | 222 return shortcut_info; |
| 222 } | 223 } |
| 223 | 224 |
| 224 void GetInfoForApp(const extensions::Extension* extension, | 225 void GetInfoForApp(const extensions::Extension* extension, |
| 225 Profile* profile, | 226 Profile* profile, |
| 226 const InfoCallback& callback) { | 227 const InfoCallback& callback) { |
| 227 web_app::ShortcutInfo shortcut_info = | 228 scoped_ptr<web_app::ShortcutInfo> shortcut_info( |
| 228 web_app::ShortcutInfoForExtensionAndProfile(extension, profile); | 229 web_app::ShortcutInfoForExtensionAndProfile(extension, profile)); |
| 229 const std::vector<extensions::FileHandlerInfo>* file_handlers = | 230 const std::vector<extensions::FileHandlerInfo>* file_handlers = |
| 230 extensions::FileHandlers::GetFileHandlers(extension); | 231 extensions::FileHandlers::GetFileHandlers(extension); |
| 231 extensions::FileHandlersInfo file_handlers_info = | 232 extensions::FileHandlersInfo file_handlers_info = |
| 232 file_handlers ? *file_handlers : extensions::FileHandlersInfo(); | 233 file_handlers ? *file_handlers : extensions::FileHandlersInfo(); |
| 233 | 234 |
| 234 std::vector<extensions::ImageLoader::ImageRepresentation> info_list; | 235 std::vector<extensions::ImageLoader::ImageRepresentation> info_list; |
| 235 for (size_t i = 0; i < kNumDesiredSizes; ++i) { | 236 for (size_t i = 0; i < kNumDesiredSizes; ++i) { |
| 236 int size = kDesiredSizes[i]; | 237 int size = kDesiredSizes[i]; |
| 237 extensions::ExtensionResource resource = | 238 extensions::ExtensionResource resource = |
| 238 extensions::IconsInfo::GetIconResource( | 239 extensions::IconsInfo::GetIconResource( |
| (...skipping 25 matching lines...) Expand all Loading... |
| 264 resource, | 265 resource, |
| 265 extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE, | 266 extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE, |
| 266 gfx::Size(size, size), | 267 gfx::Size(size, size), |
| 267 ui::SCALE_FACTOR_100P)); | 268 ui::SCALE_FACTOR_100P)); |
| 268 } | 269 } |
| 269 | 270 |
| 270 // |info_list| may still be empty at this point, in which case | 271 // |info_list| may still be empty at this point, in which case |
| 271 // LoadImageFamilyAsync will call the OnImageLoaded callback with an empty | 272 // LoadImageFamilyAsync will call the OnImageLoaded callback with an empty |
| 272 // image and exit immediately. | 273 // image and exit immediately. |
| 273 extensions::ImageLoader::Get(profile)->LoadImageFamilyAsync( | 274 extensions::ImageLoader::Get(profile)->LoadImageFamilyAsync( |
| 274 extension, | 275 extension, info_list, |
| 275 info_list, | 276 base::Bind(&OnImageLoaded, base::Passed(&shortcut_info), |
| 276 base::Bind(&OnImageLoaded, shortcut_info, file_handlers_info, callback)); | 277 file_handlers_info, callback)); |
| 277 } | 278 } |
| 278 | 279 |
| 279 void GetShortcutInfoForApp(const extensions::Extension* extension, | 280 void GetShortcutInfoForApp(const extensions::Extension* extension, |
| 280 Profile* profile, | 281 Profile* profile, |
| 281 const ShortcutInfoCallback& callback) { | 282 const ShortcutInfoCallback& callback) { |
| 282 GetInfoForApp( | 283 GetInfoForApp( |
| 283 extension, profile, base::Bind(&IgnoreFileHandlersInfo, callback)); | 284 extension, profile, base::Bind(&IgnoreFileHandlersInfo, callback)); |
| 284 } | 285 } |
| 285 | 286 |
| 286 bool ShouldCreateShortcutFor(web_app::ShortcutCreationReason reason, | 287 bool ShouldCreateShortcutFor(web_app::ShortcutCreationReason reason, |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 std::string GetExtensionIdFromApplicationName(const std::string& app_name) { | 371 std::string GetExtensionIdFromApplicationName(const std::string& app_name) { |
| 371 std::string prefix(kCrxAppPrefix); | 372 std::string prefix(kCrxAppPrefix); |
| 372 if (app_name.substr(0, prefix.length()) != prefix) | 373 if (app_name.substr(0, prefix.length()) != prefix) |
| 373 return std::string(); | 374 return std::string(); |
| 374 return app_name.substr(prefix.length()); | 375 return app_name.substr(prefix.length()); |
| 375 } | 376 } |
| 376 | 377 |
| 377 void CreateShortcutsWithInfo( | 378 void CreateShortcutsWithInfo( |
| 378 ShortcutCreationReason reason, | 379 ShortcutCreationReason reason, |
| 379 const ShortcutLocations& locations, | 380 const ShortcutLocations& locations, |
| 380 const ShortcutInfo& shortcut_info, | 381 scoped_ptr<ShortcutInfo> shortcut_info, |
| 381 const extensions::FileHandlersInfo& file_handlers_info) { | 382 const extensions::FileHandlersInfo& file_handlers_info) { |
| 382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 383 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 383 | 384 |
| 384 // If the shortcut is for an application shortcut with the new bookmark app | 385 // If the shortcut is for an application shortcut with the new bookmark app |
| 385 // flow disabled, there will be no corresponding extension. | 386 // flow disabled, there will be no corresponding extension. |
| 386 if (!shortcut_info.extension_id.empty()) { | 387 if (!shortcut_info->extension_id.empty()) { |
| 387 // It's possible for the extension to be deleted before we get here. | 388 // It's possible for the extension to be deleted before we get here. |
| 388 // For example, creating a hosted app from a website. Double check that | 389 // For example, creating a hosted app from a website. Double check that |
| 389 // it still exists. | 390 // it still exists. |
| 390 Profile* profile = g_browser_process->profile_manager()->GetProfileByPath( | 391 Profile* profile = g_browser_process->profile_manager()->GetProfileByPath( |
| 391 shortcut_info.profile_path); | 392 shortcut_info->profile_path); |
| 392 if (!profile) | 393 if (!profile) |
| 393 return; | 394 return; |
| 394 | 395 |
| 395 extensions::ExtensionRegistry* registry = | 396 extensions::ExtensionRegistry* registry = |
| 396 extensions::ExtensionRegistry::Get(profile); | 397 extensions::ExtensionRegistry::Get(profile); |
| 397 const extensions::Extension* extension = registry->GetExtensionById( | 398 const extensions::Extension* extension = registry->GetExtensionById( |
| 398 shortcut_info.extension_id, extensions::ExtensionRegistry::EVERYTHING); | 399 shortcut_info->extension_id, extensions::ExtensionRegistry::EVERYTHING); |
| 399 if (!extension) | 400 if (!extension) |
| 400 return; | 401 return; |
| 401 } | 402 } |
| 402 | 403 |
| 403 ScheduleCreatePlatformShortcut(reason, locations, shortcut_info, | 404 ScheduleCreatePlatformShortcut(reason, locations, shortcut_info.Pass(), |
| 404 file_handlers_info); | 405 file_handlers_info); |
| 405 } | 406 } |
| 406 | 407 |
| 407 void CreateNonAppShortcut(const ShortcutLocations& locations, | 408 void CreateNonAppShortcut(const ShortcutLocations& locations, |
| 408 const ShortcutInfo& shortcut_info) { | 409 scoped_ptr<ShortcutInfo> shortcut_info) { |
| 409 ScheduleCreatePlatformShortcut(SHORTCUT_CREATION_AUTOMATED, locations, | 410 ScheduleCreatePlatformShortcut(SHORTCUT_CREATION_AUTOMATED, locations, |
| 410 shortcut_info, extensions::FileHandlersInfo()); | 411 shortcut_info.Pass(), |
| 412 extensions::FileHandlersInfo()); |
| 411 } | 413 } |
| 412 | 414 |
| 413 void CreateShortcuts(ShortcutCreationReason reason, | 415 void CreateShortcuts(ShortcutCreationReason reason, |
| 414 const ShortcutLocations& locations, | 416 const ShortcutLocations& locations, |
| 415 Profile* profile, | 417 Profile* profile, |
| 416 const extensions::Extension* app) { | 418 const extensions::Extension* app) { |
| 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 418 | 420 |
| 419 if (!ShouldCreateShortcutFor(reason, profile, app)) | 421 if (!ShouldCreateShortcutFor(reason, profile, app)) |
| 420 return; | 422 return; |
| 421 | 423 |
| 422 GetInfoForApp( | 424 GetInfoForApp( |
| 423 app, profile, base::Bind(&CreateShortcutsWithInfo, reason, locations)); | 425 app, profile, base::Bind(&CreateShortcutsWithInfo, reason, locations)); |
| 424 } | 426 } |
| 425 | 427 |
| 426 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app) { | 428 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app) { |
| 427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 429 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 428 | 430 |
| 429 ShortcutInfo shortcut_info = | 431 scoped_ptr<ShortcutInfo> shortcut_info( |
| 430 ShortcutInfoForExtensionAndProfile(app, profile); | 432 ShortcutInfoForExtensionAndProfile(app, profile)); |
| 433 base::FilePath shortcut_data_dir = GetShortcutDataDir(*shortcut_info); |
| 431 BrowserThread::PostTask( | 434 BrowserThread::PostTask( |
| 432 BrowserThread::FILE, | 435 BrowserThread::FILE, FROM_HERE, |
| 433 FROM_HERE, | |
| 434 base::Bind(&web_app::internals::DeletePlatformShortcuts, | 436 base::Bind(&web_app::internals::DeletePlatformShortcuts, |
| 435 GetShortcutDataDir(shortcut_info), shortcut_info)); | 437 shortcut_data_dir, base::Passed(&shortcut_info))); |
| 436 } | 438 } |
| 437 | 439 |
| 438 void UpdateAllShortcuts(const base::string16& old_app_title, | 440 void UpdateAllShortcuts(const base::string16& old_app_title, |
| 439 Profile* profile, | 441 Profile* profile, |
| 440 const extensions::Extension* app) { | 442 const extensions::Extension* app) { |
| 441 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 443 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 442 | 444 |
| 443 GetInfoForApp(app, | 445 GetInfoForApp(app, |
| 444 profile, | 446 profile, |
| 445 base::Bind(&UpdateAllShortcutsForShortcutInfo, old_app_title)); | 447 base::Bind(&UpdateAllShortcutsForShortcutInfo, old_app_title)); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 | 484 |
| 483 #if defined(OS_LINUX) | 485 #if defined(OS_LINUX) |
| 484 std::string GetWMClassFromAppName(std::string app_name) { | 486 std::string GetWMClassFromAppName(std::string app_name) { |
| 485 base::i18n::ReplaceIllegalCharactersInPath(&app_name, '_'); | 487 base::i18n::ReplaceIllegalCharactersInPath(&app_name, '_'); |
| 486 base::TrimString(app_name, "_", &app_name); | 488 base::TrimString(app_name, "_", &app_name); |
| 487 return app_name; | 489 return app_name; |
| 488 } | 490 } |
| 489 #endif | 491 #endif |
| 490 | 492 |
| 491 } // namespace web_app | 493 } // namespace web_app |
| OLD | NEW |