web_app_win.cc revision 558790d6acca3451cf3a6b497803a5f07d0bec58
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/web_applications/web_app_win.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <shlobj.h> 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/file_util.h" 11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/files/file_enumerator.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/md5.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h" 15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/strings/string_piece.h" 16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h" 17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/shortcut.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/windows_version.h" 20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "chrome/browser/web_applications/web_app.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/launcher_support/chrome_launcher_support.h" 23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/installer/util/util_constants.h" 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/icon_util.h" 26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/gfx/image/image.h" 27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/gfx/image/image_family.h" 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::FilePath::CharType kIconChecksumFileExt[] = 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FILE_PATH_LITERAL(".ico.md5"); 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Width and height of icons exported to .ico files. 35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Calculates checksum of an icon family using MD5. 37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// The checksum is derived from all of the icons in the family. 38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void GetImageCheckSum(const gfx::ImageFamily& image, base::MD5Digest* digest) { 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(digest); 40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::MD5Context md5_context; 41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::MD5Init(&md5_context); 42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) for (gfx::ImageFamily::const_iterator it = image.begin(); it != image.end(); 44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ++it) { 45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) SkBitmap bitmap = it->AsBitmap(); 46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) SkAutoLockPixels image_lock(bitmap); 48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::StringPiece image_data( 49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) reinterpret_cast<const char*>(bitmap.getPixels()), bitmap.getSize()); 50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::MD5Update(&md5_context, image_data); 51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::MD5Final(digest, &md5_context); 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Saves |image| as an |icon_file| with the checksum. 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool SaveIconWithCheckSum(const base::FilePath& icon_file, 58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const gfx::ImageFamily& image) { 59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!IconUtil::CreateIconFileFromImageFamily(image, icon_file)) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::MD5Digest digest; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetImageCheckSum(image, &digest); 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath cheksum_file(icon_file.ReplaceExtension(kIconChecksumFileExt)); 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return file_util::WriteFile(cheksum_file, 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) reinterpret_cast<const char*>(&digest), 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sizeof(digest)) == sizeof(digest); 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if |icon_file| is missing or different from |image|. 72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ShouldUpdateIcon(const base::FilePath& icon_file, 73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const gfx::ImageFamily& image) { 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath checksum_file( 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) icon_file.ReplaceExtension(kIconChecksumFileExt)); 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if icon_file or checksum file is missing. 787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!base::PathExists(icon_file) || 797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch !base::PathExists(checksum_file)) 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::MD5Digest persisted_image_checksum; 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (sizeof(persisted_image_checksum) != file_util::ReadFile(checksum_file, 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) reinterpret_cast<char*>(&persisted_image_checksum), 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sizeof(persisted_image_checksum))) 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::MD5Digest downloaded_image_checksum; 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetImageCheckSum(image, &downloaded_image_checksum); 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Update icon if checksums are not equal. 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return memcmp(&persisted_image_checksum, &downloaded_image_checksum, 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sizeof(base::MD5Digest)) != 0; 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Returns true if |shortcut_file_name| matches profile |profile_path|, and has 97eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// an --app-id flag. 98eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool IsAppShortcutForProfile(const base::FilePath& shortcut_file_name, 99eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const base::FilePath& profile_path) { 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) string16 cmd_line_string; 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (base::win::ResolveShortcut(shortcut_file_name, NULL, &cmd_line_string)) { 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) cmd_line_string = L"program " + cmd_line_string; 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CommandLine shortcut_cmd_line = CommandLine::FromString(cmd_line_string); 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return shortcut_cmd_line.HasSwitch(switches::kProfileDirectory) && 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_cmd_line.GetSwitchValuePath(switches::kProfileDirectory) == 106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch profile_path.BaseName() && 107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_cmd_line.HasSwitch(switches::kAppId); 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Finds shortcuts in |shortcut_path| that match profile for |profile_path| and 114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// extension with title |shortcut_name|. 115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// If |shortcut_name| is empty, finds all shortcuts matching |profile_path|. 116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochstd::vector<base::FilePath> FindAppShortcutsByProfileAndTitle( 1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& shortcut_path, 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& profile_path, 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const string16& shortcut_name) { 1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<base::FilePath> shortcut_paths; 121eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 122eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (shortcut_name.empty()) { 123eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Find all shortcuts for this profile. 124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FileEnumerator files(shortcut_path, false, 125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FileEnumerator::FILES, 126eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FILE_PATH_LITERAL("*.lnk")); 127eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath shortcut_file = files.Next(); 128eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch while (!shortcut_file.empty()) { 129eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (IsAppShortcutForProfile(shortcut_file, profile_path)) 130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_paths.push_back(shortcut_file); 131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_file = files.Next(); 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 133eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } else { 134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Find all shortcuts matching |shortcut_name|. 135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath base_path = shortcut_path. 136eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch Append(web_app::internals::GetSanitizedFileName(shortcut_name)). 137eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch AddExtension(FILE_PATH_LITERAL(".lnk")); 138eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 139eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const int fileNamesToCheck = 10; 140eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch for (int i = 0; i < fileNamesToCheck; ++i) { 141eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath shortcut_file = base_path; 142eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (i > 0) { 143eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_file = shortcut_file.InsertBeforeExtensionASCII( 144eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::StringPrintf(" (%d)", i)); 145eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 1467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (base::PathExists(shortcut_file) && 147eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch IsAppShortcutForProfile(shortcut_file, profile_path)) { 148eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_paths.push_back(shortcut_file); 149eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 152eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return shortcut_paths; 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 15690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Creates application shortcuts in a given set of paths. 15790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |shortcut_paths| is a list of directories in which shortcuts should be 158558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// created. If |creation_reason| is SHORTCUT_CREATION_AUTOMATED and there is an 159558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// existing shortcut to this app for this profile, does nothing (succeeding). 16090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns true on success, false on failure. 16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Must be called on the FILE thread. 16290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CreateShortcutsInPaths( 1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& web_app_path, 1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const ShellIntegration::ShortcutInfo& shortcut_info, 165eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const std::vector<base::FilePath>& shortcut_paths, 166558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch web_app::ShortcutCreationReason creation_reason, 167eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::vector<base::FilePath>* out_filenames) { 1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Ensure web_app_path exists. 1697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!base::PathExists(web_app_path) && 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) !file_util::CreateDirectory(web_app_path)) { 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Generates file name to use with persisted ico and shortcut file. 1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath file_name = 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) web_app::internals::GetSanitizedFileName(shortcut_info.title); 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Creates an ico file to use with shortcut. 179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath icon_file = web_app_path.Append(file_name).AddExtension( 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FILE_PATH_LITERAL(".ico")); 181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon)) { 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath chrome_exe; 186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) NOTREACHED(); 188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return false; 189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Working directory. 192c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath working_dir(chrome_exe.DirName()); 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CommandLine cmd_line(CommandLine::NO_PROGRAM); 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) cmd_line = ShellIntegration::CommandLineArgsForLauncher(shortcut_info.url, 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_info.extension_id, shortcut_info.profile_path); 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(evan): we rely on the fact that command_line_string() is 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // properly quoted for a Windows command line. The method on 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // CommandLine should probably be renamed to better reflect that 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // fact. 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) string16 wide_switches(cmd_line.GetCommandLineString()); 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sanitize description 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) string16 description = shortcut_info.description; 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (description.length() >= MAX_PATH) 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) description.resize(MAX_PATH - 1); 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Generates app id from web app url and profile path. 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string app_name(web_app::GenerateApplicationNameFromInfo(shortcut_info)); 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) string16 app_id(ShellIntegration::GetAppModelIdForProfile( 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) UTF8ToUTF16(app_name), shortcut_info.profile_path)); 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool success = true; 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (size_t i = 0; i < shortcut_paths.size(); ++i) { 2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath shortcut_file = shortcut_paths[i].Append(file_name). 217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) AddExtension(installer::kLnkExt); 218558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch if (creation_reason == web_app::SHORTCUT_CREATION_AUTOMATED) { 219eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Check whether there is an existing shortcut to this app. 220eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::vector<base::FilePath> shortcut_files = 221eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FindAppShortcutsByProfileAndTitle(shortcut_paths[i], 222eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_info.profile_path, 223eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_info.title); 224eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (!shortcut_files.empty()) 225eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch continue; 226eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (shortcut_paths[i] != web_app_path) { 2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int unique_number = 2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) file_util::GetUniquePathNumber(shortcut_file, FILE_PATH_LITERAL("")); 2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (unique_number == -1) { 2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) success = false; 2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) continue; 2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } else if (unique_number > 0) { 2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) shortcut_file = shortcut_file.InsertBeforeExtensionASCII( 2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::StringPrintf(" (%d)", unique_number)); 2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::win::ShortcutProperties shortcut_properties; 239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) shortcut_properties.set_target(chrome_exe); 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_properties.set_working_dir(working_dir); 2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_properties.set_arguments(wide_switches); 2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_properties.set_description(description); 2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_properties.set_icon(icon_file, 0); 2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_properties.set_app_id(app_id); 2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_properties.set_dual_mode(false); 2467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!base::PathExists(shortcut_file.DirName()) && 247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) !file_util::CreateDirectory(shortcut_file.DirName())) { 248c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) NOTREACHED(); 249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return false; 250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) success = base::win::CreateOrUpdateShortcutLink( 2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_file, shortcut_properties, 2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::win::SHORTCUT_CREATE_ALWAYS) && success; 254eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (out_filenames) 255eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch out_filenames->push_back(shortcut_file); 2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 25890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return success; 25990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 26190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Gets the directories with shortcuts for an app, and deletes the shortcuts. 26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This will search the standard locations for shortcuts named |title| that open 26390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// in the profile with |profile_path|. 26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |was_pinned_to_taskbar| will be set to true if there was previously a 26590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shortcut pinned to the taskbar for this app; false otherwise. 266eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// If |web_app_path| is empty, this will not delete shortcuts from the web app 267eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// directory. If |title| is empty, all shortcuts for this profile will be 268eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// deleted. 26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |shortcut_paths| will be populated with a list of directories where shortcuts 27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// for this app were found (and deleted). This will delete duplicate shortcuts, 27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// but only return each path once, even if it contained multiple deleted 27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shortcuts. Both of these may be NULL. 27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void GetShortcutLocationsAndDeleteShortcuts( 27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const base::FilePath& web_app_path, 27590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const base::FilePath& profile_path, 27690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const string16& title, 27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool* was_pinned_to_taskbar, 27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath>* shortcut_paths) { 27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 28190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Get all possible locations for shortcuts. 28290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ShellIntegration::ShortcutLocations all_shortcut_locations; 28390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) all_shortcut_locations.in_applications_menu = true; 28490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) all_shortcut_locations.in_quick_launch_bar = true; 28590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) all_shortcut_locations.on_desktop = true; 28690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Delete shortcuts from the Chrome Apps subdirectory. 28790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // This matches the subdir name set by CreateApplicationShortcutView::Accept 28890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // for Chrome apps (not URL apps, but this function does not apply for them). 28990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) all_shortcut_locations.applications_menu_subdir = 29090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) web_app::GetAppShortcutsSubdirName(); 29190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath> all_paths = web_app::internals::GetShortcutPaths( 29290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) all_shortcut_locations); 293eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (base::win::GetVersion() >= base::win::VERSION_WIN7 && 294eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch !web_app_path.empty()) { 29590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) all_paths.push_back(web_app_path); 296eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 29790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 29890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (was_pinned_to_taskbar) { 29990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Determine if there is a link to this app in the TaskBar pin directory. 30090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath taskbar_pin_path; 30190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (PathService::Get(base::DIR_TASKBAR_PINS, &taskbar_pin_path)) { 30290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath> taskbar_pin_files = 303eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FindAppShortcutsByProfileAndTitle(taskbar_pin_path, profile_path, 304eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch title); 30590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *was_pinned_to_taskbar = !taskbar_pin_files.empty(); 30690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } else { 30790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *was_pinned_to_taskbar = false; 30890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 30990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 31090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 31190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (std::vector<base::FilePath>::const_iterator i = all_paths.begin(); 31290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) i != all_paths.end(); ++i) { 31390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath> shortcut_files = 314eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FindAppShortcutsByProfileAndTitle(*i, profile_path, title); 31590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (shortcut_paths && !shortcut_files.empty()) { 31690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) shortcut_paths->push_back(*i); 31790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 31890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (std::vector<base::FilePath>::const_iterator j = shortcut_files.begin(); 31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) j != shortcut_files.end(); ++j) { 32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Any shortcut could have been pinned, either by chrome or the user, so 32190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // they are all unpinned. 32290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::win::TaskbarUnpinShortcutLink(j->value().c_str()); 3237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch base::DeleteFile(*j, false); 32490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 32590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 32690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 32790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 32890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace 32990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 33090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace web_app { 33190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 332eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbase::FilePath CreateShortcutInWebAppDir( 333eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const base::FilePath& web_app_dir, 334eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const ShellIntegration::ShortcutInfo& shortcut_info) { 335eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::vector<base::FilePath> paths; 336eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch paths.push_back(web_app_dir); 337eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::vector<base::FilePath> out_filenames; 338eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch CreateShortcutsInPaths(web_app_dir, shortcut_info, paths, 339558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch SHORTCUT_CREATION_BY_USER, &out_filenames); 340eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch DCHECK_EQ(out_filenames.size(), 1u); 341eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return out_filenames[0]; 342eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 343eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 34490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace internals { 34590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 34690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Saves |image| to |icon_file| if the file is outdated and refresh shell's 34790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// icon cache to ensure correct icon is displayed. Returns true if icon_file 34890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is up to date or successfully updated. 34990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CheckAndSaveIcon(const base::FilePath& icon_file, 35090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const gfx::ImageFamily& image) { 35190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (ShouldUpdateIcon(icon_file, image)) { 35290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (SaveIconWithCheckSum(icon_file, image)) { 35390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Refresh shell's icon cache. This call is quite disruptive as user would 35490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // see explorer rebuilding the icon cache. It would be great that we find 35590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // a better way to achieve this. 35690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT, 35790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) NULL, NULL); 35890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } else { 35990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 36090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 36190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 36290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 36390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return true; 36490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 36590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 36690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CreatePlatformShortcuts( 36790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const base::FilePath& web_app_path, 36890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const ShellIntegration::ShortcutInfo& shortcut_info, 369eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const ShellIntegration::ShortcutLocations& creation_locations, 370558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch ShortcutCreationReason creation_reason) { 37190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 37290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 37390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Shortcut paths under which to create shortcuts. 37490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath> shortcut_paths = 37590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) GetShortcutPaths(creation_locations); 37690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 37790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool pin_to_taskbar = creation_locations.in_quick_launch_bar && 37890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) (base::win::GetVersion() >= base::win::VERSION_WIN7); 37990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 38090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Create/update the shortcut in the web app path for the "Pin To Taskbar" 38190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // option in Win7. We use the web app path shortcut because we will overwrite 38290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // it rather than appending unique numbers if the shortcut already exists. 38390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // This prevents pinned apps from having unique numbers in their names. 38490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (pin_to_taskbar) 38590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) shortcut_paths.push_back(web_app_path); 38690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 38790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (shortcut_paths.empty()) 38890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 38990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 390eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (!CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths, 391558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch creation_reason, NULL)) 39290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 39390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 39490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (pin_to_taskbar) { 39590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath file_name = 39690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) web_app::internals::GetSanitizedFileName(shortcut_info.title); 3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Use the web app path shortcut for pinning to avoid having unique numbers 3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // in the application name. 3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath shortcut_to_pin = web_app_path.Append(file_name). 400c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) AddExtension(installer::kLnkExt); 40190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (!base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str())) 40290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 40590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return true; 4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void UpdatePlatformShortcuts( 4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& web_app_path, 41090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const string16& old_app_title, 4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ShellIntegration::ShortcutInfo& shortcut_info) { 412c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Generates file name to use with persisted ico and shortcut file. 4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath file_name = 4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) web_app::internals::GetSanitizedFileName(shortcut_info.title); 4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 41890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (old_app_title != shortcut_info.title) { 41990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // The app's title has changed. Delete all existing app shortcuts and 42090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // recreate them in any locations they already existed (but do not add them 42190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // to locations where they do not currently exist). 42290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool was_pinned_to_taskbar; 42390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath> shortcut_paths; 42490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) GetShortcutLocationsAndDeleteShortcuts( 42590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) web_app_path, shortcut_info.profile_path, old_app_title, 42690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) &was_pinned_to_taskbar, &shortcut_paths); 427eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths, 428558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch SHORTCUT_CREATION_BY_USER, NULL); 42990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // If the shortcut was pinned to the taskbar, 43090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // GetShortcutLocationsAndDeleteShortcuts will have deleted it. In that 43190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // case, re-pin it. 43290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (was_pinned_to_taskbar) { 43390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath file_name = 43490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) web_app::internals::GetSanitizedFileName(shortcut_info.title); 43590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Use the web app path shortcut for pinning to avoid having unique 43690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // numbers in the application name. 43790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath shortcut_to_pin = web_app_path.Append(file_name). 43890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) AddExtension(installer::kLnkExt); 43990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str()); 44090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 44190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 44290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If an icon file exists, and is out of date, replace it with the new icon 4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and let the shell know the icon has been modified. 445c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath icon_file = web_app_path.Append(file_name).AddExtension( 4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FILE_PATH_LITERAL(".ico")); 4477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (base::PathExists(icon_file)) { 448c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon); 4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DeletePlatformShortcuts( 4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& web_app_path, 4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ShellIntegration::ShortcutInfo& shortcut_info) { 45590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) GetShortcutLocationsAndDeleteShortcuts( 45690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) web_app_path, shortcut_info.profile_path, shortcut_info.title, NULL, 45790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) NULL); 458c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 459c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // If there are no more shortcuts in the Chrome Apps subdirectory, remove it. 460c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath chrome_apps_dir; 461c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (PathService::Get(base::DIR_START_MENU, &chrome_apps_dir)) { 46290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome_apps_dir = chrome_apps_dir.Append(GetAppShortcutsSubdirName()); 463c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (file_util::IsDirectoryEmpty(chrome_apps_dir)) 4647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch base::DeleteFile(chrome_apps_dir, false); 465eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 466eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 467eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 468eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid DeleteAllShortcutsForProfile(const base::FilePath& profile_path) { 469eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch GetShortcutLocationsAndDeleteShortcuts(base::FilePath(), profile_path, L"", 470eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch NULL, NULL); 471eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 472eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // If there are no more shortcuts in the Chrome Apps subdirectory, remove it. 473eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath chrome_apps_dir; 474eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (PathService::Get(base::DIR_START_MENU, &chrome_apps_dir)) { 475eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch chrome_apps_dir = chrome_apps_dir.Append(GetAppShortcutsSubdirName()); 476eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (file_util::IsDirectoryEmpty(chrome_apps_dir)) 4777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch base::DeleteFile(chrome_apps_dir, false); 478c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 479c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 480c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 481c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)std::vector<base::FilePath> GetShortcutPaths( 482c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const ShellIntegration::ShortcutLocations& creation_locations) { 483c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Shortcut paths under which to create shortcuts. 484c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::vector<base::FilePath> shortcut_paths; 485c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Locations to add to shortcut_paths. 486c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) struct { 487c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bool use_this_location; 488c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int location_id; 489c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const wchar_t* subdir; 490c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } locations[] = { 491c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) { 492c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) creation_locations.on_desktop, 493c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::DIR_USER_DESKTOP, 494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) NULL 495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) }, { 496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) creation_locations.in_applications_menu, 497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::DIR_START_MENU, 498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) creation_locations.applications_menu_subdir.empty() ? NULL : 499c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) creation_locations.applications_menu_subdir.c_str() 500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) }, { 501c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) creation_locations.in_quick_launch_bar, 502c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // For Win7, in_quick_launch_bar means pinning to taskbar. Use 503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // base::PATH_START as a flag for this case. 504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) (base::win::GetVersion() >= base::win::VERSION_WIN7) ? 505c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::PATH_START : base::DIR_APP_DATA, 506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) (base::win::GetVersion() >= base::win::VERSION_WIN7) ? 507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) NULL : L"Microsoft\\Internet Explorer\\Quick Launch" 508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 509c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) }; 510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Populate shortcut_paths. 511c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) for (int i = 0; i < arraysize(locations); ++i) { 512c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (locations[i].use_this_location) { 513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath path; 514c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Skip the Win7 case. 516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (locations[i].location_id == base::PATH_START) 517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) continue; 518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!PathService::Get(locations[i].location_id, &path)) { 520c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) continue; 521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 523c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (locations[i].subdir != NULL) 524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) path = path.Append(locations[i].subdir); 525c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) shortcut_paths.push_back(path); 526c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 527c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 528c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return shortcut_paths; 5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace internals 5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace web_app 534