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
, const nsAString
& aLaunchUrl
,
37 bool aInPrivateBrowsing
, bool aIsSilent
)
41 mAlertListener(aAlertListener
),
47 mClickable(aClickable
),
48 mRequireInteraction(aRequireInteraction
),
49 mInPrivateBrowsing(aInPrivateBrowsing
),
50 mActions(aActions
.Clone()),
51 mIsSystemPrincipal(aIsSystemPrincipal
),
52 mLaunchUrl(aLaunchUrl
),
54 mSentFinished(!aAlertListener
) {}
56 nsresult
InitAlertAsync(nsIAlertNotification
* aAlert
);
58 void OnWriteImageFinished(nsresult rv
);
63 void UnregisterHandler();
65 nsresult
CreateToastXmlString(const nsAString
& aImageURL
, nsAString
& aString
);
67 nsresult
GetWindowsTag(nsAString
& aWindowsTag
);
68 nsresult
SetWindowsTag(const nsAString
& aWindowsTag
);
70 // Exposed for consumption by `ToastNotification.cpp`.
71 static nsresult
FindLaunchURLAndPrivilegedNameForWindowsTag(
72 const nsAString
& aWindowsTag
, const nsAString
& aAumid
, bool& aFoundTag
,
73 nsAString
& aLaunchUrl
, nsAString
& aPrivilegedName
);
76 virtual ~ToastNotificationHandler();
78 using IXmlDocument
= ABI::Windows::Data::Xml::Dom::IXmlDocument
;
79 using IToastNotifier
= ABI::Windows::UI::Notifications::IToastNotifier
;
80 using IToastNotification
=
81 ABI::Windows::UI::Notifications::IToastNotification
;
82 using IToastDismissedEventArgs
=
83 ABI::Windows::UI::Notifications::IToastDismissedEventArgs
;
84 using IToastFailedEventArgs
=
85 ABI::Windows::UI::Notifications::IToastFailedEventArgs
;
86 using ToastTemplateType
= ABI::Windows::UI::Notifications::ToastTemplateType
;
88 using ComPtr
= Microsoft::WRL::ComPtr
<T
>;
90 Result
<nsString
, nsresult
> GetLaunchArgument();
92 ComPtr
<IToastNotification
> mNotification
;
93 ComPtr
<IToastNotifier
> mNotifier
;
95 RefPtr
<ToastNotification
> mBackend
;
100 nsCOMPtr
<nsICancelable
> mImageRequest
;
101 nsCOMPtr
<nsIFile
> mImageFile
;
105 EventRegistrationToken mActivatedToken
;
106 EventRegistrationToken mDismissedToken
;
107 EventRegistrationToken mFailedToken
;
109 nsCOMPtr
<nsIObserver
> mAlertListener
;
116 bool mRequireInteraction
;
117 bool mInPrivateBrowsing
;
118 nsTArray
<RefPtr
<nsIAlertAction
>> mActions
;
119 bool mIsSystemPrincipal
;
124 nsresult
TryShowAlert();
126 nsresult
AsyncSaveImage(imgIRequest
* aRequest
);
127 nsresult
OnWriteImageSuccess();
130 nsresult
InitWindowsTag();
131 bool CreateWindowsNotificationFromXml(ComPtr
<IXmlDocument
>& aToastXml
);
132 ComPtr
<IXmlDocument
> CreateToastXmlDocument();
134 HRESULT
OnActivate(const ComPtr
<IToastNotification
>& notification
,
135 const ComPtr
<IInspectable
>& inspectable
);
136 HRESULT
OnDismiss(const ComPtr
<IToastNotification
>& notification
,
137 const ComPtr
<IToastDismissedEventArgs
>& aArgs
);
138 HRESULT
OnFail(const ComPtr
<IToastNotification
>& notification
,
139 const ComPtr
<IToastFailedEventArgs
>& aArgs
);
141 static HRESULT
GetLaunchArgumentValueForKey(
142 const ComPtr
<IToastNotification
> toast
, const nsAString
& key
,
144 static ComPtr
<IToastNotification
> FindNotificationByTag(
145 const nsAString
& aWindowsTag
, const nsAString
& aAumid
);
148 } // namespace widget
149 } // namespace mozilla