From 7eb14243eb3d90baf896c34be9336a77f43c9ca3 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Sat, 24 Jun 2017 21:03:27 +1000 Subject: [PATCH] Refactor notification options --- atom/browser/api/atom_api_notification.cc | 12 +++++-- .../browser/linux/libnotify_notification.cc | 23 ++++--------- .../browser/linux/libnotify_notification.h | 10 +----- brightray/browser/mac/cocoa_notification.h | 10 +----- brightray/browser/mac/cocoa_notification.mm | 28 ++++++---------- brightray/browser/notification.h | 27 ++++++++------- .../browser/platform_notification_service.cc | 17 +++++++--- brightray/browser/win/win32_notification.cc | 33 +++++++++---------- brightray/browser/win/win32_notification.h | 6 +--- .../browser/win/windows_toast_notification.cc | 20 ++++------- .../browser/win/windows_toast_notification.h | 10 +----- 11 files changed, 80 insertions(+), 116 deletions(-) diff --git a/atom/browser/api/atom_api_notification.cc b/atom/browser/api/atom_api_notification.cc index 7119df3767d..351ff15d999 100644 --- a/atom/browser/api/atom_api_notification.cc +++ b/atom/browser/api/atom_api_notification.cc @@ -162,8 +162,16 @@ void Notification::Show() { if (presenter_) { notification_ = presenter_->CreateNotification(this); if (notification_) { - notification_->Show(title_, body_, "", GURL(), icon_.AsBitmap(), silent_, - has_reply_, reply_placeholder_, actions_); + brightray::NotificationOptions options; + options.title = title_; + options.msg = body_; + options.icon_url = GURL(); + options.icon = icon_.AsBitmap(); + options.silent = silent_; + options.has_reply = has_reply_; + options.reply_placeholder = reply_placeholder_; + options.actions = actions_; + notification_->Show(options); } } } diff --git a/brightray/browser/linux/libnotify_notification.cc b/brightray/browser/linux/libnotify_notification.cc index f93030656ca..db59e451388 100644 --- a/brightray/browser/linux/libnotify_notification.cc +++ b/brightray/browser/linux/libnotify_notification.cc @@ -85,19 +85,10 @@ LibnotifyNotification::~LibnotifyNotification() { } } -void LibnotifyNotification::Show(const base::string16& title, - const base::string16& body, - const std::string& tag, - const GURL& icon_url, - const SkBitmap& icon, - bool silent, - bool has_reply, - const base::string16& reply_placeholder, - const std::vector actions - ) { +void LibnotifyNotification::Show(const NotificationOptions& options) { notification_ = libnotify_loader_.notify_notification_new( - base::UTF16ToUTF8(title).c_str(), - base::UTF16ToUTF8(body).c_str(), + base::UTF16ToUTF8(options.title).c_str(), + base::UTF16ToUTF8(options.msg).c_str(), nullptr); g_signal_connect( @@ -111,8 +102,8 @@ void LibnotifyNotification::Show(const base::string16& title, nullptr); } - if (!icon.drawsNothing()) { - GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(icon); + if (!options.icon.drawsNothing()) { + GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(options.icon); libnotify_loader_.notify_notification_set_image_from_pixbuf( notification_, pixbuf); libnotify_loader_.notify_notification_set_timeout( @@ -120,8 +111,8 @@ void LibnotifyNotification::Show(const base::string16& title, g_object_unref(pixbuf); } - if (!tag.empty()) { - GQuark id = g_quark_from_string(tag.c_str()); + if (!options.tag.empty()) { + GQuark id = g_quark_from_string(options.tag.c_str()); g_object_set(G_OBJECT(notification_), "id", id, NULL); } diff --git a/brightray/browser/linux/libnotify_notification.h b/brightray/browser/linux/libnotify_notification.h index 3616051f055..c3ca3088128 100644 --- a/brightray/browser/linux/libnotify_notification.h +++ b/brightray/browser/linux/libnotify_notification.h @@ -23,15 +23,7 @@ class LibnotifyNotification : public Notification { static bool Initialize(); // Notification: - void Show(const base::string16& title, - const base::string16& msg, - const std::string& tag, - const GURL& icon_url, - const SkBitmap& icon, - bool silent, - bool has_reply, - const base::string16& reply_placeholder, - const std::vector actions) override; + void Show(const NotificationOptions& options) override; void Dismiss() override; private: diff --git a/brightray/browser/mac/cocoa_notification.h b/brightray/browser/mac/cocoa_notification.h index 9eabbd7b5fd..5fa1a98e19d 100644 --- a/brightray/browser/mac/cocoa_notification.h +++ b/brightray/browser/mac/cocoa_notification.h @@ -22,15 +22,7 @@ class CocoaNotification : public Notification { ~CocoaNotification(); // Notification: - void Show(const base::string16& title, - const base::string16& msg, - const std::string& tag, - const GURL& icon_url, - const SkBitmap& icon, - bool silent, - const bool has_reply, - const base::string16& reply_placeholder, - const std::vector actions) override; + void Show(const NotificationOptions& options) override; void Dismiss() override; void NotificationDisplayed(); diff --git a/brightray/browser/mac/cocoa_notification.mm b/brightray/browser/mac/cocoa_notification.mm index dda650079d2..56aaf81e350 100644 --- a/brightray/browser/mac/cocoa_notification.mm +++ b/brightray/browser/mac/cocoa_notification.mm @@ -24,27 +24,19 @@ CocoaNotification::~CocoaNotification() { removeDeliveredNotification:notification_]; } -void CocoaNotification::Show(const base::string16& title, - const base::string16& body, - const std::string& tag, - const GURL& icon_url, - const SkBitmap& icon, - bool silent, - bool has_reply, - const base::string16& reply_placeholder, - const std::vector actions) { +void CocoaNotification::Show(const NotificationOptions& options) { notification_.reset([[NSUserNotification alloc] init]); - [notification_ setTitle:base::SysUTF16ToNSString(title)]; - [notification_ setInformativeText:base::SysUTF16ToNSString(body)]; + [notification_ setTitle:base::SysUTF16ToNSString(options.title)]; + [notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)]; if ([notification_ respondsToSelector:@selector(setContentImage:)] && - !icon.drawsNothing()) { + !options.icon.drawsNothing()) { NSImage* image = skia::SkBitmapToNSImageWithColorSpace( - icon, base::mac::GetGenericRGBColorSpace()); + options.icon, base::mac::GetGenericRGBColorSpace()); [notification_ setContentImage:image]; } - if (silent) { + if (options.silent) { [notification_ setSoundName:nil]; } else { [notification_ setSoundName:NSUserNotificationDefaultSoundName]; @@ -52,8 +44,8 @@ void CocoaNotification::Show(const base::string16& title, [notification_ setHasActionButton:false]; - for (size_t i = 0; i < actions.size(); i++) { - NotificationAction action = actions[i]; + for (size_t i = 0; i < options.actions.size(); i++) { + NotificationAction action = options.actions[i]; if (action.type == base::UTF8ToUTF16("button")) { [notification_ setHasActionButton:true]; @@ -62,8 +54,8 @@ void CocoaNotification::Show(const base::string16& title, } } - if (has_reply) { - [notification_ setResponsePlaceholder:base::SysUTF16ToNSString(reply_placeholder)]; + if (options.has_reply) { + [notification_ setResponsePlaceholder:base::SysUTF16ToNSString(options.reply_placeholder)]; [notification_ setHasReplyButton:true]; } diff --git a/brightray/browser/notification.h b/brightray/browser/notification.h index 1cf57db9d67..c9f31e68f58 100644 --- a/brightray/browser/notification.h +++ b/brightray/browser/notification.h @@ -10,9 +10,8 @@ #include "base/memory/weak_ptr.h" #include "base/strings/string16.h" - -class GURL; -class SkBitmap; +#include "third_party/skia/include/core/SkBitmap.h" +#include "url/gurl.h" namespace brightray { @@ -24,20 +23,24 @@ struct NotificationAction { base::string16 text; }; +struct NotificationOptions { + base::string16 title; + base::string16 msg; + std::string tag; + GURL icon_url; + SkBitmap icon; + bool silent; + bool has_reply; + base::string16 reply_placeholder; + std::vector actions; +}; + class Notification { public: virtual ~Notification(); // Shows the notification. - virtual void Show(const base::string16& title, - const base::string16& msg, - const std::string& tag, - const GURL& icon_url, - const SkBitmap& icon, - bool silent, - bool has_reply, - const base::string16& reply_placeholder, - const std::vector actions) = 0; + virtual void Show(const NotificationOptions& options) = 0; // Closes the notification, this instance will be destroyed after the // notification gets closed. virtual void Dismiss() = 0; diff --git a/brightray/browser/platform_notification_service.cc b/brightray/browser/platform_notification_service.cc index 4e38b89fa75..63e32b0cd0e 100644 --- a/brightray/browser/platform_notification_service.cc +++ b/brightray/browser/platform_notification_service.cc @@ -29,12 +29,19 @@ void OnWebNotificationAllowed(base::WeakPtr notification, bool allowed) { if (!notification) return; - if (allowed) - notification->Show(data.title, data.body, data.tag, data.icon, icon, - audio_muted ? true : data.silent, false, - base::UTF8ToUTF16(""), {}); - else + if (allowed) { + brightray::NotificationOptions options; + options.title = data.title; + options.msg = data.body; + options.tag = data.tag; + options.icon_url = data.icon; + options.icon = icon; + options.silent = audio_muted ? true : data.silent; + options.has_reply = false; + notification->Show(options); + } else { notification->Destroy(); + } } } // namespace diff --git a/brightray/browser/win/win32_notification.cc b/brightray/browser/win/win32_notification.cc index aaaa26c78d6..9c9cd623f48 100644 --- a/brightray/browser/win/win32_notification.cc +++ b/brightray/browser/win/win32_notification.cc @@ -10,50 +10,49 @@ namespace brightray { -void Win32Notification::Show( - const base::string16& title, const base::string16& msg, - const std::string& tag, const GURL& icon_url, - const SkBitmap& icon, bool silent, - bool has_reply, const base::string16& reply_placeholder, - const std::vector actions) { +void Win32Notification::Show(const NotificationOptions& options) { auto presenter = static_cast(this->presenter()); if (!presenter) return; HBITMAP image = NULL; - if (!icon.drawsNothing()) { - if (icon.colorType() == kBGRA_8888_SkColorType) { - icon.lockPixels(); + if (!options.icon.drawsNothing()) { + if (options.icon.colorType() == kBGRA_8888_SkColorType) { + options.icon.lockPixels(); BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) }; - bmi.biWidth = icon.width(); - bmi.biHeight = -icon.height(); + bmi.biWidth = options.icon.width(); + bmi.biHeight = -options.icon.height(); bmi.biPlanes = 1; bmi.biBitCount = 32; bmi.biCompression = BI_RGB; HDC hdcScreen = GetDC(NULL); - image = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, icon.getPixels(), + image = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, + options.icon.getPixels(), reinterpret_cast(&bmi), DIB_RGB_COLORS); ReleaseDC(NULL, hdcScreen); - icon.unlockPixels(); + options.icon.unlockPixels(); } } Win32Notification* existing = nullptr; - if (!tag.empty()) existing = presenter->GetNotificationObjectByTag(tag); + if (!options.tag.empty()) + existing = presenter->GetNotificationObjectByTag(options.tag); if (existing) { existing->tag_.clear(); this->notification_ref_ = std::move(existing->notification_ref_); - this->notification_ref_.Set(title, msg, image); + this->notification_ref_.Set(options.title, options.msg, image); } else { - this->notification_ref_ = presenter->AddNotification(title, msg, image); + this->notification_ref_ = presenter->AddNotification(options.title, + options.msg, + image); } - this->tag_ = tag; + this->tag_ = options.tag; if (image) DeleteObject(image); } diff --git a/brightray/browser/win/win32_notification.h b/brightray/browser/win/win32_notification.h index 4cebdee36eb..e4d1b3ddedd 100644 --- a/brightray/browser/win/win32_notification.h +++ b/brightray/browser/win/win32_notification.h @@ -10,11 +10,7 @@ class Win32Notification : public brightray::Notification { NotificationPresenterWin7* presenter) : Notification(delegate, presenter) { } - void Show(const base::string16& title, const base::string16& msg, - const std::string& tag, const GURL& icon_url, - const SkBitmap& icon, bool silent, - bool has_reply, const base::string16& reply_placeholder, - const std::vector actions) override; + void Show(const NotificationOptions& options) override; void Dismiss() override; const DesktopNotificationController::Notification& GetRef() const { diff --git a/brightray/browser/win/windows_toast_notification.cc b/brightray/browser/win/windows_toast_notification.cc index 4a44706dd59..13a8f002616 100644 --- a/brightray/browser/win/windows_toast_notification.cc +++ b/brightray/browser/win/windows_toast_notification.cc @@ -85,23 +85,15 @@ WindowsToastNotification::~WindowsToastNotification() { } } -void WindowsToastNotification::Show(const base::string16& title, - const base::string16& msg, - const std::string& tag, - const GURL& icon_url, - const SkBitmap& icon, - bool silent, - bool has_reply, - const base::string16& reply_placeholder, - const std::vector< - NotificationAction - > actions) { +void WindowsToastNotification::Show(const NotificationOptions& options) { auto presenter_win = static_cast(presenter()); - std::wstring icon_path = presenter_win->SaveIconToFilesystem(icon, icon_url); + std::wstring icon_path = presenter_win->SaveIconToFilesystem( + options.icon, + options.icon_url); ComPtr toast_xml; - if (FAILED(GetToastXml(toast_manager_.Get(), title, msg, icon_path, silent, - &toast_xml))) { + if (FAILED(GetToastXml(toast_manager_.Get(), options.title, options.msg, + icon_path, options.silent, &toast_xml))) { NotificationFailed(); return; } diff --git a/brightray/browser/win/windows_toast_notification.h b/brightray/browser/win/windows_toast_notification.h index ae5e604ad7b..62c8b844c75 100644 --- a/brightray/browser/win/windows_toast_notification.h +++ b/brightray/browser/win/windows_toast_notification.h @@ -51,15 +51,7 @@ class WindowsToastNotification : public Notification { protected: // Notification: - void Show(const base::string16& title, - const base::string16& msg, - const std::string& tag, - const GURL& icon_url, - const SkBitmap& icon, - bool silent, - bool has_reply, - const base::string16& reply_placeholder, - const std::vector actions) override; + void Show(const NotificationOptions& options) override; void Dismiss() override; private: