Merge pull request #195 from deepak1556/notification_disable_patch

allow client to decide displaying web notifications
This commit is contained in:
Cheng Zhao 2016-01-27 21:02:52 +08:00
commit 144061cb2e
3 changed files with 39 additions and 12 deletions

View file

@ -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<void(bool)>& 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

View file

@ -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) {
notification->Dismiss();
}
void OnWebNotificationAllowed(
brightray::BrowserClient* browser_client,
const SkBitmap& icon,
const content::PlatformNotificationData& data,
scoped_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback,
bool allowed) {
if (!allowed)
return;
auto presenter = browser_client->GetNotificationPresenter();
if (!presenter)
return;
scoped_ptr<NotificationDelegateAdapter> 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<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) {
auto presenter = browser_client_->GetNotificationPresenter();
if (!presenter)
return;
scoped_ptr<NotificationDelegateAdapter> 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(

View file

@ -48,6 +48,7 @@ class PlatformNotificationService
std::set<std::string>* displayed_notifications) override;
private:
int render_process_id_;
BrowserClient* browser_client_;
DISALLOW_COPY_AND_ASSIGN(PlatformNotificationService);