electron/shell/browser/notifications/mac/cocoa_notification.mm
Electron Bot cdf04f3ae7
chore: bump chromium to 92.0.4488.0 (master) (#28676)
* chore: bump chromium in DEPS to 92.0.4478.0

* chore: update chromium patches

* chore: update v8 patches

* fix: add scale parameter to LookupIconFromFilepath

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2748317
Follow up: https://github.com/electron/electron/issues/28678

* build: depend on gtkprint config for gtk_util.h

Refs: https://chromium-review.googlesource.com/c/chromium/src/+/2824022

* build: add missing print_job_constants header

Refs: unknown

* chore: bump chromium in DEPS to 92.0.4479.0

* update patches

* chore: bump chromium in DEPS to 92.0.4480.0

* chore: bump chromium in DEPS to 92.0.4481.0

* chore: bump chromium in DEPS to 92.0.4482.2

* chore: bump chromium in DEPS to 92.0.4483.0

* chore: update patches

* chore: bump chromium in DEPS to 92.0.4484.0

* chore: bump chromium in DEPS to 92.0.4485.0

* fix patches

* update patches

* 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...")

https://chromium-review.googlesource.com/c/chromium/src/+/2810414

* 2781233: NotificationService: Plumb document_url for non-persistent notifications.

https://chromium-review.googlesource.com/c/chromium/src/+/2781233

* fixup! 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...")

* 2836669: Refactor GTK build target and dependencies

https://chromium-review.googlesource.com/c/chromium/src/+/2836669

* chore: bump chromium in DEPS to 92.0.4486.0

* update patches

* fix DecrementCapturerCount patch

* explicitly include badging.mojom.h

* include ui/gtk/gtk_ui_factory.h for BuildGtkUi()

* fixup! 2810414: [LSC] Add PRESUBMIT check for ASCIIToUTF16("...") and UTF8ToUTF16("...")

* iwyu fix for base::size

* iwyu for TRACE_EVENT0

* 2799631: Use structured interface for DevTools messages

https://chromium-review.googlesource.com/c/chromium/src/+/2799631

* 2801573: Convert enum to enum class for Widget::InitParams::Activatable

https://chromium-review.googlesource.com/c/chromium/src/+/2801573

* 2805764: Add ContentBrowserClient support for service worker-scoped binders

https://chromium-review.googlesource.com/c/chromium/src/+/2805764

* fixup! 2799631: Use structured interface for DevTools messages

* fixup! 2805764: Add ContentBrowserClient support for service worker-scoped binders

* oops, use of linux_ui after std::move

* fix devtools message handling for null params

* disable node test parallel/test-debug-args

https://chromium-review.googlesource.com/c/v8/v8/+/2843348

* fix gn check

* chore: bump chromium in DEPS to 92.0.4487.0

* chore: update patches

* chore: bump chromium in DEPS to 92.0.4488.0

* update patches

* Remove vpython use from Chromium DEPS file

https://chromium-review.googlesource.com/c/chromium/src/+/2810121

* Partial revert "workaround: disable CFG longjmp protection for Windows on Arm"

https://chromium-review.googlesource.com/c/chromium/src/+/2788210

Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
Co-authored-by: deepak1556 <hop2deep@gmail.com>
Co-authored-by: Jeremy Rose <nornagon@nornagon.net>
2021-04-27 14:27:34 -07:00

173 lines
5.4 KiB
Text

// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/notifications/mac/cocoa_notification.h"
#include <string>
#include <utility>
#include "base/logging.h"
#include "base/mac/mac_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "shell/browser/notifications/notification_delegate.h"
#include "shell/browser/notifications/notification_presenter.h"
#include "skia/ext/skia_utils_mac.h"
namespace electron {
CocoaNotification::CocoaNotification(NotificationDelegate* delegate,
NotificationPresenter* presenter)
: Notification(delegate, presenter) {}
CocoaNotification::~CocoaNotification() {
if (notification_)
[NSUserNotificationCenter.defaultUserNotificationCenter
removeDeliveredNotification:notification_];
}
void CocoaNotification::Show(const NotificationOptions& options) {
notification_.reset([[NSUserNotification alloc] init]);
NSString* identifier =
[NSString stringWithFormat:@"%@:notification:%@",
[[NSBundle mainBundle] bundleIdentifier],
[[[NSUUID alloc] init] UUIDString]];
[notification_ setTitle:base::SysUTF16ToNSString(options.title)];
[notification_ setSubtitle:base::SysUTF16ToNSString(options.subtitle)];
[notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)];
[notification_ setIdentifier:identifier];
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) {
LOG(INFO) << "Notification created (" << [identifier UTF8String] << ")";
}
if (!options.icon.drawsNothing()) {
NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
options.icon, base::mac::GetGenericRGBColorSpace());
[notification_ setContentImage:image];
}
if (options.silent) {
[notification_ setSoundName:nil];
} else if (options.sound.empty()) {
[notification_ setSoundName:NSUserNotificationDefaultSoundName];
} else {
[notification_ setSoundName:base::SysUTF16ToNSString(options.sound)];
}
[notification_ setHasActionButton:false];
int i = 0;
action_index_ = UINT_MAX;
NSMutableArray* additionalActions =
[[[NSMutableArray alloc] init] autorelease];
for (const auto& action : options.actions) {
if (action.type == u"button") {
if (action_index_ == UINT_MAX) {
// First button observed is the displayed action
[notification_ setHasActionButton:true];
[notification_
setActionButtonTitle:base::SysUTF16ToNSString(action.text)];
action_index_ = i;
} else {
// All of the rest are appended to the list of additional actions
NSString* actionIdentifier =
[NSString stringWithFormat:@"%@Action%d", identifier, i];
NSUserNotificationAction* notificationAction = [NSUserNotificationAction
actionWithIdentifier:actionIdentifier
title:base::SysUTF16ToNSString(action.text)];
[additionalActions addObject:notificationAction];
additional_action_indices_.insert(
std::make_pair(base::SysNSStringToUTF8(actionIdentifier), i));
}
}
i++;
}
if ([additionalActions count] > 0) {
[notification_ setAdditionalActions:additionalActions];
}
if (options.has_reply) {
[notification_ setResponsePlaceholder:base::SysUTF16ToNSString(
options.reply_placeholder)];
[notification_ setHasReplyButton:true];
}
if (!options.close_button_text.empty()) {
[notification_ setOtherButtonTitle:base::SysUTF16ToNSString(
options.close_button_text)];
}
[NSUserNotificationCenter.defaultUserNotificationCenter
deliverNotification:notification_];
}
void CocoaNotification::Dismiss() {
if (notification_)
[NSUserNotificationCenter.defaultUserNotificationCenter
removeDeliveredNotification:notification_];
NotificationDismissed();
notification_.reset(nil);
}
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::NotificationActivated() {
if (delegate())
delegate()->NotificationAction(action_index_);
this->LogAction("button clicked");
}
void CocoaNotification::NotificationActivated(
NSUserNotificationAction* action) {
if (delegate()) {
unsigned index = action_index_;
std::string identifier = base::SysNSStringToUTF8(action.identifier);
for (const auto& it : additional_action_indices_) {
if (it.first == identifier) {
index = it.second;
break;
}
}
delegate()->NotificationAction(index);
}
this->LogAction("button clicked");
}
void CocoaNotification::NotificationDismissed() {
if (delegate())
delegate()->NotificationClosed();
this->LogAction("dismissed");
}
void CocoaNotification::LogAction(const char* action) {
if (getenv("ELECTRON_DEBUG_NOTIFICATIONS") && notification_) {
NSString* identifier = [notification_ valueForKey:@"identifier"];
DCHECK(identifier);
LOG(INFO) << "Notification " << action << " (" << [identifier UTF8String]
<< ")";
}
}
} // namespace electron