2013-03-29 13:36:07 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Copyright (c) 2013 Adam Roben <adam@roben.org>. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE-CHROMIUM file.
|
|
|
|
|
2013-03-28 22:03:58 +00:00
|
|
|
#import "browser/notification_presenter_mac.h"
|
2013-03-28 21:17:46 +00:00
|
|
|
|
2014-07-28 03:52:26 +00:00
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/stl_util.h"
|
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2015-03-09 02:37:13 +00:00
|
|
|
#include "content/public/common/platform_notification_data.h"
|
2014-07-28 03:52:26 +00:00
|
|
|
#include "content/public/browser/desktop_notification_delegate.h"
|
2013-03-28 21:17:46 +00:00
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
2014-07-28 03:52:26 +00:00
|
|
|
@interface BRYUserNotificationCenterDelegate : NSObject<NSUserNotificationCenterDelegate> {
|
|
|
|
@private
|
|
|
|
brightray::NotificationPresenterMac* presenter_;
|
|
|
|
}
|
|
|
|
- (instancetype)initWithNotificationPresenter:(brightray::NotificationPresenterMac*)presenter;
|
2013-03-28 22:08:34 +00:00
|
|
|
@end
|
|
|
|
|
2013-03-28 21:17:46 +00:00
|
|
|
namespace brightray {
|
|
|
|
|
2013-03-29 12:32:00 +00:00
|
|
|
namespace {
|
|
|
|
|
2014-07-28 03:52:26 +00:00
|
|
|
} // namespace
|
2013-03-29 12:32:00 +00:00
|
|
|
|
2013-03-28 22:03:58 +00:00
|
|
|
NotificationPresenter* NotificationPresenter::Create() {
|
|
|
|
return new NotificationPresenterMac;
|
|
|
|
}
|
|
|
|
|
2013-03-28 22:08:34 +00:00
|
|
|
NotificationPresenterMac::NotificationPresenterMac()
|
2014-07-28 03:52:26 +00:00
|
|
|
: delegate_([[BRYUserNotificationCenterDelegate alloc] initWithNotificationPresenter:this]) {
|
2013-03-28 22:08:34 +00:00
|
|
|
NSUserNotificationCenter.defaultUserNotificationCenter.delegate = delegate_;
|
2013-03-28 22:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationPresenterMac::~NotificationPresenterMac() {
|
2014-07-28 03:52:26 +00:00
|
|
|
NSUserNotificationCenter.defaultUserNotificationCenter.delegate = nil;
|
2013-03-28 22:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationPresenterMac::ShowNotification(
|
2015-03-09 02:37:13 +00:00
|
|
|
const content::PlatformNotificationData& data,
|
2014-10-11 08:38:27 +00:00
|
|
|
scoped_ptr<content::DesktopNotificationDelegate> delegate,
|
2014-07-28 03:52:26 +00:00
|
|
|
base::Closure* cancel_callback) {
|
|
|
|
auto notification = [[NSUserNotification alloc] init];
|
2015-03-09 02:37:13 +00:00
|
|
|
notification.title = base::SysUTF16ToNSString(data.title);
|
|
|
|
notification.informativeText = base::SysUTF16ToNSString(data.body);
|
2014-07-28 03:52:26 +00:00
|
|
|
|
2014-10-11 08:38:27 +00:00
|
|
|
notifications_map_[delegate.get()].reset(notification);
|
2013-03-28 21:17:46 +00:00
|
|
|
[NSUserNotificationCenter.defaultUserNotificationCenter deliverNotification:notification];
|
2014-07-28 03:52:26 +00:00
|
|
|
|
|
|
|
if (cancel_callback)
|
|
|
|
*cancel_callback = base::Bind(
|
|
|
|
&NotificationPresenterMac::CancelNotification,
|
|
|
|
base::Unretained(this),
|
2014-10-11 08:38:27 +00:00
|
|
|
delegate.release());
|
2013-03-28 21:17:46 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 03:52:26 +00:00
|
|
|
content::DesktopNotificationDelegate* NotificationPresenterMac::GetDelegateFromNotification(
|
|
|
|
NSUserNotification* notification) {
|
|
|
|
for (NotificationsMap::const_iterator it = notifications_map_.begin();
|
|
|
|
it != notifications_map_.end(); ++it)
|
|
|
|
if ([it->second isEqual:notification])
|
|
|
|
return it->first;
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-03-29 13:36:07 +00:00
|
|
|
|
2014-07-28 03:52:26 +00:00
|
|
|
void NotificationPresenterMac::RemoveNotification(content::DesktopNotificationDelegate* delegate) {
|
2014-10-11 08:38:27 +00:00
|
|
|
if (ContainsKey(notifications_map_, delegate)) {
|
|
|
|
delete delegate;
|
2014-07-28 03:52:26 +00:00
|
|
|
notifications_map_.erase(delegate);
|
2014-10-11 08:38:27 +00:00
|
|
|
}
|
2014-07-28 03:52:26 +00:00
|
|
|
}
|
2013-03-29 13:36:07 +00:00
|
|
|
|
2014-07-28 03:52:26 +00:00
|
|
|
void NotificationPresenterMac::CancelNotification(content::DesktopNotificationDelegate* delegate) {
|
|
|
|
if (!ContainsKey(notifications_map_, delegate))
|
|
|
|
return;
|
2013-03-29 13:36:07 +00:00
|
|
|
|
|
|
|
// Notifications in -deliveredNotifications aren't the same objects we passed to
|
|
|
|
// -deliverNotification:, but they will respond YES to -isEqual:.
|
2014-07-28 03:52:26 +00:00
|
|
|
auto notification = notifications_map_[delegate];
|
2013-03-29 13:36:07 +00:00
|
|
|
auto center = NSUserNotificationCenter.defaultUserNotificationCenter;
|
2014-07-28 03:52:26 +00:00
|
|
|
for (NSUserNotification* deliveredNotification in center.deliveredNotifications)
|
|
|
|
if ([notification isEqual:deliveredNotification]) {
|
|
|
|
[center removeDeliveredNotification:deliveredNotification];
|
|
|
|
delegate->NotificationClosed(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoveNotification(delegate);
|
2013-03-29 13:36:07 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 03:52:26 +00:00
|
|
|
} // namespace brightray
|
2013-03-28 22:08:34 +00:00
|
|
|
|
|
|
|
@implementation BRYUserNotificationCenterDelegate
|
|
|
|
|
2014-07-28 03:52:26 +00:00
|
|
|
- (instancetype)initWithNotificationPresenter:(brightray::NotificationPresenterMac*)presenter {
|
|
|
|
self = [super init];
|
|
|
|
if (!self)
|
|
|
|
return nil;
|
2013-03-29 12:59:21 +00:00
|
|
|
|
2014-07-28 03:52:26 +00:00
|
|
|
presenter_ = presenter;
|
|
|
|
return self;
|
2013-03-29 12:59:21 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 03:52:26 +00:00
|
|
|
- (void)userNotificationCenter:(NSUserNotificationCenter*)center
|
|
|
|
didDeliverNotification:(NSUserNotification*)notification {
|
|
|
|
auto delegate = presenter_->GetDelegateFromNotification(notification);
|
|
|
|
if (delegate)
|
|
|
|
delegate->NotificationDisplayed();
|
|
|
|
}
|
2013-03-29 13:02:40 +00:00
|
|
|
|
2014-07-28 03:52:26 +00:00
|
|
|
- (void)userNotificationCenter:(NSUserNotificationCenter*)center
|
|
|
|
didActivateNotification:(NSUserNotification *)notification {
|
|
|
|
auto delegate = presenter_->GetDelegateFromNotification(notification);
|
|
|
|
if (delegate) {
|
|
|
|
delegate->NotificationClick();
|
|
|
|
presenter_->RemoveNotification(delegate);
|
|
|
|
}
|
2013-03-29 13:02:40 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 03:52:26 +00:00
|
|
|
- (BOOL)userNotificationCenter:(NSUserNotificationCenter*)center
|
|
|
|
shouldPresentNotification:(NSUserNotification*)notification {
|
2013-03-28 22:08:34 +00:00
|
|
|
// Display notifications even if the app is active.
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|