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

View file

@ -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_);
}

View file

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

View file

@ -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(&current_app_id)))
return false;
app_id->Set(current_app_id);
CoTaskMemFree(current_app_id);
if (SUCCEEDED(GetCurrentProcessExplicitAppUserModelID(&current_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<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() {
@ -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;

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