Merge pull request #195 from deepak1556/notification_disable_patch
allow client to decide displaying web notifications
This commit is contained in:
commit
144061cb2e
3 changed files with 39 additions and 12 deletions
|
@ -28,6 +28,12 @@ class BrowserClient : public content::ContentBrowserClient {
|
||||||
|
|
||||||
NotificationPresenter* GetNotificationPresenter();
|
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:
|
protected:
|
||||||
// Subclasses should override this to provide their own BrowserMainParts
|
// Subclasses should override this to provide their own BrowserMainParts
|
||||||
// implementation. The lifetime of the returned instance is managed by the
|
// implementation. The lifetime of the returned instance is managed by the
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include "browser/notification_delegate_adapter.h"
|
#include "browser/notification_delegate_adapter.h"
|
||||||
#include "browser/notification_presenter.h"
|
#include "browser/notification_presenter.h"
|
||||||
#include "content/public/common/platform_notification_data.h"
|
#include "content/public/common/platform_notification_data.h"
|
||||||
|
#include "third_party/skia/include/core/SkBitmap.h"
|
||||||
|
|
||||||
namespace brightray {
|
namespace brightray {
|
||||||
|
|
||||||
|
@ -19,11 +20,34 @@ void RemoveNotification(base::WeakPtr<Notification> notification) {
|
||||||
notification->Dismiss();
|
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
|
} // namespace
|
||||||
|
|
||||||
PlatformNotificationService::PlatformNotificationService(
|
PlatformNotificationService::PlatformNotificationService(
|
||||||
BrowserClient* browser_client)
|
BrowserClient* browser_client)
|
||||||
: browser_client_(browser_client) {
|
: browser_client_(browser_client),
|
||||||
|
render_process_id_(0) {
|
||||||
}
|
}
|
||||||
|
|
||||||
PlatformNotificationService::~PlatformNotificationService() {}
|
PlatformNotificationService::~PlatformNotificationService() {}
|
||||||
|
@ -32,6 +56,7 @@ blink::WebNotificationPermission PlatformNotificationService::CheckPermissionOnU
|
||||||
content::BrowserContext* browser_context,
|
content::BrowserContext* browser_context,
|
||||||
const GURL& origin,
|
const GURL& origin,
|
||||||
int render_process_id) {
|
int render_process_id) {
|
||||||
|
render_process_id_ = render_process_id;
|
||||||
return blink::WebNotificationPermissionAllowed;
|
return blink::WebNotificationPermissionAllowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,17 +74,12 @@ void PlatformNotificationService::DisplayNotification(
|
||||||
const content::PlatformNotificationData& data,
|
const content::PlatformNotificationData& data,
|
||||||
scoped_ptr<content::DesktopNotificationDelegate> delegate,
|
scoped_ptr<content::DesktopNotificationDelegate> delegate,
|
||||||
base::Closure* cancel_callback) {
|
base::Closure* cancel_callback) {
|
||||||
auto presenter = browser_client_->GetNotificationPresenter();
|
browser_client_->WebNotificationAllowed(
|
||||||
if (!presenter)
|
render_process_id_,
|
||||||
return;
|
base::Bind(&OnWebNotificationAllowed,
|
||||||
scoped_ptr<NotificationDelegateAdapter> adapter(
|
browser_client_, icon, data,
|
||||||
new NotificationDelegateAdapter(delegate.Pass()));
|
base::Passed(&delegate),
|
||||||
auto notification = presenter->CreateNotification(adapter.get());
|
cancel_callback));
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlatformNotificationService::DisplayPersistentNotification(
|
void PlatformNotificationService::DisplayPersistentNotification(
|
||||||
|
|
|
@ -48,6 +48,7 @@ class PlatformNotificationService
|
||||||
std::set<std::string>* displayed_notifications) override;
|
std::set<std::string>* displayed_notifications) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
int render_process_id_;
|
||||||
BrowserClient* browser_client_;
|
BrowserClient* browser_client_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(PlatformNotificationService);
|
DISALLOW_COPY_AND_ASSIGN(PlatformNotificationService);
|
||||||
|
|
Loading…
Reference in a new issue