2015-11-08 06:11:15 +00:00
|
|
|
// Copyright (c) 2015 Felix Rieseberg <feriese@microsoft.com> and Jason Poon <jason.poon@microsoft.com>. All rights reserved.
|
2015-11-04 18:13:52 +00:00
|
|
|
// Copyright (c) 2015 Ryan McShane <rmcshane@bandwidth.com> and Brandon Smith <bsmith@bandwidth.com>
|
|
|
|
// Thanks to both of those folks mentioned above who first thought up a bunch of this code
|
|
|
|
// and released it as MIT to the world.
|
|
|
|
|
|
|
|
#include "windows_toast_notification.h"
|
2015-11-08 03:41:29 +00:00
|
|
|
#include "content/public/browser/desktop_notification_delegate.h"
|
|
|
|
|
2015-11-04 18:13:52 +00:00
|
|
|
using namespace WinToasts;
|
2015-11-08 06:11:15 +00:00
|
|
|
using namespace Microsoft::WRL;
|
|
|
|
using namespace ABI::Windows::UI::Notifications;
|
|
|
|
using namespace ABI::Windows::Data::Xml::Dom;
|
2015-11-04 18:13:52 +00:00
|
|
|
|
|
|
|
#define BREAK_IF_BAD(hr) if(!SUCCEEDED(hr)) break;
|
|
|
|
|
|
|
|
namespace WinToasts {
|
|
|
|
|
2015-11-08 06:11:15 +00:00
|
|
|
// Initialize Windows Runtime
|
|
|
|
static HRESULT init = Windows::Foundation::Initialize(RO_INIT_MULTITHREADED);
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
WindowsToastNotification::WindowsToastNotification(const char* appName, content::DesktopNotificationDelegate* delegate)
|
2015-11-04 18:13:52 +00:00
|
|
|
{
|
2015-11-08 06:11:15 +00:00
|
|
|
HSTRING toastNotifMgrStr = NULL;
|
|
|
|
HSTRING appId = NULL;
|
|
|
|
|
|
|
|
HRESULT hr = CreateHString(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager, &toastNotifMgrStr);
|
|
|
|
|
|
|
|
hr = Windows::Foundation::GetActivationFactory(toastNotifMgrStr, &m_toastManager);
|
|
|
|
|
|
|
|
WCHAR wAppName[MAX_PATH];
|
|
|
|
swprintf(wAppName, ARRAYSIZE(wAppName), L"%S", appName);
|
|
|
|
hr = CreateHString(wAppName, &appId);
|
|
|
|
|
|
|
|
m_toastManager->CreateToastNotifierWithId(appId, &m_toastNotifier);
|
|
|
|
|
|
|
|
if (toastNotifMgrStr != NULL) {
|
|
|
|
WindowsDeleteString(toastNotifMgrStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appId != NULL) {
|
|
|
|
WindowsDeleteString(appId);
|
|
|
|
}
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
n_delegate = delegate;
|
2015-11-04 18:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WindowsToastNotification::~WindowsToastNotification()
|
|
|
|
{
|
2015-11-08 03:41:29 +00:00
|
|
|
if (n_delegate) {
|
|
|
|
delete n_delegate;
|
2015-11-04 18:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-08 06:11:15 +00:00
|
|
|
void WindowsToastNotification::ShowNotification(const WCHAR* title, const WCHAR* msg, std::string iconPath, ComPtr<IToastNotification>& toast)
|
2015-11-04 18:13:52 +00:00
|
|
|
{
|
2015-11-08 03:41:29 +00:00
|
|
|
HRESULT hr;
|
2015-11-08 06:11:15 +00:00
|
|
|
HSTRING toastNotifStr = NULL;
|
2015-11-04 18:13:52 +00:00
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
do {
|
2015-11-04 18:13:52 +00:00
|
|
|
ComPtr<IXmlDocument> toastXml;
|
2015-11-08 06:11:15 +00:00
|
|
|
hr = GetToastXml(m_toastManager.Get(), title, msg, iconPath, &toastXml);
|
2015-11-04 18:13:52 +00:00
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
hr = CreateHString(RuntimeClass_Windows_UI_Notifications_ToastNotification, &toastNotifStr);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IToastNotificationFactory> toastFactory;
|
|
|
|
hr = Windows::Foundation::GetActivationFactory(toastNotifStr, &toastFactory);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
hr = toastFactory->CreateToastNotification(toastXml.Get(), &toast);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
hr = SetupCallbacks(toast.Get());
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
2015-11-08 06:11:15 +00:00
|
|
|
hr = m_toastNotifier->Show(toast.Get());
|
2015-11-04 18:13:52 +00:00
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
n_delegate->NotificationDisplayed();
|
2015-11-04 18:13:52 +00:00
|
|
|
} while (FALSE);
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
if (toastNotifStr != NULL) {
|
2015-11-04 18:13:52 +00:00
|
|
|
WindowsDeleteString(toastNotifStr);
|
|
|
|
}
|
2015-11-08 06:11:15 +00:00
|
|
|
}
|
2015-11-04 18:13:52 +00:00
|
|
|
|
2015-11-08 06:11:15 +00:00
|
|
|
void WindowsToastNotification::DismissNotification(ComPtr<IToastNotification> toast)
|
|
|
|
{
|
|
|
|
m_toastNotifier->Hide(toast.Get());
|
2015-11-04 18:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsToastNotification::NotificationClicked()
|
|
|
|
{
|
2015-11-08 03:41:29 +00:00
|
|
|
delete this;
|
2015-11-04 18:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowsToastNotification::NotificationDismissed()
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
HRESULT WindowsToastNotification::GetToastXml(
|
2015-11-08 06:11:15 +00:00
|
|
|
IToastNotificationManagerStatics* toastManager,
|
2015-11-08 03:41:29 +00:00
|
|
|
const WCHAR* title,
|
|
|
|
const WCHAR* msg,
|
|
|
|
std::string iconPath,
|
|
|
|
IXmlDocument** toastXml) {
|
|
|
|
|
2015-11-04 18:13:52 +00:00
|
|
|
HRESULT hr;
|
|
|
|
ToastTemplateType templateType;
|
2015-11-08 03:41:29 +00:00
|
|
|
if (title == NULL || msg == NULL) {
|
2015-11-04 18:13:52 +00:00
|
|
|
// Single line toast
|
2015-11-08 03:41:29 +00:00
|
|
|
templateType = iconPath.length() == 0 ? ToastTemplateType_ToastText01 : ToastTemplateType_ToastImageAndText01;
|
2015-11-08 06:11:15 +00:00
|
|
|
hr = m_toastManager->GetTemplateContent(templateType, toastXml);
|
2015-11-04 18:13:52 +00:00
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
const WCHAR* text = title != NULL ? title : msg;
|
|
|
|
hr = SetXmlText(*toastXml, text);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Title and body toast
|
2015-11-08 03:41:29 +00:00
|
|
|
templateType = iconPath.length() == 0 ? ToastTemplateType_ToastText02 : ToastTemplateType_ToastImageAndText02;
|
2015-11-08 06:11:15 +00:00
|
|
|
hr = toastManager->GetTemplateContent(templateType, toastXml);
|
2015-11-04 18:13:52 +00:00
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
hr = SetXmlText(*toastXml, title, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
if (iconPath.length() != 0 && SUCCEEDED(hr)) {
|
2015-11-04 18:13:52 +00:00
|
|
|
// Toast has image
|
2015-11-08 03:41:29 +00:00
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
hr = SetXmlImage(*toastXml, iconPath);
|
2015-11-04 18:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't stop a notification from showing just because an image couldn't be displayed. By default the app icon will be shown.
|
|
|
|
hr = S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT WindowsToastNotification::SetXmlText(IXmlDocument* doc, const WCHAR* text)
|
|
|
|
{
|
|
|
|
HSTRING tag = NULL;
|
|
|
|
|
|
|
|
ComPtr<IXmlNodeList> nodeList;
|
|
|
|
HRESULT hr = GetTextNodeList(&tag, doc, &nodeList, 1);
|
2015-11-08 06:11:15 +00:00
|
|
|
do {
|
2015-11-04 18:13:52 +00:00
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> node;
|
|
|
|
hr = nodeList->Item(0, &node);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
hr = AppendTextToXml(doc, node.Get(), text);
|
|
|
|
} while (FALSE);
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
if (tag != NULL) {
|
2015-11-04 18:13:52 +00:00
|
|
|
WindowsDeleteString(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT WindowsToastNotification::SetXmlText(IXmlDocument* doc, const WCHAR* title, const WCHAR* body)
|
|
|
|
{
|
|
|
|
HSTRING tag = NULL;
|
|
|
|
ComPtr<IXmlNodeList> nodeList;
|
|
|
|
HRESULT hr = GetTextNodeList(&tag, doc, &nodeList, 2);
|
2015-11-08 06:11:15 +00:00
|
|
|
do {
|
2015-11-04 18:13:52 +00:00
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> node;
|
|
|
|
hr = nodeList->Item(0, &node);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
hr = AppendTextToXml(doc, node.Get(), title);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
hr = nodeList->Item(1, &node);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
hr = AppendTextToXml(doc, node.Get(), body);
|
|
|
|
} while (FALSE);
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
if (tag != NULL) {
|
2015-11-04 18:13:52 +00:00
|
|
|
WindowsDeleteString(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
HRESULT WindowsToastNotification::SetXmlImage(IXmlDocument* doc, std::string iconPath)
|
2015-11-04 18:13:52 +00:00
|
|
|
{
|
|
|
|
HSTRING tag = NULL;
|
|
|
|
HSTRING src = NULL;
|
|
|
|
HSTRING imgPath = NULL;
|
|
|
|
HRESULT hr = CreateHString(L"image", &tag);
|
2015-11-08 03:41:29 +00:00
|
|
|
|
|
|
|
do {
|
2015-11-04 18:13:52 +00:00
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IXmlNodeList> nodeList;
|
|
|
|
hr = doc->GetElementsByTagName(tag, &nodeList);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> imageNode;
|
|
|
|
hr = nodeList->Item(0, &imageNode);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IXmlNamedNodeMap> attrs;
|
|
|
|
hr = imageNode->get_Attributes(&attrs);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
hr = CreateHString(L"src", &src);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> srcAttr;
|
|
|
|
hr = attrs->GetNamedItem(src, &srcAttr);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
WCHAR xmlPath[MAX_PATH];
|
2015-11-08 03:41:29 +00:00
|
|
|
swprintf(xmlPath, ARRAYSIZE(xmlPath), L"%S", iconPath);
|
2015-11-04 18:13:52 +00:00
|
|
|
hr = CreateHString(xmlPath, &imgPath);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IXmlText> srcText;
|
|
|
|
hr = doc->CreateTextNode(imgPath, &srcText);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> srcNode;
|
|
|
|
hr = srcText.As(&srcNode);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> childNode;
|
|
|
|
hr = srcAttr->AppendChild(srcNode.Get(), &childNode);
|
|
|
|
} while (FALSE);
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
if (tag != NULL) {
|
2015-11-04 18:13:52 +00:00
|
|
|
WindowsDeleteString(tag);
|
|
|
|
}
|
2015-11-08 03:41:29 +00:00
|
|
|
if (src != NULL) {
|
2015-11-04 18:13:52 +00:00
|
|
|
WindowsDeleteString(src);
|
|
|
|
}
|
2015-11-08 03:41:29 +00:00
|
|
|
if (imgPath != NULL) {
|
2015-11-04 18:13:52 +00:00
|
|
|
WindowsDeleteString(imgPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT WindowsToastNotification::GetTextNodeList(HSTRING* tag, IXmlDocument* doc, IXmlNodeList** nodeList, UINT32 reqLength)
|
|
|
|
{
|
|
|
|
HRESULT hr = CreateHString(L"text", tag);
|
|
|
|
do{
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
hr = doc->GetElementsByTagName(*tag, nodeList);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
UINT32 nodeLength;
|
|
|
|
hr = (*nodeList)->get_Length(&nodeLength);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
if (nodeLength < reqLength) {
|
|
|
|
hr = E_INVALIDARG;
|
|
|
|
}
|
|
|
|
} while (FALSE);
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
if (!SUCCEEDED(hr)) {
|
2015-11-04 18:13:52 +00:00
|
|
|
// Allow the caller to delete this string on success
|
|
|
|
WindowsDeleteString(*tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT WindowsToastNotification::AppendTextToXml(IXmlDocument* doc, IXmlNode* node, const WCHAR* text)
|
|
|
|
{
|
|
|
|
HSTRING str = NULL;
|
|
|
|
HRESULT hr = CreateHString(text, &str);
|
2015-11-08 03:41:29 +00:00
|
|
|
do {
|
2015-11-04 18:13:52 +00:00
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IXmlText> xmlText;
|
|
|
|
hr = doc->CreateTextNode(str, &xmlText);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> textNode;
|
|
|
|
hr = xmlText.As(&textNode);
|
|
|
|
BREAK_IF_BAD(hr);
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> appendNode;
|
|
|
|
hr = node->AppendChild(textNode.Get(), &appendNode);
|
|
|
|
} while (FALSE);
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
if (str != NULL) {
|
2015-11-04 18:13:52 +00:00
|
|
|
WindowsDeleteString(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT WindowsToastNotification::SetupCallbacks(IToastNotification* toast)
|
|
|
|
{
|
|
|
|
EventRegistrationToken activatedToken, dismissedToken;
|
2015-11-09 19:47:18 +00:00
|
|
|
m_eventHandler = Make<ToastEventHandler>(this, n_delegate);
|
|
|
|
HRESULT hr = toast->add_Activated(m_eventHandler.Get(), &activatedToken);
|
|
|
|
|
2015-11-04 18:13:52 +00:00
|
|
|
if (SUCCEEDED(hr)) {
|
2015-11-09 19:47:18 +00:00
|
|
|
hr = toast->add_Dismissed(m_eventHandler.Get(), &dismissedToken);
|
2015-11-04 18:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT WindowsToastNotification::CreateHString(const WCHAR* source, HSTRING* dest)
|
|
|
|
{
|
|
|
|
if (source == NULL || dest == NULL) {
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT hr = WindowsCreateString(source, wcslen(source), dest);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2015-11-08 03:41:29 +00:00
|
|
|
/*
|
|
|
|
/ Toast Event Handler
|
|
|
|
*/
|
|
|
|
ToastEventHandler::ToastEventHandler(WindowsToastNotification* notification, content::DesktopNotificationDelegate* delegate)
|
2015-11-04 18:13:52 +00:00
|
|
|
{
|
|
|
|
m_notification = notification;
|
2015-11-08 03:41:29 +00:00
|
|
|
n_delegate = delegate;
|
2015-11-04 18:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ToastEventHandler::~ToastEventHandler()
|
|
|
|
{
|
|
|
|
// Empty
|
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP ToastEventHandler::Invoke(IToastNotification* sender, IInspectable* args)
|
|
|
|
{
|
2015-11-08 03:41:29 +00:00
|
|
|
// Notification "activated" (clicked)
|
|
|
|
n_delegate->NotificationClick();
|
|
|
|
|
|
|
|
if (m_notification != NULL) {
|
2015-11-04 18:13:52 +00:00
|
|
|
m_notification->NotificationClicked();
|
|
|
|
}
|
2015-11-08 03:41:29 +00:00
|
|
|
|
2015-11-04 18:13:52 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP ToastEventHandler::Invoke(IToastNotification* sender, IToastDismissedEventArgs* e)
|
2015-11-08 03:41:29 +00:00
|
|
|
{
|
|
|
|
// Notification dismissed
|
|
|
|
n_delegate->NotificationClosed();
|
|
|
|
|
|
|
|
if (m_notification != NULL) {
|
2015-11-04 18:13:52 +00:00
|
|
|
m_notification->NotificationDismissed();
|
2015-11-08 03:41:29 +00:00
|
|
|
|
2015-11-04 18:13:52 +00:00
|
|
|
}
|
2015-11-08 03:41:29 +00:00
|
|
|
|
2015-11-04 18:13:52 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-10 10:04:46 +00:00
|
|
|
} //namespace
|