From 3d070e1376ea609903a6d04d213380c94fc63637 Mon Sep 17 00:00:00 2001 From: Francisco Magdaleno Date: Thu, 25 Jul 2019 13:47:29 -0700 Subject: [PATCH] Revert "[macos] Revert check on FlutterCodecs and refactor message function] (#10009)" (flutter/engine#10141) This reverts commit 471657f667d1489274fe7a414a8523e1429e4bd7. --- .../framework/Source/FlutterChannels.mm | 2 +- .../common/framework/Source/FlutterCodecs.mm | 2 +- .../macos/framework/Source/FLEEngine.mm | 69 ++++++++++--------- 3 files changed, 38 insertions(+), 35 deletions(-) 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 0d54d6d605..33ff2da28d 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(data.length > 0 ? [_codec decode:data] : nil); + callback([_codec decode:data]); }; [_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 b06dab7b85..b55cf52775 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 == nil) + if (message.length == 0) 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 a6ae8e60e3..1486f1b980 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,35 +332,42 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FLEEngine* #pragma mark - FlutterBinaryMessenger - (void)sendOnChannel:(nonnull NSString*)channel message:(nullable NSData*)message { - [self sendOnChannel:channel message:message binaryReply:nil]; + 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); + } } - (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; - - 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(); + FlutterEngineResult result = FlutterPlatformMessageCreateResponseHandle( + _engine, message_reply, captures.get(), &response_handle); + if (result != kSuccess) { + NSLog(@"Failed to create a FlutterPlatformMessageResponseHandle"); + return; } + captures.release(); FlutterPlatformMessage platformMessage = { .struct_size = sizeof(FlutterPlatformMessage), @@ -370,19 +377,15 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FLEEngine* .response_handle = response_handle, }; - 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 = FlutterEngineSendPlatformMessage(_engine, &platformMessage); + if (result != kSuccess) { + NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel, result); } - 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); - }; - } + result = FlutterPlatformMessageReleaseResponseHandle(_engine, response_handle); + if (result != kSuccess) { + NSLog(@"Failed to release the response handle"); + }; } - (void)setMessageHandlerOnChannel:(nonnull NSString*)channel