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
NotificationPresenter* NotificationPresenter::Create() {
return new NotificationPresenterWin;
if (WindowsToastNotification::Initialize())
return new NotificationPresenterWin;
else
return nullptr;
}
NotificationPresenterWin::NotificationPresenterWin() {

View file

@ -29,27 +29,39 @@ bool GetAppUserModelId(ScopedHString* app_id) {
} // namespace
WindowsToastNotification::WindowsToastNotification(
scoped_ptr<content::DesktopNotificationDelegate> 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<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics>
WindowsToastNotification::toast_manager_;
// static
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(
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<content::DesktopNotificationDelegate> delegate)
: delegate_(delegate.Pass()),
weak_factory_(this) {
}
WindowsToastNotification::~WindowsToastNotification() {

View file

@ -33,6 +33,9 @@ using DesktopToastFailedEventHandler =
class WindowsToastNotification {
public:
// Should be called before using this class.
static bool Initialize();
WindowsToastNotification(
scoped_ptr<content::DesktopNotificationDelegate> delegate);
~WindowsToastNotification();
@ -74,10 +77,11 @@ class WindowsToastNotification {
const std::wstring& text);
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_;
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_;
base::WeakPtrFactory<WindowsToastNotification> weak_factory_;