Skip to content

Commit

Permalink
Bug 1860271 - Resolved launch on login checkbox and infobar bugs when…
Browse files Browse the repository at this point in the history
… user created Startup shortcut exists r=nalexander,settings-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D191763
  • Loading branch information
nipunshukla21 committed Dec 11, 2023
1 parent 9f7d060 commit 954e579
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 23 deletions.
20 changes: 13 additions & 7 deletions browser/components/BrowserGlue.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,18 +1237,24 @@ BrowserGlue.prototype = {
].getService(Ci.nsIToolkitProfileService);
if (
AppConstants.platform == "win" &&
Services.prefs.getBoolPref(launchOnLoginPref) &&
!profileSvc.startWithLastProfile
) {
// If we don't start with last profile, the user
// likely sees the profile selector on launch.
if (Services.prefs.getBoolPref(launchOnLoginPref)) {
Services.telemetry.setEventRecordingEnabled(
"launch_on_login",
true
);
Services.telemetry.recordEvent(
"launch_on_login",
"last_profile_disable",
"startup"
);
}
Services.prefs.setBoolPref(launchOnLoginPref, false);
Services.telemetry.setEventRecordingEnabled("launch_on_login", true);
Services.telemetry.recordEvent(
"launch_on_login",
"last_profile_disable:",
"startup"
);
// Only remove registry key, not shortcut here as we can assume
// if a user manually created a shortcut they want this behavior.
await lazy.WindowsLaunchOnLogin.removeLaunchOnLoginRegistryKey();
}
break;
Expand Down
8 changes: 8 additions & 0 deletions browser/components/newtab/lib/ASRouterTargeting.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ChromeUtils.defineESModuleGetters(lazy, {
TargetingContext: "resource://messaging-system/targeting/Targeting.sys.mjs",
TelemetryEnvironment: "resource://gre/modules/TelemetryEnvironment.sys.mjs",
TelemetrySession: "resource://gre/modules/TelemetrySession.sys.mjs",
WindowsLaunchOnLogin: "resource://gre/modules/WindowsLaunchOnLogin.sys.mjs",
});

XPCOMUtils.defineLazyModuleGetters(lazy, {
Expand Down Expand Up @@ -836,6 +837,13 @@ const TargetingGetters = {
return QueryCache.getters.doesAppNeedPrivatePin.get();
},

get launchOnLoginEnabled() {
if (AppConstants.platform !== "win") {
return false;
}
return lazy.WindowsLaunchOnLogin.getLaunchOnLoginEnabled();
},

/**
* Is this invocation running in background task mode?
*
Expand Down
6 changes: 4 additions & 2 deletions browser/components/newtab/lib/OnboardingMessageProvider.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,8 @@ const BASE_MESSAGES = () => [
lifetime: 1,
},
trigger: { id: "defaultBrowserCheck" },
targeting: `source == 'newtab' && 'browser.startup.windowsLaunchOnLogin.disableLaunchOnLoginPrompt'|preferenceValue == false
targeting: `source == 'newtab' && !launchOnLoginEnabled
&& 'browser.startup.windowsLaunchOnLogin.disableLaunchOnLoginPrompt'|preferenceValue == false
&& 'browser.startup.windowsLaunchOnLogin.enabled'|preferenceValue == true && isDefaultBrowser && !activeNotifications`,
},
{
Expand Down Expand Up @@ -1053,7 +1054,8 @@ const BASE_MESSAGES = () => [
lifetime: 1,
},
trigger: { id: "defaultBrowserCheck" },
targeting: `source == 'newtab' && 'browser.startup.windowsLaunchOnLogin.disableLaunchOnLoginPrompt'|preferenceValue == false
targeting: `source == 'newtab' && !launchOnLoginEnabled
&& 'browser.startup.windowsLaunchOnLogin.disableLaunchOnLoginPrompt'|preferenceValue == false
&& 'browser.startup.windowsLaunchOnLogin.enabled'|preferenceValue == true && isDefaultBrowser && !activeNotifications
&& messageImpressions.INFOBAR_LAUNCH_ON_LOGIN[messageImpressions.INFOBAR_LAUNCH_ON_LOGIN | length - 1]
&& messageImpressions.INFOBAR_LAUNCH_ON_LOGIN[messageImpressions.INFOBAR_LAUNCH_ON_LOGIN | length - 1] <
Expand Down
28 changes: 14 additions & 14 deletions browser/components/preferences/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,15 @@ var gMainPane = {
NimbusFeatures.windowsLaunchOnLogin.recordExposureEvent({
once: true,
});
if (NimbusFeatures.windowsLaunchOnLogin.getVariable("enabled")) {
// We do a check here for startWithLastProfile as we could
// have disabled the pref for the user before they're ever
// exposed to the experiment on a new profile.
if (
NimbusFeatures.windowsLaunchOnLogin.getVariable("enabled") &&
Cc["@mozilla.org/toolkit/profile-service;1"].getService(
Ci.nsIToolkitProfileService
).startWithLastProfile
) {
document.getElementById("windowsLaunchOnLoginBox").hidden = false;
}
}
Expand Down Expand Up @@ -659,17 +667,8 @@ var gMainPane = {
let launchOnLoginCheckbox = document.getElementById(
"windowsLaunchOnLogin"
);
let registryName = WindowsLaunchOnLogin.getLaunchOnLoginRegistryName();
WindowsLaunchOnLogin.withLaunchOnLoginRegistryKey(async wrk => {
try {
// Reflect registry key value in about:preferences
launchOnLoginCheckbox.checked = wrk.hasValue(registryName);
} catch (e) {
// We should only end up here if we fail to open the registry
console.error("Failed to open Windows registry", e);
}
});

launchOnLoginCheckbox.checked =
WindowsLaunchOnLogin.getLaunchOnLoginEnabled();
let approvedByWindows = WindowsLaunchOnLogin.getLaunchOnLoginApproved();
launchOnLoginCheckbox.disabled = !approvedByWindows;
document.getElementById("windowsLaunchOnLoginDisabledBox").hidden =
Expand Down Expand Up @@ -1635,7 +1634,7 @@ var gMainPane = {
startupPref.value = newValue;
},

onWindowsLaunchOnLoginChange(event) {
async onWindowsLaunchOnLoginChange(event) {
if (AppConstants.platform !== "win") {
return;
}
Expand All @@ -1647,8 +1646,9 @@ var gMainPane = {
true
);
} else {
// windowsLaunchOnLogin has been unchecked: delete registry key
// windowsLaunchOnLogin has been unchecked: delete registry key and shortcut
WindowsLaunchOnLogin.removeLaunchOnLoginRegistryKey();
await WindowsLaunchOnLogin.removeLaunchOnLoginShortcuts();
}
},

Expand Down
23 changes: 23 additions & 0 deletions browser/components/shell/nsIWindowsShellService.idl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ interface nsIWindowsShellService : nsISupports
in AString aDescription, in nsIFile aIconFile, in unsigned short aIconIndex,
in AString aAppUserModelId, in AString aShortcutFolder, in AString aShortcutName);

/*
* Searches the %USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
* folder and returns an array with the path of all shortcuts with a target matching the
* current Firefox install location. The AUMID isn't required here as we are only looking
* for the currently running binary, whether that's firefox.exe or the private browsing
* proxy executable.
*
* It is possible to return an empty array if no shortcuts are found.
*
* @return An array of paths for all launch on login shortcuts.s
*
* @throws NS_ERROR_ABORT
* if instance cannot be created.
* @throws NS_ERROR_FILE_NOT_FOUND
* if %USERPROFILE%\AppData\Roaming\ cannot be opened.
* @throws NS_ERROR_FAILURE
* if the executable file cannot be found.
* @throws NS_ERROR_FILE_UNRECOGNIZED_PATH
* if the executable file cannot be converted into a string.
*/

Array<AString> getLaunchOnLoginShortcuts();

/*
* Pin the current app to the taskbar. If aPrivateBrowsing is true, the
* Private Browsing version of the app (with a different icon and launch
Expand Down
98 changes: 98 additions & 0 deletions browser/components/shell/nsWindowsShellService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include <windows.h>
#include <shellapi.h>
#include <strsafe.h>
#include <propvarutil.h>
#include <propkey.h>

Expand Down Expand Up @@ -911,6 +912,103 @@ nsWindowsShellService::CreateShortcut(
return NS_OK;
}

NS_IMETHODIMP
nsWindowsShellService::GetLaunchOnLoginShortcuts(
nsTArray<nsString>& aShortcutPaths) {
aShortcutPaths.Clear();

// Get AppData\\Roaming folder using a known folder ID
RefPtr<IKnownFolderManager> fManager;
RefPtr<IKnownFolder> roamingAppData;
LPWSTR roamingAppDataW;
nsString roamingAppDataNS;
HRESULT hr =
CoCreateInstance(CLSID_KnownFolderManager, nullptr, CLSCTX_INPROC_SERVER,
IID_IKnownFolderManager, getter_AddRefs(fManager));
if (FAILED(hr)) {
return NS_ERROR_ABORT;
}
fManager->GetFolder(FOLDERID_RoamingAppData,
roamingAppData.StartAssignment());
hr = roamingAppData->GetPath(0, &roamingAppDataW);
if (FAILED(hr)) {
return NS_ERROR_FILE_NOT_FOUND;
}

// Append startup folder to AppData\\Roaming
roamingAppDataNS.Assign(roamingAppDataW);
CoTaskMemFree(roamingAppDataW);
nsString startupFolder =
roamingAppDataNS +
u"\\Microsoft\\Windows\\Start Menu\\Programs\\Startup"_ns;
nsString startupFolderWildcard = startupFolder + u"\\*.lnk"_ns;

// Get known path for binary file for later comparison with shortcuts.
// Returns lowercase file path which should be fine for Windows as all
// directories and files are case-insensitive by default.
RefPtr<nsIFile> binFile;
nsString binPath;
nsresult rv = XRE_GetBinaryPath(binFile.StartAssignment());
if (FAILED(rv)) {
return NS_ERROR_FAILURE;
}
rv = binFile->GetPath(binPath);
if (FAILED(rv)) {
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
}

// Check for if first file exists with a shortcut extension (.lnk)
WIN32_FIND_DATAW ffd;
HANDLE fileHandle = INVALID_HANDLE_VALUE;
fileHandle = FindFirstFileW(startupFolderWildcard.get(), &ffd);
if (fileHandle == INVALID_HANDLE_VALUE) {
// This means that no files were found in the folder which
// doesn't imply an error. Most of the time the user won't
// have any shortcuts here.
return NS_OK;
}

do {
// Extract shortcut target path from every
// shortcut in the startup folder.
nsString fileName(ffd.cFileName);
RefPtr<IShellLinkW> link;
RefPtr<IPersistFile> ppf;
nsString target;
target.SetLength(MAX_PATH);
CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER,
IID_IShellLinkW, getter_AddRefs(link));
hr = link->QueryInterface(IID_IPersistFile, getter_AddRefs(ppf));
if (NS_WARN_IF(FAILED(hr))) {
continue;
}
nsString filePath = startupFolder + u"\\"_ns + fileName;
hr = ppf->Load(filePath.get(), STGM_READ);
if (NS_WARN_IF(FAILED(hr))) {
continue;
}
hr = link->Resolve(nullptr, SLR_NO_UI);
if (NS_WARN_IF(FAILED(hr))) {
continue;
}
hr = link->GetPath(target.get(), MAX_PATH, nullptr, 0);
if (NS_WARN_IF(FAILED(hr))) {
continue;
}

// If shortcut target matches known binary file value
// then add the path to the shortcut as a valid
// startup shortcut. This has to be a substring search as
// the user could have added unknown command line arguments
// to the shortcut.
if (_wcsnicmp(target.get(), binPath.get(), binPath.Length()) == 0) {
aShortcutPaths.AppendElement(filePath);
}
} while (FindNextFile(fileHandle, &ffd) != 0);
FindClose(fileHandle);
return NS_OK;
}

// Look for any installer-created shortcuts in the given location that match
// the given AUMID and EXE Path. If one is found, output its path.
//
Expand Down
56 changes: 56 additions & 0 deletions toolkit/modules/WindowsLaunchOnLogin.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@ export var WindowsLaunchOnLogin = {
}
},

/**
* Gets a list of all launch on login shortcuts in the
* %USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup folder
* that point to the current Firefox executable.
*/
getLaunchOnLoginShortcutList() {
let shellService = Cc["@mozilla.org/browser/shell-service;1"].getService(
Ci.nsIWindowsShellService
);
return shellService.getLaunchOnLoginShortcuts();
},

/**
* Safely removes all launch on login shortcuts in the
* %USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup folder
* that point to the current Firefox executable.
*/
async removeLaunchOnLoginShortcuts() {
let shortcuts = this.getLaunchOnLoginShortcutList();
for (let i = 0; i < shortcuts.length; i++) {
await IOUtils.remove(shortcuts[i]);
}
},

/**
* Checks if Windows launch on login was independently enabled or disabled
* by the user in the Windows Startup Apps menu. The registry key that
Expand Down Expand Up @@ -119,6 +143,38 @@ export var WindowsLaunchOnLogin = {
return true;
},

/**
* Checks if Windows launch on login has an existing registry key or user-created shortcut in
* %USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup. The registry key that
* stores this information should not be modified.
*/
getLaunchOnLoginEnabled() {
let registryName = this.getLaunchOnLoginRegistryName();
let regExists = false;
let shortcutExists = false;
this.withLaunchOnLoginRegistryKey(wrk => {
try {
// Start by checking if registry key exists.
regExists = wrk.hasValue(registryName);
} catch (e) {
// We should only end up here if we fail to open the registry
console.error("Failed to open Windows registry", e);
}
});
if (!regExists) {
shortcutExists = !!this.getLaunchOnLoginShortcutList().length;
}
// Even if a user disables it later on we want the launch on login
// infobar to remain disabled as the user is aware of the option.
if (regExists || shortcutExists) {
Services.prefs.setBoolPref(
"browser.startup.windowsLaunchOnLogin.disableLaunchOnLoginPrompt",
true
);
}
return regExists || shortcutExists;
},

/**
* Quotes a string for use as a single command argument, using Windows quoting
* conventions.
Expand Down

0 comments on commit 954e579

Please sign in to comment.