From 9897f3aab04b5caa003e3fc19da5123a76eb6c88 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 24 Dec 2015 22:06:41 +0800 Subject: [PATCH] Add an abstract Notification class --- brightray/browser/mac/cocoa_notification.h | 17 +++----- brightray/browser/mac/cocoa_notification.mm | 3 +- .../browser/mac/notification_presenter_mac.mm | 2 +- brightray/browser/notification.h | 39 +++++++++++++++++++ brightray/filenames.gypi | 1 + 5 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 brightray/browser/notification.h diff --git a/brightray/browser/mac/cocoa_notification.h b/brightray/browser/mac/cocoa_notification.h index c1a54678e2..3fefaa5171 100644 --- a/brightray/browser/mac/cocoa_notification.h +++ b/brightray/browser/mac/cocoa_notification.h @@ -10,16 +10,15 @@ #import #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 GetWeakPtr() { - return weak_factory_.GetWeakPtr(); - } - private: static void Cleanup(); scoped_ptr delegate_; base::scoped_nsobject notification_; - base::WeakPtrFactory weak_factory_; - static base::scoped_nsobject notification_delegate_; static std::set notifications_; diff --git a/brightray/browser/mac/cocoa_notification.mm b/brightray/browser/mac/cocoa_notification.mm index 5983cc48a0..ceaf1f0d4c 100644 --- a/brightray/browser/mac/cocoa_notification.mm +++ b/brightray/browser/mac/cocoa_notification.mm @@ -40,8 +40,7 @@ void CocoaNotification::Cleanup() { CocoaNotification::CocoaNotification( scoped_ptr delegate) - : delegate_(delegate.Pass()), - weak_factory_(this) { + : delegate_(delegate.Pass()) { if (!notification_delegate_) { notification_delegate_.reset([[NotificationDelegate alloc] init]); NSUserNotificationCenter.defaultUserNotificationCenter.delegate = diff --git a/brightray/browser/mac/notification_presenter_mac.mm b/brightray/browser/mac/notification_presenter_mac.mm index ead663bcbf..fafde993a9 100644 --- a/brightray/browser/mac/notification_presenter_mac.mm +++ b/brightray/browser/mac/notification_presenter_mac.mm @@ -13,7 +13,7 @@ namespace brightray { namespace { -void RemoveNotification(base::WeakPtr notification) { +void RemoveNotification(base::WeakPtr notification) { if (notification) notification->DismissNotification(); } diff --git a/brightray/browser/notification.h b/brightray/browser/notification.h new file mode 100644 index 0000000000..b629eda8fd --- /dev/null +++ b/brightray/browser/notification.h @@ -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 GetWeakPtr() { + return weak_factory_.GetWeakPtr(); + } + + protected: + virtual ~Notification() {} + + private: + base::WeakPtrFactory weak_factory_; + + DISALLOW_COPY_AND_ASSIGN(Notification); +}; + +} // namespace brightray + +#endif // BROWSER_NOTIFICATION_H_ diff --git a/brightray/filenames.gypi b/brightray/filenames.gypi index 2f7ed8d368..e8d3b18009 100644 --- a/brightray/filenames.gypi +++ b/brightray/filenames.gypi @@ -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',