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 "nsICancelable.h"
15 #include "nsIWindowsAlertsService.h"
17 #include "mozilla/Result.h"
22 enum class ImagePlacement
{
28 class ToastNotification
;
30 class ToastNotificationHandler final
31 : public nsIAlertNotificationImageListener
{
34 NS_DECL_NSIALERTNOTIFICATIONIMAGELISTENER
36 ToastNotificationHandler(
37 ToastNotification
* backend
, const nsAString
& aumid
,
38 nsIObserver
* aAlertListener
, const nsAString
& aName
,
39 const nsAString
& aCookie
, const nsAString
& aTitle
, const nsAString
& aMsg
,
40 const nsAString
& aHostPort
, bool aClickable
, bool aRequireInteraction
,
41 const nsTArray
<RefPtr
<nsIAlertAction
>>& aActions
, bool aIsSystemPrincipal
,
42 const nsAString
& aOpaqueRelaunchData
, bool aInPrivateBrowsing
,
43 bool aIsSilent
, bool aHandlesActions
= false,
44 ImagePlacement aImagePlacement
= ImagePlacement::eInline
)
48 mAlertListener(aAlertListener
),
54 mClickable(aClickable
),
55 mRequireInteraction(aRequireInteraction
),
56 mInPrivateBrowsing(aInPrivateBrowsing
),
57 mActions(aActions
.Clone()),
58 mIsSystemPrincipal(aIsSystemPrincipal
),
59 mOpaqueRelaunchData(aOpaqueRelaunchData
),
61 mSentFinished(!aAlertListener
),
62 mHandleActions(aHandlesActions
),
63 mImagePlacement(aImagePlacement
) {}
65 nsresult
InitAlertAsync(nsIAlertNotification
* aAlert
);
67 void OnWriteImageFinished(nsresult rv
);
72 void UnregisterHandler();
74 nsString
ActionArgsJSONString(
75 const nsString
& aAction
,
76 const nsString
& aOpaqueRelaunchData
/* = u""_ns */);
77 nsresult
CreateToastXmlString(const nsAString
& aImageURL
, nsAString
& aString
);
79 nsresult
GetWindowsTag(nsAString
& aWindowsTag
);
80 nsresult
SetWindowsTag(const nsAString
& aWindowsTag
);
82 // Exposed for consumption by `ToastNotification.cpp`.
83 static nsresult
FindNotificationDataForWindowsTag(
84 const nsAString
& aWindowsTag
, const nsAString
& aAumid
, bool& aFoundTag
,
85 nsAString
& aNotificationData
);
88 virtual ~ToastNotificationHandler();
90 using IXmlDocument
= ABI::Windows::Data::Xml::Dom::IXmlDocument
;
91 using IToastNotifier
= ABI::Windows::UI::Notifications::IToastNotifier
;
92 using IToastNotification
=
93 ABI::Windows::UI::Notifications::IToastNotification
;
94 using IToastDismissedEventArgs
=
95 ABI::Windows::UI::Notifications::IToastDismissedEventArgs
;
96 using IToastFailedEventArgs
=
97 ABI::Windows::UI::Notifications::IToastFailedEventArgs
;
98 using ToastTemplateType
= ABI::Windows::UI::Notifications::ToastTemplateType
;
100 using ComPtr
= Microsoft::WRL::ComPtr
<T
>;
102 Result
<nsString
, nsresult
> GetLaunchArgument();
104 ComPtr
<IToastNotification
> mNotification
;
105 ComPtr
<IToastNotifier
> mNotifier
;
107 RefPtr
<ToastNotification
> mBackend
;
110 nsString mWindowsTag
;
112 nsCOMPtr
<nsICancelable
> mImageRequest
;
113 nsCOMPtr
<nsIFile
> mImageFile
;
117 EventRegistrationToken mActivatedToken
;
118 EventRegistrationToken mDismissedToken
;
119 EventRegistrationToken mFailedToken
;
121 nsCOMPtr
<nsIObserver
> mAlertListener
;
128 bool mRequireInteraction
;
129 bool mInPrivateBrowsing
;
130 nsTArray
<RefPtr
<nsIAlertAction
>> mActions
;
131 bool mIsSystemPrincipal
;
132 nsString mOpaqueRelaunchData
;
136 ImagePlacement mImagePlacement
;
138 nsresult
TryShowAlert();
140 nsresult
AsyncSaveImage(imgIRequest
* aRequest
);
141 nsresult
OnWriteImageSuccess();
144 nsresult
InitWindowsTag();
145 bool CreateWindowsNotificationFromXml(ComPtr
<IXmlDocument
>& aToastXml
);
146 ComPtr
<IXmlDocument
> CreateToastXmlDocument();
148 HRESULT
OnActivate(const ComPtr
<IToastNotification
>& notification
,
149 const ComPtr
<IInspectable
>& inspectable
);
150 HRESULT
OnDismiss(const ComPtr
<IToastNotification
>& notification
,
151 const ComPtr
<IToastDismissedEventArgs
>& aArgs
);
152 HRESULT
OnFail(const ComPtr
<IToastNotification
>& notification
,
153 const ComPtr
<IToastFailedEventArgs
>& aArgs
);
155 static ComPtr
<IToastNotification
> FindNotificationByTag(
156 const nsAString
& aWindowsTag
, const nsAString
& aAumid
);
159 } // namespace widget
160 } // namespace mozilla