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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 NotificationPlatformBridge* NotificationPlatformBridge::Create() { | 102 NotificationPlatformBridge* NotificationPlatformBridge::Create() { |
103 return new NotificationPlatformBridgeMac( | 103 return new NotificationPlatformBridgeMac( |
104 [NSUserNotificationCenter defaultUserNotificationCenter]); | 104 [NSUserNotificationCenter defaultUserNotificationCenter]); |
105 } | 105 } |
106 | 106 |
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 | |
Robert Sesek
2016/10/12 15:37:36
nit: remove extra blank line
Miguel Garcia
2016/10/15 09:48:49
Done.
| |
112 @end | 113 @end |
113 | 114 |
114 // Interface to communicate with the Alert XPC service. | 115 // Interface to communicate with the Alert XPC service. |
115 @interface NotificationRemoteDispatcher : NSObject | 116 @interface NotificationRemoteDispatcher : NSObject |
116 | 117 |
118 // Deliver a notification to the XPC service to be displayed as an alert. | |
117 - (void)dispatchNotification:(NSDictionary*)data; | 119 - (void)dispatchNotification:(NSDictionary*)data; |
118 | 120 |
121 // Close a notification for a given |notificationId| and |profileId|. | |
122 - (void)closeNotificationWithId:(NSString*)notificationId | |
123 withProfileId:(NSString*)profileId; | |
124 | |
125 // Close all notifications. | |
126 - (void)closeAllNotifications; | |
127 | |
119 @end | 128 @end |
120 | 129 |
121 // ///////////////////////////////////////////////////////////////////////////// | 130 // ///////////////////////////////////////////////////////////////////////////// |
122 | 131 |
123 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac( | 132 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac( |
124 NSUserNotificationCenter* notification_center) | 133 NSUserNotificationCenter* notification_center) |
125 : delegate_([NotificationCenterDelegate alloc]), | 134 : delegate_([NotificationCenterDelegate alloc]), |
126 notification_center_(notification_center), | 135 notification_center_(notification_center), |
127 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 136 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
128 notification_remote_dispatcher_( | 137 notification_remote_dispatcher_( |
129 [[NotificationRemoteDispatcher alloc] init]) | 138 [[NotificationRemoteDispatcher alloc] init]) |
130 #else | 139 #else |
131 notification_remote_dispatcher_(nullptr) | 140 notification_remote_dispatcher_(nullptr) |
132 #endif // ENABLE_XPC_NOTIFICATIONS | 141 #endif // ENABLE_XPC_NOTIFICATIONS |
133 { | 142 { |
134 [notification_center_ setDelegate:delegate_.get()]; | 143 [notification_center_ setDelegate:delegate_.get()]; |
135 } | 144 } |
136 | 145 |
137 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { | 146 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { |
138 [notification_center_ setDelegate:nil]; | 147 [notification_center_ setDelegate:nil]; |
139 | 148 |
140 // TODO(miguelg) remove only alerts shown by the XPC service. | |
141 // TODO(miguelg) do not remove banners if possible. | 149 // TODO(miguelg) do not remove banners if possible. |
142 [notification_center_ removeAllDeliveredNotifications]; | 150 [notification_center_ removeAllDeliveredNotifications]; |
151 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | |
152 [notification_remote_dispatcher_ closeAllNotifications]; | |
153 #endif // BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | |
143 } | 154 } |
144 | 155 |
145 void NotificationPlatformBridgeMac::Display( | 156 void NotificationPlatformBridgeMac::Display( |
146 NotificationCommon::Type notification_type, | 157 NotificationCommon::Type notification_type, |
147 const std::string& notification_id, | 158 const std::string& notification_id, |
148 const std::string& profile_id, | 159 const std::string& profile_id, |
149 bool incognito, | 160 bool incognito, |
150 const Notification& notification) { | 161 const Notification& notification) { |
151 base::scoped_nsobject<NotificationBuilder> builder( | 162 base::scoped_nsobject<NotificationBuilder> builder( |
152 [[NotificationBuilder alloc] | 163 [[NotificationBuilder alloc] |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 } | 235 } |
225 #else | 236 #else |
226 NSUserNotification* toast = [builder buildUserNotification]; | 237 NSUserNotification* toast = [builder buildUserNotification]; |
227 [notification_center_ deliverNotification:toast]; | 238 [notification_center_ deliverNotification:toast]; |
228 #endif // ENABLE_XPC_NOTIFICATIONS | 239 #endif // ENABLE_XPC_NOTIFICATIONS |
229 } | 240 } |
230 | 241 |
231 void NotificationPlatformBridgeMac::Close(const std::string& profile_id, | 242 void NotificationPlatformBridgeMac::Close(const std::string& profile_id, |
232 const std::string& notification_id) { | 243 const std::string& notification_id) { |
233 NSString* candidate_id = base::SysUTF8ToNSString(notification_id); | 244 NSString* candidate_id = base::SysUTF8ToNSString(notification_id); |
245 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); | |
234 | 246 |
235 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); | 247 bool notification_removed = false; |
236 for (NSUserNotification* toast in | 248 for (NSUserNotification* toast in |
237 [notification_center_ deliveredNotifications]) { | 249 [notification_center_ deliveredNotifications]) { |
238 NSString* toast_id = | 250 NSString* toast_id = |
239 [toast.userInfo objectForKey:notification_constants::kNotificationId]; | 251 [toast.userInfo objectForKey:notification_constants::kNotificationId]; |
240 | 252 |
241 NSString* persistent_profile_id = [toast.userInfo | 253 NSString* persistent_profile_id = [toast.userInfo |
242 objectForKey:notification_constants::kNotificationProfileId]; | 254 objectForKey:notification_constants::kNotificationProfileId]; |
243 | 255 |
244 if ([toast_id isEqualToString:candidate_id] && | 256 if ([toast_id isEqualToString:candidate_id] && |
245 [persistent_profile_id isEqualToString:current_profile_id]) { | 257 [persistent_profile_id isEqualToString:current_profile_id]) { |
246 [notification_center_ removeDeliveredNotification:toast]; | 258 [notification_center_ removeDeliveredNotification:toast]; |
259 notification_removed = true; | |
260 break; | |
247 } | 261 } |
248 } | 262 } |
263 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | |
264 // If no banner existed with that ID try to see if there is an alert | |
265 // in the xpc server. | |
266 if (!notification_removed) { | |
267 [notification_remote_dispatcher_ | |
268 closeNotificationWithId:candidate_id | |
269 withProfileId:current_profile_id]; | |
270 } | |
271 #endif // ENABLE_XPC_NOTIFICATIONS | |
249 } | 272 } |
250 | 273 |
251 bool NotificationPlatformBridgeMac::GetDisplayed( | 274 bool NotificationPlatformBridgeMac::GetDisplayed( |
252 const std::string& profile_id, | 275 const std::string& profile_id, |
253 bool incognito, | 276 bool incognito, |
254 std::set<std::string>* notifications) const { | 277 std::set<std::string>* notifications) const { |
255 DCHECK(notifications); | 278 DCHECK(notifications); |
256 | 279 |
257 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); | 280 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); |
258 for (NSUserNotification* toast in | 281 for (NSUserNotification* toast in |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
396 shouldPresentNotification:(NSUserNotification*)nsNotification { | 419 shouldPresentNotification:(NSUserNotification*)nsNotification { |
397 // Always display notifications, regardless of whether the app is foreground. | 420 // Always display notifications, regardless of whether the app is foreground. |
398 return YES; | 421 return YES; |
399 } | 422 } |
400 | 423 |
401 @end | 424 @end |
402 | 425 |
403 @implementation NotificationRemoteDispatcher { | 426 @implementation NotificationRemoteDispatcher { |
404 // The connection to the XPC server in charge of delivering alerts. | 427 // The connection to the XPC server in charge of delivering alerts. |
405 base::scoped_nsobject<NSXPCConnection> xpcConnection_; | 428 base::scoped_nsobject<NSXPCConnection> xpcConnection_; |
429 base::scoped_nsobject<NSMutableSet> alertsDisplayed_; | |
Peter Beverloo
2016/10/12 12:20:38
delete (line 434 too)
Miguel Garcia
2016/10/15 09:48:49
Done.
| |
406 } | 430 } |
407 | 431 |
408 - (instancetype)init { | 432 - (instancetype)init { |
409 if ((self = [super init])) { | 433 if ((self = [super init])) { |
434 alertsDisplayed_.reset([[NSMutableSet alloc] init]); | |
410 xpcConnection_.reset([[NSXPCConnection alloc] | 435 xpcConnection_.reset([[NSXPCConnection alloc] |
411 initWithServiceName: | 436 initWithServiceName: |
412 [NSString | 437 [NSString |
413 stringWithFormat:notification_constants::kAlertXPCServiceName, | 438 stringWithFormat:notification_constants::kAlertXPCServiceName, |
414 [base::mac::OuterBundle() bundleIdentifier]]]); | 439 [base::mac::OuterBundle() bundleIdentifier]]]); |
415 xpcConnection_.get().remoteObjectInterface = | 440 xpcConnection_.get().remoteObjectInterface = |
416 [NSXPCInterface interfaceWithProtocol:@protocol(NotificationDelivery)]; | 441 [NSXPCInterface interfaceWithProtocol:@protocol(NotificationDelivery)]; |
417 | 442 |
418 xpcConnection_.get().interruptionHandler = ^{ | 443 xpcConnection_.get().interruptionHandler = ^{ |
419 LOG(WARNING) << "connection interrupted: interruptionHandler: "; | 444 LOG(WARNING) << "connection interrupted: interruptionHandler: "; |
(...skipping 14 matching lines...) Expand all Loading... | |
434 [xpcConnection_ resume]; | 459 [xpcConnection_ resume]; |
435 } | 460 } |
436 | 461 |
437 return self; | 462 return self; |
438 } | 463 } |
439 | 464 |
440 - (void)dispatchNotification:(NSDictionary*)data { | 465 - (void)dispatchNotification:(NSDictionary*)data { |
441 [[xpcConnection_ remoteObjectProxy] deliverNotification:data]; | 466 [[xpcConnection_ remoteObjectProxy] deliverNotification:data]; |
442 } | 467 } |
443 | 468 |
469 - (void)closeNotificationWithId:(NSString*)notificationId | |
470 withProfileId:(NSString*)profileId { | |
471 [[xpcConnection_ remoteObjectProxy] closeNotificationWithId:notificationId | |
472 withProfileId:profileId]; | |
473 } | |
474 | |
475 - (void)closeAllNotifications { | |
476 [[xpcConnection_ remoteObjectProxy] closeAllNotifications]; | |
477 } | |
478 | |
444 // NotificationReply implementation | 479 // NotificationReply implementation |
445 - (void)notificationClick:(NSDictionary*)notificationResponseData { | 480 - (void)notificationClick:(NSDictionary*)notificationResponseData { |
446 NotificationPlatformBridgeMac::ProcessNotificationResponse( | 481 NotificationPlatformBridgeMac::ProcessNotificationResponse( |
447 notificationResponseData); | 482 notificationResponseData); |
448 } | 483 } |
449 | 484 |
450 @end | 485 @end |
OLD | NEW |