• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/platform/android/platform_message_response_android.h"
6 
7 #include "flutter/fml/make_copyable.h"
8 #include "flutter/shell/platform/android/platform_view_android_jni.h"
9 
10 namespace flutter {
11 
PlatformMessageResponseAndroid(int response_id,fml::jni::JavaObjectWeakGlobalRef weak_java_object,fml::RefPtr<fml::TaskRunner> platform_task_runner)12 PlatformMessageResponseAndroid::PlatformMessageResponseAndroid(
13     int response_id,
14     fml::jni::JavaObjectWeakGlobalRef weak_java_object,
15     fml::RefPtr<fml::TaskRunner> platform_task_runner)
16     : response_id_(response_id),
17       weak_java_object_(weak_java_object),
18       platform_task_runner_(std::move(platform_task_runner)) {}
19 
20 PlatformMessageResponseAndroid::~PlatformMessageResponseAndroid() = default;
21 
22 // |flutter::PlatformMessageResponse|
Complete(std::unique_ptr<fml::Mapping> data)23 void PlatformMessageResponseAndroid::Complete(
24     std::unique_ptr<fml::Mapping> data) {
25   platform_task_runner_->PostTask(
26       fml::MakeCopyable([response = response_id_,               //
27                          weak_java_object = weak_java_object_,  //
28                          data = std::move(data)                 //
29   ]() {
30         // We are on the platform thread. Attempt to get the strong reference to
31         // the Java object.
32         auto* env = fml::jni::AttachCurrentThread();
33         auto java_object = weak_java_object.get(env);
34 
35         if (java_object.is_null()) {
36           // The Java object was collected before this message response got to
37           // it. Drop the response on the floor.
38           return;
39         }
40 
41         // Convert the vector to a Java byte array.
42         fml::jni::ScopedJavaLocalRef<jbyteArray> data_array(
43             env, env->NewByteArray(data->GetSize()));
44         env->SetByteArrayRegion(
45             data_array.obj(), 0, data->GetSize(),
46             reinterpret_cast<const jbyte*>(data->GetMapping()));
47 
48         // Make the response call into Java.
49         FlutterViewHandlePlatformMessageResponse(env, java_object.obj(),
50                                                  response, data_array.obj());
51       }));
52 }
53 
54 // |flutter::PlatformMessageResponse|
CompleteEmpty()55 void PlatformMessageResponseAndroid::CompleteEmpty() {
56   platform_task_runner_->PostTask(
57       fml::MakeCopyable([response = response_id_,              //
58                          weak_java_object = weak_java_object_  //
59   ]() {
60         // We are on the platform thread. Attempt to get the strong reference to
61         // the Java object.
62         auto* env = fml::jni::AttachCurrentThread();
63         auto java_object = weak_java_object.get(env);
64 
65         if (java_object.is_null()) {
66           // The Java object was collected before this message response got to
67           // it. Drop the response on the floor.
68           return;
69         }
70         // Make the response call into Java.
71         FlutterViewHandlePlatformMessageResponse(env, java_object.obj(),
72                                                  response, nullptr);
73       }));
74 }
75 }  // namespace flutter
76