| 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/alert_notification_service.h" | 5 #import "chrome/browser/ui/cocoa/notifications/alert_notification_service.h" |
| 6 | 6 |
| 7 #include <unistd.h> |
| 8 |
| 7 #import "base/mac/scoped_nsobject.h" | 9 #import "base/mac/scoped_nsobject.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 8 #import "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" | 11 #import "chrome/browser/ui/cocoa/notifications/notification_builder_mac.h" |
| 9 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" | 12 #include "chrome/browser/ui/cocoa/notifications/notification_constants_mac.h" |
| 10 #import "chrome/browser/ui/cocoa/notifications/xpc_transaction_handler.h" | 13 #import "chrome/browser/ui/cocoa/notifications/xpc_transaction_handler.h" |
| 11 #include "third_party/crashpad/crashpad/client/crashpad_client.h" | 14 #include "third_party/crashpad/crashpad/client/crashpad_client.h" |
| 15 #include "third_party/crashpad/crashpad/client/crashpad_info.h" |
| 16 #include "third_party/crashpad/crashpad/client/simple_string_dictionary.h" |
| 12 | 17 |
| 13 @class NSUserNotificationCenter; | 18 @class NSUserNotificationCenter; |
| 14 | 19 |
| 20 namespace { |
| 21 |
| 22 crashpad::SimpleStringDictionary* GetCrashpadAnnotations() { |
| 23 static crashpad::SimpleStringDictionary* annotations = []() { |
| 24 auto* annotations = new crashpad::SimpleStringDictionary(); |
| 25 annotations->SetKeyValue("ptype", "AlertNotificationService.xpc"); |
| 26 annotations->SetKeyValue("pid", base::IntToString(getpid()).c_str()); |
| 27 return annotations; |
| 28 }(); |
| 29 return annotations; |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
| 15 @implementation AlertNotificationService { | 34 @implementation AlertNotificationService { |
| 16 XPCTransactionHandler* transactionHandler_; | 35 XPCTransactionHandler* transactionHandler_; |
| 17 | 36 |
| 18 // Ensures that the XPC service has been configured for crash reporting. | 37 // Ensures that the XPC service has been configured for crash reporting. |
| 19 // Other messages should not be sent to a new instance of the service | 38 // Other messages should not be sent to a new instance of the service |
| 20 // before -setMachExceptionPort: is called. | 39 // before -setMachExceptionPort: is called. |
| 40 // Because XPC callouts occur on a concurrent dispatch queue, this must be |
| 41 // accessed in a @synchronized(self) block. |
| 21 BOOL didSetExceptionPort_; | 42 BOOL didSetExceptionPort_; |
| 22 } | 43 } |
| 23 | 44 |
| 24 - (instancetype)initWithTransactionHandler:(XPCTransactionHandler*)handler { | 45 - (instancetype)initWithTransactionHandler:(XPCTransactionHandler*)handler { |
| 25 if ((self = [super init])) { | 46 if ((self = [super init])) { |
| 26 transactionHandler_ = handler; | 47 transactionHandler_ = handler; |
| 27 } | 48 } |
| 28 return self; | 49 return self; |
| 29 } | 50 } |
| 30 | 51 |
| 31 - (void)setMachExceptionPort:(CrXPCMachPort*)port { | 52 - (void)setMachExceptionPort:(CrXPCMachPort*)port { |
| 32 base::mac::ScopedMachSendRight sendRight([port takeRight]); | 53 base::mac::ScopedMachSendRight sendRight([port takeRight]); |
| 33 if (!sendRight.is_valid()) { | 54 if (!sendRight.is_valid()) { |
| 34 NOTREACHED(); | 55 NOTREACHED(); |
| 35 return; | 56 return; |
| 36 } | 57 } |
| 37 | 58 |
| 38 crashpad::CrashpadClient client; | 59 @synchronized(self) { |
| 39 didSetExceptionPort_ = client.SetHandlerMachPort(std::move(sendRight)); | 60 if (didSetExceptionPort_) { |
| 40 DCHECK(didSetExceptionPort_); | 61 return; |
| 62 } |
| 63 |
| 64 crashpad::CrashpadClient client; |
| 65 didSetExceptionPort_ = client.SetHandlerMachPort(std::move(sendRight)); |
| 66 DCHECK(didSetExceptionPort_); |
| 67 |
| 68 crashpad::CrashpadInfo::GetCrashpadInfo()->set_simple_annotations( |
| 69 GetCrashpadAnnotations()); |
| 70 } |
| 41 } | 71 } |
| 42 | 72 |
| 43 - (void)deliverNotification:(NSDictionary*)notificationData { | 73 - (void)deliverNotification:(NSDictionary*)notificationData { |
| 44 DCHECK(didSetExceptionPort_); | 74 DCHECK(didSetExceptionPort_); |
| 45 | 75 |
| 46 base::scoped_nsobject<NotificationBuilder> builder( | 76 base::scoped_nsobject<NotificationBuilder> builder( |
| 47 [[NotificationBuilder alloc] initWithDictionary:notificationData]); | 77 [[NotificationBuilder alloc] initWithDictionary:notificationData]); |
| 48 | 78 |
| 49 NSUserNotification* toast = [builder buildUserNotification]; | 79 NSUserNotification* toast = [builder buildUserNotification]; |
| 50 [[NSUserNotificationCenter defaultUserNotificationCenter] | 80 [[NSUserNotificationCenter defaultUserNotificationCenter] |
| (...skipping 26 matching lines...) Expand all Loading... |
| 77 | 107 |
| 78 - (void)closeAllNotifications { | 108 - (void)closeAllNotifications { |
| 79 DCHECK(didSetExceptionPort_); | 109 DCHECK(didSetExceptionPort_); |
| 80 | 110 |
| 81 [[NSUserNotificationCenter defaultUserNotificationCenter] | 111 [[NSUserNotificationCenter defaultUserNotificationCenter] |
| 82 removeAllDeliveredNotifications]; | 112 removeAllDeliveredNotifications]; |
| 83 [transactionHandler_ closeTransactionIfNeeded]; | 113 [transactionHandler_ closeTransactionIfNeeded]; |
| 84 } | 114 } |
| 85 | 115 |
| 86 @end | 116 @end |
| OLD | NEW |