|
22 | 22 | #include "DefaultBrowser.h" |
23 | 23 | #include "EventLog.h" |
24 | 24 | #include "Registry.h" |
| 25 | +#include "SetDefaultBrowser.h" |
25 | 26 |
|
26 | 27 | #include "wintoastlib.h" |
27 | 28 |
|
@@ -90,6 +91,14 @@ static ULONGLONG GetFollowupNotificationRequestTime() { |
90 | 91 | .valueOr(0); |
91 | 92 | } |
92 | 93 |
|
| 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 | + |
93 | 102 | struct ToastStrings { |
94 | 103 | mozilla::UniquePtr<wchar_t[]> text1; |
95 | 104 | mozilla::UniquePtr<wchar_t[]> text2; |
@@ -226,6 +235,35 @@ static bool GetStrings(Strings& strings) { |
226 | 235 | return true; |
227 | 236 | } |
228 | 237 |
|
| 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 | + |
229 | 267 | // This encapsulates the data that needs to be protected by a mutex because it |
230 | 268 | // will be shared by the main thread and the handler thread. |
231 | 269 | // To ensure the data is only written once, handlerDataHasBeenSet should be |
@@ -254,14 +292,15 @@ class ToastHandler : public WinToastLib::IWinToastHandler { |
254 | 292 | NotificationType mWhichNotification; |
255 | 293 | bool mIsLocalizedNotification; |
256 | 294 | HANDLE mEvent; |
| 295 | + const std::wstring mAumiStr; |
257 | 296 |
|
258 | 297 | public: |
259 | 298 | 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) {} |
265 | 304 |
|
266 | 305 | void FinishHandler(NotificationActivities& returnData) const { |
267 | 306 | SetReturnData(returnData); |
@@ -305,6 +344,9 @@ class ToastHandler : public WinToastLib::IWinToastHandler { |
305 | 344 | activitiesPerformed.shown = NotificationShown::Shown; |
306 | 345 | activitiesPerformed.action = NotificationAction::ToastClicked; |
307 | 346 |
|
| 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(). |
308 | 350 | LaunchModernSettingsDialogDefaultApps(); |
309 | 351 |
|
310 | 352 | FinishHandler(activitiesPerformed); |
@@ -343,7 +385,8 @@ class ToastHandler : public WinToastLib::IWinToastHandler { |
343 | 385 | // "Make Firefox the default" button, on both the initial and followup |
344 | 386 | // notifications. "Yes" button on the localized notification. |
345 | 387 | activitiesPerformed.action = NotificationAction::MakeFirefoxDefaultButton; |
346 | | - LaunchModernSettingsDialogDefaultApps(); |
| 388 | + |
| 389 | + SetDefaultBrowserFromNotification(mAumiStr.c_str()); |
347 | 390 | } |
348 | 391 |
|
349 | 392 | FinishHandler(activitiesPerformed); |
@@ -481,7 +524,7 @@ static NotificationActivities ShowNotification( |
481 | 524 | toastTemplate.addAction(toastStrings->action2.get()); |
482 | 525 | toastTemplate.setImagePath(absImagePath.get()); |
483 | 526 | ToastHandler* handler = |
484 | | - new ToastHandler(whichNotification, isEnglishInstall, event.get()); |
| 527 | + new ToastHandler(whichNotification, isEnglishInstall, event.get(), aumi); |
485 | 528 | INT64 id = WinToast::instance()->showToast(toastTemplate, handler, &error); |
486 | 529 | if (id < 0) { |
487 | 530 | LOG_ERROR_MESSAGE(WinToast::strerror(error).c_str()); |
|
0 commit comments