[email protected] | 178f851 | 2012-02-09 01:49:36 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ed543187 | 2009-11-17 08:39:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/web_applications/web_app.h" |
| 6 | |
[email protected] | 8721270 | 2011-12-08 21:17:19 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 8806d3b | 2012-04-13 06:46:34 | [diff] [blame] | 8 | #include "base/bind_helpers.h" |
mitchelljones | 58e35915 | 2014-12-19 01:10:13 | [diff] [blame] | 9 | #include "base/command_line.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
[email protected] | a0b60cfd | 2011-04-06 18:02:41 | [diff] [blame] | 11 | #include "base/i18n/file_util_icu.h" |
[email protected] | f18d37ef | 2014-04-01 11:17:42 | [diff] [blame] | 12 | #include "base/prefs/pref_service.h" |
[email protected] | 9f0abdb | 2013-06-10 21:49:34 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 7f070d4 | 2011-03-09 20:25:32 | [diff] [blame] | 15 | #include "base/threading/thread.h" |
mitchelljones | 1c30102 | 2014-12-11 00:38:47 | [diff] [blame] | 16 | #include "chrome/browser/browser_process.h" |
[email protected] | 090e1ee7 | 2014-06-03 13:08:40 | [diff] [blame] | 17 | #include "chrome/browser/extensions/extension_ui_util.h" |
[email protected] | f18d37ef | 2014-04-01 11:17:42 | [diff] [blame] | 18 | #include "chrome/browser/profiles/profile.h" |
mitchelljones | 1c30102 | 2014-12-11 00:38:47 | [diff] [blame] | 19 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 20 | #include "chrome/common/chrome_constants.h" |
mitchelljones | 58e35915 | 2014-12-19 01:10:13 | [diff] [blame] | 21 | #include "chrome/common/chrome_switches.h" |
[email protected] | 6b414c23 | 2013-06-05 07:53:34 | [diff] [blame] | 22 | #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
[email protected] | f18d37ef | 2014-04-01 11:17:42 | [diff] [blame] | 23 | #include "chrome/common/pref_names.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 24 | #include "content/public/browser/browser_thread.h" |
[email protected] | 090e1ee7 | 2014-06-03 13:08:40 | [diff] [blame] | 25 | #include "extensions/browser/extension_registry.h" |
[email protected] | 326e6f0 | 2014-06-20 04:53:37 | [diff] [blame] | 26 | #include "extensions/browser/image_loader.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 27 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 28 | #include "extensions/common/extension.h" |
[email protected] | 090e1ee7 | 2014-06-03 13:08:40 | [diff] [blame] | 29 | #include "extensions/common/extension_set.h" |
[email protected] | 0db486f | 2014-04-09 19:32:22 | [diff] [blame] | 30 | #include "extensions/common/manifest_handlers/icons_handler.h" |
mukai | 4245dfe8 | 2014-09-05 17:40:51 | [diff] [blame] | 31 | #include "extensions/grit/extensions_browser_resources.h" |
[email protected] | f18d37ef | 2014-04-01 11:17:42 | [diff] [blame] | 32 | #include "skia/ext/image_operations.h" |
| 33 | #include "third_party/skia/include/core/SkBitmap.h" |
| 34 | #include "ui/base/resource/resource_bundle.h" |
| 35 | #include "ui/gfx/image/image.h" |
| 36 | #include "ui/gfx/image/image_family.h" |
| 37 | #include "ui/gfx/image/image_skia.h" |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 38 | #include "url/url_constants.h" |
[email protected] | f18d37ef | 2014-04-01 11:17:42 | [diff] [blame] | 39 | |
| 40 | #if defined(OS_WIN) |
| 41 | #include "ui/gfx/icon_util.h" |
| 42 | #endif |
[email protected] | ed543187 | 2009-11-17 08:39:51 | [diff] [blame] | 43 | |
[email protected] | c7264f4 | 2014-06-22 21:23:09 | [diff] [blame] | 44 | #if defined(TOOLKIT_VIEWS) |
| 45 | #include "chrome/browser/extensions/tab_helper.h" |
sdefresne | 45582697 | 2015-04-10 15:25:15 | [diff] [blame] | 46 | #include "components/favicon/content/content_favicon_driver.h" |
[email protected] | c7264f4 | 2014-06-22 21:23:09 | [diff] [blame] | 47 | #endif |
| 48 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 49 | using content::BrowserThread; |
| 50 | |
[email protected] | ed543187 | 2009-11-17 08:39:51 | [diff] [blame] | 51 | namespace { |
| 52 | |
[email protected] | f18d37ef | 2014-04-01 11:17:42 | [diff] [blame] | 53 | #if defined(OS_MACOSX) |
| 54 | const int kDesiredSizes[] = {16, 32, 128, 256, 512}; |
| 55 | const size_t kNumDesiredSizes = arraysize(kDesiredSizes); |
| 56 | #elif defined(OS_LINUX) |
| 57 | // Linux supports icons of any size. FreeDesktop Icon Theme Specification states |
| 58 | // that "Minimally you should install a 48x48 icon in the hicolor theme." |
| 59 | const int kDesiredSizes[] = {16, 32, 48, 128, 256, 512}; |
| 60 | const size_t kNumDesiredSizes = arraysize(kDesiredSizes); |
| 61 | #elif defined(OS_WIN) |
| 62 | const int* kDesiredSizes = IconUtil::kIconDimensions; |
| 63 | const size_t kNumDesiredSizes = IconUtil::kNumIconDimensions; |
| 64 | #else |
| 65 | const int kDesiredSizes[] = {32}; |
| 66 | const size_t kNumDesiredSizes = arraysize(kDesiredSizes); |
| 67 | #endif |
| 68 | |
[email protected] | 16f76563 | 2010-09-21 21:31:27 | [diff] [blame] | 69 | #if defined(TOOLKIT_VIEWS) |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 70 | // Predicator for sorting images from largest to smallest. |
[email protected] | 38789d8 | 2010-11-17 06:03:44 | [diff] [blame] | 71 | bool IconPrecedes(const WebApplicationInfo::IconInfo& left, |
| 72 | const WebApplicationInfo::IconInfo& right) { |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 73 | return left.width < right.width; |
| 74 | } |
[email protected] | 16f76563 | 2010-09-21 21:31:27 | [diff] [blame] | 75 | #endif |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 76 | |
[email protected] | 238dd7b | 2014-05-23 07:07:20 | [diff] [blame] | 77 | base::FilePath GetShortcutDataDir(const web_app::ShortcutInfo& shortcut_info) { |
| 78 | return web_app::GetWebAppDataDirectory(shortcut_info.profile_path, |
| 79 | shortcut_info.extension_id, |
| 80 | shortcut_info.url); |
[email protected] | e66ba95 | 2012-10-09 09:59:44 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | e6be3fe | 2014-04-11 13:17:51 | [diff] [blame] | 83 | void UpdateAllShortcutsForShortcutInfo( |
| 84 | const base::string16& old_app_title, |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 85 | scoped_ptr<web_app::ShortcutInfo> shortcut_info, |
[email protected] | e6be3fe | 2014-04-11 13:17:51 | [diff] [blame] | 86 | const extensions::FileHandlersInfo& file_handlers_info) { |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 87 | base::FilePath shortcut_data_dir = GetShortcutDataDir(*shortcut_info); |
[email protected] | e6be3fe | 2014-04-11 13:17:51 | [diff] [blame] | 88 | BrowserThread::PostTask( |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 89 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 238dd7b | 2014-05-23 07:07:20 | [diff] [blame] | 90 | base::Bind(&web_app::internals::UpdatePlatformShortcuts, |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 91 | shortcut_data_dir, old_app_title, base::Passed(&shortcut_info), |
| 92 | file_handlers_info)); |
[email protected] | 1488508 | 2014-04-08 04:41:28 | [diff] [blame] | 93 | } |
| 94 | |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 95 | void OnImageLoaded(scoped_ptr<web_app::ShortcutInfo> shortcut_info, |
[email protected] | e6be3fe | 2014-04-11 13:17:51 | [diff] [blame] | 96 | extensions::FileHandlersInfo file_handlers_info, |
[email protected] | 5bbfbae | 2014-06-18 18:26:38 | [diff] [blame] | 97 | web_app::InfoCallback callback, |
[email protected] | f18d37ef | 2014-04-01 11:17:42 | [diff] [blame] | 98 | const gfx::ImageFamily& image_family) { |
| 99 | // If the image failed to load (e.g. if the resource being loaded was empty) |
| 100 | // use the standard application icon. |
| 101 | if (image_family.empty()) { |
| 102 | gfx::Image default_icon = |
| 103 | ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON); |
| 104 | int size = kDesiredSizes[kNumDesiredSizes - 1]; |
| 105 | SkBitmap bmp = skia::ImageOperations::Resize( |
| 106 | *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST, |
| 107 | size, size); |
| 108 | gfx::ImageSkia image_skia = gfx::ImageSkia::CreateFrom1xBitmap(bmp); |
| 109 | // We are on the UI thread, and this image is needed from the FILE thread, |
| 110 | // for creating shortcut icon files. |
| 111 | image_skia.MakeThreadSafe(); |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 112 | shortcut_info->favicon.Add(gfx::Image(image_skia)); |
[email protected] | f18d37ef | 2014-04-01 11:17:42 | [diff] [blame] | 113 | } else { |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 114 | shortcut_info->favicon = image_family; |
[email protected] | f18d37ef | 2014-04-01 11:17:42 | [diff] [blame] | 115 | } |
| 116 | |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 117 | callback.Run(shortcut_info.Pass(), file_handlers_info); |
[email protected] | e6be3fe | 2014-04-11 13:17:51 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 5bbfbae | 2014-06-18 18:26:38 | [diff] [blame] | 120 | void IgnoreFileHandlersInfo( |
| 121 | const web_app::ShortcutInfoCallback& shortcut_info_callback, |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 122 | scoped_ptr<web_app::ShortcutInfo> shortcut_info, |
[email protected] | 5bbfbae | 2014-06-18 18:26:38 | [diff] [blame] | 123 | const extensions::FileHandlersInfo& file_handlers_info) { |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 124 | shortcut_info_callback.Run(shortcut_info.Pass()); |
[email protected] | 5bbfbae | 2014-06-18 18:26:38 | [diff] [blame] | 125 | } |
| 126 | |
mitchelljones | 421df67c | 2014-12-18 03:06:50 | [diff] [blame] | 127 | void ScheduleCreatePlatformShortcut( |
| 128 | web_app::ShortcutCreationReason reason, |
| 129 | const web_app::ShortcutLocations& locations, |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 130 | scoped_ptr<web_app::ShortcutInfo> shortcut_info, |
mitchelljones | 421df67c | 2014-12-18 03:06:50 | [diff] [blame] | 131 | const extensions::FileHandlersInfo& file_handlers_info) { |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 132 | base::FilePath shortcut_data_dir = GetShortcutDataDir(*shortcut_info); |
mitchelljones | 421df67c | 2014-12-18 03:06:50 | [diff] [blame] | 133 | BrowserThread::PostTask( |
| 134 | BrowserThread::FILE, FROM_HERE, |
| 135 | base::Bind( |
| 136 | base::IgnoreResult(&web_app::internals::CreatePlatformShortcuts), |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 137 | shortcut_data_dir, base::Passed(&shortcut_info), file_handlers_info, |
mitchelljones | 421df67c | 2014-12-18 03:06:50 | [diff] [blame] | 138 | locations, reason)); |
| 139 | } |
| 140 | |
[email protected] | 5bbfbae | 2014-06-18 18:26:38 | [diff] [blame] | 141 | } // namespace |
| 142 | |
| 143 | namespace web_app { |
| 144 | |
| 145 | // The following string is used to build the directory name for |
| 146 | // shortcuts to chrome applications (the kind which are installed |
| 147 | // from a CRX). Application shortcuts to URLs use the {host}_{path} |
| 148 | // for the name of this directory. Hosts can't include an underscore. |
| 149 | // By starting this string with an underscore, we ensure that there |
| 150 | // are no naming conflicts. |
| 151 | static const char kCrxAppPrefix[] = "_crx_"; |
| 152 | |
| 153 | namespace internals { |
| 154 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 155 | base::FilePath GetSanitizedFileName(const base::string16& name) { |
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 156 | #if defined(OS_WIN) |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 157 | base::string16 file_name = name; |
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 158 | #else |
[email protected] | ab6df3b1 | 2013-12-24 23:32:26 | [diff] [blame] | 159 | std::string file_name = base::UTF16ToUTF8(name); |
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 160 | #endif |
[email protected] | 6bc03de | 2014-08-07 23:59:15 | [diff] [blame] | 161 | base::i18n::ReplaceIllegalCharactersInPath(&file_name, '_'); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 162 | return base::FilePath(file_name); |
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | f847e608 | 2011-03-24 00:08:26 | [diff] [blame] | 165 | } // namespace internals |
| 166 | |
[email protected] | 9b1b5fe | 2014-05-15 08:23:17 | [diff] [blame] | 167 | ShortcutInfo::ShortcutInfo() |
[email protected] | 2e0424a | 2014-04-15 13:02:15 | [diff] [blame] | 168 | : is_platform_app(false) { |
| 169 | } |
| 170 | |
[email protected] | 9b1b5fe | 2014-05-15 08:23:17 | [diff] [blame] | 171 | ShortcutInfo::~ShortcutInfo() {} |
[email protected] | 2e0424a | 2014-04-15 13:02:15 | [diff] [blame] | 172 | |
[email protected] | 9b1b5fe | 2014-05-15 08:23:17 | [diff] [blame] | 173 | ShortcutLocations::ShortcutLocations() |
[email protected] | 2e0424a | 2014-04-15 13:02:15 | [diff] [blame] | 174 | : on_desktop(false), |
| 175 | applications_menu_location(APP_MENU_LOCATION_NONE), |
[email protected] | da0349e | 2014-06-11 07:38:28 | [diff] [blame] | 176 | in_quick_launch_bar(false) { |
[email protected] | 2e0424a | 2014-04-15 13:02:15 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | c7264f4 | 2014-06-22 21:23:09 | [diff] [blame] | 179 | #if defined(TOOLKIT_VIEWS) |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 180 | scoped_ptr<ShortcutInfo> GetShortcutInfoForTab( |
| 181 | content::WebContents* web_contents) { |
sdefresne | 45582697 | 2015-04-10 15:25:15 | [diff] [blame] | 182 | const favicon::FaviconDriver* favicon_driver = |
| 183 | favicon::ContentFaviconDriver::FromWebContents(web_contents); |
[email protected] | 2d1d16f | 2014-04-03 18:18:05 | [diff] [blame] | 184 | const extensions::TabHelper* extensions_tab_helper = |
| 185 | extensions::TabHelper::FromWebContents(web_contents); |
| 186 | const WebApplicationInfo& app_info = extensions_tab_helper->web_app_info(); |
| 187 | |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 188 | scoped_ptr<ShortcutInfo> info(new ShortcutInfo); |
[email protected] | 2d1d16f | 2014-04-03 18:18:05 | [diff] [blame] | 189 | info->url = app_info.app_url.is_empty() ? web_contents->GetURL() : |
| 190 | app_info.app_url; |
| 191 | info->title = app_info.title.empty() ? |
| 192 | (web_contents->GetTitle().empty() ? base::UTF8ToUTF16(info->url.spec()) : |
| 193 | web_contents->GetTitle()) : |
| 194 | app_info.title; |
| 195 | info->description = app_info.description; |
sdefresne | 45582697 | 2015-04-10 15:25:15 | [diff] [blame] | 196 | info->favicon.Add(favicon_driver->GetFavicon()); |
[email protected] | 2d1d16f | 2014-04-03 18:18:05 | [diff] [blame] | 197 | |
| 198 | Profile* profile = |
| 199 | Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 200 | info->profile_path = profile->GetPath(); |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 201 | |
| 202 | return info; |
[email protected] | 2d1d16f | 2014-04-03 18:18:05 | [diff] [blame] | 203 | } |
[email protected] | c7264f4 | 2014-06-22 21:23:09 | [diff] [blame] | 204 | #endif |
[email protected] | 2d1d16f | 2014-04-03 18:18:05 | [diff] [blame] | 205 | |
| 206 | #if !defined(OS_WIN) |
| 207 | void UpdateShortcutForTabContents(content::WebContents* web_contents) {} |
| 208 | #endif |
| 209 | |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 210 | scoped_ptr<ShortcutInfo> ShortcutInfoForExtensionAndProfile( |
| 211 | const extensions::Extension* app, |
| 212 | Profile* profile) { |
| 213 | scoped_ptr<ShortcutInfo> shortcut_info(new ShortcutInfo); |
| 214 | shortcut_info->extension_id = app->id(); |
| 215 | shortcut_info->is_platform_app = app->is_platform_app(); |
dominickn | a0e14763 | 2015-07-20 02:43:22 | [diff] [blame] | 216 | shortcut_info->from_bookmark = app->from_bookmark(); |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 217 | shortcut_info->url = extensions::AppLaunchInfo::GetLaunchWebURL(app); |
| 218 | shortcut_info->title = base::UTF8ToUTF16(app->name()); |
| 219 | shortcut_info->description = base::UTF8ToUTF16(app->description()); |
| 220 | shortcut_info->extension_path = app->path(); |
| 221 | shortcut_info->profile_path = profile->GetPath(); |
| 222 | shortcut_info->profile_name = |
[email protected] | f18d37ef | 2014-04-01 11:17:42 | [diff] [blame] | 223 | profile->GetPrefs()->GetString(prefs::kProfileName); |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 224 | shortcut_info->version_for_display = app->GetVersionForDisplay(); |
[email protected] | f18d37ef | 2014-04-01 11:17:42 | [diff] [blame] | 225 | return shortcut_info; |
| 226 | } |
| 227 | |
[email protected] | e886528e | 2014-07-01 10:18:41 | [diff] [blame] | 228 | void GetInfoForApp(const extensions::Extension* extension, |
| 229 | Profile* profile, |
| 230 | const InfoCallback& callback) { |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 231 | scoped_ptr<web_app::ShortcutInfo> shortcut_info( |
| 232 | web_app::ShortcutInfoForExtensionAndProfile(extension, profile)); |
[email protected] | e886528e | 2014-07-01 10:18:41 | [diff] [blame] | 233 | const std::vector<extensions::FileHandlerInfo>* file_handlers = |
| 234 | extensions::FileHandlers::GetFileHandlers(extension); |
| 235 | extensions::FileHandlersInfo file_handlers_info = |
| 236 | file_handlers ? *file_handlers : extensions::FileHandlersInfo(); |
| 237 | |
| 238 | std::vector<extensions::ImageLoader::ImageRepresentation> info_list; |
| 239 | for (size_t i = 0; i < kNumDesiredSizes; ++i) { |
| 240 | int size = kDesiredSizes[i]; |
| 241 | extensions::ExtensionResource resource = |
| 242 | extensions::IconsInfo::GetIconResource( |
| 243 | extension, size, ExtensionIconSet::MATCH_EXACTLY); |
| 244 | if (!resource.empty()) { |
| 245 | info_list.push_back(extensions::ImageLoader::ImageRepresentation( |
| 246 | resource, |
| 247 | extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE, |
| 248 | gfx::Size(size, size), |
| 249 | ui::SCALE_FACTOR_100P)); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (info_list.empty()) { |
| 254 | size_t i = kNumDesiredSizes - 1; |
| 255 | int size = kDesiredSizes[i]; |
| 256 | |
| 257 | // If there is no icon at the desired sizes, we will resize what we can get. |
| 258 | // Making a large icon smaller is preferred to making a small icon larger, |
| 259 | // so look for a larger icon first: |
| 260 | extensions::ExtensionResource resource = |
| 261 | extensions::IconsInfo::GetIconResource( |
| 262 | extension, size, ExtensionIconSet::MATCH_BIGGER); |
| 263 | if (resource.empty()) { |
| 264 | resource = extensions::IconsInfo::GetIconResource( |
| 265 | extension, size, ExtensionIconSet::MATCH_SMALLER); |
| 266 | } |
| 267 | info_list.push_back(extensions::ImageLoader::ImageRepresentation( |
| 268 | resource, |
| 269 | extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE, |
| 270 | gfx::Size(size, size), |
| 271 | ui::SCALE_FACTOR_100P)); |
| 272 | } |
| 273 | |
| 274 | // |info_list| may still be empty at this point, in which case |
| 275 | // LoadImageFamilyAsync will call the OnImageLoaded callback with an empty |
| 276 | // image and exit immediately. |
| 277 | extensions::ImageLoader::Get(profile)->LoadImageFamilyAsync( |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 278 | extension, info_list, |
| 279 | base::Bind(&OnImageLoaded, base::Passed(&shortcut_info), |
| 280 | file_handlers_info, callback)); |
[email protected] | e886528e | 2014-07-01 10:18:41 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void GetShortcutInfoForApp(const extensions::Extension* extension, |
| 284 | Profile* profile, |
| 285 | const ShortcutInfoCallback& callback) { |
| 286 | GetInfoForApp( |
[email protected] | 5bbfbae | 2014-06-18 18:26:38 | [diff] [blame] | 287 | extension, profile, base::Bind(&IgnoreFileHandlersInfo, callback)); |
[email protected] | f18d37ef | 2014-04-01 11:17:42 | [diff] [blame] | 288 | } |
| 289 | |
benwells | a0e8eaa | 2015-02-13 04:32:26 | [diff] [blame] | 290 | bool ShouldCreateShortcutFor(web_app::ShortcutCreationReason reason, |
| 291 | Profile* profile, |
[email protected] | 090e1ee7 | 2014-06-03 13:08:40 | [diff] [blame] | 292 | const extensions::Extension* extension) { |
benwells | a0e8eaa | 2015-02-13 04:32:26 | [diff] [blame] | 293 | // Shortcuts should never be created for component apps, or for apps that |
| 294 | // cannot be shown in the launcher. |
| 295 | if (extension->location() == extensions::Manifest::COMPONENT || |
| 296 | !extensions::ui_util::CanDisplayInAppLauncher(extension, profile)) { |
| 297 | return false; |
| 298 | } |
mitchelljones | 1c30102 | 2014-12-11 00:38:47 | [diff] [blame] | 299 | |
benwells | a0e8eaa | 2015-02-13 04:32:26 | [diff] [blame] | 300 | // Otherwise, always create shortcuts for v2 packaged apps. |
| 301 | if (extension->is_platform_app()) |
| 302 | return true; |
| 303 | |
mitchelljones | 1c30102 | 2014-12-11 00:38:47 | [diff] [blame] | 304 | #if defined(OS_MACOSX) |
mitchelljones | 3e8befd | 2015-02-13 01:06:12 | [diff] [blame] | 305 | if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 306 | switches::kDisableHostedAppShimCreation)) { |
benwells | a0e8eaa | 2015-02-13 04:32:26 | [diff] [blame] | 307 | return extension->is_hosted_app(); |
mitchelljones | 58e35915 | 2014-12-19 01:10:13 | [diff] [blame] | 308 | } |
mitchelljones | 1c30102 | 2014-12-11 00:38:47 | [diff] [blame] | 309 | |
benwells | a0e8eaa | 2015-02-13 04:32:26 | [diff] [blame] | 310 | return false; |
| 311 | #else |
| 312 | // For other platforms, allow shortcut creation if it was explicitly |
| 313 | // requested by the user (i.e. is not automatic). |
| 314 | return reason == SHORTCUT_CREATION_BY_USER; |
| 315 | #endif |
[email protected] | 090e1ee7 | 2014-06-03 13:08:40 | [diff] [blame] | 316 | } |
| 317 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 318 | base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path, |
| 319 | const std::string& extension_id, |
| 320 | const GURL& url) { |
[email protected] | c002e75 | 2012-08-10 12:50:11 | [diff] [blame] | 321 | DCHECK(!profile_path.empty()); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 322 | base::FilePath app_data_dir(profile_path.Append(chrome::kWebAppDirname)); |
[email protected] | 2b80909f | 2012-02-24 04:50:21 | [diff] [blame] | 323 | |
| 324 | if (!extension_id.empty()) { |
| 325 | return app_data_dir.AppendASCII( |
| 326 | GenerateApplicationNameFromExtensionId(extension_id)); |
| 327 | } |
| 328 | |
| 329 | std::string host(url.host()); |
| 330 | std::string scheme(url.has_scheme() ? url.scheme() : "http"); |
| 331 | std::string port(url.has_port() ? url.port() : "80"); |
| 332 | std::string scheme_port(scheme + "_" + port); |
| 333 | |
| 334 | #if defined(OS_WIN) |
[email protected] | ab6df3b1 | 2013-12-24 23:32:26 | [diff] [blame] | 335 | base::FilePath::StringType host_path(base::UTF8ToUTF16(host)); |
| 336 | base::FilePath::StringType scheme_port_path(base::UTF8ToUTF16(scheme_port)); |
[email protected] | 2b80909f | 2012-02-24 04:50:21 | [diff] [blame] | 337 | #elif defined(OS_POSIX) |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 338 | base::FilePath::StringType host_path(host); |
| 339 | base::FilePath::StringType scheme_port_path(scheme_port); |
[email protected] | 2b80909f | 2012-02-24 04:50:21 | [diff] [blame] | 340 | #endif |
| 341 | |
| 342 | return app_data_dir.Append(host_path).Append(scheme_port_path); |
| 343 | } |
| 344 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 345 | base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path, |
| 346 | const extensions::Extension& extension) { |
[email protected] | 2b80909f | 2012-02-24 04:50:21 | [diff] [blame] | 347 | return GetWebAppDataDirectory( |
[email protected] | 6b414c23 | 2013-06-05 07:53:34 | [diff] [blame] | 348 | profile_path, |
| 349 | extension.id(), |
| 350 | GURL(extensions::AppLaunchInfo::GetLaunchWebURL(&extension))); |
[email protected] | 2b80909f | 2012-02-24 04:50:21 | [diff] [blame] | 351 | } |
| 352 | |
[email protected] | 9b1b5fe | 2014-05-15 08:23:17 | [diff] [blame] | 353 | std::string GenerateApplicationNameFromInfo(const ShortcutInfo& shortcut_info) { |
[email protected] | 238dd7b | 2014-05-23 07:07:20 | [diff] [blame] | 354 | if (!shortcut_info.extension_id.empty()) |
[email protected] | 9b1b5fe | 2014-05-15 08:23:17 | [diff] [blame] | 355 | return GenerateApplicationNameFromExtensionId(shortcut_info.extension_id); |
[email protected] | 238dd7b | 2014-05-23 07:07:20 | [diff] [blame] | 356 | else |
[email protected] | 9b1b5fe | 2014-05-15 08:23:17 | [diff] [blame] | 357 | return GenerateApplicationNameFromURL(shortcut_info.url); |
[email protected] | a0b60cfd | 2011-04-06 18:02:41 | [diff] [blame] | 358 | } |
| 359 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 360 | std::string GenerateApplicationNameFromURL(const GURL& url) { |
[email protected] | 86b5401 | 2009-11-19 09:18:50 | [diff] [blame] | 361 | std::string t; |
| 362 | t.append(url.host()); |
| 363 | t.append("_"); |
| 364 | t.append(url.path()); |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 365 | return t; |
[email protected] | 86b5401 | 2009-11-19 09:18:50 | [diff] [blame] | 366 | } |
| 367 | |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 368 | std::string GenerateApplicationNameFromExtensionId(const std::string& id) { |
[email protected] | 9b1b5fe | 2014-05-15 08:23:17 | [diff] [blame] | 369 | std::string t(kCrxAppPrefix); |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 370 | t.append(id); |
| 371 | return t; |
| 372 | } |
| 373 | |
[email protected] | edee3faf | 2011-05-25 21:40:10 | [diff] [blame] | 374 | std::string GetExtensionIdFromApplicationName(const std::string& app_name) { |
| 375 | std::string prefix(kCrxAppPrefix); |
| 376 | if (app_name.substr(0, prefix.length()) != prefix) |
| 377 | return std::string(); |
| 378 | return app_name.substr(prefix.length()); |
| 379 | } |
| 380 | |
[email protected] | 371bd2e | 2014-07-01 07:10:02 | [diff] [blame] | 381 | void CreateShortcutsWithInfo( |
| 382 | ShortcutCreationReason reason, |
| 383 | const ShortcutLocations& locations, |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 384 | scoped_ptr<ShortcutInfo> shortcut_info, |
[email protected] | 371bd2e | 2014-07-01 07:10:02 | [diff] [blame] | 385 | const extensions::FileHandlersInfo& file_handlers_info) { |
anujk.sharma | 9fcea9a | 2015-04-22 06:57:49 | [diff] [blame] | 386 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 0b7df36d | 2012-07-11 09:50:47 | [diff] [blame] | 387 | |
benwells | 863dc8b5 | 2015-01-20 06:47:01 | [diff] [blame] | 388 | // If the shortcut is for an application shortcut with the new bookmark app |
| 389 | // flow disabled, there will be no corresponding extension. |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 390 | if (!shortcut_info->extension_id.empty()) { |
benwells | 863dc8b5 | 2015-01-20 06:47:01 | [diff] [blame] | 391 | // It's possible for the extension to be deleted before we get here. |
| 392 | // For example, creating a hosted app from a website. Double check that |
| 393 | // it still exists. |
| 394 | Profile* profile = g_browser_process->profile_manager()->GetProfileByPath( |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 395 | shortcut_info->profile_path); |
benwells | 863dc8b5 | 2015-01-20 06:47:01 | [diff] [blame] | 396 | if (!profile) |
| 397 | return; |
mitchelljones | 1c30102 | 2014-12-11 00:38:47 | [diff] [blame] | 398 | |
benwells | 863dc8b5 | 2015-01-20 06:47:01 | [diff] [blame] | 399 | extensions::ExtensionRegistry* registry = |
| 400 | extensions::ExtensionRegistry::Get(profile); |
| 401 | const extensions::Extension* extension = registry->GetExtensionById( |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 402 | shortcut_info->extension_id, extensions::ExtensionRegistry::EVERYTHING); |
benwells | 863dc8b5 | 2015-01-20 06:47:01 | [diff] [blame] | 403 | if (!extension) |
| 404 | return; |
| 405 | } |
mitchelljones | 1c30102 | 2014-12-11 00:38:47 | [diff] [blame] | 406 | |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 407 | ScheduleCreatePlatformShortcut(reason, locations, shortcut_info.Pass(), |
mitchelljones | 421df67c | 2014-12-18 03:06:50 | [diff] [blame] | 408 | file_handlers_info); |
| 409 | } |
| 410 | |
| 411 | void CreateNonAppShortcut(const ShortcutLocations& locations, |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 412 | scoped_ptr<ShortcutInfo> shortcut_info) { |
mitchelljones | 421df67c | 2014-12-18 03:06:50 | [diff] [blame] | 413 | ScheduleCreatePlatformShortcut(SHORTCUT_CREATION_AUTOMATED, locations, |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 414 | shortcut_info.Pass(), |
| 415 | extensions::FileHandlersInfo()); |
[email protected] | f8a3129 | 2014-04-04 05:45:56 | [diff] [blame] | 416 | } |
| 417 | |
[email protected] | 9b1b5fe | 2014-05-15 08:23:17 | [diff] [blame] | 418 | void CreateShortcuts(ShortcutCreationReason reason, |
| 419 | const ShortcutLocations& locations, |
| 420 | Profile* profile, |
| 421 | const extensions::Extension* app) { |
anujk.sharma | 9fcea9a | 2015-04-22 06:57:49 | [diff] [blame] | 422 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 1488508 | 2014-04-08 04:41:28 | [diff] [blame] | 423 | |
benwells | a0e8eaa | 2015-02-13 04:32:26 | [diff] [blame] | 424 | if (!ShouldCreateShortcutFor(reason, profile, app)) |
[email protected] | 090e1ee7 | 2014-06-03 13:08:40 | [diff] [blame] | 425 | return; |
| 426 | |
[email protected] | e886528e | 2014-07-01 10:18:41 | [diff] [blame] | 427 | GetInfoForApp( |
[email protected] | 5bbfbae | 2014-06-18 18:26:38 | [diff] [blame] | 428 | app, profile, base::Bind(&CreateShortcutsWithInfo, reason, locations)); |
[email protected] | 1488508 | 2014-04-08 04:41:28 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app) { |
anujk.sharma | 9fcea9a | 2015-04-22 06:57:49 | [diff] [blame] | 432 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | f8a3129 | 2014-04-04 05:45:56 | [diff] [blame] | 433 | |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 434 | scoped_ptr<ShortcutInfo> shortcut_info( |
| 435 | ShortcutInfoForExtensionAndProfile(app, profile)); |
| 436 | base::FilePath shortcut_data_dir = GetShortcutDataDir(*shortcut_info); |
[email protected] | f8a3129 | 2014-04-04 05:45:56 | [diff] [blame] | 437 | BrowserThread::PostTask( |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 438 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 238dd7b | 2014-05-23 07:07:20 | [diff] [blame] | 439 | base::Bind(&web_app::internals::DeletePlatformShortcuts, |
mgiuca | b21fe8ee | 2015-04-09 04:00:44 | [diff] [blame] | 440 | shortcut_data_dir, base::Passed(&shortcut_info))); |
[email protected] | ed543187 | 2009-11-17 08:39:51 | [diff] [blame] | 441 | } |
| 442 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 443 | void UpdateAllShortcuts(const base::string16& old_app_title, |
[email protected] | 1488508 | 2014-04-08 04:41:28 | [diff] [blame] | 444 | Profile* profile, |
| 445 | const extensions::Extension* app) { |
anujk.sharma | 9fcea9a | 2015-04-22 06:57:49 | [diff] [blame] | 446 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | e66ba95 | 2012-10-09 09:59:44 | [diff] [blame] | 447 | |
[email protected] | e886528e | 2014-07-01 10:18:41 | [diff] [blame] | 448 | GetInfoForApp(app, |
| 449 | profile, |
| 450 | base::Bind(&UpdateAllShortcutsForShortcutInfo, old_app_title)); |
[email protected] | 8806d3b | 2012-04-13 06:46:34 | [diff] [blame] | 451 | } |
| 452 | |
[email protected] | 12ea22a | 2009-11-19 07:17:23 | [diff] [blame] | 453 | bool IsValidUrl(const GURL& url) { |
| 454 | static const char* const kValidUrlSchemes[] = { |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 455 | url::kFileScheme, |
| 456 | url::kFileSystemScheme, |
| 457 | url::kFtpScheme, |
[email protected] | e8ca69c | 2014-05-07 15:31:19 | [diff] [blame] | 458 | url::kHttpScheme, |
| 459 | url::kHttpsScheme, |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 460 | extensions::kExtensionScheme, |
[email protected] | 12ea22a | 2009-11-19 07:17:23 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | for (size_t i = 0; i < arraysize(kValidUrlSchemes); ++i) { |
| 464 | if (url.SchemeIs(kValidUrlSchemes[i])) |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | return false; |
| 469 | } |
| 470 | |
[email protected] | be3df2b | 2010-06-25 21:39:07 | [diff] [blame] | 471 | #if defined(TOOLKIT_VIEWS) |
[email protected] | 38789d8 | 2010-11-17 06:03:44 | [diff] [blame] | 472 | void GetIconsInfo(const WebApplicationInfo& app_info, |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 473 | IconInfoList* icons) { |
| 474 | DCHECK(icons); |
| 475 | |
| 476 | icons->clear(); |
| 477 | for (size_t i = 0; i < app_info.icons.size(); ++i) { |
| 478 | // We only take square shaped icons (i.e. width == height). |
| 479 | if (app_info.icons[i].width == app_info.icons[i].height) { |
| 480 | icons->push_back(app_info.icons[i]); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | std::sort(icons->begin(), icons->end(), &IconPrecedes); |
| 485 | } |
[email protected] | be3df2b | 2010-06-25 21:39:07 | [diff] [blame] | 486 | #endif |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 487 | |
[email protected] | f93a7745 | 2013-09-02 05:26:35 | [diff] [blame] | 488 | #if defined(OS_LINUX) |
[email protected] | a0b60cfd | 2011-04-06 18:02:41 | [diff] [blame] | 489 | std::string GetWMClassFromAppName(std::string app_name) { |
[email protected] | 6bc03de | 2014-08-07 23:59:15 | [diff] [blame] | 490 | base::i18n::ReplaceIllegalCharactersInPath(&app_name, '_'); |
[email protected] | 466c986 | 2013-12-03 22:05:28 | [diff] [blame] | 491 | base::TrimString(app_name, "_", &app_name); |
[email protected] | a0b60cfd | 2011-04-06 18:02:41 | [diff] [blame] | 492 | return app_name; |
| 493 | } |
| 494 | #endif |
| 495 | |
[email protected] | f847e608 | 2011-03-24 00:08:26 | [diff] [blame] | 496 | } // namespace web_app |