Started handling messages from background isolates for Android (flutter/engine#35804)

This commit is contained in:
gaaclarke
2022-09-12 18:43:22 -07:00
committed by GitHub
parent aa973bd9f1
commit 77d178e919
7 changed files with 11 additions and 7 deletions

View File

@@ -586,7 +586,7 @@ class PlatformDispatcher {
/// [token]. This is required if platform channels are to be used on a
/// background isolate.
void registerBackgroundIsolate(RootIsolateToken token) {
if (!Platform.isIOS) {
if (!(Platform.isIOS || Platform.isAndroid)) {
// Issue: https://github.com/flutter/flutter/issues/13937
throw UnimplementedError("Platform doesn't yet support platform channels on background isolates.");
}

View File

@@ -977,7 +977,7 @@ public class FlutterJNI {
nativeCleanupMessageData(messageData);
}
// Called by native on the ui thread.
// Called by native on any thread.
// TODO(mattcarroll): determine if message is nonull or nullable
@SuppressWarnings("unused")
@VisibleForTesting

View File

@@ -311,6 +311,7 @@ class DartMessenger implements BinaryMessenger, PlatformMessageHandler {
@Nullable ByteBuffer message,
int replyId,
long messageData) {
// Called from any thread.
final DartMessengerTaskQueue taskQueue = (handlerInfo != null) ? handlerInfo.taskQueue : null;
TraceSection.beginAsyncSection("PlatformChannel ScheduleHandler on " + channel, replyId);
Runnable myRunnable =
@@ -338,11 +339,13 @@ class DartMessenger implements BinaryMessenger, PlatformMessageHandler {
@Override
public void handleMessageFromDart(
@NonNull String channel, @Nullable ByteBuffer message, int replyId, long messageData) {
// Called from the ui thread.
// Called from any thread.
Log.v(TAG, "Received message from Dart over channel '" + channel + "'");
HandlerInfo handlerInfo;
boolean messageDeferred;
// This lock can potentially be a bottleneck and could replaced with a
// read/write lock.
synchronized (handlersLock) {
handlerInfo = messageHandlers.get(channel);
messageDeferred = (enableBufferingIncomingMessages.get() && handlerInfo == null);

View File

@@ -10,6 +10,7 @@ import java.nio.ByteBuffer;
/** Handler that receives messages from Dart code. */
public interface PlatformMessageHandler {
/** Called from any thread. */
void handleMessageFromDart(
@NonNull final String channel,
@Nullable ByteBuffer message,

View File

@@ -50,8 +50,8 @@ void PlatformMessageHandlerAndroid::InvokePlatformMessageEmptyResponseCallback(
// |PlatformView|
void PlatformMessageHandlerAndroid::HandlePlatformMessage(
std::unique_ptr<flutter::PlatformMessage> message) {
// Called from the ui thread.
int response_id = next_response_id_++;
// Called from any thread.
int response_id = next_response_id_.fetch_add(1);
if (auto response = message->response()) {
std::lock_guard lock(pending_responses_mutex_);
pending_responses_[response_id] = response;

View File

@@ -28,7 +28,7 @@ class PlatformMessageHandlerAndroid : public PlatformMessageHandler {
private:
const std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;
int next_response_id_ = 1;
std::atomic<int> next_response_id_ = 1;
std::unordered_map<int, fml::RefPtr<flutter::PlatformMessageResponse>>
pending_responses_;
std::mutex pending_responses_mutex_;

View File

@@ -1140,7 +1140,7 @@ PlatformViewAndroidJNIImpl::~PlatformViewAndroidJNIImpl() = default;
void PlatformViewAndroidJNIImpl::FlutterViewHandlePlatformMessage(
std::unique_ptr<flutter::PlatformMessage> message,
int responseId) {
// Called from the ui thread.
// Called from any thread.
JNIEnv* env = fml::jni::AttachCurrentThread();
auto java_object = java_object_.get(env);