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.

     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 #include "ToastNotificationHandler.h"
     7 #include "MetroUtils.h"
     8 #include "mozilla/Services.h"
     9 #include "FrameworkView.h"
    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;
    18 typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_IInspectable_t ToastActivationHandler;
    19 typedef __FITypedEventHandler_2_Windows__CUI__CNotifications__CToastNotification_Windows__CUI__CNotifications__CToastDismissedEventArgs ToastDismissHandler;
    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;
    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;
    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);
    43   AssertRetHRESULT(toastTextElements->Item(0, &titleTextNodeRoot), false);
    44   AssertRetHRESULT(toastTextElements->Item(1, &msgTextNodeRoot), false);
    45   AssertRetHRESULT(toastImageElements->Item(0, &imageNodeRoot), false);
    47   Microsoft::WRL::ComPtr<IXmlNamedNodeMap> attributes;
    48   AssertRetHRESULT(imageNodeRoot->get_Attributes(&attributes), false);
    49   AssertRetHRESULT(attributes->GetNamedItem(srcNodeStr, &srcAttribute), false);
    51   SetNodeValueString(title, titleTextNodeRoot.Get(), toastXml.Get());
    52   SetNodeValueString(msg, msgTextNodeRoot.Get(), toastXml.Get());
    53   SetNodeValueString(imagePath, srcAttribute.Get(), toastXml.Get());
    55   return CreateWindowsNotificationFromXml(toastXml.Get(), aAppId);
    56 }
    58 bool
    59 ToastNotificationHandler::DisplayTextNotification(HSTRING title,
    60                                                   HSTRING msg,
    61                                                   const nsAString& aCookie,
    62                                                   const nsAString& aAppId)
    63 {
    64   mCookie = aCookie;
    66   Microsoft::WRL::ComPtr<IXmlDocument> toastXml =
    67     InitializeXmlForTemplate(ToastTemplateType::ToastTemplateType_ToastText03);
    68   Microsoft::WRL::ComPtr<IXmlNodeList> toastTextElements;
    69   Microsoft::WRL::ComPtr<IXmlNode> titleTextNodeRoot, msgTextNodeRoot;
    71   HSTRING textNodeStr;
    72   HSTRING_HEADER textHeader;
    73   WindowsCreateStringReference(L"text", 4, &textHeader, &textNodeStr);
    74   toastXml->GetElementsByTagName(textNodeStr, &toastTextElements);
    76   AssertRetHRESULT(toastTextElements->Item(0, &titleTextNodeRoot), false);
    77   AssertRetHRESULT(toastTextElements->Item(1, &msgTextNodeRoot), false);
    79   SetNodeValueString(title, titleTextNodeRoot.Get(), toastXml.Get());
    80   SetNodeValueString(msg, msgTextNodeRoot.Get(), toastXml.Get());
    82   return CreateWindowsNotificationFromXml(toastXml.Get(), aAppId);
    83 }
    85 Microsoft::WRL::ComPtr<IXmlDocument>
    86 ToastNotificationHandler::InitializeXmlForTemplate(ToastTemplateType templateType) {
    87   Microsoft::WRL::ComPtr<IXmlDocument> toastXml;
    89   AssertRetHRESULT(GetActivationFactory(HStringReference(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(),
    90     mToastNotificationManagerStatics.GetAddressOf()), nullptr);
    92   mToastNotificationManagerStatics->GetTemplateContent(templateType, &toastXml);
    94   return toastXml;
    95 }
    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);
   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);
   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);
   126   MetroUtils::FireObserver("metro_native_toast_shown", mCookie.get());
   128   return true;
   129 }
   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;
   136   AssertHRESULT(xml->CreateTextNode(inputString, &inputText));
   137   AssertHRESULT(inputText.As(&inputTextNode));
   138   AssertHRESULT(node->AppendChild(inputTextNode.Get(), &pAppendedChild));
   139 }
   141 HRESULT ToastNotificationHandler::OnActivate(IToastNotification *notification, IInspectable *inspectable) {
   142   MetroUtils::FireObserver("metro_native_toast_clicked", mCookie.get());
   143   return S_OK;
   144 }
   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