win: Adapt to new Notification style
This commit is contained in:
parent
5b7c7be804
commit
b091f27abf
8 changed files with 72 additions and 71 deletions
|
@ -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;
|
||||
|
||||
|
|
|
@ -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)];
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,11 +31,6 @@ bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) {
|
|||
return base::WriteFile(path, data, size) == size;
|
||||
}
|
||||
|
||||
void RemoveNotification(base::WeakPtr<WindowsToastNotification> 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<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(
|
||||
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));
|
||||
|
|
|
@ -16,12 +16,12 @@
|
|||
#ifndef 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/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<content::DesktopNotificationDelegate> 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);
|
||||
|
|
|
@ -8,9 +8,10 @@
|
|||
#include <shlobj.h>
|
||||
|
||||
#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<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics>
|
||||
WindowsToastNotification::toast_manager_;
|
||||
|
@ -61,18 +68,25 @@ bool WindowsToastNotification::Initialize() {
|
|||
}
|
||||
|
||||
WindowsToastNotification::WindowsToastNotification(
|
||||
scoped_ptr<content::DesktopNotificationDelegate> 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<NotificationPresenterWin*>(presenter());
|
||||
std::wstring icon_path = presenter_win->SaveIconToFilesystem(icon, icon_url);
|
||||
|
||||
ComPtr<IXmlDocument> 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<ToastEventHandler>(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_));
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -10,10 +10,7 @@
|
|||
#include <windows.ui.notifications.h>
|
||||
#include <wrl/implements.h>
|
||||
|
||||
#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<ABI::Windows::UI::Notifications::ToastNotification*,
|
||||
ABI::Windows::UI::Notifications::ToastFailedEventArgs*>;
|
||||
|
||||
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<content::DesktopNotificationDelegate> 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<WindowsToastNotification> 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<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics> toast_manager_;
|
||||
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<ABI::Windows::UI::Notifications::IToastNotification> toast_notification_;
|
||||
|
||||
base::WeakPtrFactory<WindowsToastNotification> weak_factory_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(WindowsToastNotification);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue