diff --git a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterChannels.mm b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterChannels.mm index 33ff2da28d..0d54d6d605 100644 --- a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterChannels.mm +++ b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterChannels.mm @@ -51,7 +51,7 @@ - (void)sendMessage:(id)message reply:(FlutterReply)callback { FlutterBinaryReply reply = ^(NSData* data) { if (callback) - callback([_codec decode:data]); + callback(data.length > 0 ? [_codec decode:data] : nil); }; [_messenger sendOnChannel:_name message:[_codec encode:message] binaryReply:reply]; } diff --git a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm index b55cf52775..b06dab7b85 100644 --- a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm +++ b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm @@ -73,7 +73,7 @@ } - (id)decode:(NSData*)message { - if (message.length == 0) + if (message == nil) return nil; BOOL isSimpleValue = NO; id decoded = nil; diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FLEEngine.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FLEEngine.mm index 1486f1b980..a6ae8e60e3 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FLEEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FLEEngine.mm @@ -332,42 +332,35 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FLEEngine* #pragma mark - FlutterBinaryMessenger - (void)sendOnChannel:(nonnull NSString*)channel message:(nullable NSData*)message { - FlutterPlatformMessage platformMessage = { - .struct_size = sizeof(FlutterPlatformMessage), - .channel = [channel UTF8String], - .message = static_cast(message.bytes), - .message_size = message.length, - }; - - FlutterEngineResult result = FlutterEngineSendPlatformMessage(_engine, &platformMessage); - if (result != kSuccess) { - NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel, result); - } + [self sendOnChannel:channel message:message binaryReply:nil]; } - (void)sendOnChannel:(NSString*)channel message:(NSData* _Nullable)message binaryReply:(FlutterBinaryReply _Nullable)callback { - struct Captures { - FlutterBinaryReply reply; - }; - auto captures = std::make_unique(); - captures->reply = callback; - auto message_reply = [](const uint8_t* data, size_t data_size, void* user_data) { - auto captures = reinterpret_cast(user_data); - NSData* reply_data = [NSData dataWithBytes:(void*)data length:data_size]; - captures->reply(reply_data); - delete captures; - }; - FlutterPlatformMessageResponseHandle* response_handle = nullptr; - FlutterEngineResult result = FlutterPlatformMessageCreateResponseHandle( - _engine, message_reply, captures.get(), &response_handle); - if (result != kSuccess) { - NSLog(@"Failed to create a FlutterPlatformMessageResponseHandle"); - return; + + if (callback) { + struct Captures { + FlutterBinaryReply reply; + }; + auto captures = std::make_unique(); + captures->reply = callback; + auto message_reply = [](const uint8_t* data, size_t data_size, void* user_data) { + auto captures = reinterpret_cast(user_data); + NSData* reply_data = [NSData dataWithBytes:static_cast(data) length:data_size]; + captures->reply(reply_data); + delete captures; + }; + + FlutterEngineResult create_result = FlutterPlatformMessageCreateResponseHandle( + _engine, message_reply, captures.get(), &response_handle); + if (create_result != kSuccess) { + NSLog(@"Failed to create a FlutterPlatformMessageResponseHandle (%d)", create_result); + return; + } + captures.release(); } - captures.release(); FlutterPlatformMessage platformMessage = { .struct_size = sizeof(FlutterPlatformMessage), @@ -377,15 +370,19 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FLEEngine* .response_handle = response_handle, }; - result = FlutterEngineSendPlatformMessage(_engine, &platformMessage); - if (result != kSuccess) { - NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel, result); + FlutterEngineResult message_result = FlutterEngineSendPlatformMessage(_engine, &platformMessage); + if (message_result != kSuccess) { + NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel, + message_result); } - result = FlutterPlatformMessageReleaseResponseHandle(_engine, response_handle); - if (result != kSuccess) { - NSLog(@"Failed to release the response handle"); - }; + if (response_handle != nullptr) { + FlutterEngineResult release_result = + FlutterPlatformMessageReleaseResponseHandle(_engine, response_handle); + if (release_result != kSuccess) { + NSLog(@"Failed to release the response handle (%d).", release_result); + }; + } } - (void)setMessageHandlerOnChannel:(nonnull NSString*)channel