37
37
38
38
#include < intsafe.h>
39
39
#include < strsafe.h>
40
- #include < wrl\client.h>
41
40
#include < wrl\implements.h>
42
41
43
- using namespace Microsoft ::WRL;
44
42
using namespace Windows ::Foundation;
45
43
46
44
namespace nw {
@@ -198,6 +196,7 @@ IFACEMETHODIMP ToastEventHandler::Invoke(_In_ IToastNotification* /* sender */,
198
196
BOOL succeeded = nw::NotificationManager::getSingleton ()->DesktopNotificationPostClose (_render_process_id, _render_frame_id, _notification_id, tdr == ToastDismissalReason_UserCanceled);
199
197
hr = succeeded ? S_OK : E_FAIL;
200
198
}
199
+ nw::NotificationManager::getSingleton ()->CancelDesktopNotification (_render_process_id, _render_frame_id, _notification_id);
201
200
return hr;
202
201
}
203
202
@@ -326,37 +325,28 @@ HRESULT NotificationManagerToastWin::CreateToastXml(_In_ IToastNotificationManag
326
325
HRESULT NotificationManagerToastWin::CreateToast (_In_ IToastNotificationManagerStatics *toastManager, _In_ IXmlDocument *xml,
327
326
const int render_process_id, const int render_frame_id, const int notification_id)
328
327
{
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);
335
330
if (SUCCEEDED (hr))
336
331
{
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 );
339
334
if (SUCCEEDED (hr))
340
335
{
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);
343
341
if (SUCCEEDED (hr))
344
342
{
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);
350
344
if (SUCCEEDED (hr))
351
345
{
352
- hr = toast->add_Dismissed (eventHandler.Get (), &dismissedToken );
346
+ hr = toast->add_Failed (eventHandler.Get (), &failedToken );
353
347
if (SUCCEEDED (hr))
354
348
{
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 ());
360
350
}
361
351
}
362
352
}
@@ -381,6 +371,15 @@ bool NotificationManagerToastWin::IsSupported() {
381
371
}
382
372
383
373
NotificationManagerToastWin::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
+ }
384
383
}
385
384
386
385
NotificationManagerToastWin::~NotificationManagerToastWin () {
@@ -395,24 +394,28 @@ bool NotificationManagerToastWin::AddDesktopNotification(const content::ShowDesk
395
394
if (host == NULL )
396
395
return false ;
397
396
398
- ComPtr<IToastNotificationManagerStatics> toastStatics;
399
- HRESULT hr = GetActivationFactory (StringReferenceWrapper (RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get (), &toastStatics);
400
- if (SUCCEEDED (hr))
401
- {
397
+
402
398
ComPtr<IXmlDocument> toastXml;
403
- hr = CreateToastXml (toastStatics .Get (), params, &toastXml);
399
+ HRESULT hr = CreateToastXml (toastStatics_ .Get (), params, &toastXml);
404
400
if (SUCCEEDED (hr))
405
401
{
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);
407
403
if (SUCCEEDED (hr))
408
404
DesktopNotificationPostDisplay (render_process_id, render_frame_id, notification_id);
409
405
}
410
- }
406
+
411
407
412
408
return SUCCEEDED (hr);
413
409
}
414
410
415
411
bool 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 ()));
417
420
}
418
421
} // namespace nw
0 commit comments