[macOS] Wait for binding to be ready before requesting exits from framework (flutter/engine#41753)

Similar to https://github.com/flutter/engine/pull/41733 and
https://github.com/flutter/engine/pull/41782, this causes the macos
embedding to wait until it hears that the scheduler binding has
registered itself before proceeding to send termination requests to the
framework.

This allows applications that don't use the framework (just use
`dart:ui` directly) to exit automatically when the last window is
closed. Without this change, the last window closes, but the app does
not exit.

Depends on framework PR https://github.com/flutter/flutter/pull/126075
landing first.
This commit is contained in:
Greg Spencer
2023-05-16 14:29:32 -07:00
committed by GitHub
parent 4eebfb2246
commit f4ac622197
4 changed files with 24 additions and 8 deletions

View File

@@ -55,7 +55,7 @@
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication* _Nonnull)sender {
// If the framework has already told us to terminate, terminate immediately.
if ([[self terminationHandler] shouldTerminate]) {
if ([self terminationHandler] == nil || [[self terminationHandler] shouldTerminate]) {
return NSTerminateNow;
}

View File

@@ -168,6 +168,7 @@ constexpr char kTextPlainFormat[] = "text/plain";
- (instancetype)initWithEngine:(FlutterEngine*)engine
terminator:(FlutterTerminationCallback)terminator {
self = [super init];
_acceptingRequests = NO;
_engine = engine;
_terminator = terminator ? terminator : ^(id sender) {
// Default to actually terminating the application. The terminator exists to
@@ -205,6 +206,11 @@ constexpr char kTextPlainFormat[] = "text/plain";
exitType:(FlutterAppExitType)type
result:(nullable FlutterResult)result {
_shouldTerminate = YES;
if (![self acceptingRequests]) {
// Until the Dart application has signaled that it is ready to handle
// termination requests, the app will just terminate when asked.
type = kFlutterAppExitTypeRequired;
}
switch (type) {
case kFlutterAppExitTypeCancelable: {
FlutterJSONMethodCodec* codec = [FlutterJSONMethodCodec sharedInstance];
@@ -1032,8 +1038,7 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FlutterEngi
} else if ([call.method isEqualToString:@"System.exitApplication"]) {
[[self terminationHandler] handleRequestAppExitMethodCall:call.arguments result:result];
} else if ([call.method isEqualToString:@"System.initializationComplete"]) {
// TODO(gspencergoog): Handle this message to enable exit message listening.
// https://github.com/flutter/flutter/issues/126033
[self terminationHandler].acceptingRequests = YES;
result(nil);
} else {
result(FlutterMethodNotImplemented);

View File

@@ -702,7 +702,7 @@ TEST_F(FlutterEngineTest, ManageControllersIfInitiatedByEngine) {
TEST_F(FlutterEngineTest, HandlesTerminationRequest) {
id engineMock = CreateMockFlutterEngine(nil);
__block NSString* nextResponse = @"exit";
__block BOOL triedToTerminate = FALSE;
__block BOOL triedToTerminate = NO;
FlutterEngineTerminationHandler* terminationHandler =
[[FlutterEngineTerminationHandler alloc] initWithEngine:engineMock
terminator:^(id sender) {
@@ -744,14 +744,24 @@ TEST_F(FlutterEngineTest, HandlesTerminationRequest) {
[FlutterMethodCall methodCallWithMethodName:@"System.exitApplication"
arguments:@{@"type" : @"cancelable"}];
triedToTerminate = FALSE;
// Always terminate when the binding isn't ready (which is the default).
triedToTerminate = NO;
calledAfterTerminate = @"";
nextResponse = @"cancel";
[engineMock handleMethodCall:methodExitApplication result:appExitResult];
EXPECT_STREQ([calledAfterTerminate UTF8String], "");
EXPECT_TRUE(triedToTerminate);
// Once the binding is ready, handle the request.
terminationHandler.acceptingRequests = YES;
triedToTerminate = NO;
calledAfterTerminate = @"";
nextResponse = @"exit";
[engineMock handleMethodCall:methodExitApplication result:appExitResult];
EXPECT_STREQ([calledAfterTerminate UTF8String], "exit");
EXPECT_TRUE(triedToTerminate);
triedToTerminate = FALSE;
triedToTerminate = NO;
calledAfterTerminate = @"";
nextResponse = @"cancel";
[engineMock handleMethodCall:methodExitApplication result:appExitResult];
@@ -759,7 +769,7 @@ TEST_F(FlutterEngineTest, HandlesTerminationRequest) {
EXPECT_FALSE(triedToTerminate);
// Check that it doesn't crash on error.
triedToTerminate = FALSE;
triedToTerminate = NO;
calledAfterTerminate = @"";
nextResponse = @"error";
[engineMock handleMethodCall:methodExitApplication result:appExitResult];
@@ -768,7 +778,7 @@ TEST_F(FlutterEngineTest, HandlesTerminationRequest) {
}
TEST_F(FlutterEngineTest, HandleAccessibilityEvent) {
__block BOOL announced = FALSE;
__block BOOL announced = NO;
id engineMock = CreateMockFlutterEngine(nil);
OCMStub([engineMock announceAccessibilityMessage:[OCMArg any]

View File

@@ -53,6 +53,7 @@ typedef NS_ENUM(NSInteger, FlutterAppExitResponse) {
@interface FlutterEngineTerminationHandler : NSObject
@property(nonatomic, readonly) BOOL shouldTerminate;
@property(nonatomic, readwrite) BOOL acceptingRequests;
- (instancetype)initWithEngine:(FlutterEngine*)engine
terminator:(nullable FlutterTerminationCallback)terminator;