Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit e151e3d

Browse files
committed
Bug 1722086 - Part 1: Set default browser from WDBA with UserChoice. r=bytesized
Differential Revision: https://phabricator.services.mozilla.com/D121558
1 parent 1742c40 commit e151e3d

2 files changed

Lines changed: 83 additions & 7 deletions

File tree

toolkit/mozapps/defaultagent/Notification.cpp

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "DefaultBrowser.h"
2323
#include "EventLog.h"
2424
#include "Registry.h"
25+
#include "SetDefaultBrowser.h"
2526

2627
#include "wintoastlib.h"
2728

@@ -90,6 +91,14 @@ static ULONGLONG GetFollowupNotificationRequestTime() {
9091
.valueOr(0);
9192
}
9293

94+
// Returns false if no value is set.
95+
static bool GetPrefSetDefaultBrowserUserChoice() {
96+
return RegistryGetValueBool(IsPrefixed::Prefixed,
97+
L"SetDefaultBrowserUserChoice")
98+
.unwrapOr(mozilla::Some(false))
99+
.valueOr(false);
100+
}
101+
93102
struct ToastStrings {
94103
mozilla::UniquePtr<wchar_t[]> text1;
95104
mozilla::UniquePtr<wchar_t[]> text2;
@@ -226,6 +235,35 @@ static bool GetStrings(Strings& strings) {
226235
return true;
227236
}
228237

238+
/*
239+
* Set the default browser.
240+
*
241+
* First check if we can directly write UserChoice, if so attempt that.
242+
* If we can't write UserChoice, or if the attempt fails, fall back to
243+
* showing the Default Apps page of Settings.
244+
*
245+
* @param aAumi The AUMI of the installation to set as default.
246+
*
247+
* @return Success (SUCCEEDED(hr)) if all associations were set with
248+
* UserChoice and checked successfully.
249+
* Other return codes indicate a failure which causes us to
250+
* fall back to Settings, see return codes of
251+
* SetDefaultBrowserUserChoice().
252+
*/
253+
static HRESULT SetDefaultBrowserFromNotification(const wchar_t* aumi) {
254+
// TODO maybe fall back to protocol dialog on Windows 11 (bug 1719832)?
255+
256+
HRESULT hr = E_FAIL;
257+
if (GetPrefSetDefaultBrowserUserChoice()) {
258+
hr = SetDefaultBrowserUserChoice(aumi);
259+
}
260+
261+
if (FAILED(hr)) {
262+
LaunchModernSettingsDialogDefaultApps();
263+
}
264+
return hr;
265+
}
266+
229267
// This encapsulates the data that needs to be protected by a mutex because it
230268
// will be shared by the main thread and the handler thread.
231269
// To ensure the data is only written once, handlerDataHasBeenSet should be
@@ -254,14 +292,15 @@ class ToastHandler : public WinToastLib::IWinToastHandler {
254292
NotificationType mWhichNotification;
255293
bool mIsLocalizedNotification;
256294
HANDLE mEvent;
295+
const std::wstring mAumiStr;
257296

258297
public:
259298
ToastHandler(NotificationType whichNotification, bool isEnglishInstall,
260-
HANDLE event) {
261-
mWhichNotification = whichNotification;
262-
mIsLocalizedNotification = !isEnglishInstall;
263-
mEvent = event;
264-
}
299+
HANDLE event, const wchar_t* aumi)
300+
: mWhichNotification(whichNotification),
301+
mIsLocalizedNotification(!isEnglishInstall),
302+
mEvent(event),
303+
mAumiStr(aumi) {}
265304

266305
void FinishHandler(NotificationActivities& returnData) const {
267306
SetReturnData(returnData);
@@ -305,6 +344,9 @@ class ToastHandler : public WinToastLib::IWinToastHandler {
305344
activitiesPerformed.shown = NotificationShown::Shown;
306345
activitiesPerformed.action = NotificationAction::ToastClicked;
307346

347+
// An activation without clicking a specific button does not clearly
348+
// signal that the default should be changed, so just show the settings
349+
// dialog instead of SetDefaultBrowserFromNotification().
308350
LaunchModernSettingsDialogDefaultApps();
309351

310352
FinishHandler(activitiesPerformed);
@@ -343,7 +385,8 @@ class ToastHandler : public WinToastLib::IWinToastHandler {
343385
// "Make Firefox the default" button, on both the initial and followup
344386
// notifications. "Yes" button on the localized notification.
345387
activitiesPerformed.action = NotificationAction::MakeFirefoxDefaultButton;
346-
LaunchModernSettingsDialogDefaultApps();
388+
389+
SetDefaultBrowserFromNotification(mAumiStr.c_str());
347390
}
348391

349392
FinishHandler(activitiesPerformed);
@@ -481,7 +524,7 @@ static NotificationActivities ShowNotification(
481524
toastTemplate.addAction(toastStrings->action2.get());
482525
toastTemplate.setImagePath(absImagePath.get());
483526
ToastHandler* handler =
484-
new ToastHandler(whichNotification, isEnglishInstall, event.get());
527+
new ToastHandler(whichNotification, isEnglishInstall, event.get(), aumi);
485528
INT64 id = WinToast::instance()->showToast(toastTemplate, handler, &error);
486529
if (id < 0) {
487530
LOG_ERROR_MESSAGE(WinToast::strerror(error).c_str());

toolkit/xre/nsAppRunner.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ static const char kPrefDefaultAgentEnabled[] = "default-browser-agent.enabled";
266266
static const char kPrefServicesSettingsServer[] = "services.settings.server";
267267
static const char kPrefSecurityContentSignatureRootHash[] =
268268
"security.content.signature.root_hash";
269+
static const char kPrefSetDefaultBrowserUserChoicePref[] =
270+
"browser.shell.setDefaultBrowserUserChoice";
269271
#endif // defined(MOZ_DEFAULT_BROWSER_AGENT)
270272

271273
#if defined(XP_WIN)
@@ -2133,6 +2135,32 @@ static void OnDefaultAgentTelemetryPrefChanged(const char* aPref, void* aData) {
21332135
NS_ENSURE_SUCCESS_VOID(rv);
21342136
}
21352137

2138+
static void OnSetDefaultBrowserUserChoicePrefChanged(const char* aPref,
2139+
void* aData) {
2140+
nsresult rv;
2141+
if (strcmp(aPref, kPrefSetDefaultBrowserUserChoicePref) != 0) {
2142+
return;
2143+
}
2144+
nsAutoString valueName;
2145+
valueName.AssignLiteral("SetDefaultBrowserUserChoice");
2146+
rv = PrependRegistryValueName(valueName);
2147+
NS_ENSURE_SUCCESS_VOID(rv);
2148+
2149+
nsCOMPtr<nsIWindowsRegKey> regKey =
2150+
do_CreateInstance("@mozilla.org/windows-registry-key;1", &rv);
2151+
NS_ENSURE_SUCCESS_VOID(rv);
2152+
2153+
nsAutoString keyName;
2154+
keyName.AppendLiteral(DEFAULT_BROWSER_AGENT_KEY_NAME);
2155+
rv = regKey->Create(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER, keyName,
2156+
nsIWindowsRegKey::ACCESS_WRITE);
2157+
2158+
bool prefVal = Preferences::GetBool(aPref, true);
2159+
2160+
rv = regKey->WriteIntValue(valueName, prefVal);
2161+
NS_ENSURE_SUCCESS_VOID(rv);
2162+
}
2163+
21362164
static void OnDefaultAgentRemoteSettingsPrefChanged(const char* aPref,
21372165
void* aData) {
21382166
nsresult rv;
@@ -5119,6 +5147,11 @@ nsresult XREMain::XRE_mainRun() {
51195147
Preferences::RegisterCallbackAndCall(
51205148
&OnDefaultAgentRemoteSettingsPrefChanged,
51215149
kPrefSecurityContentSignatureRootHash);
5150+
5151+
Preferences::RegisterCallbackAndCall(
5152+
&OnSetDefaultBrowserUserChoicePrefChanged,
5153+
kPrefSetDefaultBrowserUserChoicePref);
5154+
51225155
SetDefaultAgentLastRunTime();
51235156
}
51245157
# endif // defined(MOZ_DEFAULT_BROWSER_AGENT)

0 commit comments

Comments
 (0)