From 3b09c370d4ca5af9a05c92f753519c0f2a43eeab Mon Sep 17 00:00:00 2001 From: Robo Date: Sat, 23 Jan 2016 19:02:30 +0530 Subject: [PATCH] allow client to decide displaying web notifications --- brightray/browser/browser_client.h | 6 +++ .../browser/platform_notification_service.cc | 44 ++++++++++++++----- .../browser/platform_notification_service.h | 1 + 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/brightray/browser/browser_client.h b/brightray/browser/browser_client.h index bb6575c9b2b6..9b7cff7fe4cf 100644 --- a/brightray/browser/browser_client.h +++ b/brightray/browser/browser_client.h @@ -28,6 +28,12 @@ class BrowserClient : public content::ContentBrowserClient { NotificationPresenter* GetNotificationPresenter(); + // Subclasses should override this to enable or disable WebNotification. + virtual void WebNotificationAllowed(int render_process_id, + const base::Callback& callback) { + callback.Run(true); + } + protected: // Subclasses should override this to provide their own BrowserMainParts // implementation. The lifetime of the returned instance is managed by the diff --git a/brightray/browser/platform_notification_service.cc b/brightray/browser/platform_notification_service.cc index e4658f35ffb2..53b94242c80d 100644 --- a/brightray/browser/platform_notification_service.cc +++ b/brightray/browser/platform_notification_service.cc @@ -9,6 +9,7 @@ #include "browser/notification_delegate_adapter.h" #include "browser/notification_presenter.h" #include "content/public/common/platform_notification_data.h" +#include "third_party/skia/include/core/SkBitmap.h" namespace brightray { @@ -19,11 +20,34 @@ void RemoveNotification(base::WeakPtr notification) { notification->Dismiss(); } +void OnWebNotificationAllowed( + brightray::BrowserClient* browser_client, + const SkBitmap& icon, + const content::PlatformNotificationData& data, + scoped_ptr delegate, + base::Closure* cancel_callback, + bool allowed) { + if (!allowed) + return; + auto presenter = browser_client->GetNotificationPresenter(); + if (!presenter) + return; + scoped_ptr adapter( + new NotificationDelegateAdapter(delegate.Pass())); + auto notification = presenter->CreateNotification(adapter.get()); + if (notification) { + ignore_result(adapter.release()); // it will release itself automatically. + notification->Show(data.title, data.body, data.icon, icon, data.silent); + *cancel_callback = base::Bind(&RemoveNotification, notification); + } +} + } // namespace PlatformNotificationService::PlatformNotificationService( BrowserClient* browser_client) - : browser_client_(browser_client) { + : browser_client_(browser_client), + render_process_id_(0) { } PlatformNotificationService::~PlatformNotificationService() {} @@ -32,6 +56,7 @@ blink::WebNotificationPermission PlatformNotificationService::CheckPermissionOnU content::BrowserContext* browser_context, const GURL& origin, int render_process_id) { + render_process_id_ = render_process_id; return blink::WebNotificationPermissionAllowed; } @@ -49,17 +74,12 @@ void PlatformNotificationService::DisplayNotification( const content::PlatformNotificationData& data, scoped_ptr delegate, base::Closure* cancel_callback) { - auto presenter = browser_client_->GetNotificationPresenter(); - if (!presenter) - return; - scoped_ptr adapter( - new NotificationDelegateAdapter(delegate.Pass())); - auto notification = presenter->CreateNotification(adapter.get()); - if (notification) { - ignore_result(adapter.release()); // it will release itself automatically. - notification->Show(data.title, data.body, data.icon, icon, data.silent); - *cancel_callback = base::Bind(&RemoveNotification, notification); - } + browser_client_->WebNotificationAllowed( + render_process_id_, + base::Bind(&OnWebNotificationAllowed, + browser_client_, icon, data, + base::Passed(&delegate), + cancel_callback)); } void PlatformNotificationService::DisplayPersistentNotification( diff --git a/brightray/browser/platform_notification_service.h b/brightray/browser/platform_notification_service.h index 0c598965df4e..c84a02aa02e6 100644 --- a/brightray/browser/platform_notification_service.h +++ b/brightray/browser/platform_notification_service.h @@ -48,6 +48,7 @@ class PlatformNotificationService std::set* displayed_notifications) override; private: + int render_process_id_; BrowserClient* browser_client_; DISALLOW_COPY_AND_ASSIGN(PlatformNotificationService);