summaryrefslogtreecommitdiff
path: root/widget/windows/winrt/ToastNotificationHandler.cpp
blob: 1466cfef49f062b7df268be92b65bbd601ed9dc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ToastNotificationHandler.h"
#include "MetroUtils.h"
#include "mozilla/Services.h"
#include "FrameworkView.h"

using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Data::Xml::Dom;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace mozilla;
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;

bool
ToastNotificationHandler::DisplayNotification(HSTRING title,
                                              HSTRING msg,
                                              HSTRING imagePath,
                                              const nsAString& aCookie,
                                              const nsAString& aAppId)
{
  mCookie = aCookie;

  Microsoft::WRL::ComPtr<IXmlDocument> toastXml =
    InitializeXmlForTemplate(ToastTemplateType::ToastTemplateType_ToastImageAndText03);
  Microsoft::WRL::ComPtr<IXmlNodeList> toastTextElements, toastImageElements;
  Microsoft::WRL::ComPtr<IXmlNode> titleTextNodeRoot, msgTextNodeRoot, imageNodeRoot, srcAttribute;

  HSTRING textNodeStr, imageNodeStr, srcNodeStr;
  HSTRING_HEADER textHeader, imageHeader, srcHeader;
  WindowsCreateStringReference(L"text", 4, &textHeader, &textNodeStr);
  WindowsCreateStringReference(L"image", 5, &imageHeader, &imageNodeStr);
  WindowsCreateStringReference(L"src", 3, &srcHeader, &srcNodeStr);
  toastXml->GetElementsByTagName(textNodeStr, &toastTextElements);
  toastXml->GetElementsByTagName(imageNodeStr, &toastImageElements);

  AssertRetHRESULT(toastTextElements->Item(0, &titleTextNodeRoot), false);
  AssertRetHRESULT(toastTextElements->Item(1, &msgTextNodeRoot), false);
  AssertRetHRESULT(toastImageElements->Item(0, &imageNodeRoot), false);

  Microsoft::WRL::ComPtr<IXmlNamedNodeMap> attributes;
  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());

  return CreateWindowsNotificationFromXml(toastXml.Get(), aAppId);
}

bool
ToastNotificationHandler::DisplayTextNotification(HSTRING title,
                                                  HSTRING msg,
                                                  const nsAString& aCookie,
                                                  const nsAString& aAppId)
{
  mCookie = aCookie;

  Microsoft::WRL::ComPtr<IXmlDocument> toastXml =
    InitializeXmlForTemplate(ToastTemplateType::ToastTemplateType_ToastText03);
  Microsoft::WRL::ComPtr<IXmlNodeList> toastTextElements;
  Microsoft::WRL::ComPtr<IXmlNode> titleTextNodeRoot, msgTextNodeRoot;

  HSTRING textNodeStr;
  HSTRING_HEADER textHeader;
  WindowsCreateStringReference(L"text", 4, &textHeader, &textNodeStr);
  toastXml->GetElementsByTagName(textNodeStr, &toastTextElements);

  AssertRetHRESULT(toastTextElements->Item(0, &titleTextNodeRoot), false);
  AssertRetHRESULT(toastTextElements->Item(1, &msgTextNodeRoot), false);

  SetNodeValueString(title, titleTextNodeRoot.Get(), toastXml.Get());
  SetNodeValueString(msg, msgTextNodeRoot.Get(), toastXml.Get());

  return CreateWindowsNotificationFromXml(toastXml.Get(), aAppId);
}

Microsoft::WRL::ComPtr<IXmlDocument>
ToastNotificationHandler::InitializeXmlForTemplate(ToastTemplateType templateType) {
  Microsoft::WRL::ComPtr<IXmlDocument> toastXml;

  AssertRetHRESULT(GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(),
    mToastNotificationManagerStatics.GetAddressOf()), nullptr);

  mToastNotificationManagerStatics->GetTemplateContent(templateType, &toastXml);

  return toastXml;
}

bool
ToastNotificationHandler::CreateWindowsNotificationFromXml(IXmlDocument *toastXml,
                                                           const nsAString& aAppId)
{
  Microsoft::WRL::ComPtr<IToastNotification> notification;
  Microsoft::WRL::ComPtr<IToastNotificationFactory> factory;
  AssertRetHRESULT(GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotification).Get(),
    factory.GetAddressOf()), false);
  AssertRetHRESULT(factory->CreateToastNotification(toastXml, &notification),
                   false);

  EventRegistrationToken activatedToken;
  AssertRetHRESULT(notification->add_Activated(Callback<ToastActivationHandler>(this,
    &ToastNotificationHandler::OnActivate).Get(), &activatedToken), false);
  EventRegistrationToken dismissedToken;
  AssertRetHRESULT(notification->add_Dismissed(Callback<ToastDismissHandler>(this,
    &ToastNotificationHandler::OnDismiss).Get(), &dismissedToken), false);

  Microsoft::WRL::ComPtr<IToastNotifier> notifier;
  if (aAppId.IsEmpty()) {
    AssertRetHRESULT(mToastNotificationManagerStatics->CreateToastNotifier(
                       &notifier), false);
  } else {
    AssertRetHRESULT(mToastNotificationManagerStatics->CreateToastNotifierWithId(
                    HStringReference(PromiseFlatString(aAppId).get()).Get(),
                    &notifier), false);
  }
  AssertRetHRESULT(notifier->Show(notification.Get()), false);

  MetroUtils::FireObserver("metro_native_toast_shown", mCookie.get());

  return true;
}

void ToastNotificationHandler::SetNodeValueString(HSTRING inputString,
  Microsoft::WRL::ComPtr<IXmlNode> node, Microsoft::WRL::ComPtr<IXmlDocument> xml) {
  Microsoft::WRL::ComPtr<IXmlText> inputText;
  Microsoft::WRL::ComPtr<IXmlNode> inputTextNode, pAppendedChild;

  AssertHRESULT(xml->CreateTextNode(inputString, &inputText));
  AssertHRESULT(inputText.As(&inputTextNode));
  AssertHRESULT(node->AppendChild(inputTextNode.Get(), &pAppendedChild));
}

HRESULT ToastNotificationHandler::OnActivate(IToastNotification *notification, IInspectable *inspectable) {
  MetroUtils::FireObserver("metro_native_toast_clicked", mCookie.get());
  return S_OK;
}

HRESULT
ToastNotificationHandler::OnDismiss(IToastNotification *notification,
                                    IToastDismissedEventArgs* aArgs)
{
  MetroUtils::FireObserver("metro_native_toast_dismissed", mCookie.get());
  delete this;
  return S_OK;
}