From ee4442f964a78e2425bb1b680c9bed0b7bd0f4ee Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Tue, 25 Oct 2016 19:02:06 +0530 Subject: [PATCH] set notification close callback before requesting user permission --- .../browser/linux/libnotify_notification.cc | 6 ++- brightray/browser/mac/cocoa_notification.mm | 5 +- brightray/browser/notification.h | 6 +-- .../browser/platform_notification_service.cc | 51 +++++++++---------- 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/brightray/browser/linux/libnotify_notification.cc b/brightray/browser/linux/libnotify_notification.cc index a3f273c1c027..6946abbc93f2 100644 --- a/brightray/browser/linux/libnotify_notification.cc +++ b/brightray/browser/linux/libnotify_notification.cc @@ -82,8 +82,10 @@ LibnotifyNotification::LibnotifyNotification(NotificationDelegate* delegate, } LibnotifyNotification::~LibnotifyNotification() { - g_signal_handlers_disconnect_by_data(notification_, this); - g_object_unref(notification_); + if (notification_) { + g_signal_handlers_disconnect_by_data(notification_, this); + g_object_unref(notification_); + } } void LibnotifyNotification::Show(const base::string16& title, diff --git a/brightray/browser/mac/cocoa_notification.mm b/brightray/browser/mac/cocoa_notification.mm index e7ed61dbf369..fe21000e376b 100644 --- a/brightray/browser/mac/cocoa_notification.mm +++ b/brightray/browser/mac/cocoa_notification.mm @@ -24,8 +24,9 @@ CocoaNotification::CocoaNotification(NotificationDelegate* delegate, } CocoaNotification::~CocoaNotification() { - [NSUserNotificationCenter.defaultUserNotificationCenter - removeDeliveredNotification:notification_]; + if (notification_) + [NSUserNotificationCenter.defaultUserNotificationCenter + removeDeliveredNotification:notification_]; } void CocoaNotification::Show(const base::string16& title, diff --git a/brightray/browser/notification.h b/brightray/browser/notification.h index afacd50c1931..87096bbe2635 100644 --- a/brightray/browser/notification.h +++ b/brightray/browser/notification.h @@ -34,6 +34,9 @@ class Notification { void NotificationDismissed(); void NotificationFailed(); + // delete this. + void Destroy(); + base::WeakPtr GetWeakPtr() { return weak_factory_.GetWeakPtr(); } @@ -46,9 +49,6 @@ class Notification { NotificationPresenter* presenter); virtual ~Notification(); - // delete this. - void Destroy(); - private: friend class NotificationPresenter; diff --git a/brightray/browser/platform_notification_service.cc b/brightray/browser/platform_notification_service.cc index 77ca31a5bb51..5fabac82c346 100644 --- a/brightray/browser/platform_notification_service.cc +++ b/brightray/browser/platform_notification_service.cc @@ -21,28 +21,17 @@ void RemoveNotification(base::WeakPtr notification) { notification->Dismiss(); } -void OnWebNotificationAllowed( - brightray::BrowserClient* browser_client, - const SkBitmap& icon, - const content::PlatformNotificationData& data, - std::unique_ptr delegate, - base::Closure* cancel_callback, - bool audio_muted, - bool allowed) { - if (!allowed) +void OnWebNotificationAllowed(base::WeakPtr notification, + const SkBitmap& icon, + const content::PlatformNotificationData& data, + bool audio_muted, + bool allowed) { + if (!allowed) { + notification->Destroy(); return; - auto presenter = browser_client->GetNotificationPresenter(); - if (!presenter) - return; - std::unique_ptr adapter( - new NotificationDelegateAdapter(std::move(delegate))); - auto notification = presenter->CreateNotification(adapter.get()); - if (notification) { - ignore_result(adapter.release()); // it will release itself automatically. - notification->Show(data.title, data.body, data.tag, data.icon, icon, - audio_muted ? true : data.silent); - *cancel_callback = base::Bind(&RemoveNotification, notification); } + notification->Show(data.title, data.body, data.tag, data.icon, icon, + audio_muted ? true : data.silent); } } // namespace @@ -77,14 +66,20 @@ void PlatformNotificationService::DisplayNotification( const content::NotificationResources& notification_resources, std::unique_ptr delegate, base::Closure* cancel_callback) { - browser_client_->WebNotificationAllowed( - render_process_id_, - base::Bind(&OnWebNotificationAllowed, - browser_client_, - notification_resources.notification_icon, - notification_data, - base::Passed(&delegate), - cancel_callback)); + auto presenter = browser_client_->GetNotificationPresenter(); + if (!presenter) + return; + std::unique_ptr adapter( + new NotificationDelegateAdapter(std::move(delegate))); + auto notification = presenter->CreateNotification(adapter.get()); + if (notification) { + ignore_result(adapter.release()); // it will release itself automatically. + *cancel_callback = base::Bind(&RemoveNotification, notification); + browser_client_->WebNotificationAllowed( + render_process_id_, base::Bind(&OnWebNotificationAllowed, notification, + notification_resources.notification_icon, + notification_data)); + } } void PlatformNotificationService::DisplayPersistentNotification(