From 2872f815fdbe4a84bbec1cd910e81e2e21fffbdf Mon Sep 17 00:00:00 2001 From: Dmitry Kulikov Date: Wed, 5 Nov 2025 18:54:34 +0000 Subject: [PATCH] 8360120: Bundled macOS applications not receiving OpenURL events when launched as subprocess Reviewed-by: kizune, prr --- .../classes/com/apple/eawt/Application.java | 4 +- .../libawt_lwawt/awt/ApplicationDelegate.h | 3 +- .../libawt_lwawt/awt/ApplicationDelegate.m | 40 +++++++++++++++---- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/java.desktop/macosx/classes/com/apple/eawt/Application.java b/src/java.desktop/macosx/classes/com/apple/eawt/Application.java index c1daee47912..137aa864510 100644 --- a/src/java.desktop/macosx/classes/com/apple/eawt/Application.java +++ b/src/java.desktop/macosx/classes/com/apple/eawt/Application.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -75,6 +75,7 @@ import sun.lwawt.macosx.CPlatformWindow; */ public final class Application { private static native void nativeInitializeApplicationDelegate(); + private static native void nativeInstallOpenURLEventHandler(); static Application sApplication = null; @@ -211,6 +212,7 @@ public final class Application { * @since Java for Mac OS X 10.5 Update 8 */ public void setOpenURIHandler(final OpenURIHandler openURIHandler) { + nativeInstallOpenURLEventHandler(); eventHandler.openURIDispatcher.setHandler(openURIHandler); } diff --git a/src/java.desktop/macosx/native/libawt_lwawt/awt/ApplicationDelegate.h b/src/java.desktop/macosx/native/libawt_lwawt/awt/ApplicationDelegate.h index 8d0723c555a..831bdeee603 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/awt/ApplicationDelegate.h +++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/ApplicationDelegate.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -44,6 +44,7 @@ BOOL fHandlesDocumentTypes; BOOL fHandlesURLTypes; + BOOL fOpenURLHandlerInstalled; } @property (nonatomic, retain) NSMenuItem *fPreferencesMenu; diff --git a/src/java.desktop/macosx/native/libawt_lwawt/awt/ApplicationDelegate.m b/src/java.desktop/macosx/native/libawt_lwawt/awt/ApplicationDelegate.m index b256c4121d4..d84fe9afa4a 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/awt/ApplicationDelegate.m +++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/ApplicationDelegate.m @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -243,11 +243,9 @@ AWT_ASSERT_APPKIT_THREAD; NSBundle *bundle = [NSBundle mainBundle]; fHandlesDocumentTypes = [bundle objectForInfoDictionaryKey:@"CFBundleDocumentTypes"] != nil || [bundle _hasEAWTOverride:@"DocumentHandler"]; fHandlesURLTypes = [bundle objectForInfoDictionaryKey:@"CFBundleURLTypes"] != nil || [bundle _hasEAWTOverride:@"URLHandler"]; + fOpenURLHandlerInstalled = NO; if (fHandlesURLTypes) { - [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self - andSelector:@selector(_handleOpenURLEvent:withReplyEvent:) - forEventClass:kInternetEventClass - andEventID:kAEGetURL]; + [self _installOpenURLHandler]; } // By HIG, Preferences are not available unless there is a handler. By default in Mac OS X, @@ -302,9 +300,19 @@ static jclass sjc_AppEventHandler = NULL; #define GET_APPEVENTHANDLER_CLASS_RETURN(ret) \ GET_CLASS_RETURN(sjc_AppEventHandler, "com/apple/eawt/_AppEventHandler", ret); -- (void)_handleOpenURLEvent:(NSAppleEventDescriptor *)openURLEvent withReplyEvent:(NSAppleEventDescriptor *)replyEvent { - if (!fHandlesURLTypes) return; +- (void)_installOpenURLHandler { +AWT_ASSERT_APPKIT_THREAD; + if (fOpenURLHandlerInstalled) return; + [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self + andSelector:@selector(_handleOpenURLEvent:withReplyEvent:) + forEventClass:kInternetEventClass + andEventID:kAEGetURL]; + + fOpenURLHandlerInstalled = YES; +} + +- (void)_handleOpenURLEvent:(NSAppleEventDescriptor *)openURLEvent withReplyEvent:(NSAppleEventDescriptor *)replyEvent { NSString *url = [[openURLEvent paramDescriptorForKeyword:keyDirectObject] stringValue]; [ApplicationDelegate _openURL:url]; @@ -626,6 +634,24 @@ JNI_COCOA_ENTER(env); JNI_COCOA_EXIT(env); } +/* + * Class: com_apple_eawt_Application + * Method: nativeInstallOpenURLEventHandler + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_apple_eawt_Application_nativeInstallOpenURLEventHandler +(JNIEnv *env, jclass clz) +{ +JNI_COCOA_ENTER(env); + [ThreadUtilities performOnMainThreadWaiting:YES block:^(){ + ApplicationDelegate *delegate = [ApplicationDelegate sharedDelegate]; + if (delegate != nil) { + [delegate _installOpenURLHandler]; + } + }]; +JNI_COCOA_EXIT(env); +} + /* * Class: com_apple_eawt__AppEventHandler * Method: nativeOpenCocoaAboutWindow