Copy platform message buffers into byte arrays passed via JNI (flutter/engine#3536)

The Java message object may be held by user-provided message handlers beyond
the lifetime of the raw blink::PlatformMessage and its data buffer
This commit is contained in:
Jason Simmons
2017-03-28 17:15:01 -07:00
committed by GitHub
parent 616d1c9b1e
commit 9961a50959
3 changed files with 17 additions and 16 deletions

View File

@@ -596,11 +596,11 @@ public class FlutterView extends SurfaceView
}
// Called by native to send us a platform message.
private void handlePlatformMessage(String channel, ByteBuffer message, final int responseId) {
private void handlePlatformMessage(String channel, byte[] message, final int responseId) {
OnBinaryMessageListenerAsync listener = mMessageListeners.get(channel);
if (listener != null) {
try {
listener.onMessage(this, message,
listener.onMessage(this, ByteBuffer.wrap(message),
new BinaryMessageResponse() {
@Override
public void send(ByteBuffer response) {
@@ -622,11 +622,11 @@ public class FlutterView extends SurfaceView
private final Map<Integer, BinaryMessageReplyCallback> mPendingResponses = new HashMap<>();
// Called by native to respond to a platform message that we sent.
private void handlePlatformMessageResponse(int responseId, ByteBuffer response) {
private void handlePlatformMessageResponse(int responseId, byte[] response) {
BinaryMessageReplyCallback callback = mPendingResponses.remove(responseId);
if (callback != null) {
try {
callback.onReply(response);
callback.onReply(ByteBuffer.wrap(response));
} catch (Exception ex) {
Log.e(TAG, "Uncaught exception in binary message listener reply", ex);
}

View File

@@ -285,15 +285,17 @@ void PlatformViewAndroid::HandlePlatformMessage(
pending_responses_[response_id] = response;
}
fml::jni::ScopedJavaLocalRef<jobject> message_buffer(env,
env->NewDirectByteBuffer(const_cast<uint8_t*>(message->data().data()),
message->data().size()));
fml::jni::ScopedJavaLocalRef<jbyteArray> message_array(env,
env->NewByteArray(message->data().size()));
env->SetByteArrayRegion(
message_array.obj(), 0, message->data().size(),
reinterpret_cast<const jbyte*>(message->data().data()));
auto java_channel = fml::jni::StringToJavaString(env, message->channel());
message = nullptr;
// This call can re-enter in InvokePlatformMessageResponseCallback.
FlutterViewHandlePlatformMessage(
env, view.obj(), java_channel.obj(), message_buffer.obj(), response_id);
env, view.obj(), java_channel.obj(), message_array.obj(), response_id);
}
void PlatformViewAndroid::HandlePlatformMessageResponse(
@@ -306,13 +308,13 @@ void PlatformViewAndroid::HandlePlatformMessageResponse(
if (view.is_null())
return;
std::string message_data(reinterpret_cast<const char*>(data.data()),
data.size());
auto java_message_data = fml::jni::StringToJavaString(env, message_data);
fml::jni::ScopedJavaLocalRef<jbyteArray> data_array(env,
env->NewByteArray(data.size()));
env->SetByteArrayRegion(data_array.obj(), 0, data.size(),
reinterpret_cast<const jbyte*>(data.data()));
FlutterViewHandlePlatformMessageResponse(env, view.obj(), response_id,
java_message_data.obj());
data_array.obj());
}
void PlatformViewAndroid::DispatchSemanticsAction(jint id, jint action) {

View File

@@ -279,15 +279,14 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
g_handle_platform_message_method =
env->GetMethodID(g_flutter_view_class->obj(), "handlePlatformMessage",
"(Ljava/lang/String;Ljava/nio/ByteBuffer;I)V");
"(Ljava/lang/String;[BI)V");
if (g_handle_platform_message_method == nullptr) {
return false;
}
g_handle_platform_message_response_method = env->GetMethodID(
g_flutter_view_class->obj(), "handlePlatformMessageResponse",
"(ILjava/nio/ByteBuffer;)V");
g_flutter_view_class->obj(), "handlePlatformMessageResponse", "(I[B)V");
if (g_handle_platform_message_response_method == nullptr) {
return false;