| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 | 81 |
| 82 base::FilePath GetShortcutDataDir(const web_app::ShortcutInfo& shortcut_info) { | 82 base::FilePath GetShortcutDataDir(const web_app::ShortcutInfo& shortcut_info) { |
| 83 return web_app::GetWebAppDataDirectory(shortcut_info.profile_path, | 83 return web_app::GetWebAppDataDirectory(shortcut_info.profile_path, |
| 84 shortcut_info.extension_id, | 84 shortcut_info.extension_id, |
| 85 shortcut_info.url); | 85 shortcut_info.url); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void UpdateAllShortcutsForShortcutInfo( | 88 void UpdateAllShortcutsForShortcutInfo( |
| 89 const base::string16& old_app_title, | 89 const base::string16& old_app_title, |
| 90 const base::Closure& callback, | 90 const base::Closure& callback, |
| 91 std::unique_ptr<web_app::ShortcutInfo> shortcut_info) { | 91 scoped_refptr<web_app::ShortcutInfo> shortcut_info) { |
| 92 base::FilePath shortcut_data_dir = GetShortcutDataDir(*shortcut_info); | 92 base::FilePath shortcut_data_dir = GetShortcutDataDir(*shortcut_info); |
| 93 base::Closure task = base::Bind(&web_app::internals::UpdatePlatformShortcuts, | 93 base::Closure task = |
| 94 shortcut_data_dir, old_app_title, | 94 base::Bind(&web_app::internals::UpdatePlatformShortcuts, |
| 95 base::Passed(&shortcut_info)); | 95 shortcut_data_dir, old_app_title, std::move(shortcut_info)); |
| 96 if (callback.is_null()) { | 96 if (callback.is_null()) { |
| 97 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); | 97 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, task); |
| 98 } else { | 98 } else { |
| 99 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, task, | 99 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, task, |
| 100 callback); | 100 callback); |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 | 103 |
| 104 void OnImageLoaded(std::unique_ptr<web_app::ShortcutInfo> shortcut_info, | 104 void OnImageLoaded(scoped_refptr<web_app::ShortcutInfo> shortcut_info, |
| 105 web_app::ShortcutInfoCallback callback, | 105 web_app::ShortcutInfoCallback callback, |
| 106 const gfx::ImageFamily& image_family) { | 106 const gfx::ImageFamily& image_family) { |
| 107 // If the image failed to load (e.g. if the resource being loaded was empty) | 107 // If the image failed to load (e.g. if the resource being loaded was empty) |
| 108 // use the standard application icon. | 108 // use the standard application icon. |
| 109 if (image_family.empty()) { | 109 if (image_family.empty()) { |
| 110 gfx::Image default_icon = | 110 gfx::Image default_icon = |
| 111 ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); | 111 ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); |
| 112 int size = kDesiredSizes[kNumDesiredSizes - 1]; | 112 int size = kDesiredSizes[kNumDesiredSizes - 1]; |
| 113 SkBitmap bmp = skia::ImageOperations::Resize( | 113 SkBitmap bmp = skia::ImageOperations::Resize( |
| 114 *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, | 114 *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, |
| 115 size, size); | 115 size, size); |
| 116 gfx::ImageSkia image_skia = gfx::ImageSkia::CreateFrom1xBitmap(bmp); | 116 gfx::ImageSkia image_skia = gfx::ImageSkia::CreateFrom1xBitmap(bmp); |
| 117 // We are on the UI thread, and this image is needed from the FILE thread, | 117 // We are on the UI thread, and this image is needed from the FILE thread, |
| 118 // for creating shortcut icon files. | 118 // for creating shortcut icon files. |
| 119 image_skia.MakeThreadSafe(); | 119 image_skia.MakeThreadSafe(); |
| 120 shortcut_info->favicon.Add(gfx::Image(image_skia)); | 120 shortcut_info->favicon.Add(gfx::Image(image_skia)); |
| 121 } else { | 121 } else { |
| 122 shortcut_info->favicon = image_family; | 122 shortcut_info->favicon = image_family; |
| 123 } | 123 } |
| 124 | 124 |
| 125 callback.Run(std::move(shortcut_info)); | 125 callback.Run(std::move(shortcut_info)); |
| 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 std::unique_ptr<web_app::ShortcutInfo> shortcut_info) { | 131 scoped_refptr<web_app::ShortcutInfo> shortcut_info) { |
| 132 base::FilePath shortcut_data_dir = GetShortcutDataDir(*shortcut_info); | 132 base::FilePath shortcut_data_dir = GetShortcutDataDir(*shortcut_info); |
| 133 BrowserThread::PostTask( | 133 BrowserThread::PostTask( |
| 134 BrowserThread::FILE, FROM_HERE, | 134 BrowserThread::FILE, FROM_HERE, |
| 135 base::Bind( | 135 base::Bind( |
| 136 base::IgnoreResult(&web_app::internals::CreatePlatformShortcuts), | 136 base::IgnoreResult(&web_app::internals::CreatePlatformShortcuts), |
| 137 shortcut_data_dir, base::Passed(&shortcut_info), locations, reason)); | 137 shortcut_data_dir, std::move(shortcut_info), locations, reason)); |
| 138 } | 138 } |
| 139 | 139 |
| 140 } // namespace | 140 } // namespace |
| 141 | 141 |
| 142 namespace web_app { | 142 namespace web_app { |
| 143 | 143 |
| 144 // The following string is used to build the directory name for | 144 // The following string is used to build the directory name for |
| 145 // shortcuts to chrome applications (the kind which are installed | 145 // shortcuts to chrome applications (the kind which are installed |
| 146 // from a CRX). Application shortcuts to URLs use the {host}_{path} | 146 // from a CRX). Application shortcuts to URLs use the {host}_{path} |
| 147 // for the name of this directory. Hosts can't include an underscore. | 147 // for the name of this directory. Hosts can't include an underscore. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 166 ShortcutInfo::ShortcutInfo() {} | 166 ShortcutInfo::ShortcutInfo() {} |
| 167 ShortcutInfo::~ShortcutInfo() {} | 167 ShortcutInfo::~ShortcutInfo() {} |
| 168 | 168 |
| 169 ShortcutLocations::ShortcutLocations() | 169 ShortcutLocations::ShortcutLocations() |
| 170 : on_desktop(false), | 170 : on_desktop(false), |
| 171 applications_menu_location(APP_MENU_LOCATION_NONE), | 171 applications_menu_location(APP_MENU_LOCATION_NONE), |
| 172 in_quick_launch_bar(false) { | 172 in_quick_launch_bar(false) { |
| 173 } | 173 } |
| 174 | 174 |
| 175 #if defined(TOOLKIT_VIEWS) | 175 #if defined(TOOLKIT_VIEWS) |
| 176 std::unique_ptr<ShortcutInfo> GetShortcutInfoForTab( | 176 scoped_refptr<ShortcutInfo> GetShortcutInfoForTab( |
| 177 content::WebContents* web_contents) { | 177 content::WebContents* web_contents) { |
| 178 const favicon::FaviconDriver* favicon_driver = | 178 const favicon::FaviconDriver* favicon_driver = |
| 179 favicon::ContentFaviconDriver::FromWebContents(web_contents); | 179 favicon::ContentFaviconDriver::FromWebContents(web_contents); |
| 180 const extensions::TabHelper* extensions_tab_helper = | 180 const extensions::TabHelper* extensions_tab_helper = |
| 181 extensions::TabHelper::FromWebContents(web_contents); | 181 extensions::TabHelper::FromWebContents(web_contents); |
| 182 const WebApplicationInfo& app_info = extensions_tab_helper->web_app_info(); | 182 const WebApplicationInfo& app_info = extensions_tab_helper->web_app_info(); |
| 183 | 183 |
| 184 std::unique_ptr<ShortcutInfo> info(new ShortcutInfo); | 184 scoped_refptr<ShortcutInfo> info(new ShortcutInfo); |
| 185 info->url = app_info.app_url.is_empty() ? web_contents->GetURL() : | 185 info->url = app_info.app_url.is_empty() ? web_contents->GetURL() : |
| 186 app_info.app_url; | 186 app_info.app_url; |
| 187 info->title = app_info.title.empty() ? | 187 info->title = app_info.title.empty() ? |
| 188 (web_contents->GetTitle().empty() ? base::UTF8ToUTF16(info->url.spec()) : | 188 (web_contents->GetTitle().empty() ? base::UTF8ToUTF16(info->url.spec()) : |
| 189 web_contents->GetTitle()) : | 189 web_contents->GetTitle()) : |
| 190 app_info.title; | 190 app_info.title; |
| 191 info->description = app_info.description; | 191 info->description = app_info.description; |
| 192 info->favicon.Add(favicon_driver->GetFavicon()); | 192 info->favicon.Add(favicon_driver->GetFavicon()); |
| 193 | 193 |
| 194 Profile* profile = | 194 Profile* profile = |
| 195 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 195 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 196 info->profile_path = profile->GetPath(); | 196 info->profile_path = profile->GetPath(); |
| 197 | 197 |
| 198 return info; | 198 return info; |
| 199 } | 199 } |
| 200 #endif | 200 #endif |
| 201 | 201 |
| 202 #if !defined(OS_WIN) | 202 #if !defined(OS_WIN) |
| 203 void UpdateShortcutForTabContents(content::WebContents* web_contents) {} | 203 void UpdateShortcutForTabContents(content::WebContents* web_contents) {} |
| 204 #endif | 204 #endif |
| 205 | 205 |
| 206 std::unique_ptr<ShortcutInfo> ShortcutInfoForExtensionAndProfile( | 206 scoped_refptr<ShortcutInfo> ShortcutInfoForExtensionAndProfile( |
| 207 const extensions::Extension* app, | 207 const extensions::Extension* app, |
| 208 Profile* profile) { | 208 Profile* profile) { |
| 209 std::unique_ptr<ShortcutInfo> shortcut_info(new ShortcutInfo); | 209 scoped_refptr<ShortcutInfo> shortcut_info(new ShortcutInfo); |
| 210 shortcut_info->extension_id = app->id(); | 210 shortcut_info->extension_id = app->id(); |
| 211 shortcut_info->is_platform_app = app->is_platform_app(); | 211 shortcut_info->is_platform_app = app->is_platform_app(); |
| 212 | 212 |
| 213 // Some default-installed apps are converted into bookmark apps on Chrome | 213 // Some default-installed apps are converted into bookmark apps on Chrome |
| 214 // first run. These should not be considered as being created (by the user) | 214 // first run. These should not be considered as being created (by the user) |
| 215 // from a web page. | 215 // from a web page. |
| 216 shortcut_info->from_bookmark = | 216 shortcut_info->from_bookmark = |
| 217 app->from_bookmark() && !app->was_installed_by_default(); | 217 app->from_bookmark() && !app->was_installed_by_default(); |
| 218 | 218 |
| 219 shortcut_info->url = extensions::AppLaunchInfo::GetLaunchWebURL(app); | 219 shortcut_info->url = extensions::AppLaunchInfo::GetLaunchWebURL(app); |
| 220 shortcut_info->title = base::UTF8ToUTF16(app->name()); | 220 shortcut_info->title = base::UTF8ToUTF16(app->name()); |
| 221 shortcut_info->description = base::UTF8ToUTF16(app->description()); | 221 shortcut_info->description = base::UTF8ToUTF16(app->description()); |
| 222 shortcut_info->extension_path = app->path(); | 222 shortcut_info->extension_path = app->path(); |
| 223 shortcut_info->profile_path = profile->GetPath(); | 223 shortcut_info->profile_path = profile->GetPath(); |
| 224 shortcut_info->profile_name = | 224 shortcut_info->profile_name = |
| 225 profile->GetPrefs()->GetString(prefs::kProfileName); | 225 profile->GetPrefs()->GetString(prefs::kProfileName); |
| 226 shortcut_info->version_for_display = app->GetVersionForDisplay(); | 226 shortcut_info->version_for_display = app->GetVersionForDisplay(); |
| 227 return shortcut_info; | 227 return shortcut_info; |
| 228 } | 228 } |
| 229 | 229 |
| 230 void GetShortcutInfoForApp(const extensions::Extension* extension, | 230 void GetShortcutInfoForApp(const extensions::Extension* extension, |
| 231 Profile* profile, | 231 Profile* profile, |
| 232 const ShortcutInfoCallback& callback) { | 232 const ShortcutInfoCallback& callback) { |
| 233 std::unique_ptr<web_app::ShortcutInfo> shortcut_info( | 233 scoped_refptr<web_app::ShortcutInfo> shortcut_info( |
| 234 web_app::ShortcutInfoForExtensionAndProfile(extension, profile)); | 234 web_app::ShortcutInfoForExtensionAndProfile(extension, profile)); |
| 235 | 235 |
| 236 std::vector<extensions::ImageLoader::ImageRepresentation> info_list; | 236 std::vector<extensions::ImageLoader::ImageRepresentation> info_list; |
| 237 for (size_t i = 0; i < kNumDesiredSizes; ++i) { | 237 for (size_t i = 0; i < kNumDesiredSizes; ++i) { |
| 238 int size = kDesiredSizes[i]; | 238 int size = kDesiredSizes[i]; |
| 239 extensions::ExtensionResource resource = | 239 extensions::ExtensionResource resource = |
| 240 extensions::IconsInfo::GetIconResource( | 240 extensions::IconsInfo::GetIconResource( |
| 241 extension, size, ExtensionIconSet::MATCH_EXACTLY); | 241 extension, size, ExtensionIconSet::MATCH_EXACTLY); |
| 242 if (!resource.empty()) { | 242 if (!resource.empty()) { |
| 243 info_list.push_back(extensions::ImageLoader::ImageRepresentation( | 243 info_list.push_back(extensions::ImageLoader::ImageRepresentation( |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 | 379 |
| 380 std::string GetExtensionIdFromApplicationName(const std::string& app_name) { | 380 std::string GetExtensionIdFromApplicationName(const std::string& app_name) { |
| 381 std::string prefix(kCrxAppPrefix); | 381 std::string prefix(kCrxAppPrefix); |
| 382 if (app_name.substr(0, prefix.length()) != prefix) | 382 if (app_name.substr(0, prefix.length()) != prefix) |
| 383 return std::string(); | 383 return std::string(); |
| 384 return app_name.substr(prefix.length()); | 384 return app_name.substr(prefix.length()); |
| 385 } | 385 } |
| 386 | 386 |
| 387 void CreateShortcutsWithInfo(ShortcutCreationReason reason, | 387 void CreateShortcutsWithInfo(ShortcutCreationReason reason, |
| 388 const ShortcutLocations& locations, | 388 const ShortcutLocations& locations, |
| 389 std::unique_ptr<ShortcutInfo> shortcut_info) { | 389 scoped_refptr<ShortcutInfo> shortcut_info) { |
| 390 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 390 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 391 | 391 |
| 392 // If the shortcut is for an application shortcut with the new bookmark app | 392 // If the shortcut is for an application shortcut with the new bookmark app |
| 393 // flow disabled, there will be no corresponding extension. | 393 // flow disabled, there will be no corresponding extension. |
| 394 if (!shortcut_info->extension_id.empty()) { | 394 if (!shortcut_info->extension_id.empty()) { |
| 395 // It's possible for the extension to be deleted before we get here. | 395 // It's possible for the extension to be deleted before we get here. |
| 396 // For example, creating a hosted app from a website. Double check that | 396 // For example, creating a hosted app from a website. Double check that |
| 397 // it still exists. | 397 // it still exists. |
| 398 Profile* profile = g_browser_process->profile_manager()->GetProfileByPath( | 398 Profile* profile = g_browser_process->profile_manager()->GetProfileByPath( |
| 399 shortcut_info->profile_path); | 399 shortcut_info->profile_path); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 420 if (!ShouldCreateShortcutFor(reason, profile, app)) | 420 if (!ShouldCreateShortcutFor(reason, profile, app)) |
| 421 return; | 421 return; |
| 422 | 422 |
| 423 GetShortcutInfoForApp( | 423 GetShortcutInfoForApp( |
| 424 app, profile, base::Bind(&CreateShortcutsWithInfo, reason, locations)); | 424 app, profile, base::Bind(&CreateShortcutsWithInfo, reason, locations)); |
| 425 } | 425 } |
| 426 | 426 |
| 427 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app) { | 427 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app) { |
| 428 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 428 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 429 | 429 |
| 430 std::unique_ptr<ShortcutInfo> shortcut_info( | 430 scoped_refptr<ShortcutInfo> shortcut_info( |
| 431 ShortcutInfoForExtensionAndProfile(app, profile)); | 431 ShortcutInfoForExtensionAndProfile(app, profile)); |
| 432 base::FilePath shortcut_data_dir = GetShortcutDataDir(*shortcut_info); | 432 base::FilePath shortcut_data_dir = GetShortcutDataDir(*shortcut_info); |
| 433 BrowserThread::PostTask( | 433 BrowserThread::PostTask( |
| 434 BrowserThread::FILE, FROM_HERE, | 434 BrowserThread::FILE, FROM_HERE, |
| 435 base::Bind(&web_app::internals::DeletePlatformShortcuts, | 435 base::Bind(&web_app::internals::DeletePlatformShortcuts, |
| 436 shortcut_data_dir, base::Passed(&shortcut_info))); | 436 shortcut_data_dir, base::Passed(&shortcut_info))); |
| 437 } | 437 } |
| 438 | 438 |
| 439 void UpdateAllShortcuts(const base::string16& old_app_title, | 439 void UpdateAllShortcuts(const base::string16& old_app_title, |
| 440 Profile* profile, | 440 Profile* profile, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 | 484 |
| 485 #if defined(OS_LINUX) | 485 #if defined(OS_LINUX) |
| 486 std::string GetWMClassFromAppName(std::string app_name) { | 486 std::string GetWMClassFromAppName(std::string app_name) { |
| 487 base::i18n::ReplaceIllegalCharactersInPath(&app_name, '_'); | 487 base::i18n::ReplaceIllegalCharactersInPath(&app_name, '_'); |
| 488 base::TrimString(app_name, "_", &app_name); | 488 base::TrimString(app_name, "_", &app_name); |
| 489 return app_name; | 489 return app_name; |
| 490 } | 490 } |
| 491 #endif | 491 #endif |
| 492 | 492 |
| 493 } // namespace web_app | 493 } // namespace web_app |
| OLD | NEW |