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

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

Issue 2047643003: DO NOT SUBMIT. Implement an XPCService for notifications. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge to use AlertNotificationService Created 4 years, 5 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/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"
(...skipping 11 matching lines...) Expand all
22 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" 22 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h"
23 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" 23 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h"
24 #import "chrome/browser/ui/cocoa/notifications/notification_response_builder_mac .h" 24 #import "chrome/browser/ui/cocoa/notifications/notification_response_builder_mac .h"
25 #include "chrome/grit/generated_resources.h" 25 #include "chrome/grit/generated_resources.h"
26 #include "components/url_formatter/elide_url.h" 26 #include "components/url_formatter/elide_url.h"
27 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati onConstants.h" 27 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati onConstants.h"
28 #include "ui/base/l10n/l10n_util_mac.h" 28 #include "ui/base/l10n/l10n_util_mac.h"
29 #include "url/gurl.h" 29 #include "url/gurl.h"
30 #include "url/origin.h" 30 #include "url/origin.h"
31 31
32 #import "base/mac/bundle_locations.h"
33 #import "chrome/browser/ui/cocoa/notifications/notification_delivery.h"
34 #include "base/bind.h"
35 #include "base/bind_helpers.h"
36
32 @class NSUserNotification; 37 @class NSUserNotification;
33 @class NSUserNotificationCenter; 38 @class NSUserNotificationCenter;
34 39
35 // The mapping from web notifications to NsUserNotification works as follows 40 // The mapping from web notifications to NsUserNotification works as follows
36 41
37 // notification#title in NSUserNotification.title 42 // notification#title in NSUserNotification.title
38 // notification#message in NSUserNotification.informativeText 43 // notification#message in NSUserNotification.informativeText
39 // notification#context_message in NSUserNotification.subtitle 44 // notification#context_message in NSUserNotification.subtitle
40 // notification#tag in NSUserNotification.identifier (10.9) 45 // notification#tag in NSUserNotification.identifier (10.9)
41 // notification#icon in NSUserNotification.contentImage (10.9) 46 // notification#icon in NSUserNotification.contentImage (10.9)
(...skipping 25 matching lines...) Expand all
67 72
68 NotificationDisplayService* display_service = 73 NotificationDisplayService* display_service =
69 NotificationDisplayServiceFactory::GetForProfile(profile); 74 NotificationDisplayServiceFactory::GetForProfile(profile);
70 75
71 static_cast<NativeNotificationDisplayService*>(display_service) 76 static_cast<NativeNotificationDisplayService*>(display_service)
72 ->ProcessNotificationOperation(operation, notification_type, origin, 77 ->ProcessNotificationOperation(operation, notification_type, origin,
73 notification_id, action_index); 78 notification_id, action_index);
74 } 79 }
75 80
76 } // namespace 81 } // namespace
82 void ProcessNotificationResponse(NotificationCommon::Operation operation,
83 NotificationCommon::Type type,
84 const std::string& profile_id,
85 bool incognito,
86 const std::string& origin,
87 const std::string& notification_id,
88 int32_t button_index) {
89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
90 ProfileManager* profileManager = g_browser_process->profile_manager();
91 DCHECK(profileManager);
92
93 profileManager->LoadProfile(
94 profile_id, incognito, base::Bind(&ProfileLoadedCallback, operation, type,
95 origin, notification_id, button_index));
96 }
77 97
78 // static 98 // static
79 NotificationPlatformBridge* NotificationPlatformBridge::Create() { 99 NotificationPlatformBridge* NotificationPlatformBridge::Create() {
80 return new NotificationPlatformBridgeMac( 100 return new NotificationPlatformBridgeMac(
81 [NSUserNotificationCenter defaultUserNotificationCenter]); 101 [NSUserNotificationCenter defaultUserNotificationCenter]);
82 } 102 }
83 103
84 // A Cocoa class that represents the delegate of NSUserNotificationCenter and 104 // A Cocoa class that represents the delegate of NSUserNotificationCenter and
85 // can forward commands to C++. 105 // can forward commands to C++.
86 @interface NotificationCenterDelegate 106 @interface NotificationCenterDelegate
87 : NSObject<NSUserNotificationCenterDelegate> { 107 : NSObject<NSUserNotificationCenterDelegate> {
88 } 108 }
89 @end 109 @end
90 110
91 // ///////////////////////////////////////////////////////////////////////////// 111 // /////////////////////////////////////////////////////////////////////////////
92 112
93 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac( 113 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac(
94 NSUserNotificationCenter* notification_center) 114 NSUserNotificationCenter* notification_center)
95 : delegate_([NotificationCenterDelegate alloc]), 115 : delegate_([NotificationCenterDelegate alloc]),
96 notification_center_(notification_center) { 116 notification_center_(notification_center),
117 notification_remote_dispatcher_(
118 [[NotificationRemoteDispatcher alloc] init]) {
97 [notification_center_ setDelegate:delegate_.get()]; 119 [notification_center_ setDelegate:delegate_.get()];
98 } 120 }
99 121
100 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { 122 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() {
101 [notification_center_ setDelegate:nil]; 123 [notification_center_ setDelegate:nil];
102 124
103 // TODO(miguelg) lift this restriction if possible. 125 // TODO(miguelg) lift this restriction if possible.
104 [notification_center_ removeAllDeliveredNotifications]; 126 [notification_center_ removeAllDeliveredNotifications];
105 } 127 }
106 128
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 } 183 }
162 } 184 }
163 } 185 }
164 186
165 [builder setOrigin:base::SysUTF8ToNSString(notification.origin_url().spec())]; 187 [builder setOrigin:base::SysUTF8ToNSString(notification.origin_url().spec())];
166 [builder setNotificationId:base::SysUTF8ToNSString(notification_id)]; 188 [builder setNotificationId:base::SysUTF8ToNSString(notification_id)];
167 [builder setProfileId:base::SysUTF8ToNSString(profile_id)]; 189 [builder setProfileId:base::SysUTF8ToNSString(profile_id)];
168 [builder setIncognito:incognito]; 190 [builder setIncognito:incognito];
169 [builder setNotificationType:[NSNumber numberWithInteger:notification_type]]; 191 [builder setNotificationType:[NSNumber numberWithInteger:notification_type]];
170 192
171 NSUserNotification* toast = [builder buildUserNotification]; 193 NSLog(@"BUNDLE %@",
172 [notification_center_ deliverNotification:toast]; 194 [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"]);
195 NSLog(@"NOTIFICATION TYPE %@",
196 [[NSBundle mainBundle]
197 objectForInfoDictionaryKey:@"NSUserNotificationAlertStyle"]);
198 if (notification.never_timeout()) {
199 NSDictionary* dict = [builder buildDictionary];
200 [notification_remote_dispatcher_ dispatchNotification:dict];
201 } else {
202 LOG(WARNING) << "DELIVER NOTIFICATION";
203 NSUserNotification* toast = [builder buildUserNotification];
204 [notification_center_ deliverNotification:toast];
205 }
173 } 206 }
174 207
175 void NotificationPlatformBridgeMac::Close(const std::string& profile_id, 208 void NotificationPlatformBridgeMac::Close(const std::string& profile_id,
176 const std::string& notification_id) { 209 const std::string& notification_id) {
177 NSString* candidate_id = base::SysUTF8ToNSString(notification_id); 210 NSString* candidate_id = base::SysUTF8ToNSString(notification_id);
178 211
179 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id); 212 NSString* current_profile_id = base::SysUTF8ToNSString(profile_id);
180 for (NSUserNotification* toast in 213 for (NSUserNotification* toast in
181 [notification_center_ deliveredNotifications]) { 214 [notification_center_ deliveredNotifications]) {
182 NSString* toast_id = 215 NSString* toast_id =
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 &ProfileLoadedCallback, static_cast<NotificationCommon::Operation>( 353 &ProfileLoadedCallback, static_cast<NotificationCommon::Operation>(
321 operation.unsignedIntValue), 354 operation.unsignedIntValue),
322 static_cast<NotificationCommon::Type>( 355 static_cast<NotificationCommon::Type>(
323 notificationType.unsignedIntValue), 356 notificationType.unsignedIntValue),
324 notificationOrigin, persistentNotificationId, buttonIndex.intValue)); 357 notificationOrigin, persistentNotificationId, buttonIndex.intValue));
325 } 358 }
326 359
327 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center 360 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center
328 shouldPresentNotification:(NSUserNotification*)nsNotification { 361 shouldPresentNotification:(NSUserNotification*)nsNotification {
329 // Always display notifications, regardless of whether the app is foreground. 362 // Always display notifications, regardless of whether the app is foreground.
363 LOG(WARNING) << "SHOULD PRESENT? ";
330 return YES; 364 return YES;
331 } 365 }
332 366
333 @end 367 @end
368
369 @implementation NotificationRemoteDispatcher
370
371 @synthesize xpcConnection = _xpcConnection;
372
373 - (instancetype)init {
374 // scoped ns object probably?
375 _xpcConnection = [[NSXPCConnection alloc]
376 initWithServiceName:[NSString
377 stringWithFormat:@"%@.AlertNotificationService",
378 [base::mac::OuterBundle()
379 bundleIdentifier]]];
380 _xpcConnection.remoteObjectInterface =
381 [NSXPCInterface interfaceWithProtocol:@protocol(NotificationDelivery)];
382 _xpcConnection.interruptionHandler = ^{
383 NSLog(@"conn interruptionHandler: %@", _xpcConnection);
384 };
385 _xpcConnection.invalidationHandler = ^{
386 NSLog(@"conn invalidationHandler %@", _xpcConnection);
387 };
388
389 _xpcConnection.exportedInterface =
390 [NSXPCInterface interfaceWithProtocol:@protocol(NotificationReply)];
391 _xpcConnection.exportedObject = self;
392 [_xpcConnection resume];
393
394 return self;
395 }
396
397 - (void)dispatchNotification:(NSDictionary*)data {
398 LOG(WARNING) << "DISPATCHING XPC ALERT";
399
400 [[_xpcConnection remoteObjectProxy] deliverNotification:data];
401
402 //[conn invalidate];
403 //[conn release];
404 }
405
406 // NotificationReply implementation
407 //// Need to merge with local on clicks.
408 - (void)notificationClick:(NSDictionary*)notificationResponseData {
409 NSLog(@"ALERT CLICKED AND RECEIVED BACK IN CHROME with %@",
410 notificationResponseData);
411 if (!NotificationPlatformBridgeMac::VerifyNotificationData(
412 notificationResponseData))
413 return;
414
415 NSNumber* buttonIndex = [notificationResponseData
416 objectForKey:notification_constants::kNotificationButtonIndex];
417 NSNumber* operation = [notificationResponseData
418 objectForKey:notification_constants::kNotificationOperation];
419
420 std::string notificationOrigin =
421 base::SysNSStringToUTF8([notificationResponseData
422 objectForKey:notification_constants::kNotificationOrigin]);
423 std::string notificationId = base::SysNSStringToUTF8([notificationResponseData
424 objectForKey:notification_constants::kNotificationId]);
425 std::string profileId = base::SysNSStringToUTF8([notificationResponseData
426 objectForKey:notification_constants::kNotificationProfileId]);
427 NSNumber* isIncognito = [notificationResponseData
428 objectForKey:notification_constants::kNotificationIncognito];
429 NSNumber* notificationType = [notificationResponseData
430 objectForKey:notification_constants::kNotificationType];
431
432 content::BrowserThread::PostTask(
433 content::BrowserThread::UI, FROM_HERE,
434 base::Bind(ProcessNotificationResponse,
435 static_cast<NotificationCommon::Operation>(
436 operation.unsignedIntValue),
437 static_cast<NotificationCommon::Type>(
438 notificationType.unsignedIntValue),
439 profileId, [isIncognito boolValue], notificationOrigin,
440 notificationId, buttonIndex.intValue));
441 }
442
443 @end
OLDNEW
« no previous file with comments | « chrome/browser/notifications/notification_platform_bridge_mac.h ('k') | chrome/browser/ui/cocoa/notifications/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698