Sanitized notification delegate implementation

I made `brightray::NotificationDelegate` back into just an interface and
extracted the implementation used by `PlatformNotificationService`, so that
the main process notification implementation can inherit only the interface.
This commit is contained in:
Ales Pergl 2017-11-01 23:14:27 +01:00 коммит произвёл Cheng Zhao
Родитель 8750fde6d3
Коммит f3c32647af
5 изменённых файлов: 56 добавлений и 70 удалений

Просмотреть файл

@ -51,8 +51,7 @@ namespace api {
Notification::Notification(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
mate::Arguments* args)
: NotificationDelegate(std::string()) { // FIXME(alexeykuzmin)
mate::Arguments* args) {
InitWith(isolate, wrapper);
presenter_ = brightray::BrowserClient::Get()->GetNotificationPresenter();

Просмотреть файл

@ -1,34 +0,0 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "brightray/browser/notification_delegate.h"
#include "content/public/browser/notification_event_dispatcher.h"
namespace brightray {
NotificationDelegate::NotificationDelegate(const std::string& notification_id)
: notification_id_(notification_id) {
}
void NotificationDelegate::NotificationDestroyed() {
delete this;
}
void NotificationDelegate::NotificationClick() {
content::NotificationEventDispatcher::GetInstance()
->DispatchNonPersistentClickEvent(notification_id_);
}
void NotificationDelegate::NotificationClosed() {
content::NotificationEventDispatcher::GetInstance()
->DispatchNonPersistentCloseEvent(notification_id_);
}
void NotificationDelegate::NotificationDisplayed() {
content::NotificationEventDispatcher::GetInstance()
->DispatchNonPersistentShowEvent(notification_id_);
}
} // namespace brightray

Просмотреть файл

@ -7,36 +7,29 @@
#include <string>
#include "base/macros.h"
namespace brightray {
class NotificationDelegate {
public:
// The native Notification object is destroyed.
virtual void NotificationDestroyed() {}
// Failed to send the notification.
virtual void NotificationFailed() {}
// Notification was replied to
virtual void NotificationReplied(const std::string& reply) {}
virtual void NotificationAction(int index) {}
virtual void NotificationClick() {}
virtual void NotificationClosed() {}
virtual void NotificationDisplayed() {}
namespace brightray {
class NotificationDelegate {
public:
explicit NotificationDelegate(const std::string& notification_id);
virtual ~NotificationDelegate() {}
// The native Notification object is destroyed.
virtual void NotificationDestroyed();
// Failed to send the notification.
virtual void NotificationFailed() {}
// Notification was replied to
virtual void NotificationReplied(const std::string& reply) {}
virtual void NotificationAction(int index) {}
virtual void NotificationClick();
virtual void NotificationClosed();
virtual void NotificationDisplayed();
private:
const std::string notification_id_;
DISALLOW_COPY_AND_ASSIGN(NotificationDelegate);
};
} // namespace brightray
#endif // BRIGHTRAY_BROWSER_NOTIFICATION_DELEGATE_H_
protected:
NotificationDelegate() = default;
~NotificationDelegate() = default;
};
} // namespace brightray
#endif // BRIGHTRAY_BROWSER_NOTIFICATION_DELEGATE_H_

Просмотреть файл

@ -9,6 +9,7 @@
#include "brightray/browser/notification.h"
#include "brightray/browser/notification_delegate.h"
#include "brightray/browser/notification_presenter.h"
#include "content/public/browser/notification_event_dispatcher.h"
#include "content/public/common/notification_resources.h"
#include "content/public/common/platform_notification_data.h"
#include "third_party/skia/include/core/SkBitmap.h"
@ -44,6 +45,34 @@ void OnWebNotificationAllowed(base::WeakPtr<Notification> notification,
}
}
class NotificationDelegateImpl : public brightray::NotificationDelegate {
public:
explicit NotificationDelegateImpl(const std::string& notification_id)
: notification_id_(notification_id) {}
void NotificationDestroyed() override { delete this; }
void NotificationClick() override {
content::NotificationEventDispatcher::GetInstance()
->DispatchNonPersistentClickEvent(notification_id_);
}
void NotificationClosed() override {
content::NotificationEventDispatcher::GetInstance()
->DispatchNonPersistentCloseEvent(notification_id_);
}
void NotificationDisplayed() override {
content::NotificationEventDispatcher::GetInstance()
->DispatchNonPersistentShowEvent(notification_id_);
}
private:
std::string notification_id_;
DISALLOW_COPY_AND_ASSIGN(NotificationDelegateImpl);
};
} // namespace
PlatformNotificationService::PlatformNotificationService(
@ -81,8 +110,8 @@ void PlatformNotificationService::DisplayNotification(
auto presenter = browser_client_->GetNotificationPresenter();
if (!presenter)
return;
brightray::NotificationDelegate* delegate =
new NotificationDelegate(notification_id);
NotificationDelegateImpl* delegate =
new NotificationDelegateImpl(notification_id);
auto notification = presenter->CreateNotification(delegate);
if (notification) {
*cancel_callback = base::Bind(&RemoveNotification, notification);

Просмотреть файл

@ -68,7 +68,6 @@
'browser/network_delegate.cc',
'browser/network_delegate.h',
'browser/notification_delegate.h',
'browser/notification_delegate.cc',
'browser/notification_presenter.cc',
'browser/notification_presenter.h',
'browser/notification.cc',