Merge pull request #167 from atom/code-cleanup

Cleanup of the notification code
This commit is contained in:
Cheng Zhao 2015-11-10 20:31:29 +08:00
commit da0197543a
9 changed files with 373 additions and 382 deletions

View file

@ -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',

View file

@ -24,13 +24,13 @@ PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() {}
PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {}
NotificationPresenter* PlatformNotificationServiceImpl::notification_presenter() { NotificationPresenter* PlatformNotificationServiceImpl::notification_presenter() {
#if defined(OS_WIN)
// Bail out if on Windows 7 or even lower, no operating will follow
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return nullptr;
#endif
if (!notification_presenter_) { if (!notification_presenter_) {
#if defined(OS_WIN)
// Bail out if on Windows 7 or even lower, no operating will follow
if (base::win::GetVersion() < base::win::VERSION_WIN8) {
return notification_presenter_.get();
}
#endif
// Create a new presenter if on OS X, Linux, or Windows 8+ // Create a new presenter if on OS X, Linux, or Windows 8+
notification_presenter_.reset(NotificationPresenter::Create()); notification_presenter_.reset(NotificationPresenter::Create());
} }

View file

@ -4,16 +4,16 @@
// found in the LICENSE-CHROMIUM file. // found in the LICENSE-CHROMIUM file.
#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 "browser/win/windows_toast_notification.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 Microsoft::WRL; using namespace Microsoft::WRL;
using namespace ABI::Windows::UI::Notifications; using namespace ABI::Windows::UI::Notifications;
using namespace ABI::Windows::Data::Xml::Dom; using namespace ABI::Windows::Data::Xml::Dom;
@ -21,47 +21,40 @@ using namespace ABI::Windows::Foundation;
namespace brightray { namespace brightray {
namespace {
void RemoveNotification(base::WeakPtr<WindowsToastNotification> notification) {
if (notification)
notification->DismissNotification();
}
} // namespace
// static // static
NotificationPresenter* NotificationPresenter::Create() { NotificationPresenter* NotificationPresenter::Create() {
return new NotificationPresenterWin; return new NotificationPresenterWin;
} }
NotificationPresenterWin::NotificationPresenterWin() { NotificationPresenterWin::NotificationPresenterWin() {
m_lastNotification = nullptr;
} }
NotificationPresenterWin::~NotificationPresenterWin() { 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,
base::Closure* cancel_callback) { base::Closure* cancel_callback) {
// This class manages itself.
std::wstring title = data.title; auto notification = new WindowsToastNotification(
std::wstring body = data.body; GetApplicationName(), delegate.Pass());
std::string iconPath = data.icon.spec(); notification->ShowNotification(data.title, data.body, data.icon.spec());
std::string appName = GetApplicationName();
// toast notification supported in version >= Windows 8
// for prior versions, use Tray.displayBalloon
if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
wtn = new WindowsToastNotification(appName.c_str(), delegate_ptr.release());
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, &RemoveNotification, notification->GetWeakPtr());
base::Unretained(this));
} }
} }
void NotificationPresenterWin::RemoveNotification() { } // namespace brightray
if (m_lastNotification != nullptr && wtn != NULL) {
wtn->DismissNotification(m_lastNotification);
}
}
} // namespace brightray

View file

@ -13,18 +13,10 @@
// 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_
#define BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_
#ifndef BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_WIN_H_
#define BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_WIN_H_
#include "base/compiler_specific.h"
#include "browser/notification_presenter.h" #include "browser/notification_presenter.h"
#include "windows_toast_notification.h"
#include <windows.h>
#include <windows.ui.notifications.h>
#include <wrl/client.h>
#include <wrl/implements.h>
namespace brightray { namespace brightray {
@ -38,14 +30,11 @@ 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();
private: private:
WinToasts::WindowsToastNotification* wtn; DISALLOW_COPY_AND_ASSIGN(NotificationPresenterWin);
Microsoft::WRL::ComPtr<ABI::Windows::UI::Notifications::IToastNotification> m_lastNotification;
}; };
} // namespace } // namespace
#endif // BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_WIN_H_ #endif // BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_

View file

@ -0,0 +1,33 @@
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#include "browser/win/scoped_hstring.h"
#include <winstring.h>
ScopedHString::ScopedHString(const wchar_t* source)
: str_(nullptr) {
Set(source);
}
ScopedHString::ScopedHString(const std::wstring& source)
: str_(nullptr) {
WindowsCreateString(source.c_str(), source.length(), &str_);
}
ScopedHString::ScopedHString() : str_(nullptr) {
}
ScopedHString::~ScopedHString() {
if (str_)
WindowsDeleteString(str_);
}
void ScopedHString::Set(const wchar_t* source) {
if (str_) {
WindowsDeleteString(str_);
str_ = nullptr;
}
WindowsCreateString(source, wcslen(source), &str_);
}

View file

@ -0,0 +1,39 @@
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#ifndef BRIGHTRAY_BROWSER_WIN_SCOPED_HSTRING_H_
#define BRIGHTRAY_BROWSER_WIN_SCOPED_HSTRING_H_
#include <string>
#include <hstring.h>
#include <windows.h>
#include "base/basictypes.h"
class ScopedHString {
public:
// Copy from |source|.
ScopedHString(const wchar_t* source);
ScopedHString(const std::wstring& source);
// Create empty string.
ScopedHString();
~ScopedHString();
// Sets to |source|.
void Set(const wchar_t* source);
// Returns string.
operator HSTRING() const { return str_; }
// Whether there is a string created.
bool success() const { return str_; }
private:
HSTRING str_;
DISALLOW_COPY_AND_ASSIGN(ScopedHString);
};
#endif // BRIGHTRAY_BROWSER_WIN_SCOPED_HSTRING_H_

View file

@ -3,364 +3,266 @@
// 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 "base/strings/utf_string_conversions.h"
#include "browser/win/scoped_hstring.h"
#include "content/public/browser/desktop_notification_delegate.h" #include "content/public/browser/desktop_notification_delegate.h"
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; namespace brightray {
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 std::string& app_name,
scoped_ptr<content::DesktopNotificationDelegate> delegate)
: delegate_(delegate.Pass()),
weak_factory_(this) {
ScopedHString toast_manager_str(
RuntimeClass_Windows_UI_Notifications_ToastNotificationManager);
if (!toast_manager_str.success())
return;
HRESULT hr = Windows::Foundation::GetActivationFactory(
toast_manager_str, &toast_manager_);
if (FAILED(hr))
return;
hr = Windows::Foundation::GetActivationFactory(toastNotifMgrStr, &m_toastManager); ScopedHString app_id(base::UTF8ToUTF16(app_name).c_str());
if (!app_id.success())
return;
WCHAR wAppName[MAX_PATH]; toast_manager_->CreateToastNotifierWithId(app_id, &toast_notifier_);
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);
}
n_delegate = delegate;
} }
WindowsToastNotification::~WindowsToastNotification() WindowsToastNotification::~WindowsToastNotification() {
{
if (n_delegate) {
delete n_delegate;
}
} }
void WindowsToastNotification::ShowNotification(const WCHAR* title, const WCHAR* msg, std::string iconPath, ComPtr<IToastNotification>& toast) void WindowsToastNotification::ShowNotification(
{ const std::wstring& title,
HRESULT hr; const std::wstring& msg,
HSTRING toastNotifStr = NULL; std::string icon_path) {
ComPtr<IXmlDocument> toast_xml;
if(FAILED(GetToastXml(toast_manager_.Get(), title, msg, icon_path, &toast_xml)))
return;
do { ScopedHString toast_str(
ComPtr<IXmlDocument> toastXml; RuntimeClass_Windows_UI_Notifications_ToastNotification);
hr = GetToastXml(m_toastManager.Get(), title, msg, iconPath, &toastXml); if (!toast_str.success())
BREAK_IF_BAD(hr); return;
hr = CreateHString(RuntimeClass_Windows_UI_Notifications_ToastNotification, &toastNotifStr); ComPtr<IToastNotificationFactory> toast_factory;
BREAK_IF_BAD(hr); if (FAILED(Windows::Foundation::GetActivationFactory(toast_str,
&toast_factory)))
return;
ComPtr<IToastNotificationFactory> toastFactory; if (FAILED(toast_factory->CreateToastNotification(toast_xml.Get(),
hr = Windows::Foundation::GetActivationFactory(toastNotifStr, &toastFactory); &toast_notification_)))
BREAK_IF_BAD(hr); return;
hr = toastFactory->CreateToastNotification(toastXml.Get(), &toast); if (FAILED(SetupCallbacks(toast_notification_.Get())))
BREAK_IF_BAD(hr); return;
hr = SetupCallbacks(toast.Get()); if (FAILED(toast_notifier_->Show(toast_notification_.Get())))
BREAK_IF_BAD(hr); return;
hr = m_toastNotifier->Show(toast.Get()); delegate_->NotificationDisplayed();
BREAK_IF_BAD(hr);
n_delegate->NotificationDisplayed();
} while (FALSE);
if (toastNotifStr != NULL) {
WindowsDeleteString(toastNotifStr);
}
} }
void WindowsToastNotification::DismissNotification(ComPtr<IToastNotification> toast) void WindowsToastNotification::DismissNotification() {
{ toast_notifier_->Hide(toast_notification_.Get());
m_toastNotifier->Hide(toast.Get());
} }
void WindowsToastNotification::NotificationClicked() void WindowsToastNotification::NotificationClicked() {
{ delegate_->NotificationClick();
delete this; delete this;
} }
void WindowsToastNotification::NotificationDismissed() void WindowsToastNotification::NotificationDismissed() {
{ delegate_->NotificationClosed();
delete this; delete this;
} }
HRESULT WindowsToastNotification::GetToastXml( bool WindowsToastNotification::GetToastXml(
IToastNotificationManagerStatics* toastManager, IToastNotificationManagerStatics* toastManager,
const WCHAR* title, const std::wstring& title,
const WCHAR* msg, const std::wstring& msg,
std::string iconPath, std::string icon_path,
IXmlDocument** toastXml) { IXmlDocument** toast_xml) {
ToastTemplateType template_type;
HRESULT hr; if (title.empty() || msg.empty()) {
ToastTemplateType templateType; // Single line toast.
if (title == NULL || msg == NULL) { template_type = icon_path.empty() ? ToastTemplateType_ToastText01 :
// Single line toast ToastTemplateType_ToastImageAndText01;
templateType = iconPath.length() == 0 ? ToastTemplateType_ToastText01 : ToastTemplateType_ToastImageAndText01; if (FAILED(toast_manager_->GetTemplateContent(template_type, toast_xml)))
hr = m_toastManager->GetTemplateContent(templateType, toastXml); return false;
if (SUCCEEDED(hr)) { if (!SetXmlText(*toast_xml, title.empty() ? msg : title))
const WCHAR* text = title != NULL ? title : msg; return false;
hr = SetXmlText(*toastXml, text); } else {
} // Title and body toast.
} else { template_type = icon_path.empty() ? ToastTemplateType_ToastText02 :
// Title and body toast ToastTemplateType_ToastImageAndText02;
templateType = iconPath.length() == 0 ? ToastTemplateType_ToastText02 : ToastTemplateType_ToastImageAndText02; if (FAILED(toastManager->GetTemplateContent(template_type, toast_xml)))
hr = toastManager->GetTemplateContent(templateType, toastXml); return false;
if (SUCCEEDED(hr)) { if (!SetXmlText(*toast_xml, title, msg))
hr = SetXmlText(*toastXml, title, msg); return false;
} }
}
if (iconPath.length() != 0 && SUCCEEDED(hr)) { // Toast has image
// Toast has image if (!icon_path.empty())
if (SUCCEEDED(hr)) { return SetXmlImage(*toast_xml, icon_path);
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. return true;
hr = S_OK;
}
return hr;
} }
HRESULT WindowsToastNotification::SetXmlText(IXmlDocument* doc, const WCHAR* text) bool WindowsToastNotification::SetXmlText(
{ IXmlDocument* doc, const std::wstring& text) {
HSTRING tag = NULL; ScopedHString tag;
ComPtr<IXmlNodeList> node_list;
if (!GetTextNodeList(&tag, doc, &node_list, 1))
return false;
ComPtr<IXmlNodeList> nodeList; ComPtr<IXmlNode> node;
HRESULT hr = GetTextNodeList(&tag, doc, &nodeList, 1); if (FAILED(node_list->Item(0, &node)))
do { return false;
BREAK_IF_BAD(hr);
ComPtr<IXmlNode> node; return AppendTextToXml(doc, node.Get(), text);
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) bool WindowsToastNotification::SetXmlText(
{ IXmlDocument* doc, const std::wstring& title, const std::wstring& body) {
HSTRING tag = NULL; ScopedHString tag;
ComPtr<IXmlNodeList> nodeList; ComPtr<IXmlNodeList> node_list;
HRESULT hr = GetTextNodeList(&tag, doc, &nodeList, 2); if (!GetTextNodeList(&tag, doc, &node_list, 2))
do { return false;
BREAK_IF_BAD(hr);
ComPtr<IXmlNode> node; ComPtr<IXmlNode> node;
hr = nodeList->Item(0, &node); if (FAILED(node_list->Item(0, &node)))
BREAK_IF_BAD(hr); return false;
hr = AppendTextToXml(doc, node.Get(), title); if (!AppendTextToXml(doc, node.Get(), title))
BREAK_IF_BAD(hr); return false;
hr = nodeList->Item(1, &node); if (FAILED(node_list->Item(1, &node)))
BREAK_IF_BAD(hr); return false;
hr = AppendTextToXml(doc, node.Get(), body); return AppendTextToXml(doc, node.Get(), body);
} while (FALSE);
if (tag != NULL) {
WindowsDeleteString(tag);
}
return hr;
} }
HRESULT WindowsToastNotification::SetXmlImage(IXmlDocument* doc, std::string iconPath) bool WindowsToastNotification::SetXmlImage(
{ IXmlDocument* doc, std::string icon_path) {
HSTRING tag = NULL; ScopedHString tag(L"imag");
HSTRING src = NULL; if (!tag.success())
HSTRING imgPath = NULL; return false;
HRESULT hr = CreateHString(L"image", &tag);
do { ComPtr<IXmlNodeList> node_list;
BREAK_IF_BAD(hr); if (FAILED(doc->GetElementsByTagName(tag, &node_list)))
return false;
ComPtr<IXmlNodeList> nodeList; ComPtr<IXmlNode> image_node;
hr = doc->GetElementsByTagName(tag, &nodeList); if (FAILED(node_list->Item(0, &image_node)))
BREAK_IF_BAD(hr); return false;
ComPtr<IXmlNode> imageNode; ComPtr<IXmlNamedNodeMap> attrs;
hr = nodeList->Item(0, &imageNode); if (FAILED(image_node->get_Attributes(&attrs)))
BREAK_IF_BAD(hr); return false;
ComPtr<IXmlNamedNodeMap> attrs; ScopedHString src(L"src");
hr = imageNode->get_Attributes(&attrs); if (!src.success())
BREAK_IF_BAD(hr); return false;
hr = CreateHString(L"src", &src); ComPtr<IXmlNode> src_attr;
BREAK_IF_BAD(hr); if (FAILED(attrs->GetNamedItem(src, &src_attr)))
return false;
ComPtr<IXmlNode> srcAttr; ScopedHString img_path(base::UTF8ToUTF16(icon_path).c_str());
hr = attrs->GetNamedItem(src, &srcAttr); if (!img_path.success())
BREAK_IF_BAD(hr); return false;
WCHAR xmlPath[MAX_PATH]; ComPtr<IXmlText> src_text;
swprintf(xmlPath, ARRAYSIZE(xmlPath), L"%S", iconPath); if (FAILED(doc->CreateTextNode(img_path, &src_text)))
hr = CreateHString(xmlPath, &imgPath); return false;
BREAK_IF_BAD(hr);
ComPtr<IXmlText> srcText; ComPtr<IXmlNode> src_node;
hr = doc->CreateTextNode(imgPath, &srcText); if (FAILED(src_text.As(&src_node)))
BREAK_IF_BAD(hr); return false;
ComPtr<IXmlNode> srcNode; ComPtr<IXmlNode> child_node;
hr = srcText.As(&srcNode); return SUCCEEDED(src_attr->AppendChild(src_node.Get(), &child_node));
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) bool WindowsToastNotification::GetTextNodeList(
{ ScopedHString* tag,
HRESULT hr = CreateHString(L"text", tag); IXmlDocument* doc,
do{ IXmlNodeList** node_list,
BREAK_IF_BAD(hr); UINT32 req_length) {
tag->Set(L"text");
if (!tag->success())
return false;
hr = doc->GetElementsByTagName(*tag, nodeList); if (FAILED(doc->GetElementsByTagName(*tag, node_list)))
BREAK_IF_BAD(hr); return false;
UINT32 nodeLength; UINT32 node_length;
hr = (*nodeList)->get_Length(&nodeLength); if (FAILED((*node_list)->get_Length(&node_length)))
BREAK_IF_BAD(hr); return false;
if (nodeLength < reqLength) { return node_length >= req_length;
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) bool WindowsToastNotification::AppendTextToXml(
{ IXmlDocument* doc, IXmlNode* node, const std::wstring& text) {
HSTRING str = NULL; ScopedHString str(text);
HRESULT hr = CreateHString(text, &str); if (!str.success())
do { return false;
BREAK_IF_BAD(hr);
ComPtr<IXmlText> xmlText; ComPtr<IXmlText> xml_text;
hr = doc->CreateTextNode(str, &xmlText); if (FAILED(doc->CreateTextNode(str, &xml_text)))
BREAK_IF_BAD(hr); return false;
ComPtr<IXmlNode> textNode; ComPtr<IXmlNode> text_node;
hr = xmlText.As(&textNode); if (FAILED(xml_text.As(&text_node)))
BREAK_IF_BAD(hr); return false;
ComPtr<IXmlNode> appendNode; ComPtr<IXmlNode> append_node;
hr = node->AppendChild(textNode.Get(), &appendNode); return SUCCEEDED(node->AppendChild(text_node.Get(), &append_node));
} while (FALSE);
if (str != NULL) {
WindowsDeleteString(str);
}
return hr;
} }
HRESULT WindowsToastNotification::SetupCallbacks(IToastNotification* toast) bool WindowsToastNotification::SetupCallbacks(IToastNotification* toast) {
{ EventRegistrationToken activatedToken, dismissedToken;
EventRegistrationToken activatedToken, dismissedToken; event_handler_ = Make<ToastEventHandler>(this);
m_eventHandler = Make<ToastEventHandler>(this, n_delegate); if (FAILED(toast->add_Activated(event_handler_.Get(), &activatedToken)))
HRESULT hr = toast->add_Activated(m_eventHandler.Get(), &activatedToken); return false;
if (SUCCEEDED(hr)) { return SUCCEEDED(toast->add_Dismissed(event_handler_.Get(), &dismissedToken));
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)
{ : notification_(notification) {
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_->NotificationClicked();
n_delegate->NotificationClick(); return S_OK;
if (m_notification != NULL) {
m_notification->NotificationClicked();
}
return S_OK;
} }
IFACEMETHODIMP ToastEventHandler::Invoke(IToastNotification* sender, IToastDismissedEventArgs* e) IFACEMETHODIMP ToastEventHandler::Invoke(
{ IToastNotification* sender, IToastDismissedEventArgs* e) {
// Notification dismissed notification_->NotificationDismissed();
n_delegate->NotificationClosed(); return S_OK;
if (m_notification != NULL) {
m_notification->NotificationDismissed();
}
return S_OK;
} }
} //namespace } // namespace brightray

View file

@ -3,70 +3,102 @@
// 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 "base/memory/weak_ptr.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 { class ScopedHString;
typedef ITypedEventHandler<ToastNotification*, IInspectable*> DesktopToastActivatedEventHandler; namespace brightray {
typedef ITypedEventHandler<ToastNotification*, ToastDismissedEventArgs*> DesktopToastDismissedEventHandler;
class ToastEventHandler; using DesktopToastActivatedEventHandler =
ITypedEventHandler<ToastNotification*, IInspectable*>;
using DesktopToastDismissedEventHandler =
ITypedEventHandler<ToastNotification*, ToastDismissedEventArgs*>;
class WindowsToastNotification class WindowsToastNotification {
{ public:
public: WindowsToastNotification(
WindowsToastNotification(const char* appName, content::DesktopNotificationDelegate* delegate); const std::string& app_name,
~WindowsToastNotification(); scoped_ptr<content::DesktopNotificationDelegate> delegate);
void ShowNotification(const WCHAR* title, const WCHAR* msg, std::string iconPath, ComPtr<IToastNotification>& toast); ~WindowsToastNotification();
void DismissNotification(ComPtr<IToastNotification> toast);
void NotificationClicked();
void NotificationDismissed();
private: void ShowNotification(const std::wstring& title,
ComPtr<ToastEventHandler> m_eventHandler; const std::wstring& msg,
std::string icon_path);
void DismissNotification();
content::DesktopNotificationDelegate* n_delegate; base::WeakPtr<WindowsToastNotification> GetWeakPtr() {
ComPtr<IToastNotificationManagerStatics> m_toastManager; return weak_factory_.GetWeakPtr();
ComPtr<IToastNotifier> m_toastNotifier; }
HRESULT GetToastXml(IToastNotificationManagerStatics* toastManager, const WCHAR* title, const WCHAR* msg, std::string iconPath, ABI::Windows::Data::Xml::Dom::IXmlDocument** toastXml); private:
HRESULT SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, const WCHAR* text); friend class ToastEventHandler;
HRESULT SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, const WCHAR* title, const WCHAR* body);
HRESULT SetXmlImage(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, std::string iconPath); void NotificationClicked();
HRESULT GetTextNodeList(HSTRING* tag, ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, ABI::Windows::Data::Xml::Dom::IXmlNodeList** nodeList, UINT32 reqLength); void NotificationDismissed();
HRESULT AppendTextToXml(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, ABI::Windows::Data::Xml::Dom::IXmlNode* node, const WCHAR* text);
HRESULT SetupCallbacks(IToastNotification* toast); bool GetToastXml(IToastNotificationManagerStatics* toastManager,
HRESULT CreateHString(const WCHAR* source, HSTRING* dest); const std::wstring& title,
}; const std::wstring& msg,
std::string icon_path,
ABI::Windows::Data::Xml::Dom::IXmlDocument** toastXml);
bool SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
const std::wstring& text);
bool SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
const std::wstring& title,
const std::wstring& body);
bool SetXmlImage(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
std::string icon_path);
bool GetTextNodeList(ScopedHString* tag,
ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
ABI::Windows::Data::Xml::Dom::IXmlNodeList** nodeList,
UINT32 reqLength);
bool AppendTextToXml(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
ABI::Windows::Data::Xml::Dom::IXmlNode* node,
const std::wstring& text);
bool SetupCallbacks(IToastNotification* toast);
scoped_ptr<content::DesktopNotificationDelegate> delegate_;
ComPtr<ToastEventHandler> event_handler_;
ComPtr<IToastNotificationManagerStatics> toast_manager_;
ComPtr<IToastNotifier> toast_notifier_;
ComPtr<IToastNotification> toast_notification_;
base::WeakPtrFactory<WindowsToastNotification> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(WindowsToastNotification);
};
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);
~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;
};
} // namespace private:
WindowsToastNotification* notification_; // weak ref.
#endif //__WINDOWS_TOAST_NOTIFICATION_H__ DISALLOW_COPY_AND_ASSIGN(ToastEventHandler);
};
} // namespace brightray
#endif // BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_

View file

@ -67,6 +67,8 @@
'browser/win/notification_presenter_win.cc', 'browser/win/notification_presenter_win.cc',
'browser/win/windows_toast_notification.h', 'browser/win/windows_toast_notification.h',
'browser/win/windows_toast_notification.cc', 'browser/win/windows_toast_notification.cc',
'browser/win/scoped_hstring.h',
'browser/win/scoped_hstring.cc',
'browser/special_storage_policy.cc', 'browser/special_storage_policy.cc',
'browser/special_storage_policy.h', 'browser/special_storage_policy.h',
'browser/url_request_context_getter.cc', 'browser/url_request_context_getter.cc',