Chromium Code Reviews| 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 #import "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 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); | 168 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); |
| 167 for (NSUserNotification* toast in | 169 for (NSUserNotification* toast in |
| 168 [notification_center_ deliveredNotifications]) { | 170 [notification_center_ deliveredNotifications]) { |
| 169 NSString* toast_profile_id = [toast.userInfo | 171 NSString* toast_profile_id = [toast.userInfo |
| 170 objectForKey:notification_builder::kNotificationProfileId]; | 172 objectForKey:notification_constants::kNotificationProfileId]; |
| 171 if (toast_profile_id == current_profile_id) { | 173 if (toast_profile_id == current_profile_id) { |
| 172 notifications->insert(base::SysNSStringToUTF8( | 174 notifications->insert(base::SysNSStringToUTF8([toast.userInfo |
| 173 [toast.userInfo objectForKey:notification_builder::kNotificationId])); | 175 objectForKey:notification_constants::kNotificationId])); |
| 174 } | 176 } |
| 175 } | 177 } |
| 176 return true; | 178 return true; |
| 177 } | 179 } |
| 178 | 180 |
| 179 bool NotificationPlatformBridgeMac::SupportsNotificationCenter() const { | 181 bool NotificationPlatformBridgeMac::SupportsNotificationCenter() const { |
| 180 return true; | 182 return true; |
| 181 } | 183 } |
| 182 | 184 |
| 183 // ///////////////////////////////////////////////////////////////////////////// | 185 // ///////////////////////////////////////////////////////////////////////////// |
| 184 | 186 |
| 185 @implementation NotificationCenterDelegate | 187 @implementation NotificationCenterDelegate |
| 186 - (void)userNotificationCenter:(NSUserNotificationCenter*)center | 188 - (void)userNotificationCenter:(NSUserNotificationCenter*)center |
| 187 didActivateNotification:(NSUserNotification*)notification { | 189 didActivateNotification:(NSUserNotification*)notification { |
| 188 std::string notificationOrigin = | 190 NSDictionary* response = |
| 189 base::SysNSStringToUTF8([notification.userInfo | 191 [NotificationResponseBuilder buildDictionary:notification]; |
|
Peter Beverloo
2016/06/16 17:02:43
Could be pedantic and add a whole lot of DCHECKS t
| |
| 190 objectForKey:notification_builder::kNotificationOrigin]); | 192 |
| 191 NSNumber* notificationId = [notification.userInfo | 193 NSNumber* buttonIndex = |
| 192 objectForKey:notification_builder::kNotificationId]; | 194 [response objectForKey:notification_constants::kNotificationButtonIndex]; |
| 193 NSString* profileId = [notification.userInfo | 195 NSNumber* operation = |
| 194 objectForKey:notification_builder::kNotificationProfileId]; | 196 [response objectForKey:notification_constants::kNotificationOperation]; |
| 195 NSNumber* isIncognito = [notification.userInfo | 197 |
| 196 objectForKey:notification_builder::kNotificationIncognito]; | 198 std::string notificationOrigin = base::SysNSStringToUTF8( |
| 199 [response objectForKey:notification_constants::kNotificationOrigin]); | |
|
Peter Beverloo
2016/06/16 17:02:43
nit: would be good to consistently use (or not use
Miguel Garcia
2016/06/17 17:53:08
so I am using notificationId because id is a semi
| |
| 200 NSString* notificationId = [notification.userInfo | |
| 201 objectForKey:notification_constants::kNotificationId]; | |
| 202 std::string notification_id = base::SysNSStringToUTF8(notificationId); | |
| 203 int64_t persistent_notification_id; | |
| 204 if (!base::StringToInt64(notification_id, &persistent_notification_id)) { | |
| 205 LOG(ERROR) << "Unable to convert notification ID: " << notification_id | |
| 206 << " to integer."; | |
| 207 return; | |
| 208 } | |
|
Peter Beverloo
2016/06/16 17:02:42
This method has a mixture of macStyle and unix_sty
Miguel Garcia
2016/06/17 17:53:08
Yeah good point.
| |
| 209 std::string profileId = base::SysNSStringToUTF8( | |
| 210 [response objectForKey:notification_constants::kNotificationProfileId]); | |
| 211 NSNumber* isIncognito = | |
| 212 [response objectForKey:notification_constants::kNotificationIncognito]; | |
| 197 | 213 |
| 198 GURL origin(notificationOrigin); | 214 GURL origin(notificationOrigin); |
| 199 | 215 |
| 200 // Initialize operation and button index for the case where the | |
| 201 // notification itself was clicked. | |
| 202 PlatformNotificationServiceImpl::NotificationOperation operation = | |
| 203 PlatformNotificationServiceImpl::NOTIFICATION_CLICK; | |
| 204 int buttonIndex = -1; | |
| 205 | |
| 206 // Determine whether the user clicked on a button, and if they did, whether it | |
| 207 // was a developer-provided button or the mandatory Settings button. | |
| 208 if (notification.activationType == | |
| 209 NSUserNotificationActivationTypeActionButtonClicked) { | |
| 210 NSArray* alternateButtons = @[]; | |
| 211 if ([notification | |
| 212 respondsToSelector:@selector(_alternateActionButtonTitles)]) { | |
| 213 alternateButtons = | |
| 214 [notification valueForKey:@"_alternateActionButtonTitles"]; | |
| 215 } | |
| 216 | |
| 217 bool multipleButtons = (alternateButtons.count > 0); | |
| 218 | |
| 219 // No developer actions, just the settings button. | |
| 220 if (!multipleButtons) { | |
| 221 operation = PlatformNotificationServiceImpl::NOTIFICATION_SETTINGS; | |
| 222 buttonIndex = -1; | |
| 223 } else { | |
| 224 // 0 based array containing. | |
| 225 // Button 1 | |
| 226 // Button 2 (optional) | |
| 227 // Settings | |
| 228 NSNumber* actionIndex = | |
| 229 [notification valueForKey:@"_alternateActionIndex"]; | |
| 230 operation = (actionIndex.unsignedLongValue == alternateButtons.count - 1) | |
| 231 ? PlatformNotificationServiceImpl::NOTIFICATION_SETTINGS | |
| 232 : PlatformNotificationServiceImpl::NOTIFICATION_CLICK; | |
| 233 buttonIndex = | |
| 234 (actionIndex.unsignedLongValue == alternateButtons.count - 1) | |
| 235 ? -1 | |
| 236 : actionIndex.intValue; | |
| 237 } | |
| 238 } | |
| 239 | |
| 240 PlatformNotificationServiceImpl::GetInstance() | 216 PlatformNotificationServiceImpl::GetInstance() |
| 241 ->ProcessPersistentNotificationOperation( | 217 ->ProcessPersistentNotificationOperation( |
| 242 operation, base::SysNSStringToUTF8(profileId), | 218 static_cast<PlatformNotificationServiceImpl::NotificationOperation>( |
| 243 [isIncognito boolValue], origin, notificationId.longLongValue, | 219 operation.intValue), |
| 244 buttonIndex); | 220 profileId, [isIncognito boolValue], origin, |
| 221 persistent_notification_id, buttonIndex.intValue); | |
| 245 } | 222 } |
| 246 | 223 |
| 247 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center | 224 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center |
| 248 shouldPresentNotification:(NSUserNotification*)nsNotification { | 225 shouldPresentNotification:(NSUserNotification*)nsNotification { |
| 249 // Always display notifications, regardless of whether the app is foreground. | 226 // Always display notifications, regardless of whether the app is foreground. |
| 250 return YES; | 227 return YES; |
| 251 } | 228 } |
| 252 | 229 |
| 253 @end | 230 @end |
| OLD | NEW |