[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" |
[email protected] | ed543187 | 2009-11-17 08:39:51 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | a0b60cfd | 2011-04-06 18:02:41 | [diff] [blame] | 10 | #include "base/i18n/file_util_icu.h" |
[email protected] | a0b60cfd | 2011-04-06 18:02:41 | [diff] [blame] | 11 | #include "base/string_util.h" |
[email protected] | 7f070d4 | 2011-03-09 20:25:32 | [diff] [blame] | 12 | #include "base/threading/thread.h" |
[email protected] | 1cb92b8 | 2010-03-08 23:12:15 | [diff] [blame] | 13 | #include "base/utf_string_conversions.h" |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 14 | #include "chrome/common/chrome_constants.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame^] | 15 | #include "chrome/common/extensions/extension.h" |
[email protected] | 12ea22a | 2009-11-19 07:17:23 | [diff] [blame] | 16 | #include "chrome/common/url_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 17 | #include "content/public/browser/browser_thread.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame^] | 18 | #include "extensions/common/constants.h" |
[email protected] | ed543187 | 2009-11-17 08:39:51 | [diff] [blame] | 19 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 20 | using content::BrowserThread; |
| 21 | |
[email protected] | ed543187 | 2009-11-17 08:39:51 | [diff] [blame] | 22 | namespace { |
| 23 | |
[email protected] | 16f76563 | 2010-09-21 21:31:27 | [diff] [blame] | 24 | #if defined(TOOLKIT_VIEWS) |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 25 | // Predicator for sorting images from largest to smallest. |
[email protected] | 38789d8 | 2010-11-17 06:03:44 | [diff] [blame] | 26 | bool IconPrecedes(const WebApplicationInfo::IconInfo& left, |
| 27 | const WebApplicationInfo::IconInfo& right) { |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 28 | return left.width < right.width; |
| 29 | } |
[email protected] | 16f76563 | 2010-09-21 21:31:27 | [diff] [blame] | 30 | #endif |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 31 | |
[email protected] | c002e75 | 2012-08-10 12:50:11 | [diff] [blame] | 32 | void DeleteShortcutsOnFileThread( |
| 33 | const ShellIntegration::ShortcutInfo& shortcut_info) { |
| 34 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 35 | |
| 36 | FilePath shortcut_data_dir = web_app::GetWebAppDataDirectory( |
| 37 | shortcut_info.profile_path, shortcut_info.extension_id, GURL()); |
| 38 | return web_app::internals::DeletePlatformShortcuts( |
| 39 | shortcut_data_dir, shortcut_info); |
| 40 | } |
| 41 | |
[email protected] | e66ba95 | 2012-10-09 09:59:44 | [diff] [blame] | 42 | void UpdateShortcutsOnFileThread( |
| 43 | const ShellIntegration::ShortcutInfo& shortcut_info) { |
| 44 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 45 | |
| 46 | FilePath shortcut_data_dir = web_app::GetWebAppDataDirectory( |
| 47 | shortcut_info.profile_path, shortcut_info.extension_id, GURL()); |
| 48 | return web_app::internals::UpdatePlatformShortcuts( |
| 49 | shortcut_data_dir, shortcut_info); |
| 50 | } |
| 51 | |
[email protected] | f847e608 | 2011-03-24 00:08:26 | [diff] [blame] | 52 | } // namespace |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 53 | |
[email protected] | ed543187 | 2009-11-17 08:39:51 | [diff] [blame] | 54 | namespace web_app { |
| 55 | |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 56 | // The following string is used to build the directory name for |
| 57 | // shortcuts to chrome applications (the kind which are installed |
| 58 | // from a CRX). Application shortcuts to URLs use the {host}_{path} |
| 59 | // for the name of this directory. Hosts can't include an underscore. |
| 60 | // By starting this string with an underscore, we ensure that there |
| 61 | // are no naming conflicts. |
| 62 | static const char* kCrxAppPrefix = "_crx_"; |
| 63 | |
[email protected] | f847e608 | 2011-03-24 00:08:26 | [diff] [blame] | 64 | namespace internals { |
| 65 | |
[email protected] | b6b7222 | 2012-02-11 02:04:13 | [diff] [blame] | 66 | FilePath GetSanitizedFileName(const string16& name) { |
| 67 | #if defined(OS_WIN) |
| 68 | string16 file_name = name; |
| 69 | #else |
| 70 | std::string file_name = UTF16ToUTF8(name); |
| 71 | #endif |
| 72 | file_util::ReplaceIllegalCharactersInPath(&file_name, '_'); |
| 73 | return FilePath(file_name); |
| 74 | } |
| 75 | |
[email protected] | f847e608 | 2011-03-24 00:08:26 | [diff] [blame] | 76 | } // namespace internals |
| 77 | |
[email protected] | 2b80909f | 2012-02-24 04:50:21 | [diff] [blame] | 78 | FilePath GetWebAppDataDirectory(const FilePath& profile_path, |
| 79 | const std::string& extension_id, |
| 80 | const GURL& url) { |
[email protected] | c002e75 | 2012-08-10 12:50:11 | [diff] [blame] | 81 | DCHECK(!profile_path.empty()); |
[email protected] | 2b80909f | 2012-02-24 04:50:21 | [diff] [blame] | 82 | FilePath app_data_dir(profile_path.Append(chrome::kWebAppDirname)); |
| 83 | |
| 84 | if (!extension_id.empty()) { |
| 85 | return app_data_dir.AppendASCII( |
| 86 | GenerateApplicationNameFromExtensionId(extension_id)); |
| 87 | } |
| 88 | |
| 89 | std::string host(url.host()); |
| 90 | std::string scheme(url.has_scheme() ? url.scheme() : "http"); |
| 91 | std::string port(url.has_port() ? url.port() : "80"); |
| 92 | std::string scheme_port(scheme + "_" + port); |
| 93 | |
| 94 | #if defined(OS_WIN) |
| 95 | FilePath::StringType host_path(UTF8ToUTF16(host)); |
| 96 | FilePath::StringType scheme_port_path(UTF8ToUTF16(scheme_port)); |
| 97 | #elif defined(OS_POSIX) |
| 98 | FilePath::StringType host_path(host); |
| 99 | FilePath::StringType scheme_port_path(scheme_port); |
| 100 | #endif |
| 101 | |
| 102 | return app_data_dir.Append(host_path).Append(scheme_port_path); |
| 103 | } |
| 104 | |
| 105 | FilePath GetWebAppDataDirectory(const FilePath& profile_path, |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 106 | const extensions::Extension& extension) { |
[email protected] | 2b80909f | 2012-02-24 04:50:21 | [diff] [blame] | 107 | return GetWebAppDataDirectory( |
| 108 | profile_path, extension.id(), GURL(extension.launch_web_url())); |
| 109 | } |
| 110 | |
[email protected] | a0b60cfd | 2011-04-06 18:02:41 | [diff] [blame] | 111 | std::string GenerateApplicationNameFromInfo( |
| 112 | const ShellIntegration::ShortcutInfo& shortcut_info) { |
| 113 | if (!shortcut_info.extension_id.empty()) { |
| 114 | return web_app::GenerateApplicationNameFromExtensionId( |
| 115 | shortcut_info.extension_id); |
| 116 | } else { |
| 117 | return web_app::GenerateApplicationNameFromURL( |
| 118 | shortcut_info.url); |
| 119 | } |
| 120 | } |
| 121 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 122 | std::string GenerateApplicationNameFromURL(const GURL& url) { |
[email protected] | 86b5401 | 2009-11-19 09:18:50 | [diff] [blame] | 123 | std::string t; |
| 124 | t.append(url.host()); |
| 125 | t.append("_"); |
| 126 | t.append(url.path()); |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 127 | return t; |
[email protected] | 86b5401 | 2009-11-19 09:18:50 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | 2f1c09d | 2011-01-14 14:58:14 | [diff] [blame] | 130 | std::string GenerateApplicationNameFromExtensionId(const std::string& id) { |
| 131 | std::string t(web_app::kCrxAppPrefix); |
| 132 | t.append(id); |
| 133 | return t; |
| 134 | } |
| 135 | |
[email protected] | edee3faf | 2011-05-25 21:40:10 | [diff] [blame] | 136 | std::string GetExtensionIdFromApplicationName(const std::string& app_name) { |
| 137 | std::string prefix(kCrxAppPrefix); |
| 138 | if (app_name.substr(0, prefix.length()) != prefix) |
| 139 | return std::string(); |
| 140 | return app_name.substr(prefix.length()); |
| 141 | } |
| 142 | |
[email protected] | c002e75 | 2012-08-10 12:50:11 | [diff] [blame] | 143 | void CreateShortcuts(const ShellIntegration::ShortcutInfo& shortcut_info) { |
[email protected] | 8806d3b | 2012-04-13 06:46:34 | [diff] [blame] | 144 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 145 | |
[email protected] | 8721270 | 2011-12-08 21:17:19 | [diff] [blame] | 146 | BrowserThread::PostTask( |
| 147 | BrowserThread::FILE, |
| 148 | FROM_HERE, |
[email protected] | c002e75 | 2012-08-10 12:50:11 | [diff] [blame] | 149 | base::Bind(base::IgnoreResult(&CreateShortcutsOnFileThread), |
| 150 | shortcut_info)); |
[email protected] | 0b7df36d | 2012-07-11 09:50:47 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | c002e75 | 2012-08-10 12:50:11 | [diff] [blame] | 153 | void DeleteAllShortcuts(const ShellIntegration::ShortcutInfo& shortcut_info) { |
[email protected] | 0b7df36d | 2012-07-11 09:50:47 | [diff] [blame] | 154 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 155 | |
| 156 | BrowserThread::PostTask( |
| 157 | BrowserThread::FILE, |
| 158 | FROM_HERE, |
[email protected] | c002e75 | 2012-08-10 12:50:11 | [diff] [blame] | 159 | base::Bind(&DeleteShortcutsOnFileThread, shortcut_info)); |
[email protected] | ed543187 | 2009-11-17 08:39:51 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | e66ba95 | 2012-10-09 09:59:44 | [diff] [blame] | 162 | void UpdateAllShortcuts(const ShellIntegration::ShortcutInfo& shortcut_info) { |
| 163 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 164 | |
| 165 | BrowserThread::PostTask( |
| 166 | BrowserThread::FILE, |
| 167 | FROM_HERE, |
| 168 | base::Bind(&UpdateShortcutsOnFileThread, shortcut_info)); |
| 169 | } |
| 170 | |
[email protected] | c002e75 | 2012-08-10 12:50:11 | [diff] [blame] | 171 | bool CreateShortcutsOnFileThread( |
[email protected] | 8806d3b | 2012-04-13 06:46:34 | [diff] [blame] | 172 | const ShellIntegration::ShortcutInfo& shortcut_info) { |
| 173 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 174 | |
| 175 | FilePath shortcut_data_dir = GetWebAppDataDirectory( |
[email protected] | c002e75 | 2012-08-10 12:50:11 | [diff] [blame] | 176 | shortcut_info.profile_path, shortcut_info.extension_id, |
| 177 | shortcut_info.url); |
| 178 | return internals::CreatePlatformShortcuts(shortcut_data_dir, shortcut_info); |
[email protected] | 8806d3b | 2012-04-13 06:46:34 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | 12ea22a | 2009-11-19 07:17:23 | [diff] [blame] | 181 | bool IsValidUrl(const GURL& url) { |
| 182 | static const char* const kValidUrlSchemes[] = { |
| 183 | chrome::kFileScheme, |
[email protected] | f1f8639 | 2012-04-03 13:51:58 | [diff] [blame] | 184 | chrome::kFileSystemScheme, |
[email protected] | 12ea22a | 2009-11-19 07:17:23 | [diff] [blame] | 185 | chrome::kFtpScheme, |
| 186 | chrome::kHttpScheme, |
| 187 | chrome::kHttpsScheme, |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame^] | 188 | extensions::kExtensionScheme, |
[email protected] | 12ea22a | 2009-11-19 07:17:23 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | for (size_t i = 0; i < arraysize(kValidUrlSchemes); ++i) { |
| 192 | if (url.SchemeIs(kValidUrlSchemes[i])) |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | return false; |
| 197 | } |
| 198 | |
[email protected] | be3df2b | 2010-06-25 21:39:07 | [diff] [blame] | 199 | #if defined(TOOLKIT_VIEWS) |
[email protected] | 38789d8 | 2010-11-17 06:03:44 | [diff] [blame] | 200 | void GetIconsInfo(const WebApplicationInfo& app_info, |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 201 | IconInfoList* icons) { |
| 202 | DCHECK(icons); |
| 203 | |
| 204 | icons->clear(); |
| 205 | for (size_t i = 0; i < app_info.icons.size(); ++i) { |
| 206 | // We only take square shaped icons (i.e. width == height). |
| 207 | if (app_info.icons[i].width == app_info.icons[i].height) { |
| 208 | icons->push_back(app_info.icons[i]); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | std::sort(icons->begin(), icons->end(), &IconPrecedes); |
| 213 | } |
[email protected] | be3df2b | 2010-06-25 21:39:07 | [diff] [blame] | 214 | #endif |
[email protected] | eabfdae9 | 2009-12-11 06:13:51 | [diff] [blame] | 215 | |
[email protected] | a13283cc | 2012-04-05 00:21:22 | [diff] [blame] | 216 | #if defined(TOOLKIT_GTK) |
[email protected] | a0b60cfd | 2011-04-06 18:02:41 | [diff] [blame] | 217 | std::string GetWMClassFromAppName(std::string app_name) { |
| 218 | file_util::ReplaceIllegalCharactersInPath(&app_name, '_'); |
| 219 | TrimString(app_name, "_", &app_name); |
| 220 | return app_name; |
| 221 | } |
| 222 | #endif |
| 223 | |
[email protected] | f847e608 | 2011-03-24 00:08:26 | [diff] [blame] | 224 | } // namespace web_app |