OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/notifications/notification_response_builder_mac
.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 // Make sure this Obj-C enum is kept in sync with the |
| 13 // PlatformNotificationServiceImpl NotificationOperation enum. |
| 14 // The latter cannot be reused because the XPC service is not aware of |
| 15 // PlatformNotificationCenter. |
| 16 enum NotificationOperation { |
| 17 NOTIFICATION_CLICK = 0, |
| 18 NOTIFICATION_CLOSE = 1, |
| 19 NOTIFICATION_SETTINGS = 2 |
| 20 }; |
| 21 } // namespace |
| 22 |
| 23 @implementation NotificationResponseBuilder |
| 24 |
| 25 + (NSDictionary*)buildDictionary:(NSUserNotification*)notification { |
| 26 NSString* origin = |
| 27 [[notification userInfo] |
| 28 objectForKey:notification_constants::kNotificationOrigin] |
| 29 ? [[notification userInfo] |
| 30 objectForKey:notification_constants::kNotificationOrigin] |
| 31 : @""; |
| 32 DCHECK([[notification userInfo] |
| 33 objectForKey:notification_constants::kNotificationId]); |
| 34 NSString* notificationId = [[notification userInfo] |
| 35 objectForKey:notification_constants::kNotificationId]; |
| 36 |
| 37 DCHECK([[notification userInfo] |
| 38 objectForKey:notification_constants::kNotificationProfileId]); |
| 39 NSString* profileId = [[notification userInfo] |
| 40 objectForKey:notification_constants::kNotificationProfileId]; |
| 41 |
| 42 DCHECK([[notification userInfo] |
| 43 objectForKey:notification_constants::kNotificationIncognito]); |
| 44 NSNumber* incognito = [[notification userInfo] |
| 45 objectForKey:notification_constants::kNotificationIncognito]; |
| 46 |
| 47 // Initialize operation and button index for the case where the |
| 48 // notification itself was clicked. |
| 49 NotificationOperation operation = NOTIFICATION_CLICK; |
| 50 int buttonIndex = -1; |
| 51 |
| 52 // Determine whether the user clicked on a button, and if they did, whether it |
| 53 // was a developer-provided button or the mandatory Settings button. |
| 54 if (notification.activationType == |
| 55 NSUserNotificationActivationTypeActionButtonClicked) { |
| 56 NSArray* alternateButtons = @[]; |
| 57 if ([notification |
| 58 respondsToSelector:@selector(_alternateActionButtonTitles)]) { |
| 59 alternateButtons = |
| 60 [notification valueForKey:@"_alternateActionButtonTitles"]; |
| 61 } |
| 62 |
| 63 bool multipleButtons = (alternateButtons.count > 0); |
| 64 |
| 65 // No developer actions, just the settings button. |
| 66 if (!multipleButtons) { |
| 67 operation = NOTIFICATION_SETTINGS; |
| 68 buttonIndex = -1; |
| 69 } else { |
| 70 // 0 based array containing. |
| 71 // Button 1 |
| 72 // Button 2 (optional) |
| 73 // Settings |
| 74 NSNumber* actionIndex = |
| 75 [notification valueForKey:@"_alternateActionIndex"]; |
| 76 operation = (actionIndex.unsignedLongValue == alternateButtons.count - 1) |
| 77 ? NOTIFICATION_SETTINGS |
| 78 : NOTIFICATION_CLICK; |
| 79 buttonIndex = |
| 80 (actionIndex.unsignedLongValue == alternateButtons.count - 1) |
| 81 ? -1 |
| 82 : actionIndex.intValue; |
| 83 } |
| 84 } |
| 85 |
| 86 return @{ |
| 87 notification_constants::kNotificationOrigin : origin, |
| 88 notification_constants::kNotificationId : notificationId, |
| 89 notification_constants::kNotificationProfileId : profileId, |
| 90 notification_constants::kNotificationIncognito : incognito, |
| 91 notification_constants::kNotificationOperation : |
| 92 [NSNumber numberWithInt:operation], |
| 93 notification_constants:: |
| 94 kNotificationButtonIndex : [NSNumber numberWithInt:buttonIndex], |
| 95 }; |
| 96 } |
| 97 |
| 98 @end |
OLD | NEW |