| 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/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" |
| 16 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 16 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
| 17 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 17 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/profiles/profile_manager.h" | 19 #include "chrome/browser/profiles/profile_manager.h" |
| 20 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" | 20 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" |
| 21 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" |
| 22 #import "chrome/browser/ui/cocoa/notifications/notification_response_builder_mac
.h" |
| 21 #include "chrome/grit/generated_resources.h" | 23 #include "chrome/grit/generated_resources.h" |
| 22 #include "components/url_formatter/elide_url.h" | 24 #include "components/url_formatter/elide_url.h" |
| 23 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onConstants.h" | 25 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onConstants.h" |
| 24 #include "ui/base/l10n/l10n_util_mac.h" | 26 #include "ui/base/l10n/l10n_util_mac.h" |
| 25 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 26 #include "url/origin.h" | 28 #include "url/origin.h" |
| 27 | 29 |
| 28 @class NSUserNotification; | 30 @class NSUserNotification; |
| 29 @class NSUserNotificationCenter; | 31 @class NSUserNotificationCenter; |
| 30 | 32 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 } | 141 } |
| 140 | 142 |
| 141 void NotificationPlatformBridgeMac::Close(const std::string& profile_id, | 143 void NotificationPlatformBridgeMac::Close(const std::string& profile_id, |
| 142 const std::string& notification_id) { | 144 const std::string& notification_id) { |
| 143 NSString* candidate_id = base::SysUTF8ToNSString(notification_id); | 145 NSString* candidate_id = base::SysUTF8ToNSString(notification_id); |
| 144 | 146 |
| 145 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); | 147 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); |
| 146 for (NSUserNotification* toast in | 148 for (NSUserNotification* toast in |
| 147 [notification_center_ deliveredNotifications]) { | 149 [notification_center_ deliveredNotifications]) { |
| 148 NSString* toast_id = | 150 NSString* toast_id = |
| 149 [toast.userInfo objectForKey:notification_builder::kNotificationId]; | 151 [toast.userInfo objectForKey:notification_constants::kNotificationId]; |
| 150 | 152 |
| 151 NSString* persistent_profile_id = [toast.userInfo | 153 NSString* persistent_profile_id = [toast.userInfo |
| 152 objectForKey:notification_builder::kNotificationProfileId]; | 154 objectForKey:notification_constants::kNotificationProfileId]; |
| 153 | 155 |
| 154 if (toast_id == candidate_id && | 156 if (toast_id == candidate_id && |
| 155 persistent_profile_id == current_profile_id) { | 157 persistent_profile_id == current_profile_id) { |
| 156 [notification_center_ removeDeliveredNotification:toast]; | 158 [notification_center_ removeDeliveredNotification:toast]; |
| 157 } | 159 } |
| 158 } | 160 } |
| 159 } | 161 } |
| 160 | 162 |
| 161 bool NotificationPlatformBridgeMac::GetDisplayed( | 163 bool NotificationPlatformBridgeMac::GetDisplayed( |
| 162 const std::string& profile_id, | 164 const std::string& profile_id, |
| 163 bool incognito, | 165 bool incognito, |
| 164 std::set<std::string>* notifications) const { | 166 std::set<std::string>* notifications) const { |
| 165 DCHECK(notifications); | 167 DCHECK(notifications); |
| 166 | 168 |
| 167 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); | 169 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); |
| 168 for (NSUserNotification* toast in | 170 for (NSUserNotification* toast in |
| 169 [notification_center_ deliveredNotifications]) { | 171 [notification_center_ deliveredNotifications]) { |
| 170 NSString* toast_profile_id = [toast.userInfo | 172 NSString* toast_profile_id = [toast.userInfo |
| 171 objectForKey:notification_builder::kNotificationProfileId]; | 173 objectForKey:notification_constants::kNotificationProfileId]; |
| 172 if (toast_profile_id == current_profile_id) { | 174 if (toast_profile_id == current_profile_id) { |
| 173 notifications->insert(base::SysNSStringToUTF8( | 175 notifications->insert(base::SysNSStringToUTF8([toast.userInfo |
| 174 [toast.userInfo objectForKey:notification_builder::kNotificationId])); | 176 objectForKey:notification_constants::kNotificationId])); |
| 175 } | 177 } |
| 176 } | 178 } |
| 177 return true; | 179 return true; |
| 178 } | 180 } |
| 179 | 181 |
| 180 bool NotificationPlatformBridgeMac::SupportsNotificationCenter() const { | 182 bool NotificationPlatformBridgeMac::SupportsNotificationCenter() const { |
| 181 return true; | 183 return true; |
| 182 } | 184 } |
| 183 | 185 |
| 184 // ///////////////////////////////////////////////////////////////////////////// | 186 // ///////////////////////////////////////////////////////////////////////////// |
| 185 | 187 |
| 186 @implementation NotificationCenterDelegate | 188 @implementation NotificationCenterDelegate |
| 187 - (void)userNotificationCenter:(NSUserNotificationCenter*)center | 189 - (void)userNotificationCenter:(NSUserNotificationCenter*)center |
| 188 didActivateNotification:(NSUserNotification*)notification { | 190 didActivateNotification:(NSUserNotification*)notification { |
| 189 std::string notificationOrigin = | 191 NSDictionary* response = |
| 190 base::SysNSStringToUTF8([notification.userInfo | 192 [NotificationResponseBuilder buildDictionary:notification]; |
| 191 objectForKey:notification_builder::kNotificationOrigin]); | 193 |
| 194 NSNumber* buttonIndex = |
| 195 [response objectForKey:notification_constants::kNotificationButtonIndex]; |
| 196 NSNumber* operation = |
| 197 [response objectForKey:notification_constants::kNotificationOperation]; |
| 198 |
| 199 std::string notificationOrigin = base::SysNSStringToUTF8( |
| 200 [response objectForKey:notification_constants::kNotificationOrigin]); |
| 192 NSString* notificationId = [notification.userInfo | 201 NSString* notificationId = [notification.userInfo |
| 193 objectForKey:notification_builder::kNotificationId]; | 202 objectForKey:notification_constants::kNotificationId]; |
| 194 std::string persistentNotificationId = | 203 std::string persistentNotificationId = |
| 195 base::SysNSStringToUTF8(notificationId); | 204 base::SysNSStringToUTF8(notificationId); |
| 196 int64_t persistentId; | 205 int64_t persistentId; |
| 197 if (!base::StringToInt64(persistentNotificationId, &persistentId)) { | 206 if (!base::StringToInt64(persistentNotificationId, &persistentId)) { |
| 198 LOG(ERROR) << "Unable to convert notification ID: " | 207 LOG(ERROR) << "Unable to convert notification ID: " |
| 199 << persistentNotificationId << " to integer."; | 208 << persistentNotificationId << " to integer."; |
| 200 return; | 209 return; |
| 201 } | 210 } |
| 202 | 211 std::string profileId = base::SysNSStringToUTF8( |
| 203 NSString* profileId = [notification.userInfo | 212 [response objectForKey:notification_constants::kNotificationProfileId]); |
| 204 objectForKey:notification_builder::kNotificationProfileId]; | 213 NSNumber* isIncognito = |
| 205 NSNumber* isIncognito = [notification.userInfo | 214 [response objectForKey:notification_constants::kNotificationIncognito]; |
| 206 objectForKey:notification_builder::kNotificationIncognito]; | |
| 207 | 215 |
| 208 GURL origin(notificationOrigin); | 216 GURL origin(notificationOrigin); |
| 209 | 217 |
| 210 // Initialize operation and button index for the case where the | |
| 211 // notification itself was clicked. | |
| 212 PlatformNotificationServiceImpl::NotificationOperation operation = | |
| 213 PlatformNotificationServiceImpl::NOTIFICATION_CLICK; | |
| 214 int buttonIndex = -1; | |
| 215 | |
| 216 // Determine whether the user clicked on a button, and if they did, whether it | |
| 217 // was a developer-provided button or the mandatory Settings button. | |
| 218 if (notification.activationType == | |
| 219 NSUserNotificationActivationTypeActionButtonClicked) { | |
| 220 NSArray* alternateButtons = @[]; | |
| 221 if ([notification | |
| 222 respondsToSelector:@selector(_alternateActionButtonTitles)]) { | |
| 223 alternateButtons = | |
| 224 [notification valueForKey:@"_alternateActionButtonTitles"]; | |
| 225 } | |
| 226 | |
| 227 bool multipleButtons = (alternateButtons.count > 0); | |
| 228 | |
| 229 // No developer actions, just the settings button. | |
| 230 if (!multipleButtons) { | |
| 231 operation = PlatformNotificationServiceImpl::NOTIFICATION_SETTINGS; | |
| 232 buttonIndex = -1; | |
| 233 } else { | |
| 234 // 0 based array containing. | |
| 235 // Button 1 | |
| 236 // Button 2 (optional) | |
| 237 // Settings | |
| 238 NSNumber* actionIndex = | |
| 239 [notification valueForKey:@"_alternateActionIndex"]; | |
| 240 operation = (actionIndex.unsignedLongValue == alternateButtons.count - 1) | |
| 241 ? PlatformNotificationServiceImpl::NOTIFICATION_SETTINGS | |
| 242 : PlatformNotificationServiceImpl::NOTIFICATION_CLICK; | |
| 243 buttonIndex = | |
| 244 (actionIndex.unsignedLongValue == alternateButtons.count - 1) | |
| 245 ? -1 | |
| 246 : actionIndex.intValue; | |
| 247 } | |
| 248 } | |
| 249 | |
| 250 PlatformNotificationServiceImpl::GetInstance() | 218 PlatformNotificationServiceImpl::GetInstance() |
| 251 ->ProcessPersistentNotificationOperation( | 219 ->ProcessPersistentNotificationOperation( |
| 252 operation, base::SysNSStringToUTF8(profileId), | 220 static_cast<PlatformNotificationServiceImpl::NotificationOperation>( |
| 253 [isIncognito boolValue], origin, persistentId, buttonIndex); | 221 operation.intValue), |
| 222 profileId, [isIncognito boolValue], origin, persistentId, |
| 223 buttonIndex.intValue); |
| 254 } | 224 } |
| 255 | 225 |
| 256 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center | 226 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center |
| 257 shouldPresentNotification:(NSUserNotification*)nsNotification { | 227 shouldPresentNotification:(NSUserNotification*)nsNotification { |
| 258 // Always display notifications, regardless of whether the app is foreground. | 228 // Always display notifications, regardless of whether the app is foreground. |
| 259 return YES; | 229 return YES; |
| 260 } | 230 } |
| 261 | 231 |
| 262 @end | 232 @end |
| OLD | NEW |