OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/notifications/notification_platform_bridge_mac.h" | 5 #include "chrome/browser/notifications/notification_platform_bridge_mac.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // A Cocoa class that represents the delegate of NSUserNotificationCenter and | 107 // A Cocoa class that represents the delegate of NSUserNotificationCenter and |
108 // can forward commands to C++. | 108 // can forward commands to C++. |
109 @interface NotificationCenterDelegate | 109 @interface NotificationCenterDelegate |
110 : NSObject<NSUserNotificationCenterDelegate> { | 110 : NSObject<NSUserNotificationCenterDelegate> { |
111 } | 111 } |
112 @end | 112 @end |
113 | 113 |
114 // Interface to communicate with the Alert XPC service. | 114 // Interface to communicate with the Alert XPC service. |
115 @interface NotificationRemoteDispatcher : NSObject | 115 @interface NotificationRemoteDispatcher : NSObject |
116 | 116 |
| 117 // Deliver a notification to the XPC service to be displayed as an alert. |
117 - (void)dispatchNotification:(NSDictionary*)data; | 118 - (void)dispatchNotification:(NSDictionary*)data; |
118 | 119 |
| 120 // Close a notification for a given |notificationId| and |profileId|. |
| 121 - (void)closeNotificationWithId:(NSString*)notificationId |
| 122 withProfileId:(NSString*)profileId; |
| 123 |
| 124 // Close all notifications. |
| 125 - (void)closeAllNotifications; |
| 126 |
119 @end | 127 @end |
120 | 128 |
121 // ///////////////////////////////////////////////////////////////////////////// | 129 // ///////////////////////////////////////////////////////////////////////////// |
122 | 130 |
123 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac( | 131 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac( |
124 NSUserNotificationCenter* notification_center) | 132 NSUserNotificationCenter* notification_center) |
125 : delegate_([NotificationCenterDelegate alloc]), | 133 : delegate_([NotificationCenterDelegate alloc]), |
126 notification_center_(notification_center), | 134 notification_center_(notification_center), |
127 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 135 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
128 notification_remote_dispatcher_( | 136 notification_remote_dispatcher_( |
129 [[NotificationRemoteDispatcher alloc] init]) | 137 [[NotificationRemoteDispatcher alloc] init]) |
130 #else | 138 #else |
131 notification_remote_dispatcher_(nullptr) | 139 notification_remote_dispatcher_(nullptr) |
132 #endif // ENABLE_XPC_NOTIFICATIONS | 140 #endif // ENABLE_XPC_NOTIFICATIONS |
133 { | 141 { |
134 [notification_center_ setDelegate:delegate_.get()]; | 142 [notification_center_ setDelegate:delegate_.get()]; |
135 } | 143 } |
136 | 144 |
137 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { | 145 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { |
138 [notification_center_ setDelegate:nil]; | 146 [notification_center_ setDelegate:nil]; |
139 | 147 |
140 // TODO(miguelg) remove only alerts shown by the XPC service. | |
141 // TODO(miguelg) do not remove banners if possible. | 148 // TODO(miguelg) do not remove banners if possible. |
142 [notification_center_ removeAllDeliveredNotifications]; | 149 [notification_center_ removeAllDeliveredNotifications]; |
| 150 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
| 151 [notification_remote_dispatcher_ closeAllNotifications]; |
| 152 #endif // BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
143 } | 153 } |
144 | 154 |
145 void NotificationPlatformBridgeMac::Display( | 155 void NotificationPlatformBridgeMac::Display( |
146 NotificationCommon::Type notification_type, | 156 NotificationCommon::Type notification_type, |
147 const std::string& notification_id, | 157 const std::string& notification_id, |
148 const std::string& profile_id, | 158 const std::string& profile_id, |
149 bool incognito, | 159 bool incognito, |
150 const Notification& notification) { | 160 const Notification& notification) { |
151 base::scoped_nsobject<NotificationBuilder> builder( | 161 base::scoped_nsobject<NotificationBuilder> builder( |
152 [[NotificationBuilder alloc] | 162 [[NotificationBuilder alloc] |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 } | 234 } |
225 #else | 235 #else |
226 NSUserNotification* toast = [builder buildUserNotification]; | 236 NSUserNotification* toast = [builder buildUserNotification]; |
227 [notification_center_ deliverNotification:toast]; | 237 [notification_center_ deliverNotification:toast]; |
228 #endif // ENABLE_XPC_NOTIFICATIONS | 238 #endif // ENABLE_XPC_NOTIFICATIONS |
229 } | 239 } |
230 | 240 |
231 void NotificationPlatformBridgeMac::Close(const std::string& profile_id, | 241 void NotificationPlatformBridgeMac::Close(const std::string& profile_id, |
232 const std::string& notification_id) { | 242 const std::string& notification_id) { |
233 NSString* candidate_id = base::SysUTF8ToNSString(notification_id); | 243 NSString* candidate_id = base::SysUTF8ToNSString(notification_id); |
| 244 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); |
234 | 245 |
235 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); | 246 bool notification_removed = false; |
236 for (NSUserNotification* toast in | 247 for (NSUserNotification* toast in |
237 [notification_center_ deliveredNotifications]) { | 248 [notification_center_ deliveredNotifications]) { |
238 NSString* toast_id = | 249 NSString* toast_id = |
239 [toast.userInfo objectForKey:notification_constants::kNotificationId]; | 250 [toast.userInfo objectForKey:notification_constants::kNotificationId]; |
240 | 251 |
241 NSString* persistent_profile_id = [toast.userInfo | 252 NSString* persistent_profile_id = [toast.userInfo |
242 objectForKey:notification_constants::kNotificationProfileId]; | 253 objectForKey:notification_constants::kNotificationProfileId]; |
243 | 254 |
244 if ([toast_id isEqualToString:candidate_id] && | 255 if ([toast_id isEqualToString:candidate_id] && |
245 [persistent_profile_id isEqualToString:current_profile_id]) { | 256 [persistent_profile_id isEqualToString:current_profile_id]) { |
246 [notification_center_ removeDeliveredNotification:toast]; | 257 [notification_center_ removeDeliveredNotification:toast]; |
| 258 notification_removed = true; |
| 259 break; |
247 } | 260 } |
248 } | 261 } |
| 262 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
| 263 // If no banner existed with that ID try to see if there is an alert |
| 264 // in the xpc server. |
| 265 if (!notification_removed) { |
| 266 [notification_remote_dispatcher_ |
| 267 closeNotificationWithId:candidate_id |
| 268 withProfileId:current_profile_id]; |
| 269 } |
| 270 #endif // ENABLE_XPC_NOTIFICATIONS |
249 } | 271 } |
250 | 272 |
251 bool NotificationPlatformBridgeMac::GetDisplayed( | 273 bool NotificationPlatformBridgeMac::GetDisplayed( |
252 const std::string& profile_id, | 274 const std::string& profile_id, |
253 bool incognito, | 275 bool incognito, |
254 std::set<std::string>* notifications) const { | 276 std::set<std::string>* notifications) const { |
255 DCHECK(notifications); | 277 DCHECK(notifications); |
256 | 278 |
257 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); | 279 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); |
258 for (NSUserNotification* toast in | 280 for (NSUserNotification* toast in |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 [xpcConnection_ resume]; | 456 [xpcConnection_ resume]; |
435 } | 457 } |
436 | 458 |
437 return self; | 459 return self; |
438 } | 460 } |
439 | 461 |
440 - (void)dispatchNotification:(NSDictionary*)data { | 462 - (void)dispatchNotification:(NSDictionary*)data { |
441 [[xpcConnection_ remoteObjectProxy] deliverNotification:data]; | 463 [[xpcConnection_ remoteObjectProxy] deliverNotification:data]; |
442 } | 464 } |
443 | 465 |
| 466 - (void)closeNotificationWithId:(NSString*)notificationId |
| 467 withProfileId:(NSString*)profileId { |
| 468 [[xpcConnection_ remoteObjectProxy] closeNotificationWithId:notificationId |
| 469 withProfileId:profileId]; |
| 470 } |
| 471 |
| 472 - (void)closeAllNotifications { |
| 473 [[xpcConnection_ remoteObjectProxy] closeAllNotifications]; |
| 474 } |
| 475 |
444 // NotificationReply implementation | 476 // NotificationReply implementation |
445 - (void)notificationClick:(NSDictionary*)notificationResponseData { | 477 - (void)notificationClick:(NSDictionary*)notificationResponseData { |
446 NotificationPlatformBridgeMac::ProcessNotificationResponse( | 478 NotificationPlatformBridgeMac::ProcessNotificationResponse( |
447 notificationResponseData); | 479 notificationResponseData); |
448 } | 480 } |
449 | 481 |
450 @end | 482 @end |
OLD | NEW |