Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2172)

Unified Diff: chrome/browser/notifications/notification_platform_bridge_mac.mm

Issue 2402303002: Implement the ability to close alerts programatically (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/notifications/notification_platform_bridge_mac.mm
diff --git a/chrome/browser/notifications/notification_platform_bridge_mac.mm b/chrome/browser/notifications/notification_platform_bridge_mac.mm
index 6b3c20dc60e789395257adb0ddd1f115608ac000..52e8fe82f67875cca9c2ea0c0dc6ed514ba1c9ca 100644
--- a/chrome/browser/notifications/notification_platform_bridge_mac.mm
+++ b/chrome/browser/notifications/notification_platform_bridge_mac.mm
@@ -114,8 +114,21 @@ NotificationPlatformBridge* NotificationPlatformBridge::Create() {
// Interface to communicate with the Alert XPC service.
@interface NotificationRemoteDispatcher : NSObject
+// Deliver a notification to the XPC service to be displayed as an alert.
- (void)dispatchNotification:(NSDictionary*)data;
+// CLose a notification for a given |notificationId| and |profileId|.
+- (void)closeNotification:(NSString*)notificationId
+ withProfile:(NSString*)profileId;
+
+// Close all notifications.
+- (void)closeAllNotifications;
+
+// Return true if an alert with the given |notificationId| and |profileId|
+// is being displayed. This is based on cached results so it might not
+// be totally accurate.
+- (BOOL)isAlertDisplayed:(NSString*)notificationId
+ withProfile:(NSString*)profileId;
@end
// /////////////////////////////////////////////////////////////////////////////
@@ -137,9 +150,11 @@ NotificationPlatformBridgeMac::NotificationPlatformBridgeMac(
NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() {
[notification_center_ setDelegate:nil];
- // TODO(miguelg) remove only alerts shown by the XPC service.
// TODO(miguelg) do not remove banners if possible.
[notification_center_ removeAllDeliveredNotifications];
+#if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS)
+ [notification_remote_dispatcher_ closeAllNotifications];
+#endif // BUILDFLAG(ENABLE_XPC_NOTIFICATIONS)
}
void NotificationPlatformBridgeMac::Display(
@@ -231,8 +246,16 @@ void NotificationPlatformBridgeMac::Display(
void NotificationPlatformBridgeMac::Close(const std::string& profile_id,
const std::string& notification_id) {
NSString* candidate_id = base::SysUTF8ToNSString(notification_id);
-
NSString* current_profile_id = base::SysUTF8ToNSString(profile_id);
+#if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS)
+ if ([notification_remote_dispatcher_ isAlertDisplayed:candidate_id
+ withProfile:current_profile_id]) {
+ [notification_remote_dispatcher_ closeNotification:candidate_id
+ withProfile:current_profile_id];
+ return;
+ }
+#endif // ENABLE_XPC_NOTIFICATIONS
+
for (NSUserNotification* toast in
[notification_center_ deliveredNotifications]) {
NSString* toast_id =
@@ -403,10 +426,12 @@ bool NotificationPlatformBridgeMac::VerifyNotificationData(
@implementation NotificationRemoteDispatcher {
// The connection to the XPC server in charge of delivering alerts.
base::scoped_nsobject<NSXPCConnection> xpcConnection_;
+ base::scoped_nsobject<NSMutableSet> alertsDisplayed_;
}
- (instancetype)init {
if ((self = [super init])) {
+ alertsDisplayed_.reset([[NSMutableSet alloc] init]);
Robert Sesek 2016/10/10 18:22:47 What is the purpose of this set? It looks like thi
Miguel Garcia 2016/10/11 11:55:41 It is consulted through isAlertDisplayed which is
Robert Sesek 2016/10/11 15:28:41 Sorry, missed that due to a find-in-page bug on Ma
xpcConnection_.reset([[NSXPCConnection alloc]
initWithServiceName:
[NSString
@@ -438,9 +463,44 @@ bool NotificationPlatformBridgeMac::VerifyNotificationData(
}
- (void)dispatchNotification:(NSDictionary*)data {
+ NSString* notificationId =
+ [data objectForKey:notification_constants::kNotificationId];
+ NSString* profileId =
+ [data objectForKey:notification_constants::kNotificationProfileId];
+ NSString* alertKey = [self _createAlertKeyWithNotification:notificationId
+ withProfile:profileId];
+ [alertsDisplayed_ addObject:alertKey];
[[xpcConnection_ remoteObjectProxy] deliverNotification:data];
}
+- (void)closeNotification:(NSString*)notificationId
+ withProfile:(NSString*)profileId {
+ NSString* alertKey = [self _createAlertKeyWithNotification:notificationId
+ withProfile:profileId];
+ [alertsDisplayed_ removeObject:alertKey];
+ [[xpcConnection_ remoteObjectProxy] closeNotification:notificationId
+ withProfile:profileId];
+}
+
+- (void)closeAllNotifications {
+ [alertsDisplayed_ removeAllObjects];
+ [[xpcConnection_ remoteObjectProxy] closeAllNotifications];
+}
+
+- (BOOL)isAlertDisplayed:(NSString*)notificationId
+ withProfile:(NSString*)profileId {
+ NSString* alertKey = [self _createAlertKeyWithNotification:notificationId
+ withProfile:profileId];
+ return [alertsDisplayed_ containsObject:alertKey];
+}
+
+// Creates a unique identifier for the notification. Only used for internal
+// tracking.
+- (NSString*)_createAlertKeyWithNotification:(NSString*)notificationId
Robert Sesek 2016/10/10 18:22:47 We don't name private methods with _.
Miguel Garcia 2016/10/11 11:55:41 Done.
+ withProfile:(NSString*)profileId {
+ return [NSString stringWithFormat:@"%@:%@", notificationId, profileId];
+}
+
// NotificationReply implementation
- (void)notificationClick:(NSDictionary*)notificationResponseData {
NotificationPlatformBridgeMac::ProcessNotificationResponse(

Powered by Google App Engine
This is Rietveld 408576698