2015-11-08 06:11:15 +00:00
|
|
|
// Copyright (c) 2015 Felix Rieseberg <feriese@microsoft.com> and Jason Poon <jason.poon@microsoft.com>. All rights reserved.
|
2015-11-08 03:41:29 +00:00
|
|
|
// Copyright (c) 2015 Ryan McShane <rmcshane@bandwidth.com> and Brandon Smith <bsmith@bandwidth.com>
|
|
|
|
// 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.
|
|
|
|
|
2015-11-10 10:27:18 +00:00
|
|
|
#ifndef BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_
|
|
|
|
#define BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_
|
2015-11-08 03:41:29 +00:00
|
|
|
|
2015-11-04 18:13:52 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <windows.ui.notifications.h>
|
|
|
|
#include <wrl/implements.h>
|
|
|
|
|
2015-11-10 10:27:18 +00:00
|
|
|
#include "base/bind.h"
|
2015-11-10 12:23:08 +00:00
|
|
|
#include "base/memory/weak_ptr.h"
|
2015-11-10 10:27:18 +00:00
|
|
|
#include "content/public/browser/desktop_notification_delegate.h"
|
|
|
|
#include "content/public/common/platform_notification_data.h"
|
|
|
|
|
2015-11-04 18:13:52 +00:00
|
|
|
using namespace Microsoft::WRL;
|
|
|
|
|
2015-11-10 11:50:38 +00:00
|
|
|
class ScopedHString;
|
|
|
|
|
2015-11-10 12:07:12 +00:00
|
|
|
namespace brightray {
|
2015-11-04 18:13:52 +00:00
|
|
|
|
2015-11-10 10:27:18 +00:00
|
|
|
using DesktopToastActivatedEventHandler =
|
2015-11-11 02:04:09 +00:00
|
|
|
ABI::Windows::Foundation::ITypedEventHandler<ABI::Windows::UI::Notifications::ToastNotification*,
|
|
|
|
IInspectable*>;
|
2015-11-10 10:27:18 +00:00
|
|
|
using DesktopToastDismissedEventHandler =
|
2015-11-11 02:04:09 +00:00
|
|
|
ABI::Windows::Foundation::ITypedEventHandler<ABI::Windows::UI::Notifications::ToastNotification*,
|
|
|
|
ABI::Windows::UI::Notifications::ToastDismissedEventArgs*>;
|
2015-11-10 10:27:18 +00:00
|
|
|
|
|
|
|
class WindowsToastNotification {
|
|
|
|
public:
|
2015-11-10 11:50:38 +00:00
|
|
|
WindowsToastNotification(
|
2015-11-10 12:23:08 +00:00
|
|
|
const std::string& app_name,
|
2015-11-10 11:50:38 +00:00
|
|
|
scoped_ptr<content::DesktopNotificationDelegate> delegate);
|
2015-11-10 10:27:18 +00:00
|
|
|
~WindowsToastNotification();
|
|
|
|
|
2015-11-10 12:07:12 +00:00
|
|
|
void ShowNotification(const std::wstring& title,
|
|
|
|
const std::wstring& msg,
|
2015-11-10 12:23:08 +00:00
|
|
|
std::string icon_path);
|
|
|
|
void DismissNotification();
|
|
|
|
|
|
|
|
base::WeakPtr<WindowsToastNotification> GetWeakPtr() {
|
|
|
|
return weak_factory_.GetWeakPtr();
|
|
|
|
}
|
2015-11-10 10:27:18 +00:00
|
|
|
|
|
|
|
private:
|
2015-11-10 12:23:08 +00:00
|
|
|
friend class ToastEventHandler;
|
|
|
|
|
|
|
|
void NotificationClicked();
|
|
|
|
void NotificationDismissed();
|
2015-11-10 11:50:38 +00:00
|
|
|
|
2015-11-11 02:04:09 +00:00
|
|
|
bool GetToastXml(ABI::Windows::UI::Notifications::IToastNotificationManagerStatics* toastManager,
|
2015-11-10 12:07:12 +00:00
|
|
|
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);
|
2015-11-11 02:04:09 +00:00
|
|
|
bool SetupCallbacks(ABI::Windows::UI::Notifications::IToastNotification* toast);
|
2015-11-10 12:23:08 +00:00
|
|
|
|
|
|
|
scoped_ptr<content::DesktopNotificationDelegate> delegate_;
|
|
|
|
ComPtr<ToastEventHandler> event_handler_;
|
2015-11-11 02:04:09 +00:00
|
|
|
ComPtr<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics> toast_manager_;
|
|
|
|
ComPtr<ABI::Windows::UI::Notifications::IToastNotifier> toast_notifier_;
|
|
|
|
ComPtr<ABI::Windows::UI::Notifications::IToastNotification> toast_notification_;
|
2015-11-10 12:23:08 +00:00
|
|
|
|
|
|
|
base::WeakPtrFactory<WindowsToastNotification> weak_factory_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(WindowsToastNotification);
|
2015-11-10 10:27:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ToastEventHandler : public RuntimeClass<RuntimeClassFlags<ClassicCom>,
|
|
|
|
DesktopToastActivatedEventHandler,
|
|
|
|
DesktopToastDismissedEventHandler> {
|
|
|
|
public:
|
2015-11-10 11:50:38 +00:00
|
|
|
ToastEventHandler(WindowsToastNotification* notification);
|
2015-11-10 10:27:18 +00:00
|
|
|
~ToastEventHandler();
|
|
|
|
|
2015-11-11 02:04:09 +00:00
|
|
|
IFACEMETHODIMP Invoke(ABI::Windows::UI::Notifications::IToastNotification* sender, IInspectable* args);
|
|
|
|
IFACEMETHODIMP Invoke(ABI::Windows::UI::Notifications::IToastNotification* sender, ABI::Windows::UI::Notifications::IToastDismissedEventArgs* e);
|
2015-11-10 10:27:18 +00:00
|
|
|
|
|
|
|
private:
|
2015-11-10 11:50:38 +00:00
|
|
|
WindowsToastNotification* notification_; // weak ref.
|
2015-11-10 12:23:08 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ToastEventHandler);
|
2015-11-10 10:27:18 +00:00
|
|
|
};
|
2015-11-04 18:13:52 +00:00
|
|
|
|
2015-11-10 12:07:12 +00:00
|
|
|
} // namespace brightray
|
2015-11-04 18:13:52 +00:00
|
|
|
|
2015-11-10 10:27:18 +00:00
|
|
|
#endif // BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_
|