2525#include " shared.hpp"
2626
2727#import < AppKit/AppKit.h>
28+ #import < os/log.h>
29+
30+ static os_log_t logger = os_log_create(" eu.web-eid.web-eid-safari.web-eid-safari-extension" , " extension" );
2831
2932@interface SafariWebExtensionHandler : NSObject <NSExtensionRequestHandling > {
3033 NSMutableDictionary <NSString *, NSExtensionContext *> *contexts;
@@ -35,16 +38,19 @@ @implementation SafariWebExtensionHandler
3538
3639- (id )init {
3740 if (self = [super init ]) {
38- NSLog ( @" web-eid-safari-extension: starting" );
41+ os_log (logger, " starting" );
3942 contexts = [[NSMutableDictionary <NSString *, NSExtensionContext *> alloc] init ];
43+ // Sender is not authenticated; DNCF is system-wide. This is intentional: the notification
44+ // is a wake-up signal only — payload is read from the App Group NSUserDefaults, which is
45+ // gated by team-ID entitlement and inaccessible to other processes.
4046 [NSDistributedNotificationCenter .defaultCenter addObserver: self selector: @selector (notificationEvent: ) name: WebEidExtension object: nil ];
4147 }
4248 return self;
4349}
4450
4551- (void )dealloc
4652{
47- NSLog ( @" web-eid-safari-extension: stopping" );
53+ os_log (logger, " stopping" );
4854 [NSDistributedNotificationCenter .defaultCenter removeObserver: self name: WebEidExtension object: nil ];
4955}
5056
@@ -53,14 +59,19 @@ - (void)notificationEvent:(NSNotification*)notification
5359 // Received notification from App
5460 NSString *nonce = notification.object ;
5561 NSDictionary *resp = takeValue (nonce);
56- NSLog ( @" web-eid-safari-extension: from app nonce (%@ ) request: %@ " , nonce, resp);
62+ os_log (logger, " from app nonce (%{public} @) request: %@" , nonce, resp);
5763 if (resp == nil ) {
5864 return ;
5965 }
6066
67+ // Wait for the app to fully exit before returning the response to the page.
68+ // The app is single-shot and calls QCoreApplication::quit() after posting the response.
69+ // If we return early and the page immediately sends a follow-up request, execNativeApp
70+ // would race with the still-shutting-down instance: launchApplication may no-op against
71+ // it and the WebEidStarting handshake in Loop 1 would then time out.
6172 for (int i = 0 ; i < 20 && [NSRunningApplication runningApplicationsWithBundleIdentifier: WebEidApp].count > 0 ; ++i) {
6273 [NSThread sleepForTimeInterval: 0.5 ];
63- NSLog ( @" web-eid-safari-extension: web-eid-safari is still running" );
74+ os_log (logger, " web-eid-safari is still running" );
6475 }
6576
6677 // Forward to background script
@@ -75,31 +86,31 @@ - (BOOL)execNativeApp
7586{
7687 NSURL *appURL = [NSWorkspace .sharedWorkspace URLForApplicationWithBundleIdentifier: WebEidApp];
7788 if (appURL == nil ) {
78- NSLog ( @" web-eid-safari-extension: failed to get app url" );
89+ os_log_error (logger, " failed to get app url" );
7990 return NO ;
8091 }
8192 setValue (WebEidStarting, @(true ));
8293 if (![NSWorkspace .sharedWorkspace launchApplication: appURL.path]) {
83- NSLog ( @" web-eid-safari-extension: failed to start app" );
94+ os_log_error (logger, " failed to start app" );
8495 return NO ;
8596 }
86- NSLog ( @" web-eid-safari-extension: started app" );
97+ os_log (logger, " started app" );
8798 for (int i = 0 ; i < 20 && [getUserDefaults () boolForKey: WebEidStarting]; ++i) {
8899 [NSThread sleepForTimeInterval: 0.5 ];
89- NSLog ( @" web-eid-safari-extension: waiting to be running %@ " , [getUserDefaults () objectForKey: WebEidStarting]);
100+ os_log (logger, " waiting to be running %{public} @" , [getUserDefaults () objectForKey: WebEidStarting]);
90101 }
91102 if ([(NSNumber *)takeValue (WebEidStarting) boolValue ]) {
92- NSLog ( @" web-eid-safari-extension: timeout to start app" );
103+ os_log_error (logger, " timeout to start app" );
93104 return NO ;
94105 }
95- NSLog ( @" web-eid-safari-extension: app executed" );
106+ os_log (logger, " app executed" );
96107 return YES ;
97108}
98109
99110- (void )beginRequestWithExtensionContext : (NSExtensionContext *)context
100111{
101112 id message = [context.inputItems.firstObject userInfo ][SFExtensionMessageKey];
102- NSLog ( @" web-eid-safari-extension: msg from background.js %@ " , message);
113+ os_log (logger, " msg from background.js %@" , message);
103114
104115 if ([@" status" isEqualToString: message[@" command" ]]) {
105116 NSString *version = [NSString stringWithFormat: @" %@ +%@ " ,
@@ -119,7 +130,7 @@ - (void)beginRequestWithExtensionContext:(NSExtensionContext*)context
119130 return ;
120131 }
121132
122- NSLog ( @" web-eid-safari-extension: sending message to app %@ " , message);
133+ os_log (logger, " sending message to app %@" , message);
123134
124135 // Save context
125136 NSString *nonce = [[[NSUUID alloc ] init ] UUIDString ];
0 commit comments