diff --git a/browser/components/shell/Windows11TaskbarPinning.cpp b/browser/components/shell/Windows11TaskbarPinning.cpp index 31a6af66bf7c..0c6da9a853f4 100644 --- a/browser/components/shell/Windows11TaskbarPinning.cpp +++ b/browser/components/shell/Windows11TaskbarPinning.cpp @@ -17,6 +17,7 @@ #include "mozilla/UniquePtr.h" #include "mozilla/WinHeaderOnlyUtils.h" #include "mozilla/widget/WinTaskbar.h" +#include "WinUtils.h" #include "mozilla/Logging.h" @@ -213,21 +214,27 @@ Win11PinToTaskBarResult PinCurrentAppToTaskbarWin11( primaryAumid = nsString(primaryAumid)]( Win11PinToTaskBarResultStatus status) { // Set AUMID back and ensure the icon is set correctly - HRESULT hr = - SetCurrentProcessExplicitAppUserModelID(primaryAumid.get()); - if (FAILED(hr)) { - TASKBAR_PINNING_LOG(LogLevel::Debug, - "Taskbar: reverting AUMID after pinning " - "operation failed. HRESULT = 0x%lx", - hr); + if (!widget::WinUtils::HasPackageIdentity()) { + HRESULT hr = + SetCurrentProcessExplicitAppUserModelID(primaryAumid.get()); + if (FAILED(hr)) { + TASKBAR_PINNING_LOG(LogLevel::Debug, + "Taskbar: reverting AUMID after pinning " + "operation failed. HRESULT = 0x%lx", + hr); + } } resultStatus = status; event.Set(); }; - hr = SetCurrentProcessExplicitAppUserModelID(aumid.get()); - if (FAILED(hr)) { - return CompletedOperations(Win11PinToTaskBarResultStatus::Failed); + // Set the process to have the AUMID of the shortcut we want to pin, + // this is only necessary for Win32 builds + if (!widget::WinUtils::HasPackageIdentity()) { + hr = SetCurrentProcessExplicitAppUserModelID(aumid.get()); + if (FAILED(hr)) { + return CompletedOperations(Win11PinToTaskBarResultStatus::Failed); + } } ComPtr taskbar; @@ -264,13 +271,15 @@ Win11PinToTaskBarResult PinCurrentAppToTaskbarWin11( [&event, &resultStatus, primaryAumid](Win11PinToTaskBarResultStatus status) -> HRESULT { // Set AUMID back and ensure the icon is set correctly - HRESULT hr = - SetCurrentProcessExplicitAppUserModelID(primaryAumid.get()); - if (FAILED(hr)) { - TASKBAR_PINNING_LOG(LogLevel::Debug, - "Taskbar: reverting AUMID after pinning " - "operation failed. HRESULT = 0x%lx", - hr); + if (!widget::WinUtils::HasPackageIdentity()) { + HRESULT hr = + SetCurrentProcessExplicitAppUserModelID(primaryAumid.get()); + if (FAILED(hr)) { + TASKBAR_PINNING_LOG(LogLevel::Debug, + "Taskbar: reverting AUMID after pinning " + "operation failed. HRESULT = 0x%lx", + hr); + } } resultStatus = status; event.Set(); diff --git a/browser/components/shell/nsIWindowsShellService.idl b/browser/components/shell/nsIWindowsShellService.idl index 37d858b10896..4ec61c6ce061 100644 --- a/browser/components/shell/nsIWindowsShellService.idl +++ b/browser/components/shell/nsIWindowsShellService.idl @@ -424,6 +424,16 @@ interface nsIWindowsShellService : nsIShellService boolean checkAllProgIDsExist(); boolean checkBrowserUserChoiceHashes(); + /* + * Retrieves the application-defined, explicit Application User Model ID + * for the current process. This function is only to be used on + * Windows for testing purposes + * + * @return string AUMID + * @throws NS_ERROR_FAILURE when the AUMID cannot be fetched + */ + AString checkCurrentProcessAUMIDForTesting(); + /* * Determines whether or not Firefox is the "Default Handler", i.e., * is registered to handle, the given file extension (like ".pdf") diff --git a/browser/components/shell/nsWindowsShellService.cpp b/browser/components/shell/nsWindowsShellService.cpp index 29126a828735..770bc5d798d2 100644 --- a/browser/components/shell/nsWindowsShellService.cpp +++ b/browser/components/shell/nsWindowsShellService.cpp @@ -377,6 +377,29 @@ nsWindowsShellService::CheckBrowserUserChoiceHashes(bool* aResult) { return NS_OK; } +NS_IMETHODIMP +nsWindowsShellService::CheckCurrentProcessAUMIDForTesting( + nsAString& aRetAumid) { + PWSTR id; + HRESULT hr; + + // We can't fetch process AUMID for MSIX builds, + // since SetCurrentProcessExplicitAppUserModelID + // has no effect on it + if (widget::WinUtils::HasPackageIdentity()) { + aRetAumid.Assign(u"MSIXAumidTestValue"_ns); + } else { + hr = GetCurrentProcessExplicitAppUserModelID(&id); + if (FAILED(hr)) { + return NS_ERROR_FAILURE; + } + aRetAumid.Assign(id); + CoTaskMemFree(id); + } + + return NS_OK; +} + NS_IMETHODIMP nsWindowsShellService::CanSetDefaultBrowserUserChoice(bool* aResult) { *aResult = false; diff --git a/browser/components/shell/test/browser.toml b/browser/components/shell/test/browser.toml index b699e896a31a..83233efbdbd9 100644 --- a/browser/components/shell/test/browser.toml +++ b/browser/components/shell/test/browser.toml @@ -91,6 +91,9 @@ skip-if = [ run-if = ["os == 'win'"] tags = "os_integration" +["browser_processAUMID.js"] +run-if = ["os == 'win'"] + ["browser_setDefaultBrowser.js"] tags = "os_integration" diff --git a/browser/components/shell/test/browser_processAUMID.js b/browser/components/shell/test/browser_processAUMID.js new file mode 100644 index 000000000000..58d271e75660 --- /dev/null +++ b/browser/components/shell/test/browser_processAUMID.js @@ -0,0 +1,27 @@ +/* Any copyright is dedicated to the Public Domain. + * https://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Bug 1950734 tracks how calling PinCurrentAppToTaskbarWin11 + * on MSIX may cause the process AUMID to be unnecessarily changed. + * This test verifies that the behaviour will no longer happen + */ + +ChromeUtils.defineESModuleGetters(this, { + ShellService: "resource:///modules/ShellService.sys.mjs", +}); + +add_task(async function test_processAUMID() { + let processAUMID = ShellService.checkCurrentProcessAUMIDForTesting(); + + // This function will trigger the relevant code paths that + // incorrectly changes the process AUMID on MSIX, prior to + // Bug 1950734 being fixed + await ShellService.checkPinCurrentAppToTaskbarAsync(false); + + is( + processAUMID, + ShellService.checkCurrentProcessAUMIDForTesting(), + "The process AUMID should not be changed" + ); +});