// Copyright 2018 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "flutter/shell/platform/android/platform_message_response_android.h" #include "flutter/shell/platform/android/platform_view_android_jni.h" #include "lib/fxl/functional/make_copyable.h" namespace shell { PlatformMessageResponseAndroid::PlatformMessageResponseAndroid( int response_id, fml::jni::JavaObjectWeakGlobalRef weak_java_object, fxl::RefPtr platform_task_runner) : response_id_(response_id), weak_java_object_(weak_java_object), platform_task_runner_(std::move(platform_task_runner)) {} // |blink::PlatformMessageResponse| void PlatformMessageResponseAndroid::Complete(std::vector data) { platform_task_runner_->PostTask( fxl::MakeCopyable([response = response_id_, // weak_java_object = weak_java_object_, // data = std::move(data) // ]() { // 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; } // Convert the vector to a Java byte array. fml::jni::ScopedJavaLocalRef data_array( env, env->NewByteArray(data.size())); env->SetByteArrayRegion(data_array.obj(), 0, data.size(), reinterpret_cast(data.data())); // Make the response call into Java. FlutterViewHandlePlatformMessageResponse(env, java_object.obj(), response, data_array.obj()); })); } // |blink::PlatformMessageResponse| void PlatformMessageResponseAndroid::CompleteEmpty() { 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