Simplify the management of objects

This commit is contained in:
Cheng Zhao 2015-11-10 20:23:08 +08:00
parent 4f73de0930
commit f9f8a289d9
5 changed files with 58 additions and 60 deletions

View file

@ -24,13 +24,13 @@ PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() {}
PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {} PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {}
NotificationPresenter* PlatformNotificationServiceImpl::notification_presenter() { NotificationPresenter* PlatformNotificationServiceImpl::notification_presenter() {
#if defined(OS_WIN)
// Bail out if on Windows 7 or even lower, no operating will follow
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return nullptr;
#endif
if (!notification_presenter_) { if (!notification_presenter_) {
#if defined(OS_WIN)
// Bail out if on Windows 7 or even lower, no operating will follow
if (base::win::GetVersion() < base::win::VERSION_WIN8) {
return notification_presenter_.get();
}
#endif
// Create a new presenter if on OS X, Linux, or Windows 8+ // Create a new presenter if on OS X, Linux, or Windows 8+
notification_presenter_.reset(NotificationPresenter::Create()); notification_presenter_.reset(NotificationPresenter::Create());
} }

View file

@ -21,13 +21,21 @@ using namespace ABI::Windows::Foundation;
namespace brightray { namespace brightray {
namespace {
void RemoveNotification(base::WeakPtr<WindowsToastNotification> notification) {
if (notification)
notification->DismissNotification();
}
} // namespace
// static // static
NotificationPresenter* NotificationPresenter::Create() { NotificationPresenter* NotificationPresenter::Create() {
return new NotificationPresenterWin; return new NotificationPresenterWin;
} }
NotificationPresenterWin::NotificationPresenterWin() { NotificationPresenterWin::NotificationPresenterWin() {
m_lastNotification = nullptr;
} }
NotificationPresenterWin::~NotificationPresenterWin() { NotificationPresenterWin::~NotificationPresenterWin() {
@ -38,28 +46,14 @@ void NotificationPresenterWin::ShowNotification(
const SkBitmap& icon, const SkBitmap& icon,
scoped_ptr<content::DesktopNotificationDelegate> delegate, scoped_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) { base::Closure* cancel_callback) {
std::wstring title = data.title; // This class manages itself.
std::wstring body = data.body; auto notification = new WindowsToastNotification(
std::string iconPath = data.icon.spec(); GetApplicationName(), delegate.Pass());
std::string appName = GetApplicationName(); notification->ShowNotification(data.title, data.body, data.icon.spec());
// toast notification supported in version >= Windows 8
// for prior versions, use Tray.displayBalloon
if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
wtn = new WindowsToastNotification(appName.c_str(), delegate.Pass());
wtn->ShowNotification(title, body, iconPath, m_lastNotification);
}
if (cancel_callback) { if (cancel_callback) {
*cancel_callback = base::Bind( *cancel_callback = base::Bind(
&NotificationPresenterWin::RemoveNotification, &RemoveNotification, notification->GetWeakPtr());
base::Unretained(this));
}
}
void NotificationPresenterWin::RemoveNotification() {
if (m_lastNotification != nullptr && wtn != NULL) {
wtn->DismissNotification(m_lastNotification);
} }
} }

View file

@ -16,16 +16,10 @@
#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 <windows.ui.notifications.h>
#include <wrl/implements.h>
#include "base/compiler_specific.h"
#include "browser/notification_presenter.h" #include "browser/notification_presenter.h"
namespace brightray { namespace brightray {
class WindowsToastNotification;
class NotificationPresenterWin : public NotificationPresenter { class NotificationPresenterWin : public NotificationPresenter {
public: public:
NotificationPresenterWin(); NotificationPresenterWin();
@ -37,11 +31,8 @@ class NotificationPresenterWin : public NotificationPresenter {
scoped_ptr<content::DesktopNotificationDelegate> delegate, scoped_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) override; base::Closure* cancel_callback) override;
void RemoveNotification();
private: private:
WindowsToastNotification* wtn; DISALLOW_COPY_AND_ASSIGN(NotificationPresenterWin);
Microsoft::WRL::ComPtr<ABI::Windows::UI::Notifications::IToastNotification> m_lastNotification;
}; };
} // namespace } // namespace

View file

@ -21,9 +21,10 @@ HRESULT init = Windows::Foundation::Initialize(RO_INIT_MULTITHREADED);
} // namespace } // namespace
WindowsToastNotification::WindowsToastNotification( WindowsToastNotification::WindowsToastNotification(
const char* app_name, const std::string& app_name,
scoped_ptr<content::DesktopNotificationDelegate> delegate) scoped_ptr<content::DesktopNotificationDelegate> delegate)
: delegate_(delegate.Pass()) { : delegate_(delegate.Pass()),
weak_factory_(this) {
ScopedHString toast_manager_str( ScopedHString toast_manager_str(
RuntimeClass_Windows_UI_Notifications_ToastNotificationManager); RuntimeClass_Windows_UI_Notifications_ToastNotificationManager);
if (!toast_manager_str.success()) if (!toast_manager_str.success())
@ -46,8 +47,7 @@ WindowsToastNotification::~WindowsToastNotification() {
void WindowsToastNotification::ShowNotification( void WindowsToastNotification::ShowNotification(
const std::wstring& title, const std::wstring& title,
const std::wstring& msg, const std::wstring& msg,
std::string icon_path, std::string icon_path) {
ComPtr<IToastNotification>& toast) {
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;
@ -57,25 +57,26 @@ void WindowsToastNotification::ShowNotification(
if (!toast_str.success()) if (!toast_str.success())
return; return;
ComPtr<IToastNotificationFactory> toastFactory; ComPtr<IToastNotificationFactory> toast_factory;
if (FAILED(Windows::Foundation::GetActivationFactory(toast_str, &toastFactory))) if (FAILED(Windows::Foundation::GetActivationFactory(toast_str,
&toast_factory)))
return; return;
if (FAILED(toastFactory->CreateToastNotification(toast_xml.Get(), &toast))) if (FAILED(toast_factory->CreateToastNotification(toast_xml.Get(),
&toast_notification_)))
return; return;
if (FAILED(SetupCallbacks(toast.Get()))) if (FAILED(SetupCallbacks(toast_notification_.Get())))
return; return;
if (FAILED(toast_notifier_->Show(toast.Get()))) if (FAILED(toast_notifier_->Show(toast_notification_.Get())))
return; return;
delegate_->NotificationDisplayed(); delegate_->NotificationDisplayed();
} }
void WindowsToastNotification::DismissNotification( void WindowsToastNotification::DismissNotification() {
ComPtr<IToastNotification> toast) { toast_notifier_->Hide(toast_notification_.Get());
toast_notifier_->Hide(toast.Get());
} }
void WindowsToastNotification::NotificationClicked() { void WindowsToastNotification::NotificationClicked() {

View file

@ -11,6 +11,7 @@
#include <wrl/implements.h> #include <wrl/implements.h>
#include "base/bind.h" #include "base/bind.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/desktop_notification_delegate.h" #include "content/public/browser/desktop_notification_delegate.h"
#include "content/public/common/platform_notification_data.h" #include "content/public/common/platform_notification_data.h"
@ -27,28 +28,27 @@ using DesktopToastActivatedEventHandler =
using DesktopToastDismissedEventHandler = using DesktopToastDismissedEventHandler =
ITypedEventHandler<ToastNotification*, ToastDismissedEventArgs*>; ITypedEventHandler<ToastNotification*, ToastDismissedEventArgs*>;
class ToastEventHandler;
class WindowsToastNotification { class WindowsToastNotification {
public: public:
WindowsToastNotification( WindowsToastNotification(
const char* app_name, const std::string& app_name,
scoped_ptr<content::DesktopNotificationDelegate> delegate); scoped_ptr<content::DesktopNotificationDelegate> delegate);
~WindowsToastNotification(); ~WindowsToastNotification();
void ShowNotification(const std::wstring& title, void ShowNotification(const std::wstring& title,
const std::wstring& msg, const std::wstring& msg,
std::string icon_path, std::string icon_path);
ComPtr<IToastNotification>& toast); void DismissNotification();
void DismissNotification(ComPtr<IToastNotification> toast);
void NotificationClicked(); base::WeakPtr<WindowsToastNotification> GetWeakPtr() {
void NotificationDismissed(); return weak_factory_.GetWeakPtr();
}
private: private:
scoped_ptr<content::DesktopNotificationDelegate> delegate_; friend class ToastEventHandler;
ComPtr<ToastEventHandler> event_handler_;
ComPtr<IToastNotificationManagerStatics> toast_manager_; void NotificationClicked();
ComPtr<IToastNotifier> toast_notifier_; void NotificationDismissed();
bool GetToastXml(IToastNotificationManagerStatics* toastManager, bool GetToastXml(IToastNotificationManagerStatics* toastManager,
const std::wstring& title, const std::wstring& title,
@ -70,6 +70,16 @@ 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(IToastNotification* toast); bool SetupCallbacks(IToastNotification* toast);
scoped_ptr<content::DesktopNotificationDelegate> delegate_;
ComPtr<ToastEventHandler> event_handler_;
ComPtr<IToastNotificationManagerStatics> toast_manager_;
ComPtr<IToastNotifier> toast_notifier_;
ComPtr<IToastNotification> toast_notification_;
base::WeakPtrFactory<WindowsToastNotification> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(WindowsToastNotification);
}; };
@ -85,6 +95,8 @@ class ToastEventHandler : public RuntimeClass<RuntimeClassFlags<ClassicCom>,
private: private:
WindowsToastNotification* notification_; // weak ref. WindowsToastNotification* notification_; // weak ref.
DISALLOW_COPY_AND_ASSIGN(ToastEventHandler);
}; };
} // namespace brightray } // namespace brightray