diff --git a/widget/windows/winrt/ToastNotificationHandler.cpp b/widget/windows/winrt/ToastNotificationHandler.cpp index 3e4615949d24..8e4b0ec724ff 100644 --- a/widget/windows/winrt/ToastNotificationHandler.cpp +++ b/widget/windows/winrt/ToastNotificationHandler.cpp @@ -18,7 +18,7 @@ using namespace ABI::Windows::UI::Notifications; typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_IInspectable_t ToastActivationHandler; typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_Windows__CUI__CNotifications__CToastDismissedEventArgs ToastDismissHandler; -void +bool ToastNotificationHandler::DisplayNotification(HSTRING title, HSTRING msg, HSTRING imagePath, @@ -39,22 +39,22 @@ ToastNotificationHandler::DisplayNotification(HSTRING title, toastXml->GetElementsByTagName(textNodeStr, &toastTextElements); toastXml->GetElementsByTagName(imageNodeStr, &toastImageElements); - AssertHRESULT(toastTextElements->Item(0, &titleTextNodeRoot)); - AssertHRESULT(toastTextElements->Item(1, &msgTextNodeRoot)); - AssertHRESULT(toastImageElements->Item(0, &imageNodeRoot)); + AssertRetHRESULT(toastTextElements->Item(0, &titleTextNodeRoot), false); + AssertRetHRESULT(toastTextElements->Item(1, &msgTextNodeRoot), false); + AssertRetHRESULT(toastImageElements->Item(0, &imageNodeRoot), false); ComPtr attributes; - AssertHRESULT(imageNodeRoot->get_Attributes(&attributes)); - AssertHRESULT(attributes->GetNamedItem(srcNodeStr, &srcAttribute)); + AssertRetHRESULT(imageNodeRoot->get_Attributes(&attributes), false); + AssertRetHRESULT(attributes->GetNamedItem(srcNodeStr, &srcAttribute), false); SetNodeValueString(title, titleTextNodeRoot.Get(), toastXml.Get()); SetNodeValueString(msg, msgTextNodeRoot.Get(), toastXml.Get()); SetNodeValueString(imagePath, srcAttribute.Get(), toastXml.Get()); - CreateWindowsNotificationFromXml(toastXml.Get(), aAppId); + return CreateWindowsNotificationFromXml(toastXml.Get(), aAppId); } -void +bool ToastNotificationHandler::DisplayTextNotification(HSTRING title, HSTRING msg, const nsAString& aCookie, @@ -71,13 +71,13 @@ ToastNotificationHandler::DisplayTextNotification(HSTRING title, WindowsCreateStringReference(L"text", 4, &textHeader, &textNodeStr); toastXml->GetElementsByTagName(textNodeStr, &toastTextElements); - AssertHRESULT(toastTextElements->Item(0, &titleTextNodeRoot)); - AssertHRESULT(toastTextElements->Item(1, &msgTextNodeRoot)); + AssertRetHRESULT(toastTextElements->Item(0, &titleTextNodeRoot), false); + AssertRetHRESULT(toastTextElements->Item(1, &msgTextNodeRoot), false); SetNodeValueString(title, titleTextNodeRoot.Get(), toastXml.Get()); SetNodeValueString(msg, msgTextNodeRoot.Get(), toastXml.Get()); - CreateWindowsNotificationFromXml(toastXml.Get(), aAppId); + return CreateWindowsNotificationFromXml(toastXml.Get(), aAppId); } ComPtr @@ -92,35 +92,38 @@ ToastNotificationHandler::InitializeXmlForTemplate(ToastTemplateType templateTyp return toastXml; } -void +bool ToastNotificationHandler::CreateWindowsNotificationFromXml(IXmlDocument *toastXml, const nsAString& aAppId) { ComPtr notification; ComPtr factory; - AssertHRESULT(GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotification).Get(), - factory.GetAddressOf())); - AssertHRESULT(factory->CreateToastNotification(toastXml, ¬ification)); + AssertRetHRESULT(GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotification).Get(), + factory.GetAddressOf()), false); + AssertRetHRESULT(factory->CreateToastNotification(toastXml, ¬ification), + false); EventRegistrationToken activatedToken; - AssertHRESULT(notification->add_Activated(Callback(this, - &ToastNotificationHandler::OnActivate).Get(), &activatedToken)); + AssertRetHRESULT(notification->add_Activated(Callback(this, + &ToastNotificationHandler::OnActivate).Get(), &activatedToken), false); EventRegistrationToken dismissedToken; - AssertHRESULT(notification->add_Dismissed(Callback(this, - &ToastNotificationHandler::OnDismiss).Get(), &dismissedToken)); + AssertRetHRESULT(notification->add_Dismissed(Callback(this, + &ToastNotificationHandler::OnDismiss).Get(), &dismissedToken), false); ComPtr notifier; if (aAppId.IsEmpty()) { - AssertHRESULT(mToastNotificationManagerStatics->CreateToastNotifier( - ¬ifier)); + AssertRetHRESULT(mToastNotificationManagerStatics->CreateToastNotifier( + ¬ifier), false); } else { - AssertHRESULT(mToastNotificationManagerStatics->CreateToastNotifierWithId( + AssertRetHRESULT(mToastNotificationManagerStatics->CreateToastNotifierWithId( HStringReference(PromiseFlatString(aAppId).get()).Get(), - ¬ifier)); + ¬ifier), false); } - notifier->Show(notification.Get()); + AssertRetHRESULT(notifier->Show(notification.Get()), false); MetroUtils::FireObserver("metro_native_toast_shown", mCookie.get()); + + return true; } void ToastNotificationHandler::SetNodeValueString(HSTRING inputString, ComPtr node, ComPtr xml) { @@ -142,5 +145,6 @@ ToastNotificationHandler::OnDismiss(IToastNotification *notification, IToastDismissedEventArgs* aArgs) { MetroUtils::FireObserver("metro_native_toast_dismissed", mCookie.get()); + delete this; return S_OK; } diff --git a/widget/windows/winrt/ToastNotificationHandler.h b/widget/windows/winrt/ToastNotificationHandler.h index acd49ab2181e..a0b8c729079f 100644 --- a/widget/windows/winrt/ToastNotificationHandler.h +++ b/widget/windows/winrt/ToastNotificationHandler.h @@ -25,9 +25,9 @@ class ToastNotificationHandler { ToastNotificationHandler() {}; ~ToastNotificationHandler() {}; - void DisplayNotification(HSTRING title, HSTRING msg, HSTRING imagePath, + bool DisplayNotification(HSTRING title, HSTRING msg, HSTRING imagePath, const nsAString& aCookie, const nsAString& aAppId); - void DisplayTextNotification(HSTRING title, HSTRING msg, + bool DisplayTextNotification(HSTRING title, HSTRING msg, const nsAString& aCookie, const nsAString& aAppId); HRESULT OnActivate(IToastNotification *notification, IInspectable *inspectable); @@ -38,7 +38,7 @@ class ToastNotificationHandler { nsString mCookie; ComPtr mToastNotificationManagerStatics; - void CreateWindowsNotificationFromXml(IXmlDocument *toastXml, + bool CreateWindowsNotificationFromXml(IXmlDocument *toastXml, const nsAString& aAppId); ComPtr InitializeXmlForTemplate(ToastTemplateType templateType); }; diff --git a/widget/windows/winrt/nsWinMetroUtils.cpp b/widget/windows/winrt/nsWinMetroUtils.cpp index 7c7cce94c966..e9ea40ab3166 100644 --- a/widget/windows/winrt/nsWinMetroUtils.cpp +++ b/widget/windows/winrt/nsWinMetroUtils.cpp @@ -208,12 +208,20 @@ nsWinMetroUtils::ShowNativeToast(const nsAString &aTitle, HSTRING title = HStringReference(aTitle.BeginReading()).Get(); HSTRING msg = HStringReference(aMessage.BeginReading()).Get(); + bool ret; if (anImage.Length() > 0) { HSTRING imagePath = HStringReference(anImage.BeginReading()).Get(); - notification_handler->DisplayNotification(title, msg, imagePath, aCookie, - aAppId); + ret = notification_handler->DisplayNotification(title, msg, imagePath, + aCookie, + aAppId); } else { - notification_handler->DisplayTextNotification(title, msg, aCookie, aAppId); + ret = notification_handler->DisplayTextNotification(title, msg, aCookie, + aAppId); + } + + if (!ret) { + delete notification_handler; + return NS_ERROR_FAILURE; } return NS_OK;