win: Adapt to new Notification style

This commit is contained in:
Cheng Zhao 2015-12-25 11:05:48 +08:00
parent 5b7c7be804
commit b091f27abf
8 changed files with 72 additions and 71 deletions

View file

@ -19,8 +19,10 @@ class CocoaNotification : public Notification {
NotificationPresenter* presenter); NotificationPresenter* presenter);
~CocoaNotification(); ~CocoaNotification();
// Notification:
void Show(const base::string16& title, void Show(const base::string16& title,
const base::string16& msg, const base::string16& msg,
const GURL& icon_url,
const SkBitmap& icon) override; const SkBitmap& icon) override;
void Dismiss() override; void Dismiss() override;

View file

@ -32,6 +32,7 @@ CocoaNotification::~CocoaNotification() {
void CocoaNotification::Show(const base::string16& title, void CocoaNotification::Show(const base::string16& title,
const base::string16& body, const base::string16& body,
const GURL& icon_url,
const SkBitmap& icon) { const SkBitmap& icon) {
notification_.reset([[NSUserNotification alloc] init]); notification_.reset([[NSUserNotification alloc] init]);
[notification_ setTitle:base::SysUTF16ToNSString(title)]; [notification_ setTitle:base::SysUTF16ToNSString(title)];

View file

@ -8,6 +8,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
class GURL;
class SkBitmap; class SkBitmap;
namespace brightray { namespace brightray {
@ -20,6 +21,7 @@ class Notification {
// Shows the notification. // Shows the notification.
virtual void Show(const base::string16& title, virtual void Show(const base::string16& title,
const base::string16& msg, const base::string16& msg,
const GURL& icon_url,
const SkBitmap& icon) = 0; const SkBitmap& icon) = 0;
// Closes the notification, this instance will be destroyed after the // Closes the notification, this instance will be destroyed after the
// notification gets closed. // notification gets closed.

View file

@ -57,7 +57,7 @@ void PlatformNotificationService::DisplayNotification(
auto notification = presenter->CreateNotification(adapter.get()); auto notification = presenter->CreateNotification(adapter.get());
if (notification) { if (notification) {
ignore_result(adapter.release()); // it will release itself automatically. 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); *cancel_callback = base::Bind(&RemoveNotification, notification);
} }
} }

View file

@ -31,11 +31,6 @@ bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) {
return base::WriteFile(path, data, size) == size; return base::WriteFile(path, data, size) == size;
} }
void RemoveNotification(base::WeakPtr<WindowsToastNotification> notification) {
if (notification)
notification->DismissNotification();
}
} // namespace } // namespace
// static // static
@ -58,23 +53,7 @@ bool NotificationPresenterWin::Init() {
return temp_dir_.CreateUniqueTempDir(); return temp_dir_.CreateUniqueTempDir();
} }
void NotificationPresenterWin::ShowNotification( base::string16 NotificationPresenterWin::SaveIconToFilesystem(
const content::PlatformNotificationData& data,
const SkBitmap& icon,
scoped_ptr<content::DesktopNotificationDelegate> 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(
const SkBitmap& icon, const GURL& origin) { const SkBitmap& icon, const GURL& origin) {
std::string filename = base::MD5String(origin.spec()) + ".png"; std::string filename = base::MD5String(origin.spec()) + ".png";
base::FilePath path = temp_dir_.path().Append(base::UTF8ToUTF16(filename)); base::FilePath path = temp_dir_.path().Append(base::UTF8ToUTF16(filename));

View file

@ -16,12 +16,12 @@
#ifndef BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_ #ifndef BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_
#define BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_ #define BRIGHTRAY_BROWSER_WIN_NOTIFICATION_PRESENTER_WIN_H_
#include <string>
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/strings/string16.h"
#include "browser/notification_presenter.h" #include "browser/notification_presenter.h"
class GURL; class GURL;
class SkBitmap;
namespace brightray { namespace brightray {
@ -32,15 +32,9 @@ class NotificationPresenterWin : public NotificationPresenter {
bool Init(); bool Init();
void ShowNotification( base::string16 SaveIconToFilesystem(const SkBitmap& icon, const GURL& origin);
const content::PlatformNotificationData&,
const SkBitmap& icon,
scoped_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) override;
private: private:
std::wstring SaveIconToFilesystem(const SkBitmap& icon, const GURL& origin);
base::ScopedTempDir temp_dir_; base::ScopedTempDir temp_dir_;
DISALLOW_COPY_AND_ASSIGN(NotificationPresenterWin); DISALLOW_COPY_AND_ASSIGN(NotificationPresenterWin);

View file

@ -8,9 +8,10 @@
#include <shlobj.h> #include <shlobj.h>
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "browser/notification_delegate.h"
#include "browser/win/scoped_hstring.h" #include "browser/win/scoped_hstring.h"
#include "browser/win/notification_presenter_win.h"
#include "common/application_info.h" #include "common/application_info.h"
#include "content/public/browser/desktop_notification_delegate.h"
using namespace ABI::Windows::Data::Xml::Dom; using namespace ABI::Windows::Data::Xml::Dom;
@ -31,6 +32,12 @@ bool GetAppUserModelId(ScopedHString* app_id) {
} // namespace } // namespace
// static
Notification* Notification::Create(NotificationDelegate* delegate,
NotificationPresenter* presenter) {
return new WindowsToastNotification(delegate, presenter);
}
// static // static
ComPtr<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics> ComPtr<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics>
WindowsToastNotification::toast_manager_; WindowsToastNotification::toast_manager_;
@ -61,18 +68,25 @@ bool WindowsToastNotification::Initialize() {
} }
WindowsToastNotification::WindowsToastNotification( WindowsToastNotification::WindowsToastNotification(
scoped_ptr<content::DesktopNotificationDelegate> delegate) NotificationDelegate* delegate,
: delegate_(delegate.Pass()), NotificationPresenter* presenter)
weak_factory_(this) { : Notification(delegate, presenter) {
} }
WindowsToastNotification::~WindowsToastNotification() { WindowsToastNotification::~WindowsToastNotification() {
// Remove the notification on exit, regardless whether it succeeds.
RemoveCallbacks(toast_notification_.Get());
Dismiss();
} }
void WindowsToastNotification::ShowNotification( void WindowsToastNotification::Show(
const std::wstring& title, const base::string16& title,
const std::wstring& msg, const base::string16& msg,
const std::wstring& icon_path) { const GURL& icon_url,
const SkBitmap& icon) {
auto presenter_win = static_cast<NotificationPresenterWin*>(presenter());
std::wstring icon_path = presenter_win->SaveIconToFilesystem(icon, icon_url);
ComPtr<IXmlDocument> toast_xml; ComPtr<IXmlDocument> toast_xml;
if(FAILED(GetToastXml(toast_manager_.Get(), title, msg, icon_path, &toast_xml))) if(FAILED(GetToastXml(toast_manager_.Get(), title, msg, icon_path, &toast_xml)))
return; return;
@ -97,25 +111,25 @@ void WindowsToastNotification::ShowNotification(
if (FAILED(toast_notifier_->Show(toast_notification_.Get()))) if (FAILED(toast_notifier_->Show(toast_notification_.Get())))
return; return;
delegate_->NotificationDisplayed(); delegate()->NotificationDisplayed();
} }
void WindowsToastNotification::DismissNotification() { void WindowsToastNotification::Dismiss() {
toast_notifier_->Hide(toast_notification_.Get()); toast_notifier_->Hide(toast_notification_.Get());
} }
void WindowsToastNotification::NotificationClicked() { void WindowsToastNotification::NotificationClicked() {
delegate_->NotificationClick(); delegate()->NotificationClick();
delete this; Destroy();
} }
void WindowsToastNotification::NotificationDismissed() { void WindowsToastNotification::NotificationDismissed() {
delegate_->NotificationClosed(); delegate()->NotificationClosed();
delete this; Destroy();
} }
void WindowsToastNotification::NotificationFailed() { void WindowsToastNotification::NotificationFailed() {
delete this; Destroy();
} }
bool WindowsToastNotification::GetToastXml( bool WindowsToastNotification::GetToastXml(
@ -263,16 +277,27 @@ bool WindowsToastNotification::AppendTextToXml(
return SUCCEEDED(node->AppendChild(text_node.Get(), &append_node)); return SUCCEEDED(node->AppendChild(text_node.Get(), &append_node));
} }
bool WindowsToastNotification::SetupCallbacks(ABI::Windows::UI::Notifications::IToastNotification* toast) { bool WindowsToastNotification::SetupCallbacks(
EventRegistrationToken activatedToken, dismissedToken, failedToken; ABI::Windows::UI::Notifications::IToastNotification* toast) {
event_handler_ = Make<ToastEventHandler>(this); event_handler_ = Make<ToastEventHandler>(this);
if (FAILED(toast->add_Activated(event_handler_.Get(), &activatedToken))) if (FAILED(toast->add_Activated(event_handler_.Get(), &activated_token_)))
return false; return false;
if (FAILED(toast->add_Dismissed(event_handler_.Get(), &dismissedToken))) if (FAILED(toast->add_Dismissed(event_handler_.Get(), &dismissed_token_)))
return false; 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_));
} }
/* /*

View file

@ -10,10 +10,7 @@
#include <windows.ui.notifications.h> #include <windows.ui.notifications.h>
#include <wrl/implements.h> #include <wrl/implements.h>
#include "base/bind.h" #include "browser/notification.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/desktop_notification_delegate.h"
#include "content/public/common/platform_notification_data.h"
using namespace Microsoft::WRL; using namespace Microsoft::WRL;
@ -31,23 +28,22 @@ using DesktopToastFailedEventHandler =
ABI::Windows::Foundation::ITypedEventHandler<ABI::Windows::UI::Notifications::ToastNotification*, ABI::Windows::Foundation::ITypedEventHandler<ABI::Windows::UI::Notifications::ToastNotification*,
ABI::Windows::UI::Notifications::ToastFailedEventArgs*>; ABI::Windows::UI::Notifications::ToastFailedEventArgs*>;
class WindowsToastNotification { class WindowsToastNotification : public Notification {
public: public:
// Should be called before using this class. // Should only be called by NotificationPresenterWin.
static bool Initialize(); static bool Initialize();
WindowsToastNotification( WindowsToastNotification(NotificationDelegate* delegate,
scoped_ptr<content::DesktopNotificationDelegate> delegate); NotificationPresenter* presenter);
~WindowsToastNotification(); ~WindowsToastNotification();
void ShowNotification(const std::wstring& title, protected:
const std::wstring& msg, // Notification:
const std::wstring& icon_path); void Show(const base::string16& title,
void DismissNotification(); const base::string16& msg,
const GURL& icon_url,
base::WeakPtr<WindowsToastNotification> GetWeakPtr() { const SkBitmap& icon) override;
return weak_factory_.GetWeakPtr(); void Dismiss() override;
}
private: private:
friend class ToastEventHandler; friend class ToastEventHandler;
@ -76,16 +72,18 @@ class WindowsToastNotification {
ABI::Windows::Data::Xml::Dom::IXmlNode* node, ABI::Windows::Data::Xml::Dom::IXmlNode* node,
const std::wstring& text); const std::wstring& text);
bool SetupCallbacks(ABI::Windows::UI::Notifications::IToastNotification* toast); bool SetupCallbacks(ABI::Windows::UI::Notifications::IToastNotification* toast);
bool RemoveCallbacks(ABI::Windows::UI::Notifications::IToastNotification* toast);
static ComPtr<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics> toast_manager_; static ComPtr<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics> toast_manager_;
static ComPtr<ABI::Windows::UI::Notifications::IToastNotifier> toast_notifier_; static ComPtr<ABI::Windows::UI::Notifications::IToastNotifier> toast_notifier_;
scoped_ptr<content::DesktopNotificationDelegate> delegate_; EventRegistrationToken activated_token_;
EventRegistrationToken dismissed_token_;
EventRegistrationToken failed_token_;
ComPtr<ToastEventHandler> event_handler_; ComPtr<ToastEventHandler> event_handler_;
ComPtr<ABI::Windows::UI::Notifications::IToastNotification> toast_notification_; ComPtr<ABI::Windows::UI::Notifications::IToastNotification> toast_notification_;
base::WeakPtrFactory<WindowsToastNotification> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(WindowsToastNotification); DISALLOW_COPY_AND_ASSIGN(WindowsToastNotification);
}; };