1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef ToastNotificationHandler_h__
7 #define ToastNotificationHandler_h__
9 #include <windows.ui.notifications.h>
10 #include <windows.data.xml.dom.h>
13 #include "nsIAlertsService.h"
14 #include "nsICancelable.h"
17 #include "mozilla/Result.h"
22 class ToastNotification
;
24 class ToastNotificationHandler final
25 : public nsIAlertNotificationImageListener
{
28 NS_DECL_NSIALERTNOTIFICATIONIMAGELISTENER
30 ToastNotificationHandler(ToastNotification
* backend
, const nsAString
& aumid
,
31 nsIObserver
* aAlertListener
, const nsAString
& aName
,
32 const nsAString
& aCookie
, const nsAString
& aTitle
,
33 const nsAString
& aMsg
, const nsAString
& aHostPort
,
34 bool aClickable
, bool aRequireInteraction
,
35 const nsTArray
<RefPtr
<nsIAlertAction
>>& aActions
,
36 bool aIsSystemPrincipal
,
37 const nsAString
& aOpaqueRelaunchData
,
38 bool aInPrivateBrowsing
, bool aIsSilent
)
42 mAlertListener(aAlertListener
),
48 mClickable(aClickable
),
49 mRequireInteraction(aRequireInteraction
),
50 mInPrivateBrowsing(aInPrivateBrowsing
),
51 mActions(aActions
.Clone()),
52 mIsSystemPrincipal(aIsSystemPrincipal
),
53 mOpaqueRelaunchData(aOpaqueRelaunchData
),
55 mSentFinished(!aAlertListener
) {}
57 nsresult
InitAlertAsync(nsIAlertNotification
* aAlert
);
59 void OnWriteImageFinished(nsresult rv
);
64 void UnregisterHandler();
66 nsString
ActionArgsJSONString(
67 const nsString
& aAction
,
68 const nsString
& aOpaqueRelaunchData
/* = u""_ns */);
69 nsresult
CreateToastXmlString(const nsAString
& aImageURL
, nsAString
& aString
);
71 nsresult
GetWindowsTag(nsAString
& aWindowsTag
);
72 nsresult
SetWindowsTag(const nsAString
& aWindowsTag
);
74 // Exposed for consumption by `ToastNotification.cpp`.
75 static nsresult
FindNotificationDataForWindowsTag(
76 const nsAString
& aWindowsTag
, const nsAString
& aAumid
, bool& aFoundTag
,
77 nsAString
& aNotificationData
);
80 virtual ~ToastNotificationHandler();
82 using IXmlDocument
= ABI::Windows::Data::Xml::Dom::IXmlDocument
;
83 using IToastNotifier
= ABI::Windows::UI::Notifications::IToastNotifier
;
84 using IToastNotification
=
85 ABI::Windows::UI::Notifications::IToastNotification
;
86 using IToastDismissedEventArgs
=
87 ABI::Windows::UI::Notifications::IToastDismissedEventArgs
;
88 using IToastFailedEventArgs
=
89 ABI::Windows::UI::Notifications::IToastFailedEventArgs
;
90 using ToastTemplateType
= ABI::Windows::UI::Notifications::ToastTemplateType
;
92 using ComPtr
= Microsoft::WRL::ComPtr
<T
>;
94 Result
<nsString
, nsresult
> GetLaunchArgument();
96 ComPtr
<IToastNotification
> mNotification
;
97 ComPtr
<IToastNotifier
> mNotifier
;
99 RefPtr
<ToastNotification
> mBackend
;
102 nsString mWindowsTag
;
104 nsCOMPtr
<nsICancelable
> mImageRequest
;
105 nsCOMPtr
<nsIFile
> mImageFile
;
109 EventRegistrationToken mActivatedToken
;
110 EventRegistrationToken mDismissedToken
;
111 EventRegistrationToken mFailedToken
;
113 nsCOMPtr
<nsIObserver
> mAlertListener
;
120 bool mRequireInteraction
;
121 bool mInPrivateBrowsing
;
122 nsTArray
<RefPtr
<nsIAlertAction
>> mActions
;
123 bool mIsSystemPrincipal
;
124 nsString mOpaqueRelaunchData
;
128 nsresult
TryShowAlert();
130 nsresult
AsyncSaveImage(imgIRequest
* aRequest
);
131 nsresult
OnWriteImageSuccess();
134 nsresult
InitWindowsTag();
135 bool CreateWindowsNotificationFromXml(ComPtr
<IXmlDocument
>& aToastXml
);
136 ComPtr
<IXmlDocument
> CreateToastXmlDocument();
138 HRESULT
OnActivate(const ComPtr
<IToastNotification
>& notification
,
139 const ComPtr
<IInspectable
>& inspectable
);
140 HRESULT
OnDismiss(const ComPtr
<IToastNotification
>& notification
,
141 const ComPtr
<IToastDismissedEventArgs
>& aArgs
);
142 HRESULT
OnFail(const ComPtr
<IToastNotification
>& notification
,
143 const ComPtr
<IToastFailedEventArgs
>& aArgs
);
145 static ComPtr
<IToastNotification
> FindNotificationByTag(
146 const nsAString
& aWindowsTag
, const nsAString
& aAumid
);
149 } // namespace widget
150 } // namespace mozilla