From b091f27abfc2d280c667f512ce42ab2e17423217 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 25 Dec 2015 11:05:48 +0800 Subject: [PATCH] win: Adapt to new Notification style --- brightray/browser/mac/cocoa_notification.h | 2 + brightray/browser/mac/cocoa_notification.mm | 1 + brightray/browser/notification.h | 2 + .../browser/platform_notification_service.cc | 2 +- .../browser/win/notification_presenter_win.cc | 23 +------ .../browser/win/notification_presenter_win.h | 12 +--- .../browser/win/windows_toast_notification.cc | 65 +++++++++++++------ .../browser/win/windows_toast_notification.h | 36 +++++----- 8 files changed, 72 insertions(+), 71 deletions(-) diff --git a/brightray/browser/mac/cocoa_notification.h b/brightray/browser/mac/cocoa_notification.h index 027e8373de19..9f967a5e5f91 100644 --- a/brightray/browser/mac/cocoa_notification.h +++ b/brightray/browser/mac/cocoa_notification.h @@ -19,8 +19,10 @@ class CocoaNotification : public Notification { NotificationPresenter* presenter); ~CocoaNotification(); + // Notification: void Show(const base::string16& title, const base::string16& msg, + const GURL& icon_url, const SkBitmap& icon) override; void Dismiss() override; diff --git a/brightray/browser/mac/cocoa_notification.mm b/brightray/browser/mac/cocoa_notification.mm index 317211db049c..4b24e10f52b1 100644 --- a/brightray/browser/mac/cocoa_notification.mm +++ b/brightray/browser/mac/cocoa_notification.mm @@ -32,6 +32,7 @@ CocoaNotification::~CocoaNotification() { void CocoaNotification::Show(const base::string16& title, const base::string16& body, + const GURL& icon_url, const SkBitmap& icon) { notification_.reset([[NSUserNotification alloc] init]); [notification_ setTitle:base::SysUTF16ToNSString(title)]; diff --git a/brightray/browser/notification.h b/brightray/browser/notification.h index 9bc4ac44225c..9d0deccbf451 100644 --- a/brightray/browser/notification.h +++ b/brightray/browser/notification.h @@ -8,6 +8,7 @@ #include "base/memory/weak_ptr.h" #include "base/strings/string16.h" +class GURL; class SkBitmap; namespace brightray { @@ -20,6 +21,7 @@ class Notification { // Shows the notification. virtual void Show(const base::string16& title, const base::string16& msg, + const GURL& icon_url, const SkBitmap& icon) = 0; // Closes the notification, this instance will be destroyed after the // notification gets closed. diff --git a/brightray/browser/platform_notification_service.cc b/brightray/browser/platform_notification_service.cc index 0d3ecfc180ba..7d91ce5f2e42 100644 --- a/brightray/browser/platform_notification_service.cc +++ b/brightray/browser/platform_notification_service.cc @@ -57,7 +57,7 @@ void PlatformNotificationService::DisplayNotification( auto notification = presenter->CreateNotification(adapter.get()); if (notification) { ignore_result(adapter.release()); // it will release itself automatically. - notification->Show(data.title, data.body, icon); + notification->Show(data.title, data.body, data.icon, icon); *cancel_callback = base::Bind(&RemoveNotification, notification); } } diff --git a/brightray/browser/win/notification_presenter_win.cc b/brightray/browser/win/notification_presenter_win.cc index 29534d94beee..d3d4f7dc2432 100644 --- a/brightray/browser/win/notification_presenter_win.cc +++ b/brightray/browser/win/notification_presenter_win.cc @@ -31,11 +31,6 @@ bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) { return base::WriteFile(path, data, size) == size; } -void RemoveNotification(base::WeakPtr notification) { - if (notification) - notification->DismissNotification(); -} - } // namespace // static @@ -58,23 +53,7 @@ bool NotificationPresenterWin::Init() { return temp_dir_.CreateUniqueTempDir(); } -void NotificationPresenterWin::ShowNotification( - const content::PlatformNotificationData& data, - const SkBitmap& icon, - scoped_ptr delegate, - base::Closure* cancel_callback) { - // This class manages itself. - auto notification = new WindowsToastNotification(delegate.Pass()); - notification->ShowNotification( - data.title, data.body, SaveIconToFilesystem(icon, data.icon)); - - if (cancel_callback) { - *cancel_callback = base::Bind( - &RemoveNotification, notification->GetWeakPtr()); - } -} - -std::wstring NotificationPresenterWin::SaveIconToFilesystem( +base::string16 NotificationPresenterWin::SaveIconToFilesystem( const SkBitmap& icon, const GURL& origin) { std::string filename = base::MD5String(origin.spec()) + ".png"; base::FilePath path = temp_dir_.path().Append(base::UTF8ToUTF16(filename)); diff --git a/brightray/browser/win/notification_presenter_win.h b/brightray/browser/win/notification_presenter_win.h index 21152c9cab9f..7bcf1a9909dd 100644 --- a/brightray/browser/win/notification_presenter_win.h +++ b/brightray/browser/win/notification_presenter_win.h @@ -16,12 +16,12 @@ #ifndef BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_ #define BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_ -#include - #include "base/files/scoped_temp_dir.h" +#include "base/strings/string16.h" #include "browser/notification_presenter.h" class GURL; +class SkBitmap; namespace brightray { @@ -32,15 +32,9 @@ class NotificationPresenterWin : public NotificationPresenter { bool Init(); - void ShowNotification( - const content::PlatformNotificationData&, - const SkBitmap& icon, - scoped_ptr delegate, - base::Closure* cancel_callback) override; + base::string16 SaveIconToFilesystem(const SkBitmap& icon, const GURL& origin); private: - std::wstring SaveIconToFilesystem(const SkBitmap& icon, const GURL& origin); - base::ScopedTempDir temp_dir_; DISALLOW_COPY_AND_ASSIGN(NotificationPresenterWin); diff --git a/brightray/browser/win/windows_toast_notification.cc b/brightray/browser/win/windows_toast_notification.cc index f2d77edf6775..050dff56935e 100644 --- a/brightray/browser/win/windows_toast_notification.cc +++ b/brightray/browser/win/windows_toast_notification.cc @@ -8,9 +8,10 @@ #include #include "base/strings/utf_string_conversions.h" +#include "browser/notification_delegate.h" #include "browser/win/scoped_hstring.h" +#include "browser/win/notification_presenter_win.h" #include "common/application_info.h" -#include "content/public/browser/desktop_notification_delegate.h" using namespace ABI::Windows::Data::Xml::Dom; @@ -31,6 +32,12 @@ bool GetAppUserModelId(ScopedHString* app_id) { } // namespace +// static +Notification* Notification::Create(NotificationDelegate* delegate, + NotificationPresenter* presenter) { + return new WindowsToastNotification(delegate, presenter); +} + // static ComPtr WindowsToastNotification::toast_manager_; @@ -61,18 +68,25 @@ bool WindowsToastNotification::Initialize() { } WindowsToastNotification::WindowsToastNotification( - scoped_ptr delegate) - : delegate_(delegate.Pass()), - weak_factory_(this) { + NotificationDelegate* delegate, + NotificationPresenter* presenter) + : Notification(delegate, presenter) { } WindowsToastNotification::~WindowsToastNotification() { + // Remove the notification on exit, regardless whether it succeeds. + RemoveCallbacks(toast_notification_.Get()); + Dismiss(); } -void WindowsToastNotification::ShowNotification( - const std::wstring& title, - const std::wstring& msg, - const std::wstring& icon_path) { +void WindowsToastNotification::Show( + const base::string16& title, + const base::string16& msg, + const GURL& icon_url, + const SkBitmap& icon) { + auto presenter_win = static_cast(presenter()); + std::wstring icon_path = presenter_win->SaveIconToFilesystem(icon, icon_url); + ComPtr toast_xml; if(FAILED(GetToastXml(toast_manager_.Get(), title, msg, icon_path, &toast_xml))) return; @@ -97,25 +111,25 @@ void WindowsToastNotification::ShowNotification( if (FAILED(toast_notifier_->Show(toast_notification_.Get()))) return; - delegate_->NotificationDisplayed(); + delegate()->NotificationDisplayed(); } -void WindowsToastNotification::DismissNotification() { +void WindowsToastNotification::Dismiss() { toast_notifier_->Hide(toast_notification_.Get()); } void WindowsToastNotification::NotificationClicked() { - delegate_->NotificationClick(); - delete this; + delegate()->NotificationClick(); + Destroy(); } void WindowsToastNotification::NotificationDismissed() { - delegate_->NotificationClosed(); - delete this; + delegate()->NotificationClosed(); + Destroy(); } void WindowsToastNotification::NotificationFailed() { - delete this; + Destroy(); } bool WindowsToastNotification::GetToastXml( @@ -263,16 +277,27 @@ bool WindowsToastNotification::AppendTextToXml( return SUCCEEDED(node->AppendChild(text_node.Get(), &append_node)); } -bool WindowsToastNotification::SetupCallbacks(ABI::Windows::UI::Notifications::IToastNotification* toast) { - EventRegistrationToken activatedToken, dismissedToken, failedToken; +bool WindowsToastNotification::SetupCallbacks( + ABI::Windows::UI::Notifications::IToastNotification* toast) { event_handler_ = Make(this); - if (FAILED(toast->add_Activated(event_handler_.Get(), &activatedToken))) + if (FAILED(toast->add_Activated(event_handler_.Get(), &activated_token_))) return false; - if (FAILED(toast->add_Dismissed(event_handler_.Get(), &dismissedToken))) + if (FAILED(toast->add_Dismissed(event_handler_.Get(), &dismissed_token_))) return false; - return SUCCEEDED(toast->add_Failed(event_handler_.Get(), &failedToken)); + return SUCCEEDED(toast->add_Failed(event_handler_.Get(), &failed_token_)); +} + +bool WindowsToastNotification::RemoveCallbacks( + ABI::Windows::UI::Notifications::IToastNotification* toast) { + if (FAILED(toast->remove_Activated(activated_token_))) + return false; + + if (FAILED(toast->remove_Dismissed(dismissed_token_))) + return false; + + return SUCCEEDED(toast->remove_Failed(failed_token_)); } /* diff --git a/brightray/browser/win/windows_toast_notification.h b/brightray/browser/win/windows_toast_notification.h index a5c0a3d1d249..02e056669928 100644 --- a/brightray/browser/win/windows_toast_notification.h +++ b/brightray/browser/win/windows_toast_notification.h @@ -10,10 +10,7 @@ #include #include -#include "base/bind.h" -#include "base/memory/weak_ptr.h" -#include "content/public/browser/desktop_notification_delegate.h" -#include "content/public/common/platform_notification_data.h" +#include "browser/notification.h" using namespace Microsoft::WRL; @@ -31,23 +28,22 @@ using DesktopToastFailedEventHandler = ABI::Windows::Foundation::ITypedEventHandler; -class WindowsToastNotification { +class WindowsToastNotification : public Notification { public: - // Should be called before using this class. + // Should only be called by NotificationPresenterWin. static bool Initialize(); - WindowsToastNotification( - scoped_ptr delegate); + WindowsToastNotification(NotificationDelegate* delegate, + NotificationPresenter* presenter); ~WindowsToastNotification(); - void ShowNotification(const std::wstring& title, - const std::wstring& msg, - const std::wstring& icon_path); - void DismissNotification(); - - base::WeakPtr GetWeakPtr() { - return weak_factory_.GetWeakPtr(); - } + protected: + // Notification: + void Show(const base::string16& title, + const base::string16& msg, + const GURL& icon_url, + const SkBitmap& icon) override; + void Dismiss() override; private: friend class ToastEventHandler; @@ -76,16 +72,18 @@ class WindowsToastNotification { ABI::Windows::Data::Xml::Dom::IXmlNode* node, const std::wstring& text); bool SetupCallbacks(ABI::Windows::UI::Notifications::IToastNotification* toast); + bool RemoveCallbacks(ABI::Windows::UI::Notifications::IToastNotification* toast); static ComPtr toast_manager_; static ComPtr toast_notifier_; - scoped_ptr delegate_; + EventRegistrationToken activated_token_; + EventRegistrationToken dismissed_token_; + EventRegistrationToken failed_token_; + ComPtr event_handler_; ComPtr toast_notification_; - base::WeakPtrFactory weak_factory_; - DISALLOW_COPY_AND_ASSIGN(WindowsToastNotification); };