web_app_win.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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// Creates application shortcuts in a given set of paths. 133// |shortcut_paths| is a list of directories in which shortcuts should be 134// created. 135// Returns true on success, false on failure. 136// Must be called on the FILE thread. 137bool CreateShortcutsInPaths( 138 const base::FilePath& web_app_path, 139 const ShellIntegration::ShortcutInfo& shortcut_info, 140 const std::vector<base::FilePath>& shortcut_paths) { 141 // Ensure web_app_path exists. 142 if (!file_util::PathExists(web_app_path) && 143 !file_util::CreateDirectory(web_app_path)) { 144 return false; 145 } 146 147 // Generates file name to use with persisted ico and shortcut file. 148 base::FilePath file_name = 149 web_app::internals::GetSanitizedFileName(shortcut_info.title); 150 151 // Creates an ico file to use with shortcut. 152 base::FilePath icon_file = web_app_path.Append(file_name).AddExtension( 153 FILE_PATH_LITERAL(".ico")); 154 if (!web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon)) { 155 return false; 156 } 157 158 base::FilePath chrome_exe; 159 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 160 NOTREACHED(); 161 return false; 162 } 163 164 // Working directory. 165 base::FilePath working_dir(chrome_exe.DirName()); 166 167 CommandLine cmd_line(CommandLine::NO_PROGRAM); 168 cmd_line = ShellIntegration::CommandLineArgsForLauncher(shortcut_info.url, 169 shortcut_info.extension_id, shortcut_info.profile_path); 170 171 // TODO(evan): we rely on the fact that command_line_string() is 172 // properly quoted for a Windows command line. The method on 173 // CommandLine should probably be renamed to better reflect that 174 // fact. 175 string16 wide_switches(cmd_line.GetCommandLineString()); 176 177 // Sanitize description 178 string16 description = shortcut_info.description; 179 if (description.length() >= MAX_PATH) 180 description.resize(MAX_PATH - 1); 181 182 // Generates app id from web app url and profile path. 183 std::string app_name(web_app::GenerateApplicationNameFromInfo(shortcut_info)); 184 string16 app_id(ShellIntegration::GetAppModelIdForProfile( 185 UTF8ToUTF16(app_name), shortcut_info.profile_path)); 186 187 bool success = true; 188 for (size_t i = 0; i < shortcut_paths.size(); ++i) { 189 base::FilePath shortcut_file = shortcut_paths[i].Append(file_name). 190 AddExtension(installer::kLnkExt); 191 if (shortcut_paths[i] != web_app_path) { 192 int unique_number = 193 file_util::GetUniquePathNumber(shortcut_file, FILE_PATH_LITERAL("")); 194 if (unique_number == -1) { 195 success = false; 196 continue; 197 } else if (unique_number > 0) { 198 shortcut_file = shortcut_file.InsertBeforeExtensionASCII( 199 base::StringPrintf(" (%d)", unique_number)); 200 } 201 } 202 base::win::ShortcutProperties shortcut_properties; 203 shortcut_properties.set_target(chrome_exe); 204 shortcut_properties.set_working_dir(working_dir); 205 shortcut_properties.set_arguments(wide_switches); 206 shortcut_properties.set_description(description); 207 shortcut_properties.set_icon(icon_file, 0); 208 shortcut_properties.set_app_id(app_id); 209 shortcut_properties.set_dual_mode(false); 210 if (!file_util::PathExists(shortcut_file.DirName()) && 211 !file_util::CreateDirectory(shortcut_file.DirName())) { 212 NOTREACHED(); 213 return false; 214 } 215 success = base::win::CreateOrUpdateShortcutLink( 216 shortcut_file, shortcut_properties, 217 base::win::SHORTCUT_CREATE_ALWAYS) && success; 218 } 219 220 return success; 221} 222 223// Gets the directories with shortcuts for an app, and deletes the shortcuts. 224// This will search the standard locations for shortcuts named |title| that open 225// in the profile with |profile_path|. 226// |was_pinned_to_taskbar| will be set to true if there was previously a 227// shortcut pinned to the taskbar for this app; false otherwise. 228// |shortcut_paths| will be populated with a list of directories where shortcuts 229// for this app were found (and deleted). This will delete duplicate shortcuts, 230// but only return each path once, even if it contained multiple deleted 231// shortcuts. Both of these may be NULL. 232void GetShortcutLocationsAndDeleteShortcuts( 233 const base::FilePath& web_app_path, 234 const base::FilePath& profile_path, 235 const string16& title, 236 bool* was_pinned_to_taskbar, 237 std::vector<base::FilePath>* shortcut_paths) { 238 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 239 240 // Get all possible locations for shortcuts. 241 ShellIntegration::ShortcutLocations all_shortcut_locations; 242 all_shortcut_locations.in_applications_menu = true; 243 all_shortcut_locations.in_quick_launch_bar = true; 244 all_shortcut_locations.on_desktop = true; 245 // Delete shortcuts from the Chrome Apps subdirectory. 246 // This matches the subdir name set by CreateApplicationShortcutView::Accept 247 // for Chrome apps (not URL apps, but this function does not apply for them). 248 all_shortcut_locations.applications_menu_subdir = 249 web_app::GetAppShortcutsSubdirName(); 250 std::vector<base::FilePath> all_paths = web_app::internals::GetShortcutPaths( 251 all_shortcut_locations); 252 if (base::win::GetVersion() >= base::win::VERSION_WIN7) 253 all_paths.push_back(web_app_path); 254 255 if (was_pinned_to_taskbar) { 256 // Determine if there is a link to this app in the TaskBar pin directory. 257 base::FilePath taskbar_pin_path; 258 if (PathService::Get(base::DIR_TASKBAR_PINS, &taskbar_pin_path)) { 259 std::vector<base::FilePath> taskbar_pin_files = 260 MatchingShortcutsForProfileAndExtension(taskbar_pin_path, 261 profile_path, title); 262 *was_pinned_to_taskbar = !taskbar_pin_files.empty(); 263 } else { 264 *was_pinned_to_taskbar = false; 265 } 266 } 267 268 for (std::vector<base::FilePath>::const_iterator i = all_paths.begin(); 269 i != all_paths.end(); ++i) { 270 std::vector<base::FilePath> shortcut_files = 271 MatchingShortcutsForProfileAndExtension(*i, profile_path, title); 272 if (shortcut_paths && !shortcut_files.empty()) { 273 shortcut_paths->push_back(*i); 274 } 275 for (std::vector<base::FilePath>::const_iterator j = shortcut_files.begin(); 276 j != shortcut_files.end(); ++j) { 277 // Any shortcut could have been pinned, either by chrome or the user, so 278 // they are all unpinned. 279 base::win::TaskbarUnpinShortcutLink(j->value().c_str()); 280 file_util::Delete(*j, false); 281 } 282 } 283} 284 285} // namespace 286 287namespace web_app { 288 289namespace internals { 290 291// Saves |image| to |icon_file| if the file is outdated and refresh shell's 292// icon cache to ensure correct icon is displayed. Returns true if icon_file 293// is up to date or successfully updated. 294bool CheckAndSaveIcon(const base::FilePath& icon_file, 295 const gfx::ImageFamily& image) { 296 if (ShouldUpdateIcon(icon_file, image)) { 297 if (SaveIconWithCheckSum(icon_file, image)) { 298 // Refresh shell's icon cache. This call is quite disruptive as user would 299 // see explorer rebuilding the icon cache. It would be great that we find 300 // a better way to achieve this. 301 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT, 302 NULL, NULL); 303 } else { 304 return false; 305 } 306 } 307 308 return true; 309} 310 311bool CreatePlatformShortcuts( 312 const base::FilePath& web_app_path, 313 const ShellIntegration::ShortcutInfo& shortcut_info, 314 const ShellIntegration::ShortcutLocations& creation_locations) { 315 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 316 317 // Shortcut paths under which to create shortcuts. 318 std::vector<base::FilePath> shortcut_paths = 319 GetShortcutPaths(creation_locations); 320 321 bool pin_to_taskbar = creation_locations.in_quick_launch_bar && 322 (base::win::GetVersion() >= base::win::VERSION_WIN7); 323 324 // Create/update the shortcut in the web app path for the "Pin To Taskbar" 325 // option in Win7. We use the web app path shortcut because we will overwrite 326 // it rather than appending unique numbers if the shortcut already exists. 327 // This prevents pinned apps from having unique numbers in their names. 328 if (pin_to_taskbar) 329 shortcut_paths.push_back(web_app_path); 330 331 if (shortcut_paths.empty()) 332 return false; 333 334 if (!CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths)) 335 return false; 336 337 if (pin_to_taskbar) { 338 base::FilePath file_name = 339 web_app::internals::GetSanitizedFileName(shortcut_info.title); 340 // Use the web app path shortcut for pinning to avoid having unique numbers 341 // in the application name. 342 base::FilePath shortcut_to_pin = web_app_path.Append(file_name). 343 AddExtension(installer::kLnkExt); 344 if (!base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str())) 345 return false; 346 } 347 348 return true; 349} 350 351void UpdatePlatformShortcuts( 352 const base::FilePath& web_app_path, 353 const string16& old_app_title, 354 const ShellIntegration::ShortcutInfo& shortcut_info) { 355 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 356 357 // Generates file name to use with persisted ico and shortcut file. 358 base::FilePath file_name = 359 web_app::internals::GetSanitizedFileName(shortcut_info.title); 360 361 if (old_app_title != shortcut_info.title) { 362 // The app's title has changed. Delete all existing app shortcuts and 363 // recreate them in any locations they already existed (but do not add them 364 // to locations where they do not currently exist). 365 bool was_pinned_to_taskbar; 366 std::vector<base::FilePath> shortcut_paths; 367 GetShortcutLocationsAndDeleteShortcuts( 368 web_app_path, shortcut_info.profile_path, old_app_title, 369 &was_pinned_to_taskbar, &shortcut_paths); 370 CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths); 371 // If the shortcut was pinned to the taskbar, 372 // GetShortcutLocationsAndDeleteShortcuts will have deleted it. In that 373 // case, re-pin it. 374 if (was_pinned_to_taskbar) { 375 base::FilePath file_name = 376 web_app::internals::GetSanitizedFileName(shortcut_info.title); 377 // Use the web app path shortcut for pinning to avoid having unique 378 // numbers in the application name. 379 base::FilePath shortcut_to_pin = web_app_path.Append(file_name). 380 AddExtension(installer::kLnkExt); 381 base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str()); 382 } 383 } 384 385 // If an icon file exists, and is out of date, replace it with the new icon 386 // and let the shell know the icon has been modified. 387 base::FilePath icon_file = web_app_path.Append(file_name).AddExtension( 388 FILE_PATH_LITERAL(".ico")); 389 if (file_util::PathExists(icon_file)) { 390 web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon); 391 } 392} 393 394void DeletePlatformShortcuts( 395 const base::FilePath& web_app_path, 396 const ShellIntegration::ShortcutInfo& shortcut_info) { 397 GetShortcutLocationsAndDeleteShortcuts( 398 web_app_path, shortcut_info.profile_path, shortcut_info.title, NULL, 399 NULL); 400 401 // If there are no more shortcuts in the Chrome Apps subdirectory, remove it. 402 base::FilePath chrome_apps_dir; 403 if (PathService::Get(base::DIR_START_MENU, &chrome_apps_dir)) { 404 chrome_apps_dir = chrome_apps_dir.Append(GetAppShortcutsSubdirName()); 405 if (file_util::IsDirectoryEmpty(chrome_apps_dir)) 406 file_util::Delete(chrome_apps_dir, false); 407 } 408} 409 410std::vector<base::FilePath> GetShortcutPaths( 411 const ShellIntegration::ShortcutLocations& creation_locations) { 412 // Shortcut paths under which to create shortcuts. 413 std::vector<base::FilePath> shortcut_paths; 414 // Locations to add to shortcut_paths. 415 struct { 416 bool use_this_location; 417 int location_id; 418 const wchar_t* subdir; 419 } locations[] = { 420 { 421 creation_locations.on_desktop, 422 base::DIR_USER_DESKTOP, 423 NULL 424 }, { 425 creation_locations.in_applications_menu, 426 base::DIR_START_MENU, 427 creation_locations.applications_menu_subdir.empty() ? NULL : 428 creation_locations.applications_menu_subdir.c_str() 429 }, { 430 creation_locations.in_quick_launch_bar, 431 // For Win7, in_quick_launch_bar means pinning to taskbar. Use 432 // base::PATH_START as a flag for this case. 433 (base::win::GetVersion() >= base::win::VERSION_WIN7) ? 434 base::PATH_START : base::DIR_APP_DATA, 435 (base::win::GetVersion() >= base::win::VERSION_WIN7) ? 436 NULL : L"Microsoft\\Internet Explorer\\Quick Launch" 437 } 438 }; 439 // Populate shortcut_paths. 440 for (int i = 0; i < arraysize(locations); ++i) { 441 if (locations[i].use_this_location) { 442 base::FilePath path; 443 444 // Skip the Win7 case. 445 if (locations[i].location_id == base::PATH_START) 446 continue; 447 448 if (!PathService::Get(locations[i].location_id, &path)) { 449 continue; 450 } 451 452 if (locations[i].subdir != NULL) 453 path = path.Append(locations[i].subdir); 454 shortcut_paths.push_back(path); 455 } 456 } 457 return shortcut_paths; 458} 459 460} // namespace internals 461 462} // namespace web_app 463