Skip to content

Commit 30b3d4a

Browse files
KofheinAnton Sakhon
andauthored
Added marshalling to main thread messages sent from native side (#422)
* Added marshalling to main thread messages sent from native side --------- Co-authored-by: Anton Sakhon <asakhon@universe.dart.spb@asakhon2.nb.ipa.dataart.net>
1 parent 595f7f5 commit 30b3d4a

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ Stanislav Shmarov <github@snarpix.com>
1616
Sebastian Urban <surban@surban.net>
1717
Ómar Högni Guðmarsson <ohg@skaginn3x.com>
1818
Athaariq Ardhiansyah <foss@athaariq.my.id>
19+
Anton Sakhon <kofhein@gmail.com>

src/flutter/shell/platform/common/client_wrapper/core_implementations.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ BinaryMessengerImpl::BinaryMessengerImpl(
6666

6767
BinaryMessengerImpl::~BinaryMessengerImpl() = default;
6868

69+
void CaptureCleaner(void* lambda) {
70+
auto& cleanup = *reinterpret_cast<std::function<void()>*>(lambda);
71+
cleanup();
72+
}
73+
6974
void BinaryMessengerImpl::Send(const std::string& channel,
7075
const uint8_t* message,
7176
size_t message_size,
@@ -89,10 +94,10 @@ void BinaryMessengerImpl::Send(const std::string& channel,
8994
};
9095
bool result = FlutterDesktopMessengerSendWithReply(
9196
messenger_, channel.c_str(), message, message_size, message_reply,
92-
captures);
93-
if (!result) {
94-
delete captures;
95-
}
97+
captures, [](void* captures_data) {
98+
auto captures = reinterpret_cast<Captures*>(captures_data);
99+
delete captures;
100+
});
96101
}
97102

98103
void BinaryMessengerImpl::SetMessageHandler(const std::string& channel,

src/flutter/shell/platform/common/public/flutter_messenger.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ FLUTTER_EXPORT bool FlutterDesktopMessengerSendWithReply(
6363
const uint8_t* message,
6464
const size_t message_size,
6565
const FlutterDesktopBinaryReply reply,
66-
void* user_data);
66+
void* user_data,
67+
void (*cleanup)(void* captures_data));
6768

6869
// Sends a reply to a FlutterDesktopMessage for the given response handle.
6970
//

src/flutter/shell/platform/linux_embedded/flutter_elinux.cc

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,36 @@ void FlutterDesktopPluginRegistrarSetDestructionHandler(
199199
registrar->engine->SetPluginRegistrarDestructionCallback(callback);
200200
}
201201

202-
bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger,
203-
const char* channel,
204-
const uint8_t* message,
205-
const size_t message_size,
206-
const FlutterDesktopBinaryReply reply,
207-
void* user_data) {
208-
return messenger->GetEngine()->SendPlatformMessage(
209-
channel, message, message_size, reply, user_data);
202+
bool FlutterDesktopMessengerSendWithReply(
203+
FlutterDesktopMessengerRef messenger,
204+
const char* channel,
205+
const uint8_t* message,
206+
const size_t message_size,
207+
const FlutterDesktopBinaryReply reply,
208+
void* user_data,
209+
void (*cleanup)(void* captures_data)) {
210+
// As we pass data to lambda and it's pointers we need to make sure that we
211+
// send valid data
212+
std::string channel_copy(channel);
213+
std::vector<uint8_t> message_copy(message, message + message_size);
214+
215+
messenger->GetEngine()->task_runner()->PostTask([=]() {
216+
if (!messenger->GetEngine()->SendPlatformMessage(
217+
channel_copy.c_str(), message_copy.data(), message_copy.size(),
218+
reply, user_data) &&
219+
user_data) {
220+
cleanup(user_data);
221+
}
222+
});
223+
return true;
210224
}
211225

212226
bool FlutterDesktopMessengerSend(FlutterDesktopMessengerRef messenger,
213227
const char* channel,
214228
const uint8_t* message,
215229
const size_t message_size) {
216-
return FlutterDesktopMessengerSendWithReply(messenger, channel, message,
217-
message_size, nullptr, nullptr);
230+
return FlutterDesktopMessengerSendWithReply(
231+
messenger, channel, message, message_size, nullptr, nullptr, nullptr);
218232
}
219233

220234
void FlutterDesktopMessengerSendResponse(

0 commit comments

Comments
 (0)