Bug 1635257 - Implement some of nsIAlertsDoNotDisturb for the macOS NotificationCenter backend. r=mstange, a=pascalc
authorMike Conley <mconley@mozilla.com>
Sat, 16 May 2020 00:58:29 +0000
changeset 591492 57b45999b145adced082b05149144c55d0b90ac0
parent 591491 8e66bb51409663b506083e172b83870d3d1ec3eb
child 591493 7cfdc9c5b7093370bac2a18a67fd2fe09fd54378
push id13152
push userjcristau@mozilla.com
push dateMon, 18 May 2020 14:17:16 +0000
treeherdermozilla-beta@d89dbbcf87a8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmstange, pascalc
bugs1635257
milestone77.0
Bug 1635257 - Implement some of nsIAlertsDoNotDisturb for the macOS NotificationCenter backend. r=mstange, a=pascalc This doesn't implement manualDoNotDisturb, since we don't want to show the about:preferences option for suppressing notifications on macOS. Differential Revision: https://phabricator.services.mozilla.com/D74243
widget/cocoa/OSXNotificationCenter.h
widget/cocoa/OSXNotificationCenter.mm
--- a/widget/cocoa/OSXNotificationCenter.h
+++ b/widget/cocoa/OSXNotificationCenter.h
@@ -23,21 +23,23 @@ typedef NSInteger NSUserNotificationActi
 #endif
 
 namespace mozilla {
 
 class OSXNotificationInfo;
 
 class OSXNotificationCenter : public nsIAlertsService,
                               public nsIAlertsIconData,
+                              public nsIAlertsDoNotDisturb,
                               public nsIAlertNotificationImageListener {
  public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIALERTSSERVICE
   NS_DECL_NSIALERTSICONDATA
+  NS_DECL_NSIALERTSDONOTDISTURB
   NS_DECL_NSIALERTNOTIFICATIONIMAGELISTENER
 
   OSXNotificationCenter();
 
   nsresult Init();
   void CloseAlertCocoaString(NSString* aAlertName);
   void OnActivate(NSString* aAlertName, NSUserNotificationActivationType aActivationType,
                   unsigned long long aAdditionalActionIndex);
@@ -45,13 +47,14 @@ class OSXNotificationCenter : public nsI
 
  protected:
   virtual ~OSXNotificationCenter();
 
  private:
   mozNotificationCenterDelegate* mDelegate;
   nsTArray<RefPtr<OSXNotificationInfo> > mActiveAlerts;
   nsTArray<RefPtr<OSXNotificationInfo> > mPendingAlerts;
+  bool mSuppressForScreenSharing;
 };
 
 }  // namespace mozilla
 
 #endif  // OSXNotificationCenter_h
--- a/widget/cocoa/OSXNotificationCenter.mm
+++ b/widget/cocoa/OSXNotificationCenter.mm
@@ -185,30 +185,31 @@ static id<FakeNSUserNotificationCenter> 
   NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
 }
 
 OSXNotificationCenter::OSXNotificationCenter() {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
 
   mDelegate = [[mozNotificationCenterDelegate alloc] initWithOSXNC:this];
   GetNotificationCenter().delegate = mDelegate;
+  mSuppressForScreenSharing = false;
 
   NS_OBJC_END_TRY_ABORT_BLOCK;
 }
 
 OSXNotificationCenter::~OSXNotificationCenter() {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
 
   [GetNotificationCenter() removeAllDeliveredNotifications];
   [mDelegate release];
 
   NS_OBJC_END_TRY_ABORT_BLOCK;
 }
 
-NS_IMPL_ISUPPORTS(OSXNotificationCenter, nsIAlertsService, nsIAlertsIconData,
+NS_IMPL_ISUPPORTS(OSXNotificationCenter, nsIAlertsService, nsIAlertsIconData, nsIAlertsDoNotDisturb,
                   nsIAlertNotificationImageListener)
 
 nsresult OSXNotificationCenter::Init() {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
 
   return (!!NSClassFromString(@"NSUserNotification")) ? NS_OK : NS_ERROR_FAILURE;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
@@ -245,16 +246,20 @@ OSXNotificationCenter::ShowAlert(nsIAler
 NS_IMETHODIMP
 OSXNotificationCenter::ShowAlertWithIconData(nsIAlertNotification* aAlert,
                                              nsIObserver* aAlertListener, uint32_t aIconSize,
                                              const uint8_t* aIconData) {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
 
   NS_ENSURE_ARG(aAlert);
 
+  if (mSuppressForScreenSharing) {
+    return NS_OK;
+  }
+
   Class unClass = NSClassFromString(@"NSUserNotification");
   id<FakeNSUserNotification> notification = [[unClass alloc] init];
 
   nsAutoString title;
   nsresult rv = aAlert->GetTitle(title);
   NS_ENSURE_SUCCESS(rv, rv);
   notification.title = nsCocoaUtils::ToNSString(title);
 
@@ -529,9 +534,39 @@ OSXNotificationCenter::OnImageReady(nsIS
   [cocoaImage release];
   ShowPendingNotification(osxni);
 
   return NS_OK;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
 }
 
+// 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) {
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT
+
+  NS_ENSURE_ARG(aRetVal);
+  *aRetVal = mSuppressForScreenSharing;
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT
+}
+
+NS_IMETHODIMP
+OSXNotificationCenter::SetSuppressForScreenSharing(bool aSuppress) {
+  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT
+
+  mSuppressForScreenSharing = aSuppress;
+  return NS_OK;
+
+  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT
+}
+
 }  // namespace mozilla