electron/brightray/browser/win/windows_toast_notification.h

112 lines
4.7 KiB
C
Raw Normal View History

// Copyright (c) 2015 Felix Rieseberg <feriese@microsoft.com> and Jason Poon <jason.poon@microsoft.com>. All rights reserved.
// 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.
#ifndef BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_
#define BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_
#include <windows.h>
#include <windows.ui.notifications.h>
#include <wrl/implements.h>
2015-12-25 03:05:48 +00:00
#include "browser/notification.h"
using namespace Microsoft::WRL;
2015-11-10 11:50:38 +00:00
class ScopedHString;
2015-11-10 12:07:12 +00:00
namespace brightray {
using DesktopToastActivatedEventHandler =
2015-11-11 02:04:09 +00:00
ABI::Windows::Foundation::ITypedEventHandler<ABI::Windows::UI::Notifications::ToastNotification*,
IInspectable*>;
using DesktopToastDismissedEventHandler =
2015-11-11 02:04:09 +00:00
ABI::Windows::Foundation::ITypedEventHandler<ABI::Windows::UI::Notifications::ToastNotification*,
ABI::Windows::UI::Notifications::ToastDismissedEventArgs*>;
using DesktopToastFailedEventHandler =
ABI::Windows::Foundation::ITypedEventHandler<ABI::Windows::UI::Notifications::ToastNotification*,
ABI::Windows::UI::Notifications::ToastFailedEventArgs*>;
2015-12-25 03:05:48 +00:00
class WindowsToastNotification : public Notification {
public:
2015-12-25 03:05:48 +00:00
// Should only be called by NotificationPresenterWin.
static bool Initialize();
2015-12-25 03:05:48 +00:00
WindowsToastNotification(NotificationDelegate* delegate,
NotificationPresenter* presenter);
~WindowsToastNotification();
2015-12-25 03:05:48 +00:00
protected:
// Notification:
void Show(const base::string16& title,
const base::string16& msg,
2016-04-13 03:31:56 +00:00
const std::string& tag,
2015-12-25 03:05:48 +00:00
const GURL& icon_url,
const SkBitmap& icon,
const bool silent) override;
2015-12-25 03:05:48 +00:00
void Dismiss() override;
private:
2015-11-10 12:23:08 +00:00
friend class ToastEventHandler;
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,
const std::wstring& icon_path,
const bool silent,
2015-11-10 12:07:12 +00:00
ABI::Windows::Data::Xml::Dom::IXmlDocument** toastXml);
bool SetXmlAudioSilent(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc);
2015-11-10 12:07:12 +00:00
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,
const std::wstring& icon_path);
2015-11-10 12:07:12 +00:00
bool GetTextNodeList(ScopedHString* tag,
ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
ABI::Windows::Data::Xml::Dom::IXmlNodeList** nodeList,
2016-03-08 06:02:42 +00:00
uint32_t reqLength);
2015-11-10 12:07:12 +00:00
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-12-25 03:05:48 +00:00
bool RemoveCallbacks(ABI::Windows::UI::Notifications::IToastNotification* toast);
2015-11-10 12:23:08 +00:00
static ComPtr<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics> toast_manager_;
static ComPtr<ABI::Windows::UI::Notifications::IToastNotifier> toast_notifier_;
2015-12-25 03:05:48 +00:00
EventRegistrationToken activated_token_;
EventRegistrationToken dismissed_token_;
EventRegistrationToken failed_token_;
2015-11-10 12:23:08 +00:00
ComPtr<ToastEventHandler> event_handler_;
2015-11-11 02:04:09 +00:00
ComPtr<ABI::Windows::UI::Notifications::IToastNotification> toast_notification_;
2015-11-10 12:23:08 +00:00
DISALLOW_COPY_AND_ASSIGN(WindowsToastNotification);
};
class ToastEventHandler : public RuntimeClass<RuntimeClassFlags<ClassicCom>,
DesktopToastActivatedEventHandler,
DesktopToastDismissedEventHandler,
DesktopToastFailedEventHandler> {
public:
ToastEventHandler(Notification* notification);
~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);
IFACEMETHODIMP Invoke(ABI::Windows::UI::Notifications::IToastNotification* sender, ABI::Windows::UI::Notifications::IToastFailedEventArgs* e);
private:
base::WeakPtr<Notification> notification_; // weak ref.
2015-11-10 12:23:08 +00:00
DISALLOW_COPY_AND_ASSIGN(ToastEventHandler);
};
2015-11-10 12:07:12 +00:00
} // namespace brightray
#endif // BRIGHTRAY_BROWSER_WIN_WINDOWS_TOAST_NOTIFICATION_H_