commit eaa8ef33136519fd77b0da00624c06fadc624fff
parent 2e66ab5e9ba19722090f298cc1c9c290c985e274
Author: Kagami Sascha Rosylight <krosylight@proton.me>
Date: Sat, 29 Nov 2025 18:32:22 +0000
Bug 1369833 - Part 1: Add a way to init with an object r=nalexander,tschuster
Differential Revision: https://phabricator.services.mozilla.com/D235830
Diffstat:
3 files changed, 52 insertions(+), 11 deletions(-)
diff --git a/toolkit/components/alerts/AlertNotification.cpp b/toolkit/components/alerts/AlertNotification.cpp
@@ -22,11 +22,6 @@ namespace mozilla {
NS_IMPL_ISUPPORTS(AlertNotification, nsIAlertNotification)
-AlertNotification::AlertNotification()
- : mTextClickable(false), mInPrivateBrowsing(false) {}
-
-AlertNotification::~AlertNotification() = default;
-
NS_IMETHODIMP
AlertNotification::Init(const nsAString& aName, const nsAString& aImageURL,
const nsAString& aTitle, const nsAString& aText,
@@ -57,6 +52,36 @@ AlertNotification::Init(const nsAString& aName, const nsAString& aImageURL,
return InitId();
}
+NS_IMETHODIMP
+AlertNotification::InitWithObject(nsIAlertNotification* aAlertNotification) {
+ MOZ_TRY(aAlertNotification->GetName(mName));
+ MOZ_TRY(aAlertNotification->GetImageURL(mImageURL));
+ MOZ_TRY(aAlertNotification->GetTitle(mTitle));
+ MOZ_TRY(aAlertNotification->GetText(mText));
+ MOZ_TRY(aAlertNotification->GetTextClickable(&mTextClickable));
+ MOZ_TRY(aAlertNotification->GetCookie(mCookie));
+ MOZ_TRY(aAlertNotification->GetDir(mDir));
+ MOZ_TRY(aAlertNotification->GetLang(mLang));
+ MOZ_TRY(aAlertNotification->GetData(mData));
+ MOZ_TRY(aAlertNotification->GetPrincipal(getter_AddRefs(mPrincipal)));
+ MOZ_TRY(aAlertNotification->GetInPrivateBrowsing(&mInPrivateBrowsing));
+ MOZ_TRY(aAlertNotification->GetRequireInteraction(&mRequireInteraction));
+ MOZ_TRY(aAlertNotification->GetSilent(&mSilent));
+ if (NS_FAILED(aAlertNotification->GetVibrate(mVibrate))) {
+ mVibrate.Clear();
+ };
+ nsTArray<RefPtr<nsIAlertAction>> actions;
+ if (NS_SUCCEEDED(aAlertNotification->GetActions(actions))) {
+ for (auto& action : actions) {
+ if (RefPtr<nsIAlertAction> copied =
+ AlertAction::Copy(*action).unwrapOr(nullptr)) {
+ mActions.AppendElement(copied);
+ }
+ }
+ };
+ return InitId();
+}
+
nsresult AlertNotification::InitId() {
nsAutoString id;
@@ -464,6 +489,15 @@ NS_IMPL_ISUPPORTS(AlertAction, nsIAlertAction)
AlertAction::AlertAction(const nsAString& aAction, const nsAString& aTitle)
: mAction(aAction), mTitle(aTitle) {}
+Result<already_AddRefed<AlertAction>, nsresult> AlertAction::Copy(
+ nsIAlertAction& aAction) {
+ nsAutoString action;
+ nsAutoString title;
+ MOZ_TRY(aAction.GetAction(action));
+ MOZ_TRY(aAction.GetTitle(title));
+ return do_AddRef(new AlertAction(action, title));
+}
+
NS_IMETHODIMP
AlertAction::GetAction(nsAString& aAction) {
aAction = mAction;
diff --git a/toolkit/components/alerts/AlertNotification.h b/toolkit/components/alerts/AlertNotification.h
@@ -57,10 +57,9 @@ class AlertNotification : public nsIAlertNotification {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIALERTNOTIFICATION
- AlertNotification();
protected:
- virtual ~AlertNotification();
+ virtual ~AlertNotification() = default;
private:
nsresult InitId();
@@ -70,15 +69,15 @@ class AlertNotification : public nsIAlertNotification {
nsString mImageURL;
nsString mTitle;
nsString mText;
- bool mTextClickable;
+ bool mTextClickable = false;
nsString mCookie;
nsString mDir;
nsString mLang;
- bool mRequireInteraction;
+ bool mRequireInteraction = false;
nsString mData;
nsCOMPtr<nsIPrincipal> mPrincipal;
- bool mInPrivateBrowsing;
- bool mSilent;
+ bool mInPrivateBrowsing = false;
+ bool mSilent = false;
nsTArray<uint32_t> mVibrate;
nsTArray<RefPtr<nsIAlertAction>> mActions;
nsString mOpaqueRelaunchData;
@@ -89,6 +88,8 @@ class AlertAction : public nsIAlertAction {
NS_DECL_NSIALERTACTION
AlertAction(const nsAString& aAction, const nsAString& aTitle);
+ static Result<already_AddRefed<AlertAction>, nsresult> Copy(
+ nsIAlertAction& aAction);
protected:
virtual ~AlertAction() = default;
diff --git a/toolkit/components/alerts/nsIAlertsService.idl b/toolkit/components/alerts/nsIAlertsService.idl
@@ -106,6 +106,12 @@ interface nsIAlertNotification : nsISupports
[optional] in Array<uint32_t> aVibrate);
/**
+ * Initializes an alert notification with another instance.
+ * Can be useful in JS as it can provide a dictionary object.
+ */
+ void initWithObject(in nsIAlertNotification aAlertNotification);
+
+ /**
* The unique ID of the notification, based on the profile path and the caller
* given name.
*