Bug 1861709 replace AudioCallbackDriver::ThreadRunning() assertions that mean to...
[gecko.git] / widget / windows / ToastNotificationHandler.h
blob76c7305c027a8865e7522c031aedd5726708a853
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>
11 #include <wrl.h>
12 #include "nsCOMPtr.h"
13 #include "nsIAlertsService.h"
14 #include "nsICancelable.h"
15 #include "nsIFile.h"
16 #include "nsString.h"
17 #include "mozilla/Result.h"
19 namespace mozilla {
20 namespace widget {
22 class ToastNotification;
24 class ToastNotificationHandler final
25 : public nsIAlertNotificationImageListener {
26 public:
27 NS_DECL_ISUPPORTS
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)
39 : mBackend(backend),
40 mAumid(aumid),
41 mHasImage(false),
42 mAlertListener(aAlertListener),
43 mName(aName),
44 mCookie(aCookie),
45 mTitle(aTitle),
46 mMsg(aMsg),
47 mHostPort(aHostPort),
48 mClickable(aClickable),
49 mRequireInteraction(aRequireInteraction),
50 mInPrivateBrowsing(aInPrivateBrowsing),
51 mActions(aActions.Clone()),
52 mIsSystemPrincipal(aIsSystemPrincipal),
53 mOpaqueRelaunchData(aOpaqueRelaunchData),
54 mIsSilent(aIsSilent),
55 mSentFinished(!aAlertListener) {}
57 nsresult InitAlertAsync(nsIAlertNotification* aAlert);
59 void OnWriteImageFinished(nsresult rv);
61 void HideAlert();
62 bool IsPrivate();
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);
79 protected:
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;
91 template <typename T>
92 using ComPtr = Microsoft::WRL::ComPtr<T>;
94 Result<nsString, nsresult> GetLaunchArgument();
96 ComPtr<IToastNotification> mNotification;
97 ComPtr<IToastNotifier> mNotifier;
99 RefPtr<ToastNotification> mBackend;
101 nsString mAumid;
102 nsString mWindowsTag;
104 nsCOMPtr<nsICancelable> mImageRequest;
105 nsCOMPtr<nsIFile> mImageFile;
106 nsString mImageUri;
107 bool mHasImage;
109 EventRegistrationToken mActivatedToken;
110 EventRegistrationToken mDismissedToken;
111 EventRegistrationToken mFailedToken;
113 nsCOMPtr<nsIObserver> mAlertListener;
114 nsString mName;
115 nsString mCookie;
116 nsString mTitle;
117 nsString mMsg;
118 nsString mHostPort;
119 bool mClickable;
120 bool mRequireInteraction;
121 bool mInPrivateBrowsing;
122 nsTArray<RefPtr<nsIAlertAction>> mActions;
123 bool mIsSystemPrincipal;
124 nsString mOpaqueRelaunchData;
125 bool mIsSilent;
126 bool mSentFinished;
128 nsresult TryShowAlert();
129 bool ShowAlert();
130 nsresult AsyncSaveImage(imgIRequest* aRequest);
131 nsresult OnWriteImageSuccess();
132 void SendFinished();
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
152 #endif