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
|
|
|
|
2013-10-07 19:16:49 +00:00
|
|
|
#import "base/strings/stringprintf.h"
|
2013-03-28 21:17:46 +00:00
|
|
|
#import "base/strings/sys_string_conversions.h"
|
2013-03-28 21:20:06 +00:00
|
|
|
#import "content/public/browser/render_view_host.h"
|
2013-03-28 21:17:46 +00:00
|
|
|
#import "content/public/common/show_desktop_notification_params.h"
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
2013-03-28 22:08:34 +00:00
|
|
|
@interface BRYUserNotificationCenterDelegate : NSObject <NSUserNotificationCenterDelegate>
|
|
|
|
@end
|
|
|
|
|
2013-03-28 21:17:46 +00:00
|
|
|
namespace brightray {
|
|
|
|
|
2013-03-29 12:32:00 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-03-29 13:23:10 +00:00
|
|
|
NSString * const kRenderProcessIDKey = @"RenderProcessID";
|
|
|
|
NSString * const kRenderViewIDKey = @"RenderViewID";
|
|
|
|
NSString * const kNotificationIDKey = @"NotificationID";
|
|
|
|
|
2013-03-29 12:59:21 +00:00
|
|
|
struct NotificationID {
|
2013-03-29 13:23:10 +00:00
|
|
|
NotificationID(
|
|
|
|
int render_process_id,
|
2013-03-29 13:36:07 +00:00
|
|
|
int render_view_id,
|
|
|
|
int notification_id)
|
2013-03-29 13:23:10 +00:00
|
|
|
: render_process_id(render_process_id),
|
|
|
|
render_view_id(render_view_id),
|
2013-03-29 13:36:07 +00:00
|
|
|
notification_id(notification_id) {
|
2013-03-29 13:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationID(NSUserNotification* notification)
|
|
|
|
: render_process_id([[notification.userInfo objectForKey:kRenderProcessIDKey] intValue]),
|
|
|
|
render_view_id([[notification.userInfo objectForKey:kRenderViewIDKey] intValue]),
|
|
|
|
notification_id([[notification.userInfo objectForKey:kNotificationIDKey] intValue]) {
|
|
|
|
}
|
|
|
|
|
2013-03-29 13:36:07 +00:00
|
|
|
std::string GetID() {
|
|
|
|
return base::StringPrintf("%d:%d:%d", render_process_id, render_view_id, notification_id);
|
|
|
|
}
|
|
|
|
|
2013-03-29 13:23:10 +00:00
|
|
|
NSDictionary* GetUserInfo() {
|
|
|
|
return @{
|
|
|
|
kRenderProcessIDKey: @(render_process_id),
|
|
|
|
kRenderViewIDKey: @(render_view_id),
|
|
|
|
kNotificationIDKey: @(notification_id),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-03-29 12:59:21 +00:00
|
|
|
int render_process_id;
|
|
|
|
int render_view_id;
|
|
|
|
int notification_id;
|
|
|
|
};
|
|
|
|
|
2013-10-07 19:17:39 +00:00
|
|
|
base::scoped_nsobject<NSUserNotification> CreateUserNotification(
|
2013-03-29 12:32:00 +00:00
|
|
|
const content::ShowDesktopNotificationHostMsgParams& params,
|
|
|
|
int render_process_id,
|
|
|
|
int render_view_id) {
|
|
|
|
auto notification = [[NSUserNotification alloc] init];
|
|
|
|
notification.title = base::SysUTF16ToNSString(params.title);
|
|
|
|
notification.informativeText = base::SysUTF16ToNSString(params.body);
|
2013-03-29 13:36:07 +00:00
|
|
|
notification.userInfo = NotificationID(render_process_id, render_view_id, params.notification_id).GetUserInfo();
|
2013-03-29 12:59:21 +00:00
|
|
|
|
2013-10-07 19:17:39 +00:00
|
|
|
return base::scoped_nsobject<NSUserNotification>(notification);
|
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()
|
|
|
|
: delegate_([[BRYUserNotificationCenterDelegate alloc] init]) {
|
|
|
|
NSUserNotificationCenter.defaultUserNotificationCenter.delegate = delegate_;
|
2013-03-28 22:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationPresenterMac::~NotificationPresenterMac() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationPresenterMac::ShowNotification(
|
2013-03-28 21:17:46 +00:00
|
|
|
const content::ShowDesktopNotificationHostMsgParams& params,
|
|
|
|
int render_process_id,
|
2013-03-28 21:49:29 +00:00
|
|
|
int render_view_id) {
|
2013-03-29 12:32:00 +00:00
|
|
|
auto notification = CreateUserNotification(params, render_process_id, render_view_id);
|
2013-03-29 13:36:07 +00:00
|
|
|
notification_map_.insert(std::make_pair(NotificationID(notification).GetID(), notification));
|
2013-03-28 21:17:46 +00:00
|
|
|
[NSUserNotificationCenter.defaultUserNotificationCenter deliverNotification:notification];
|
|
|
|
}
|
|
|
|
|
2013-03-29 13:36:07 +00:00
|
|
|
void NotificationPresenterMac::CancelNotification(
|
|
|
|
int render_process_id,
|
|
|
|
int render_view_id,
|
|
|
|
int notification_id) {
|
|
|
|
auto found = notification_map_.find(NotificationID(render_process_id, render_view_id, notification_id).GetID());
|
|
|
|
if (found == notification_map_.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto notification = found->second;
|
|
|
|
|
|
|
|
notification_map_.erase(found);
|
|
|
|
|
|
|
|
// Notifications in -deliveredNotifications aren't the same objects we passed to
|
|
|
|
// -deliverNotification:, but they will respond YES to -isEqual:.
|
|
|
|
auto center = NSUserNotificationCenter.defaultUserNotificationCenter;
|
|
|
|
for (NSUserNotification* deliveredNotification in center.deliveredNotifications) {
|
|
|
|
if (![notification isEqual:deliveredNotification])
|
|
|
|
continue;
|
|
|
|
[center removeDeliveredNotification:deliveredNotification];
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationID ID(notification);
|
|
|
|
auto host = content::RenderViewHost::FromID(ID.render_process_id, ID.render_view_id);
|
|
|
|
if (!host)
|
|
|
|
return;
|
|
|
|
|
|
|
|
host->DesktopNotificationPostClose(ID.notification_id, false);
|
|
|
|
}
|
|
|
|
|
2013-03-28 21:17:46 +00:00
|
|
|
}
|
2013-03-28 22:08:34 +00:00
|
|
|
|
|
|
|
@implementation BRYUserNotificationCenterDelegate
|
|
|
|
|
2013-03-29 12:59:21 +00:00
|
|
|
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification {
|
2013-03-29 13:23:10 +00:00
|
|
|
brightray::NotificationID ID(notification);
|
2013-03-29 12:59:21 +00:00
|
|
|
|
|
|
|
auto host = content::RenderViewHost::FromID(ID.render_process_id, ID.render_view_id);
|
|
|
|
if (!host)
|
|
|
|
return;
|
|
|
|
|
|
|
|
host->DesktopNotificationPostDisplay(ID.notification_id);
|
|
|
|
}
|
|
|
|
|
2013-03-29 13:02:40 +00:00
|
|
|
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
|
2013-03-29 13:23:10 +00:00
|
|
|
brightray::NotificationID ID(notification);
|
2013-03-29 13:02:40 +00:00
|
|
|
|
|
|
|
auto host = content::RenderViewHost::FromID(ID.render_process_id, ID.render_view_id);
|
|
|
|
if (!host)
|
|
|
|
return;
|
|
|
|
|
|
|
|
host->DesktopNotificationPostClick(ID.notification_id);
|
|
|
|
}
|
|
|
|
|
2013-03-28 22:08:34 +00:00
|
|
|
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
|
|
|
|
// Display notifications even if the app is active.
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|