widget/windows/winrt/ToastNotificationHandler.cpp

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

mercurial