diff --git a/brightray/browser/win/notification_presenter_win.cc b/brightray/browser/win/notification_presenter_win.cc index 8d4fe8bace04..447125f39117 100644 --- a/brightray/browser/win/notification_presenter_win.cc +++ b/brightray/browser/win/notification_presenter_win.cc @@ -7,7 +7,6 @@ #include "base/win/windows_version.h" #include "browser/win/windows_toast_notification.h" -#include "common/application_info.h" #include "content/public/browser/desktop_notification_delegate.h" #include "content/public/common/platform_notification_data.h" #include "third_party/skia/include/core/SkBitmap.h" @@ -27,7 +26,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/scoped_hstring.cc b/brightray/browser/win/scoped_hstring.cc index e3f5cbd29bbb..082ebe762270 100644 --- a/brightray/browser/win/scoped_hstring.cc +++ b/brightray/browser/win/scoped_hstring.cc @@ -8,26 +8,34 @@ ScopedHString::ScopedHString(const wchar_t* source) : str_(nullptr) { - Set(source); + Reset(source); } ScopedHString::ScopedHString(const std::wstring& source) : str_(nullptr) { - WindowsCreateString(source.c_str(), source.length(), &str_); + Reset(source); } ScopedHString::ScopedHString() : str_(nullptr) { } ScopedHString::~ScopedHString() { - if (str_) - WindowsDeleteString(str_); + Reset(); } -void ScopedHString::Set(const wchar_t* source) { +void ScopedHString::Reset() { if (str_) { WindowsDeleteString(str_); str_ = nullptr; } +} + +void ScopedHString::Reset(const wchar_t* source) { + Reset(); WindowsCreateString(source, wcslen(source), &str_); } + +void ScopedHString::Reset(const std::wstring& source) { + Reset(); + WindowsCreateString(source.c_str(), source.length(), &str_); +} diff --git a/brightray/browser/win/scoped_hstring.h b/brightray/browser/win/scoped_hstring.h index de03c0469b3a..5494df2fc5ce 100644 --- a/brightray/browser/win/scoped_hstring.h +++ b/brightray/browser/win/scoped_hstring.h @@ -22,7 +22,9 @@ class ScopedHString { ~ScopedHString(); // Sets to |source|. - void Set(const wchar_t* source); + void Reset(); + void Reset(const wchar_t* source); + void Reset(const std::wstring& source); // Returns string. operator HSTRING() const { return str_; } diff --git a/brightray/browser/win/windows_toast_notification.cc b/brightray/browser/win/windows_toast_notification.cc index 60a20dc329f5..58b4756fed91 100644 --- a/brightray/browser/win/windows_toast_notification.cc +++ b/brightray/browser/win/windows_toast_notification.cc @@ -9,6 +9,7 @@ #include "base/strings/utf_string_conversions.h" #include "browser/win/scoped_hstring.h" +#include "common/application_info.h" #include "content/public/browser/desktop_notification_delegate.h" using namespace ABI::Windows::Data::Xml::Dom; @@ -19,37 +20,50 @@ namespace { bool GetAppUserModelId(ScopedHString* app_id) { PWSTR current_app_id; - if (FAILED(GetCurrentProcessExplicitAppUserModelID(¤t_app_id))) - return false; - - app_id->Set(current_app_id); - CoTaskMemFree(current_app_id); + if (SUCCEEDED(GetCurrentProcessExplicitAppUserModelID(¤t_app_id))) { + app_id->Reset(current_app_id); + CoTaskMemFree(current_app_id); + } else { + app_id->Reset(base::UTF8ToUTF16(GetApplicationName())); + } return app_id->success(); } } // 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() { @@ -217,7 +231,7 @@ bool WindowsToastNotification::GetTextNodeList( IXmlDocument* doc, IXmlNodeList** node_list, UINT32 req_length) { - tag->Set(L"text"); + tag->Reset(L"text"); if (!tag->success()) return false; 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_;