Skip to content

Commit 6cce290

Browse files
author
Amos Shi
committedApr 25, 2024
8318854: [macos14] Running any AWT app prints Secure coding warning
Backport-of: 940f67c1a62c6f9462266f3a108649aca114cffa
1 parent 533b458 commit 6cce290

File tree

4 files changed

+64
-24
lines changed

4 files changed

+64
-24
lines changed
 

‎src/java.desktop/macosx/native/libawt_lwawt/awt/AWTWindow.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ - (void) activateWindowMenuBar {
811811
isDisabled = !awtWindow.isEnabled;
812812
}
813813

814-
if (menuBar == nil) {
814+
if (menuBar == nil && [ApplicationDelegate sharedDelegate] != nil) {
815815
menuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
816816
isDisabled = NO;
817817
}
@@ -1180,7 +1180,7 @@ + (AWTWindow *) lastKeyWindow {
11801180
window.javaMenuBar = menuBar;
11811181

11821182
CMenuBar* actualMenuBar = menuBar;
1183-
if (actualMenuBar == nil) {
1183+
if (actualMenuBar == nil && [ApplicationDelegate sharedDelegate] != nil) {
11841184
actualMenuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
11851185
}
11861186

‎src/java.desktop/macosx/native/libawt_lwawt/awt/ApplicationDelegate.m

+42-19
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ + (ApplicationDelegate *)sharedDelegate {
116116

117117
// don't install the EAWT delegate if another kind of NSApplication is installed, like say, Safari
118118
BOOL shouldInstall = NO;
119+
BOOL overrideDelegate = (getenv("AWT_OVERRIDE_NSDELEGATE") != NULL);
119120
if (NSApp != nil) {
120-
if ([NSApp isMemberOfClass:[NSApplication class]]) shouldInstall = YES;
121+
if ([NSApp isMemberOfClass:[NSApplication class]] && overrideDelegate) shouldInstall = YES;
121122
if ([NSApp isKindOfClass:[NSApplicationAWT class]]) shouldInstall = YES;
122123
}
123124
checked = YES;
@@ -409,6 +410,19 @@ - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app {
409410
return NSTerminateLater;
410411
}
411412

413+
- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app {
414+
static BOOL checked = NO;
415+
static BOOL supportsSecureState = YES;
416+
417+
if (checked == NO) {
418+
checked = YES;
419+
if (getenv("AWT_DISABLE_NSDELEGATE_SECURE_SAVE") != NULL) {
420+
supportsSecureState = NO;
421+
}
422+
}
423+
return supportsSecureState;
424+
}
425+
412426
+ (void)_systemWillPowerOff {
413427
[self _notifyJava:com_apple_eawt__AppEventHandler_NOTIFY_SHUTDOWN];
414428
}
@@ -506,8 +520,10 @@ + (void)_setDockIconImage:(NSImage *)image {
506520
[dockImageView setImageScaling:NSImageScaleProportionallyUpOrDown];
507521
[dockImageView setImage:image];
508522

509-
[[ApplicationDelegate sharedDelegate].fProgressIndicator removeFromSuperview];
510-
[dockImageView addSubview:[ApplicationDelegate sharedDelegate].fProgressIndicator];
523+
if ([ApplicationDelegate sharedDelegate] != nil) {
524+
[[ApplicationDelegate sharedDelegate].fProgressIndicator removeFromSuperview];
525+
[dockImageView addSubview:[ApplicationDelegate sharedDelegate].fProgressIndicator];
526+
}
511527

512528
// add it to the NSDockTile
513529
[dockTile setContentView: dockImageView];
@@ -520,14 +536,15 @@ + (void)_setDockIconProgress:(NSNumber *)value {
520536
AWT_ASSERT_APPKIT_THREAD;
521537

522538
ApplicationDelegate *delegate = [ApplicationDelegate sharedDelegate];
523-
if ([value doubleValue] >= 0 && [value doubleValue] <=100) {
524-
[delegate.fProgressIndicator setDoubleValue:[value doubleValue]];
525-
[delegate.fProgressIndicator setHidden:NO];
526-
} else {
527-
[delegate.fProgressIndicator setHidden:YES];
539+
if (delegate != nil) {
540+
if ([value doubleValue] >= 0 && [value doubleValue] <=100) {
541+
[delegate.fProgressIndicator setDoubleValue:[value doubleValue]];
542+
[delegate.fProgressIndicator setHidden:NO];
543+
} else {
544+
[delegate.fProgressIndicator setHidden:YES];
545+
}
546+
[[NSApp dockTile] display];
528547
}
529-
530-
[[NSApp dockTile] display];
531548
}
532549

533550
// Obtains the image of the Dock icon, either manually set, a drawn copy, or the default NSApplicationIcon
@@ -638,7 +655,9 @@ + (NSImage *)_dockIconImage {
638655

639656
NSMenu *menu = (NSMenu *)jlong_to_ptr(nsMenuPtr);
640657
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
641-
[ApplicationDelegate sharedDelegate].fDockMenu = menu;
658+
if ([ApplicationDelegate sharedDelegate] != nil) {
659+
[ApplicationDelegate sharedDelegate].fDockMenu = menu;
660+
}
642661
}];
643662

644663
JNI_COCOA_EXIT(env);
@@ -818,13 +837,15 @@ + (NSImage *)_dockIconImage {
818837

819838
[ThreadUtilities performOnMainThreadWaiting:NO block:^(){
820839
ApplicationDelegate *delegate = [ApplicationDelegate sharedDelegate];
821-
switch (menuID) {
822-
case com_apple_eawt__AppMenuBarHandler_MENU_ABOUT:
823-
[delegate _updateAboutMenu:visible enabled:enabled];
824-
break;
825-
case com_apple_eawt__AppMenuBarHandler_MENU_PREFS:
826-
[delegate _updatePreferencesMenu:visible enabled:enabled];
827-
break;
840+
if (delegate != nil) {
841+
switch (menuID) {
842+
case com_apple_eawt__AppMenuBarHandler_MENU_ABOUT:
843+
[delegate _updateAboutMenu:visible enabled:enabled];
844+
break;
845+
case com_apple_eawt__AppMenuBarHandler_MENU_PREFS:
846+
[delegate _updatePreferencesMenu:visible enabled:enabled];
847+
break;
848+
}
828849
}
829850
}];
830851

@@ -843,7 +864,9 @@ + (NSImage *)_dockIconImage {
843864

844865
CMenuBar *menu = (CMenuBar *)jlong_to_ptr(cMenuBarPtr);
845866
[ThreadUtilities performOnMainThreadWaiting:NO block:^(){
846-
[ApplicationDelegate sharedDelegate].fDefaultMenuBar = menu;
867+
if ([ApplicationDelegate sharedDelegate] != nil) {
868+
[ApplicationDelegate sharedDelegate].fDefaultMenuBar = menu;
869+
}
847870
}];
848871

849872
JNI_COCOA_EXIT(env);

‎src/java.desktop/macosx/native/libawt_lwawt/awt/CMenuBar.m

+5-3
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,11 @@ -(void) deactivate {
210210
// In theory, this might cause flickering if the window gaining focus
211211
// has its own menu. However, I couldn't reproduce it on practice, so
212212
// perhaps this is a non issue.
213-
CMenuBar* defaultMenu = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
214-
if (defaultMenu != nil) {
215-
[CMenuBar activate:defaultMenu modallyDisabled:NO];
213+
if ([ApplicationDelegate sharedDelegate] != nil) {
214+
CMenuBar* defaultMenu = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
215+
if (defaultMenu != nil) {
216+
[CMenuBar activate:defaultMenu modallyDisabled:NO];
217+
}
216218
}
217219
}
218220
}

‎src/java.desktop/macosx/native/libosxapp/QueuingApplicationDelegate.m

+15
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,21 @@ - (void)_appDidUnhide
200200
} copy]];
201201
}
202202

203+
204+
- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app
205+
{
206+
static BOOL checked = NO;
207+
static BOOL supportsSecureState = YES;
208+
209+
if (checked == NO) {
210+
checked = YES;
211+
if (getenv("AWT_DISABLE_NSDELEGATE_SECURE_SAVE") != NULL) {
212+
supportsSecureState = NO;
213+
}
214+
}
215+
return supportsSecureState;
216+
}
217+
203218
- (void)processQueuedEventsWithTargetDelegate:(id <NSApplicationDelegate>)delegate
204219
{
205220
self.realDelegate = delegate;

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Apr 25, 2024

@openjdk-notifier[bot]
Please sign in to comment.