From c4aaa21eb31089e078f113926ddc26945476c0e7 Mon Sep 17 00:00:00 2001 From: Mikkel Nygaard Ravn Date: Tue, 17 Apr 2018 16:02:10 +0200 Subject: [PATCH] Fix Android platform channels (flutter/engine#5025) --- .../platform_message_response_android.cc | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/platform_message_response_android.cc b/engine/src/flutter/shell/platform/android/platform_message_response_android.cc index 214080ec5b..b1bdc49147 100644 --- a/engine/src/flutter/shell/platform/android/platform_message_response_android.cc +++ b/engine/src/flutter/shell/platform/android/platform_message_response_android.cc @@ -35,13 +35,6 @@ void PlatformMessageResponseAndroid::Complete(std::vector data) { return; } - if (data.size() == 0) { - // If the data is empty, there is no reason to create a Java byte - // array. Make the response now with a nullptr now. - FlutterViewHandlePlatformMessageResponse(env, java_object.obj(), - response, nullptr); - } - // Convert the vector to a Java byte array. fml::jni::ScopedJavaLocalRef data_array( env, env->NewByteArray(data.size())); @@ -56,7 +49,23 @@ void PlatformMessageResponseAndroid::Complete(std::vector data) { // |blink::PlatformMessageResponse| void PlatformMessageResponseAndroid::CompleteEmpty() { - Complete(std::vector{}); -} + platform_task_runner_->PostTask( + fxl::MakeCopyable([response = response_id_, // + weak_java_object = weak_java_object_ // + ]() { + // We are on the platform thread. Attempt to get the strong reference to + // the Java object. + auto env = fml::jni::AttachCurrentThread(); + auto java_object = weak_java_object.get(env); + if (java_object.is_null()) { + // The Java object was collected before this message response got to + // it. Drop the response on the floor. + return; + } + // Make the response call into Java. + FlutterViewHandlePlatformMessageResponse(env, java_object.obj(), + response, nullptr); + })); +} } // namespace shell