| 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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 notification_center_([notification_center retain]), | 137 notification_center_([notification_center retain]), |
| 138 alert_dispatcher_([alert_dispatcher retain]) { | 138 alert_dispatcher_([alert_dispatcher retain]) { |
| 139 [notification_center_ setDelegate:delegate_.get()]; | 139 [notification_center_ setDelegate:delegate_.get()]; |
| 140 } | 140 } |
| 141 | 141 |
| 142 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { | 142 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { |
| 143 [notification_center_ setDelegate:nil]; | 143 [notification_center_ setDelegate:nil]; |
| 144 | 144 |
| 145 // TODO(miguelg) do not remove banners if possible. | 145 // TODO(miguelg) do not remove banners if possible. |
| 146 [notification_center_ removeAllDeliveredNotifications]; | 146 [notification_center_ removeAllDeliveredNotifications]; |
| 147 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | |
| 148 [alert_dispatcher_ closeAllNotifications]; | 147 [alert_dispatcher_ closeAllNotifications]; |
| 149 #endif // BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | |
| 150 } | 148 } |
| 151 | 149 |
| 152 // static | 150 // static |
| 153 NotificationPlatformBridge* NotificationPlatformBridge::Create() { | 151 NotificationPlatformBridge* NotificationPlatformBridge::Create() { |
| 154 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | |
| 155 base::scoped_nsobject<AlertDispatcherImpl> alert_dispatcher( | 152 base::scoped_nsobject<AlertDispatcherImpl> alert_dispatcher( |
| 156 [[AlertDispatcherImpl alloc] init]); | 153 [[AlertDispatcherImpl alloc] init]); |
| 157 return new NotificationPlatformBridgeMac( | 154 return new NotificationPlatformBridgeMac( |
| 158 [NSUserNotificationCenter defaultUserNotificationCenter], | 155 [NSUserNotificationCenter defaultUserNotificationCenter], |
| 159 alert_dispatcher.get()); | 156 alert_dispatcher.get()); |
| 160 #else | |
| 161 return new NotificationPlatformBridgeMac( | |
| 162 [NSUserNotificationCenter defaultUserNotificationCenter], nil); | |
| 163 #endif // ENABLE_XPC_NOTIFICATIONS | |
| 164 } | 157 } |
| 165 | 158 |
| 166 void NotificationPlatformBridgeMac::Display( | 159 void NotificationPlatformBridgeMac::Display( |
| 167 NotificationCommon::Type notification_type, | 160 NotificationCommon::Type notification_type, |
| 168 const std::string& notification_id, | 161 const std::string& notification_id, |
| 169 const std::string& profile_id, | 162 const std::string& profile_id, |
| 170 bool incognito, | 163 bool incognito, |
| 171 const Notification& notification) { | 164 const Notification& notification) { |
| 172 base::scoped_nsobject<NotificationBuilder> builder( | 165 base::scoped_nsobject<NotificationBuilder> builder( |
| 173 [[NotificationBuilder alloc] | 166 [[NotificationBuilder alloc] |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } | 218 } |
| 226 } | 219 } |
| 227 } | 220 } |
| 228 | 221 |
| 229 [builder setOrigin:base::SysUTF8ToNSString(notification.origin_url().spec())]; | 222 [builder setOrigin:base::SysUTF8ToNSString(notification.origin_url().spec())]; |
| 230 [builder setNotificationId:base::SysUTF8ToNSString(notification_id)]; | 223 [builder setNotificationId:base::SysUTF8ToNSString(notification_id)]; |
| 231 [builder setProfileId:base::SysUTF8ToNSString(profile_id)]; | 224 [builder setProfileId:base::SysUTF8ToNSString(profile_id)]; |
| 232 [builder setIncognito:incognito]; | 225 [builder setIncognito:incognito]; |
| 233 [builder setNotificationType:[NSNumber numberWithInteger:notification_type]]; | 226 [builder setNotificationType:[NSNumber numberWithInteger:notification_type]]; |
| 234 | 227 |
| 235 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | |
| 236 // Send persistent notifications to the XPC service so they | 228 // Send persistent notifications to the XPC service so they |
| 237 // can be displayed as alerts. Chrome itself can only display | 229 // can be displayed as alerts. Chrome itself can only display |
| 238 // banners. | 230 // banners. |
| 239 if (notification.never_timeout()) { | 231 if (notification.never_timeout()) { |
| 240 NSDictionary* dict = [builder buildDictionary]; | 232 NSDictionary* dict = [builder buildDictionary]; |
| 241 [alert_dispatcher_ dispatchNotification:dict]; | 233 [alert_dispatcher_ dispatchNotification:dict]; |
| 242 } else { | 234 } else { |
| 243 NSUserNotification* toast = [builder buildUserNotification]; | 235 NSUserNotification* toast = [builder buildUserNotification]; |
| 244 [notification_center_ deliverNotification:toast]; | 236 [notification_center_ deliverNotification:toast]; |
| 245 } | 237 } |
| 246 #else | |
| 247 NSUserNotification* toast = [builder buildUserNotification]; | |
| 248 [notification_center_ deliverNotification:toast]; | |
| 249 #endif // ENABLE_XPC_NOTIFICATIONS | |
| 250 } | 238 } |
| 251 | 239 |
| 252 void NotificationPlatformBridgeMac::Close(const std::string& profile_id, | 240 void NotificationPlatformBridgeMac::Close(const std::string& profile_id, |
| 253 const std::string& notification_id) { | 241 const std::string& notification_id) { |
| 254 NSString* candidate_id = base::SysUTF8ToNSString(notification_id); | 242 NSString* candidate_id = base::SysUTF8ToNSString(notification_id); |
| 255 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); | 243 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); |
| 256 | 244 |
| 257 bool notification_removed = false; | 245 bool notification_removed = false; |
| 258 for (NSUserNotification* toast in | 246 for (NSUserNotification* toast in |
| 259 [notification_center_ deliveredNotifications]) { | 247 [notification_center_ deliveredNotifications]) { |
| 260 NSString* toast_id = | 248 NSString* toast_id = |
| 261 [toast.userInfo objectForKey:notification_constants::kNotificationId]; | 249 [toast.userInfo objectForKey:notification_constants::kNotificationId]; |
| 262 | 250 |
| 263 NSString* persistent_profile_id = [toast.userInfo | 251 NSString* persistent_profile_id = [toast.userInfo |
| 264 objectForKey:notification_constants::kNotificationProfileId]; | 252 objectForKey:notification_constants::kNotificationProfileId]; |
| 265 | 253 |
| 266 if ([toast_id isEqualToString:candidate_id] && | 254 if ([toast_id isEqualToString:candidate_id] && |
| 267 [persistent_profile_id isEqualToString:current_profile_id]) { | 255 [persistent_profile_id isEqualToString:current_profile_id]) { |
| 268 [notification_center_ removeDeliveredNotification:toast]; | 256 [notification_center_ removeDeliveredNotification:toast]; |
| 269 notification_removed = true; | 257 notification_removed = true; |
| 270 break; | 258 break; |
| 271 } | 259 } |
| 272 } | 260 } |
| 273 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 261 |
| 274 // If no banner existed with that ID try to see if there is an alert | 262 // If no banner existed with that ID try to see if there is an alert |
| 275 // in the xpc server. | 263 // in the xpc server. |
| 276 if (!notification_removed) { | 264 if (!notification_removed) { |
| 277 [alert_dispatcher_ closeNotificationWithId:candidate_id | 265 [alert_dispatcher_ closeNotificationWithId:candidate_id |
| 278 withProfileId:current_profile_id]; | 266 withProfileId:current_profile_id]; |
| 279 } | 267 } |
| 280 #endif // ENABLE_XPC_NOTIFICATIONS | |
| 281 } | 268 } |
| 282 | 269 |
| 283 void NotificationPlatformBridgeMac::GetDisplayed( | 270 void NotificationPlatformBridgeMac::GetDisplayed( |
| 284 const std::string& profile_id, | 271 const std::string& profile_id, |
| 285 bool incognito, | 272 bool incognito, |
| 286 const GetDisplayedNotificationsCallback& callback) const { | 273 const GetDisplayedNotificationsCallback& callback) const { |
| 287 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | |
| 288 [alert_dispatcher_ | 274 [alert_dispatcher_ |
| 289 getDisplayedAlertsForProfileId:base::SysUTF8ToNSString(profile_id) | 275 getDisplayedAlertsForProfileId:base::SysUTF8ToNSString(profile_id) |
| 290 incognito:incognito | 276 incognito:incognito |
| 291 notificationCenter:notification_center_ | 277 notificationCenter:notification_center_ |
| 292 callback:callback]; | 278 callback:callback]; |
| 293 | |
| 294 #else | |
| 295 | |
| 296 auto displayed_notifications = base::MakeUnique<std::set<std::string>>(); | |
| 297 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); | |
| 298 for (NSUserNotification* toast in | |
| 299 [notification_center_ deliveredNotifications]) { | |
| 300 NSString* toast_profile_id = [toast.userInfo | |
| 301 objectForKey:notification_constants::kNotificationProfileId]; | |
| 302 BOOL incognito_notification = [[toast.userInfo | |
| 303 objectForKey:notification_constants::kNotificationIncognito] boolValue]; | |
| 304 if ([toast_profile_id isEqualToString:current_profile_id] && | |
| 305 incognito == incognito_notification) { | |
| 306 displayed_notifications->insert(base::SysNSStringToUTF8([toast.userInfo | |
| 307 objectForKey:notification_constants::kNotificationId])); | |
| 308 } | |
| 309 } | |
| 310 content::BrowserThread::PostTask( | |
| 311 content::BrowserThread::UI, FROM_HERE, | |
| 312 base::Bind(callback, base::Passed(&displayed_notifications), | |
| 313 true /* supports_synchronization */)); | |
| 314 | |
| 315 #endif // ENABLE_XPC_NOTIFICATIONS | |
| 316 } | 279 } |
| 317 | 280 |
| 318 // static | 281 // static |
| 319 void NotificationPlatformBridgeMac::ProcessNotificationResponse( | 282 void NotificationPlatformBridgeMac::ProcessNotificationResponse( |
| 320 NSDictionary* response) { | 283 NSDictionary* response) { |
| 321 if (!NotificationPlatformBridgeMac::VerifyNotificationData(response)) | 284 if (!NotificationPlatformBridgeMac::VerifyNotificationData(response)) |
| 322 return; | 285 return; |
| 323 | 286 |
| 324 NSNumber* button_index = | 287 NSNumber* button_index = |
| 325 [response objectForKey:notification_constants::kNotificationButtonIndex]; | 288 [response objectForKey:notification_constants::kNotificationButtonIndex]; |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 base::scoped_nsobject<CrXPCMachPort> xpcPort( | 529 base::scoped_nsobject<CrXPCMachPort> xpcPort( |
| 567 [[CrXPCMachPort alloc] initWithMachSendRight:std::move(exceptionPort)]); | 530 [[CrXPCMachPort alloc] initWithMachSendRight:std::move(exceptionPort)]); |
| 568 [proxy setMachExceptionPort:xpcPort]; | 531 [proxy setMachExceptionPort:xpcPort]; |
| 569 setExceptionPort_ = YES; | 532 setExceptionPort_ = YES; |
| 570 } | 533 } |
| 571 | 534 |
| 572 return proxy; | 535 return proxy; |
| 573 } | 536 } |
| 574 | 537 |
| 575 @end | 538 @end |
| OLD | NEW |