Merge pull request #180 from atom/no-app-user-model-id

Use application name when app user model ID is not available
This commit is contained in:
Cheng Zhao 2015-11-24 15:13:47 +08:00
commit 5ce2ebf264
5 changed files with 59 additions and 29 deletions

View file

@ -7,7 +7,6 @@
#include "base/win/windows_version.h" #include "base/win/windows_version.h"
#include "browser/win/windows_toast_notification.h" #include "browser/win/windows_toast_notification.h"
#include "common/application_info.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"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
@ -27,7 +26,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

@ -8,26 +8,34 @@
ScopedHString::ScopedHString(const wchar_t* source) ScopedHString::ScopedHString(const wchar_t* source)
: str_(nullptr) { : str_(nullptr) {
Set(source); Reset(source);
} }
ScopedHString::ScopedHString(const std::wstring& source) ScopedHString::ScopedHString(const std::wstring& source)
: str_(nullptr) { : str_(nullptr) {
WindowsCreateString(source.c_str(), source.length(), &str_); Reset(source);
} }
ScopedHString::ScopedHString() : str_(nullptr) { ScopedHString::ScopedHString() : str_(nullptr) {
} }
ScopedHString::~ScopedHString() { ScopedHString::~ScopedHString() {
if (str_) Reset();
WindowsDeleteString(str_);
} }
void ScopedHString::Set(const wchar_t* source) { void ScopedHString::Reset() {
if (str_) { if (str_) {
WindowsDeleteString(str_); WindowsDeleteString(str_);
str_ = nullptr; str_ = nullptr;
} }
}
void ScopedHString::Reset(const wchar_t* source) {
Reset();
WindowsCreateString(source, wcslen(source), &str_); WindowsCreateString(source, wcslen(source), &str_);
} }
void ScopedHString::Reset(const std::wstring& source) {
Reset();
WindowsCreateString(source.c_str(), source.length(), &str_);
}

View file

@ -22,7 +22,9 @@ class ScopedHString {
~ScopedHString(); ~ScopedHString();
// Sets to |source|. // 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. // Returns string.
operator HSTRING() const { return str_; } operator HSTRING() const { return str_; }

View file

@ -9,6 +9,7 @@
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "browser/win/scoped_hstring.h" #include "browser/win/scoped_hstring.h"
#include "common/application_info.h"
#include "content/public/browser/desktop_notification_delegate.h" #include "content/public/browser/desktop_notification_delegate.h"
using namespace ABI::Windows::Data::Xml::Dom; using namespace ABI::Windows::Data::Xml::Dom;
@ -19,37 +20,50 @@ namespace {
bool GetAppUserModelId(ScopedHString* app_id) { bool GetAppUserModelId(ScopedHString* app_id) {
PWSTR current_app_id; PWSTR current_app_id;
if (FAILED(GetCurrentProcessExplicitAppUserModelID(&current_app_id))) if (SUCCEEDED(GetCurrentProcessExplicitAppUserModelID(&current_app_id))) {
return false; app_id->Reset(current_app_id);
CoTaskMemFree(current_app_id);
app_id->Set(current_app_id); } else {
CoTaskMemFree(current_app_id); app_id->Reset(base::UTF8ToUTF16(GetApplicationName()));
}
return app_id->success(); return app_id->success();
} }
} // 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() {
@ -217,7 +231,7 @@ bool WindowsToastNotification::GetTextNodeList(
IXmlDocument* doc, IXmlDocument* doc,
IXmlNodeList** node_list, IXmlNodeList** node_list,
UINT32 req_length) { UINT32 req_length) {
tag->Set(L"text"); tag->Reset(L"text");
if (!tag->success()) if (!tag->success())
return false; return false;

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_;