diff --git a/brightray/browser/win/notification_presenter_win.cc b/brightray/browser/win/notification_presenter_win.cc index 23ec1303c7ca..87fc6da55e4c 100644 --- a/brightray/browser/win/notification_presenter_win.cc +++ b/brightray/browser/win/notification_presenter_win.cc @@ -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) { diff --git a/brightray/browser/win/notification_presenter_win.h b/brightray/browser/win/notification_presenter_win.h index fa7ccf40d1f6..9a1a1d6576b0 100644 --- a/brightray/browser/win/notification_presenter_win.h +++ b/brightray/browser/win/notification_presenter_win.h @@ -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 #include -#include #include +#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 m_lastNotification; }; diff --git a/brightray/browser/win/scoped_hstring.cc b/brightray/browser/win/scoped_hstring.cc index 7954bf1552fc..e3f5cbd29bbb 100644 --- a/brightray/browser/win/scoped_hstring.cc +++ b/brightray/browser/win/scoped_hstring.cc @@ -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) { } diff --git a/brightray/browser/win/scoped_hstring.h b/brightray/browser/win/scoped_hstring.h index 71c8bfcbe03f..de03c0469b3a 100644 --- a/brightray/browser/win/scoped_hstring.h +++ b/brightray/browser/win/scoped_hstring.h @@ -5,6 +5,8 @@ #ifndef BRIGHTRAY_BROWSER_WIN_SCOPED_HSTRING_H_ #define BRIGHTRAY_BROWSER_WIN_SCOPED_HSTRING_H_ +#include + #include #include @@ -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(); diff --git a/brightray/browser/win/windows_toast_notification.cc b/brightray/browser/win/windows_toast_notification.cc index 192b5a25db5d..2187788332a3 100644 --- a/brightray/browser/win/windows_toast_notification.cc +++ b/brightray/browser/win/windows_toast_notification.cc @@ -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& toast) { ComPtr 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 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 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 diff --git a/brightray/browser/win/windows_toast_notification.h b/brightray/browser/win/windows_toast_notification.h index 6dee88698b6c..8a8c52935291 100644 --- a/brightray/browser/win/windows_toast_notification.h +++ b/brightray/browser/win/windows_toast_notification.h @@ -20,7 +20,7 @@ using namespace ABI::Windows::Foundation; class ScopedHString; -namespace WinToasts { +namespace brightray { using DesktopToastActivatedEventHandler = ITypedEventHandler; @@ -36,7 +36,10 @@ class WindowsToastNotification { scoped_ptr delegate); ~WindowsToastNotification(); - void ShowNotification(const WCHAR* title, const WCHAR* msg, std::string iconPath, ComPtr& toast); + void ShowNotification(const std::wstring& title, + const std::wstring& msg, + std::string icon_path, + ComPtr& toast); void DismissNotification(ComPtr toast); void NotificationClicked(); void NotificationDismissed(); @@ -47,12 +50,25 @@ class WindowsToastNotification { ComPtr toast_manager_; ComPtr 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, WindowsToastNotification* notification_; // weak ref. }; -} // namespace +} // namespace brightray #endif // BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_