Chromium Code Reviews| Index: chrome/browser/notifications/notification_ui_manager_mac.mm |
| diff --git a/chrome/browser/notifications/notification_ui_manager_mac.mm b/chrome/browser/notifications/notification_ui_manager_mac.mm |
| index 74023f7ab5a65b090c1cc9c51b39b7b7c671b489..72aaac0f3d3af5835b6d6510eb202f3c7a904f13 100644 |
| --- a/chrome/browser/notifications/notification_ui_manager_mac.mm |
| +++ b/chrome/browser/notifications/notification_ui_manager_mac.mm |
| @@ -11,6 +11,7 @@ |
| #include "base/mac/mac_util.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/sys_string_conversions.h" |
| +#include "chrome/browser/notifications/message_center_notification_display_service.h" |
| #include "chrome/browser/notifications/notification.h" |
| #include "chrome/browser/notifications/persistent_notification_delegate.h" |
| #include "chrome/browser/notifications/platform_notification_service_impl.h" |
| @@ -46,29 +47,28 @@ namespace { |
| // native ones. |
| NSString* const kNotificationOriginKey = @"notification_origin"; |
| NSString* const kNotificationPersistentIdKey = @"notification_persistent_id"; |
| -NSString* const kNotificationDelegateIdKey = @"notification_delegate_id"; |
| -// TODO(miguelg) get rid of this key once ProfileID has been ported |
| -// from the void* it is today to the stable identifier provided |
| -// in kNotificationProfilePersistentIdKey. |
| -NSString* const kNotificationProfileIdKey = @"notification_profile_id"; |
| NSString* const kNotificationProfilePersistentIdKey = |
| @"notification_profile_persistent_id"; |
| NSString* const kNotificationIncognitoKey = @"notification_incognito"; |
| } // namespace |
| -// Only use native notifications for web, behind a flag and on 10.8+ |
| +#if defined(OS_MACOSX) |
|
Peter Beverloo
2016/03/21 15:57:04
When is this ever not true?
|
| +// Only use native notifications for web, behind a flag |
| // static |
| -NotificationUIManager* |
| -NotificationUIManager::CreateNativeNotificationManager() { |
| +NotificationDisplayService* NotificationDisplayService::Create( |
| + Profile* profile) { |
| if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| - switches::kEnableNativeNotifications) && |
| - base::mac::IsOSMountainLionOrLater()) { |
| - return new NotificationUIManagerMac(); |
| + switches::kEnableNativeNotifications)) { |
| + return new NotificationUIManagerMac( |
| + profile, [NSUserNotificationCenter defaultUserNotificationCenter]); |
| } |
| - return nullptr; |
| + |
| + // Fall back to the MessageCenter backed Display Service. |
| + return new MessageCenterNotificationDisplayService(profile); |
| } |
| +#endif |
| // A Cocoa class that represents the delegate of NSUserNotificationCenter and |
| // can forward commands to C++. |
| @@ -82,19 +82,21 @@ NotificationUIManager::CreateNativeNotificationManager() { |
| // ///////////////////////////////////////////////////////////////////////////// |
| -NotificationUIManagerMac::NotificationUIManagerMac() |
| - : delegate_([[NotificationCenterDelegate alloc] initWithManager:this]) { |
| - [[NSUserNotificationCenter defaultUserNotificationCenter] |
| - setDelegate:delegate_.get()]; |
| +NotificationUIManagerMac::NotificationUIManagerMac( |
| + Profile* profile, |
| + NSUserNotificationCenter* notification_center) |
| + : NotificationDisplayService(profile), |
| + delegate_([[NotificationCenterDelegate alloc] initWithManager:this]), |
| + notification_center_(notification_center) { |
| + [notification_center_ setDelegate:delegate_.get()]; |
| } |
| NotificationUIManagerMac::~NotificationUIManagerMac() { |
| - [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:nil]; |
| - CancelAll(); |
| + [notification_center_ setDelegate:nil]; |
| + [notification_center_ removeAllDeliveredNotifications]; |
| } |
| -void NotificationUIManagerMac::Add(const Notification& notification, |
| - Profile* profile) { |
| +void NotificationUIManagerMac::Display(const Notification& notification) { |
| // The Mac notification UI manager only supports Web Notifications, which |
| // have a PersistentNotificationDelegate. The persistent id of the |
| // notification is exposed through it's interface. |
| @@ -109,13 +111,13 @@ void NotificationUIManagerMac::Add(const Notification& notification, |
| // TODO(miguelg): try to elide the origin perhaps See NSString |
| // stringWithFormat. It seems that the informativeText font is constant. |
| - NSString* informativeText = |
| + NSString* informative_text = |
| notification.context_message().empty() |
| ? base::SysUTF8ToNSString(notification.origin_url().spec()) |
| : base::SysUTF16ToNSString(notification.context_message()); |
| - [toast setInformativeText:informativeText]; |
| + [toast setInformativeText:informative_text]; |
| - // Some functionality is only available in 10.9+ or requires private APIs |
| + // Some functionality requires private APIs |
| // Icon |
| if ([toast respondsToSelector:@selector(_identityImage)] && |
| !notification.icon().IsEmpty()) { |
| @@ -186,56 +188,40 @@ void NotificationUIManagerMac::Add(const Notification& notification, |
| } |
| } |
| - int64_t persistent_notification_id = delegate->persistent_notification_id(); |
| - int64_t profile_id = reinterpret_cast<int64_t>(GetProfileID(profile)); |
| + std::string persistent_notification_id = |
| + base::Int64ToString(delegate->persistent_notification_id()); |
| toast.get().userInfo = @{ |
| kNotificationOriginKey : |
| base::SysUTF8ToNSString(notification.origin_url().spec()), |
| kNotificationPersistentIdKey : |
| - [NSNumber numberWithLongLong:persistent_notification_id], |
| - kNotificationDelegateIdKey : |
| - base::SysUTF8ToNSString(notification.delegate_id()), |
| - kNotificationProfileIdKey : [NSNumber numberWithLongLong:profile_id], |
| + base::SysUTF8ToNSString(persistent_notification_id), |
| kNotificationProfilePersistentIdKey : |
| - base::SysUTF8ToNSString(profile->GetPath().BaseName().value()), |
| + base::SysUTF8ToNSString(profile_->GetPath().BaseName().value()), |
| kNotificationIncognitoKey : |
| - [NSNumber numberWithBool:profile->IsOffTheRecord()] |
| + [NSNumber numberWithBool:profile_->IsOffTheRecord()] |
| }; |
| - [[NSUserNotificationCenter defaultUserNotificationCenter] |
| - deliverNotification:toast]; |
| -} |
| - |
| -bool NotificationUIManagerMac::Update(const Notification& notification, |
| - Profile* profile) { |
| - NOTREACHED(); |
| - return false; |
| -} |
| - |
| -const Notification* NotificationUIManagerMac::FindById( |
| - const std::string& delegate_id, |
| - ProfileID profile_id) const { |
| - NOTREACHED(); |
| - return nil; |
| + [notification_center_ deliverNotification:toast]; |
| } |
| -bool NotificationUIManagerMac::CancelById(const std::string& delegate_id, |
| - ProfileID profile_id) { |
| - int64_t persistent_notification_id = 0; |
| - // TODO(peter): Use the |delegate_id| directly when notification ids are being |
| - // generated by content/ instead of us. |
| - if (!base::StringToInt64(delegate_id, &persistent_notification_id)) |
| - return false; |
| +bool NotificationUIManagerMac::Close( |
| + const std::string& persistent_notification_id) { |
| + NSString* candidate_id = base::SysUTF8ToNSString(persistent_notification_id); |
| - NSUserNotificationCenter* notificationCenter = |
| - [NSUserNotificationCenter defaultUserNotificationCenter]; |
| + NSString* current_profile_id = |
| + base::SysUTF8ToNSString(profile_->GetPath().BaseName().value()); |
| for (NSUserNotification* toast in |
| - [notificationCenter deliveredNotifications]) { |
| - NSNumber* toast_id = |
| + [notification_center_ deliveredNotifications]) { |
| + NSString* toast_id = |
| [toast.userInfo objectForKey:kNotificationPersistentIdKey]; |
| - if (toast_id.longLongValue == persistent_notification_id) { |
| - [notificationCenter removeDeliveredNotification:toast]; |
| + |
| + NSString* persistent_profile_id = |
| + [toast.userInfo objectForKey:kNotificationProfilePersistentIdKey]; |
| + |
| + if (toast_id == candidate_id && |
| + persistent_profile_id == current_profile_id) { |
| + [notification_center_ removeDeliveredNotification:toast]; |
| return true; |
| } |
| } |
| @@ -243,49 +229,25 @@ bool NotificationUIManagerMac::CancelById(const std::string& delegate_id, |
| return false; |
| } |
| -std::set<std::string> |
| -NotificationUIManagerMac::GetAllIdsByProfileAndSourceOrigin( |
| - ProfileID profile_id, |
| - const GURL& source) { |
| - NOTREACHED(); |
| - return std::set<std::string>(); |
| -} |
| - |
| -std::set<std::string> NotificationUIManagerMac::GetAllIdsByProfile( |
| - ProfileID profile_id) { |
| - // ProfileID in mac is not safe to use across browser restarts |
| - // Therefore because when chrome quits we cancel all pending notifications. |
| - // TODO(miguelg) get rid of ProfileID as a void* for native notifications. |
| - std::set<std::string> delegate_ids; |
| - NSUserNotificationCenter* notificationCenter = |
| - [NSUserNotificationCenter defaultUserNotificationCenter]; |
| +std::set<std::string> NotificationUIManagerMac::GetDisplayed() const { |
| + // For now, when chrome quits we cancel all pending notifications. |
| + std::set<std::string> notification_ids; |
| + NSString* current_profile_id = |
| + base::SysUTF8ToNSString(profile_->GetPath().BaseName().value()); |
| for (NSUserNotification* toast in |
| - [notificationCenter deliveredNotifications]) { |
| - NSNumber* toast_profile_id = |
| - [toast.userInfo objectForKey:kNotificationProfileIdKey]; |
| - if (toast_profile_id.longLongValue == |
| - reinterpret_cast<int64_t>(profile_id)) { |
| - delegate_ids.insert(base::SysNSStringToUTF8( |
| - [toast.userInfo objectForKey:kNotificationDelegateIdKey])); |
| + [notification_center_ deliveredNotifications]) { |
| + NSString* toast_profile_id = |
| + [toast.userInfo objectForKey:kNotificationProfilePersistentIdKey]; |
| + if (toast_profile_id == current_profile_id) { |
| + notification_ids.insert(base::SysNSStringToUTF8( |
| + [toast.userInfo objectForKey:kNotificationPersistentIdKey])); |
| } |
| } |
| - return delegate_ids; |
| -} |
| - |
| -bool NotificationUIManagerMac::CancelAllBySourceOrigin( |
| - const GURL& source_origin) { |
| - NOTREACHED(); |
| - return false; |
| -} |
| - |
| -bool NotificationUIManagerMac::CancelAllByProfile(ProfileID profile_id) { |
| - NOTREACHED(); |
| - return false; |
| + return notification_ids; |
| } |
| -void NotificationUIManagerMac::CancelAll() { |
| - [[NSUserNotificationCenter defaultUserNotificationCenter] |
| - removeAllDeliveredNotifications]; |
| +bool NotificationUIManagerMac::SupportsNotificationCenter() const { |
| + return true; |
| } |
| // ///////////////////////////////////////////////////////////////////////////// |