web_app_win.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 3// found in the LICENSE file. 4 5#include "chrome/browser/web_applications/web_app.h" 6 7#include <shlobj.h> 8 9#include "base/command_line.h" 10#include "base/file_util.h" 11#include "base/logging.h" 12#include "base/md5.h" 13#include "base/path_service.h" 14#include "base/stringprintf.h" 15#include "base/strings/string_piece.h" 16#include "base/utf_string_conversions.h" 17#include "base/win/shortcut.h" 18#include "base/win/windows_version.h" 19#include "chrome/common/chrome_switches.h" 20#include "chrome/installer/launcher_support/chrome_launcher_support.h" 21#include "chrome/installer/util/util_constants.h" 22#include "content/public/browser/browser_thread.h" 23#include "ui/gfx/icon_util.h" 24#include "ui/gfx/image/image.h" 25#include "ui/gfx/image/image_family.h" 26 27namespace { 28 29const base::FilePath::CharType kIconChecksumFileExt[] = 30 FILE_PATH_LITERAL(".ico.md5"); 31 32// Width and height of icons exported to .ico files. 33 34// Calculates checksum of an icon family using MD5. 35// The checksum is derived from all of the icons in the family. 36void GetImageCheckSum(const gfx::ImageFamily& image, base::MD5Digest* digest) { 37 DCHECK(digest); 38 base::MD5Context md5_context; 39 base::MD5Init(&md5_context); 40 41 for (gfx::ImageFamily::const_iterator it = image.begin(); it != image.end(); 42 ++it) { 43 SkBitmap bitmap = it->AsBitmap(); 44 45 SkAutoLockPixels image_lock(bitmap); 46 base::StringPiece image_data( 47 reinterpret_cast<const char*>(bitmap.getPixels()), bitmap.getSize()); 48 base::MD5Update(&md5_context, image_data); 49 } 50 51 base::MD5Final(digest, &md5_context); 52} 53 54// Saves |image| as an |icon_file| with the checksum. 55bool SaveIconWithCheckSum(const base::FilePath& icon_file, 56 const gfx::ImageFamily& image) { 57 if (!IconUtil::CreateIconFileFromImageFamily(image, icon_file)) 58 return false; 59 60 base::MD5Digest digest; 61 GetImageCheckSum(image, &digest); 62 63 base::FilePath cheksum_file(icon_file.ReplaceExtension(kIconChecksumFileExt)); 64 return file_util::WriteFile(cheksum_file, 65 reinterpret_cast<const char*>(&digest), 66 sizeof(digest)) == sizeof(digest); 67} 68 69// Returns true if |icon_file| is missing or different from |image|. 70bool ShouldUpdateIcon(const base::FilePath& icon_file, 71 const gfx::ImageFamily& image) { 72 base::FilePath checksum_file( 73 icon_file.ReplaceExtension(kIconChecksumFileExt)); 74 75 // Returns true if icon_file or checksum file is missing. 76 if (!file_util::PathExists(icon_file) || 77 !file_util::PathExists(checksum_file)) 78 return true; 79 80 base::MD5Digest persisted_image_checksum; 81 if (sizeof(persisted_image_checksum) != file_util::ReadFile(checksum_file, 82 reinterpret_cast<char*>(&persisted_image_checksum), 83 sizeof(persisted_image_checksum))) 84 return true; 85 86 base::MD5Digest downloaded_image_checksum; 87 GetImageCheckSum(image, &downloaded_image_checksum); 88 89 // Update icon if checksums are not equal. 90 return memcmp(&persisted_image_checksum, &downloaded_image_checksum, 91 sizeof(base::MD5Digest)) != 0; 92} 93 94bool ShortcutIsForProfile(const base::FilePath& shortcut_file_name, 95 const base::FilePath& profile_path) { 96 string16 cmd_line_string; 97 if (base::win::ResolveShortcut(shortcut_file_name, NULL, &cmd_line_string)) { 98 cmd_line_string = L"program " + cmd_line_string; 99 CommandLine shortcut_cmd_line = CommandLine::FromString(cmd_line_string); 100 return shortcut_cmd_line.HasSwitch(switches::kProfileDirectory) && 101 shortcut_cmd_line.GetSwitchValuePath(switches::kProfileDirectory) == 102 profile_path.BaseName(); 103 } 104 105 return false; 106} 107 108std::vector<base::FilePath> MatchingShortcutsForProfileAndExtension( 109 const base::FilePath& shortcut_path, 110 const base::FilePath& profile_path, 111 const string16& shortcut_name) { 112 std::vector<base::FilePath> shortcut_paths; 113 base::FilePath base_path = shortcut_path. 114 Append(web_app::internals::GetSanitizedFileName(shortcut_name)). 115 AddExtension(FILE_PATH_LITERAL(".lnk")); 116 117 const int fileNamesToCheck = 10; 118 for (int i = 0; i < fileNamesToCheck; ++i) { 119 base::FilePath shortcut_file = base_path; 120 if (i) { 121 shortcut_file = shortcut_file.InsertBeforeExtensionASCII( 122 base::StringPrintf(" (%d)", i)); 123 } 124 if (file_util::PathExists(shortcut_file) && 125 ShortcutIsForProfile(shortcut_file, profile_path)) { 126 shortcut_paths.push_back(shortcut_file); 127 } 128 } 129 return shortcut_paths; 130} 131 132} // namespace 133 134namespace web_app { 135 136namespace internals { 137 138// Saves |image| to |icon_file| if the file is outdated and refresh shell's 139// icon cache to ensure correct icon is displayed. Returns true if icon_file 140// is up to date or successfully updated. 141bool CheckAndSaveIcon(const base::FilePath& icon_file, 142 const gfx::ImageFamily& image) { 143 if (ShouldUpdateIcon(icon_file, image)) { 144 if (SaveIconWithCheckSum(icon_file, image)) { 145 // Refresh shell's icon cache. This call is quite disruptive as user would 146 // see explorer rebuilding the icon cache. It would be great that we find 147 // a better way to achieve this. 148 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT, 149 NULL, NULL); 150 } else { 151 return false; 152 } 153 } 154 155 return true; 156} 157 158bool CreatePlatformShortcuts( 159 const base::FilePath& web_app_path, 160 const ShellIntegration::ShortcutInfo& shortcut_info, 161 const ShellIntegration::ShortcutLocations& creation_locations) { 162 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 163 164 // Shortcut paths under which to create shortcuts. 165 std::vector<base::FilePath> shortcut_paths = 166 GetShortcutPaths(creation_locations); 167 168 bool pin_to_taskbar = creation_locations.in_quick_launch_bar && 169 (base::win::GetVersion() >= base::win::VERSION_WIN7); 170 171 // Create/update the shortcut in the web app path for the "Pin To Taskbar" 172 // option in Win7. We use the web app path shortcut because we will overwrite 173 // it rather than appending unique numbers if the shortcut already exists. 174 // This prevents pinned apps from having unique numbers in their names. 175 if (pin_to_taskbar) 176 shortcut_paths.push_back(web_app_path); 177 178 if (shortcut_paths.empty()) 179 return false; 180 181 // Ensure web_app_path exists. 182 if (!file_util::PathExists(web_app_path) && 183 !file_util::CreateDirectory(web_app_path)) { 184 return false; 185 } 186 187 // Generates file name to use with persisted ico and shortcut file. 188 base::FilePath file_name = 189 web_app::internals::GetSanitizedFileName(shortcut_info.title); 190 191 // Creates an ico file to use with shortcut. 192 base::FilePath icon_file = web_app_path.Append(file_name).AddExtension( 193 FILE_PATH_LITERAL(".ico")); 194 if (!web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon)) { 195 return false; 196 } 197 198 base::FilePath chrome_exe; 199 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 200 NOTREACHED(); 201 return false; 202 } 203 204 // Working directory. 205 base::FilePath working_dir(chrome_exe.DirName()); 206 207 CommandLine cmd_line(CommandLine::NO_PROGRAM); 208 cmd_line = ShellIntegration::CommandLineArgsForLauncher(shortcut_info.url, 209 shortcut_info.extension_id, shortcut_info.profile_path); 210 211 // TODO(evan): we rely on the fact that command_line_string() is 212 // properly quoted for a Windows command line. The method on 213 // CommandLine should probably be renamed to better reflect that 214 // fact. 215 string16 wide_switches(cmd_line.GetCommandLineString()); 216 217 // Sanitize description 218 string16 description = shortcut_info.description; 219 if (description.length() >= MAX_PATH) 220 description.resize(MAX_PATH - 1); 221 222 // Generates app id from web app url and profile path. 223 std::string app_name(web_app::GenerateApplicationNameFromInfo(shortcut_info)); 224 string16 app_id(ShellIntegration::GetAppModelIdForProfile( 225 UTF8ToUTF16(app_name), shortcut_info.profile_path)); 226 227 bool success = true; 228 for (size_t i = 0; i < shortcut_paths.size(); ++i) { 229 base::FilePath shortcut_file = shortcut_paths[i].Append(file_name). 230 AddExtension(installer::kLnkExt); 231 if (shortcut_paths[i] != web_app_path) { 232 int unique_number = 233 file_util::GetUniquePathNumber(shortcut_file, FILE_PATH_LITERAL("")); 234 if (unique_number == -1) { 235 success = false; 236 continue; 237 } else if (unique_number > 0) { 238 shortcut_file = shortcut_file.InsertBeforeExtensionASCII( 239 base::StringPrintf(" (%d)", unique_number)); 240 } 241 } 242 base::win::ShortcutProperties shortcut_properties; 243 shortcut_properties.set_target(chrome_exe); 244 shortcut_properties.set_working_dir(working_dir); 245 shortcut_properties.set_arguments(wide_switches); 246 shortcut_properties.set_description(description); 247 shortcut_properties.set_icon(icon_file, 0); 248 shortcut_properties.set_app_id(app_id); 249 shortcut_properties.set_dual_mode(false); 250 if (!file_util::PathExists(shortcut_file.DirName()) && 251 !file_util::CreateDirectory(shortcut_file.DirName())) { 252 NOTREACHED(); 253 return false; 254 } 255 success = base::win::CreateOrUpdateShortcutLink( 256 shortcut_file, shortcut_properties, 257 base::win::SHORTCUT_CREATE_ALWAYS) && success; 258 } 259 260 if (success && pin_to_taskbar) { 261 // Use the web app path shortcut for pinning to avoid having unique numbers 262 // in the application name. 263 base::FilePath shortcut_to_pin = web_app_path.Append(file_name). 264 AddExtension(installer::kLnkExt); 265 success = base::win::TaskbarPinShortcutLink( 266 shortcut_to_pin.value().c_str()) && success; 267 } 268 269 return success; 270} 271 272void UpdatePlatformShortcuts( 273 const base::FilePath& web_app_path, 274 const ShellIntegration::ShortcutInfo& shortcut_info) { 275 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 276 277 // Generates file name to use with persisted ico and shortcut file. 278 base::FilePath file_name = 279 web_app::internals::GetSanitizedFileName(shortcut_info.title); 280 281 // If an icon file exists, and is out of date, replace it with the new icon 282 // and let the shell know the icon has been modified. 283 base::FilePath icon_file = web_app_path.Append(file_name).AddExtension( 284 FILE_PATH_LITERAL(".ico")); 285 if (file_util::PathExists(icon_file)) { 286 web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon); 287 } 288} 289 290void DeletePlatformShortcuts( 291 const base::FilePath& web_app_path, 292 const ShellIntegration::ShortcutInfo& shortcut_info) { 293 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 294 295 // Get all possible locations for shortcuts. 296 ShellIntegration::ShortcutLocations all_shortcut_locations; 297 all_shortcut_locations.in_applications_menu = true; 298 all_shortcut_locations.in_quick_launch_bar = true; 299 all_shortcut_locations.on_desktop = true; 300 // Delete shortcuts from the Chrome Apps subdirectory. 301 // This matches the subdir name set by CreateApplicationShortcutView::Accept 302 // for Chrome apps (not URL apps, but this function does not apply for them). 303 string16 start_menu_subdir = GetAppShortcutsSubdirName(); 304 all_shortcut_locations.applications_menu_subdir = start_menu_subdir; 305 std::vector<base::FilePath> shortcut_paths = GetShortcutPaths( 306 all_shortcut_locations); 307 if (base::win::GetVersion() >= base::win::VERSION_WIN7) 308 shortcut_paths.push_back(web_app_path); 309 310 for (std::vector<base::FilePath>::const_iterator i = shortcut_paths.begin(); 311 i != shortcut_paths.end(); ++i) { 312 std::vector<base::FilePath> shortcut_files = 313 MatchingShortcutsForProfileAndExtension(*i, shortcut_info.profile_path, 314 shortcut_info.title); 315 for (std::vector<base::FilePath>::const_iterator j = shortcut_files.begin(); 316 j != shortcut_files.end(); ++j) { 317 // Any shortcut could have been pinned, either by chrome or the user, so 318 // they are all unpinned. 319 base::win::TaskbarUnpinShortcutLink(j->value().c_str()); 320 file_util::Delete(*j, false); 321 } 322 } 323 324 // If there are no more shortcuts in the Chrome Apps subdirectory, remove it. 325 base::FilePath chrome_apps_dir; 326 if (PathService::Get(base::DIR_START_MENU, &chrome_apps_dir)) { 327 chrome_apps_dir = chrome_apps_dir.Append(start_menu_subdir); 328 if (file_util::IsDirectoryEmpty(chrome_apps_dir)) 329 file_util::Delete(chrome_apps_dir, false); 330 } 331} 332 333std::vector<base::FilePath> GetShortcutPaths( 334 const ShellIntegration::ShortcutLocations& creation_locations) { 335 // Shortcut paths under which to create shortcuts. 336 std::vector<base::FilePath> shortcut_paths; 337 // Locations to add to shortcut_paths. 338 struct { 339 bool use_this_location; 340 int location_id; 341 const wchar_t* subdir; 342 } locations[] = { 343 { 344 creation_locations.on_desktop, 345 base::DIR_USER_DESKTOP, 346 NULL 347 }, { 348 creation_locations.in_applications_menu, 349 base::DIR_START_MENU, 350 creation_locations.applications_menu_subdir.empty() ? NULL : 351 creation_locations.applications_menu_subdir.c_str() 352 }, { 353 creation_locations.in_quick_launch_bar, 354 // For Win7, in_quick_launch_bar means pinning to taskbar. Use 355 // base::PATH_START as a flag for this case. 356 (base::win::GetVersion() >= base::win::VERSION_WIN7) ? 357 base::PATH_START : base::DIR_APP_DATA, 358 (base::win::GetVersion() >= base::win::VERSION_WIN7) ? 359 NULL : L"Microsoft\\Internet Explorer\\Quick Launch" 360 } 361 }; 362 // Populate shortcut_paths. 363 for (int i = 0; i < arraysize(locations); ++i) { 364 if (locations[i].use_this_location) { 365 base::FilePath path; 366 367 // Skip the Win7 case. 368 if (locations[i].location_id == base::PATH_START) 369 continue; 370 371 if (!PathService::Get(locations[i].location_id, &path)) { 372 continue; 373 } 374 375 if (locations[i].subdir != NULL) 376 path = path.Append(locations[i].subdir); 377 shortcut_paths.push_back(path); 378 } 379 } 380 return shortcut_paths; 381} 382 383} // namespace internals 384 385} // namespace web_app 386