Merge pull request #11431 from electron/notifications-debugging

infra: Allow notifications debugging
This commit is contained in:
Charles Kerr 2017-12-19 09:06:12 -06:00 committed by GitHub
commit 781311aa3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 5 deletions

View file

@ -32,6 +32,8 @@ class CocoaNotification : public Notification {
NSUserNotification* notification() const { return notification_; }
private:
void LogAction(const char* action);
base::scoped_nsobject<NSUserNotification> notification_;
int action_index_;

View file

@ -28,12 +28,19 @@ CocoaNotification::~CocoaNotification() {
void CocoaNotification::Show(const NotificationOptions& options) {
notification_.reset([[NSUserNotification alloc] init]);
NSString* identifier = [NSString stringWithFormat:@"%s%d", "ElectronNotification", g_identifier_];
[notification_ setTitle:base::SysUTF16ToNSString(options.title)];
[notification_ setSubtitle:base::SysUTF16ToNSString(options.subtitle)];
[notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)];
[notification_ setIdentifier:[NSString stringWithFormat:@"%s%d", "ElectronNotification", g_identifier_]];
[notification_ setIdentifier:identifier];
g_identifier_++;
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
LOG(INFO) << "Notification created (" << [identifier UTF8String] << ")";
}
if ([notification_ respondsToSelector:@selector(setContentImage:)] &&
!options.icon.drawsNothing()) {
NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
@ -74,22 +81,38 @@ void CocoaNotification::Dismiss() {
if (notification_)
[NSUserNotificationCenter.defaultUserNotificationCenter
removeDeliveredNotification:notification_];
NotificationDismissed();
this->LogAction("dismissed");
}
void CocoaNotification::NotificationDisplayed() {
if (delegate())
delegate()->NotificationDisplayed();
this->LogAction("displayed");
}
void CocoaNotification::NotificationReplied(const std::string& reply) {
if (delegate())
delegate()->NotificationReplied(reply);
this->LogAction("replied to");
}
void CocoaNotification::NotificationButtonClicked() {
if (delegate())
delegate()->NotificationAction(action_index_);
this->LogAction("button clicked");
}
void CocoaNotification::LogAction(const char* action) {
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
NSString* identifier = [notification_ valueForKey:@"identifier"];
LOG(INFO) << "Notification " << action << " (" << [identifier UTF8String] << ")";
}
}
} // namespace brightray

View file

@ -28,13 +28,18 @@
- (void)userNotificationCenter:(NSUserNotificationCenter*)center
didActivateNotification:(NSUserNotification *)notif {
auto notification = presenter_->GetNotification(notif);
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
LOG(INFO) << "Notification activated (" << [notif.identifier UTF8String] << ")";
}
if (notification) {
if (notif.activationType == NSUserNotificationActivationTypeReplied) {
notification->NotificationReplied([notif.response.string UTF8String]);
} else if (notif.activationType == NSUserNotificationActivationTypeActionButtonClicked) {
notification->NotificationButtonClicked();
} else if (notif.activationType == NSUserNotificationActivationTypeContentsClicked) {
notification->NotificationClicked();
notification->NotificationClicked();
}
}
}

View file

@ -22,6 +22,11 @@ CocoaNotification* NotificationPresenterMac::GetNotification(
isEqual:ns_notification.identifier])
return native_notification;
}
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
LOG(INFO) << "Could not find notification for " << [ns_notification.identifier UTF8String];
}
return nullptr;
}

View file

@ -9,6 +9,7 @@
#include <string>
#include <vector>
#include "base/environment.h"
#include "base/files/file_util.h"
#include "base/md5.h"
#include "base/strings/utf_string_conversions.h"
@ -26,6 +27,10 @@ namespace brightray {
namespace {
bool IsDebuggingNotifications() {
return base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS");
}
bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) {
std::vector<unsigned char> png_data;
if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data))
@ -49,6 +54,10 @@ NotificationPresenter* NotificationPresenter::Create() {
new NotificationPresenterWin);
if (!presenter->Init())
return nullptr;
if (IsDebuggingNotifications())
LOG(INFO) << "Successfully created Windows notifications presenter";
return presenter.release();
}

View file

@ -11,6 +11,7 @@
#include <shlobj.h>
#include <vector>
#include "base/environment.h"
#include "base/strings/utf_string_conversions.h"
#include "brightray/browser/notification_delegate.h"
#include "brightray/browser/win/notification_presenter_win.h"
@ -41,6 +42,10 @@ bool GetAppUserModelId(ScopedHString* app_id) {
return app_id->success();
}
bool IsDebuggingNotifications() {
return base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS");
}
} // namespace
// static
@ -129,11 +134,14 @@ void WindowsToastNotification::Show(const NotificationOptions& options) {
return;
}
if (IsDebuggingNotifications()) LOG(INFO) << "Notification created";
if (delegate())
delegate()->NotificationDisplayed();
}
void WindowsToastNotification::Dismiss() {
if (IsDebuggingNotifications()) LOG(INFO) << "Hiding notification";
toast_notifier_->Hide(toast_notification_.Get());
}
@ -164,16 +172,28 @@ bool WindowsToastNotification::GetToastXml(
? ABI::Windows::UI::Notifications::ToastTemplateType_ToastText02
: ABI::Windows::UI::Notifications::
ToastTemplateType_ToastImageAndText02;
if (FAILED(toastManager->GetTemplateContent(template_type, toast_xml)))
if (FAILED(toastManager->GetTemplateContent(template_type, toast_xml))) {
if (IsDebuggingNotifications())
LOG(INFO) << "Fetching XML template failed";
return false;
if (!SetXmlText(*toast_xml, title, msg))
}
if (!SetXmlText(*toast_xml, title, msg)) {
if (IsDebuggingNotifications())
LOG(INFO) << "Setting text fields on template failed";
return false;
}
}
// Configure the toast's notification sound
if (silent) {
if (FAILED(SetXmlAudioSilent(*toast_xml)))
if (FAILED(SetXmlAudioSilent(*toast_xml))) {
if (IsDebuggingNotifications()) {
LOG(INFO) << "Setting \"silent\" option on notification failed";
}
return false;
}
}
// Configure the toast's image
@ -398,6 +418,8 @@ IFACEMETHODIMP ToastEventHandler::Invoke(
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&Notification::NotificationClicked, notification_));
if (IsDebuggingNotifications()) LOG(INFO) << "Notification clicked";
return S_OK;
}
@ -407,6 +429,8 @@ IFACEMETHODIMP ToastEventHandler::Invoke(
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&Notification::NotificationDismissed, notification_));
if (IsDebuggingNotifications()) LOG(INFO) << "Notification dismissed";
return S_OK;
}
@ -416,6 +440,8 @@ IFACEMETHODIMP ToastEventHandler::Invoke(
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&Notification::NotificationFailed, notification_));
if (IsDebuggingNotifications()) LOG(INFO) << "Notification failed";
return S_OK;
}