| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #import "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" | 5 #import "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" |
| 6 | 6 |
| 7 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
| 8 | 8 |
| 9 #include "base/mac/mac_util.h" | 9 #include "base/mac/mac_util.h" |
| 10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
| 11 #include "chrome/grit/generated_resources.h" | 11 #include "chrome/grit/generated_resources.h" |
| 12 #import "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" |
| 12 #include "ui/base/l10n/l10n_util_mac.h" | 13 #include "ui/base/l10n/l10n_util_mac.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // Internal builder constants representing the different notification fields | 17 // Internal builder constants representing the different notification fields |
| 17 // They don't need to be exposed outside the builder. | 18 // They don't need to be exposed outside the builder. |
| 18 | 19 |
| 19 NSString* const kNotificationTitle = @"title"; | 20 NSString* const kNotificationTitle = @"title"; |
| 20 NSString* const kNotificationSubTitle = @"subtitle"; | 21 NSString* const kNotificationSubTitle = @"subtitle"; |
| 21 NSString* const kNotificationInformativeText = @"informativeText"; | 22 NSString* const kNotificationInformativeText = @"informativeText"; |
| 22 NSString* const kNotificationImage = @"icon"; | 23 NSString* const kNotificationImage = @"icon"; |
| 23 NSString* const kNotificationButtonOne = @"buttonOne"; | 24 NSString* const kNotificationButtonOne = @"buttonOne"; |
| 24 NSString* const kNotificationButtonTwo = @"buttonTwo"; | 25 NSString* const kNotificationButtonTwo = @"buttonTwo"; |
| 25 NSString* const kNotificationTag = @"tag"; | 26 NSString* const kNotificationTag = @"tag"; |
| 26 NSString* const kNotificationCloseButtonTag = @"closeButton"; | 27 NSString* const kNotificationCloseButtonTag = @"closeButton"; |
| 27 NSString* const kNotificationOptionsButtonTag = @"optionsButton"; | 28 NSString* const kNotificationOptionsButtonTag = @"optionsButton"; |
| 28 NSString* const kNotificationSettingsButtonTag = @"settingsButton"; | 29 NSString* const kNotificationSettingsButtonTag = @"settingsButton"; |
| 29 } // namespace | 30 } // namespace |
| 30 | 31 |
| 31 namespace notification_builder { | |
| 32 | |
| 33 // Exposed constants to include user related data in the notification. | |
| 34 NSString* const kNotificationOrigin = @"notificationOrigin"; | |
| 35 NSString* const kNotificationId = @"notificationId"; | |
| 36 NSString* const kNotificationProfileId = @"notificationProfileId"; | |
| 37 NSString* const kNotificationIncognito = @"notificationIncognito"; | |
| 38 | |
| 39 } // namespace notification_builder | |
| 40 | |
| 41 @implementation NotificationBuilder { | 32 @implementation NotificationBuilder { |
| 42 base::scoped_nsobject<NSMutableDictionary> notificationData_; | 33 base::scoped_nsobject<NSMutableDictionary> notificationData_; |
| 43 } | 34 } |
| 44 | 35 |
| 45 - (instancetype)init { | 36 - (instancetype)init { |
| 46 if ((self = [super init])) { | 37 if ((self = [super init])) { |
| 47 notificationData_.reset([[NSMutableDictionary alloc] init]); | 38 notificationData_.reset([[NSMutableDictionary alloc] init]); |
| 48 [notificationData_ | 39 [notificationData_ |
| 49 setObject:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_CLOSE) | 40 setObject:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_CLOSE) |
| 50 forKey:kNotificationCloseButtonTag]; | 41 forKey:kNotificationCloseButtonTag]; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 } | 87 } |
| 97 | 88 |
| 98 - (void)setTag:(NSString*)tag { | 89 - (void)setTag:(NSString*)tag { |
| 99 if (tag.length) | 90 if (tag.length) |
| 100 [notificationData_ setObject:tag forKey:kNotificationTag]; | 91 [notificationData_ setObject:tag forKey:kNotificationTag]; |
| 101 } | 92 } |
| 102 | 93 |
| 103 - (void)setOrigin:(NSString*)origin { | 94 - (void)setOrigin:(NSString*)origin { |
| 104 if (origin.length) | 95 if (origin.length) |
| 105 [notificationData_ setObject:origin | 96 [notificationData_ setObject:origin |
| 106 forKey:notification_builder::kNotificationOrigin]; | 97 forKey:notification_constants::kNotificationOrigin]; |
| 107 } | 98 } |
| 108 | 99 |
| 109 - (void)setNotificationId:(NSString*)notificationId { | 100 - (void)setNotificationId:(NSString*)notificationId { |
| 110 DCHECK(notificationId.length); | 101 DCHECK(notificationId.length); |
| 111 [notificationData_ setObject:notificationId | 102 [notificationData_ setObject:notificationId |
| 112 forKey:notification_builder::kNotificationId]; | 103 forKey:notification_constants::kNotificationId]; |
| 113 } | 104 } |
| 114 | 105 |
| 115 - (void)setProfileId:(NSString*)profileId { | 106 - (void)setProfileId:(NSString*)profileId { |
| 116 DCHECK(profileId.length); | 107 DCHECK(profileId.length); |
| 117 [notificationData_ setObject:profileId | 108 [notificationData_ setObject:profileId |
| 118 forKey:notification_builder::kNotificationProfileId]; | 109 forKey:notification_constants::kNotificationProfileId]; |
| 119 } | 110 } |
| 120 | 111 |
| 121 - (void)setIncognito:(BOOL)incognito { | 112 - (void)setIncognito:(BOOL)incognito { |
| 122 [notificationData_ setObject:[NSNumber numberWithBool:incognito] | 113 [notificationData_ setObject:[NSNumber numberWithBool:incognito] |
| 123 forKey:notification_builder::kNotificationIncognito]; | 114 forKey:notification_constants::kNotificationIncognito]; |
| 124 } | 115 } |
| 125 | 116 |
| 126 - (NSUserNotification*)buildUserNotification { | 117 - (NSUserNotification*)buildUserNotification { |
| 127 base::scoped_nsobject<NSUserNotification> toast( | 118 base::scoped_nsobject<NSUserNotification> toast( |
| 128 [[NSUserNotification alloc] init]); | 119 [[NSUserNotification alloc] init]); |
| 129 [toast setTitle:[notificationData_ objectForKey:kNotificationTitle]]; | 120 [toast setTitle:[notificationData_ objectForKey:kNotificationTitle]]; |
| 130 [toast setSubtitle:[notificationData_ objectForKey:kNotificationSubTitle]]; | 121 [toast setSubtitle:[notificationData_ objectForKey:kNotificationSubTitle]]; |
| 131 [toast setInformativeText:[notificationData_ | 122 [toast setInformativeText:[notificationData_ |
| 132 objectForKey:kNotificationInformativeText]]; | 123 objectForKey:kNotificationInformativeText]]; |
| 133 | 124 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 } | 179 } |
| 189 | 180 |
| 190 // Tag | 181 // Tag |
| 191 if ([toast respondsToSelector:@selector(setIdentifier:)] && | 182 if ([toast respondsToSelector:@selector(setIdentifier:)] && |
| 192 [notificationData_ objectForKey:kNotificationTag]) { | 183 [notificationData_ objectForKey:kNotificationTag]) { |
| 193 [toast setValue:[notificationData_ objectForKey:kNotificationTag] | 184 [toast setValue:[notificationData_ objectForKey:kNotificationTag] |
| 194 forKey:@"identifier"]; | 185 forKey:@"identifier"]; |
| 195 } | 186 } |
| 196 | 187 |
| 197 NSString* origin = | 188 NSString* origin = |
| 198 [notificationData_ objectForKey:notification_builder::kNotificationOrigin] | 189 [notificationData_ |
| 190 objectForKey:notification_constants::kNotificationOrigin] |
| 199 ? [notificationData_ | 191 ? [notificationData_ |
| 200 objectForKey:notification_builder::kNotificationOrigin] | 192 objectForKey:notification_constants::kNotificationOrigin] |
| 201 : @""; | 193 : @""; |
| 202 DCHECK( | 194 DCHECK( |
| 203 [notificationData_ objectForKey:notification_builder::kNotificationId]); | 195 [notificationData_ objectForKey:notification_constants::kNotificationId]); |
| 204 NSString* notificationId = | 196 NSString* notificationId = |
| 205 [notificationData_ objectForKey:notification_builder::kNotificationId]; | 197 [notificationData_ objectForKey:notification_constants::kNotificationId]; |
| 206 | 198 |
| 207 DCHECK([notificationData_ | 199 DCHECK([notificationData_ |
| 208 objectForKey:notification_builder::kNotificationProfileId]); | 200 objectForKey:notification_constants::kNotificationProfileId]); |
| 209 NSString* profileId = [notificationData_ | 201 NSString* profileId = [notificationData_ |
| 210 objectForKey:notification_builder::kNotificationProfileId]; | 202 objectForKey:notification_constants::kNotificationProfileId]; |
| 211 | 203 |
| 212 DCHECK([notificationData_ | 204 DCHECK([notificationData_ |
| 213 objectForKey:notification_builder::kNotificationIncognito]); | 205 objectForKey:notification_constants::kNotificationIncognito]); |
| 214 NSNumber* incognito = [notificationData_ | 206 NSNumber* incognito = [notificationData_ |
| 215 objectForKey:notification_builder::kNotificationIncognito]; | 207 objectForKey:notification_constants::kNotificationIncognito]; |
| 216 | 208 |
| 217 toast.get().userInfo = @{ | 209 toast.get().userInfo = @{ |
| 218 notification_builder::kNotificationOrigin : origin, | 210 notification_constants::kNotificationOrigin : origin, |
| 219 notification_builder::kNotificationId : notificationId, | 211 notification_constants::kNotificationId : notificationId, |
| 220 notification_builder::kNotificationProfileId : profileId, | 212 notification_constants::kNotificationProfileId : profileId, |
| 221 notification_builder::kNotificationIncognito : incognito, | 213 notification_constants::kNotificationIncognito : incognito, |
| 222 }; | 214 }; |
| 223 | 215 |
| 224 return toast.autorelease(); | 216 return toast.autorelease(); |
| 225 } | 217 } |
| 226 | 218 |
| 227 - (NSDictionary*)buildDictionary { | 219 - (NSDictionary*)buildDictionary { |
| 228 return [[notificationData_ copy] autorelease]; | 220 return [[notificationData_ copy] autorelease]; |
| 229 } | 221 } |
| 230 | 222 |
| 231 @end | 223 @end |
| OLD | NEW |