From c060539562523507833b3d55641d7a7e9439cac8 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 24 Nov 2015 14:40:58 +0800 Subject: [PATCH] Only create NotificationPresenter when succeeded to initailize toast manager This fix crash when we failed to initailize toast manager. --- .../browser/win/notification_presenter_win.cc | 5 ++- .../browser/win/windows_toast_notification.cc | 38 ++++++++++++------- .../browser/win/windows_toast_notification.h | 8 +++- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/brightray/browser/win/notification_presenter_win.cc b/brightray/browser/win/notification_presenter_win.cc index 8d4fe8bace04..63feb44224ef 100644 --- a/brightray/browser/win/notification_presenter_win.cc +++ b/brightray/browser/win/notification_presenter_win.cc @@ -27,7 +27,10 @@ void RemoveNotification(base::WeakPtr notification) { // static NotificationPresenter* NotificationPresenter::Create() { - return new NotificationPresenterWin; + if (WindowsToastNotification::Initialize()) + return new NotificationPresenterWin; + else + return nullptr; } NotificationPresenterWin::NotificationPresenterWin() { diff --git a/brightray/browser/win/windows_toast_notification.cc b/brightray/browser/win/windows_toast_notification.cc index 60a20dc329f5..64806430a7eb 100644 --- a/brightray/browser/win/windows_toast_notification.cc +++ b/brightray/browser/win/windows_toast_notification.cc @@ -29,27 +29,39 @@ bool GetAppUserModelId(ScopedHString* app_id) { } // namespace -WindowsToastNotification::WindowsToastNotification( - scoped_ptr delegate) - : delegate_(delegate.Pass()), - weak_factory_(this) { - // If it wasn't for Windows 7, we could do this statically - HRESULT init = Windows::Foundation::Initialize(RO_INIT_MULTITHREADED); +// static +ComPtr + WindowsToastNotification::toast_manager_; + +// static +ComPtr + WindowsToastNotification::toast_notifier_; + +// static +bool WindowsToastNotification::Initialize() { + // Just initialize, don't care if it fails or already initialized. + Windows::Foundation::Initialize(RO_INIT_MULTITHREADED); ScopedHString toast_manager_str( RuntimeClass_Windows_UI_Notifications_ToastNotificationManager); if (!toast_manager_str.success()) - return; - HRESULT hr = Windows::Foundation::GetActivationFactory( - toast_manager_str, &toast_manager_); - if (FAILED(hr)) - return; + return false; + if (FAILED(Windows::Foundation::GetActivationFactory(toast_manager_str, + &toast_manager_))) + return false; ScopedHString app_id; if (!GetAppUserModelId(&app_id)) - return; + return false; - toast_manager_->CreateToastNotifierWithId(app_id, &toast_notifier_); + return SUCCEEDED( + toast_manager_->CreateToastNotifierWithId(app_id, &toast_notifier_)); +} + +WindowsToastNotification::WindowsToastNotification( + scoped_ptr delegate) + : delegate_(delegate.Pass()), + weak_factory_(this) { } WindowsToastNotification::~WindowsToastNotification() { diff --git a/brightray/browser/win/windows_toast_notification.h b/brightray/browser/win/windows_toast_notification.h index 87dffae438b2..acae591bc478 100644 --- a/brightray/browser/win/windows_toast_notification.h +++ b/brightray/browser/win/windows_toast_notification.h @@ -33,6 +33,9 @@ using DesktopToastFailedEventHandler = class WindowsToastNotification { public: + // Should be called before using this class. + static bool Initialize(); + WindowsToastNotification( scoped_ptr delegate); ~WindowsToastNotification(); @@ -74,10 +77,11 @@ class WindowsToastNotification { const std::wstring& text); bool SetupCallbacks(ABI::Windows::UI::Notifications::IToastNotification* toast); + static ComPtr toast_manager_; + static ComPtr toast_notifier_; + scoped_ptr delegate_; ComPtr event_handler_; - ComPtr toast_manager_; - ComPtr toast_notifier_; ComPtr toast_notification_; base::WeakPtrFactory weak_factory_;