Skip to content

Commit 9776c51

Browse files
metsmamrts
authored andcommitted
Optimize, response version number from extension
WE2-563 Signed-off-by: Raul Metsma <[email protected]>
1 parent 4ad481d commit 9776c51

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/mac/safari-extension.mm

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ - (id)init {
4141
return self;
4242
}
4343

44-
- (void)dealloc {
44+
- (void)dealloc
45+
{
4546
[NSDistributedNotificationCenter.defaultCenter removeObserver:self name:WebEidExtension object:nil];
4647
}
4748

48-
- (void)notificationEvent:(NSNotification *)notification {
49+
- (void)notificationEvent:(NSNotification*)notification
50+
{
4951
// Received notification from App
5052
NSString *nonce = notification.object;
5153
NSDictionary *resp = takeValue(nonce);
@@ -54,6 +56,11 @@ - (void)notificationEvent:(NSNotification *)notification {
5456
return;
5557
}
5658

59+
for (int i = 0; i < 20 && [NSRunningApplication runningApplicationsWithBundleIdentifier:WebEidApp].count > 0; ++i) {
60+
[NSThread sleepForTimeInterval:0.5];
61+
NSLog(@"web-eid-safari-extension: web-eid-safari is still running");
62+
}
63+
5764
// Forward to background script
5865
NSExtensionContext *context = contexts[nonce];
5966
[contexts removeObjectForKey:nonce];
@@ -62,11 +69,8 @@ - (void)notificationEvent:(NSNotification *)notification {
6269
[context completeRequestReturningItems:@[ response ] completionHandler:nil];
6370
}
6471

65-
- (BOOL)execNativeApp {
66-
if ([NSRunningApplication runningApplicationsWithBundleIdentifier:WebEidApp].count > 0) {
67-
NSLog(@"web-eid-safari-extension: web-eid-safari is running");
68-
return YES;
69-
}
72+
- (BOOL)execNativeApp
73+
{
7074
NSURL *appURL = [NSWorkspace.sharedWorkspace URLForApplicationWithBundleIdentifier:WebEidApp];
7175
if (appURL == nil) {
7276
NSLog(@"web-eid-safari-extension: failed to get app url");
@@ -90,11 +94,21 @@ - (BOOL)execNativeApp {
9094
return YES;
9195
}
9296

93-
- (void)beginRequestWithExtensionContext:(NSExtensionContext *)context
97+
- (void)beginRequestWithExtensionContext:(NSExtensionContext*)context
9498
{
9599
id message = [context.inputItems.firstObject userInfo][SFExtensionMessageKey];
96100
NSLog(@"web-eid-safari-extension: msg from background.js %@", message);
97101

102+
if ([@"status" isEqualToString:message[@"command"]]) {
103+
NSString *version = [NSString stringWithFormat:@"%@.%@",
104+
NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"],
105+
NSBundle.mainBundle.infoDictionary[@"CFBundleVersion"]];
106+
NSExtensionItem *response = [[NSExtensionItem alloc] init];
107+
response.userInfo = @{ SFExtensionMessageKey: @{@"version": version} };
108+
[context completeRequestReturningItems:@[ response ] completionHandler:nil];
109+
return;
110+
}
111+
98112
if (![self execNativeApp]) {
99113
NSDictionary *resp = @{@"code": @"ERR_WEBEID_NATIVE_FATAL", @"message": @"Failed to start app"};
100114
NSExtensionItem *response = [[NSExtensionItem alloc] init];

0 commit comments

Comments
 (0)