web_app_win.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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" 22a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/installer/util/browser_distribution.h" 23a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/installer/util/shell_util.h" 24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/installer/util/util_constants.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/icon_util.h" 27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/gfx/image/image.h" 28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/gfx/image/image_family.h" 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const base::FilePath::CharType kIconChecksumFileExt[] = 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FILE_PATH_LITERAL(".ico.md5"); 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Width and height of icons exported to .ico files. 36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Calculates checksum of an icon family using MD5. 38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// The checksum is derived from all of the icons in the family. 39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void GetImageCheckSum(const gfx::ImageFamily& image, base::MD5Digest* digest) { 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK(digest); 41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::MD5Context md5_context; 42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::MD5Init(&md5_context); 43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) for (gfx::ImageFamily::const_iterator it = image.begin(); it != image.end(); 45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ++it) { 46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) SkBitmap bitmap = it->AsBitmap(); 47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) SkAutoLockPixels image_lock(bitmap); 49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::StringPiece image_data( 50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) reinterpret_cast<const char*>(bitmap.getPixels()), bitmap.getSize()); 51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::MD5Update(&md5_context, image_data); 52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::MD5Final(digest, &md5_context); 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Saves |image| as an |icon_file| with the checksum. 582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool SaveIconWithCheckSum(const base::FilePath& icon_file, 59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const gfx::ImageFamily& image) { 60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!IconUtil::CreateIconFileFromImageFamily(image, icon_file)) 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::MD5Digest digest; 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetImageCheckSum(image, &digest); 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath cheksum_file(icon_file.ReplaceExtension(kIconChecksumFileExt)); 67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return base::WriteFile(cheksum_file, 68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) reinterpret_cast<const char*>(&digest), 69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) sizeof(digest)) == sizeof(digest); 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if |icon_file| is missing or different from |image|. 73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool ShouldUpdateIcon(const base::FilePath& icon_file, 74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const gfx::ImageFamily& image) { 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath checksum_file( 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) icon_file.ReplaceExtension(kIconChecksumFileExt)); 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if icon_file or checksum file is missing. 797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!base::PathExists(icon_file) || 807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch !base::PathExists(checksum_file)) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::MD5Digest persisted_image_checksum; 84a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (sizeof(persisted_image_checksum) != base::ReadFile(checksum_file, 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) reinterpret_cast<char*>(&persisted_image_checksum), 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sizeof(persisted_image_checksum))) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::MD5Digest downloaded_image_checksum; 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GetImageCheckSum(image, &downloaded_image_checksum); 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Update icon if checksums are not equal. 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return memcmp(&persisted_image_checksum, &downloaded_image_checksum, 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sizeof(base::MD5Digest)) != 0; 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 97eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Returns true if |shortcut_file_name| matches profile |profile_path|, and has 98eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// an --app-id flag. 99eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool IsAppShortcutForProfile(const base::FilePath& shortcut_file_name, 100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const base::FilePath& profile_path) { 101a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 cmd_line_string; 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (base::win::ResolveShortcut(shortcut_file_name, NULL, &cmd_line_string)) { 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) cmd_line_string = L"program " + cmd_line_string; 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CommandLine shortcut_cmd_line = CommandLine::FromString(cmd_line_string); 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return shortcut_cmd_line.HasSwitch(switches::kProfileDirectory) && 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_cmd_line.GetSwitchValuePath(switches::kProfileDirectory) == 107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch profile_path.BaseName() && 108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_cmd_line.HasSwitch(switches::kAppId); 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Finds shortcuts in |shortcut_path| that match profile for |profile_path| and 115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// extension with title |shortcut_name|. 116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// If |shortcut_name| is empty, finds all shortcuts matching |profile_path|. 117eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochstd::vector<base::FilePath> FindAppShortcutsByProfileAndTitle( 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& shortcut_path, 1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& profile_path, 120a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& shortcut_name) { 1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<base::FilePath> shortcut_paths; 122eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 123eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (shortcut_name.empty()) { 124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Find all shortcuts for this profile. 125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FileEnumerator files(shortcut_path, false, 126eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FileEnumerator::FILES, 127eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FILE_PATH_LITERAL("*.lnk")); 128eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath shortcut_file = files.Next(); 129eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch while (!shortcut_file.empty()) { 130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (IsAppShortcutForProfile(shortcut_file, profile_path)) 131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_paths.push_back(shortcut_file); 132eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_file = files.Next(); 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } else { 135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Find all shortcuts matching |shortcut_name|. 136eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath base_path = shortcut_path. 137eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch Append(web_app::internals::GetSanitizedFileName(shortcut_name)). 138eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch AddExtension(FILE_PATH_LITERAL(".lnk")); 139eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 140eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const int fileNamesToCheck = 10; 141eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch for (int i = 0; i < fileNamesToCheck; ++i) { 142eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath shortcut_file = base_path; 143eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (i > 0) { 144eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_file = shortcut_file.InsertBeforeExtensionASCII( 145eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::StringPrintf(" (%d)", i)); 146eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 1477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (base::PathExists(shortcut_file) && 148eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch IsAppShortcutForProfile(shortcut_file, profile_path)) { 149eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_paths.push_back(shortcut_file); 150eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 153eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return shortcut_paths; 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 15790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Creates application shortcuts in a given set of paths. 15890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |shortcut_paths| is a list of directories in which shortcuts should be 159558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// created. If |creation_reason| is SHORTCUT_CREATION_AUTOMATED and there is an 160558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// existing shortcut to this app for this profile, does nothing (succeeding). 16190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns true on success, false on failure. 16290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Must be called on the FILE thread. 16390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CreateShortcutsInPaths( 1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& web_app_path, 1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const ShellIntegration::ShortcutInfo& shortcut_info, 166eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const std::vector<base::FilePath>& shortcut_paths, 167558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch web_app::ShortcutCreationReason creation_reason, 168eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::vector<base::FilePath>* out_filenames) { 1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Ensure web_app_path exists. 1707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!base::PathExists(web_app_path) && 171a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) !base::CreateDirectory(web_app_path)) { 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Generates file name to use with persisted ico and shortcut file. 1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath file_name = 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) web_app::internals::GetSanitizedFileName(shortcut_info.title); 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Creates an ico file to use with shortcut. 180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath icon_file = web_app_path.Append(file_name).AddExtension( 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FILE_PATH_LITERAL(".ico")); 182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon)) { 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath chrome_exe; 187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { 188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) NOTREACHED(); 189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return false; 190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Working directory. 193c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath working_dir(chrome_exe.DirName()); 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CommandLine cmd_line(CommandLine::NO_PROGRAM); 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) cmd_line = ShellIntegration::CommandLineArgsForLauncher(shortcut_info.url, 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_info.extension_id, shortcut_info.profile_path); 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TODO(evan): we rely on the fact that command_line_string() is 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // properly quoted for a Windows command line. The method on 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // CommandLine should probably be renamed to better reflect that 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // fact. 203a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 wide_switches(cmd_line.GetCommandLineString()); 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sanitize description 206a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 description = shortcut_info.description; 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (description.length() >= MAX_PATH) 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) description.resize(MAX_PATH - 1); 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Generates app id from web app url and profile path. 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string app_name(web_app::GenerateApplicationNameFromInfo(shortcut_info)); 212a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 app_id(ShellIntegration::GetAppModelIdForProfile( 2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) base::UTF8ToUTF16(app_name), shortcut_info.profile_path)); 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool success = true; 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (size_t i = 0; i < shortcut_paths.size(); ++i) { 2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath shortcut_file = shortcut_paths[i].Append(file_name). 218c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) AddExtension(installer::kLnkExt); 219558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch if (creation_reason == web_app::SHORTCUT_CREATION_AUTOMATED) { 220eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Check whether there is an existing shortcut to this app. 221eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::vector<base::FilePath> shortcut_files = 222eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FindAppShortcutsByProfileAndTitle(shortcut_paths[i], 223eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_info.profile_path, 224eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shortcut_info.title); 225eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (!shortcut_files.empty()) 226eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch continue; 227eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (shortcut_paths[i] != web_app_path) { 2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int unique_number = 230a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::GetUniquePathNumber(shortcut_file, 231a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::FilePath::StringType()); 2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (unique_number == -1) { 2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) success = false; 2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) continue; 2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } else if (unique_number > 0) { 2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) shortcut_file = shortcut_file.InsertBeforeExtensionASCII( 2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::StringPrintf(" (%d)", unique_number)); 2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::win::ShortcutProperties shortcut_properties; 241c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) shortcut_properties.set_target(chrome_exe); 2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_properties.set_working_dir(working_dir); 2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_properties.set_arguments(wide_switches); 2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_properties.set_description(description); 2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_properties.set_icon(icon_file, 0); 2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_properties.set_app_id(app_id); 2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_properties.set_dual_mode(false); 2487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!base::PathExists(shortcut_file.DirName()) && 249a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) !base::CreateDirectory(shortcut_file.DirName())) { 250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) NOTREACHED(); 251c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return false; 252c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) success = base::win::CreateOrUpdateShortcutLink( 2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) shortcut_file, shortcut_properties, 2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::win::SHORTCUT_CREATE_ALWAYS) && success; 256eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (out_filenames) 257eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch out_filenames->push_back(shortcut_file); 2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return success; 26190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 26390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Gets the directories with shortcuts for an app, and deletes the shortcuts. 26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This will search the standard locations for shortcuts named |title| that open 26590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// in the profile with |profile_path|. 26690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |was_pinned_to_taskbar| will be set to true if there was previously a 26790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shortcut pinned to the taskbar for this app; false otherwise. 268eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// If |web_app_path| is empty, this will not delete shortcuts from the web app 269eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// directory. If |title| is empty, all shortcuts for this profile will be 270eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// deleted. 27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |shortcut_paths| will be populated with a list of directories where shortcuts 27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// for this app were found (and deleted). This will delete duplicate shortcuts, 27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// but only return each path once, even if it contained multiple deleted 27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// shortcuts. Both of these may be NULL. 27590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void GetShortcutLocationsAndDeleteShortcuts( 27690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const base::FilePath& web_app_path, 27790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const base::FilePath& profile_path, 278a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& title, 27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool* was_pinned_to_taskbar, 28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath>* shortcut_paths) { 28190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 28290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 28390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Get all possible locations for shortcuts. 28490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ShellIntegration::ShortcutLocations all_shortcut_locations; 28590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) all_shortcut_locations.in_quick_launch_bar = true; 28690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) all_shortcut_locations.on_desktop = true; 28790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Delete shortcuts from the Chrome Apps subdirectory. 28890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // This matches the subdir name set by CreateApplicationShortcutView::Accept 28990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // for Chrome apps (not URL apps, but this function does not apply for them). 290a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) all_shortcut_locations.applications_menu_location = 291a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellIntegration::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; 29290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath> all_paths = web_app::internals::GetShortcutPaths( 29390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) all_shortcut_locations); 294eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (base::win::GetVersion() >= base::win::VERSION_WIN7 && 295eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch !web_app_path.empty()) { 29690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) all_paths.push_back(web_app_path); 297eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 29890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 29990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (was_pinned_to_taskbar) { 30090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Determine if there is a link to this app in the TaskBar pin directory. 30190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath taskbar_pin_path; 30290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (PathService::Get(base::DIR_TASKBAR_PINS, &taskbar_pin_path)) { 30390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath> taskbar_pin_files = 304eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FindAppShortcutsByProfileAndTitle(taskbar_pin_path, profile_path, 305eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch title); 30690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *was_pinned_to_taskbar = !taskbar_pin_files.empty(); 30790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } else { 30890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *was_pinned_to_taskbar = false; 30990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 31090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 31190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 31290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (std::vector<base::FilePath>::const_iterator i = all_paths.begin(); 31390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) i != all_paths.end(); ++i) { 31490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath> shortcut_files = 315eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch FindAppShortcutsByProfileAndTitle(*i, profile_path, title); 31690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (shortcut_paths && !shortcut_files.empty()) { 31790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) shortcut_paths->push_back(*i); 31890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (std::vector<base::FilePath>::const_iterator j = shortcut_files.begin(); 32090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) j != shortcut_files.end(); ++j) { 32190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Any shortcut could have been pinned, either by chrome or the user, so 32290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // they are all unpinned. 32390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::win::TaskbarUnpinShortcutLink(j->value().c_str()); 3247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch base::DeleteFile(*j, false); 32590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 32690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 32790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 32890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 32990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace 33090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 33190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace web_app { 33290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 333eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbase::FilePath CreateShortcutInWebAppDir( 334eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const base::FilePath& web_app_dir, 335eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const ShellIntegration::ShortcutInfo& shortcut_info) { 336eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::vector<base::FilePath> paths; 337eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch paths.push_back(web_app_dir); 338eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::vector<base::FilePath> out_filenames; 339eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch CreateShortcutsInPaths(web_app_dir, shortcut_info, paths, 340558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch SHORTCUT_CREATION_BY_USER, &out_filenames); 341eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch DCHECK_EQ(out_filenames.size(), 1u); 342eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return out_filenames[0]; 343eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 344eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 34590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace internals { 34690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 34790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Saves |image| to |icon_file| if the file is outdated and refresh shell's 34890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// icon cache to ensure correct icon is displayed. Returns true if icon_file 34990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is up to date or successfully updated. 35090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CheckAndSaveIcon(const base::FilePath& icon_file, 35190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const gfx::ImageFamily& image) { 35290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (ShouldUpdateIcon(icon_file, image)) { 35390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (SaveIconWithCheckSum(icon_file, image)) { 35490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Refresh shell's icon cache. This call is quite disruptive as user would 35590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // see explorer rebuilding the icon cache. It would be great that we find 35690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // a better way to achieve this. 35790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT, 35890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) NULL, NULL); 35990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } else { 36090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 36190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 36290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 36390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 36490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return true; 36590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 36690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 36790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool CreatePlatformShortcuts( 36890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const base::FilePath& web_app_path, 36990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const ShellIntegration::ShortcutInfo& shortcut_info, 370eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const ShellIntegration::ShortcutLocations& creation_locations, 371558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch ShortcutCreationReason creation_reason) { 37290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 37390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 37490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Shortcut paths under which to create shortcuts. 37590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath> shortcut_paths = 37690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) GetShortcutPaths(creation_locations); 37790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 37890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool pin_to_taskbar = creation_locations.in_quick_launch_bar && 37990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) (base::win::GetVersion() >= base::win::VERSION_WIN7); 38090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 38190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Create/update the shortcut in the web app path for the "Pin To Taskbar" 38290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // option in Win7. We use the web app path shortcut because we will overwrite 38390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // it rather than appending unique numbers if the shortcut already exists. 38490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // This prevents pinned apps from having unique numbers in their names. 38590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (pin_to_taskbar) 38690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) shortcut_paths.push_back(web_app_path); 38790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 38890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (shortcut_paths.empty()) 38990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 39090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 391eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (!CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths, 392558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch creation_reason, NULL)) 39390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 39490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 39590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (pin_to_taskbar) { 39690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath file_name = 39790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) web_app::internals::GetSanitizedFileName(shortcut_info.title); 3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Use the web app path shortcut for pinning to avoid having unique numbers 3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // in the application name. 4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath shortcut_to_pin = web_app_path.Append(file_name). 401c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) AddExtension(installer::kLnkExt); 40290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (!base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str())) 40390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 40690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return true; 4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void UpdatePlatformShortcuts( 4102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& web_app_path, 411a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& old_app_title, 4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ShellIntegration::ShortcutInfo& shortcut_info) { 413c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Generates file name to use with persisted ico and shortcut file. 4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath file_name = 4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) web_app::internals::GetSanitizedFileName(shortcut_info.title); 4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 41990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (old_app_title != shortcut_info.title) { 42090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // The app's title has changed. Delete all existing app shortcuts and 42190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // recreate them in any locations they already existed (but do not add them 42290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // to locations where they do not currently exist). 42390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) bool was_pinned_to_taskbar; 42490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::vector<base::FilePath> shortcut_paths; 42590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) GetShortcutLocationsAndDeleteShortcuts( 42690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) web_app_path, shortcut_info.profile_path, old_app_title, 42790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) &was_pinned_to_taskbar, &shortcut_paths); 428eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths, 429558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch SHORTCUT_CREATION_BY_USER, NULL); 43090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // If the shortcut was pinned to the taskbar, 43190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // GetShortcutLocationsAndDeleteShortcuts will have deleted it. In that 43290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // case, re-pin it. 43390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (was_pinned_to_taskbar) { 43490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath file_name = 43590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) web_app::internals::GetSanitizedFileName(shortcut_info.title); 43690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Use the web app path shortcut for pinning to avoid having unique 43790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // numbers in the application name. 43890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::FilePath shortcut_to_pin = web_app_path.Append(file_name). 43990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) AddExtension(installer::kLnkExt); 44090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str()); 44190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 44290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 44390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If an icon file exists, and is out of date, replace it with the new icon 4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and let the shell know the icon has been modified. 446c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath icon_file = web_app_path.Append(file_name).AddExtension( 4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FILE_PATH_LITERAL(".ico")); 4487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (base::PathExists(icon_file)) { 449c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon); 4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DeletePlatformShortcuts( 4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& web_app_path, 4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ShellIntegration::ShortcutInfo& shortcut_info) { 45690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) GetShortcutLocationsAndDeleteShortcuts( 45790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) web_app_path, shortcut_info.profile_path, shortcut_info.title, NULL, 45890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) NULL); 459c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 460c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // If there are no more shortcuts in the Chrome Apps subdirectory, remove it. 461c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath chrome_apps_dir; 462a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (ShellUtil::GetShortcutPath( 463a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR, 464a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) BrowserDistribution::GetDistribution(), 465a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellUtil::CURRENT_USER, 466a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) &chrome_apps_dir)) { 467a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (base::IsDirectoryEmpty(chrome_apps_dir)) 4687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch base::DeleteFile(chrome_apps_dir, false); 469eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 470eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 471eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 472eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid DeleteAllShortcutsForProfile(const base::FilePath& profile_path) { 473eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch GetShortcutLocationsAndDeleteShortcuts(base::FilePath(), profile_path, L"", 474eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch NULL, NULL); 475eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 476eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // If there are no more shortcuts in the Chrome Apps subdirectory, remove it. 477eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::FilePath chrome_apps_dir; 478a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (ShellUtil::GetShortcutPath( 479a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR, 480a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) BrowserDistribution::GetDistribution(), 481a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellUtil::CURRENT_USER, 482a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) &chrome_apps_dir)) { 483a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (base::IsDirectoryEmpty(chrome_apps_dir)) 4847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch base::DeleteFile(chrome_apps_dir, false); 485c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 486c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 487c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 488c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)std::vector<base::FilePath> GetShortcutPaths( 489c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const ShellIntegration::ShortcutLocations& creation_locations) { 490c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Shortcut paths under which to create shortcuts. 491c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::vector<base::FilePath> shortcut_paths; 492c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Locations to add to shortcut_paths. 493c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) struct { 494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bool use_this_location; 495a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellUtil::ShortcutLocation location_id; 496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } locations[] = { 497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) { 498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) creation_locations.on_desktop, 499a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellUtil::SHORTCUT_LOCATION_DESKTOP 500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) }, { 501a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) creation_locations.applications_menu_location == 502a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellIntegration::APP_MENU_LOCATION_ROOT, 503a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellUtil::SHORTCUT_LOCATION_START_MENU_ROOT 504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) }, { 505a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) creation_locations.applications_menu_location == 506a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellIntegration::APP_MENU_LOCATION_SUBDIR_CHROME, 507a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_DIR 508a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) }, { 509a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) creation_locations.applications_menu_location == 510a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellIntegration::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS, 511a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellUtil::SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR 512a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) }, { 513a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // For Win7+, |in_quick_launch_bar| indicates that we are pinning to 514a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // taskbar. This needs to be handled by callers. 515a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) creation_locations.in_quick_launch_bar && 516a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::win::GetVersion() < base::win::VERSION_WIN7, 517a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellUtil::SHORTCUT_LOCATION_QUICK_LAUNCH 518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) }; 520a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 521a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Populate shortcut_paths. 523c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) for (int i = 0; i < arraysize(locations); ++i) { 524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) if (locations[i].use_this_location) { 525c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::FilePath path; 526a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) if (!ShellUtil::GetShortcutPath(locations[i].location_id, 527a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) dist, 528a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ShellUtil::CURRENT_USER, 529a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) &path)) { 530a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) NOTREACHED(); 531c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) continue; 532c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 533c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) shortcut_paths.push_back(path); 534c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 535c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 536c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return shortcut_paths; 5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace internals 5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace web_app 542