| 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_ui_manager_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/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/mac/mac_util.h" | 10 #include "base/mac/mac_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/notifications/notification.h" | 14 #include "chrome/browser/notifications/notification.h" |
| 15 #include "chrome/browser/notifications/notification_display_service_factory.h" | 15 #include "chrome/browser/notifications/notification_display_service_factory.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 NSString* const kNotificationPersistentIdKey = @"notification_persistent_id"; | 48 NSString* const kNotificationPersistentIdKey = @"notification_persistent_id"; |
| 49 | 49 |
| 50 NSString* const kNotificationProfilePersistentIdKey = | 50 NSString* const kNotificationProfilePersistentIdKey = |
| 51 @"notification_profile_persistent_id"; | 51 @"notification_profile_persistent_id"; |
| 52 NSString* const kNotificationIncognitoKey = @"notification_incognito"; | 52 NSString* const kNotificationIncognitoKey = @"notification_incognito"; |
| 53 | 53 |
| 54 } // namespace | 54 } // namespace |
| 55 | 55 |
| 56 // static | 56 // static |
| 57 NotificationPlatformBridge* NotificationPlatformBridge::Create() { | 57 NotificationPlatformBridge* NotificationPlatformBridge::Create() { |
| 58 return new NotificationUIManagerMac( | 58 return new NotificationPlatformBridgeMac( |
| 59 [NSUserNotificationCenter defaultUserNotificationCenter]); | 59 [NSUserNotificationCenter defaultUserNotificationCenter]); |
| 60 } | 60 } |
| 61 | 61 |
| 62 // A Cocoa class that represents the delegate of NSUserNotificationCenter and | 62 // A Cocoa class that represents the delegate of NSUserNotificationCenter and |
| 63 // can forward commands to C++. | 63 // can forward commands to C++. |
| 64 @interface NotificationCenterDelegate | 64 @interface NotificationCenterDelegate |
| 65 : NSObject<NSUserNotificationCenterDelegate> { | 65 : NSObject<NSUserNotificationCenterDelegate> { |
| 66 } | 66 } |
| 67 @end | 67 @end |
| 68 | 68 |
| 69 // ///////////////////////////////////////////////////////////////////////////// | 69 // ///////////////////////////////////////////////////////////////////////////// |
| 70 | 70 |
| 71 NotificationUIManagerMac::NotificationUIManagerMac( | 71 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac( |
| 72 NSUserNotificationCenter* notification_center) | 72 NSUserNotificationCenter* notification_center) |
| 73 : delegate_([NotificationCenterDelegate alloc]), | 73 : delegate_([NotificationCenterDelegate alloc]), |
| 74 notification_center_(notification_center) { | 74 notification_center_(notification_center) { |
| 75 [notification_center_ setDelegate:delegate_.get()]; | 75 [notification_center_ setDelegate:delegate_.get()]; |
| 76 } | 76 } |
| 77 | 77 |
| 78 NotificationUIManagerMac::~NotificationUIManagerMac() { | 78 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { |
| 79 [notification_center_ setDelegate:nil]; | 79 [notification_center_ setDelegate:nil]; |
| 80 | 80 |
| 81 // TODO(miguelg) lift this restriction if possible. | 81 // TODO(miguelg) lift this restriction if possible. |
| 82 [notification_center_ removeAllDeliveredNotifications]; | 82 [notification_center_ removeAllDeliveredNotifications]; |
| 83 } | 83 } |
| 84 | 84 |
| 85 void NotificationUIManagerMac::Display(const std::string& notification_id, | 85 void NotificationPlatformBridgeMac::Display(const std::string& notification_id, |
| 86 const std::string& profile_id, | 86 const std::string& profile_id, |
| 87 bool incognito, | 87 bool incognito, |
| 88 const Notification& notification) { | 88 const Notification& notification) { |
| 89 base::scoped_nsobject<NSUserNotification> toast( | 89 base::scoped_nsobject<NSUserNotification> toast( |
| 90 [[NSUserNotification alloc] init]); | 90 [[NSUserNotification alloc] init]); |
| 91 [toast setTitle:base::SysUTF16ToNSString(notification.title())]; | 91 [toast setTitle:base::SysUTF16ToNSString(notification.title())]; |
| 92 [toast setSubtitle:base::SysUTF16ToNSString(notification.message())]; | 92 [toast setSubtitle:base::SysUTF16ToNSString(notification.message())]; |
| 93 | 93 |
| 94 // TODO(miguelg): try to elide the origin perhaps See NSString | 94 // TODO(miguelg): try to elide the origin perhaps See NSString |
| 95 // stringWithFormat. It seems that the informativeText font is constant. | 95 // stringWithFormat. It seems that the informativeText font is constant. |
| 96 NSString* informative_text = | 96 NSString* informative_text = |
| 97 notification.context_message().empty() | 97 notification.context_message().empty() |
| 98 ? base::SysUTF8ToNSString(notification.origin_url().spec()) | 98 ? base::SysUTF8ToNSString(notification.origin_url().spec()) |
| (...skipping 27 matching lines...) Expand all Loading... |
| 126 } else { | 126 } else { |
| 127 // Otherwise show the alternate menu, then show the developer actions and | 127 // Otherwise show the alternate menu, then show the developer actions and |
| 128 // finally the settings one. | 128 // finally the settings one. |
| 129 DCHECK( | 129 DCHECK( |
| 130 [toast respondsToSelector:@selector(_alwaysShowAlternateActionMenu)]); | 130 [toast respondsToSelector:@selector(_alwaysShowAlternateActionMenu)]); |
| 131 DCHECK( | 131 DCHECK( |
| 132 [toast respondsToSelector:@selector(_alternateActionButtonTitles)]); | 132 [toast respondsToSelector:@selector(_alternateActionButtonTitles)]); |
| 133 | 133 |
| 134 [toast setActionButtonTitle:l10n_util::GetNSString( | 134 [toast setActionButtonTitle:l10n_util::GetNSString( |
| 135 IDS_NOTIFICATION_BUTTON_OPTIONS)]; | 135 IDS_NOTIFICATION_BUTTON_OPTIONS)]; |
| 136 [toast setValue:@YES | 136 [toast setValue:@YES forKey:@"_alwaysShowAlternateActionMenu"]; |
| 137 forKey:@"_alwaysShowAlternateActionMenu"]; | |
| 138 | 137 |
| 139 NSMutableArray* buttons = [NSMutableArray arrayWithCapacity:3]; | 138 NSMutableArray* buttons = [NSMutableArray arrayWithCapacity:3]; |
| 140 for (const auto& action : notification.buttons()) | 139 for (const auto& action : notification.buttons()) |
| 141 [buttons addObject:base::SysUTF16ToNSString(action.title)]; | 140 [buttons addObject:base::SysUTF16ToNSString(action.title)]; |
| 142 [buttons | 141 [buttons |
| 143 addObject:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_SETTINGS)]; | 142 addObject:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_SETTINGS)]; |
| 144 | 143 |
| 145 [toast setValue:buttons forKey:@"_alternateActionButtonTitles"]; | 144 [toast setValue:buttons forKey:@"_alternateActionButtonTitles"]; |
| 146 } | 145 } |
| 147 } | 146 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 174 kNotificationOriginKey : | 173 kNotificationOriginKey : |
| 175 base::SysUTF8ToNSString(notification.origin_url().spec()), | 174 base::SysUTF8ToNSString(notification.origin_url().spec()), |
| 176 kNotificationPersistentIdKey : base::SysUTF8ToNSString(notification_id), | 175 kNotificationPersistentIdKey : base::SysUTF8ToNSString(notification_id), |
| 177 kNotificationProfilePersistentIdKey : base::SysUTF8ToNSString(profile_id), | 176 kNotificationProfilePersistentIdKey : base::SysUTF8ToNSString(profile_id), |
| 178 kNotificationIncognitoKey : [NSNumber numberWithBool:incognito] | 177 kNotificationIncognitoKey : [NSNumber numberWithBool:incognito] |
| 179 }; | 178 }; |
| 180 | 179 |
| 181 [notification_center_ deliverNotification:toast]; | 180 [notification_center_ deliverNotification:toast]; |
| 182 } | 181 } |
| 183 | 182 |
| 184 void NotificationUIManagerMac::Close(const std::string& profile_id, | 183 void NotificationPlatformBridgeMac::Close(const std::string& profile_id, |
| 185 const std::string& notification_id) { | 184 const std::string& notification_id) { |
| 186 NSString* candidate_id = base::SysUTF8ToNSString(notification_id); | 185 NSString* candidate_id = base::SysUTF8ToNSString(notification_id); |
| 187 | 186 |
| 188 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); | 187 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); |
| 189 for (NSUserNotification* toast in | 188 for (NSUserNotification* toast in |
| 190 [notification_center_ deliveredNotifications]) { | 189 [notification_center_ deliveredNotifications]) { |
| 191 NSString* toast_id = | 190 NSString* toast_id = |
| 192 [toast.userInfo objectForKey:kNotificationPersistentIdKey]; | 191 [toast.userInfo objectForKey:kNotificationPersistentIdKey]; |
| 193 | 192 |
| 194 NSString* persistent_profile_id = | 193 NSString* persistent_profile_id = |
| 195 [toast.userInfo objectForKey:kNotificationProfilePersistentIdKey]; | 194 [toast.userInfo objectForKey:kNotificationProfilePersistentIdKey]; |
| 196 | 195 |
| 197 if (toast_id == candidate_id && | 196 if (toast_id == candidate_id && |
| 198 persistent_profile_id == current_profile_id) { | 197 persistent_profile_id == current_profile_id) { |
| 199 [notification_center_ removeDeliveredNotification:toast]; | 198 [notification_center_ removeDeliveredNotification:toast]; |
| 200 } | 199 } |
| 201 } | 200 } |
| 202 } | 201 } |
| 203 | 202 |
| 204 bool NotificationUIManagerMac::GetDisplayed( | 203 bool NotificationPlatformBridgeMac::GetDisplayed( |
| 205 const std::string& profile_id, | 204 const std::string& profile_id, |
| 206 bool incognito, | 205 bool incognito, |
| 207 std::set<std::string>* notifications) const { | 206 std::set<std::string>* notifications) const { |
| 208 DCHECK(notifications); | 207 DCHECK(notifications); |
| 209 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); | 208 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); |
| 210 for (NSUserNotification* toast in | 209 for (NSUserNotification* toast in |
| 211 [notification_center_ deliveredNotifications]) { | 210 [notification_center_ deliveredNotifications]) { |
| 212 NSString* toast_profile_id = | 211 NSString* toast_profile_id = |
| 213 [toast.userInfo objectForKey:kNotificationProfilePersistentIdKey]; | 212 [toast.userInfo objectForKey:kNotificationProfilePersistentIdKey]; |
| 214 if (toast_profile_id == current_profile_id) { | 213 if (toast_profile_id == current_profile_id) { |
| 215 notifications->insert(base::SysNSStringToUTF8( | 214 notifications->insert(base::SysNSStringToUTF8( |
| 216 [toast.userInfo objectForKey:kNotificationPersistentIdKey])); | 215 [toast.userInfo objectForKey:kNotificationPersistentIdKey])); |
| 217 } | 216 } |
| 218 } | 217 } |
| 219 return true; | 218 return true; |
| 220 } | 219 } |
| 221 | 220 |
| 222 bool NotificationUIManagerMac::SupportsNotificationCenter() const { | 221 bool NotificationPlatformBridgeMac::SupportsNotificationCenter() const { |
| 223 return true; | 222 return true; |
| 224 } | 223 } |
| 225 | 224 |
| 226 // ///////////////////////////////////////////////////////////////////////////// | 225 // ///////////////////////////////////////////////////////////////////////////// |
| 227 | 226 |
| 228 @implementation NotificationCenterDelegate | 227 @implementation NotificationCenterDelegate |
| 229 - (void)userNotificationCenter:(NSUserNotificationCenter*)center | 228 - (void)userNotificationCenter:(NSUserNotificationCenter*)center |
| 230 didActivateNotification:(NSUserNotification*)notification { | 229 didActivateNotification:(NSUserNotification*)notification { |
| 231 std::string notificationOrigin = base::SysNSStringToUTF8( | 230 std::string notificationOrigin = base::SysNSStringToUTF8( |
| 232 [notification.userInfo objectForKey:kNotificationOriginKey]); | 231 [notification.userInfo objectForKey:kNotificationOriginKey]); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 persistentNotificationId.longLongValue, buttonIndex); | 285 persistentNotificationId.longLongValue, buttonIndex); |
| 287 } | 286 } |
| 288 | 287 |
| 289 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center | 288 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center |
| 290 shouldPresentNotification:(NSUserNotification*)nsNotification { | 289 shouldPresentNotification:(NSUserNotification*)nsNotification { |
| 291 // Always display notifications, regardless of whether the app is foreground. | 290 // Always display notifications, regardless of whether the app is foreground. |
| 292 return YES; | 291 return YES; |
| 293 } | 292 } |
| 294 | 293 |
| 295 @end | 294 @end |
| OLD | NEW |