Code cleanup with Chromium's coding style
This commit is contained in:
parent
37a89ee0d9
commit
1b9c9e40e3
5 changed files with 358 additions and 358 deletions
|
@ -164,8 +164,11 @@
|
||||||
'msvs_settings': {
|
'msvs_settings': {
|
||||||
'VCLinkerTool': {
|
'VCLinkerTool': {
|
||||||
'AdditionalDependencies': [
|
'AdditionalDependencies': [
|
||||||
|
# Windows Runtime.
|
||||||
|
'crypt32.lib',
|
||||||
'runtimeobject.lib',
|
'runtimeobject.lib',
|
||||||
'windowsapp.lib'
|
'shlwapi.lib',
|
||||||
|
'windowsapp.lib',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -203,8 +206,6 @@
|
||||||
'msvs_settings': {
|
'msvs_settings': {
|
||||||
'VCLinkerTool': {
|
'VCLinkerTool': {
|
||||||
'AdditionalDependencies': [
|
'AdditionalDependencies': [
|
||||||
'Shlwapi.lib',
|
|
||||||
'Crypt32.lib',
|
|
||||||
'advapi32.lib',
|
'advapi32.lib',
|
||||||
'dbghelp.lib',
|
'dbghelp.lib',
|
||||||
'delayimp.lib',
|
'delayimp.lib',
|
||||||
|
|
|
@ -5,13 +5,12 @@
|
||||||
|
|
||||||
#include "browser/win/notification_presenter_win.h"
|
#include "browser/win/notification_presenter_win.h"
|
||||||
#include "base/win/windows_version.h"
|
#include "base/win/windows_version.h"
|
||||||
|
#include "common/application_info.h"
|
||||||
#include "content/public/browser/desktop_notification_delegate.h"
|
#include "content/public/browser/desktop_notification_delegate.h"
|
||||||
#include "content/public/common/platform_notification_data.h"
|
#include "content/public/common/platform_notification_data.h"
|
||||||
#include "third_party/skia/include/core/SkBitmap.h"
|
#include "third_party/skia/include/core/SkBitmap.h"
|
||||||
#include "common/application_info.h"
|
|
||||||
|
|
||||||
#pragma comment(lib, "runtimeobject.lib")
|
#pragma comment(lib, "runtimeobject.lib")
|
||||||
#pragma comment(lib, "Crypt32.lib")
|
|
||||||
|
|
||||||
using namespace WinToasts;
|
using namespace WinToasts;
|
||||||
using namespace Microsoft::WRL;
|
using namespace Microsoft::WRL;
|
||||||
|
@ -34,14 +33,13 @@ NotificationPresenterWin::~NotificationPresenterWin() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void NotificationPresenterWin::ShowNotification(
|
void NotificationPresenterWin::ShowNotification(
|
||||||
const content::PlatformNotificationData& data,
|
const content::PlatformNotificationData& data,
|
||||||
const SkBitmap& icon,
|
const SkBitmap& icon,
|
||||||
scoped_ptr<content::DesktopNotificationDelegate> delegate_ptr,
|
scoped_ptr<content::DesktopNotificationDelegate> delegate_ptr,
|
||||||
base::Closure* cancel_callback) {
|
base::Closure* cancel_callback) {
|
||||||
|
|
||||||
std::wstring title = data.title;
|
std::wstring title = data.title;
|
||||||
std::wstring body = data.body;
|
std::wstring body = data.body;
|
||||||
std::string iconPath = data.icon.spec();
|
std::string iconPath = data.icon.spec();
|
||||||
std::string appName = GetApplicationName();
|
std::string appName = GetApplicationName();
|
||||||
|
|
||||||
// toast notification supported in version >= Windows 8
|
// toast notification supported in version >= Windows 8
|
||||||
|
@ -50,7 +48,7 @@ void NotificationPresenterWin::ShowNotification(
|
||||||
wtn = new WindowsToastNotification(appName.c_str(), delegate_ptr.release());
|
wtn = new WindowsToastNotification(appName.c_str(), delegate_ptr.release());
|
||||||
wtn->ShowNotification(title.c_str(), body.c_str(), iconPath, m_lastNotification);
|
wtn->ShowNotification(title.c_str(), body.c_str(), iconPath, m_lastNotification);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cancel_callback) {
|
if (cancel_callback) {
|
||||||
*cancel_callback = base::Bind(
|
*cancel_callback = base::Bind(
|
||||||
&NotificationPresenterWin::RemoveNotification,
|
&NotificationPresenterWin::RemoveNotification,
|
||||||
|
@ -64,4 +62,4 @@ void NotificationPresenterWin::RemoveNotification() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace brightray
|
} // namespace brightray
|
||||||
|
|
|
@ -13,9 +13,8 @@
|
||||||
// windowsNotification.onclick = function () { console.log("Notification clicked") };
|
// windowsNotification.onclick = function () { console.log("Notification clicked") };
|
||||||
// windowsNotification.onclose = function () { console.log("Notification dismissed") };
|
// windowsNotification.onclose = function () { console.log("Notification dismissed") };
|
||||||
|
|
||||||
|
#ifndef BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_
|
||||||
#ifndef BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_WIN_H_
|
#define BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_
|
||||||
#define BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_WIN_H_
|
|
||||||
|
|
||||||
#include "base/compiler_specific.h"
|
#include "base/compiler_specific.h"
|
||||||
#include "browser/notification_presenter.h"
|
#include "browser/notification_presenter.h"
|
||||||
|
@ -38,7 +37,7 @@ class NotificationPresenterWin : public NotificationPresenter {
|
||||||
const SkBitmap& icon,
|
const SkBitmap& icon,
|
||||||
scoped_ptr<content::DesktopNotificationDelegate> delegate,
|
scoped_ptr<content::DesktopNotificationDelegate> delegate,
|
||||||
base::Closure* cancel_callback) override;
|
base::Closure* cancel_callback) override;
|
||||||
|
|
||||||
void RemoveNotification();
|
void RemoveNotification();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -48,4 +47,4 @@ class NotificationPresenterWin : public NotificationPresenter {
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
#endif // BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_WIN_H_
|
#endif // BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_
|
||||||
|
|
|
@ -3,100 +3,100 @@
|
||||||
// Thanks to both of those folks mentioned above who first thought up a bunch of this code
|
// 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.
|
// and released it as MIT to the world.
|
||||||
|
|
||||||
#include "windows_toast_notification.h"
|
#include "browser/win/windows_toast_notification.h"
|
||||||
|
|
||||||
#include "content/public/browser/desktop_notification_delegate.h"
|
#include "content/public/browser/desktop_notification_delegate.h"
|
||||||
|
|
||||||
using namespace WinToasts;
|
using namespace WinToasts;
|
||||||
using namespace Microsoft::WRL;
|
|
||||||
using namespace ABI::Windows::UI::Notifications;
|
|
||||||
using namespace ABI::Windows::Data::Xml::Dom;
|
using namespace ABI::Windows::Data::Xml::Dom;
|
||||||
|
|
||||||
#define BREAK_IF_BAD(hr) if(!SUCCEEDED(hr)) break;
|
#define BREAK_IF_BAD(hr) if(!SUCCEEDED(hr)) break;
|
||||||
|
|
||||||
namespace WinToasts {
|
namespace WinToasts {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
// Initialize Windows Runtime
|
// Initialize Windows Runtime
|
||||||
static HRESULT init = Windows::Foundation::Initialize(RO_INIT_MULTITHREADED);
|
HRESULT init = Windows::Foundation::Initialize(RO_INIT_MULTITHREADED);
|
||||||
|
|
||||||
WindowsToastNotification::WindowsToastNotification(const char* appName, content::DesktopNotificationDelegate* delegate)
|
} // namespace
|
||||||
{
|
|
||||||
HSTRING toastNotifMgrStr = NULL;
|
|
||||||
HSTRING appId = NULL;
|
|
||||||
|
|
||||||
HRESULT hr = CreateHString(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager, &toastNotifMgrStr);
|
WindowsToastNotification::WindowsToastNotification(
|
||||||
|
const char* appName, content::DesktopNotificationDelegate* delegate) {
|
||||||
|
HSTRING toastNotifMgrStr = nullptr;
|
||||||
|
HSTRING appId = nullptr;
|
||||||
|
|
||||||
hr = Windows::Foundation::GetActivationFactory(toastNotifMgrStr, &m_toastManager);
|
HRESULT hr = CreateHString(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager, &toastNotifMgrStr);
|
||||||
|
|
||||||
WCHAR wAppName[MAX_PATH];
|
hr = Windows::Foundation::GetActivationFactory(toastNotifMgrStr, &m_toastManager);
|
||||||
swprintf(wAppName, ARRAYSIZE(wAppName), L"%S", appName);
|
|
||||||
hr = CreateHString(wAppName, &appId);
|
|
||||||
|
|
||||||
m_toastManager->CreateToastNotifierWithId(appId, &m_toastNotifier);
|
WCHAR wAppName[MAX_PATH];
|
||||||
|
swprintf(wAppName, ARRAYSIZE(wAppName), L"%S", appName);
|
||||||
|
hr = CreateHString(wAppName, &appId);
|
||||||
|
|
||||||
if (toastNotifMgrStr != NULL) {
|
m_toastManager->CreateToastNotifierWithId(appId, &m_toastNotifier);
|
||||||
WindowsDeleteString(toastNotifMgrStr);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (appId != NULL) {
|
if (toastNotifMgrStr) {
|
||||||
WindowsDeleteString(appId);
|
WindowsDeleteString(toastNotifMgrStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
n_delegate = delegate;
|
if (appId) {
|
||||||
|
WindowsDeleteString(appId);
|
||||||
|
}
|
||||||
|
|
||||||
|
n_delegate = delegate;
|
||||||
}
|
}
|
||||||
|
|
||||||
WindowsToastNotification::~WindowsToastNotification()
|
WindowsToastNotification::~WindowsToastNotification() {
|
||||||
{
|
if (n_delegate) {
|
||||||
if (n_delegate) {
|
delete n_delegate;
|
||||||
delete n_delegate;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowsToastNotification::ShowNotification(const WCHAR* title, const WCHAR* msg, std::string iconPath, ComPtr<IToastNotification>& toast)
|
void WindowsToastNotification::ShowNotification(
|
||||||
{
|
const WCHAR* title, const WCHAR* msg, std::string iconPath, ComPtr<IToastNotification>& toast) {
|
||||||
HRESULT hr;
|
HRESULT hr;
|
||||||
HSTRING toastNotifStr = NULL;
|
HSTRING toastNotifStr = nullptr;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
ComPtr<IXmlDocument> toastXml;
|
ComPtr<IXmlDocument> toastXml;
|
||||||
hr = GetToastXml(m_toastManager.Get(), title, msg, iconPath, &toastXml);
|
hr = GetToastXml(m_toastManager.Get(), title, msg, iconPath, &toastXml);
|
||||||
BREAK_IF_BAD(hr);
|
BREAK_IF_BAD(hr);
|
||||||
|
|
||||||
hr = CreateHString(RuntimeClass_Windows_UI_Notifications_ToastNotification, &toastNotifStr);
|
hr = CreateHString(RuntimeClass_Windows_UI_Notifications_ToastNotification, &toastNotifStr);
|
||||||
BREAK_IF_BAD(hr);
|
BREAK_IF_BAD(hr);
|
||||||
|
|
||||||
ComPtr<IToastNotificationFactory> toastFactory;
|
ComPtr<IToastNotificationFactory> toastFactory;
|
||||||
hr = Windows::Foundation::GetActivationFactory(toastNotifStr, &toastFactory);
|
hr = Windows::Foundation::GetActivationFactory(toastNotifStr, &toastFactory);
|
||||||
BREAK_IF_BAD(hr);
|
BREAK_IF_BAD(hr);
|
||||||
|
|
||||||
hr = toastFactory->CreateToastNotification(toastXml.Get(), &toast);
|
hr = toastFactory->CreateToastNotification(toastXml.Get(), &toast);
|
||||||
BREAK_IF_BAD(hr);
|
BREAK_IF_BAD(hr);
|
||||||
|
|
||||||
hr = SetupCallbacks(toast.Get());
|
hr = SetupCallbacks(toast.Get());
|
||||||
BREAK_IF_BAD(hr);
|
BREAK_IF_BAD(hr);
|
||||||
|
|
||||||
hr = m_toastNotifier->Show(toast.Get());
|
hr = m_toastNotifier->Show(toast.Get());
|
||||||
BREAK_IF_BAD(hr);
|
BREAK_IF_BAD(hr);
|
||||||
|
|
||||||
n_delegate->NotificationDisplayed();
|
n_delegate->NotificationDisplayed();
|
||||||
} while (FALSE);
|
} while (FALSE);
|
||||||
|
|
||||||
if (toastNotifStr != NULL) {
|
if (toastNotifStr) {
|
||||||
WindowsDeleteString(toastNotifStr);
|
WindowsDeleteString(toastNotifStr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowsToastNotification::DismissNotification(ComPtr<IToastNotification> toast)
|
void WindowsToastNotification::DismissNotification(
|
||||||
{
|
ComPtr<IToastNotification> toast) {
|
||||||
m_toastNotifier->Hide(toast.Get());
|
m_toastNotifier->Hide(toast.Get());
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowsToastNotification::NotificationClicked()
|
void WindowsToastNotification::NotificationClicked() {
|
||||||
{
|
|
||||||
delete this;
|
delete this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowsToastNotification::NotificationDismissed()
|
void WindowsToastNotification::NotificationDismissed() {
|
||||||
{
|
|
||||||
delete this;
|
delete this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,261 +106,260 @@ HRESULT WindowsToastNotification::GetToastXml(
|
||||||
const WCHAR* msg,
|
const WCHAR* msg,
|
||||||
std::string iconPath,
|
std::string iconPath,
|
||||||
IXmlDocument** toastXml) {
|
IXmlDocument** toastXml) {
|
||||||
|
HRESULT hr;
|
||||||
HRESULT hr;
|
ToastTemplateType templateType;
|
||||||
ToastTemplateType templateType;
|
if (title == NULL || msg == NULL) {
|
||||||
if (title == NULL || msg == NULL) {
|
// Single line toast
|
||||||
// Single line toast
|
templateType = iconPath.length() == 0 ? ToastTemplateType_ToastText01 : ToastTemplateType_ToastImageAndText01;
|
||||||
templateType = iconPath.length() == 0 ? ToastTemplateType_ToastText01 : ToastTemplateType_ToastImageAndText01;
|
hr = m_toastManager->GetTemplateContent(templateType, toastXml);
|
||||||
hr = m_toastManager->GetTemplateContent(templateType, toastXml);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
const WCHAR* text = title != NULL ? title : msg;
|
|
||||||
hr = SetXmlText(*toastXml, text);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Title and body toast
|
|
||||||
templateType = iconPath.length() == 0 ? ToastTemplateType_ToastText02 : ToastTemplateType_ToastImageAndText02;
|
|
||||||
hr = toastManager->GetTemplateContent(templateType, toastXml);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
hr = SetXmlText(*toastXml, title, msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (iconPath.length() != 0 && SUCCEEDED(hr)) {
|
|
||||||
// Toast has image
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
hr = SetXmlImage(*toastXml, iconPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
do {
|
|
||||||
BREAK_IF_BAD(hr);
|
|
||||||
|
|
||||||
ComPtr<IXmlNode> node;
|
|
||||||
hr = nodeList->Item(0, &node);
|
|
||||||
BREAK_IF_BAD(hr);
|
|
||||||
|
|
||||||
hr = AppendTextToXml(doc, node.Get(), text);
|
|
||||||
} while (FALSE);
|
|
||||||
|
|
||||||
if (tag != NULL) {
|
|
||||||
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);
|
|
||||||
do {
|
|
||||||
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);
|
|
||||||
|
|
||||||
if (tag != NULL) {
|
|
||||||
WindowsDeleteString(tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
return hr;
|
|
||||||
}
|
|
||||||
|
|
||||||
HRESULT WindowsToastNotification::SetXmlImage(IXmlDocument* doc, std::string iconPath)
|
|
||||||
{
|
|
||||||
HSTRING tag = NULL;
|
|
||||||
HSTRING src = NULL;
|
|
||||||
HSTRING imgPath = NULL;
|
|
||||||
HRESULT hr = CreateHString(L"image", &tag);
|
|
||||||
|
|
||||||
do {
|
|
||||||
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];
|
|
||||||
swprintf(xmlPath, ARRAYSIZE(xmlPath), L"%S", iconPath);
|
|
||||||
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);
|
|
||||||
|
|
||||||
if (tag != NULL) {
|
|
||||||
WindowsDeleteString(tag);
|
|
||||||
}
|
|
||||||
if (src != NULL) {
|
|
||||||
WindowsDeleteString(src);
|
|
||||||
}
|
|
||||||
if (imgPath != NULL) {
|
|
||||||
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);
|
|
||||||
|
|
||||||
if (!SUCCEEDED(hr)) {
|
|
||||||
// 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);
|
|
||||||
do {
|
|
||||||
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);
|
|
||||||
|
|
||||||
if (str != NULL) {
|
|
||||||
WindowsDeleteString(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
return hr;
|
|
||||||
}
|
|
||||||
|
|
||||||
HRESULT WindowsToastNotification::SetupCallbacks(IToastNotification* toast)
|
|
||||||
{
|
|
||||||
EventRegistrationToken activatedToken, dismissedToken;
|
|
||||||
m_eventHandler = Make<ToastEventHandler>(this, n_delegate);
|
|
||||||
HRESULT hr = toast->add_Activated(m_eventHandler.Get(), &activatedToken);
|
|
||||||
|
|
||||||
if (SUCCEEDED(hr)) {
|
if (SUCCEEDED(hr)) {
|
||||||
hr = toast->add_Dismissed(m_eventHandler.Get(), &dismissedToken);
|
const WCHAR* text = title != NULL ? title : msg;
|
||||||
|
hr = SetXmlText(*toastXml, text);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Title and body toast
|
||||||
|
templateType = iconPath.length() == 0 ? ToastTemplateType_ToastText02 : ToastTemplateType_ToastImageAndText02;
|
||||||
|
hr = toastManager->GetTemplateContent(templateType, toastXml);
|
||||||
|
if (SUCCEEDED(hr)) {
|
||||||
|
hr = SetXmlText(*toastXml, title, msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (iconPath.length() != 0 && SUCCEEDED(hr)) {
|
||||||
|
// Toast has image
|
||||||
|
if (SUCCEEDED(hr)) {
|
||||||
|
hr = SetXmlImage(*toastXml, iconPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
return hr;
|
// 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::CreateHString(const WCHAR* source, HSTRING* dest)
|
HRESULT WindowsToastNotification::SetXmlText(
|
||||||
{
|
IXmlDocument* doc, const WCHAR* text) {
|
||||||
if (source == NULL || dest == NULL) {
|
HSTRING tag = NULL;
|
||||||
return E_INVALIDARG;
|
|
||||||
}
|
|
||||||
|
|
||||||
HRESULT hr = WindowsCreateString(source, wcslen(source), dest);
|
ComPtr<IXmlNodeList> nodeList;
|
||||||
return hr;
|
HRESULT hr = GetTextNodeList(&tag, doc, &nodeList, 1);
|
||||||
|
do {
|
||||||
|
BREAK_IF_BAD(hr);
|
||||||
|
|
||||||
|
ComPtr<IXmlNode> node;
|
||||||
|
hr = nodeList->Item(0, &node);
|
||||||
|
BREAK_IF_BAD(hr);
|
||||||
|
|
||||||
|
hr = AppendTextToXml(doc, node.Get(), text);
|
||||||
|
} while (FALSE);
|
||||||
|
|
||||||
|
if (tag != NULL) {
|
||||||
|
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);
|
||||||
|
do {
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (tag != NULL) {
|
||||||
|
WindowsDeleteString(tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
return hr;
|
||||||
|
}
|
||||||
|
|
||||||
|
HRESULT WindowsToastNotification::SetXmlImage(
|
||||||
|
IXmlDocument* doc, std::string iconPath) {
|
||||||
|
HSTRING tag = NULL;
|
||||||
|
HSTRING src = NULL;
|
||||||
|
HSTRING imgPath = NULL;
|
||||||
|
HRESULT hr = CreateHString(L"image", &tag);
|
||||||
|
|
||||||
|
do {
|
||||||
|
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];
|
||||||
|
swprintf(xmlPath, ARRAYSIZE(xmlPath), L"%S", iconPath);
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (tag != NULL) {
|
||||||
|
WindowsDeleteString(tag);
|
||||||
|
}
|
||||||
|
if (src != NULL) {
|
||||||
|
WindowsDeleteString(src);
|
||||||
|
}
|
||||||
|
if (imgPath != NULL) {
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (!SUCCEEDED(hr)) {
|
||||||
|
// 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);
|
||||||
|
do {
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (str != NULL) {
|
||||||
|
WindowsDeleteString(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
return hr;
|
||||||
|
}
|
||||||
|
|
||||||
|
HRESULT WindowsToastNotification::SetupCallbacks(IToastNotification* toast) {
|
||||||
|
EventRegistrationToken activatedToken, dismissedToken;
|
||||||
|
m_eventHandler = Make<ToastEventHandler>(this, n_delegate);
|
||||||
|
HRESULT hr = toast->add_Activated(m_eventHandler.Get(), &activatedToken);
|
||||||
|
|
||||||
|
if (SUCCEEDED(hr)) {
|
||||||
|
hr = toast->add_Dismissed(m_eventHandler.Get(), &dismissedToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
/ Toast Event Handler
|
/ Toast Event Handler
|
||||||
*/
|
*/
|
||||||
ToastEventHandler::ToastEventHandler(WindowsToastNotification* notification, content::DesktopNotificationDelegate* delegate)
|
ToastEventHandler::ToastEventHandler(
|
||||||
{
|
WindowsToastNotification* notification,
|
||||||
m_notification = notification;
|
content::DesktopNotificationDelegate* delegate) {
|
||||||
n_delegate = delegate;
|
m_notification = notification;
|
||||||
|
n_delegate = delegate;
|
||||||
}
|
}
|
||||||
|
|
||||||
ToastEventHandler::~ToastEventHandler()
|
ToastEventHandler::~ToastEventHandler() {
|
||||||
{
|
|
||||||
// Empty
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IFACEMETHODIMP ToastEventHandler::Invoke(IToastNotification* sender, IInspectable* args)
|
IFACEMETHODIMP ToastEventHandler::Invoke(
|
||||||
{
|
IToastNotification* sender, IInspectable* args) {
|
||||||
// Notification "activated" (clicked)
|
// Notification "activated" (clicked)
|
||||||
n_delegate->NotificationClick();
|
n_delegate->NotificationClick();
|
||||||
|
|
||||||
if (m_notification != NULL) {
|
if (m_notification != NULL) {
|
||||||
m_notification->NotificationClicked();
|
m_notification->NotificationClicked();
|
||||||
}
|
}
|
||||||
|
|
||||||
return S_OK;
|
return S_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
IFACEMETHODIMP ToastEventHandler::Invoke(IToastNotification* sender, IToastDismissedEventArgs* e)
|
IFACEMETHODIMP ToastEventHandler::Invoke(
|
||||||
{
|
IToastNotification* sender, IToastDismissedEventArgs* e) {
|
||||||
// Notification dismissed
|
// Notification dismissed
|
||||||
n_delegate->NotificationClosed();
|
n_delegate->NotificationClosed();
|
||||||
|
|
||||||
if (m_notification != NULL) {
|
if (m_notification != NULL) {
|
||||||
m_notification->NotificationDismissed();
|
m_notification->NotificationDismissed();
|
||||||
|
}
|
||||||
}
|
|
||||||
|
return S_OK;
|
||||||
return S_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} //namespace
|
} //namespace
|
||||||
|
|
|
@ -3,70 +3,73 @@
|
||||||
// Thanks to both of those folks mentioned above who first thought up a bunch of this code
|
// 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.
|
// and released it as MIT to the world.
|
||||||
|
|
||||||
#ifndef __WINDOWS_TOAST_NOTIFICATION_H__
|
#ifndef BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_
|
||||||
#define __WINDOWS_TOAST_NOTIFICATION_H__
|
#define BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_
|
||||||
|
|
||||||
#include "content/public/browser/desktop_notification_delegate.h"
|
|
||||||
#include "content/public/common/platform_notification_data.h"
|
|
||||||
#include "base/bind.h"
|
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <windows.ui.notifications.h>
|
#include <windows.ui.notifications.h>
|
||||||
#include <wrl/implements.h>
|
#include <wrl/implements.h>
|
||||||
|
|
||||||
|
#include "base/bind.h"
|
||||||
|
#include "content/public/browser/desktop_notification_delegate.h"
|
||||||
|
#include "content/public/common/platform_notification_data.h"
|
||||||
|
|
||||||
using namespace Microsoft::WRL;
|
using namespace Microsoft::WRL;
|
||||||
using namespace ABI::Windows::UI::Notifications;
|
using namespace ABI::Windows::UI::Notifications;
|
||||||
using namespace ABI::Windows::Foundation;
|
using namespace ABI::Windows::Foundation;
|
||||||
|
|
||||||
namespace WinToasts {
|
namespace WinToasts {
|
||||||
|
|
||||||
typedef ITypedEventHandler<ToastNotification*, IInspectable*> DesktopToastActivatedEventHandler;
|
using DesktopToastActivatedEventHandler =
|
||||||
typedef ITypedEventHandler<ToastNotification*, ToastDismissedEventArgs*> DesktopToastDismissedEventHandler;
|
ITypedEventHandler<ToastNotification*, IInspectable*>;
|
||||||
|
using DesktopToastDismissedEventHandler =
|
||||||
|
ITypedEventHandler<ToastNotification*, ToastDismissedEventArgs*>;
|
||||||
|
|
||||||
class ToastEventHandler;
|
class ToastEventHandler;
|
||||||
|
|
||||||
class WindowsToastNotification
|
class WindowsToastNotification {
|
||||||
{
|
public:
|
||||||
public:
|
WindowsToastNotification(const char* appName, content::DesktopNotificationDelegate* delegate);
|
||||||
WindowsToastNotification(const char* appName, content::DesktopNotificationDelegate* delegate);
|
~WindowsToastNotification();
|
||||||
~WindowsToastNotification();
|
|
||||||
void ShowNotification(const WCHAR* title, const WCHAR* msg, std::string iconPath, ComPtr<IToastNotification>& toast);
|
|
||||||
void DismissNotification(ComPtr<IToastNotification> toast);
|
|
||||||
void NotificationClicked();
|
|
||||||
void NotificationDismissed();
|
|
||||||
|
|
||||||
private:
|
void ShowNotification(const WCHAR* title, const WCHAR* msg, std::string iconPath, ComPtr<IToastNotification>& toast);
|
||||||
ComPtr<ToastEventHandler> m_eventHandler;
|
void DismissNotification(ComPtr<IToastNotification> toast);
|
||||||
|
void NotificationClicked();
|
||||||
|
void NotificationDismissed();
|
||||||
|
|
||||||
content::DesktopNotificationDelegate* n_delegate;
|
private:
|
||||||
ComPtr<IToastNotificationManagerStatics> m_toastManager;
|
ComPtr<ToastEventHandler> m_eventHandler;
|
||||||
ComPtr<IToastNotifier> m_toastNotifier;
|
|
||||||
|
|
||||||
HRESULT GetToastXml(IToastNotificationManagerStatics* toastManager, const WCHAR* title, const WCHAR* msg, std::string iconPath, ABI::Windows::Data::Xml::Dom::IXmlDocument** toastXml);
|
content::DesktopNotificationDelegate* n_delegate;
|
||||||
HRESULT SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, const WCHAR* text);
|
ComPtr<IToastNotificationManagerStatics> m_toastManager;
|
||||||
HRESULT SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, const WCHAR* title, const WCHAR* body);
|
ComPtr<IToastNotifier> m_toastNotifier;
|
||||||
HRESULT SetXmlImage(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, std::string iconPath);
|
|
||||||
HRESULT GetTextNodeList(HSTRING* tag, ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, ABI::Windows::Data::Xml::Dom::IXmlNodeList** nodeList, UINT32 reqLength);
|
HRESULT GetToastXml(IToastNotificationManagerStatics* toastManager, const WCHAR* title, const WCHAR* msg, std::string iconPath, ABI::Windows::Data::Xml::Dom::IXmlDocument** toastXml);
|
||||||
HRESULT AppendTextToXml(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, ABI::Windows::Data::Xml::Dom::IXmlNode* node, const WCHAR* text);
|
HRESULT SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, const WCHAR* text);
|
||||||
HRESULT SetupCallbacks(IToastNotification* toast);
|
HRESULT SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, const WCHAR* title, const WCHAR* body);
|
||||||
HRESULT CreateHString(const WCHAR* source, HSTRING* dest);
|
HRESULT SetXmlImage(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, std::string iconPath);
|
||||||
};
|
HRESULT GetTextNodeList(HSTRING* tag, ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, ABI::Windows::Data::Xml::Dom::IXmlNodeList** nodeList, UINT32 reqLength);
|
||||||
|
HRESULT AppendTextToXml(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, ABI::Windows::Data::Xml::Dom::IXmlNode* node, const WCHAR* text);
|
||||||
|
HRESULT SetupCallbacks(IToastNotification* toast);
|
||||||
|
HRESULT CreateHString(const WCHAR* source, HSTRING* dest);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class ToastEventHandler :
|
class ToastEventHandler : public RuntimeClass<RuntimeClassFlags<ClassicCom>,
|
||||||
public RuntimeClass<RuntimeClassFlags<ClassicCom>, DesktopToastActivatedEventHandler, DesktopToastDismissedEventHandler>
|
DesktopToastActivatedEventHandler,
|
||||||
{
|
DesktopToastDismissedEventHandler> {
|
||||||
public:
|
public:
|
||||||
ToastEventHandler(WindowsToastNotification* notification, content::DesktopNotificationDelegate* delegate);
|
ToastEventHandler(WindowsToastNotification* notification, content::DesktopNotificationDelegate* delegate);
|
||||||
~ToastEventHandler();
|
~ToastEventHandler();
|
||||||
IFACEMETHODIMP Invoke(IToastNotification* sender, IInspectable* args);
|
|
||||||
IFACEMETHODIMP Invoke(IToastNotification* sender, IToastDismissedEventArgs* e);
|
|
||||||
|
|
||||||
private:
|
IFACEMETHODIMP Invoke(IToastNotification* sender, IInspectable* args);
|
||||||
WindowsToastNotification* m_notification;
|
IFACEMETHODIMP Invoke(IToastNotification* sender, IToastDismissedEventArgs* e);
|
||||||
content::DesktopNotificationDelegate* n_delegate;
|
|
||||||
};
|
private:
|
||||||
|
WindowsToastNotification* m_notification;
|
||||||
|
content::DesktopNotificationDelegate* n_delegate;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
#endif //__WINDOWS_TOAST_NOTIFICATION_H__
|
#endif // BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue