From 8f5463faab51b87787dbc315ef4b97812bada466 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Mon, 9 Nov 2015 20:47:18 +0100 Subject: [PATCH] Simplify ToastEventHandler using WRL::RuntimeClass, which implements the IUnknown interface --- .../browser/win/windows_toast_notification.cc | 14 +++------ .../browser/win/windows_toast_notification.h | 29 ++----------------- 2 files changed, 6 insertions(+), 37 deletions(-) diff --git a/brightray/browser/win/windows_toast_notification.cc b/brightray/browser/win/windows_toast_notification.cc index 5d306032d6..804cd55289 100644 --- a/brightray/browser/win/windows_toast_notification.cc +++ b/brightray/browser/win/windows_toast_notification.cc @@ -41,16 +41,11 @@ WindowsToastNotification::WindowsToastNotification(const char* appName, content: WindowsDeleteString(appId); } - m_eventHandler = NULL; n_delegate = delegate; } WindowsToastNotification::~WindowsToastNotification() { - if (m_eventHandler) { - delete m_eventHandler; - } - if (n_delegate) { delete n_delegate; } @@ -309,12 +304,11 @@ HRESULT WindowsToastNotification::AppendTextToXml(IXmlDocument* doc, IXmlNode* n HRESULT WindowsToastNotification::SetupCallbacks(IToastNotification* toast) { EventRegistrationToken activatedToken, dismissedToken; - m_eventHandler = new ToastEventHandler(this, n_delegate); - ComPtr eventHandler(m_eventHandler); - HRESULT hr = toast->add_Activated(eventHandler.Get(), &activatedToken); - + m_eventHandler = Make(this, n_delegate); + HRESULT hr = toast->add_Activated(m_eventHandler.Get(), &activatedToken); + if (SUCCEEDED(hr)) { - hr = toast->add_Dismissed(eventHandler.Get(), &dismissedToken); + hr = toast->add_Dismissed(m_eventHandler.Get(), &dismissedToken); } return hr; diff --git a/brightray/browser/win/windows_toast_notification.h b/brightray/browser/win/windows_toast_notification.h index 474d55c767..d501954ec5 100644 --- a/brightray/browser/win/windows_toast_notification.h +++ b/brightray/browser/win/windows_toast_notification.h @@ -36,7 +36,7 @@ namespace WinToasts { void NotificationDismissed(); private: - ToastEventHandler* m_eventHandler; + ComPtr m_eventHandler; content::DesktopNotificationDelegate* n_delegate; ComPtr m_toastManager; @@ -54,40 +54,15 @@ namespace WinToasts { class ToastEventHandler : - public Implements + public RuntimeClass, DesktopToastActivatedEventHandler, DesktopToastDismissedEventHandler> { public: ToastEventHandler(WindowsToastNotification* notification, content::DesktopNotificationDelegate* delegate); ~ToastEventHandler(); IFACEMETHODIMP Invoke(IToastNotification* sender, IInspectable* args); IFACEMETHODIMP Invoke(IToastNotification* sender, IToastDismissedEventArgs* e); - IFACEMETHODIMP_(ULONG) AddRef() { return InterlockedIncrement(&m_ref); } - - IFACEMETHODIMP_(ULONG) Release() { - ULONG l = InterlockedDecrement(&m_ref); - if (l == 0) delete this; - return l; - } - - IFACEMETHODIMP QueryInterface(_In_ REFIID riid, _COM_Outptr_ void **ppv) { - if (IsEqualIID(riid, IID_IUnknown)) - *ppv = static_cast(static_cast(this)); - else if (IsEqualIID(riid, __uuidof(DesktopToastActivatedEventHandler))) - *ppv = static_cast(this); - else if (IsEqualIID(riid, __uuidof(DesktopToastDismissedEventHandler))) - *ppv = static_cast(this); - else *ppv = nullptr; - - if (*ppv) { - reinterpret_cast(*ppv)->AddRef(); - return S_OK; - } - - return E_NOINTERFACE; - } private: - ULONG m_ref; WindowsToastNotification* m_notification; content::DesktopNotificationDelegate* n_delegate; };