Commit e80c2da4 authored by Mohamed Adel's avatar Mohamed Adel Committed by Commit Bot

Group shared methods between macOS bridges

This CL creates a base class for notification builder mac and extends it
to build UNNotifications. It also groups shared methods between the NPBs
for macOS platforms together to avoid redundancy.

Bug: 1129987, 1129988
Change-Id: I41ef920aafed94a0ac8e9851c5884112f0a30aff
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2418446Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarRichard Knoll <knollr@chromium.org>
Commit-Queue: Mohamed Adel <adelm@google.com>
Cr-Commit-Position: refs/heads/master@{#809361}
parent 8d249484
...@@ -4684,6 +4684,8 @@ static_library("browser") { ...@@ -4684,6 +4684,8 @@ static_library("browser") {
"notifications/notification_platform_bridge_mac.mm", "notifications/notification_platform_bridge_mac.mm",
"notifications/notification_platform_bridge_mac_unnotification.h", "notifications/notification_platform_bridge_mac_unnotification.h",
"notifications/notification_platform_bridge_mac_unnotification.mm", "notifications/notification_platform_bridge_mac_unnotification.mm",
"notifications/notification_platform_bridge_mac_utils.h",
"notifications/notification_platform_bridge_mac_utils.mm",
"obsolete_system/obsolete_system_mac.cc", "obsolete_system/obsolete_system_mac.cc",
"password_manager/password_manager_util_mac.h", "password_manager/password_manager_util_mac.h",
"password_manager/password_manager_util_mac.mm", "password_manager/password_manager_util_mac.mm",
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/callback_helpers.h" #include "base/callback_helpers.h"
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/i18n/number_formatting.h"
#include "base/mac/bundle_locations.h" #include "base/mac/bundle_locations.h"
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
#include "base/mac/mac_util.h" #include "base/mac/mac_util.h"
...@@ -30,6 +29,7 @@ ...@@ -30,6 +29,7 @@
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/notification_common.h" #include "chrome/browser/notifications/notification_common.h"
#include "chrome/browser/notifications/notification_display_service_impl.h" #include "chrome/browser/notifications/notification_display_service_impl.h"
#include "chrome/browser/notifications/notification_platform_bridge_mac_utils.h"
#include "chrome/browser/notifications/platform_notification_service_impl.h" #include "chrome/browser/notifications/platform_notification_service_impl.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
...@@ -40,17 +40,14 @@ ...@@ -40,17 +40,14 @@
#include "chrome/common/buildflags.h" #include "chrome/common/buildflags.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "components/crash/core/app/crashpad.h" #include "components/crash/core/app/crashpad.h"
#include "components/url_formatter/elide_url.h"
#include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "third_party/blink/public/common/notifications/notification_constants.h" #include "third_party/blink/public/common/notifications/notification_constants.h"
#include "third_party/crashpad/crashpad/client/crashpad_client.h" #include "third_party/crashpad/crashpad/client/crashpad_client.h"
#include "ui/base/l10n/l10n_util_mac.h" #include "ui/base/l10n/l10n_util_mac.h"
#include "ui/message_center/public/cpp/notification.h" #include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_types.h" #include "ui/message_center/public/cpp/notification_types.h"
#include "url/gurl.h" #include "url/gurl.h"
#include "url/origin.h"
@class NSUserNotification; @class NSUserNotification;
@class NSUserNotificationCenter; @class NSUserNotificationCenter;
...@@ -113,20 +110,6 @@ void RecordXPCEvent(XPCConnectionEvent event) { ...@@ -113,20 +110,6 @@ void RecordXPCEvent(XPCConnectionEvent event) {
XPC_CONNECTION_EVENT_COUNT); XPC_CONNECTION_EVENT_COUNT);
} }
base::string16 CreateNotificationTitle(
const message_center::Notification& notification) {
base::string16 title;
// Show progress percentage if available. We don't support indeterminate
// states on macOS native notifications.
if (notification.type() == message_center::NOTIFICATION_TYPE_PROGRESS &&
notification.progress() >= 0 && notification.progress() <= 100) {
title += base::FormatPercent(notification.progress());
title += base::UTF8ToUTF16(" - ");
}
title += notification.title();
return title;
}
bool IsPersistentNotification( bool IsPersistentNotification(
const message_center::Notification& notification) { const message_center::Notification& notification) {
if (!NotificationPlatformBridgeMac::SupportsAlerts()) if (!NotificationPlatformBridgeMac::SupportsAlerts())
...@@ -136,48 +119,6 @@ bool IsPersistentNotification( ...@@ -136,48 +119,6 @@ bool IsPersistentNotification(
notification.type() == message_center::NOTIFICATION_TYPE_PROGRESS; notification.type() == message_center::NOTIFICATION_TYPE_PROGRESS;
} }
base::string16 CreateNotificationContext(
const message_center::Notification& notification,
bool requires_attribution) {
if (!requires_attribution)
return notification.context_message();
// Mac OS notifications don't provide a good way to elide the domain (or tell
// you the maximum width of the subtitle field). We have experimentally
// determined the maximum number of characters that fit using the widest
// possible character (m). If the domain fits in those character we show it
// completely. Otherwise we use eTLD + 1.
// These numbers have been obtained through experimentation on various
// Mac OS platforms.
constexpr size_t kMaxDomainLengthAlert = 19;
constexpr size_t kMaxDomainLengthBanner = 28;
size_t max_characters = IsPersistentNotification(notification)
? kMaxDomainLengthAlert
: kMaxDomainLengthBanner;
base::string16 origin = url_formatter::FormatOriginForSecurityDisplay(
url::Origin::Create(notification.origin_url()),
url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
if (origin.size() <= max_characters)
return origin;
// Too long, use etld+1
base::string16 etldplusone =
base::UTF8ToUTF16(net::registry_controlled_domains::GetDomainAndRegistry(
notification.origin_url(),
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
// localhost, raw IPs etc. are not handled by GetDomainAndRegistry.
if (etldplusone.empty())
return origin;
return etldplusone;
}
// Implements the version check to determine if alerts are supported. Do not // Implements the version check to determine if alerts are supported. Do not
// call this method directly as SysInfo::OperatingSystemVersionNumbers might be // call this method directly as SysInfo::OperatingSystemVersionNumbers might be
// an expensive call. Instead use NotificationPlatformBridgeMac::SupportsAlerts // an expensive call. Instead use NotificationPlatformBridgeMac::SupportsAlerts
...@@ -255,8 +196,8 @@ void NotificationPlatformBridgeMac::Display( ...@@ -255,8 +196,8 @@ void NotificationPlatformBridgeMac::Display(
settingsLabel:l10n_util::GetNSString( settingsLabel:l10n_util::GetNSString(
IDS_NOTIFICATION_BUTTON_SETTINGS)]); IDS_NOTIFICATION_BUTTON_SETTINGS)]);
[builder [builder setTitle:base::SysUTF16ToNSString(
setTitle:base::SysUTF16ToNSString(CreateNotificationTitle(notification))]; CreateMacNotificationTitle(notification))];
base::string16 context_message = base::string16 context_message =
notification.items().empty() notification.items().empty()
...@@ -269,8 +210,11 @@ void NotificationPlatformBridgeMac::Display( ...@@ -269,8 +210,11 @@ void NotificationPlatformBridgeMac::Display(
bool requires_attribution = bool requires_attribution =
notification.context_message().empty() && notification.context_message().empty() &&
notification_type != NotificationHandler::Type::EXTENSION; notification_type != NotificationHandler::Type::EXTENSION;
[builder setSubTitle:base::SysUTF16ToNSString(CreateNotificationContext(
notification, requires_attribution))]; bool is_persistent = IsPersistentNotification(notification);
[builder setSubTitle:base::SysUTF16ToNSString(CreateMacNotificationContext(
is_persistent, notification, requires_attribution))];
if (!notification.icon().IsEmpty()) { if (!notification.icon().IsEmpty()) {
[builder setIcon:notification.icon().ToNSImage()]; [builder setIcon:notification.icon().ToNSImage()];
......
...@@ -9,6 +9,9 @@ ...@@ -9,6 +9,9 @@
#include "base/notreached.h" #include "base/notreached.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/notifications/notification_platform_bridge_mac_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/cocoa/notifications/unnotification_builder_mac.h"
#include "ui/message_center/public/cpp/notification.h" #include "ui/message_center/public/cpp/notification.h"
@class NSNotification; @class NSNotification;
...@@ -50,16 +53,38 @@ void NotificationPlatformBridgeMacUNNotification::Display( ...@@ -50,16 +53,38 @@ void NotificationPlatformBridgeMacUNNotification::Display(
Profile* profile, Profile* profile,
const message_center::Notification& notification, const message_center::Notification& notification,
std::unique_ptr<NotificationCommon::Metadata> metadata) { std::unique_ptr<NotificationCommon::Metadata> metadata) {
base::scoped_nsobject<UNMutableNotificationContent> content( base::scoped_nsobject<UNNotificationBuilder> builder(
[[UNMutableNotificationContent alloc] init]); [[UNNotificationBuilder alloc] init]);
[content setTitle:base::SysUTF16ToNSString(notification.title())]; base::string16 context_message =
notification.items().empty()
base::string16 context_message = notification.message(); ? notification.message()
[content setBody:base::SysUTF16ToNSString(context_message)]; : (notification.items().at(0).title + base::UTF8ToUTF16(" - ") +
notification.items().at(0).message);
[content
setSubtitle:base::SysUTF8ToNSString(notification.origin_url().spec())]; bool requires_attribution =
notification.context_message().empty() &&
notification_type != NotificationHandler::Type::EXTENSION;
[builder setTitle:base::SysUTF16ToNSString(
CreateMacNotificationTitle(notification))];
[builder setContextMessage:base::SysUTF16ToNSString(context_message)];
[builder setSubTitle:base::SysUTF16ToNSString(CreateMacNotificationContext(
/*is_persistent=*/false, notification,
requires_attribution))];
[builder setOrigin:base::SysUTF8ToNSString(notification.origin_url().spec())];
[builder setNotificationId:base::SysUTF8ToNSString(notification.id())];
[builder setProfileId:base::SysUTF8ToNSString(GetProfileId(profile))];
[builder setIncognito:profile->IsOffTheRecord()];
[builder setCreatorPid:[NSNumber numberWithInteger:static_cast<NSInteger>(
getpid())]];
[builder
setNotificationType:[NSNumber numberWithInteger:static_cast<NSInteger>(
notification_type)]];
UNMutableNotificationContent* content = [builder buildUserNotification];
// TODO(crbug/1129398): Add close button to banners // TODO(crbug/1129398): Add close button to banners
UNNotificationRequest* request = [UNNotificationRequest UNNotificationRequest* request = [UNNotificationRequest
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_PLATFORM_BRIDGE_MAC_UTILS_H_
#define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_PLATFORM_BRIDGE_MAC_UTILS_H_
#include "base/strings/string16.h"
#include "ui/message_center/public/cpp/notification.h"
// This file is a combination of methods that are shared between the macOS
// notification bridges.
base::string16 CreateMacNotificationTitle(
const message_center::Notification& notification);
base::string16 CreateMacNotificationContext(
bool is_persistent,
const message_center::Notification& notification,
bool requires_attribution);
#endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_PLATFORM_BRIDGE_MAC_UTILS_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/notifications/notification_platform_bridge_mac_utils.h"
#include "base/i18n/number_formatting.h"
#include "base/strings/utf_string_conversions.h"
#include "components/url_formatter/elide_url.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "url/gurl.h"
#include "url/origin.h"
base::string16 CreateMacNotificationTitle(
const message_center::Notification& notification) {
base::string16 title;
// Show progress percentage if available. We don't support indeterminate
// states on macOS native notifications.
if (notification.type() == message_center::NOTIFICATION_TYPE_PROGRESS &&
notification.progress() >= 0 && notification.progress() <= 100) {
title += base::FormatPercent(notification.progress());
title += base::UTF8ToUTF16(" - ");
}
title += notification.title();
return title;
}
base::string16 CreateMacNotificationContext(
bool is_persistent,
const message_center::Notification& notification,
bool requires_attribution) {
if (!requires_attribution)
return notification.context_message();
// Mac OS notifications don't provide a good way to elide the domain (or tell
// you the maximum width of the subtitle field). We have experimentally
// determined the maximum number of characters that fit using the widest
// possible character (m). If the domain fits in those character we show it
// completely. Otherwise we use eTLD + 1.
// These numbers have been obtained through experimentation on various
// Mac OS platforms.
constexpr size_t kMaxDomainLengthAlert = 19;
constexpr size_t kMaxDomainLengthBanner = 28;
size_t max_characters =
is_persistent ? kMaxDomainLengthAlert : kMaxDomainLengthBanner;
base::string16 origin = url_formatter::FormatOriginForSecurityDisplay(
url::Origin::Create(notification.origin_url()),
url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
if (origin.size() <= max_characters)
return origin;
// Too long, use etld+1
base::string16 etldplusone =
base::UTF8ToUTF16(net::registry_controlled_domains::GetDomainAndRegistry(
notification.origin_url(),
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
// localhost, raw IPs etc. are not handled by GetDomainAndRegistry.
if (etldplusone.empty())
return origin;
return etldplusone;
}
...@@ -31,6 +31,7 @@ mac_app_bundle("alert_notification_xpc_service") { ...@@ -31,6 +31,7 @@ mac_app_bundle("alert_notification_xpc_service") {
frameworks = [ frameworks = [
"AppKit.framework", "AppKit.framework",
"Foundation.framework", "Foundation.framework",
"UserNotifications.framework",
] ]
ldflags = [] ldflags = []
...@@ -48,6 +49,8 @@ mac_app_bundle("alert_notification_xpc_service") { ...@@ -48,6 +49,8 @@ mac_app_bundle("alert_notification_xpc_service") {
static_library("common") { static_library("common") {
sources = [ sources = [
"notification_builder_base.h",
"notification_builder_base.mm",
"notification_builder_mac.h", "notification_builder_mac.h",
"notification_builder_mac.mm", "notification_builder_mac.mm",
"notification_constants_mac.h", "notification_constants_mac.h",
...@@ -55,6 +58,8 @@ static_library("common") { ...@@ -55,6 +58,8 @@ static_library("common") {
"notification_delivery.h", "notification_delivery.h",
"notification_response_builder_mac.h", "notification_response_builder_mac.h",
"notification_response_builder_mac.mm", "notification_response_builder_mac.mm",
"unnotification_builder_mac.h",
"unnotification_builder_mac.mm",
"xpc_mach_port.h", "xpc_mach_port.h",
"xpc_mach_port.mm", "xpc_mach_port.mm",
] ]
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_COCOA_NOTIFICATIONS_NOTIFICATION_BUILDER_BASE_H_
#define CHROME_BROWSER_UI_COCOA_NOTIFICATIONS_NOTIFICATION_BUILDER_BASE_H_
#import <Foundation/Foundation.h>
#include "base/mac/scoped_nsobject.h"
#include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h"
@interface NotificationBuilderBase : NSObject {
base::scoped_nsobject<NSMutableDictionary> _notificationData;
}
// Initializes an empty builder along with |_notificationData|.
- (instancetype)init;
// Initializes a builder by deserializing |data|. The |data| must have been
// generated by calling the buildDictionary function on another builder
// instance.
- (instancetype)initWithDictionary:(NSDictionary*)data;
// Setters
// Note for XPC users. Always use the setters from Chrome's main app. Do not
// attempt to use them from XPC since some of the default strings and other
// defaults are not available from the xpc service.
- (void)setTitle:(NSString*)title;
- (void)setSubTitle:(NSString*)subTitle;
- (void)setContextMessage:(NSString*)contextMessage;
- (void)setIcon:(NSImage*)icon;
- (void)setButtons:(NSString*)primaryButton
secondaryButton:(NSString*)secondaryButton;
- (void)setTag:(NSString*)tag;
- (void)setOrigin:(NSString*)origin;
- (void)setNotificationId:(NSString*)notificationId;
- (void)setProfileId:(NSString*)profileId;
- (void)setIncognito:(BOOL)incognito;
- (void)setCreatorPid:(NSNumber*)pid;
- (void)setNotificationType:(NSNumber*)notificationType;
- (void)setShowSettingsButton:(BOOL)showSettingsButton;
// Returns a representation of a notification that can be serialized.
// Another instance of NotificationBuilder can read this directly and generate
// a notification out of it via the |buildbuildUserNotification| method.
- (NSDictionary*)buildDictionary;
@end
#endif // CHROME_BROWSER_UI_COCOA_NOTIFICATIONS_NOTIFICATION_BUILDER_BASE_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/cocoa/notifications/notification_builder_base.h"
#import <AppKit/AppKit.h>
@implementation NotificationBuilderBase
- (instancetype)init {
if ((self = [super init])) {
_notificationData.reset([[NSMutableDictionary alloc] init]);
}
return self;
}
- (instancetype)initWithDictionary:(NSDictionary*)data {
if ((self = [super init])) {
_notificationData.reset([data copy]);
}
return self;
}
- (void)setTitle:(NSString*)title {
if (title.length) {
[_notificationData setObject:title
forKey:notification_constants::kNotificationTitle];
}
}
- (void)setSubTitle:(NSString*)subTitle {
if (subTitle.length) {
[_notificationData setObject:subTitle
forKey:notification_constants::kNotificationSubTitle];
}
}
- (void)setContextMessage:(NSString*)contextMessage {
if (contextMessage.length) {
[_notificationData
setObject:contextMessage
forKey:notification_constants::kNotificationInformativeText];
}
}
- (void)setIcon:(NSImage*)icon {
if (icon) {
if ([icon conformsToProtocol:@protocol(NSSecureCoding)]) {
[_notificationData setObject:icon
forKey:notification_constants::kNotificationImage];
} else { // NSImage only conforms to NSSecureCoding from 10.10 onwards.
[_notificationData setObject:[icon TIFFRepresentation]
forKey:notification_constants::kNotificationImage];
}
}
}
- (void)setButtons:(NSString*)primaryButton
secondaryButton:(NSString*)secondaryButton {
DCHECK(primaryButton.length);
[_notificationData setObject:primaryButton
forKey:notification_constants::kNotificationButtonOne];
if (secondaryButton.length) {
[_notificationData
setObject:secondaryButton
forKey:notification_constants::kNotificationButtonTwo];
}
}
- (void)setTag:(NSString*)tag {
if (tag.length) {
[_notificationData setObject:tag
forKey:notification_constants::kNotificationTag];
}
}
- (void)setOrigin:(NSString*)origin {
if (origin.length) {
[_notificationData setObject:origin
forKey:notification_constants::kNotificationOrigin];
}
}
- (void)setNotificationId:(NSString*)notificationId {
DCHECK(notificationId.length);
[_notificationData setObject:notificationId
forKey:notification_constants::kNotificationId];
}
- (void)setProfileId:(NSString*)profileId {
DCHECK(profileId.length);
[_notificationData setObject:profileId
forKey:notification_constants::kNotificationProfileId];
}
- (void)setIncognito:(BOOL)incognito {
[_notificationData setObject:[NSNumber numberWithBool:incognito]
forKey:notification_constants::kNotificationIncognito];
}
- (void)setCreatorPid:(NSNumber*)pid {
[_notificationData setObject:pid
forKey:notification_constants::kNotificationCreatorPid];
}
- (void)setNotificationType:(NSNumber*)notificationType {
[_notificationData setObject:notificationType
forKey:notification_constants::kNotificationType];
}
- (void)setShowSettingsButton:(BOOL)showSettingsButton {
[_notificationData
setObject:[NSNumber numberWithBool:showSettingsButton]
forKey:notification_constants::kNotificationHasSettingsButton];
}
- (NSDictionary*)buildDictionary {
return [[_notificationData copy] autorelease];
}
@end
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#include "base/mac/scoped_nsobject.h" #include "base/mac/scoped_nsobject.h"
#import "chrome/browser/ui/cocoa/notifications/notification_builder_base.h"
@class NSUserNotification; @class NSUserNotification;
...@@ -31,46 +32,18 @@ ...@@ -31,46 +32,18 @@
// // Deserialize the |notificationData| in to a new builder. // // Deserialize the |notificationData| in to a new builder.
// base::scoped_nsobject<NotificationBuilder> finalBuilder( // base::scoped_nsobject<NotificationBuilder> finalBuilder(
// [[NotificationBuilder alloc] initWithData:notificationData]); // [[NotificationBuilder alloc] initWithData:notificationData]);
@interface NotificationBuilder : NSObject @interface NotificationBuilder : NotificationBuilderBase
// Initializes a builder by deserializing |data|. The |data| must have been
// generated by calling the buildDictionary function on another builder
// instance.
- (instancetype)initWithDictionary:(NSDictionary*)data;
// Initializes a builder with default values for the button labels. // Initializes a builder with default values for the button labels.
- (instancetype)initWithCloseLabel:(NSString*)closeLabel - (instancetype)initWithCloseLabel:(NSString*)closeLabel
optionsLabel:(NSString*)optionsLabel optionsLabel:(NSString*)optionsLabel
settingsLabel:(NSString*)settingsLabel; settingsLabel:(NSString*)settingsLabel;
// Setters
// Note for XPC users. Always use the setters from Chrome's main app. Do not
// attempt to use them from XPC since some of the default strings and other
// defaults are not available from the xpc service.
- (void)setTitle:(NSString*)title;
- (void)setSubTitle:(NSString*)subTitle;
- (void)setContextMessage:(NSString*)contextMessage;
- (void)setIcon:(NSImage*)icon;
- (void)setButtons:(NSString*)primaryButton
secondaryButton:(NSString*)secondaryButton;
- (void)setTag:(NSString*)tag;
- (void)setOrigin:(NSString*)origin;
- (void)setNotificationId:(NSString*)notificationId;
- (void)setProfileId:(NSString*)profileId;
- (void)setIncognito:(BOOL)incognito;
- (void)setCreatorPid:(NSNumber*)pid;
- (void)setNotificationType:(NSNumber*)notificationType;
- (void)setShowSettingsButton:(BOOL)showSettingsButton;
// Returns a notification ready to be displayed out of the provided // Returns a notification ready to be displayed out of the provided
// |notificationData|. // |notificationData|.
- (NSUserNotification*)buildUserNotification; - (NSUserNotification*)buildUserNotification;
// Returns a representation of a notification that can be serialized.
// Another instance of NotificationBuilder can read this directly and generate
// a notification out of it via the |buildbuildUserNotification| method.
- (NSDictionary*)buildDictionary;
@end @end
#endif // CHROME_BROWSER_UI_COCOA_NOTIFICATIONS_NOTIFICATION_BUILDER_MAC_H_ #endif // CHROME_BROWSER_UI_COCOA_NOTIFICATIONS_NOTIFICATION_BUILDER_MAC_H_
...@@ -9,6 +9,17 @@ ...@@ -9,6 +9,17 @@
namespace notification_constants { namespace notification_constants {
extern NSString* const kNotificationTitle;
extern NSString* const kNotificationSubTitle;
extern NSString* const kNotificationInformativeText;
extern NSString* const kNotificationImage;
extern NSString* const kNotificationButtonOne;
extern NSString* const kNotificationButtonTwo;
extern NSString* const kNotificationTag;
extern NSString* const kNotificationCloseButtonTag;
extern NSString* const kNotificationOptionsButtonTag;
extern NSString* const kNotificationSettingsButtonTag;
extern NSString* const kNotificationOrigin; extern NSString* const kNotificationOrigin;
extern NSString* const kNotificationId; extern NSString* const kNotificationId;
extern NSString* const kNotificationProfileId; extern NSString* const kNotificationProfileId;
......
...@@ -6,6 +6,19 @@ ...@@ -6,6 +6,19 @@
namespace notification_constants { namespace notification_constants {
// Applicable to NotificationBuilderBase -> NotificationBuilder,
// UNNotificationBuilder
NSString* const kNotificationTitle = @"title";
NSString* const kNotificationSubTitle = @"subtitle";
NSString* const kNotificationInformativeText = @"informativeText";
NSString* const kNotificationImage = @"icon";
NSString* const kNotificationButtonOne = @"buttonOne";
NSString* const kNotificationButtonTwo = @"buttonTwo";
NSString* const kNotificationTag = @"tag";
NSString* const kNotificationCloseButtonTag = @"closeButton";
NSString* const kNotificationOptionsButtonTag = @"optionsButton";
NSString* const kNotificationSettingsButtonTag = @"settingsButton";
// Applicable to NotificationBuilder and NotificationResponseBuilder // Applicable to NotificationBuilder and NotificationResponseBuilder
NSString* const kNotificationOrigin = @"notificationOrigin"; NSString* const kNotificationOrigin = @"notificationOrigin";
NSString* const kNotificationId = @"notificationId"; NSString* const kNotificationId = @"notificationId";
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_COCOA_NOTIFICATIONS_UNNOTIFICATION_BUILDER_MAC_H_
#define CHROME_BROWSER_UI_COCOA_NOTIFICATIONS_UNNOTIFICATION_BUILDER_MAC_H_
#import <Foundation/Foundation.h>
#include "base/mac/scoped_nsobject.h"
#import "chrome/browser/ui/cocoa/notifications/notification_builder_base.h"
@class UNMutableNotificationContent;
// Provides a marshallable way for storing the information required to construct
// a UNMutableNotificationContent that is to be displayed on the system.
//
// A quick example:
// base::scoped_nsobject<UNNotificationBuilder> builder(
// [[UNNotificationBuilder alloc] init]);
// [builder setTitle:@"Hello"];
//
// // Build a notification out of the data.
// UNMutableNotificationContent* notification =
// [builder buildUserNotification];
//
// // Serialize a notification out of the data.
// NSDictionary* notificationData = [builder buildDictionary];
//
// // Deserialize the |notificationData| in to a new builder.
// base::scoped_nsobject<UNNotificationBuilder> finalBuilder(
// [[UNNotificationBuilder alloc] initWithData:notificationData]);
API_AVAILABLE(macosx(10.14))
@interface UNNotificationBuilder : NotificationBuilderBase
// Returns a notification ready to be displayed out of the provided
// |notificationData|.
- (UNMutableNotificationContent*)buildUserNotification;
@end
#endif // CHROME_BROWSER_UI_COCOA_NOTIFICATIONS_UNNOTIFICATION_BUILDER_MAC_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/cocoa/notifications/unnotification_builder_mac.h"
#import <AppKit/AppKit.h>
#import <UserNotifications/UserNotifications.h>
#include "base/mac/mac_util.h"
#include "base/mac/scoped_nsobject.h"
#include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h"
@implementation UNNotificationBuilder
- (UNMutableNotificationContent*)buildUserNotification {
base::scoped_nsobject<UNMutableNotificationContent> toast(
[[UNMutableNotificationContent alloc] init]);
[toast setTitle:[_notificationData
objectForKey:notification_constants::kNotificationTitle]];
[toast
setSubtitle:[_notificationData objectForKey:notification_constants::
kNotificationSubTitle]];
[toast setBody:[_notificationData
objectForKey:notification_constants::
kNotificationInformativeText]];
// Type (needed to define the buttons)
NSNumber* type = [_notificationData
objectForKey:notification_constants::kNotificationType];
NSString* origin =
[_notificationData
objectForKey:notification_constants::kNotificationOrigin]
? [_notificationData
objectForKey:notification_constants::kNotificationOrigin]
: @"";
DCHECK(
[_notificationData objectForKey:notification_constants::kNotificationId]);
NSString* notificationId =
[_notificationData objectForKey:notification_constants::kNotificationId];
DCHECK([_notificationData
objectForKey:notification_constants::kNotificationProfileId]);
NSString* profileId = [_notificationData
objectForKey:notification_constants::kNotificationProfileId];
DCHECK([_notificationData
objectForKey:notification_constants::kNotificationCreatorPid]);
NSNumber* creatorPid = [_notificationData
objectForKey:notification_constants::kNotificationCreatorPid];
DCHECK([_notificationData
objectForKey:notification_constants::kNotificationIncognito]);
NSNumber* incognito = [_notificationData
objectForKey:notification_constants::kNotificationIncognito];
[toast setUserInfo:@{
notification_constants::kNotificationOrigin : origin,
notification_constants::kNotificationId : notificationId,
notification_constants::kNotificationProfileId : profileId,
notification_constants::kNotificationIncognito : incognito,
notification_constants::kNotificationType : type,
notification_constants::kNotificationCreatorPid : creatorPid,
}];
return toast.autorelease();
}
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment