Add an abstract Notification class

This commit is contained in:
Cheng Zhao 2015-12-24 22:06:41 +08:00
parent 41084883cf
commit 9897f3aab0
5 changed files with 47 additions and 15 deletions

View file

@ -10,16 +10,15 @@
#import <Foundation/Foundation.h>
#include "base/mac/scoped_nsobject.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string16.h"
#include "base/memory/scoped_ptr.h"
#include "browser/notification.h"
#include "content/public/browser/desktop_notification_delegate.h"
@class NotificationDelegate;
class SkBitmap;
namespace brightray {
class CocoaNotification {
class CocoaNotification : public Notification {
public:
static CocoaNotification* FromNSNotification(
NSUserNotification* notification);
@ -30,24 +29,18 @@ class CocoaNotification {
void ShowNotification(const base::string16& title,
const base::string16& msg,
const SkBitmap& icon);
void DismissNotification();
const SkBitmap& icon) override;
void DismissNotification() override;
void NotifyDisplayed();
void NotifyClick();
base::WeakPtr<CocoaNotification> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
private:
static void Cleanup();
scoped_ptr<content::DesktopNotificationDelegate> delegate_;
base::scoped_nsobject<NSUserNotification> notification_;
base::WeakPtrFactory<CocoaNotification> weak_factory_;
static base::scoped_nsobject<NotificationDelegate> notification_delegate_;
static std::set<CocoaNotification*> notifications_;

View file

@ -40,8 +40,7 @@ void CocoaNotification::Cleanup() {
CocoaNotification::CocoaNotification(
scoped_ptr<content::DesktopNotificationDelegate> delegate)
: delegate_(delegate.Pass()),
weak_factory_(this) {
: delegate_(delegate.Pass()) {
if (!notification_delegate_) {
notification_delegate_.reset([[NotificationDelegate alloc] init]);
NSUserNotificationCenter.defaultUserNotificationCenter.delegate =

View file

@ -13,7 +13,7 @@ namespace brightray {
namespace {
void RemoveNotification(base::WeakPtr<CocoaNotification> notification) {
void RemoveNotification(base::WeakPtr<Notification> notification) {
if (notification)
notification->DismissNotification();
}

View file

@ -0,0 +1,39 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef BROWSER_NOTIFICATION_H_
#define BROWSER_NOTIFICATION_H_
#include "base/memory/weak_ptr.h"
#include "base/strings/string16.h"
class SkBitmap;
namespace brightray {
class Notification {
public:
Notification() : weak_factory_(this) {}
virtual void ShowNotification(const base::string16& title,
const base::string16& msg,
const SkBitmap& icon) = 0;
virtual void DismissNotification() = 0;
base::WeakPtr<Notification> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
protected:
virtual ~Notification() {}
private:
base::WeakPtrFactory<Notification> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(Notification);
};
} // namespace brightray
#endif // BROWSER_NOTIFICATION_H_

View file

@ -61,6 +61,7 @@
'browser/network_delegate.cc',
'browser/network_delegate.h',
'browser/notification_presenter.h',
'browser/notification.h',
'browser/permission_manager.cc',
'browser/permission_manager.h',
'browser/platform_notification_service_impl.cc',