Bug 1826326 [wpt PR 39360] - Get rid of ResourceWarnings produced by the tests, a...
[gecko.git] / widget / windows / ToastNotificationHandler.h
blobe9f1f8d79c350cd7731f6bfb25d7ca3134f8594a
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, const nsAString& aLaunchUrl,
37 bool aInPrivateBrowsing, bool aIsSilent)
38 : mBackend(backend),
39 mAumid(aumid),
40 mHasImage(false),
41 mAlertListener(aAlertListener),
42 mName(aName),
43 mCookie(aCookie),
44 mTitle(aTitle),
45 mMsg(aMsg),
46 mHostPort(aHostPort),
47 mClickable(aClickable),
48 mRequireInteraction(aRequireInteraction),
49 mInPrivateBrowsing(aInPrivateBrowsing),
50 mActions(aActions.Clone()),
51 mIsSystemPrincipal(aIsSystemPrincipal),
52 mLaunchUrl(aLaunchUrl),
53 mIsSilent(aIsSilent),
54 mSentFinished(!aAlertListener) {}
56 nsresult InitAlertAsync(nsIAlertNotification* aAlert);
58 void OnWriteImageFinished(nsresult rv);
60 void HideAlert();
61 bool IsPrivate();
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);
75 protected:
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;
87 template <typename T>
88 using ComPtr = Microsoft::WRL::ComPtr<T>;
90 Result<nsString, nsresult> GetLaunchArgument();
92 ComPtr<IToastNotification> mNotification;
93 ComPtr<IToastNotifier> mNotifier;
95 RefPtr<ToastNotification> mBackend;
97 nsString mAumid;
98 nsString mWindowsTag;
100 nsCOMPtr<nsICancelable> mImageRequest;
101 nsCOMPtr<nsIFile> mImageFile;
102 nsString mImageUri;
103 bool mHasImage;
105 EventRegistrationToken mActivatedToken;
106 EventRegistrationToken mDismissedToken;
107 EventRegistrationToken mFailedToken;
109 nsCOMPtr<nsIObserver> mAlertListener;
110 nsString mName;
111 nsString mCookie;
112 nsString mTitle;
113 nsString mMsg;
114 nsString mHostPort;
115 bool mClickable;
116 bool mRequireInteraction;
117 bool mInPrivateBrowsing;
118 nsTArray<RefPtr<nsIAlertAction>> mActions;
119 bool mIsSystemPrincipal;
120 nsString mLaunchUrl;
121 bool mIsSilent;
122 bool mSentFinished;
124 nsresult TryShowAlert();
125 bool ShowAlert();
126 nsresult AsyncSaveImage(imgIRequest* aRequest);
127 nsresult OnWriteImageSuccess();
128 void SendFinished();
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,
143 nsAString& value);
144 static ComPtr<IToastNotification> FindNotificationByTag(
145 const nsAString& aWindowsTag, const nsAString& aAumid);
148 } // namespace widget
149 } // namespace mozilla
151 #endif