mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
[macos] Add reply to binary messenger (flutter/engine#9953)
* Add reply to binary messenger * Address comments * Formatting * Address comments
This commit is contained in:
parent
1aa2f5db62
commit
4833061af2
@ -61,10 +61,7 @@ FLUTTER_EXPORT
|
||||
*/
|
||||
- (void)sendOnChannel:(NSString*)channel
|
||||
message:(NSData* _Nullable)message
|
||||
binaryReply:(FlutterBinaryReply _Nullable)callback
|
||||
// TODO: Add macOS support for replies once
|
||||
// https://github.com/flutter/flutter/issues/18852 is fixed.
|
||||
API_UNAVAILABLE(macos);
|
||||
binaryReply:(FlutterBinaryReply _Nullable)callback;
|
||||
|
||||
/**
|
||||
* Registers a message handler for incoming binary messages from the Flutter side
|
||||
|
||||
@ -103,11 +103,7 @@ FLUTTER_EXPORT
|
||||
* @param message The message. Must be supported by the codec of this channel.
|
||||
* @param callback A callback to be invoked with the message reply from Flutter.
|
||||
*/
|
||||
- (void)sendMessage:(id _Nullable)message
|
||||
reply:(FlutterReply _Nullable)callback
|
||||
// TODO: Add macOS support for replies once
|
||||
// https://github.com/flutter/flutter/issues/18852 is fixed.
|
||||
API_UNAVAILABLE(macos);
|
||||
- (void)sendMessage:(id _Nullable)message reply:(FlutterReply _Nullable)callback;
|
||||
|
||||
/**
|
||||
* Registers a message handler with this channel.
|
||||
|
||||
@ -73,7 +73,7 @@
|
||||
}
|
||||
|
||||
- (id)decode:(NSData*)message {
|
||||
if (message == nil)
|
||||
if (message.length == 0)
|
||||
return nil;
|
||||
BOOL isSimpleValue = NO;
|
||||
id decoded = nil;
|
||||
|
||||
@ -345,6 +345,49 @@ static void OnPlatformMessage(const FlutterPlatformMessage* message, FLEEngine*
|
||||
}
|
||||
}
|
||||
|
||||
- (void)sendOnChannel:(NSString*)channel
|
||||
message:(NSData* _Nullable)message
|
||||
binaryReply:(FlutterBinaryReply _Nullable)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:(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;
|
||||
}
|
||||
captures.release();
|
||||
|
||||
FlutterPlatformMessage platformMessage = {
|
||||
.struct_size = sizeof(FlutterPlatformMessage),
|
||||
.channel = [channel UTF8String],
|
||||
.message = static_cast<const uint8_t*>(message.bytes),
|
||||
.message_size = message.length,
|
||||
.response_handle = response_handle,
|
||||
};
|
||||
|
||||
result = FlutterEngineSendPlatformMessage(_engine, &platformMessage);
|
||||
if (result != kSuccess) {
|
||||
NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel, result);
|
||||
}
|
||||
|
||||
result = FlutterPlatformMessageReleaseResponseHandle(_engine, response_handle);
|
||||
if (result != kSuccess) {
|
||||
NSLog(@"Failed to release the response handle");
|
||||
};
|
||||
}
|
||||
|
||||
- (void)setMessageHandlerOnChannel:(nonnull NSString*)channel
|
||||
binaryMessageHandler:(nullable FlutterBinaryMessageHandler)handler {
|
||||
_messageHandlers[channel] = [handler copy];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user