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

Commit bd38702

Browse files
[macos] Revert check on FlutterCodecs and refactor message function] (#10009)
* [macos] Revert check on FlutterCodecs and refactor message function] * Remove duplicated code * Move FlutterPlatformMessage initialization
1 parent d6e1a93 commit bd38702

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

shell/platform/darwin/common/framework/Source/FlutterChannels.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ - (void)sendMessage:(id)message {
5151
- (void)sendMessage:(id)message reply:(FlutterReply)callback {
5252
FlutterBinaryReply reply = ^(NSData* data) {
5353
if (callback)
54-
callback([_codec decode:data]);
54+
callback(data.length > 0 ? [_codec decode:data] : nil);
5555
};
5656
[_messenger sendOnChannel:_name message:[_codec encode:message] binaryReply:reply];
5757
}

shell/platform/darwin/common/framework/Source/FlutterCodecs.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ - (NSData*)encode:(id)message {
7373
}
7474

7575
- (id)decode:(NSData*)message {
76-
if (message.length == 0)
76+
if (message == nil)
7777
return nil;
7878
BOOL isSimpleValue = NO;
7979
id decoded = nil;

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

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -332,42 +332,35 @@ - (void)shutDownEngine {
332332
#pragma mark - FlutterBinaryMessenger
333333

334334
- (void)sendOnChannel:(nonnull NSString*)channel message:(nullable NSData*)message {
335-
FlutterPlatformMessage platformMessage = {
336-
.struct_size = sizeof(FlutterPlatformMessage),
337-
.channel = [channel UTF8String],
338-
.message = static_cast<const uint8_t*>(message.bytes),
339-
.message_size = message.length,
340-
};
341-
342-
FlutterEngineResult result = FlutterEngineSendPlatformMessage(_engine, &platformMessage);
343-
if (result != kSuccess) {
344-
NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel, result);
345-
}
335+
[self sendOnChannel:channel message:message binaryReply:nil];
346336
}
347337

348338
- (void)sendOnChannel:(NSString*)channel
349339
message:(NSData* _Nullable)message
350340
binaryReply:(FlutterBinaryReply _Nullable)callback {
351-
struct Captures {
352-
FlutterBinaryReply reply;
353-
};
354-
auto captures = std::make_unique<Captures>();
355-
captures->reply = callback;
356-
auto message_reply = [](const uint8_t* data, size_t data_size, void* user_data) {
357-
auto captures = reinterpret_cast<Captures*>(user_data);
358-
NSData* reply_data = [NSData dataWithBytes:(void*)data length:data_size];
359-
captures->reply(reply_data);
360-
delete captures;
361-
};
362-
363341
FlutterPlatformMessageResponseHandle* response_handle = nullptr;
364-
FlutterEngineResult result = FlutterPlatformMessageCreateResponseHandle(
365-
_engine, message_reply, captures.get(), &response_handle);
366-
if (result != kSuccess) {
367-
NSLog(@"Failed to create a FlutterPlatformMessageResponseHandle");
368-
return;
342+
343+
if (callback) {
344+
struct Captures {
345+
FlutterBinaryReply reply;
346+
};
347+
auto captures = std::make_unique<Captures>();
348+
captures->reply = callback;
349+
auto message_reply = [](const uint8_t* data, size_t data_size, void* user_data) {
350+
auto captures = reinterpret_cast<Captures*>(user_data);
351+
NSData* reply_data = [NSData dataWithBytes:static_cast<const void*>(data) length:data_size];
352+
captures->reply(reply_data);
353+
delete captures;
354+
};
355+
356+
FlutterEngineResult create_result = FlutterPlatformMessageCreateResponseHandle(
357+
_engine, message_reply, captures.get(), &response_handle);
358+
if (create_result != kSuccess) {
359+
NSLog(@"Failed to create a FlutterPlatformMessageResponseHandle (%d)", create_result);
360+
return;
361+
}
362+
captures.release();
369363
}
370-
captures.release();
371364

372365
FlutterPlatformMessage platformMessage = {
373366
.struct_size = sizeof(FlutterPlatformMessage),
@@ -377,15 +370,19 @@ - (void)sendOnChannel:(NSString*)channel
377370
.response_handle = response_handle,
378371
};
379372

380-
result = FlutterEngineSendPlatformMessage(_engine, &platformMessage);
381-
if (result != kSuccess) {
382-
NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel, result);
373+
FlutterEngineResult message_result = FlutterEngineSendPlatformMessage(_engine, &platformMessage);
374+
if (message_result != kSuccess) {
375+
NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel,
376+
message_result);
383377
}
384378

385-
result = FlutterPlatformMessageReleaseResponseHandle(_engine, response_handle);
386-
if (result != kSuccess) {
387-
NSLog(@"Failed to release the response handle");
388-
};
379+
if (response_handle != nullptr) {
380+
FlutterEngineResult release_result =
381+
FlutterPlatformMessageReleaseResponseHandle(_engine, response_handle);
382+
if (release_result != kSuccess) {
383+
NSLog(@"Failed to release the response handle (%d).", release_result);
384+
};
385+
}
389386
}
390387

391388
- (void)setMessageHandlerOnChannel:(nonnull NSString*)channel

0 commit comments

Comments
 (0)