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

Added marshalling to main thread messages sent from native side #422

Merged
merged 3 commits into from
Sep 6, 2024
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Stanislav Shmarov <github@snarpix.com>
Sebastian Urban <surban@surban.net>
Ómar Högni Guðmarsson <ohg@skaginn3x.com>
Athaariq Ardhiansyah <foss@athaariq.my.id>
Anton Sakhon <kofhein@gmail.com>
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ BinaryMessengerImpl::BinaryMessengerImpl(

BinaryMessengerImpl::~BinaryMessengerImpl() = default;

void CaptureCleaner(void* lambda) {
auto& cleanup = *reinterpret_cast<std::function<void()>*>(lambda);
cleanup();
}

void BinaryMessengerImpl::Send(const std::string& channel,
const uint8_t* message,
size_t message_size,
Expand All @@ -89,10 +94,10 @@ void BinaryMessengerImpl::Send(const std::string& channel,
};
bool result = FlutterDesktopMessengerSendWithReply(
messenger_, channel.c_str(), message, message_size, message_reply,
captures);
if (!result) {
delete captures;
}
captures, [](void* captures_data) {
auto captures = reinterpret_cast<Captures*>(captures_data);
delete captures;
});
}

void BinaryMessengerImpl::SetMessageHandler(const std::string& channel,
Expand Down
3 changes: 2 additions & 1 deletion src/flutter/shell/platform/common/public/flutter_messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ FLUTTER_EXPORT bool FlutterDesktopMessengerSendWithReply(
const uint8_t* message,
const size_t message_size,
const FlutterDesktopBinaryReply reply,
void* user_data);
void* user_data,
void (*cleanup)(void* captures_data));

// Sends a reply to a FlutterDesktopMessage for the given response handle.
//
Expand Down
34 changes: 24 additions & 10 deletions src/flutter/shell/platform/linux_embedded/flutter_elinux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,22 +199,36 @@ void FlutterDesktopPluginRegistrarSetDestructionHandler(
registrar->engine->SetPluginRegistrarDestructionCallback(callback);
}

bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger,
const char* channel,
const uint8_t* message,
const size_t message_size,
const FlutterDesktopBinaryReply reply,
void* user_data) {
return messenger->GetEngine()->SendPlatformMessage(
channel, message, message_size, reply, user_data);
bool FlutterDesktopMessengerSendWithReply(
FlutterDesktopMessengerRef messenger,
const char* channel,
const uint8_t* message,
const size_t message_size,
const FlutterDesktopBinaryReply reply,
void* user_data,
void (*cleanup)(void* captures_data)) {
// As we pass data to lambda and it's pointers we need to make sure that we
// send valid data
std::string channel_copy(channel);
std::vector<uint8_t> message_copy(message, message + message_size);

messenger->GetEngine()->task_runner()->PostTask([=]() {
if (!messenger->GetEngine()->SendPlatformMessage(
channel_copy.c_str(), message_copy.data(), message_copy.size(),
reply, user_data) &&
user_data) {
cleanup(user_data);
}
});
return true;
}

bool FlutterDesktopMessengerSend(FlutterDesktopMessengerRef messenger,
const char* channel,
const uint8_t* message,
const size_t message_size) {
return FlutterDesktopMessengerSendWithReply(messenger, channel, message,
message_size, nullptr, nullptr);
return FlutterDesktopMessengerSendWithReply(
messenger, channel, message, message_size, nullptr, nullptr, nullptr);
}

void FlutterDesktopMessengerSendResponse(
Expand Down
Loading