Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "[macos] Revert check on FlutterCodecs and refactor message fu… #10141

Merged
merged 1 commit into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ - (void)sendMessage:(id)message {
- (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];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ - (NSData*)encode:(id)message {
}

- (id)decode:(NSData*)message {
if (message == nil)
if (message.length == 0)
return nil;
BOOL isSimpleValue = NO;
id decoded = nil;
Expand Down
69 changes: 36 additions & 33 deletions shell/platform/darwin/macos/framework/Source/FLEEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -332,35 +332,42 @@ - (void)shutDownEngine {
#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<const uint8_t*>(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 {
FlutterPlatformMessageResponseHandle* response_handle = nullptr;

if (callback) {
struct Captures {
FlutterBinaryReply reply;
};
auto captures = std::make_unique<Captures>();
captures->reply = callback;
auto message_reply = [](const uint8_t* data, size_t data_size, void* user_data) {
auto captures = reinterpret_cast<Captures*>(user_data);
NSData* reply_data = [NSData dataWithBytes:static_cast<const void*>(data) length:data_size];
captures->reply(reply_data);
delete captures;
};
struct Captures {
FlutterBinaryReply reply;
};
auto captures = std::make_unique<Captures>();
captures->reply = callback;
auto message_reply = [](const uint8_t* data, size_t data_size, void* user_data) {
auto captures = reinterpret_cast<Captures*>(user_data);
NSData* reply_data = [NSData dataWithBytes:(void*)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();
FlutterPlatformMessageResponseHandle* response_handle = nullptr;
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),
Expand All @@ -370,19 +377,15 @@ - (void)sendOnChannel:(NSString*)channel
.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
Expand Down