Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit be27fba

Browse files
committed
Wait for binding to be ready before requesting exits from framework
1 parent b649d58 commit be27fba

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

shell/platform/darwin/macos/framework/Source/FlutterAppDelegate.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ - (NSString*)applicationName {
5555

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

shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ @implementation FlutterEngineTerminationHandler {
170170
- (instancetype)initWithEngine:(FlutterEngine*)engine
171171
terminator:(FlutterTerminationCallback)terminator {
172172
self = [super init];
173+
_bindingIsReady = NO;
173174
_engine = engine;
174175
_terminator = terminator ? terminator : ^(id sender) {
175176
// Default to actually terminating the application. The terminator exists to
@@ -207,6 +208,11 @@ - (void)requestApplicationTermination:(id)sender
207208
exitType:(FlutterAppExitType)type
208209
result:(nullable FlutterResult)result {
209210
_shouldTerminate = YES;
211+
if (![self bindingIsReady]) {
212+
// Until the binding has signaled that it is ready to handle application
213+
// termination requests, the app will just terminate when asked.
214+
type = kFlutterAppExitTypeRequired;
215+
}
210216
switch (type) {
211217
case kFlutterAppExitTypeCancelable: {
212218
FlutterJSONMethodCodec* codec = [FlutterJSONMethodCodec sharedInstance];
@@ -1031,6 +1037,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
10311037
result(@{@"value" : @([self clipboardHasStrings])});
10321038
} else if ([call.method isEqualToString:@"System.exitApplication"]) {
10331039
[[self terminationHandler] handleRequestAppExitMethodCall:call.arguments result:result];
1040+
} else if ([call.method isEqualToString:@"System.enableApplicationLifecycle"]) {
1041+
[self terminationHandler].bindingIsReady = YES;
10341042
} else {
10351043
result(FlutterMethodNotImplemented);
10361044
}

shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,16 @@ - (nonnull NSView*)createWithViewIdentifier:(int64_t)viewId arguments:(nullable
742742
[FlutterMethodCall methodCallWithMethodName:@"System.exitApplication"
743743
arguments:@{@"type" : @"cancelable"}];
744744

745+
// Always terminate when the binding isn't ready (which is the default).
746+
triedToTerminate = FALSE;
747+
calledAfterTerminate = @"";
748+
nextResponse = @"cancel";
749+
[engineMock handleMethodCall:methodExitApplication result:appExitResult];
750+
EXPECT_STREQ([calledAfterTerminate UTF8String], "");
751+
EXPECT_TRUE(triedToTerminate);
752+
753+
// Once the binding is ready, handle the request.
754+
terminationHandler.bindingIsReady = YES;
745755
triedToTerminate = FALSE;
746756
calledAfterTerminate = @"";
747757
nextResponse = @"exit";

shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ typedef NS_ENUM(NSInteger, FlutterAppExitResponse) {
5353
@interface FlutterEngineTerminationHandler : NSObject
5454

5555
@property(nonatomic, readonly) BOOL shouldTerminate;
56+
@property(nonatomic, readwrite) BOOL bindingIsReady;
5657

5758
- (instancetype)initWithEngine:(FlutterEngine*)engine
5859
terminator:(nullable FlutterTerminationCallback)terminator;

0 commit comments

Comments
 (0)