| OLD | NEW |
| 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" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/mac/bundle_locations.h" | 11 #include "base/mac/bundle_locations.h" |
| 12 #include "base/mac/foundation_util.h" | 12 #include "base/mac/foundation_util.h" |
| 13 #include "base/mac/mac_util.h" | 13 #include "base/mac/mac_util.h" |
| 14 #include "base/mac/scoped_mach_port.h" |
| 14 #include "base/mac/scoped_nsobject.h" | 15 #include "base/mac/scoped_nsobject.h" |
| 15 #include "base/strings/nullable_string16.h" | 16 #include "base/strings/nullable_string16.h" |
| 16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/sys_string_conversions.h" | 18 #include "base/strings/sys_string_conversions.h" |
| 18 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/notifications/native_notification_display_service.h" | 20 #include "chrome/browser/notifications/native_notification_display_service.h" |
| 20 #include "chrome/browser/notifications/notification.h" | 21 #include "chrome/browser/notifications/notification.h" |
| 21 #include "chrome/browser/notifications/notification_common.h" | 22 #include "chrome/browser/notifications/notification_common.h" |
| 22 #include "chrome/browser/notifications/notification_display_service_factory.h" | 23 #include "chrome/browser/notifications/notification_display_service_factory.h" |
| 23 #include "chrome/browser/notifications/persistent_notification_delegate.h" | 24 #include "chrome/browser/notifications/persistent_notification_delegate.h" |
| 24 #include "chrome/browser/notifications/platform_notification_service_impl.h" | 25 #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/browser/profiles/profile_manager.h" | 27 #include "chrome/browser/profiles/profile_manager.h" |
| 27 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" | 28 #include "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" |
| 29 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" |
| 28 #import "chrome/browser/ui/cocoa/notifications/notification_delivery.h" | 30 #import "chrome/browser/ui/cocoa/notifications/notification_delivery.h" |
| 29 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" | |
| 30 #import "chrome/browser/ui/cocoa/notifications/notification_response_builder_mac
.h" | 31 #import "chrome/browser/ui/cocoa/notifications/notification_response_builder_mac
.h" |
| 31 #include "chrome/common/features.h" | 32 #include "chrome/common/features.h" |
| 32 #include "chrome/grit/generated_resources.h" | 33 #include "chrome/grit/generated_resources.h" |
| 34 #include "components/crash/content/app/crashpad.h" |
| 33 #include "components/url_formatter/elide_url.h" | 35 #include "components/url_formatter/elide_url.h" |
| 34 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onConstants.h" | 36 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onConstants.h" |
| 37 #include "third_party/crashpad/crashpad/client/crashpad_client.h" |
| 35 #include "ui/base/l10n/l10n_util_mac.h" | 38 #include "ui/base/l10n/l10n_util_mac.h" |
| 36 #include "url/gurl.h" | 39 #include "url/gurl.h" |
| 37 #include "url/origin.h" | 40 #include "url/origin.h" |
| 38 | 41 |
| 39 @class NSUserNotification; | 42 @class NSUserNotification; |
| 40 @class NSUserNotificationCenter; | 43 @class NSUserNotificationCenter; |
| 41 | 44 |
| 42 // The mapping from web notifications to NsUserNotification works as follows | 45 // The mapping from web notifications to NsUserNotification works as follows |
| 43 | 46 |
| 44 // notification#title in NSUserNotification.title | 47 // notification#title in NSUserNotification.title |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 shouldPresentNotification:(NSUserNotification*)nsNotification { | 424 shouldPresentNotification:(NSUserNotification*)nsNotification { |
| 422 // Always display notifications, regardless of whether the app is foreground. | 425 // Always display notifications, regardless of whether the app is foreground. |
| 423 return YES; | 426 return YES; |
| 424 } | 427 } |
| 425 | 428 |
| 426 @end | 429 @end |
| 427 | 430 |
| 428 @implementation AlertDispatcherImpl { | 431 @implementation AlertDispatcherImpl { |
| 429 // The connection to the XPC server in charge of delivering alerts. | 432 // The connection to the XPC server in charge of delivering alerts. |
| 430 base::scoped_nsobject<NSXPCConnection> xpcConnection_; | 433 base::scoped_nsobject<NSXPCConnection> xpcConnection_; |
| 434 |
| 435 // YES if the remote object has had |-setMachExceptionPort:| called |
| 436 // since the service was last started, interrupted, or invalidated. |
| 437 // If NO, then -serviceProxy will set the exception port. |
| 438 BOOL setExceptionPort_; |
| 431 } | 439 } |
| 432 | 440 |
| 433 - (instancetype)init { | 441 - (instancetype)init { |
| 434 if ((self = [super init])) { | 442 if ((self = [super init])) { |
| 435 xpcConnection_.reset([[NSXPCConnection alloc] | 443 xpcConnection_.reset([[NSXPCConnection alloc] |
| 436 initWithServiceName: | 444 initWithServiceName: |
| 437 [NSString | 445 [NSString |
| 438 stringWithFormat:notification_constants::kAlertXPCServiceName, | 446 stringWithFormat:notification_constants::kAlertXPCServiceName, |
| 439 [base::mac::FrameworkBundle() | 447 [base::mac::FrameworkBundle() |
| 440 bundleIdentifier]]]); | 448 bundleIdentifier]]]); |
| 441 xpcConnection_.get().remoteObjectInterface = | 449 xpcConnection_.get().remoteObjectInterface = |
| 442 [NSXPCInterface interfaceWithProtocol:@protocol(NotificationDelivery)]; | 450 [NSXPCInterface interfaceWithProtocol:@protocol(NotificationDelivery)]; |
| 443 | 451 |
| 444 xpcConnection_.get().interruptionHandler = ^{ | 452 xpcConnection_.get().interruptionHandler = ^{ |
| 445 LOG(WARNING) << "connection interrupted: interruptionHandler: "; | 453 LOG(WARNING) << "connection interrupted: interruptionHandler: "; |
| 454 setExceptionPort_ = NO; |
| 446 // TODO(miguelg): perhaps add some UMA here. | 455 // TODO(miguelg): perhaps add some UMA here. |
| 447 // We will be getting this handler both when the XPC server crashes or | 456 // We will be getting this handler both when the XPC server crashes or |
| 448 // when it decides to close the connection. | 457 // when it decides to close the connection. |
| 449 }; | 458 }; |
| 450 xpcConnection_.get().invalidationHandler = ^{ | 459 xpcConnection_.get().invalidationHandler = ^{ |
| 451 LOG(WARNING) << "connection invalidationHandler received"; | 460 LOG(WARNING) << "connection invalidationHandler received"; |
| 461 setExceptionPort_ = NO; |
| 452 // This means that the connection should be recreated if it needs | 462 // This means that the connection should be recreated if it needs |
| 453 // to be used again. It should not really happen. | 463 // to be used again. It should not really happen. |
| 454 DCHECK(false) << "XPC Connection invalidated"; | 464 DCHECK(false) << "XPC Connection invalidated"; |
| 455 }; | 465 }; |
| 456 | 466 |
| 457 xpcConnection_.get().exportedInterface = | 467 xpcConnection_.get().exportedInterface = |
| 458 [NSXPCInterface interfaceWithProtocol:@protocol(NotificationReply)]; | 468 [NSXPCInterface interfaceWithProtocol:@protocol(NotificationReply)]; |
| 459 xpcConnection_.get().exportedObject = self; | 469 xpcConnection_.get().exportedObject = self; |
| 460 [xpcConnection_ resume]; | 470 [xpcConnection_ resume]; |
| 461 } | 471 } |
| 462 | 472 |
| 463 return self; | 473 return self; |
| 464 } | 474 } |
| 465 | 475 |
| 466 - (void)dispatchNotification:(NSDictionary*)data { | 476 - (void)dispatchNotification:(NSDictionary*)data { |
| 467 [[xpcConnection_ remoteObjectProxy] deliverNotification:data]; | 477 [[self serviceProxy] deliverNotification:data]; |
| 468 } | 478 } |
| 469 | 479 |
| 470 - (void)closeNotificationWithId:(NSString*)notificationId | 480 - (void)closeNotificationWithId:(NSString*)notificationId |
| 471 withProfileId:(NSString*)profileId { | 481 withProfileId:(NSString*)profileId { |
| 472 [[xpcConnection_ remoteObjectProxy] closeNotificationWithId:notificationId | 482 [[self serviceProxy] closeNotificationWithId:notificationId |
| 473 withProfileId:profileId]; | 483 withProfileId:profileId]; |
| 474 } | 484 } |
| 475 | 485 |
| 476 - (void)closeAllNotifications { | 486 - (void)closeAllNotifications { |
| 477 [[xpcConnection_ remoteObjectProxy] closeAllNotifications]; | 487 [[self serviceProxy] closeAllNotifications]; |
| 478 } | 488 } |
| 479 | 489 |
| 480 // NotificationReply implementation | 490 // NotificationReply: |
| 491 |
| 481 - (void)notificationClick:(NSDictionary*)notificationResponseData { | 492 - (void)notificationClick:(NSDictionary*)notificationResponseData { |
| 482 NotificationPlatformBridgeMac::ProcessNotificationResponse( | 493 NotificationPlatformBridgeMac::ProcessNotificationResponse( |
| 483 notificationResponseData); | 494 notificationResponseData); |
| 484 } | 495 } |
| 485 | 496 |
| 497 // Private methods: |
| 498 |
| 499 // Retrieves the connection's remoteObjectProxy. Always use this as opposed |
| 500 // to going directly through the connection, since this will ensure that the |
| 501 // service has its exception port configured for crash reporting. |
| 502 - (id<NotificationDelivery>)serviceProxy { |
| 503 id<NotificationDelivery> proxy = [xpcConnection_ remoteObjectProxy]; |
| 504 |
| 505 if (!setExceptionPort_) { |
| 506 base::mac::ScopedMachSendRight exceptionPort( |
| 507 crash_reporter::GetCrashpadClient().GetHandlerMachPort()); |
| 508 base::scoped_nsobject<CrXPCMachPort> xpcPort( |
| 509 [[CrXPCMachPort alloc] initWithMachSendRight:std::move(exceptionPort)]); |
| 510 [proxy setMachExceptionPort:xpcPort]; |
| 511 setExceptionPort_ = YES; |
| 512 } |
| 513 |
| 514 return proxy; |
| 515 } |
| 516 |
| 486 @end | 517 @end |
| OLD | NEW |