feat: add support for UNNotificationResponse in app 'ready' event (#25950)

This commit is contained in:
Milan Burda 2020-10-28 02:25:10 +01:00 committed by GitHub
parent bf89237f60
commit d2727f5aba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 9 deletions

View file

@ -17,6 +17,8 @@
#include "shell/browser/mac/dict_util.h"
#import "shell/browser/mac/electron_application.h"
#import <UserNotifications/UserNotifications.h>
#if BUILDFLAG(USE_ALLOCATOR_SHIM)
// On macOS 10.12, the IME system attempts to allocate a 2^64 size buffer,
// which would typically cause an OOM crash. To avoid this, the problematic
@ -41,6 +43,29 @@ static base::mac::ScopedObjCClassSwizzler* g_swizzle_imk_input_session;
@end
#endif // BUILDFLAG(USE_ALLOCATOR_SHIM)
static NSDictionary* UNNotificationResponseToNSDictionary(
UNNotificationResponse* response) API_AVAILABLE(macosx(10.14)) {
// [response isKindOfClass:[UNNotificationResponse class]]
if (![response respondsToSelector:@selector(actionIdentifier)] ||
![response respondsToSelector:@selector(notification)]) {
return nil;
}
NSMutableDictionary* result = [[NSMutableDictionary alloc] init];
result[@"actionIdentifier"] = response.actionIdentifier;
result[@"date"] = @(response.notification.date.timeIntervalSince1970);
result[@"identifier"] = response.notification.request.identifier;
result[@"userInfo"] = response.notification.request.content.userInfo;
// [response isKindOfClass:[UNTextInputNotificationResponse class]]
if ([response respondsToSelector:@selector(userText)]) {
result[@"userText"] =
static_cast<UNTextInputNotificationResponse*>(response).userText;
}
return result;
}
@implementation ElectronApplicationDelegate
- (void)setApplicationDockMenu:(electron::ElectronMenuModel*)model {
@ -68,16 +93,23 @@ static base::mac::ScopedObjCClassSwizzler* g_swizzle_imk_input_session;
}
- (void)applicationDidFinishLaunching:(NSNotification*)notify {
NSUserNotification* user_notification =
NSObject* user_notification =
[notify userInfo][NSApplicationLaunchUserNotificationKey];
NSDictionary* notification_info = nil;
if ([user_notification isKindOfClass:[NSUserNotification class]]) {
electron::Browser::Get()->DidFinishLaunching(
electron::NSDictionaryToDictionaryValue(user_notification.userInfo));
} else {
electron::Browser::Get()->DidFinishLaunching(base::DictionaryValue());
if (user_notification) {
if ([user_notification isKindOfClass:[NSUserNotification class]]) {
notification_info =
[static_cast<NSUserNotification*>(user_notification) userInfo];
} else if (@available(macOS 10.14, *)) {
notification_info = UNNotificationResponseToNSDictionary(
static_cast<UNNotificationResponse*>(user_notification));
}
}
electron::Browser::Get()->DidFinishLaunching(
electron::NSDictionaryToDictionaryValue(notification_info));
#if BUILDFLAG(USE_ALLOCATOR_SHIM)
// Disable fatal OOM to hack around an OS bug https://crbug.com/654695.
if (base::mac::IsOS10_12()) {