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

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

Issue 2158463003: Initial client side implementation of the XPC service (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 3 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
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/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/command_line.h"
12 #include "base/mac/bundle_locations.h"
9 #include "base/mac/foundation_util.h" 13 #include "base/mac/foundation_util.h"
10 #include "base/mac/mac_util.h" 14 #include "base/mac/mac_util.h"
11 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/sys_string_conversions.h" 16 #include "base/strings/sys_string_conversions.h"
13 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/notifications/native_notification_display_service.h" 18 #include "chrome/browser/notifications/native_notification_display_service.h"
15 #include "chrome/browser/notifications/notification.h" 19 #include "chrome/browser/notifications/notification.h"
16 #include "chrome/browser/notifications/notification_common.h" 20 #include "chrome/browser/notifications/notification_common.h"
17 #include "chrome/browser/notifications/notification_display_service_factory.h" 21 #include "chrome/browser/notifications/notification_display_service_factory.h"
18 #include "chrome/browser/notifications/persistent_notification_delegate.h" 22 #include "chrome/browser/notifications/persistent_notification_delegate.h"
19 #include "chrome/browser/notifications/platform_notification_service_impl.h" 23 #include "chrome/browser/notifications/platform_notification_service_impl.h"
20 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_manager.h" 25 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" 26 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h"
27 #import "chrome/browser/ui/cocoa/notifications/notification_delivery.h"
23 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" 28 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h"
24 #import "chrome/browser/ui/cocoa/notifications/notification_response_builder_mac .h" 29 #import "chrome/browser/ui/cocoa/notifications/notification_response_builder_mac .h"
30 #include "chrome/common/chrome_switches.h"
31 #include "chrome/grit/generated_resources.h"
25 #include "components/url_formatter/elide_url.h" 32 #include "components/url_formatter/elide_url.h"
26 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati onConstants.h" 33 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati onConstants.h"
34 #include "ui/base/l10n/l10n_util_mac.h"
27 #include "url/gurl.h" 35 #include "url/gurl.h"
28 #include "url/origin.h" 36 #include "url/origin.h"
29 37
30 @class NSUserNotification; 38 @class NSUserNotification;
31 @class NSUserNotificationCenter; 39 @class NSUserNotificationCenter;
32 40
33 // The mapping from web notifications to NsUserNotification works as follows 41 // The mapping from web notifications to NsUserNotification works as follows
34 42
35 // notification#title in NSUserNotification.title 43 // notification#title in NSUserNotification.title
36 // notification#message in NSUserNotification.informativeText 44 // notification#message in NSUserNotification.informativeText
(...skipping 27 matching lines...) Expand all
64 } 72 }
65 73
66 NotificationDisplayService* display_service = 74 NotificationDisplayService* display_service =
67 NotificationDisplayServiceFactory::GetForProfile(profile); 75 NotificationDisplayServiceFactory::GetForProfile(profile);
68 76
69 static_cast<NativeNotificationDisplayService*>(display_service) 77 static_cast<NativeNotificationDisplayService*>(display_service)
70 ->ProcessNotificationOperation(operation, notification_type, origin, 78 ->ProcessNotificationOperation(operation, notification_type, origin,
71 notification_id, action_index); 79 notification_id, action_index);
72 } 80 }
73 81
82 // Loads the profile and process the Notification response
83 void DoProcessNotificationResponse(NotificationCommon::Operation operation,
84 NotificationCommon::Type type,
85 const std::string& profile_id,
86 bool incognito,
87 const std::string& origin,
88 const std::string& notification_id,
89 int32_t button_index) {
90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
91 ProfileManager* profileManager = g_browser_process->profile_manager();
92 DCHECK(profileManager);
93
94 profileManager->LoadProfile(
95 profile_id, incognito, base::Bind(&ProfileLoadedCallback, operation, type,
96 origin, notification_id, button_index));
97 }
98
74 } // namespace 99 } // namespace
75 100
76 // static 101 // static
77 NotificationPlatformBridge* NotificationPlatformBridge::Create() { 102 NotificationPlatformBridge* NotificationPlatformBridge::Create() {
78 return new NotificationPlatformBridgeMac( 103 return new NotificationPlatformBridgeMac(
79 [NSUserNotificationCenter defaultUserNotificationCenter]); 104 [NSUserNotificationCenter defaultUserNotificationCenter]);
80 } 105 }
81 106
82 // A Cocoa class that represents the delegate of NSUserNotificationCenter and 107 // A Cocoa class that represents the delegate of NSUserNotificationCenter and
83 // can forward commands to C++. 108 // can forward commands to C++.
84 @interface NotificationCenterDelegate 109 @interface NotificationCenterDelegate
85 : NSObject<NSUserNotificationCenterDelegate> { 110 : NSObject<NSUserNotificationCenterDelegate> {
86 } 111 }
87 @end 112 @end
88 113
89 // ///////////////////////////////////////////////////////////////////////////// 114 // /////////////////////////////////////////////////////////////////////////////
90 115
91 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac( 116 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac(
92 NSUserNotificationCenter* notification_center) 117 NSUserNotificationCenter* notification_center)
93 : delegate_([NotificationCenterDelegate alloc]), 118 : delegate_([NotificationCenterDelegate alloc]),
94 notification_center_(notification_center) { 119 notification_center_(notification_center),
120 notification_remote_dispatcher_(
121 [[NotificationRemoteDispatcher alloc] init]) {
95 [notification_center_ setDelegate:delegate_.get()]; 122 [notification_center_ setDelegate:delegate_.get()];
96 } 123 }
97 124
98 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { 125 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() {
99 [notification_center_ setDelegate:nil]; 126 [notification_center_ setDelegate:nil];
100 127
101 // TODO(miguelg) lift this restriction if possible. 128 // TODO(miguelg) lift this restriction if possible.
129 // But make sure the ones shown by the XPC service are actually
130 // removed.
Peter Beverloo 2016/09/09 14:56:19 Is this comment separate from the TODO, or is it a
Miguel Garcia 2016/09/12 13:49:00 Formatted as 2 TODOs now On 2016/09/09 14:56:19,
102 [notification_center_ removeAllDeliveredNotifications]; 131 [notification_center_ removeAllDeliveredNotifications];
103 } 132 }
104 133
105 void NotificationPlatformBridgeMac::Display( 134 void NotificationPlatformBridgeMac::Display(
106 NotificationCommon::Type notification_type, 135 NotificationCommon::Type notification_type,
107 const std::string& notification_id, 136 const std::string& notification_id,
108 const std::string& profile_id, 137 const std::string& profile_id,
109 bool incognito, 138 bool incognito,
110 const Notification& notification) { 139 const Notification& notification) {
111 base::scoped_nsobject<NotificationBuilder> builder( 140 base::scoped_nsobject<NotificationBuilder> builder(
(...skipping 20 matching lines...) Expand all
132 NSString* buttonOne = SysUTF16ToNSString(buttons[0].title); 161 NSString* buttonOne = SysUTF16ToNSString(buttons[0].title);
133 NSString* buttonTwo = nullptr; 162 NSString* buttonTwo = nullptr;
134 if (buttons.size() > 1) 163 if (buttons.size() > 1)
135 buttonTwo = SysUTF16ToNSString(buttons[1].title); 164 buttonTwo = SysUTF16ToNSString(buttons[1].title);
136 [builder setButtons:buttonOne secondaryButton:buttonTwo]; 165 [builder setButtons:buttonOne secondaryButton:buttonTwo];
137 } 166 }
138 167
139 // Tag 168 // Tag
140 if (!notification.tag().empty()) { 169 if (!notification.tag().empty()) {
141 [builder setTag:base::SysUTF8ToNSString(notification.tag())]; 170 [builder setTag:base::SysUTF8ToNSString(notification.tag())];
171
142 // If renotify is needed, delete the notification with the same tag 172 // If renotify is needed, delete the notification with the same tag
143 // from the notification center before displaying this one. 173 // from the notification center before displaying this one.
144 // TODO(miguelg): This will need to work for alerts as well via XPC 174 // TODO(miguelg): This will need to work for alerts as well via XPC
145 // once supported. 175 // once supported.
146 if (notification.renotify()) { 176 if (notification.renotify()) {
147 NSUserNotificationCenter* notification_center = 177 NSUserNotificationCenter* notification_center =
148 [NSUserNotificationCenter defaultUserNotificationCenter]; 178 [NSUserNotificationCenter defaultUserNotificationCenter];
149 for (NSUserNotification* existing_notification in 179 for (NSUserNotification* existing_notification in
150 [notification_center deliveredNotifications]) { 180 [notification_center deliveredNotifications]) {
151 NSString* identifier = 181 NSString* identifier =
152 [existing_notification valueForKey:@"identifier"]; 182 [existing_notification valueForKey:@"identifier"];
153 if ([identifier 183 if ([identifier
154 isEqualToString:base::SysUTF8ToNSString(notification.tag())]) { 184 isEqualToString:base::SysUTF8ToNSString(notification.tag())]) {
155 [notification_center 185 [notification_center
156 removeDeliveredNotification:existing_notification]; 186 removeDeliveredNotification:existing_notification];
157 break; 187 break;
158 } 188 }
159 } 189 }
160 } 190 }
161 } 191 }
162 192
163 [builder setOrigin:base::SysUTF8ToNSString(notification.origin_url().spec())]; 193 [builder setOrigin:base::SysUTF8ToNSString(notification.origin_url().spec())];
164 [builder setNotificationId:base::SysUTF8ToNSString(notification_id)]; 194 [builder setNotificationId:base::SysUTF8ToNSString(notification_id)];
165 [builder setProfileId:base::SysUTF8ToNSString(profile_id)]; 195 [builder setProfileId:base::SysUTF8ToNSString(profile_id)];
166 [builder setIncognito:incognito]; 196 [builder setIncognito:incognito];
167 [builder setNotificationType:[NSNumber numberWithInteger:notification_type]]; 197 [builder setNotificationType:[NSNumber numberWithInteger:notification_type]];
168 198 if (notification.never_timeout() &&
169 NSUserNotification* toast = [builder buildUserNotification]; 199 base::CommandLine::ForCurrentProcess()->HasSwitch(
170 [notification_center_ deliverNotification:toast]; 200 switches::kEnableNativeAlerts)) {
Peter Beverloo 2016/09/09 14:56:19 As discussed, we'll want to switch this to a base:
201 NSDictionary* dict = [builder buildDictionary];
202 [notification_remote_dispatcher_ dispatchNotification:dict];
203 } else {
204 NSUserNotification* toast = [builder buildUserNotification];
205 [notification_center_ deliverNotification:toast];
206 }
171 } 207 }
172 208
173 void NotificationPlatformBridgeMac::Close(const std::string& profile_id, 209 void NotificationPlatformBridgeMac::Close(const std::string& profile_id,
174 const std::string& notification_id) { 210 const std::string& notification_id) {
175 NSString* candidate_id = base::SysUTF8ToNSString(notification_id); 211 NSString* candidate_id = base::SysUTF8ToNSString(notification_id);
176 212
177 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); 213 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id);
178 for (NSUserNotification* toast in 214 for (NSUserNotification* toast in
179 [notification_center_ deliveredNotifications]) { 215 [notification_center_ deliveredNotifications]) {
180 NSString* toast_id = 216 NSString* toast_id =
(...skipping 26 matching lines...) Expand all
207 } 243 }
208 } 244 }
209 return true; 245 return true;
210 } 246 }
211 247
212 bool NotificationPlatformBridgeMac::SupportsNotificationCenter() const { 248 bool NotificationPlatformBridgeMac::SupportsNotificationCenter() const {
213 return true; 249 return true;
214 } 250 }
215 251
216 // static 252 // static
253 void NotificationPlatformBridgeMac::ProcessNotificationResponse(
254 NSDictionary* notificationResponseData) {
Robert Sesek 2016/09/08 21:10:08 naming: under_scores (and maybe a shorter name to
Peter Beverloo 2016/09/09 14:56:19 I *really* dislike the hacker_style vs. camelCase
Robert Sesek 2016/09/09 20:43:43 The style rule isn't about the file, though. It's
Miguel Garcia 2016/09/12 13:48:58 Done.
Miguel Garcia 2016/09/12 13:49:00 I re-named it to a single word to make both of you
255 if (!NotificationPlatformBridgeMac::VerifyNotificationData(
256 notificationResponseData))
257 return;
258
259 NSNumber* buttonIndex = [notificationResponseData
260 objectForKey:notification_constants::kNotificationButtonIndex];
261 NSNumber* operation = [notificationResponseData
262 objectForKey:notification_constants::kNotificationOperation];
263
264 std::string notificationOrigin =
265 base::SysNSStringToUTF8([notificationResponseData
266 objectForKey:notification_constants::kNotificationOrigin]);
267 std::string notificationId = base::SysNSStringToUTF8([notificationResponseData
268 objectForKey:notification_constants::kNotificationId]);
269 std::string profileId = base::SysNSStringToUTF8([notificationResponseData
270 objectForKey:notification_constants::kNotificationProfileId]);
271 NSNumber* isIncognito = [notificationResponseData
272 objectForKey:notification_constants::kNotificationIncognito];
273 NSNumber* notificationType = [notificationResponseData
274 objectForKey:notification_constants::kNotificationType];
275
276 content::BrowserThread::PostTask(
277 content::BrowserThread::UI, FROM_HERE,
278 base::Bind(DoProcessNotificationResponse,
279 static_cast<NotificationCommon::Operation>(
280 operation.unsignedIntValue),
281 static_cast<NotificationCommon::Type>(
282 notificationType.unsignedIntValue),
283 profileId, [isIncognito boolValue], notificationOrigin,
284 notificationId, buttonIndex.intValue));
285 }
286
287 // static
217 bool NotificationPlatformBridgeMac::VerifyNotificationData( 288 bool NotificationPlatformBridgeMac::VerifyNotificationData(
218 NSDictionary* response) { 289 NSDictionary* response) {
219 if (![response 290 if (![response
220 objectForKey:notification_constants::kNotificationButtonIndex] || 291 objectForKey:notification_constants::kNotificationButtonIndex] ||
221 ![response objectForKey:notification_constants::kNotificationOperation] || 292 ![response objectForKey:notification_constants::kNotificationOperation] ||
222 ![response objectForKey:notification_constants::kNotificationId] || 293 ![response objectForKey:notification_constants::kNotificationId] ||
223 ![response objectForKey:notification_constants::kNotificationProfileId] || 294 ![response objectForKey:notification_constants::kNotificationProfileId] ||
224 ![response objectForKey:notification_constants::kNotificationIncognito] || 295 ![response objectForKey:notification_constants::kNotificationIncognito] ||
225 ![response objectForKey:notification_constants::kNotificationType]) { 296 ![response objectForKey:notification_constants::kNotificationType]) {
226 LOG(ERROR) << "Missing required key"; 297 LOG(ERROR) << "Missing required key";
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 } 350 }
280 351
281 return true; 352 return true;
282 } 353 }
283 354
284 // ///////////////////////////////////////////////////////////////////////////// 355 // /////////////////////////////////////////////////////////////////////////////
285 356
286 @implementation NotificationCenterDelegate 357 @implementation NotificationCenterDelegate
287 - (void)userNotificationCenter:(NSUserNotificationCenter*)center 358 - (void)userNotificationCenter:(NSUserNotificationCenter*)center
288 didActivateNotification:(NSUserNotification*)notification { 359 didActivateNotification:(NSUserNotification*)notification {
289 NSDictionary* response = 360 NSDictionary* notificationResponseData =
290 [NotificationResponseBuilder buildDictionary:notification]; 361 [NotificationResponseBuilder buildDictionary:notification];
291 if (!NotificationPlatformBridgeMac::VerifyNotificationData(response)) 362 NotificationPlatformBridgeMac::ProcessNotificationResponse(
292 return; 363 notificationResponseData);
293
294 NSNumber* buttonIndex =
295 [response objectForKey:notification_constants::kNotificationButtonIndex];
296 NSNumber* operation =
297 [response objectForKey:notification_constants::kNotificationOperation];
298
299 std::string notificationOrigin = base::SysNSStringToUTF8(
300 [response objectForKey:notification_constants::kNotificationOrigin]);
301 NSString* notificationId =
302 [response objectForKey:notification_constants::kNotificationId];
303 std::string persistentNotificationId =
304 base::SysNSStringToUTF8(notificationId);
305 std::string profileId = base::SysNSStringToUTF8(
306 [response objectForKey:notification_constants::kNotificationProfileId]);
307 NSNumber* isIncognito =
308 [response objectForKey:notification_constants::kNotificationIncognito];
309 NSNumber* notificationType =
310 [response objectForKey:notification_constants::kNotificationType];
311
312 ProfileManager* profileManager = g_browser_process->profile_manager();
313 DCHECK(profileManager);
314
315 profileManager->LoadProfile(
316 profileId, [isIncognito boolValue],
317 base::Bind(
318 &ProfileLoadedCallback, static_cast<NotificationCommon::Operation>(
319 operation.unsignedIntValue),
320 static_cast<NotificationCommon::Type>(
321 notificationType.unsignedIntValue),
322 notificationOrigin, persistentNotificationId, buttonIndex.intValue));
323 } 364 }
324 365
325 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center 366 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center
326 shouldPresentNotification:(NSUserNotification*)nsNotification { 367 shouldPresentNotification:(NSUserNotification*)nsNotification {
327 // Always display notifications, regardless of whether the app is foreground. 368 // Always display notifications, regardless of whether the app is foreground.
328 return YES; 369 return YES;
329 } 370 }
330 371
331 @end 372 @end
373
374 @implementation NotificationRemoteDispatcher
375
376 @synthesize xpcConnection = _xpcConnection;
Robert Sesek 2016/09/08 21:10:08 There's no need for this @property. It can just be
Robert Sesek 2016/09/08 21:10:08 Use trailing underscores.
Miguel Garcia 2016/09/12 13:49:00 Done.
Miguel Garcia 2016/09/12 13:49:00 Done.
377
378 - (instancetype)init {
Robert Sesek 2016/09/08 21:10:08 This needs to be wrapped in a if ((self = [super
Miguel Garcia 2016/09/12 13:49:00 Done.
379 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
380 switches::kEnableNativeAlerts)) {
381 _xpcConnection = [[NSXPCConnection alloc]
Robert Sesek 2016/09/08 21:10:08 This is leaked.
Miguel Garcia 2016/09/12 13:49:00 Done.
382 initWithServiceName:
383 [NSString
384 stringWithFormat:notification_constants::kAlertXPCServiceName,
385 [base::mac::OuterBundle() bundleIdentifier]]];
386 _xpcConnection.remoteObjectInterface =
387 [NSXPCInterface interfaceWithProtocol:@protocol(NotificationDelivery)];
388 _xpcConnection.interruptionHandler = ^{
389 NSLog(@"connection interrupted: interruptionHandler: %@", _xpcConnection);
Robert Sesek 2016/09/08 21:10:08 Use Chromium logging instead.
Miguel Garcia 2016/09/12 13:48:58 Done.
390 // TODO(miguelg): perhaps add some UMA here.
391 // We will be getting this handler both when the XPC server crashes or
392 // when it decides to close the connection.
393 };
394 _xpcConnection.invalidationHandler = ^{
395 NSLog(@"conn invalidationHandler %@", _xpcConnection);
396 // This means that the connection should be recreated if it needs
397 // to be used again. It should not really happen.
398 DCHECK(false) << "XPC Connection invalidated";
399 };
400
401 _xpcConnection.exportedInterface =
402 [NSXPCInterface interfaceWithProtocol:@protocol(NotificationReply)];
403 _xpcConnection.exportedObject = self;
404 [_xpcConnection resume];
405 }
406
407 return self;
408 }
409
410 - (void)dispatchNotification:(NSDictionary*)data {
411 [[_xpcConnection remoteObjectProxy] deliverNotification:data];
412 }
413
414 // NotificationReply implementation
415 - (void)notificationClick:(NSDictionary*)notificationResponseData {
416 NotificationPlatformBridgeMac::ProcessNotificationResponse(
417 notificationResponseData);
418 }
419
420 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698