changeset 2074:1a196ada0bc9

Mac: Add safety checks. Check bundleIdentifier is not nil before calling Notification APIs on Mojave. This will abort if the application ID is not set. Add basic script for self-signing the dwtest application if possible.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 21 May 2020 14:17:36 +0000
parents cf70a52f702e
children c020ad267ae2
files mac/dw.m mac/finishup.sh
diffstat 2 files changed, 52 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/mac/dw.m	Fri May 15 11:25:07 2020 +0000
+++ b/mac/dw.m	Thu May 21 14:17:36 2020 +0000
@@ -1437,7 +1437,7 @@
 -(void)applicationDidFinishLaunching:(NSNotification *)aNotification
 {
 #ifdef BUILDING_FOR_MOJAVE
-    if (!@available(macOS 10.14, *))
+    if (@available(macOS 10.14, *)) {} else
 #endif
     {
         NSUserNotificationCenter* unc = [NSUserNotificationCenter defaultUserNotificationCenter];
@@ -10766,14 +10766,17 @@
     // Configure the notification's payload.
     if (@available(macOS 10.14, *))
     {
-        UNMutableNotificationContent* notification = [[UNMutableNotificationContent alloc] init];
-        
-        if(notification)
-        {
-            notification.title = [NSString stringWithUTF8String:title];
-            if(description)
-                notification.body = [NSString stringWithUTF8String:outbuf];
-            retval = notification;
+        if([[NSBundle mainBundle] bundleIdentifier] != nil)
+        {
+            UNMutableNotificationContent* notification = [[UNMutableNotificationContent alloc] init];
+            
+            if(notification)
+            {
+                notification.title = [NSString stringWithUTF8String:title];
+                if(description)
+                    notification.body = [NSString stringWithUTF8String:outbuf];
+                retval = notification;
+            }
         }
     }
     else
@@ -10813,15 +10816,18 @@
         // Schedule the notification.
         if (@available(macOS 10.14, *))
         {
-            UNMutableNotificationContent *content = (UNMutableNotificationContent *)notification;
-            NSString *notid = [NSString stringWithFormat:@"dw-notification-%llu", (unsigned long long)notification];
-            UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:notid
-            content:content trigger:nil];
-            
-            UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
-            [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
-                    _event_handler(notification, nil, 8);
-            }];
+            if([[NSBundle mainBundle] bundleIdentifier] != nil)
+            {
+                UNMutableNotificationContent *content = (UNMutableNotificationContent *)notification;
+                NSString *notid = [NSString stringWithFormat:@"dw-notification-%llu", (unsigned long long)notification];
+                UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:notid
+                content:content trigger:nil];
+                
+                UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
+                [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
+                        _event_handler(notification, nil, 8);
+                }];
+            }
         }
         else
 #endif
@@ -12102,15 +12108,18 @@
 #ifdef BUILDING_FOR_MOJAVE
     if (@available(macOS 10.14, *))
     {
-        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
-        if(center)
-        {
-            [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert|UNAuthorizationOptionSound
-                    completionHandler:^(BOOL granted, NSError * _Nullable error) {
-                        if (!granted) {
-                            NSLog(@"WARNING: Unable to get notification permission.");
-                        }
-            }];
+        if([[NSBundle mainBundle] bundleIdentifier] != nil)
+        {
+            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
+            if(center)
+            {
+                [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert|UNAuthorizationOptionSound
+                        completionHandler:^(BOOL granted, NSError * _Nullable error) {
+                            if (!granted) {
+                                NSLog(@"WARNING: Unable to get notification permission. %@", error.localizedDescription);
+                            }
+                }];
+            }
         }
     }
     _DWDirtyDrawables = [[NSMutableArray alloc] init];
--- a/mac/finishup.sh	Fri May 15 11:25:07 2020 +0000
+++ b/mac/finishup.sh	Thu May 21 14:17:36 2020 +0000
@@ -9,4 +9,20 @@
     cp -f $1/mac/Info.plist dwtest.app/Contents
     cp -f $1/mac/PkgInfo dwtest.app/Contents
     cp -f dwtest dwtest.app/Contents/MacOS
+    # Check if there is a certificate to sign with...
+    if [ ! -f mac/key.crt ]; then
+       if [ -f mac/key.rsa ]; then
+          # If not we generate a self-signed one for testing purposes
+          echo "No certifiacte in mac/key.crt so generating self-signed certificate..."
+          openssl req -new -key mac/key.rsa -out mac/key.csr -config mac/openssl.cnf
+          openssl x509 -req -days 3650 -in mac/key.csr -signkey mac/key.rsa -out mac/key.crt -extfile mac/openssl.cnf -extensions codesign
+          certtool i mac/key.crt k="`pwd`/mac/key.keychain" r=mac/key.rsa c p=moof
+       else
+           echo "No key pair found, cannot generate certificate... code will be unsigned."
+       fi
+    fi
+    if [ -f mac/key.keychain ]; then
+        echo "Signing the apllication with certificate in mac/key.crt"
+        codesign -s my-signing-identity --keychain mac/key.keychain dwtest.app/Contents/MacOS/dwtest
+    fi
 fi