Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / widget / windows / ToastNotificationHandler.h
blobb3be34709cc9b021024b64fa8df003a24edd2cf0
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 "nsICancelable.h"
14 #include "nsIFile.h"
15 #include "nsIWindowsAlertsService.h"
16 #include "nsString.h"
17 #include "mozilla/Result.h"
19 namespace mozilla {
20 namespace widget {
22 enum class ImagePlacement {
23 eInline,
24 eHero,
25 eIcon,
28 class ToastNotification;
30 class ToastNotificationHandler final
31 : public nsIAlertNotificationImageListener {
32 public:
33 NS_DECL_ISUPPORTS
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)
45 : mBackend(backend),
46 mAumid(aumid),
47 mHasImage(false),
48 mAlertListener(aAlertListener),
49 mName(aName),
50 mCookie(aCookie),
51 mTitle(aTitle),
52 mMsg(aMsg),
53 mHostPort(aHostPort),
54 mClickable(aClickable),
55 mRequireInteraction(aRequireInteraction),
56 mInPrivateBrowsing(aInPrivateBrowsing),
57 mActions(aActions.Clone()),
58 mIsSystemPrincipal(aIsSystemPrincipal),
59 mOpaqueRelaunchData(aOpaqueRelaunchData),
60 mIsSilent(aIsSilent),
61 mSentFinished(!aAlertListener),
62 mHandleActions(aHandlesActions),
63 mImagePlacement(aImagePlacement) {}
65 nsresult InitAlertAsync(nsIAlertNotification* aAlert);
67 void OnWriteImageFinished(nsresult rv);
69 void HideAlert();
70 bool IsPrivate();
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);
87 protected:
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;
99 template <typename T>
100 using ComPtr = Microsoft::WRL::ComPtr<T>;
102 Result<nsString, nsresult> GetLaunchArgument();
104 ComPtr<IToastNotification> mNotification;
105 ComPtr<IToastNotifier> mNotifier;
107 RefPtr<ToastNotification> mBackend;
109 nsString mAumid;
110 nsString mWindowsTag;
112 nsCOMPtr<nsICancelable> mImageRequest;
113 nsCOMPtr<nsIFile> mImageFile;
114 nsString mImageUri;
115 bool mHasImage;
117 EventRegistrationToken mActivatedToken;
118 EventRegistrationToken mDismissedToken;
119 EventRegistrationToken mFailedToken;
121 nsCOMPtr<nsIObserver> mAlertListener;
122 nsString mName;
123 nsString mCookie;
124 nsString mTitle;
125 nsString mMsg;
126 nsString mHostPort;
127 bool mClickable;
128 bool mRequireInteraction;
129 bool mInPrivateBrowsing;
130 nsTArray<RefPtr<nsIAlertAction>> mActions;
131 bool mIsSystemPrincipal;
132 nsString mOpaqueRelaunchData;
133 bool mIsSilent;
134 bool mSentFinished;
135 bool mHandleActions;
136 ImagePlacement mImagePlacement;
138 nsresult TryShowAlert();
139 bool ShowAlert();
140 nsresult AsyncSaveImage(imgIRequest* aRequest);
141 nsresult OnWriteImageSuccess();
142 void SendFinished();
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
162 #endif