Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: chrome/browser/notifications/notification_builder_mac.mm

Issue 2053613002: Make the notification builder more XPC friendly (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: revert unintended change Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "chrome/browser/notifications/notification_builder_mac.h"
6
7 #import <AppKit/AppKit.h>
8
9 #include "base/mac/mac_util.h"
10 #include "base/mac/scoped_nsobject.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "ui/base/l10n/l10n_util_mac.h"
13
14 namespace {
15
16 // Internal builder constants representing the different notification fields
17 // They don't need to be exposed outside the builder.
18
19 NSString* const kNotificationTitle = @"title";
20 NSString* const kNotificationSubTitle = @"subtitle";
21 NSString* const kNotificationInformativeText = @"informativeText";
22 NSString* const kNotificationImage = @"icon";
23 NSString* const kNotificationButtonOne = @"buttonOne";
24 NSString* const kNotificationButtonTwo = @"buttonTwo";
25 NSString* const kNotificationTag = @"tag";
26
27 } // namespace
28
29 namespace notification_builder {
30
31 // Exposed constants to include user related data in the notification.
32 NSString* const kNotificationOrigin = @"notificationOrigin";
33 NSString* const kNotificationId = @"notificationId";
34 NSString* const kNotificationProfileId = @"notificationProfileId";
35 NSString* const kNotificationIncognito = @"notificationIncognito";
36
37 } // namespace notification_builder
38
39 @implementation NotificationBuilder {
40 base::scoped_nsobject<NSMutableDictionary> notificationData_;
41 }
42
43 - (instancetype)init {
44 if ((self = [super init])) {
45 notificationData_.reset([[NSMutableDictionary alloc] init]);
46 }
47 return self;
48 }
49
50 - (instancetype)initWithDictionary:(NSDictionary*)data {
51 if ((self = [super init])) {
52 notificationData_.reset([data copy]);
53 }
54 return self;
55 }
56
57 - (void)setTitle:(NSString*)title {
58 if (title.length)
59 [notificationData_ setObject:title forKey:kNotificationTitle];
60 }
61
62 - (void)setSubTitle:(NSString*)subTitle {
63 if (subTitle.length)
64 [notificationData_ setObject:subTitle forKey:kNotificationSubTitle];
65 }
66
67 - (void)setContextMessage:(NSString*)contextMessage {
68 if (contextMessage.length)
69 [notificationData_ setObject:contextMessage
70 forKey:kNotificationInformativeText];
71 }
72
73 - (void)setIcon:(NSImage*)icon {
74 if (icon)
75 [notificationData_ setObject:icon forKey:kNotificationImage];
76 }
77
78 - (void)setButtons:(NSString*)primaryButton
79 secondaryButton:(NSString*)secondaryButton {
80 DCHECK(primaryButton.length);
81 [notificationData_ setObject:primaryButton forKey:kNotificationButtonOne];
82 if (secondaryButton.length) {
83 [notificationData_ setObject:secondaryButton forKey:kNotificationButtonTwo];
84 }
85 }
86
87 - (void)setTag:(NSString*)tag {
88 if (tag.length)
89 [notificationData_ setObject:tag forKey:kNotificationTag];
90 }
91
92 - (void)setOrigin:(NSString*)origin {
93 if (origin.length)
94 [notificationData_ setObject:origin
95 forKey:notification_builder::kNotificationOrigin];
96 }
97
98 - (void)setNotificationId:(NSString*)notificationId {
99 DCHECK(notificationId.length);
100 [notificationData_ setObject:notificationId
101 forKey:notification_builder::kNotificationId];
102 }
103
104 - (void)setProfileId:(NSString*)profileId {
105 DCHECK(profileId.length);
106 [notificationData_ setObject:profileId
107 forKey:notification_builder::kNotificationProfileId];
108 }
109
110 - (void)setIncognito:(BOOL)incognito {
111 [notificationData_ setObject:[NSNumber numberWithBool:incognito]
112 forKey:notification_builder::kNotificationIncognito];
113 }
114
115 - (NSUserNotification*)buildUserNotification {
116 base::scoped_nsobject<NSUserNotification> toast(
117 [[NSUserNotification alloc] init]);
118 [toast setTitle:[notificationData_ objectForKey:kNotificationTitle]];
119 [toast setSubtitle:[notificationData_ objectForKey:kNotificationSubTitle]];
120 [toast setInformativeText:[notificationData_
121 objectForKey:kNotificationInformativeText]];
122
123 // Icon
124 if ([notificationData_ objectForKey:kNotificationImage]) {
125 if ([toast respondsToSelector:@selector(_identityImage)]) {
126 NSImage* image = [notificationData_ objectForKey:kNotificationImage];
127 [toast setValue:image forKey:@"_identityImage"];
128 [toast setValue:@NO forKey:@"_identityImageHasBorder"];
129 }
130 }
131
132 // Buttons
133 if ([toast respondsToSelector:@selector(_showsButtons)]) {
134 [toast setValue:@YES forKey:@"_showsButtons"];
135 // A default close button label is provided by the platform but we
136 // explicitly override it in case the user decides to not
137 // use the OS language in Chrome.
138 [toast setOtherButtonTitle:l10n_util::GetNSString(
139 IDS_NOTIFICATION_BUTTON_CLOSE)];
140
141 // Display the Settings button as the action button if there are either no
142 // developer-provided action buttons, or the alternate action menu is not
143 // available on this Mac version. This avoids needlessly showing the menu.
144 // TODO(miguelg): Extensions should not have a settings button.
145 if (![notificationData_ objectForKey:kNotificationButtonOne] ||
146 ![toast respondsToSelector:@selector(_alwaysShowAlternateActionMenu)]) {
147 [toast setActionButtonTitle:l10n_util::GetNSString(
148 IDS_NOTIFICATION_BUTTON_SETTINGS)];
149 } else {
150 // Otherwise show the alternate menu, then show the developer actions and
151 // finally the settings one.
152 DCHECK(
153 [toast respondsToSelector:@selector(_alwaysShowAlternateActionMenu)]);
154 DCHECK(
155 [toast respondsToSelector:@selector(_alternateActionButtonTitles)]);
156 [toast setActionButtonTitle:l10n_util::GetNSString(
157 IDS_NOTIFICATION_BUTTON_OPTIONS)];
158 [toast setValue:@YES forKey:@"_alwaysShowAlternateActionMenu"];
159
160 NSMutableArray* buttons = [NSMutableArray arrayWithCapacity:3];
161 [buttons
162 addObject:[notificationData_ objectForKey:kNotificationButtonOne]];
163 if ([notificationData_ objectForKey:kNotificationButtonTwo]) {
164 [buttons
165 addObject:[notificationData_ objectForKey:kNotificationButtonTwo]];
166 }
167 [buttons
168 addObject:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_SETTINGS)];
169 [toast setValue:buttons forKey:@"_alternateActionButtonTitles"];
170 }
171 }
172
173 // Tag
174 if ([toast respondsToSelector:@selector(setIdentifier:)] &&
175 [notificationData_ objectForKey:kNotificationTag]) {
176 [toast setValue:[notificationData_ objectForKey:kNotificationTag]
177 forKey:@"identifier"];
178 }
179
180 NSString* origin =
181 [notificationData_ objectForKey:notification_builder::kNotificationOrigin]
182 ? [notificationData_
183 objectForKey:notification_builder::kNotificationOrigin]
184 : @"";
185 DCHECK(
186 [notificationData_ objectForKey:notification_builder::kNotificationId]);
187 NSString* notificationId =
188 [notificationData_ objectForKey:notification_builder::kNotificationId];
189
190 DCHECK([notificationData_
191 objectForKey:notification_builder::kNotificationProfileId]);
192 NSString* profileId = [notificationData_
193 objectForKey:notification_builder::kNotificationProfileId];
194
195 DCHECK([notificationData_
196 objectForKey:notification_builder::kNotificationIncognito]);
197 NSNumber* incognito = [notificationData_
198 objectForKey:notification_builder::kNotificationIncognito];
199
200 toast.get().userInfo = @{
201 notification_builder::kNotificationOrigin : origin,
202 notification_builder::kNotificationId : notificationId,
203 notification_builder::kNotificationProfileId : profileId,
204 notification_builder::kNotificationIncognito : incognito,
205 };
206
207 return toast.autorelease();
208 }
209
210 - (NSDictionary*)buildDictionary {
211 return [[notificationData_ copy] autorelease];
212 }
213
214 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698