Make the toast type really work

This commit is contained in:
Cheng Zhao 2015-11-10 20:07:12 +08:00
parent 6b9371c4cd
commit 4f73de0930
6 changed files with 53 additions and 32 deletions

View file

@ -4,7 +4,9 @@
// found in the LICENSE-CHROMIUM file.
#include "browser/win/notification_presenter_win.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/common/platform_notification_data.h"
@ -12,7 +14,6 @@
#pragma comment(lib, "runtimeobject.lib")
using namespace WinToasts;
using namespace Microsoft::WRL;
using namespace ABI::Windows::UI::Notifications;
using namespace ABI::Windows::Data::Xml::Dom;
@ -46,7 +47,7 @@ void NotificationPresenterWin::ShowNotification(
// for prior versions, use Tray.displayBalloon
if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
wtn = new WindowsToastNotification(appName.c_str(), delegate.Pass());
wtn->ShowNotification(title.c_str(), body.c_str(), iconPath, m_lastNotification);
wtn->ShowNotification(title, body, iconPath, m_lastNotification);
}
if (cancel_callback) {

View file

@ -16,17 +16,16 @@
#ifndef BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_
#define BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_
#include "base/compiler_specific.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>
#include "base/compiler_specific.h"
#include "browser/notification_presenter.h"
namespace brightray {
class WindowsToastNotification;
class NotificationPresenterWin : public NotificationPresenter {
public:
NotificationPresenterWin();
@ -41,7 +40,7 @@ class NotificationPresenterWin : public NotificationPresenter {
void RemoveNotification();
private:
WinToasts::WindowsToastNotification* wtn;
WindowsToastNotification* wtn;
Microsoft::WRL::ComPtr<ABI::Windows::UI::Notifications::IToastNotification> m_lastNotification;
};

View file

@ -11,6 +11,11 @@ ScopedHString::ScopedHString(const wchar_t* source)
Set(source);
}
ScopedHString::ScopedHString(const std::wstring& source)
: str_(nullptr) {
WindowsCreateString(source.c_str(), source.length(), &str_);
}
ScopedHString::ScopedHString() : str_(nullptr) {
}

View file

@ -5,6 +5,8 @@
#ifndef BRIGHTRAY_BROWSER_WIN_SCOPED_HSTRING_H_
#define BRIGHTRAY_BROWSER_WIN_SCOPED_HSTRING_H_
#include <string>
#include <hstring.h>
#include <windows.h>
@ -14,6 +16,7 @@ class ScopedHString {
public:
// Copy from |source|.
ScopedHString(const wchar_t* source);
ScopedHString(const std::wstring& source);
// Create empty string.
ScopedHString();
~ScopedHString();

View file

@ -9,12 +9,9 @@
#include "browser/win/scoped_hstring.h"
#include "content/public/browser/desktop_notification_delegate.h"
using namespace WinToasts;
using namespace ABI::Windows::Data::Xml::Dom;
#define BREAK_IF_BAD(hr) if(!SUCCEEDED(hr)) break;
namespace WinToasts {
namespace brightray {
namespace {
@ -47,8 +44,8 @@ WindowsToastNotification::~WindowsToastNotification() {
}
void WindowsToastNotification::ShowNotification(
const WCHAR* title,
const WCHAR* msg,
const std::wstring& title,
const std::wstring& msg,
std::string icon_path,
ComPtr<IToastNotification>& toast) {
ComPtr<IXmlDocument> toast_xml;
@ -93,18 +90,18 @@ void WindowsToastNotification::NotificationDismissed() {
bool WindowsToastNotification::GetToastXml(
IToastNotificationManagerStatics* toastManager,
const WCHAR* title,
const WCHAR* msg,
const std::wstring& title,
const std::wstring& msg,
std::string icon_path,
IXmlDocument** toast_xml) {
ToastTemplateType template_type;
if (!title || !msg) {
if (title.empty() || msg.empty()) {
// Single line toast.
template_type = icon_path.empty() ? ToastTemplateType_ToastText01 :
ToastTemplateType_ToastImageAndText01;
if (FAILED(toast_manager_->GetTemplateContent(template_type, toast_xml)))
return false;
if (!SetXmlText(*toast_xml, title ? title : msg))
if (!SetXmlText(*toast_xml, title.empty() ? msg : title))
return false;
} else {
// Title and body toast.
@ -124,7 +121,7 @@ bool WindowsToastNotification::GetToastXml(
}
bool WindowsToastNotification::SetXmlText(
IXmlDocument* doc, const WCHAR* text) {
IXmlDocument* doc, const std::wstring& text) {
ScopedHString tag;
ComPtr<IXmlNodeList> node_list;
if (!GetTextNodeList(&tag, doc, &node_list, 1))
@ -138,7 +135,7 @@ bool WindowsToastNotification::SetXmlText(
}
bool WindowsToastNotification::SetXmlText(
IXmlDocument* doc, const WCHAR* title, const WCHAR* body) {
IXmlDocument* doc, const std::wstring& title, const std::wstring& body) {
ScopedHString tag;
ComPtr<IXmlNodeList> node_list;
if (!GetTextNodeList(&tag, doc, &node_list, 2))
@ -219,7 +216,7 @@ bool WindowsToastNotification::GetTextNodeList(
}
bool WindowsToastNotification::AppendTextToXml(
IXmlDocument* doc, IXmlNode* node, const WCHAR* text) {
IXmlDocument* doc, IXmlNode* node, const std::wstring& text) {
ScopedHString str(text);
if (!str.success())
return false;
@ -267,4 +264,4 @@ IFACEMETHODIMP ToastEventHandler::Invoke(
return S_OK;
}
} // namespace WinToasts
} // namespace brightray

View file

@ -20,7 +20,7 @@ using namespace ABI::Windows::Foundation;
class ScopedHString;
namespace WinToasts {
namespace brightray {
using DesktopToastActivatedEventHandler =
ITypedEventHandler<ToastNotification*, IInspectable*>;
@ -36,7 +36,10 @@ class WindowsToastNotification {
scoped_ptr<content::DesktopNotificationDelegate> delegate);
~WindowsToastNotification();
void ShowNotification(const WCHAR* title, const WCHAR* msg, std::string iconPath, ComPtr<IToastNotification>& toast);
void ShowNotification(const std::wstring& title,
const std::wstring& msg,
std::string icon_path,
ComPtr<IToastNotification>& toast);
void DismissNotification(ComPtr<IToastNotification> toast);
void NotificationClicked();
void NotificationDismissed();
@ -47,12 +50,25 @@ class WindowsToastNotification {
ComPtr<IToastNotificationManagerStatics> toast_manager_;
ComPtr<IToastNotifier> toast_notifier_;
bool GetToastXml(IToastNotificationManagerStatics* toastManager, const WCHAR* title, const WCHAR* msg, std::string iconPath, ABI::Windows::Data::Xml::Dom::IXmlDocument** toastXml);
bool SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, const WCHAR* text);
bool SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, const WCHAR* title, const WCHAR* body);
bool SetXmlImage(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, std::string iconPath);
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 WCHAR* text);
bool GetToastXml(IToastNotificationManagerStatics* toastManager,
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);
};
@ -71,6 +87,6 @@ class ToastEventHandler : public RuntimeClass<RuntimeClassFlags<ClassicCom>,
WindowsToastNotification* notification_; // weak ref.
};
} // namespace
} // namespace brightray
#endif // BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_