Skip to content

Commit

Permalink
Clean up crash when NSApp wasn't a FlutterApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
gspencergoog committed Feb 28, 2023
1 parent f06fefb commit 0255d98
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
39 changes: 21 additions & 18 deletions shell/platform/darwin/macos/framework/Source/FlutterApplication.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ + (NSApplication*)sharedApplication {
// +sharedApplication initializes the global NSApp, so if we're delivering
// something other than a FlutterApplication, warn the developer once.
#ifndef FLUTTER_RELEASE
static bool notified = false;
if (!notified && ![NSApp isKindOfClass:[FlutterApplication class]]) {
NSLog(@"NSApp should be of type %s, not %s. "
"Some application lifecycle requests (e.g. ServicesBinding.exitApplication) "
"and notifications will be unavailable.\n"
"Modify the application's NSPrincipleClass to be %s"
"in the Info.plist to fix this.",
[[self className] UTF8String], [[NSApp className] UTF8String],
[[self className] UTF8String]);
notified = true;
}
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
if (![app respondsToSelector:@selector(terminateApplication:)]) {
NSLog(@"NSApp should be of type %s, not %s.\n"
"System requests for the application to terminate will not be sent to "
"the Flutter framework, so the framework will be unable to cancel "
"those requests.\n"
"Modify the application's NSPrincipleClass to be %s in the "
"Info.plist to fix this.",
[[self className] UTF8String], [[NSApp className] UTF8String],
[[self className] UTF8String]);
}
});
#endif // !FLUTTER_RELEASE
return app;
}
Expand Down Expand Up @@ -62,16 +64,17 @@ + (NSApplication*)sharedApplication {
// it if it is OK to terminate. When that method channel call returns with a
// result, the application either terminates or continues running.
- (void)terminate:(id)sender {
FlutterEngineTerminationHandler* terminationHandler =
[static_cast<FlutterAppDelegate*>([NSApp delegate]) terminationHandler];
if (terminationHandler) {
[terminationHandler requestApplicationTermination:self
exitType:kFlutterAppExitTypeCancelable
result:nil];
} else {
FlutterAppDelegate* delegate = [self delegate];
if (!delegate || ![delegate respondsToSelector:@selector(terminationHandler)] ||
[delegate terminationHandler] == nil) {
// If there's no termination handler, then just terminate.
[super terminate:sender];
}
FlutterEngineTerminationHandler* terminationHandler =
[static_cast<FlutterAppDelegate*>([self delegate]) terminationHandler];
[terminationHandler requestApplicationTermination:sender
exitType:kFlutterAppExitTypeCancelable
result:nil];
// Return, don't exit. The application delegate is responsible for exiting on
// its own by calling |-terminateApplication|.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,12 @@ - (instancetype)initWithEngine:(FlutterEngine*)engine
_terminator = terminator ? terminator : ^(id sender) {
// Default to actually terminating the application. The terminator exists to
// allow tests to override it so that an actual exit doesn't occur.
[[FlutterApplication sharedApplication] terminateApplication:sender];
FlutterApplication* flutterApp = [FlutterApplication sharedApplication];
if (flutterApp && [flutterApp respondsToSelector:@selector(terminateApplication:)]) {
[[FlutterApplication sharedApplication] terminateApplication:sender];
} else if (flutterApp) {
[flutterApp terminate:sender];
}
};
FlutterAppDelegate* appDelegate =
(FlutterAppDelegate*)[[FlutterApplication sharedApplication] delegate];
Expand Down

0 comments on commit 0255d98

Please sign in to comment.