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" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 ProfileManager* profileManager = g_browser_process->profile_manager(); | 93 ProfileManager* profileManager = g_browser_process->profile_manager(); |
94 DCHECK(profileManager); | 94 DCHECK(profileManager); |
95 | 95 |
96 profileManager->LoadProfile( | 96 profileManager->LoadProfile( |
97 profile_id, incognito, base::Bind(&ProfileLoadedCallback, operation, type, | 97 profile_id, incognito, base::Bind(&ProfileLoadedCallback, operation, type, |
98 origin, notification_id, button_index)); | 98 origin, notification_id, button_index)); |
99 } | 99 } |
100 | 100 |
101 } // namespace | 101 } // namespace |
102 | 102 |
103 // static | |
104 NotificationPlatformBridge* NotificationPlatformBridge::Create() { | |
105 return new NotificationPlatformBridgeMac( | |
106 [NSUserNotificationCenter defaultUserNotificationCenter]); | |
107 } | |
108 | |
109 // A Cocoa class that represents the delegate of NSUserNotificationCenter and | 103 // A Cocoa class that represents the delegate of NSUserNotificationCenter and |
110 // can forward commands to C++. | 104 // can forward commands to C++. |
111 @interface NotificationCenterDelegate | 105 @interface NotificationCenterDelegate |
112 : NSObject<NSUserNotificationCenterDelegate> { | 106 : NSObject<NSUserNotificationCenterDelegate> { |
113 } | 107 } |
114 @end | 108 @end |
115 | 109 |
116 // Interface to communicate with the Alert XPC service. | 110 // Interface to communicate with the Alert XPC service. |
117 @interface NotificationRemoteDispatcher : NSObject | 111 @interface AlertDispatcherImpl : NSObject<AlertDispatcher> |
118 | 112 |
119 // Deliver a notification to the XPC service to be displayed as an alert. | 113 // Deliver a notification to the XPC service to be displayed as an alert. |
120 - (void)dispatchNotification:(NSDictionary*)data; | 114 - (void)dispatchNotification:(NSDictionary*)data; |
121 | 115 |
122 // Close a notification for a given |notificationId| and |profileId|. | 116 // Close a notification for a given |notificationId| and |profileId|. |
123 - (void)closeNotificationWithId:(NSString*)notificationId | 117 - (void)closeNotificationWithId:(NSString*)notificationId |
124 withProfileId:(NSString*)profileId; | 118 withProfileId:(NSString*)profileId; |
125 | 119 |
126 // Close all notifications. | 120 // Close all notifications. |
127 - (void)closeAllNotifications; | 121 - (void)closeAllNotifications; |
128 | 122 |
129 @end | 123 @end |
130 | 124 |
131 // ///////////////////////////////////////////////////////////////////////////// | 125 // ///////////////////////////////////////////////////////////////////////////// |
132 | |
133 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac( | 126 NotificationPlatformBridgeMac::NotificationPlatformBridgeMac( |
134 NSUserNotificationCenter* notification_center) | 127 NSUserNotificationCenter* notification_center, |
| 128 id<AlertDispatcher> alert_dispatcher) |
135 : delegate_([NotificationCenterDelegate alloc]), | 129 : delegate_([NotificationCenterDelegate alloc]), |
136 notification_center_(notification_center), | 130 notification_center_([notification_center retain]), |
137 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 131 alert_dispatcher_([alert_dispatcher retain]) { |
138 notification_remote_dispatcher_( | |
139 [[NotificationRemoteDispatcher alloc] init]) | |
140 #else | |
141 notification_remote_dispatcher_(nullptr) | |
142 #endif // ENABLE_XPC_NOTIFICATIONS | |
143 { | |
144 [notification_center_ setDelegate:delegate_.get()]; | 132 [notification_center_ setDelegate:delegate_.get()]; |
145 } | 133 } |
146 | 134 |
147 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { | 135 NotificationPlatformBridgeMac::~NotificationPlatformBridgeMac() { |
148 [notification_center_ setDelegate:nil]; | 136 [notification_center_ setDelegate:nil]; |
149 | 137 |
150 // TODO(miguelg) do not remove banners if possible. | 138 // TODO(miguelg) do not remove banners if possible. |
151 [notification_center_ removeAllDeliveredNotifications]; | 139 [notification_center_ removeAllDeliveredNotifications]; |
152 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 140 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
153 [notification_remote_dispatcher_ closeAllNotifications]; | 141 [alert_dispatcher_ closeAllNotifications]; |
154 #endif // BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 142 #endif // BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
155 } | 143 } |
156 | 144 |
| 145 // static |
| 146 NotificationPlatformBridge* NotificationPlatformBridge::Create() { |
| 147 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
| 148 base::scoped_nsobject<AlertDispatcherImpl> alert_dispatcher( |
| 149 [[AlertDispatcherImpl alloc] init]); |
| 150 return new NotificationPlatformBridgeMac( |
| 151 [NSUserNotificationCenter defaultUserNotificationCenter], |
| 152 alert_dispatcher.get()); |
| 153 #else |
| 154 return new NotificationPlatformBridgeMac( |
| 155 [NSUserNotificationCenter defaultUserNotificationCenter], nil); |
| 156 #endif // ENABLE_XPC_NOTIFICATIONS |
| 157 } |
| 158 |
157 void NotificationPlatformBridgeMac::Display( | 159 void NotificationPlatformBridgeMac::Display( |
158 NotificationCommon::Type notification_type, | 160 NotificationCommon::Type notification_type, |
159 const std::string& notification_id, | 161 const std::string& notification_id, |
160 const std::string& profile_id, | 162 const std::string& profile_id, |
161 bool incognito, | 163 bool incognito, |
162 const Notification& notification) { | 164 const Notification& notification) { |
163 base::scoped_nsobject<NotificationBuilder> builder( | 165 base::scoped_nsobject<NotificationBuilder> builder( |
164 [[NotificationBuilder alloc] | 166 [[NotificationBuilder alloc] |
165 initWithCloseLabel:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_CLOSE) | 167 initWithCloseLabel:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_CLOSE) |
166 optionsLabel:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_OPTIONS) | 168 optionsLabel:l10n_util::GetNSString(IDS_NOTIFICATION_BUTTON_OPTIONS) |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 [builder setProfileId:base::SysUTF8ToNSString(profile_id)]; | 224 [builder setProfileId:base::SysUTF8ToNSString(profile_id)]; |
223 [builder setIncognito:incognito]; | 225 [builder setIncognito:incognito]; |
224 [builder setNotificationType:[NSNumber numberWithInteger:notification_type]]; | 226 [builder setNotificationType:[NSNumber numberWithInteger:notification_type]]; |
225 | 227 |
226 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 228 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
227 // Send persistent notifications to the XPC service so they | 229 // Send persistent notifications to the XPC service so they |
228 // can be displayed as alerts. Chrome itself can only display | 230 // can be displayed as alerts. Chrome itself can only display |
229 // banners. | 231 // banners. |
230 if (notification.never_timeout()) { | 232 if (notification.never_timeout()) { |
231 NSDictionary* dict = [builder buildDictionary]; | 233 NSDictionary* dict = [builder buildDictionary]; |
232 [notification_remote_dispatcher_ dispatchNotification:dict]; | 234 [alert_dispatcher_ dispatchNotification:dict]; |
233 } else { | 235 } else { |
234 NSUserNotification* toast = [builder buildUserNotification]; | 236 NSUserNotification* toast = [builder buildUserNotification]; |
235 [notification_center_ deliverNotification:toast]; | 237 [notification_center_ deliverNotification:toast]; |
236 } | 238 } |
237 #else | 239 #else |
238 NSUserNotification* toast = [builder buildUserNotification]; | 240 NSUserNotification* toast = [builder buildUserNotification]; |
239 [notification_center_ deliverNotification:toast]; | 241 [notification_center_ deliverNotification:toast]; |
240 #endif // ENABLE_XPC_NOTIFICATIONS | 242 #endif // ENABLE_XPC_NOTIFICATIONS |
241 } | 243 } |
242 | 244 |
(...skipping 15 matching lines...) Expand all Loading... |
258 [persistent_profile_id isEqualToString:current_profile_id]) { | 260 [persistent_profile_id isEqualToString:current_profile_id]) { |
259 [notification_center_ removeDeliveredNotification:toast]; | 261 [notification_center_ removeDeliveredNotification:toast]; |
260 notification_removed = true; | 262 notification_removed = true; |
261 break; | 263 break; |
262 } | 264 } |
263 } | 265 } |
264 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) | 266 #if BUILDFLAG(ENABLE_XPC_NOTIFICATIONS) |
265 // If no banner existed with that ID try to see if there is an alert | 267 // If no banner existed with that ID try to see if there is an alert |
266 // in the xpc server. | 268 // in the xpc server. |
267 if (!notification_removed) { | 269 if (!notification_removed) { |
268 [notification_remote_dispatcher_ | 270 [alert_dispatcher_ closeNotificationWithId:candidate_id |
269 closeNotificationWithId:candidate_id | 271 withProfileId:current_profile_id]; |
270 withProfileId:current_profile_id]; | |
271 } | 272 } |
272 #endif // ENABLE_XPC_NOTIFICATIONS | 273 #endif // ENABLE_XPC_NOTIFICATIONS |
273 } | 274 } |
274 | 275 |
275 bool NotificationPlatformBridgeMac::GetDisplayed( | 276 bool NotificationPlatformBridgeMac::GetDisplayed( |
276 const std::string& profile_id, | 277 const std::string& profile_id, |
277 bool incognito, | 278 bool incognito, |
278 std::set<std::string>* notifications) const { | 279 std::set<std::string>* notifications) const { |
279 DCHECK(notifications); | 280 DCHECK(notifications); |
280 | 281 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 } | 418 } |
418 | 419 |
419 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center | 420 - (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center |
420 shouldPresentNotification:(NSUserNotification*)nsNotification { | 421 shouldPresentNotification:(NSUserNotification*)nsNotification { |
421 // Always display notifications, regardless of whether the app is foreground. | 422 // Always display notifications, regardless of whether the app is foreground. |
422 return YES; | 423 return YES; |
423 } | 424 } |
424 | 425 |
425 @end | 426 @end |
426 | 427 |
427 @implementation NotificationRemoteDispatcher { | 428 @implementation AlertDispatcherImpl { |
428 // The connection to the XPC server in charge of delivering alerts. | 429 // The connection to the XPC server in charge of delivering alerts. |
429 base::scoped_nsobject<NSXPCConnection> xpcConnection_; | 430 base::scoped_nsobject<NSXPCConnection> xpcConnection_; |
430 } | 431 } |
431 | 432 |
432 - (instancetype)init { | 433 - (instancetype)init { |
433 if ((self = [super init])) { | 434 if ((self = [super init])) { |
434 xpcConnection_.reset([[NSXPCConnection alloc] | 435 xpcConnection_.reset([[NSXPCConnection alloc] |
435 initWithServiceName: | 436 initWithServiceName: |
436 [NSString | 437 [NSString |
437 stringWithFormat:notification_constants::kAlertXPCServiceName, | 438 stringWithFormat:notification_constants::kAlertXPCServiceName, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 [[xpcConnection_ remoteObjectProxy] closeAllNotifications]; | 476 [[xpcConnection_ remoteObjectProxy] closeAllNotifications]; |
476 } | 477 } |
477 | 478 |
478 // NotificationReply implementation | 479 // NotificationReply implementation |
479 - (void)notificationClick:(NSDictionary*)notificationResponseData { | 480 - (void)notificationClick:(NSDictionary*)notificationResponseData { |
480 NotificationPlatformBridgeMac::ProcessNotificationResponse( | 481 NotificationPlatformBridgeMac::ProcessNotificationResponse( |
481 notificationResponseData); | 482 notificationResponseData); |
482 } | 483 } |
483 | 484 |
484 @end | 485 @end |
OLD | NEW |