| 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" |
| 11 #include "base/mac/bundle_locations.h" | 11 #include "base/mac/bundle_locations.h" |
| 12 #include "base/mac/foundation_util.h" | 12 #include "base/mac/foundation_util.h" |
| 13 #include "base/mac/mac_util.h" | 13 #include "base/mac/mac_util.h" |
| 14 #include "base/mac/scoped_nsobject.h" | 14 #include "base/mac/scoped_nsobject.h" |
| 15 #include "base/strings/nullable_string16.h" | 15 #include "base/strings/nullable_string16.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
| 18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/notifications/alert_dispatcher.h" |
| 19 #include "chrome/browser/notifications/native_notification_display_service.h" | 20 #include "chrome/browser/notifications/native_notification_display_service.h" |
| 20 #include "chrome/browser/notifications/notification.h" | 21 #include "chrome/browser/notifications/notification.h" |
| 21 #include "chrome/browser/notifications/notification_common.h" | 22 #include "chrome/browser/notifications/notification_common.h" |
| 22 #include "chrome/browser/notifications/notification_display_service_factory.h" | 23 #include "chrome/browser/notifications/notification_display_service_factory.h" |
| 23 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 24 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
| 24 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 25 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/browser/profiles/profile_manager.h" | 27 #include "chrome/browser/profiles/profile_manager.h" |
| 27 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" | 28 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" |
| 28 #import "chrome/browser/ui/cocoa/notifications/notification_delivery.h" | 29 #import "chrome/browser/ui/cocoa/notifications/notification_delivery.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 ProfileManager* profileManager = g_browser_process->profile_manager(); | 94 ProfileManager* profileManager = g_browser_process->profile_manager(); |
| 94 DCHECK(profileManager); | 95 DCHECK(profileManager); |
| 95 | 96 |
| 96 profileManager->LoadProfile( | 97 profileManager->LoadProfile( |
| 97 profile_id, incognito, base::Bind(&ProfileLoadedCallback, operation, type, | 98 profile_id, incognito, base::Bind(&ProfileLoadedCallback, operation, type, |
| 98 origin, notification_id, button_index)); | 99 origin, notification_id, button_index)); |
| 99 } | 100 } |
| 100 | 101 |
| 101 } // namespace | 102 } // namespace |
| 102 | 103 |
| 103 // static | |
| 104 NotificationPlatformBridge* NotificationPlatformBridge::Create() { | |
| 105 return new NotificationPlatformBridgeMac( | |
| 106 [NSUserNotificationCenter defaultUserNotificationCenter]); | |
| 107 } | |
| 108 | |
| 109 // A Cocoa class that represents the delegate of NSUserNotificationCenter and | 104 // A Cocoa class that represents the delegate of NSUserNotificationCenter and |
| 110 // can forward commands to C++. | 105 // can forward commands to C++. |
| 111 @interface NotificationCenterDelegate | 106 @interface NotificationCenterDelegate |
| 112 : NSObject<NSUserNotificationCenterDelegate> { | 107 : NSObject<NSUserNotificationCenterDelegate> { |
| 113 } | 108 } |
| 114 @end | 109 @end |
| 115 | 110 |
| 116 // Interface to communicate with the Alert XPC service. | 111 // Interface to communicate with the Alert XPC service. |
| 117 @interface NotificationRemoteDispatcher : NSObject | 112 @interface AlertDispatcherImpl : NSObject<AlertDispatcher> |
| 113 |
| 114 - (instancetype)init; |
| 118 | 115 |
| 119 // Deliver a notification to the XPC service to be displayed as an alert. | 116 // Deliver a notification to the XPC service to be displayed as an alert. |
| 120 - (void)dispatchNotification:(NSDictionary*)data; | 117 - (void)dispatchNotification:(NSDictionary*)data; |
| 121 | 118 |
| 122 // Close a notification for a given |notificationId| and |profileId|. | 119 // Close a notification for a given |notificationId| and |profileId|. |
| 123 - (void)closeNotificationWithId:(NSString*)notificationId | 120 - (void)closeNotificationWithId:(NSString*)notificationId |
| 124 withProfileId:(NSString*)profileId; | 121 withProfileId:(NSString*)profileId; |
| 125 | 122 |
| 126 // Close all notifications. | 123 // Close all notifications. |
| 127 - (void)closeAllNotifications; | 124 - (void)closeAllNotifications; |
| 128 | 125 |
| 129 @end | 126 @end |
| 130 | 127 |
| 131 // ///////////////////////////////////////////////////////////////////////////// | 128 // ///////////////////////////////////////////////////////////////////////////// |
| 132 | |
| 133 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac( | 129 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac( |
| 134 NSUserNotificationCenter* notification_center) | 130 NSUserNotificationCenter* notification_center, |
| 131 id<AlertDispatcher> alert_dispatcher) |
| 135 : delegate_([NotificationCenterDelegate alloc]), | 132 : delegate_([NotificationCenterDelegate alloc]), |
| 136 notification_center_(notification_center), | 133 notification_center_(notification_center), |
| 137 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 134 alert_dispatcher_(alert_dispatcher) { |
| 138 notification_remote_dispatcher_( | |
| 139 [[NotificationRemoteDispatcher alloc] init]) | |
| 140 #else | |
| 141 notification_remote_dispatcher_(nullptr) | |
| 142 #endif // ENABLE_XPC_NOTIFICATIONS | |
| 143 { | |
| 144 [notification_center_ setDelegate:delegate_.get()]; | 135 [notification_center_ setDelegate:delegate_.get()]; |
| 145 } | 136 } |
| 146 | 137 |
| 147 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { | 138 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { |
| 148 [notification_center_ setDelegate:nil]; | 139 [notification_center_ setDelegate:nil]; |
| 149 | 140 |
| 150 // TODO(miguelg) do not remove banners if possible. | 141 // TODO(miguelg) do not remove banners if possible. |
| 151 [notification_center_ removeAllDeliveredNotifications]; | 142 [notification_center_ removeAllDeliveredNotifications]; |
| 152 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 143 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
| 153 [notification_remote_dispatcher_ closeAllNotifications]; | 144 [alert_dispatcher_ closeAllNotifications]; |
| 154 #endif // BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 145 #endif // BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
| 155 } | 146 } |
| 156 | 147 |
| 148 // static |
| 149 NotificationPlatformBridge* NotificationPlatformBridge::Create() { |
| 150 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
| 151 return new NotificationPlatformBridgeMac( |
| 152 [NSUserNotificationCenter defaultUserNotificationCenter], |
| 153 [[AlertDispatcherImpl alloc] init]); |
| 154 #else |
| 155 return new NotificationPlatformBridgeMac( |
| 156 [NSUserNotificationCenter defaultUserNotificationCenter], nil); |
| 157 #endif // ENABLE_XPC_NOTIFICATIONS |
| 158 } |
| 159 |
| 157 void NotificationPlatformBridgeMac::Display( | 160 void NotificationPlatformBridgeMac::Display( |
| 158 NotificationCommon::Type notification_type, | 161 NotificationCommon::Type notification_type, |
| 159 const std::string& notification_id, | 162 const std::string& notification_id, |
| 160 const std::string& profile_id, | 163 const std::string& profile_id, |
| 161 bool incognito, | 164 bool incognito, |
| 162 const Notification& notification) { | 165 const Notification& notification) { |
| 163 base::scoped_nsobject<NotificationBuilder> builder( | 166 base::scoped_nsobject<NotificationBuilder> builder( |
| 164 [[NotificationBuilder alloc] | 167 [[NotificationBuilder alloc] |
| 165 initWithCloseLabel:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_CLOSE) | 168 initWithCloseLabel:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_CLOSE) |
| 166 optionsLabel:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_OPTIONS) | 169 optionsLabel:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_OPTIONS) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 [builder setProfileId:base::SysUTF8ToNSString(profile_id)]; | 225 [builder setProfileId:base::SysUTF8ToNSString(profile_id)]; |
| 223 [builder setIncognito:incognito]; | 226 [builder setIncognito:incognito]; |
| 224 [builder setNotificationType:[NSNumber numberWithInteger:notification_type]]; | 227 [builder setNotificationType:[NSNumber numberWithInteger:notification_type]]; |
| 225 | 228 |
| 226 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 229 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
| 227 // Send persistent notifications to the XPC service so they | 230 // Send persistent notifications to the XPC service so they |
| 228 // can be displayed as alerts. Chrome itself can only display | 231 // can be displayed as alerts. Chrome itself can only display |
| 229 // banners. | 232 // banners. |
| 230 if (notification.never_timeout()) { | 233 if (notification.never_timeout()) { |
| 231 NSDictionary* dict = [builder buildDictionary]; | 234 NSDictionary* dict = [builder buildDictionary]; |
| 232 [notification_remote_dispatcher_ dispatchNotification:dict]; | 235 [alert_dispatcher_ dispatchNotification:dict]; |
| 233 } else { | 236 } else { |
| 234 NSUserNotification* toast = [builder buildUserNotification]; | 237 NSUserNotification* toast = [builder buildUserNotification]; |
| 235 [notification_center_ deliverNotification:toast]; | 238 [notification_center_ deliverNotification:toast]; |
| 236 } | 239 } |
| 237 #else | 240 #else |
| 238 NSUserNotification* toast = [builder buildUserNotification]; | 241 NSUserNotification* toast = [builder buildUserNotification]; |
| 239 [notification_center_ deliverNotification:toast]; | 242 [notification_center_ deliverNotification:toast]; |
| 240 #endif // ENABLE_XPC_NOTIFICATIONS | 243 #endif // ENABLE_XPC_NOTIFICATIONS |
| 241 } | 244 } |
| 242 | 245 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 258 [persistent_profile_id isEqualToString:current_profile_id]) { | 261 [persistent_profile_id isEqualToString:current_profile_id]) { |
| 259 [notification_center_ removeDeliveredNotification:toast]; | 262 [notification_center_ removeDeliveredNotification:toast]; |
| 260 notification_removed = true; | 263 notification_removed = true; |
| 261 break; | 264 break; |
| 262 } | 265 } |
| 263 } | 266 } |
| 264 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 267 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
| 265 // If no banner existed with that ID try to see if there is an alert | 268 // If no banner existed with that ID try to see if there is an alert |
| 266 // in the xpc server. | 269 // in the xpc server. |
| 267 if (!notification_removed) { | 270 if (!notification_removed) { |
| 268 [notification_remote_dispatcher_ | 271 [alert_dispatcher_ closeNotificationWithId:candidate_id |
| 269 closeNotificationWithId:candidate_id | 272 withProfileId:current_profile_id]; |
| 270 withProfileId:current_profile_id]; | |
| 271 } | 273 } |
| 272 #endif // ENABLE_XPC_NOTIFICATIONS | 274 #endif // ENABLE_XPC_NOTIFICATIONS |
| 273 } | 275 } |
| 274 | 276 |
| 275 bool NotificationPlatformBridgeMac::GetDisplayed( | 277 bool NotificationPlatformBridgeMac::GetDisplayed( |
| 276 const std::string& profile_id, | 278 const std::string& profile_id, |
| 277 bool incognito, | 279 bool incognito, |
| 278 std::set<std::string>* notifications) const { | 280 std::set<std::string>* notifications) const { |
| 279 DCHECK(notifications); | 281 DCHECK(notifications); |
| 280 | 282 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 } | 419 } |
| 418 | 420 |
| 419 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center | 421 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center |
| 420 shouldPresentNotification:(NSUserNotification*)nsNotification { | 422 shouldPresentNotification:(NSUserNotification*)nsNotification { |
| 421 // Always display notifications, regardless of whether the app is foreground. | 423 // Always display notifications, regardless of whether the app is foreground. |
| 422 return YES; | 424 return YES; |
| 423 } | 425 } |
| 424 | 426 |
| 425 @end | 427 @end |
| 426 | 428 |
| 427 @implementation NotificationRemoteDispatcher { | 429 @implementation AlertDispatcherImpl { |
| 428 // The connection to the XPC server in charge of delivering alerts. | 430 // The connection to the XPC server in charge of delivering alerts. |
| 429 base::scoped_nsobject<NSXPCConnection> xpcConnection_; | 431 base::scoped_nsobject<NSXPCConnection> xpcConnection_; |
| 430 } | 432 } |
| 431 | 433 |
| 432 - (instancetype)init { | 434 - (instancetype)init { |
| 433 if ((self = [super init])) { | 435 if ((self = [super init])) { |
| 434 xpcConnection_.reset([[NSXPCConnection alloc] | 436 xpcConnection_.reset([[NSXPCConnection alloc] |
| 435 initWithServiceName: | 437 initWithServiceName: |
| 436 [NSString | 438 [NSString |
| 437 stringWithFormat:notification_constants::kAlertXPCServiceName, | 439 stringWithFormat:notification_constants::kAlertXPCServiceName, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 [[xpcConnection_ remoteObjectProxy] closeAllNotifications]; | 477 [[xpcConnection_ remoteObjectProxy] closeAllNotifications]; |
| 476 } | 478 } |
| 477 | 479 |
| 478 // NotificationReply implementation | 480 // NotificationReply implementation |
| 479 - (void)notificationClick:(NSDictionary*)notificationResponseData { | 481 - (void)notificationClick:(NSDictionary*)notificationResponseData { |
| 480 NotificationPlatformBridgeMac::ProcessNotificationResponse( | 482 NotificationPlatformBridgeMac::ProcessNotificationResponse( |
| 481 notificationResponseData); | 483 notificationResponseData); |
| 482 } | 484 } |
| 483 | 485 |
| 484 @end | 486 @end |
| OLD | NEW |