diff --git a/xpcom/io/nsILocalFileWin.idl b/xpcom/io/nsILocalFileWin.idl index e76c7e69eb0..283ad071dda 100644 --- a/xpcom/io/nsILocalFileWin.idl +++ b/xpcom/io/nsILocalFileWin.idl @@ -83,5 +83,37 @@ interface nsILocalFileWin : nsILocalFile * Throws NS_ERROR_FAILURE if the set or get fails. */ attribute unsigned long fileAttributesWin; + + /** + * setShortcut + * + * Creates the specified shortcut, or updates it if it already exists. + * + * If the shortcut is being updated (i.e. the shortcut already exists), + * any excluded parameters will remain unchanged in the shortcut file. + * For example, if you want to change the description of a specific + * shortcut but keep the target, working dir, args, and icon the same, + * pass null for those parameters and only pass in a value for the + * description. + * + * If the shortcut does not already exist and targetFile is not specified, + * setShortcut will throw NS_ERROR_FILE_TARGET_DOES_NOT_EXIST. + * + * @param targetFile the path that the shortcut should target + * @param workingDir the working dir that should be set for the shortcut + * @param args the args string that should be set for the shortcut + * @param description the description that should be set for the shortcut + * @param iconFile the file containing an icon to be used for this + shortcut + * @param iconIndex this value selects a specific icon from within + iconFile. If iconFile contains only one icon, this + value should be 0. + */ + void setShortcut([optional] in nsILocalFile targetFile, + [optional] in nsILocalFile workingDir, + [optional] in wstring args, + [optional] in wstring description, + [optional] in nsILocalFile iconFile, + [optional] in long iconIndex); }; diff --git a/xpcom/io/nsLocalFileWin.cpp b/xpcom/io/nsLocalFileWin.cpp index 2538933983e..fb988ba48f4 100644 --- a/xpcom/io/nsLocalFileWin.cpp +++ b/xpcom/io/nsLocalFileWin.cpp @@ -338,6 +338,14 @@ public: nsresult Init(); nsresult Resolve(const WCHAR* in, WCHAR* out); + nsresult SetShortcut(bool updateExisting, + const WCHAR* shortcutPath, + const WCHAR* targetPath, + const WCHAR* workingDir, + const WCHAR* args, + const WCHAR* description, + const WCHAR* iconFile, + PRInt32 iconIndex); private: Mutex mLock; @@ -389,6 +397,76 @@ ShortcutResolver::Resolve(const WCHAR* in, WCHAR* out) return NS_OK; } +nsresult +ShortcutResolver::SetShortcut(bool updateExisting, + const WCHAR* shortcutPath, + const WCHAR* targetPath, + const WCHAR* workingDir, + const WCHAR* args, + const WCHAR* description, + const WCHAR* iconPath, + PRInt32 iconIndex) +{ + if (!mShellLink) { + return NS_ERROR_FAILURE; + } + + if (!shortcutPath) { + return NS_ERROR_FAILURE; + } + + MutexAutoLock lock(mLock); + + if (updateExisting) { + if (FAILED(mPersistFile->Load(shortcutPath, STGM_READWRITE))) { + return NS_ERROR_FAILURE; + } + } else { + if (!targetPath) { + return NS_ERROR_FILE_TARGET_DOES_NOT_EXIST; + } + + // Since we reuse our IPersistFile, we have to clear out any values that + // may be left over from previous calls to SetShortcut. + if (FAILED(mShellLink->SetWorkingDirectory(L"")) + || FAILED(mShellLink->SetArguments(L"")) + || FAILED(mShellLink->SetDescription(L"")) + || FAILED(mShellLink->SetIconLocation(L"", 0))) { + return NS_ERROR_FAILURE; + } + } + + if (targetPath && FAILED(mShellLink->SetPath(targetPath))) { + return NS_ERROR_FAILURE; + } + + if (workingDir && FAILED(mShellLink->SetWorkingDirectory(workingDir))) { + return NS_ERROR_FAILURE; + } + + if (args && FAILED(mShellLink->SetArguments(args))) { + return NS_ERROR_FAILURE; + } + + if (description && FAILED(mShellLink->SetDescription(description))) { + return NS_ERROR_FAILURE; + } + + if (iconPath && FAILED(mShellLink->SetIconLocation(iconPath, iconIndex))) { + return NS_ERROR_FAILURE; + } + + if (FAILED(mPersistFile->Save(shortcutPath, + TRUE))) { + // Second argument indicates whether the file path specified in the + // first argument should become the "current working file" for this + // IPersistFile + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + static ShortcutResolver * gResolver = nsnull; static nsresult NS_CreateShortcutResolver() @@ -1622,6 +1700,66 @@ nsLocalFile::GetVersionInfoField(const char* aField, nsAString& _retval) return rv; } +NS_IMETHODIMP +nsLocalFile::SetShortcut(nsILocalFile* targetFile, + nsILocalFile* workingDir, + const PRUnichar* args, + const PRUnichar* description, + nsILocalFile* iconFile, + PRInt32 iconIndex) +{ + bool exists; + nsresult rv = this->Exists(&exists); + if (NS_FAILED(rv)) { + return rv; + } + + const WCHAR* targetFilePath = NULL; + const WCHAR* workingDirPath = NULL; + const WCHAR* iconFilePath = NULL; + + nsAutoString targetFilePathAuto; + if (targetFile) { + rv = targetFile->GetPath(targetFilePathAuto); + if (NS_FAILED(rv)) { + return rv; + } + targetFilePath = targetFilePathAuto.get(); + } + + nsAutoString workingDirPathAuto; + if (workingDir) { + rv = workingDir->GetPath(workingDirPathAuto); + if (NS_FAILED(rv)) { + return rv; + } + workingDirPath = workingDirPathAuto.get(); + } + + nsAutoString iconPathAuto; + if (iconFile) { + rv = iconFile->GetPath(iconPathAuto); + if (NS_FAILED(rv)) { + return rv; + } + iconFilePath = iconPathAuto.get(); + } + + rv = gResolver->SetShortcut(exists, + mWorkingPath.get(), + targetFilePath, + workingDirPath, + args, + description, + iconFilePath, + iconFilePath? iconIndex : 0); + if (targetFilePath && NS_SUCCEEDED(rv)) { + MakeDirty(); + } + + return rv; +} + /** * Determines if the drive type for the specified file is rmeote or local. * diff --git a/xpcom/tests/unit/test_windows_shortcut.js b/xpcom/tests/unit/test_windows_shortcut.js new file mode 100644 index 00000000000..3b58f050662 --- /dev/null +++ b/xpcom/tests/unit/test_windows_shortcut.js @@ -0,0 +1,279 @@ +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et: */ + +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +const Cr = Components.results; +const Ci = Components.interfaces; +const Cc = Components.classes; +const Cu = Components.utils; +const CC = Components.Constructor; + +const LocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath"); + +Cu.import("resource://gre/modules/Services.jsm"); + +function run_test() +{ + // This test makes sense only on Windows, so skip it on other platforms + if ("nsILocalFileWin" in Ci + && do_get_cwd() instanceof Ci.nsILocalFileWin) { + + let tempDir = Services.dirsvc.get("TmpD", Ci.nsILocalFile); + tempDir.append("shortcutTesting"); + tempDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0666); + + test_create_noargs(tempDir); + test_create_notarget(tempDir); + test_create_targetonly(tempDir); + test_create_normal(tempDir); + test_create_unicode(tempDir); + + test_update_noargs(tempDir); + test_update_notarget(tempDir); + test_update_targetonly(tempDir); + test_update_normal(tempDir); + test_update_unicode(tempDir); + } +} + +function test_create_noargs(tempDir) +{ + let shortcutFile = tempDir.clone(); + shortcutFile.append("shouldNeverExist.lnk"); + shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); + + try + { + win.setShortcut(); + do_throw("Creating a shortcut with no args (no target) should throw"); + } + catch(e if (e instanceof Ci.nsIException + && e.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)) + { + + } +} + +function test_create_notarget(tempDir) +{ + let shortcutFile = tempDir.clone(); + shortcutFile.append("shouldNeverExist2.lnk"); + shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); + + try + { + win.setShortcut(null, + do_get_cwd(), + "arg1 arg2", + "Shortcut with no target"); + do_throw("Creating a shortcut with no target should throw"); + } + catch(e if (e instanceof Ci.nsIException + && e.result == Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)) + { + + } +} + +function test_create_targetonly(tempDir) +{ + let shortcutFile = tempDir.clone(); + shortcutFile.append("createdShortcut.lnk"); + shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let targetFile = tempDir.clone(); + targetFile.append("shortcutTarget.exe"); + targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); + + win.setShortcut(targetFile); + + let shortcutTarget = LocalFile(shortcutFile.target); + do_check_true(shortcutTarget.equals(targetFile)); +} + +function test_create_normal(tempDir) +{ + let shortcutFile = tempDir.clone(); + shortcutFile.append("createdShortcut.lnk"); + shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let targetFile = tempDir.clone(); + targetFile.append("shortcutTarget.exe"); + targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); + + win.setShortcut(targetFile, + do_get_cwd(), + "arg1 arg2", + "Ordinary shortcut"); + + let shortcutTarget = LocalFile(shortcutFile.target); + do_check_true(shortcutTarget.equals(targetFile)) +} + +function test_create_unicode(tempDir) +{ + let shortcutFile = tempDir.clone(); + shortcutFile.append("createdShortcut.lnk"); + shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let targetFile = tempDir.clone(); + targetFile.append("ṩhогТϾừ†Target.exe"); + targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); + + win.setShortcut(targetFile, + do_get_cwd(), // XXX: This should probably be a unicode dir + "ᾶṟǵ1 ᾶṟǵ2", + "ῧṋіḉѻₑ"); + + let shortcutTarget = LocalFile(shortcutFile.target); + do_check_true(shortcutTarget.equals(targetFile)) +} + +function test_update_noargs(tempDir) +{ + let shortcutFile = tempDir.clone(); + shortcutFile.append("createdShortcut.lnk"); + shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let targetFile = tempDir.clone(); + targetFile.append("shortcutTarget.exe"); + targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); + + win.setShortcut(targetFile, + do_get_cwd(), + "arg1 arg2", + "A sample shortcut"); + + win.setShortcut(); + + let shortcutTarget = LocalFile(shortcutFile.target); + do_check_true(shortcutTarget.equals(targetFile)) +} + +function test_update_notarget(tempDir) +{ + let shortcutFile = tempDir.clone(); + shortcutFile.append("createdShortcut.lnk"); + shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let targetFile = tempDir.clone(); + targetFile.append("shortcutTarget.exe"); + targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); + + win.setShortcut(targetFile, + do_get_cwd(), + "arg1 arg2", + "A sample shortcut"); + + win.setShortcut(null, + do_get_profile(), + "arg3 arg4", + "An UPDATED shortcut"); + + let shortcutTarget = LocalFile(shortcutFile.target); + do_check_true(shortcutTarget.equals(targetFile)) +} + +function test_update_targetonly(tempDir) +{ + let shortcutFile = tempDir.clone(); + shortcutFile.append("createdShortcut.lnk"); + shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let targetFile = tempDir.clone(); + targetFile.append("shortcutTarget.exe"); + targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); + + win.setShortcut(targetFile, + do_get_cwd(), + "arg1 arg2", + "A sample shortcut"); + + let newTargetFile = tempDir.clone(); + newTargetFile.append("shortcutTarget.exe"); + shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + win.setShortcut(newTargetFile); + + let shortcutTarget = LocalFile(shortcutFile.target); + do_check_true(shortcutTarget.equals(newTargetFile)) +} + +function test_update_normal(tempDir) +{ + let shortcutFile = tempDir.clone(); + shortcutFile.append("createdShortcut.lnk"); + shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let targetFile = tempDir.clone(); + targetFile.append("shortcutTarget.exe"); + targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); + + win.setShortcut(targetFile, + do_get_cwd(), + "arg1 arg2", + "A sample shortcut"); + + let newTargetFile = tempDir.clone(); + newTargetFile.append("shortcutTarget.exe"); + newTargetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + win.setShortcut(newTargetFile, + do_get_profile(), + "arg3 arg4", + "An UPDATED shortcut"); + + let shortcutTarget = LocalFile(shortcutFile.target); + do_check_true(shortcutTarget.equals(newTargetFile)) +} + +function test_update_unicode(tempDir) +{ + let shortcutFile = tempDir.clone(); + shortcutFile.append("createdShortcut.lnk"); + shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let targetFile = tempDir.clone(); + targetFile.append("shortcutTarget.exe"); + targetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + let win = shortcutFile.QueryInterface(Ci.nsILocalFileWin); + + win.setShortcut(targetFile, + do_get_cwd(), + "arg1 arg2", + "A sample shortcut"); + + let newTargetFile = tempDir.clone(); + newTargetFile.append("ṩhогТϾừ†Target.exe"); + shortcutFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); + + win.setShortcut(newTargetFile, + do_get_profile(), // XXX: This should probably be unicode + "ᾶṟǵ3 ᾶṟǵ4", + "A ῧṋіḉѻₑ shortcut"); + + let shortcutTarget = LocalFile(shortcutFile.target); + do_check_true(shortcutTarget.equals(newTargetFile)) +} diff --git a/xpcom/tests/unit/xpcshell.ini b/xpcom/tests/unit/xpcshell.ini index 60f559d9bd6..117432058d8 100644 --- a/xpcom/tests/unit/xpcshell.ini +++ b/xpcom/tests/unit/xpcshell.ini @@ -45,5 +45,6 @@ fail-if = os == "android" [test_versioncomparator.js] [test_comp_no_aslr.js] fail-if = os != "win" +[test_windows_shortcut.js] [test_bug745466.js] skip-if = os == "win"