Skip to content

Commit ec801e3

Browse files
committed
[Notification] fix "Cancel Desktop Notification" for all platform. and implement it for win8 (toast notification)
1 parent 324ebf1 commit ec801e3

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

src/nw_notification_manager_toast_win.cc

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@
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;
4442
using namespace Windows::Foundation;
4543

4644
namespace 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
326325
HRESULT 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(), &notifier);
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

383373
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(), &notifier_);
382+
}
384383
}
385384

386385
NotificationManagerToastWin::~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

415411
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()));
417420
}
418421
} // namespace nw

src/nw_notification_manager_toast_win.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@
2222

2323
#include "content/nw/src/nw_notification_manager.h"
2424
#include <windows.ui.notifications.h>
25+
#include <wrl\client.h>
26+
2527

2628
namespace nw {
29+
using namespace Microsoft::WRL;
2730
using namespace ABI::Windows::UI::Notifications;
2831
using namespace ABI::Windows::Data::Xml::Dom;
2932

3033
class NotificationManagerToastWin : public NotificationManager{
3134

35+
ComPtr<IToastNotificationManagerStatics> toastStatics_;
36+
ComPtr<IToastNotifier> notifier_;
37+
std::map<int, ComPtr<IToastNotification>> notification_map_;
38+
3239
// internal function for AddDesktopNotification
3340
virtual bool AddDesktopNotification(const content::ShowDesktopNotificationHostMsgParams& params,
3441
const int render_process_id, const int render_frame_id, const int notification_id,

src/shell_content_browser_client.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,15 @@ ShellContentBrowserClient::CreateQuotaPermissionContext() {
495495
return new ShellQuotaPermissionContext();
496496
}
497497

498+
void CancelDesktopNotification(int render_process_id, int render_frame_id, int notification_id) {
499+
nw::NotificationManager *notificationManager = nw::NotificationManager::getSingleton();
500+
if (notificationManager == NULL) {
501+
NOTIMPLEMENTED();
502+
return;
503+
}
504+
notificationManager->CancelDesktopNotification(render_process_id, render_frame_id, notification_id);
505+
}
506+
498507
void ShellContentBrowserClient::ShowDesktopNotification(
499508
const ShowDesktopNotificationHostMsgParams& params,
500509
RenderFrameHost* render_frame_host,
@@ -511,6 +520,7 @@ void ShellContentBrowserClient::ShowDesktopNotification(
511520
render_frame_host->GetRoutingID(),
512521
delegate->notification_id(),
513522
false);
523+
*cancel_callback = base::Bind(&CancelDesktopNotification, process->GetID(), render_frame_host->GetRoutingID(), delegate->notification_id());
514524
#else
515525
NOTIMPLEMENTED();
516526
#endif

0 commit comments

Comments
 (0)