diff --git a/browser/components/shell/moz.build b/browser/components/shell/moz.build index 82e5afade7ba..8e4858ca84b5 100644 --- a/browser/components/shell/moz.build +++ b/browser/components/shell/moz.build @@ -9,6 +9,9 @@ LOCAL_INCLUDES += ["/xpcom/build"] BROWSER_CHROME_MANIFESTS += ["test/browser.toml"] XPCSHELL_TESTS_MANIFESTS += ["test/unit/xpcshell.toml"] +TEST_DIRS += [ + "test/gtest", +] JAR_MANIFESTS += ["jar.mn"] diff --git a/browser/components/shell/nsWindowsShellService.cpp b/browser/components/shell/nsWindowsShellService.cpp index fe3511c91e48..f8d939121cc5 100644 --- a/browser/components/shell/nsWindowsShellService.cpp +++ b/browser/components/shell/nsWindowsShellService.cpp @@ -6,6 +6,7 @@ #define UNICODE #include "nsWindowsShellService.h" +#include "nsWindowsShellServiceInternal.h" #include "BinaryPath.h" #include "imgIContainer.h" @@ -755,19 +756,12 @@ static nsresult WriteShortcutToLog(nsIFile* aShortcutsLogDir, return NS_OK; } -static nsresult CreateShortcutImpl( - nsIFile* aBinary, const CopyableTArray& aArguments, - const nsAString& aDescription, nsIFile* aIconFile, uint16_t aIconIndex, - const nsAString& aAppUserModelId, KNOWNFOLDERID aShortcutFolder, - const nsAString& aShortcutName, const nsString& aShortcutFile, - nsIFile* aShortcutsLogDir) { - NS_ENSURE_ARG(aBinary); - NS_ENSURE_ARG(aIconFile); - - nsresult rv = - WriteShortcutToLog(aShortcutsLogDir, aShortcutFolder, aShortcutName); - NS_ENSURE_SUCCESS(rv, rv); - +nsresult CreateShellLinkObject(nsIFile* aBinary, + const CopyableTArray& aArguments, + const nsAString& aDescription, + nsIFile* aIconFile, uint16_t aIconIndex, + const nsAString& aAppUserModelId, + IShellLinkW** aLink) { RefPtr link; HRESULT hr = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLinkW, getter_AddRefs(link)); @@ -787,8 +781,8 @@ static nsresult CreateShortcutImpl( // TODO: Properly escape quotes in the string, see bug 1604287. nsString arguments; - for (auto& arg : aArguments) { - arguments.AppendPrintf("\"%S\" ", static_cast(arg.get())); + for (const auto& arg : aArguments) { + arguments += u"\""_ns + arg + u"\" "_ns; } link->SetArguments(arguments.get()); @@ -817,8 +811,30 @@ static nsresult CreateShortcutImpl( NS_ENSURE_HRESULT(hr, NS_ERROR_FAILURE); } + link.forget(aLink); + return NS_OK; +} + +static nsresult CreateShortcutImpl( + nsIFile* aBinary, const CopyableTArray& aArguments, + const nsAString& aDescription, nsIFile* aIconFile, uint16_t aIconIndex, + const nsAString& aAppUserModelId, KNOWNFOLDERID aShortcutFolder, + const nsAString& aShortcutName, const nsString& aShortcutFile, + nsIFile* aShortcutsLogDir) { + NS_ENSURE_ARG(aBinary); + NS_ENSURE_ARG(aIconFile); + + nsresult rv = + WriteShortcutToLog(aShortcutsLogDir, aShortcutFolder, aShortcutName); + NS_ENSURE_SUCCESS(rv, rv); + + RefPtr link; + rv = CreateShellLinkObject(aBinary, aArguments, aDescription, aIconFile, + aIconIndex, aAppUserModelId, getter_AddRefs(link)); + NS_ENSURE_SUCCESS(rv, rv); + RefPtr persist; - hr = link->QueryInterface(IID_IPersistFile, getter_AddRefs(persist)); + HRESULT hr = link->QueryInterface(IID_IPersistFile, getter_AddRefs(persist)); NS_ENSURE_HRESULT(hr, NS_ERROR_FAILURE); hr = persist->Save(aShortcutFile.get(), TRUE); diff --git a/browser/components/shell/nsWindowsShellServiceInternal.h b/browser/components/shell/nsWindowsShellServiceInternal.h new file mode 100644 index 000000000000..967ecfb1a991 --- /dev/null +++ b/browser/components/shell/nsWindowsShellServiceInternal.h @@ -0,0 +1,24 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef nswindowsshellserviceinternal_h____ +#define nswindowsshellserviceinternal_h____ + +#include "ErrorList.h" +#include "mozilla/RefPtr.h" +#include "nsStringFwd.h" +#include "nsTArray.h" +#include "nsIFile.h" + +#include + +nsresult CreateShellLinkObject(nsIFile* aBinary, + const CopyableTArray& aArguments, + const nsAString& aDescription, + nsIFile* aIconFile, uint16_t aIconIndex, + const nsAString& aAppUserModelId, + IShellLinkW** aLink); + +#endif // nswindowsshellserviceinternal_h____ diff --git a/browser/components/shell/test/gtest/ShellLinkTests.cpp b/browser/components/shell/test/gtest/ShellLinkTests.cpp new file mode 100644 index 000000000000..97ebd8dd1ff7 --- /dev/null +++ b/browser/components/shell/test/gtest/ShellLinkTests.cpp @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#include "gtest/gtest.h" + +#include "nsDirectoryServiceDefs.h" +#include "nsDirectoryServiceUtils.h" +#include "nsWindowsShellServiceInternal.h" +#include "nsXULAppAPI.h" + +#include + +TEST(ShellLink, NarrowCharacterArguments) +{ + nsCOMPtr exe; + nsresult rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(exe)); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + RefPtr link; + rv = CreateShellLinkObject(exe, {u"test"_ns}, u"test"_ns, exe, 0, u"aumid"_ns, + getter_AddRefs(link)); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(link != nullptr); + + std::wstring testArgs = L"\"test\" "; + + wchar_t resultArgs[sizeof(testArgs)]; + HRESULT hr = link->GetArguments(resultArgs, sizeof(resultArgs)); + ASSERT_TRUE(SUCCEEDED(hr)); + + ASSERT_TRUE(testArgs == resultArgs); +} + +TEST(ShellLink, WideCharacterArguments) +{ + nsCOMPtr exe; + nsresult rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(exe)); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + + RefPtr link; + rv = CreateShellLinkObject(exe, {u"Test\\テスト用アカウント\\Test"_ns}, + u"test"_ns, exe, 0, u"aumid"_ns, + getter_AddRefs(link)); + ASSERT_TRUE(NS_SUCCEEDED(rv)); + ASSERT_TRUE(link != nullptr); + + std::wstring testArgs = L"\"Test\\テスト用アカウント\\Test\" "; + + wchar_t resultArgs[sizeof(testArgs)]; + HRESULT hr = link->GetArguments(resultArgs, sizeof(resultArgs)); + ASSERT_TRUE(SUCCEEDED(hr)); + + ASSERT_TRUE(testArgs == resultArgs); +} diff --git a/browser/components/shell/test/gtest/moz.build b/browser/components/shell/test/gtest/moz.build new file mode 100644 index 000000000000..89e378e19fdb --- /dev/null +++ b/browser/components/shell/test/gtest/moz.build @@ -0,0 +1,14 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +if CONFIG["MOZ_WIDGET_TOOLKIT"] == "windows": + LOCAL_INCLUDES += ["/browser/components/shell"] + + UNIFIED_SOURCES += [ + "ShellLinkTests.cpp", + ] + +FINAL_LIBRARY = "xul-gtest"