Implement Notification.close()
This commit is contained in:
parent
dde3c3f78f
commit
080d9f59e9
5 changed files with 70 additions and 4 deletions
|
@ -48,4 +48,11 @@ void BrowserClient::ShowDesktopNotification(
|
||||||
notification_presenter()->ShowNotification(params, render_process_id, render_view_id);
|
notification_presenter()->ShowNotification(params, render_process_id, render_view_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BrowserClient::CancelDesktopNotification(
|
||||||
|
int render_process_id,
|
||||||
|
int render_view_id,
|
||||||
|
int notification_id) {
|
||||||
|
notification_presenter()->CancelNotification(render_process_id, render_view_id, notification_id);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,10 @@ private:
|
||||||
int render_process_id,
|
int render_process_id,
|
||||||
int render_view_id,
|
int render_view_id,
|
||||||
bool worker) OVERRIDE;
|
bool worker) OVERRIDE;
|
||||||
|
virtual void CancelDesktopNotification(
|
||||||
|
int render_process_id,
|
||||||
|
int render_view_id,
|
||||||
|
int notification_id) OVERRIDE;
|
||||||
|
|
||||||
BrowserMainParts* browser_main_parts_;
|
BrowserMainParts* browser_main_parts_;
|
||||||
scoped_ptr<NotificationPresenter> notification_presenter_;
|
scoped_ptr<NotificationPresenter> notification_presenter_;
|
||||||
|
|
|
@ -17,6 +17,10 @@ class NotificationPresenter {
|
||||||
const content::ShowDesktopNotificationHostMsgParams&,
|
const content::ShowDesktopNotificationHostMsgParams&,
|
||||||
int render_process_id,
|
int render_process_id,
|
||||||
int render_view_id) = 0;
|
int render_view_id) = 0;
|
||||||
|
virtual void CancelNotification(
|
||||||
|
int render_process_id,
|
||||||
|
int render_view_id,
|
||||||
|
int notification_id) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,15 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
#ifndef BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_MAC_H_
|
#ifndef BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_MAC_H_
|
||||||
#define BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_MAC_H_
|
#define BRIGHTRAY_BROWSER_NOTIFICATION_PRESENTER_MAC_H_
|
||||||
|
|
||||||
#import "browser/notification_presenter.h"
|
#import "browser/notification_presenter.h"
|
||||||
|
|
||||||
#import "base/memory/scoped_nsobject.h"
|
#import "base/memory/scoped_nsobject.h"
|
||||||
|
#import <map>
|
||||||
|
|
||||||
@class BRYUserNotificationCenterDelegate;
|
@class BRYUserNotificationCenterDelegate;
|
||||||
|
|
||||||
|
@ -18,8 +24,13 @@ class NotificationPresenterMac : public NotificationPresenter {
|
||||||
const content::ShowDesktopNotificationHostMsgParams&,
|
const content::ShowDesktopNotificationHostMsgParams&,
|
||||||
int render_process_id,
|
int render_process_id,
|
||||||
int render_view_id) OVERRIDE;
|
int render_view_id) OVERRIDE;
|
||||||
|
virtual void CancelNotification(
|
||||||
|
int render_process_id,
|
||||||
|
int render_view_id,
|
||||||
|
int notification_id) OVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::map<std::string, scoped_nsobject<NSUserNotification>> notification_map_;
|
||||||
scoped_nsobject<BRYUserNotificationCenterDelegate> delegate_;
|
scoped_nsobject<BRYUserNotificationCenterDelegate> delegate_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
#import "browser/notification_presenter_mac.h"
|
#import "browser/notification_presenter_mac.h"
|
||||||
|
|
||||||
|
#import "base/stringprintf.h"
|
||||||
#import "base/strings/sys_string_conversions.h"
|
#import "base/strings/sys_string_conversions.h"
|
||||||
#import "content/public/browser/render_view_host.h"
|
#import "content/public/browser/render_view_host.h"
|
||||||
#import "content/public/common/show_desktop_notification_params.h"
|
#import "content/public/common/show_desktop_notification_params.h"
|
||||||
|
@ -19,12 +25,12 @@ NSString * const kNotificationIDKey = @"NotificationID";
|
||||||
|
|
||||||
struct NotificationID {
|
struct NotificationID {
|
||||||
NotificationID(
|
NotificationID(
|
||||||
const content::ShowDesktopNotificationHostMsgParams& params,
|
|
||||||
int render_process_id,
|
int render_process_id,
|
||||||
int render_view_id)
|
int render_view_id,
|
||||||
|
int notification_id)
|
||||||
: render_process_id(render_process_id),
|
: render_process_id(render_process_id),
|
||||||
render_view_id(render_view_id),
|
render_view_id(render_view_id),
|
||||||
notification_id(params.notification_id) {
|
notification_id(notification_id) {
|
||||||
}
|
}
|
||||||
|
|
||||||
NotificationID(NSUserNotification* notification)
|
NotificationID(NSUserNotification* notification)
|
||||||
|
@ -33,6 +39,10 @@ struct NotificationID {
|
||||||
notification_id([[notification.userInfo objectForKey:kNotificationIDKey] intValue]) {
|
notification_id([[notification.userInfo objectForKey:kNotificationIDKey] intValue]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string GetID() {
|
||||||
|
return base::StringPrintf("%d:%d:%d", render_process_id, render_view_id, notification_id);
|
||||||
|
}
|
||||||
|
|
||||||
NSDictionary* GetUserInfo() {
|
NSDictionary* GetUserInfo() {
|
||||||
return @{
|
return @{
|
||||||
kRenderProcessIDKey: @(render_process_id),
|
kRenderProcessIDKey: @(render_process_id),
|
||||||
|
@ -53,7 +63,7 @@ scoped_nsobject<NSUserNotification> CreateUserNotification(
|
||||||
auto notification = [[NSUserNotification alloc] init];
|
auto notification = [[NSUserNotification alloc] init];
|
||||||
notification.title = base::SysUTF16ToNSString(params.title);
|
notification.title = base::SysUTF16ToNSString(params.title);
|
||||||
notification.informativeText = base::SysUTF16ToNSString(params.body);
|
notification.informativeText = base::SysUTF16ToNSString(params.body);
|
||||||
notification.userInfo = NotificationID(params, render_process_id, render_view_id).GetUserInfo();
|
notification.userInfo = NotificationID(render_process_id, render_view_id, params.notification_id).GetUserInfo();
|
||||||
|
|
||||||
return scoped_nsobject<NSUserNotification>(notification);
|
return scoped_nsobject<NSUserNotification>(notification);
|
||||||
}
|
}
|
||||||
|
@ -77,9 +87,39 @@ void NotificationPresenterMac::ShowNotification(
|
||||||
int render_process_id,
|
int render_process_id,
|
||||||
int render_view_id) {
|
int render_view_id) {
|
||||||
auto notification = CreateUserNotification(params, render_process_id, render_view_id);
|
auto notification = CreateUserNotification(params, render_process_id, render_view_id);
|
||||||
|
notification_map_.insert(std::make_pair(NotificationID(notification).GetID(), notification));
|
||||||
[NSUserNotificationCenter.defaultUserNotificationCenter deliverNotification:notification];
|
[NSUserNotificationCenter.defaultUserNotificationCenter deliverNotification:notification];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@implementation BRYUserNotificationCenterDelegate
|
@implementation BRYUserNotificationCenterDelegate
|
||||||
|
|
Loading…
Reference in a new issue