2015-09-25 20:18:29 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2013-12-05 23:05:10 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "OSXNotificationCenter.h"
|
|
|
|
#import <AppKit/AppKit.h>
|
|
|
|
#include "imgIRequest.h"
|
|
|
|
#include "imgIContainer.h"
|
2016-06-11 01:59:25 +00:00
|
|
|
#include "nsICancelable.h"
|
2015-09-25 20:18:29 +00:00
|
|
|
#include "nsIStringBundle.h"
|
2013-12-05 23:05:10 +00:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#import "nsCocoaUtils.h"
|
2015-12-31 19:08:22 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2015-09-25 20:18:29 +00:00
|
|
|
#include "nsContentUtils.h"
|
2013-12-05 23:05:10 +00:00
|
|
|
#include "nsObjCExceptions.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2016-06-26 22:51:05 +00:00
|
|
|
#define MAX_NOTIFICATION_NAME_LEN 5000
|
|
|
|
|
2013-12-05 23:05:10 +00:00
|
|
|
@protocol FakeNSUserNotification <NSObject>
|
|
|
|
@property(copy) NSString* title;
|
|
|
|
@property(copy) NSString* subtitle;
|
|
|
|
@property(copy) NSString* informativeText;
|
|
|
|
@property(copy) NSString* actionButtonTitle;
|
|
|
|
@property(copy) NSDictionary* userInfo;
|
|
|
|
@property(copy) NSDate* deliveryDate;
|
|
|
|
@property(copy) NSTimeZone* deliveryTimeZone;
|
|
|
|
@property(copy) NSDateComponents* deliveryRepeatInterval;
|
|
|
|
@property(readonly) NSDate* actualDeliveryDate;
|
|
|
|
@property(readonly, getter=isPresented) BOOL presented;
|
|
|
|
@property(readonly, getter=isRemote) BOOL remote;
|
|
|
|
@property(copy) NSString* soundName;
|
|
|
|
@property BOOL hasActionButton;
|
|
|
|
@property(readonly) NSUserNotificationActivationType activationType;
|
|
|
|
@property(copy) NSString* otherButtonTitle;
|
|
|
|
@property(copy) NSImage* contentImage;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@protocol FakeNSUserNotificationCenter <NSObject>
|
|
|
|
+ (id<FakeNSUserNotificationCenter>)defaultUserNotificationCenter;
|
|
|
|
@property(assign) id<NSUserNotificationCenterDelegate> delegate;
|
|
|
|
@property(copy) NSArray* scheduledNotifications;
|
|
|
|
- (void)scheduleNotification:(id<FakeNSUserNotification>)notification;
|
|
|
|
- (void)removeScheduledNotification:(id<FakeNSUserNotification>)notification;
|
|
|
|
@property(readonly) NSArray* deliveredNotifications;
|
|
|
|
- (void)deliverNotification:(id<FakeNSUserNotification>)notification;
|
|
|
|
- (void)removeDeliveredNotification:(id<FakeNSUserNotification>)notification;
|
|
|
|
- (void)removeAllDeliveredNotifications;
|
|
|
|
- (void)_removeAllDisplayedNotifications;
|
|
|
|
- (void)_removeDisplayedNotification:(id<FakeNSUserNotification>)notification;
|
|
|
|
@end
|
|
|
|
|
2018-01-04 20:45:54 +00:00
|
|
|
@interface mozNotificationCenterDelegate : NSObject <NSUserNotificationCenterDelegate> {
|
|
|
|
OSXNotificationCenter* mOSXNC;
|
|
|
|
}
|
|
|
|
- (id)initWithOSXNC:(OSXNotificationCenter*)osxnc;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation mozNotificationCenterDelegate
|
|
|
|
|
|
|
|
- (id)initWithOSXNC:(OSXNotificationCenter*)osxnc {
|
|
|
|
[super init];
|
|
|
|
// We should *never* outlive this OSXNotificationCenter.
|
|
|
|
mOSXNC = osxnc;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)userNotificationCenter:(id<FakeNSUserNotificationCenter>)center
|
|
|
|
didDeliverNotification:(id<FakeNSUserNotification>)notification {
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)userNotificationCenter:(id<FakeNSUserNotificationCenter>)center
|
|
|
|
didActivateNotification:(id<FakeNSUserNotification>)notification {
|
|
|
|
unsigned long long additionalActionIndex = ULLONG_MAX;
|
|
|
|
if ([notification respondsToSelector:@selector(_alternateActionIndex)]) {
|
|
|
|
NSNumber* alternateActionIndex = [(NSObject*)notification valueForKey:@"_alternateActionIndex"];
|
|
|
|
additionalActionIndex = [alternateActionIndex unsignedLongLongValue];
|
|
|
|
}
|
|
|
|
mOSXNC->OnActivate([[notification userInfo] valueForKey:@"name"], notification.activationType,
|
|
|
|
additionalActionIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)userNotificationCenter:(id<FakeNSUserNotificationCenter>)center
|
|
|
|
shouldPresentNotification:(id<FakeNSUserNotification>)notification {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is an undocumented method that we need for parity with Safari.
|
|
|
|
// Apple bug #15440664.
|
|
|
|
- (void)userNotificationCenter:(id<FakeNSUserNotificationCenter>)center
|
|
|
|
didRemoveDeliveredNotifications:(NSArray*)notifications {
|
|
|
|
for (id<FakeNSUserNotification> notification in notifications) {
|
|
|
|
NSString* name = [[notification userInfo] valueForKey:@"name"];
|
|
|
|
mOSXNC->CloseAlertCocoaString(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is an undocumented method that we need to be notified if a user clicks the close button.
|
|
|
|
- (void)userNotificationCenter:(id<FakeNSUserNotificationCenter>)center
|
|
|
|
didDismissAlert:(id<FakeNSUserNotification>)notification {
|
|
|
|
NSString* name = [[notification userInfo] valueForKey:@"name"];
|
|
|
|
mOSXNC->CloseAlertCocoaString(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2013-12-05 23:05:10 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2015-09-25 20:18:29 +00:00
|
|
|
enum {
|
2015-09-25 20:18:29 +00:00
|
|
|
OSXNotificationActionDisable = 0,
|
|
|
|
OSXNotificationActionSettings = 1,
|
2015-09-25 20:18:29 +00:00
|
|
|
};
|
|
|
|
|
2016-06-11 01:59:25 +00:00
|
|
|
class OSXNotificationInfo final : public nsISupports {
|
2014-05-02 16:42:31 +00:00
|
|
|
private:
|
2016-06-11 01:59:25 +00:00
|
|
|
virtual ~OSXNotificationInfo();
|
2014-05-02 16:42:31 +00:00
|
|
|
|
2013-12-05 23:05:10 +00:00
|
|
|
public:
|
2016-06-11 01:59:25 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
2013-12-05 23:05:10 +00:00
|
|
|
OSXNotificationInfo(NSString* name, nsIObserver* observer, const nsAString& alertCookie);
|
|
|
|
|
|
|
|
NSString* mName;
|
|
|
|
nsCOMPtr<nsIObserver> mObserver;
|
|
|
|
nsString mCookie;
|
2016-06-11 01:59:25 +00:00
|
|
|
RefPtr<nsICancelable> mIconRequest;
|
2017-04-25 01:51:45 +00:00
|
|
|
id<FakeNSUserNotification> mPendingNotification;
|
2013-12-05 23:05:10 +00:00
|
|
|
};
|
|
|
|
|
2016-06-11 01:59:25 +00:00
|
|
|
NS_IMPL_ISUPPORTS0(OSXNotificationInfo)
|
|
|
|
|
2013-12-05 23:05:10 +00:00
|
|
|
OSXNotificationInfo::OSXNotificationInfo(NSString* name, nsIObserver* observer,
|
|
|
|
const nsAString& alertCookie) {
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
|
|
|
NS_ASSERTION(name, "Cannot create OSXNotificationInfo without a name!");
|
|
|
|
mName = [name retain];
|
|
|
|
mObserver = observer;
|
|
|
|
mCookie = alertCookie;
|
2017-04-25 01:51:45 +00:00
|
|
|
mPendingNotification = nil;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_END_TRY_IGNORE_BLOCK;
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OSXNotificationInfo::~OSXNotificationInfo() {
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
|
|
|
[mName release];
|
2017-04-25 01:51:45 +00:00
|
|
|
[mPendingNotification release];
|
2013-12-05 23:05:10 +00:00
|
|
|
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_END_TRY_IGNORE_BLOCK;
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static id<FakeNSUserNotificationCenter> GetNotificationCenter() {
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
|
|
|
Class c = NSClassFromString(@"NSUserNotificationCenter");
|
|
|
|
return [c performSelector:@selector(defaultUserNotificationCenter)];
|
|
|
|
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_END_TRY_BLOCK_RETURN(nil);
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OSXNotificationCenter::OSXNotificationCenter() {
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;
|
2018-01-04 20:45:54 +00:00
|
|
|
|
|
|
|
mDelegate = [[mozNotificationCenterDelegate alloc] initWithOSXNC:this];
|
|
|
|
GetNotificationCenter().delegate = mDelegate;
|
2020-05-16 00:58:29 +00:00
|
|
|
mSuppressForScreenSharing = false;
|
2018-01-04 20:45:54 +00:00
|
|
|
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_END_TRY_IGNORE_BLOCK;
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OSXNotificationCenter::~OSXNotificationCenter() {
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;
|
2018-01-04 20:45:54 +00:00
|
|
|
|
|
|
|
[GetNotificationCenter() removeAllDeliveredNotifications];
|
|
|
|
[mDelegate release];
|
|
|
|
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_END_TRY_IGNORE_BLOCK;
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 00:58:29 +00:00
|
|
|
NS_IMPL_ISUPPORTS(OSXNotificationCenter, nsIAlertsService, nsIAlertsIconData, nsIAlertsDoNotDisturb,
|
2016-06-11 01:59:25 +00:00
|
|
|
nsIAlertNotificationImageListener)
|
2013-12-05 23:05:10 +00:00
|
|
|
|
|
|
|
nsresult OSXNotificationCenter::Init() {
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
|
|
|
return (!!NSClassFromString(@"NSUserNotification")) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OSXNotificationCenter::ShowAlertNotification(
|
|
|
|
const nsAString& aImageUrl, const nsAString& aAlertTitle, const nsAString& aAlertText,
|
|
|
|
bool aAlertTextClickable, const nsAString& aAlertCookie, nsIObserver* aAlertListener,
|
|
|
|
const nsAString& aAlertName, const nsAString& aBidi, const nsAString& aLang,
|
2014-11-28 19:08:29 +00:00
|
|
|
const nsAString& aData, nsIPrincipal* aPrincipal, bool aInPrivateBrowsing,
|
2016-10-13 00:27:40 +00:00
|
|
|
bool aRequireInteraction) {
|
2015-12-31 19:08:22 +00:00
|
|
|
nsCOMPtr<nsIAlertNotification> alert = do_CreateInstance(ALERT_NOTIFICATION_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(alert, NS_ERROR_FAILURE);
|
2021-04-20 18:35:36 +00:00
|
|
|
// vibrate is unused for now
|
|
|
|
nsTArray<uint32_t> vibrate;
|
2021-04-26 15:52:25 +00:00
|
|
|
nsresult rv = alert->Init(aAlertName, aImageUrl, aAlertTitle, aAlertText, aAlertTextClickable,
|
|
|
|
aAlertCookie, aBidi, aLang, aData, aPrincipal, aInPrivateBrowsing,
|
|
|
|
aRequireInteraction, false, vibrate);
|
2015-12-31 19:08:22 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return ShowAlert(alert, aAlertListener);
|
|
|
|
}
|
|
|
|
|
2016-05-12 15:58:18 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
OSXNotificationCenter::ShowPersistentNotification(const nsAString& aPersistentData,
|
|
|
|
nsIAlertNotification* aAlert,
|
|
|
|
nsIObserver* aAlertListener) {
|
|
|
|
return ShowAlert(aAlert, aAlertListener);
|
|
|
|
}
|
|
|
|
|
2015-12-31 19:08:22 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
OSXNotificationCenter::ShowAlert(nsIAlertNotification* aAlert, nsIObserver* aAlertListener) {
|
2016-01-07 20:16:54 +00:00
|
|
|
return ShowAlertWithIconData(aAlert, aAlertListener, 0, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OSXNotificationCenter::ShowAlertWithIconData(nsIAlertNotification* aAlert,
|
|
|
|
nsIObserver* aAlertListener, uint32_t aIconSize,
|
|
|
|
const uint8_t* aIconData) {
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
2015-12-31 19:08:22 +00:00
|
|
|
NS_ENSURE_ARG(aAlert);
|
|
|
|
|
2020-05-16 00:58:29 +00:00
|
|
|
if (mSuppressForScreenSharing) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-05 23:05:10 +00:00
|
|
|
Class unClass = NSClassFromString(@"NSUserNotification");
|
|
|
|
id<FakeNSUserNotification> notification = [[unClass alloc] init];
|
2015-10-06 02:48:05 +00:00
|
|
|
|
2015-12-31 19:08:22 +00:00
|
|
|
nsAutoString title;
|
|
|
|
nsresult rv = aAlert->GetTitle(title);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
notification.title = nsCocoaUtils::ToNSString(title);
|
|
|
|
|
2015-10-06 02:48:05 +00:00
|
|
|
nsAutoString hostPort;
|
2015-12-31 20:27:09 +00:00
|
|
|
rv = aAlert->GetSource(hostPort);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-10-16 02:58:56 +00:00
|
|
|
nsCOMPtr<nsIStringBundle> bundle;
|
|
|
|
nsCOMPtr<nsIStringBundleService> sbs = do_GetService(NS_STRINGBUNDLE_CONTRACTID);
|
2015-11-07 09:23:00 +00:00
|
|
|
sbs->CreateBundle("chrome://alerts/locale/alert.properties", getter_AddRefs(bundle));
|
2015-10-16 02:58:56 +00:00
|
|
|
|
2015-11-07 09:23:00 +00:00
|
|
|
if (!hostPort.IsEmpty() && bundle) {
|
2019-06-11 15:51:51 +00:00
|
|
|
AutoTArray<nsString, 1> formatStrings = {hostPort};
|
2017-08-04 04:40:52 +00:00
|
|
|
nsAutoString notificationSource;
|
2019-06-11 15:51:51 +00:00
|
|
|
bundle->FormatStringFromName("source.label", formatStrings, notificationSource);
|
2015-10-16 02:58:56 +00:00
|
|
|
notification.subtitle = nsCocoaUtils::ToNSString(notificationSource);
|
2015-10-06 02:48:05 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 19:08:22 +00:00
|
|
|
nsAutoString text;
|
|
|
|
rv = aAlert->GetText(text);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
notification.informativeText = nsCocoaUtils::ToNSString(text);
|
|
|
|
|
2013-12-05 23:05:10 +00:00
|
|
|
notification.soundName = NSUserNotificationDefaultSoundName;
|
|
|
|
notification.hasActionButton = NO;
|
2015-09-25 20:18:29 +00:00
|
|
|
|
|
|
|
// If this is not an application/extension alert, show additional actions dealing with
|
|
|
|
// permissions.
|
2015-12-31 20:27:09 +00:00
|
|
|
bool isActionable;
|
2016-01-14 06:26:41 +00:00
|
|
|
if (bundle && NS_SUCCEEDED(aAlert->GetActionable(&isActionable)) && isActionable) {
|
2017-08-04 04:40:52 +00:00
|
|
|
nsAutoString closeButtonTitle, actionButtonTitle, disableButtonTitle, settingsButtonTitle;
|
|
|
|
bundle->GetStringFromName("closeButton.title", closeButtonTitle);
|
|
|
|
bundle->GetStringFromName("actionButton.label", actionButtonTitle);
|
2016-01-14 06:26:41 +00:00
|
|
|
if (!hostPort.IsEmpty()) {
|
2019-06-11 15:51:51 +00:00
|
|
|
AutoTArray<nsString, 1> formatStrings = {hostPort};
|
2017-07-12 05:13:37 +00:00
|
|
|
bundle->FormatStringFromName("webActions.disableForOrigin.label", formatStrings,
|
2019-06-11 15:51:51 +00:00
|
|
|
disableButtonTitle);
|
2016-01-14 06:26:41 +00:00
|
|
|
}
|
2017-08-04 04:40:52 +00:00
|
|
|
bundle->GetStringFromName("webActions.settings.label", settingsButtonTitle);
|
2016-01-14 06:26:41 +00:00
|
|
|
|
|
|
|
notification.otherButtonTitle = nsCocoaUtils::ToNSString(closeButtonTitle);
|
|
|
|
|
|
|
|
// OS X 10.8 only shows action buttons if the "Alerts" style is set in
|
|
|
|
// Notification Center preferences, and doesn't support the alternate
|
|
|
|
// action menu.
|
|
|
|
if ([notification respondsToSelector:@selector(set_showsButtons:)] &&
|
|
|
|
[notification respondsToSelector:@selector(set_alwaysShowAlternateActionMenu:)] &&
|
|
|
|
[notification respondsToSelector:@selector(set_alternateActionButtonTitles:)]) {
|
|
|
|
notification.hasActionButton = YES;
|
|
|
|
notification.actionButtonTitle = nsCocoaUtils::ToNSString(actionButtonTitle);
|
|
|
|
|
|
|
|
[(NSObject*)notification setValue:@(YES) forKey:@"_showsButtons"];
|
|
|
|
[(NSObject*)notification setValue:@(YES) forKey:@"_alwaysShowAlternateActionMenu"];
|
|
|
|
[(NSObject*)notification setValue:@[
|
|
|
|
nsCocoaUtils::ToNSString(disableButtonTitle), nsCocoaUtils::ToNSString(settingsButtonTitle)
|
|
|
|
]
|
|
|
|
forKey:@"_alternateActionButtonTitles"];
|
2015-09-25 20:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-31 19:08:22 +00:00
|
|
|
nsAutoString name;
|
|
|
|
rv = aAlert->GetName(name);
|
2016-06-26 22:51:05 +00:00
|
|
|
// Don't let an alert name be more than MAX_NOTIFICATION_NAME_LEN characters.
|
|
|
|
// More than that shouldn't be necessary and userInfo (assigned to below) has
|
|
|
|
// a length limit of 16k on OS X 10.11. Exception thrown if limit exceeded.
|
|
|
|
if (name.Length() > MAX_NOTIFICATION_NAME_LEN) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-12-31 19:08:22 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NSString* alertName = nsCocoaUtils::ToNSString(name);
|
2013-12-05 23:05:10 +00:00
|
|
|
if (!alertName) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
notification.userInfo =
|
|
|
|
[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:alertName, nil]
|
|
|
|
forKeys:[NSArray arrayWithObjects:@"name", nil]];
|
|
|
|
|
2015-12-31 19:08:22 +00:00
|
|
|
nsAutoString cookie;
|
|
|
|
rv = aAlert->GetCookie(cookie);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
OSXNotificationInfo* osxni = new OSXNotificationInfo(alertName, aAlertListener, cookie);
|
|
|
|
|
2016-01-07 20:16:54 +00:00
|
|
|
// Show the favicon if supported on this version of OS X.
|
|
|
|
if (aIconSize > 0 && [notification respondsToSelector:@selector(set_identityImage:)] &&
|
|
|
|
[notification respondsToSelector:@selector(set_identityImageHasBorder:)]) {
|
|
|
|
NSData* iconData = [NSData dataWithBytes:aIconData length:aIconSize];
|
|
|
|
NSImage* icon = [[[NSImage alloc] initWithData:iconData] autorelease];
|
|
|
|
|
|
|
|
[(NSObject*)notification setValue:icon forKey:@"_identityImage"];
|
|
|
|
[(NSObject*)notification setValue:@(NO) forKey:@"_identityImageHasBorder"];
|
|
|
|
}
|
|
|
|
|
2015-12-31 19:08:22 +00:00
|
|
|
bool inPrivateBrowsing;
|
|
|
|
rv = aAlert->GetInPrivateBrowsing(&inPrivateBrowsing);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-12-05 23:05:10 +00:00
|
|
|
|
|
|
|
// Show the notification without waiting for an image if there is no icon URL or
|
|
|
|
// notification icons are not supported on this version of OS X.
|
2016-06-11 01:59:25 +00:00
|
|
|
if (![unClass instancesRespondToSelector:@selector(setContentImage:)]) {
|
2013-12-05 23:05:10 +00:00
|
|
|
CloseAlertCocoaString(alertName);
|
|
|
|
mActiveAlerts.AppendElement(osxni);
|
|
|
|
[GetNotificationCenter() deliverNotification:notification];
|
|
|
|
[notification release];
|
|
|
|
if (aAlertListener) {
|
2015-12-31 19:08:22 +00:00
|
|
|
aAlertListener->Observe(nullptr, "alertshow", cookie.get());
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mPendingAlerts.AppendElement(osxni);
|
2017-04-25 01:51:45 +00:00
|
|
|
osxni->mPendingNotification = notification;
|
2016-06-11 01:59:25 +00:00
|
|
|
// Wait six seconds for the image to load.
|
|
|
|
rv = aAlert->LoadImage(6000, this, osxni, getter_AddRefs(osxni->mIconRequest));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
ShowPendingNotification(osxni);
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2020-12-07 17:44:34 +00:00
|
|
|
OSXNotificationCenter::CloseAlert(const nsAString& aAlertName) {
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
2015-10-06 02:48:05 +00:00
|
|
|
NSString* alertName = nsCocoaUtils::ToNSString(aAlertName);
|
2013-12-05 23:05:10 +00:00
|
|
|
CloseAlertCocoaString(alertName);
|
|
|
|
return NS_OK;
|
|
|
|
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OSXNotificationCenter::CloseAlertCocoaString(NSString* aAlertName) {
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
|
|
|
if (!aAlertName) {
|
|
|
|
return; // Can't do anything without a name
|
|
|
|
}
|
|
|
|
|
|
|
|
NSArray* notifications = [GetNotificationCenter() deliveredNotifications];
|
|
|
|
for (id<FakeNSUserNotification> notification in notifications) {
|
|
|
|
NSString* name = [[notification userInfo] valueForKey:@"name"];
|
|
|
|
if ([name isEqualToString:aAlertName]) {
|
|
|
|
[GetNotificationCenter() removeDeliveredNotification:notification];
|
|
|
|
[GetNotificationCenter() _removeDisplayedNotification:notification];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < mActiveAlerts.Length(); i++) {
|
|
|
|
OSXNotificationInfo* osxni = mActiveAlerts[i];
|
|
|
|
if ([aAlertName isEqualToString:osxni->mName]) {
|
|
|
|
if (osxni->mObserver) {
|
|
|
|
osxni->mObserver->Observe(nullptr, "alertfinished", osxni->mCookie.get());
|
|
|
|
}
|
2016-06-11 01:59:25 +00:00
|
|
|
if (osxni->mIconRequest) {
|
|
|
|
osxni->mIconRequest->Cancel(NS_BINDING_ABORTED);
|
|
|
|
osxni->mIconRequest = nullptr;
|
|
|
|
}
|
2013-12-05 23:05:10 +00:00
|
|
|
mActiveAlerts.RemoveElementAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_END_TRY_IGNORE_BLOCK;
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 20:18:29 +00:00
|
|
|
void OSXNotificationCenter::OnActivate(NSString* aAlertName,
|
|
|
|
NSUserNotificationActivationType aActivationType,
|
|
|
|
unsigned long long aAdditionalActionIndex) {
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
|
|
|
if (!aAlertName) {
|
|
|
|
return; // Can't do anything without a name
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < mActiveAlerts.Length(); i++) {
|
|
|
|
OSXNotificationInfo* osxni = mActiveAlerts[i];
|
|
|
|
if ([aAlertName isEqualToString:osxni->mName]) {
|
|
|
|
if (osxni->mObserver) {
|
2015-09-30 16:48:45 +00:00
|
|
|
switch ((int)aActivationType) {
|
2015-09-25 20:18:29 +00:00
|
|
|
case NSUserNotificationActivationTypeAdditionalActionClicked:
|
|
|
|
case NSUserNotificationActivationTypeActionButtonClicked:
|
|
|
|
switch (aAdditionalActionIndex) {
|
|
|
|
case OSXNotificationActionDisable:
|
|
|
|
osxni->mObserver->Observe(nullptr, "alertdisablecallback", osxni->mCookie.get());
|
|
|
|
break;
|
2015-09-25 20:18:29 +00:00
|
|
|
case OSXNotificationActionSettings:
|
|
|
|
osxni->mObserver->Observe(nullptr, "alertsettingscallback", osxni->mCookie.get());
|
|
|
|
break;
|
2015-09-25 20:18:29 +00:00
|
|
|
default:
|
|
|
|
NS_WARNING("Unknown NSUserNotification additional action clicked");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
osxni->mObserver->Observe(nullptr, "alertclickcallback", osxni->mCookie.get());
|
|
|
|
break;
|
|
|
|
}
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_END_TRY_IGNORE_BLOCK;
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OSXNotificationCenter::ShowPendingNotification(OSXNotificationInfo* osxni) {
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_IGNORE_BLOCK;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
|
|
|
if (osxni->mIconRequest) {
|
|
|
|
osxni->mIconRequest->Cancel(NS_BINDING_ABORTED);
|
|
|
|
osxni->mIconRequest = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseAlertCocoaString(osxni->mName);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < mPendingAlerts.Length(); i++) {
|
|
|
|
if (mPendingAlerts[i] == osxni) {
|
|
|
|
mActiveAlerts.AppendElement(osxni);
|
|
|
|
mPendingAlerts.RemoveElementAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 01:51:45 +00:00
|
|
|
[GetNotificationCenter() deliverNotification:osxni->mPendingNotification];
|
2013-12-05 23:05:10 +00:00
|
|
|
|
|
|
|
if (osxni->mObserver) {
|
|
|
|
osxni->mObserver->Observe(nullptr, "alertshow", osxni->mCookie.get());
|
|
|
|
}
|
|
|
|
|
2017-04-25 01:51:45 +00:00
|
|
|
[osxni->mPendingNotification release];
|
|
|
|
osxni->mPendingNotification = nil;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
2021-02-16 22:55:21 +00:00
|
|
|
NS_OBJC_END_TRY_IGNORE_BLOCK;
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-06-11 01:59:25 +00:00
|
|
|
OSXNotificationCenter::OnImageMissing(nsISupports* aUserData) {
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
2016-06-11 01:59:25 +00:00
|
|
|
OSXNotificationInfo* osxni = static_cast<OSXNotificationInfo*>(aUserData);
|
2017-04-25 01:51:45 +00:00
|
|
|
if (osxni->mPendingNotification) {
|
2016-06-11 01:59:25 +00:00
|
|
|
// If there was an error getting the image, or the request timed out, show
|
|
|
|
// the notification without a content image.
|
2013-12-05 23:05:10 +00:00
|
|
|
ShowPendingNotification(osxni);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-06-11 01:59:25 +00:00
|
|
|
OSXNotificationCenter::OnImageReady(nsISupports* aUserData, imgIRequest* aRequest) {
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
|
2013-12-05 23:05:10 +00:00
|
|
|
|
2016-06-11 01:59:25 +00:00
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
nsresult rv = aRequest->GetImage(getter_AddRefs(image));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv) || !image)) {
|
|
|
|
return rv;
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 01:59:25 +00:00
|
|
|
OSXNotificationInfo* osxni = static_cast<OSXNotificationInfo*>(aUserData);
|
2017-04-25 01:51:45 +00:00
|
|
|
if (!osxni->mPendingNotification) {
|
2016-06-11 01:59:25 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
2016-06-11 01:59:25 +00:00
|
|
|
|
|
|
|
NSImage* cocoaImage = nil;
|
2021-03-26 02:11:14 +00:00
|
|
|
// TODO: Pass ComputedStyle here to support context paint properties
|
2019-09-04 18:33:13 +00:00
|
|
|
nsCocoaUtils::CreateDualRepresentationNSImageFromImageContainer(image, imgIContainer::FRAME_FIRST,
|
2021-03-26 02:11:14 +00:00
|
|
|
nullptr, &cocoaImage);
|
2017-04-25 01:51:45 +00:00
|
|
|
(osxni->mPendingNotification).contentImage = cocoaImage;
|
2016-06-11 01:59:25 +00:00
|
|
|
[cocoaImage release];
|
|
|
|
ShowPendingNotification(osxni);
|
|
|
|
|
2013-12-05 23:05:10 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
|
2013-12-05 23:05:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 00:58:29 +00:00
|
|
|
// nsIAlertsDoNotDisturb
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OSXNotificationCenter::GetManualDoNotDisturb(bool* aRetVal) { return NS_ERROR_NOT_IMPLEMENTED; }
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OSXNotificationCenter::SetManualDoNotDisturb(bool aDoNotDisturb) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OSXNotificationCenter::GetSuppressForScreenSharing(bool* aRetVal) {
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_BLOCK_RETURN
|
2020-05-16 00:58:29 +00:00
|
|
|
|
|
|
|
NS_ENSURE_ARG(aRetVal);
|
|
|
|
*aRetVal = mSuppressForScreenSharing;
|
|
|
|
return NS_OK;
|
|
|
|
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE)
|
2020-05-16 00:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OSXNotificationCenter::SetSuppressForScreenSharing(bool aSuppress) {
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_BLOCK_RETURN
|
2020-05-16 00:58:29 +00:00
|
|
|
|
|
|
|
mSuppressForScreenSharing = aSuppress;
|
|
|
|
return NS_OK;
|
|
|
|
|
2021-02-16 22:55:20 +00:00
|
|
|
NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE)
|
2020-05-16 00:58:29 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 23:05:10 +00:00
|
|
|
} // namespace mozilla
|