Make the toast type really work
This commit is contained in:
parent
6b9371c4cd
commit
4f73de0930
6 changed files with 53 additions and 32 deletions
|
@ -4,7 +4,9 @@
|
||||||
// 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 "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"
|
||||||
|
@ -12,7 +14,6 @@
|
||||||
|
|
||||||
#pragma comment(lib, "runtimeobject.lib")
|
#pragma comment(lib, "runtimeobject.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;
|
||||||
|
@ -46,7 +47,7 @@ void NotificationPresenterWin::ShowNotification(
|
||||||
// for prior versions, use Tray.displayBalloon
|
// for prior versions, use Tray.displayBalloon
|
||||||
if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
|
if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
|
||||||
wtn = new WindowsToastNotification(appName.c_str(), delegate.Pass());
|
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) {
|
if (cancel_callback) {
|
||||||
|
|
|
@ -16,17 +16,16 @@
|
||||||
#ifndef BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_
|
#ifndef BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_
|
||||||
#define 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 <windows.ui.notifications.h>
|
||||||
#include <wrl/client.h>
|
|
||||||
#include <wrl/implements.h>
|
#include <wrl/implements.h>
|
||||||
|
|
||||||
|
#include "base/compiler_specific.h"
|
||||||
|
#include "browser/notification_presenter.h"
|
||||||
|
|
||||||
namespace brightray {
|
namespace brightray {
|
||||||
|
|
||||||
|
class WindowsToastNotification;
|
||||||
|
|
||||||
class NotificationPresenterWin : public NotificationPresenter {
|
class NotificationPresenterWin : public NotificationPresenter {
|
||||||
public:
|
public:
|
||||||
NotificationPresenterWin();
|
NotificationPresenterWin();
|
||||||
|
@ -41,7 +40,7 @@ class NotificationPresenterWin : public NotificationPresenter {
|
||||||
void RemoveNotification();
|
void RemoveNotification();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
WinToasts::WindowsToastNotification* wtn;
|
WindowsToastNotification* wtn;
|
||||||
Microsoft::WRL::ComPtr<ABI::Windows::UI::Notifications::IToastNotification> m_lastNotification;
|
Microsoft::WRL::ComPtr<ABI::Windows::UI::Notifications::IToastNotification> m_lastNotification;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,11 @@ ScopedHString::ScopedHString(const wchar_t* source)
|
||||||
Set(source);
|
Set(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ScopedHString::ScopedHString(const std::wstring& source)
|
||||||
|
: str_(nullptr) {
|
||||||
|
WindowsCreateString(source.c_str(), source.length(), &str_);
|
||||||
|
}
|
||||||
|
|
||||||
ScopedHString::ScopedHString() : str_(nullptr) {
|
ScopedHString::ScopedHString() : str_(nullptr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
#ifndef BRIGHTRAY_BROWSER_WIN_SCOPED_HSTRING_H_
|
#ifndef BRIGHTRAY_BROWSER_WIN_SCOPED_HSTRING_H_
|
||||||
#define BRIGHTRAY_BROWSER_WIN_SCOPED_HSTRING_H_
|
#define BRIGHTRAY_BROWSER_WIN_SCOPED_HSTRING_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <hstring.h>
|
#include <hstring.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
|
@ -14,6 +16,7 @@ class ScopedHString {
|
||||||
public:
|
public:
|
||||||
// Copy from |source|.
|
// Copy from |source|.
|
||||||
ScopedHString(const wchar_t* source);
|
ScopedHString(const wchar_t* source);
|
||||||
|
ScopedHString(const std::wstring& source);
|
||||||
// Create empty string.
|
// Create empty string.
|
||||||
ScopedHString();
|
ScopedHString();
|
||||||
~ScopedHString();
|
~ScopedHString();
|
||||||
|
|
|
@ -9,12 +9,9 @@
|
||||||
#include "browser/win/scoped_hstring.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 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 {
|
namespace {
|
||||||
|
|
||||||
|
@ -47,8 +44,8 @@ WindowsToastNotification::~WindowsToastNotification() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowsToastNotification::ShowNotification(
|
void WindowsToastNotification::ShowNotification(
|
||||||
const WCHAR* title,
|
const std::wstring& title,
|
||||||
const WCHAR* msg,
|
const std::wstring& msg,
|
||||||
std::string icon_path,
|
std::string icon_path,
|
||||||
ComPtr<IToastNotification>& toast) {
|
ComPtr<IToastNotification>& toast) {
|
||||||
ComPtr<IXmlDocument> toast_xml;
|
ComPtr<IXmlDocument> toast_xml;
|
||||||
|
@ -93,18 +90,18 @@ void WindowsToastNotification::NotificationDismissed() {
|
||||||
|
|
||||||
bool 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 icon_path,
|
std::string icon_path,
|
||||||
IXmlDocument** toast_xml) {
|
IXmlDocument** toast_xml) {
|
||||||
ToastTemplateType template_type;
|
ToastTemplateType template_type;
|
||||||
if (!title || !msg) {
|
if (title.empty() || msg.empty()) {
|
||||||
// Single line toast.
|
// Single line toast.
|
||||||
template_type = icon_path.empty() ? ToastTemplateType_ToastText01 :
|
template_type = icon_path.empty() ? ToastTemplateType_ToastText01 :
|
||||||
ToastTemplateType_ToastImageAndText01;
|
ToastTemplateType_ToastImageAndText01;
|
||||||
if (FAILED(toast_manager_->GetTemplateContent(template_type, toast_xml)))
|
if (FAILED(toast_manager_->GetTemplateContent(template_type, toast_xml)))
|
||||||
return false;
|
return false;
|
||||||
if (!SetXmlText(*toast_xml, title ? title : msg))
|
if (!SetXmlText(*toast_xml, title.empty() ? msg : title))
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
// Title and body toast.
|
// Title and body toast.
|
||||||
|
@ -124,7 +121,7 @@ bool WindowsToastNotification::GetToastXml(
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WindowsToastNotification::SetXmlText(
|
bool WindowsToastNotification::SetXmlText(
|
||||||
IXmlDocument* doc, const WCHAR* text) {
|
IXmlDocument* doc, const std::wstring& text) {
|
||||||
ScopedHString tag;
|
ScopedHString tag;
|
||||||
ComPtr<IXmlNodeList> node_list;
|
ComPtr<IXmlNodeList> node_list;
|
||||||
if (!GetTextNodeList(&tag, doc, &node_list, 1))
|
if (!GetTextNodeList(&tag, doc, &node_list, 1))
|
||||||
|
@ -138,7 +135,7 @@ bool WindowsToastNotification::SetXmlText(
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
ScopedHString tag;
|
||||||
ComPtr<IXmlNodeList> node_list;
|
ComPtr<IXmlNodeList> node_list;
|
||||||
if (!GetTextNodeList(&tag, doc, &node_list, 2))
|
if (!GetTextNodeList(&tag, doc, &node_list, 2))
|
||||||
|
@ -219,7 +216,7 @@ bool WindowsToastNotification::GetTextNodeList(
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WindowsToastNotification::AppendTextToXml(
|
bool WindowsToastNotification::AppendTextToXml(
|
||||||
IXmlDocument* doc, IXmlNode* node, const WCHAR* text) {
|
IXmlDocument* doc, IXmlNode* node, const std::wstring& text) {
|
||||||
ScopedHString str(text);
|
ScopedHString str(text);
|
||||||
if (!str.success())
|
if (!str.success())
|
||||||
return false;
|
return false;
|
||||||
|
@ -267,4 +264,4 @@ IFACEMETHODIMP ToastEventHandler::Invoke(
|
||||||
return S_OK;
|
return S_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace WinToasts
|
} // namespace brightray
|
||||||
|
|
|
@ -20,7 +20,7 @@ using namespace ABI::Windows::Foundation;
|
||||||
|
|
||||||
class ScopedHString;
|
class ScopedHString;
|
||||||
|
|
||||||
namespace WinToasts {
|
namespace brightray {
|
||||||
|
|
||||||
using DesktopToastActivatedEventHandler =
|
using DesktopToastActivatedEventHandler =
|
||||||
ITypedEventHandler<ToastNotification*, IInspectable*>;
|
ITypedEventHandler<ToastNotification*, IInspectable*>;
|
||||||
|
@ -36,7 +36,10 @@ class WindowsToastNotification {
|
||||||
scoped_ptr<content::DesktopNotificationDelegate> delegate);
|
scoped_ptr<content::DesktopNotificationDelegate> delegate);
|
||||||
~WindowsToastNotification();
|
~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 DismissNotification(ComPtr<IToastNotification> toast);
|
||||||
void NotificationClicked();
|
void NotificationClicked();
|
||||||
void NotificationDismissed();
|
void NotificationDismissed();
|
||||||
|
@ -47,12 +50,25 @@ class WindowsToastNotification {
|
||||||
ComPtr<IToastNotificationManagerStatics> toast_manager_;
|
ComPtr<IToastNotificationManagerStatics> toast_manager_;
|
||||||
ComPtr<IToastNotifier> toast_notifier_;
|
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 GetToastXml(IToastNotificationManagerStatics* toastManager,
|
||||||
bool SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, const WCHAR* text);
|
const std::wstring& title,
|
||||||
bool SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, const WCHAR* title, const WCHAR* body);
|
const std::wstring& msg,
|
||||||
bool SetXmlImage(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, std::string iconPath);
|
std::string icon_path,
|
||||||
bool GetTextNodeList(ScopedHString* tag, ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, ABI::Windows::Data::Xml::Dom::IXmlNodeList** nodeList, UINT32 reqLength);
|
ABI::Windows::Data::Xml::Dom::IXmlDocument** toastXml);
|
||||||
bool AppendTextToXml(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc, ABI::Windows::Data::Xml::Dom::IXmlNode* node, const WCHAR* text);
|
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);
|
bool SetupCallbacks(IToastNotification* toast);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -71,6 +87,6 @@ class ToastEventHandler : public RuntimeClass<RuntimeClassFlags<ClassicCom>,
|
||||||
WindowsToastNotification* notification_; // weak ref.
|
WindowsToastNotification* notification_; // weak ref.
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace brightray
|
||||||
|
|
||||||
#endif // BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_
|
#endif // BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_
|
||||||
|
|
Loading…
Add table
Reference in a new issue