Only create NotificationPresenter when succeeded to initailize toast manager

This fix crash when we failed to initailize toast manager.
This commit is contained in:
Cheng Zhao 2015-11-24 14:40:58 +08:00
parent 2468c7c34e
commit c060539562
3 changed files with 35 additions and 16 deletions

View file

@ -27,7 +27,10 @@ void RemoveNotification(base::WeakPtr<WindowsToastNotification> notification) {
// static // static
NotificationPresenter* NotificationPresenter::Create() { NotificationPresenter* NotificationPresenter::Create() {
return new NotificationPresenterWin; if (WindowsToastNotification::Initialize())
return new NotificationPresenterWin;
else
return nullptr;
} }
NotificationPresenterWin::NotificationPresenterWin() { NotificationPresenterWin::NotificationPresenterWin() {

View file

@ -29,27 +29,39 @@ bool GetAppUserModelId(ScopedHString* app_id) {
} // namespace } // namespace
WindowsToastNotification::WindowsToastNotification( // static
scoped_ptr<content::DesktopNotificationDelegate> delegate) ComPtr<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics>
: delegate_(delegate.Pass()), WindowsToastNotification::toast_manager_;
weak_factory_(this) {
// If it wasn't for Windows 7, we could do this statically // static
HRESULT init = Windows::Foundation::Initialize(RO_INIT_MULTITHREADED); ComPtr<ABI::Windows::UI::Notifications::IToastNotifier>
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( ScopedHString toast_manager_str(
RuntimeClass_Windows_UI_Notifications_ToastNotificationManager); RuntimeClass_Windows_UI_Notifications_ToastNotificationManager);
if (!toast_manager_str.success()) if (!toast_manager_str.success())
return; return false;
HRESULT hr = Windows::Foundation::GetActivationFactory( if (FAILED(Windows::Foundation::GetActivationFactory(toast_manager_str,
toast_manager_str, &toast_manager_); &toast_manager_)))
if (FAILED(hr)) return false;
return;
ScopedHString app_id; ScopedHString app_id;
if (!GetAppUserModelId(&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<content::DesktopNotificationDelegate> delegate)
: delegate_(delegate.Pass()),
weak_factory_(this) {
} }
WindowsToastNotification::~WindowsToastNotification() { WindowsToastNotification::~WindowsToastNotification() {

View file

@ -33,6 +33,9 @@ using DesktopToastFailedEventHandler =
class WindowsToastNotification { class WindowsToastNotification {
public: public:
// Should be called before using this class.
static bool Initialize();
WindowsToastNotification( WindowsToastNotification(
scoped_ptr<content::DesktopNotificationDelegate> delegate); scoped_ptr<content::DesktopNotificationDelegate> delegate);
~WindowsToastNotification(); ~WindowsToastNotification();
@ -74,10 +77,11 @@ class WindowsToastNotification {
const std::wstring& text); const std::wstring& text);
bool SetupCallbacks(ABI::Windows::UI::Notifications::IToastNotification* toast); bool SetupCallbacks(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_; scoped_ptr<content::DesktopNotificationDelegate> delegate_;
ComPtr<ToastEventHandler> event_handler_; ComPtr<ToastEventHandler> event_handler_;
ComPtr<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics> toast_manager_;
ComPtr<ABI::Windows::UI::Notifications::IToastNotifier> toast_notifier_;
ComPtr<ABI::Windows::UI::Notifications::IToastNotification> toast_notification_; ComPtr<ABI::Windows::UI::Notifications::IToastNotification> toast_notification_;
base::WeakPtrFactory<WindowsToastNotification> weak_factory_; base::WeakPtrFactory<WindowsToastNotification> weak_factory_;