Simplify ToastEventHandler using WRL::RuntimeClass, which implements the IUnknown interface

This commit is contained in:
Milan Burda 2015-11-09 20:47:18 +01:00
parent ecb35883f6
commit 8f5463faab
2 changed files with 6 additions and 37 deletions

View file

@ -41,16 +41,11 @@ WindowsToastNotification::WindowsToastNotification(const char* appName, content:
WindowsDeleteString(appId); WindowsDeleteString(appId);
} }
m_eventHandler = NULL;
n_delegate = delegate; n_delegate = delegate;
} }
WindowsToastNotification::~WindowsToastNotification() WindowsToastNotification::~WindowsToastNotification()
{ {
if (m_eventHandler) {
delete m_eventHandler;
}
if (n_delegate) { if (n_delegate) {
delete n_delegate; delete n_delegate;
} }
@ -309,12 +304,11 @@ HRESULT WindowsToastNotification::AppendTextToXml(IXmlDocument* doc, IXmlNode* n
HRESULT WindowsToastNotification::SetupCallbacks(IToastNotification* toast) HRESULT WindowsToastNotification::SetupCallbacks(IToastNotification* toast)
{ {
EventRegistrationToken activatedToken, dismissedToken; EventRegistrationToken activatedToken, dismissedToken;
m_eventHandler = new ToastEventHandler(this, n_delegate); m_eventHandler = Make<ToastEventHandler>(this, n_delegate);
ComPtr<ToastEventHandler> eventHandler(m_eventHandler); HRESULT hr = toast->add_Activated(m_eventHandler.Get(), &activatedToken);
HRESULT hr = toast->add_Activated(eventHandler.Get(), &activatedToken);
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
hr = toast->add_Dismissed(eventHandler.Get(), &dismissedToken); hr = toast->add_Dismissed(m_eventHandler.Get(), &dismissedToken);
} }
return hr; return hr;

View file

@ -36,7 +36,7 @@ namespace WinToasts {
void NotificationDismissed(); void NotificationDismissed();
private: private:
ToastEventHandler* m_eventHandler; ComPtr<ToastEventHandler> m_eventHandler;
content::DesktopNotificationDelegate* n_delegate; content::DesktopNotificationDelegate* n_delegate;
ComPtr<IToastNotificationManagerStatics> m_toastManager; ComPtr<IToastNotificationManagerStatics> m_toastManager;
@ -54,40 +54,15 @@ namespace WinToasts {
class ToastEventHandler : class ToastEventHandler :
public Implements <DesktopToastActivatedEventHandler, DesktopToastDismissedEventHandler> public RuntimeClass<RuntimeClassFlags<ClassicCom>, DesktopToastActivatedEventHandler, DesktopToastDismissedEventHandler>
{ {
public: public:
ToastEventHandler(WindowsToastNotification* notification, content::DesktopNotificationDelegate* delegate); ToastEventHandler(WindowsToastNotification* notification, content::DesktopNotificationDelegate* delegate);
~ToastEventHandler(); ~ToastEventHandler();
IFACEMETHODIMP Invoke(IToastNotification* sender, IInspectable* args); IFACEMETHODIMP Invoke(IToastNotification* sender, IInspectable* args);
IFACEMETHODIMP Invoke(IToastNotification* sender, IToastDismissedEventArgs* e); 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<IUnknown*>(static_cast<DesktopToastActivatedEventHandler*>(this));
else if (IsEqualIID(riid, __uuidof(DesktopToastActivatedEventHandler)))
*ppv = static_cast<DesktopToastActivatedEventHandler*>(this);
else if (IsEqualIID(riid, __uuidof(DesktopToastDismissedEventHandler)))
*ppv = static_cast<DesktopToastDismissedEventHandler*>(this);
else *ppv = nullptr;
if (*ppv) {
reinterpret_cast<IUnknown*>(*ppv)->AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
private: private:
ULONG m_ref;
WindowsToastNotification* m_notification; WindowsToastNotification* m_notification;
content::DesktopNotificationDelegate* n_delegate; content::DesktopNotificationDelegate* n_delegate;
}; };