widget/windows/winrt/ToastNotificationHandler.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "ToastNotificationHandler.h"
michael@0 7 #include "MetroUtils.h"
michael@0 8 #include "mozilla/Services.h"
michael@0 9 #include "FrameworkView.h"
michael@0 10
michael@0 11 using namespace ABI::Windows::Foundation;
michael@0 12 using namespace ABI::Windows::Data::Xml::Dom;
michael@0 13 using namespace Microsoft::WRL;
michael@0 14 using namespace Microsoft::WRL::Wrappers;
michael@0 15 using namespace mozilla;
michael@0 16 using namespace ABI::Windows::UI::Notifications;
michael@0 17
michael@0 18 typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_IInspectable_t ToastActivationHandler;
michael@0 19 typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_Windows__CUI__CNotifications__CToastDismissedEventArgs ToastDismissHandler;
michael@0 20
michael@0 21 bool
michael@0 22 ToastNotificationHandler::DisplayNotification(HSTRING title,
michael@0 23 HSTRING msg,
michael@0 24 HSTRING imagePath,
michael@0 25 const nsAString& aCookie,
michael@0 26 const nsAString& aAppId)
michael@0 27 {
michael@0 28 mCookie = aCookie;
michael@0 29
michael@0 30 Microsoft::WRL::ComPtr<IXmlDocument> toastXml =
michael@0 31 InitializeXmlForTemplate(ToastTemplateType::ToastTemplateType_ToastImageAndText03);
michael@0 32 Microsoft::WRL::ComPtr<IXmlNodeList> toastTextElements, toastImageElements;
michael@0 33 Microsoft::WRL::ComPtr<IXmlNode> titleTextNodeRoot, msgTextNodeRoot, imageNodeRoot, srcAttribute;
michael@0 34
michael@0 35 HSTRING textNodeStr, imageNodeStr, srcNodeStr;
michael@0 36 HSTRING_HEADER textHeader, imageHeader, srcHeader;
michael@0 37 WindowsCreateStringReference(L"text", 4, &textHeader, &textNodeStr);
michael@0 38 WindowsCreateStringReference(L"image", 5, &imageHeader, &imageNodeStr);
michael@0 39 WindowsCreateStringReference(L"src", 3, &srcHeader, &srcNodeStr);
michael@0 40 toastXml->GetElementsByTagName(textNodeStr, &toastTextElements);
michael@0 41 toastXml->GetElementsByTagName(imageNodeStr, &toastImageElements);
michael@0 42
michael@0 43 AssertRetHRESULT(toastTextElements->Item(0, &titleTextNodeRoot), false);
michael@0 44 AssertRetHRESULT(toastTextElements->Item(1, &msgTextNodeRoot), false);
michael@0 45 AssertRetHRESULT(toastImageElements->Item(0, &imageNodeRoot), false);
michael@0 46
michael@0 47 Microsoft::WRL::ComPtr<IXmlNamedNodeMap> attributes;
michael@0 48 AssertRetHRESULT(imageNodeRoot->get_Attributes(&attributes), false);
michael@0 49 AssertRetHRESULT(attributes->GetNamedItem(srcNodeStr, &srcAttribute), false);
michael@0 50
michael@0 51 SetNodeValueString(title, titleTextNodeRoot.Get(), toastXml.Get());
michael@0 52 SetNodeValueString(msg, msgTextNodeRoot.Get(), toastXml.Get());
michael@0 53 SetNodeValueString(imagePath, srcAttribute.Get(), toastXml.Get());
michael@0 54
michael@0 55 return CreateWindowsNotificationFromXml(toastXml.Get(), aAppId);
michael@0 56 }
michael@0 57
michael@0 58 bool
michael@0 59 ToastNotificationHandler::DisplayTextNotification(HSTRING title,
michael@0 60 HSTRING msg,
michael@0 61 const nsAString& aCookie,
michael@0 62 const nsAString& aAppId)
michael@0 63 {
michael@0 64 mCookie = aCookie;
michael@0 65
michael@0 66 Microsoft::WRL::ComPtr<IXmlDocument> toastXml =
michael@0 67 InitializeXmlForTemplate(ToastTemplateType::ToastTemplateType_ToastText03);
michael@0 68 Microsoft::WRL::ComPtr<IXmlNodeList> toastTextElements;
michael@0 69 Microsoft::WRL::ComPtr<IXmlNode> titleTextNodeRoot, msgTextNodeRoot;
michael@0 70
michael@0 71 HSTRING textNodeStr;
michael@0 72 HSTRING_HEADER textHeader;
michael@0 73 WindowsCreateStringReference(L"text", 4, &textHeader, &textNodeStr);
michael@0 74 toastXml->GetElementsByTagName(textNodeStr, &toastTextElements);
michael@0 75
michael@0 76 AssertRetHRESULT(toastTextElements->Item(0, &titleTextNodeRoot), false);
michael@0 77 AssertRetHRESULT(toastTextElements->Item(1, &msgTextNodeRoot), false);
michael@0 78
michael@0 79 SetNodeValueString(title, titleTextNodeRoot.Get(), toastXml.Get());
michael@0 80 SetNodeValueString(msg, msgTextNodeRoot.Get(), toastXml.Get());
michael@0 81
michael@0 82 return CreateWindowsNotificationFromXml(toastXml.Get(), aAppId);
michael@0 83 }
michael@0 84
michael@0 85 Microsoft::WRL::ComPtr<IXmlDocument>
michael@0 86 ToastNotificationHandler::InitializeXmlForTemplate(ToastTemplateType templateType) {
michael@0 87 Microsoft::WRL::ComPtr<IXmlDocument> toastXml;
michael@0 88
michael@0 89 AssertRetHRESULT(GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(),
michael@0 90 mToastNotificationManagerStatics.GetAddressOf()), nullptr);
michael@0 91
michael@0 92 mToastNotificationManagerStatics->GetTemplateContent(templateType, &toastXml);
michael@0 93
michael@0 94 return toastXml;
michael@0 95 }
michael@0 96
michael@0 97 bool
michael@0 98 ToastNotificationHandler::CreateWindowsNotificationFromXml(IXmlDocument *toastXml,
michael@0 99 const nsAString& aAppId)
michael@0 100 {
michael@0 101 Microsoft::WRL::ComPtr<IToastNotification> notification;
michael@0 102 Microsoft::WRL::ComPtr<IToastNotificationFactory> factory;
michael@0 103 AssertRetHRESULT(GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotification).Get(),
michael@0 104 factory.GetAddressOf()), false);
michael@0 105 AssertRetHRESULT(factory->CreateToastNotification(toastXml, &notification),
michael@0 106 false);
michael@0 107
michael@0 108 EventRegistrationToken activatedToken;
michael@0 109 AssertRetHRESULT(notification->add_Activated(Callback<ToastActivationHandler>(this,
michael@0 110 &ToastNotificationHandler::OnActivate).Get(), &activatedToken), false);
michael@0 111 EventRegistrationToken dismissedToken;
michael@0 112 AssertRetHRESULT(notification->add_Dismissed(Callback<ToastDismissHandler>(this,
michael@0 113 &ToastNotificationHandler::OnDismiss).Get(), &dismissedToken), false);
michael@0 114
michael@0 115 Microsoft::WRL::ComPtr<IToastNotifier> notifier;
michael@0 116 if (aAppId.IsEmpty()) {
michael@0 117 AssertRetHRESULT(mToastNotificationManagerStatics->CreateToastNotifier(
michael@0 118 &notifier), false);
michael@0 119 } else {
michael@0 120 AssertRetHRESULT(mToastNotificationManagerStatics->CreateToastNotifierWithId(
michael@0 121 HStringReference(PromiseFlatString(aAppId).get()).Get(),
michael@0 122 &notifier), false);
michael@0 123 }
michael@0 124 AssertRetHRESULT(notifier->Show(notification.Get()), false);
michael@0 125
michael@0 126 MetroUtils::FireObserver("metro_native_toast_shown", mCookie.get());
michael@0 127
michael@0 128 return true;
michael@0 129 }
michael@0 130
michael@0 131 void ToastNotificationHandler::SetNodeValueString(HSTRING inputString,
michael@0 132 Microsoft::WRL::ComPtr<IXmlNode> node, Microsoft::WRL::ComPtr<IXmlDocument> xml) {
michael@0 133 Microsoft::WRL::ComPtr<IXmlText> inputText;
michael@0 134 Microsoft::WRL::ComPtr<IXmlNode> inputTextNode, pAppendedChild;
michael@0 135
michael@0 136 AssertHRESULT(xml->CreateTextNode(inputString, &inputText));
michael@0 137 AssertHRESULT(inputText.As(&inputTextNode));
michael@0 138 AssertHRESULT(node->AppendChild(inputTextNode.Get(), &pAppendedChild));
michael@0 139 }
michael@0 140
michael@0 141 HRESULT ToastNotificationHandler::OnActivate(IToastNotification *notification, IInspectable *inspectable) {
michael@0 142 MetroUtils::FireObserver("metro_native_toast_clicked", mCookie.get());
michael@0 143 return S_OK;
michael@0 144 }
michael@0 145
michael@0 146 HRESULT
michael@0 147 ToastNotificationHandler::OnDismiss(IToastNotification *notification,
michael@0 148 IToastDismissedEventArgs* aArgs)
michael@0 149 {
michael@0 150 MetroUtils::FireObserver("metro_native_toast_dismissed", mCookie.get());
michael@0 151 delete this;
michael@0 152 return S_OK;
michael@0 153 }

mercurial