Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
set cancel callback synchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver committed Jul 11, 2016
1 parent 358a610 commit ce3d97d
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions brave/browser/notifications/platform_notification_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ namespace brave {

namespace {

class NotificationProxy : public base::SupportsWeakPtr<NotificationProxy> {
public:
explicit NotificationProxy() {}

void Dismiss() {
if (notification_)
notification_->Dismiss();
}

void set_notification(base::WeakPtr<brightray::Notification> notification) {
notification_ = notification;
}
private:
base::WeakPtr<brightray::Notification> notification_;

DISALLOW_COPY_AND_ASSIGN(NotificationProxy);
};

void OnPermissionResponse(const base::Callback<void(bool)>& callback,
blink::mojom::PermissionStatus status) {
if (status == blink::mojom::PermissionStatus::GRANTED)
Expand All @@ -28,17 +46,16 @@ void OnPermissionResponse(const base::Callback<void(bool)>& callback,
callback.Run(false);
}

void RemoveNotification(base::WeakPtr<brightray::Notification> notification) {
if (notification)
notification->Dismiss();
void RemoveNotification(std::unique_ptr<NotificationProxy> notification_proxy) {
notification_proxy->Dismiss();
}

void OnWebNotificationAllowed(
brightray::BrowserClient* browser_client,
const SkBitmap& icon,
const content::PlatformNotificationData& data,
std::unique_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback,
const base::WeakPtr<NotificationProxy> notification_proxy,
bool allowed) {
if (!allowed)
return;
Expand All @@ -51,8 +68,8 @@ void OnWebNotificationAllowed(
if (notification) {
ignore_result(adapter.release()); // it will release itself automatically.
notification->Show(data.title, data.body, data.tag, data.icon, icon, data.silent);
if (cancel_callback)
*cancel_callback = base::Bind(&RemoveNotification, notification);
if (notification_proxy)
notification_proxy->set_notification(notification);
}
}

Expand Down Expand Up @@ -89,12 +106,22 @@ void PlatformNotificationServiceImpl::DisplayNotification(
const content::NotificationResources& notification_resources,
std::unique_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) {
// cancel_callback must be set when this method returns, but the
// RequestPermission callback may run asynchronously so we use
// the notification proxy as a placeholder until the actual
// notification is available
std::unique_ptr<NotificationProxy> notification_proxy(new NotificationProxy);

auto callback = base::Bind(&OnWebNotificationAllowed,
BraveContentBrowserClient::Get(),
notification_resources.notification_icon,
notification_data,
base::Passed(&delegate),
cancel_callback);
notification_proxy->AsWeakPtr());

// TODO(bridiver) - there is a memory leak on mac because CocoaNotification never sends
// NotificationClosed to the PageNotificationDelegate
*cancel_callback = base::Bind(&RemoveNotification, base::Passed(&notification_proxy));

auto permission_manager = browser_context->GetPermissionManager();
permission_manager->RequestPermission(
Expand Down

0 comments on commit ce3d97d

Please sign in to comment.