3737
3838#include < intsafe.h>
3939#include < strsafe.h>
40- #include < wrl\client.h>
4140#include < wrl\implements.h>
4241
43- using namespace Microsoft ::WRL;
4442using namespace Windows ::Foundation;
4543
4644namespace nw {
@@ -198,6 +196,7 @@ IFACEMETHODIMP ToastEventHandler::Invoke(_In_ IToastNotification* /* sender */,
198196 BOOL succeeded = nw::NotificationManager::getSingleton ()->DesktopNotificationPostClose (_render_process_id, _render_frame_id, _notification_id, tdr == ToastDismissalReason_UserCanceled);
199197 hr = succeeded ? S_OK : E_FAIL;
200198 }
199+ nw::NotificationManager::getSingleton ()->CancelDesktopNotification (_render_process_id, _render_frame_id, _notification_id);
201200 return hr;
202201}
203202
@@ -326,37 +325,28 @@ HRESULT NotificationManagerToastWin::CreateToastXml(_In_ IToastNotificationManag
326325HRESULT NotificationManagerToastWin::CreateToast (_In_ IToastNotificationManagerStatics *toastManager, _In_ IXmlDocument *xml,
327326 const int render_process_id, const int render_frame_id, const int notification_id)
328327{
329- base::string16 appID;
330- if (content::Shell::GetPackage ()->root ()->GetString (" app-id" , &appID) == false )
331- content::Shell::GetPackage ()->root ()->GetString (switches::kmName, &appID);
332-
333- ComPtr<IToastNotifier> notifier;
334- HRESULT hr = toastManager->CreateToastNotifierWithId (StringReferenceWrapper (appID.c_str (), appID.length ()).Get (), ¬ifier);
328+ ComPtr<IToastNotificationFactory> factory;
329+ HRESULT hr = GetActivationFactory (StringReferenceWrapper (RuntimeClass_Windows_UI_Notifications_ToastNotification).Get (), &factory);
335330 if (SUCCEEDED (hr))
336331 {
337- ComPtr<IToastNotificationFactory> factory ;
338- hr = GetActivationFactory ( StringReferenceWrapper (RuntimeClass_Windows_UI_Notifications_ToastNotification). Get () , &factory );
332+ ComPtr<IToastNotification>& toast = notification_map_[notification_id] ;
333+ hr = factory-> CreateToastNotification (xml , &toast );
339334 if (SUCCEEDED (hr))
340335 {
341- ComPtr<IToastNotification> toast;
342- hr = factory->CreateToastNotification (xml, &toast);
336+ // Register the event handlers
337+ EventRegistrationToken activatedToken, dismissedToken, failedToken;
338+ ComPtr<ToastEventHandler> eventHandler = new ToastEventHandler (render_process_id, render_frame_id, notification_id);
339+
340+ hr = toast->add_Activated (eventHandler.Get (), &activatedToken);
343341 if (SUCCEEDED (hr))
344342 {
345- // Register the event handlers
346- EventRegistrationToken activatedToken, dismissedToken, failedToken;
347- ComPtr<ToastEventHandler> eventHandler = new ToastEventHandler (render_process_id, render_frame_id, notification_id);
348-
349- hr = toast->add_Activated (eventHandler.Get (), &activatedToken);
343+ hr = toast->add_Dismissed (eventHandler.Get (), &dismissedToken);
350344 if (SUCCEEDED (hr))
351345 {
352- hr = toast->add_Dismissed (eventHandler.Get (), &dismissedToken );
346+ hr = toast->add_Failed (eventHandler.Get (), &failedToken );
353347 if (SUCCEEDED (hr))
354348 {
355- hr = toast->add_Failed (eventHandler.Get (), &failedToken);
356- if (SUCCEEDED (hr))
357- {
358- hr = notifier->Show (toast.Get ());
359- }
349+ hr = notifier_->Show (toast.Get ());
360350 }
361351 }
362352 }
@@ -381,6 +371,15 @@ bool NotificationManagerToastWin::IsSupported() {
381371}
382372
383373NotificationManagerToastWin::NotificationManagerToastWin () {
374+ HRESULT hr = GetActivationFactory (StringReferenceWrapper (RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get (), &toastStatics_);
375+ if (SUCCEEDED (hr))
376+ {
377+ base::string16 appID;
378+ if (content::Shell::GetPackage ()->root ()->GetString (" app-id" , &appID) == false )
379+ content::Shell::GetPackage ()->root ()->GetString (switches::kmName, &appID);
380+
381+ HRESULT hr = toastStatics_->CreateToastNotifierWithId (StringReferenceWrapper (appID.c_str (), appID.length ()).Get (), ¬ifier_);
382+ }
384383}
385384
386385NotificationManagerToastWin::~NotificationManagerToastWin () {
@@ -395,24 +394,28 @@ bool NotificationManagerToastWin::AddDesktopNotification(const content::ShowDesk
395394 if (host == NULL )
396395 return false ;
397396
398- ComPtr<IToastNotificationManagerStatics> toastStatics;
399- HRESULT hr = GetActivationFactory (StringReferenceWrapper (RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get (), &toastStatics);
400- if (SUCCEEDED (hr))
401- {
397+
402398 ComPtr<IXmlDocument> toastXml;
403- hr = CreateToastXml (toastStatics .Get (), params, &toastXml);
399+ HRESULT hr = CreateToastXml (toastStatics_ .Get (), params, &toastXml);
404400 if (SUCCEEDED (hr))
405401 {
406- hr = CreateToast (toastStatics .Get (), toastXml.Get (), render_process_id, render_frame_id, notification_id);
402+ hr = CreateToast (toastStatics_ .Get (), toastXml.Get (), render_process_id, render_frame_id, notification_id);
407403 if (SUCCEEDED (hr))
408404 DesktopNotificationPostDisplay (render_process_id, render_frame_id, notification_id);
409405 }
410- }
406+
411407
412408 return SUCCEEDED (hr);
413409}
414410
415411bool NotificationManagerToastWin::CancelDesktopNotification (int render_process_id, int render_frame_id, int notification_id) {
416- return true ;
412+ std::map<int , ComPtr<IToastNotification>>::iterator i = notification_map_.find (notification_id);
413+ if (i == notification_map_.end ())
414+ return false ;
415+
416+ ComPtr<IToastNotification> toast = i->second ;
417+ notification_map_.erase (i);
418+
419+ return SUCCEEDED (notifier_->Hide (toast.Get ()));
417420}
418421} // namespace nw
0 commit comments