Flutter Engine
The Flutter Engine
Classes | Public Member Functions | Package Functions | List of all members
io.flutter.embedding.engine.dart.DartMessenger Class Reference
Inheritance diagram for io.flutter.embedding.engine.dart.DartMessenger:
io.flutter.plugin.common.BinaryMessenger io.flutter.embedding.engine.dart.PlatformMessageHandler

Classes

class  ConcurrentTaskQueue
 
interface  DartMessengerTaskQueue
 
class  Reply
 
class  SerialTaskQueue
 
interface  TaskQueueFactory
 

Public Member Functions

TaskQueue makeBackgroundTaskQueue (TaskQueueOptions options)
 
void setMessageHandler ( @NonNull String channel, @Nullable BinaryMessenger.BinaryMessageHandler handler)
 
void setMessageHandler ( @NonNull String channel, @Nullable BinaryMessenger.BinaryMessageHandler handler, @Nullable TaskQueue taskQueue)
 
void enableBufferingIncomingMessages ()
 
void disableBufferingIncomingMessages ()
 
void send (@NonNull String channel, @NonNull ByteBuffer message)
 
void send ( @NonNull String channel, @Nullable ByteBuffer message, @Nullable BinaryMessenger.BinaryReply callback)
 
void handleMessageFromDart ( @NonNull String channel, @Nullable ByteBuffer message, int replyId, long messageData)
 
void handlePlatformMessageResponse (int replyId, @Nullable ByteBuffer reply)
 
int getPendingChannelResponseCount ()
 
- Public Member Functions inherited from io.flutter.plugin.common.BinaryMessenger
default TaskQueue makeBackgroundTaskQueue ()
 
default TaskQueue makeBackgroundTaskQueue (TaskQueueOptions options)
 
void send (@NonNull String channel, @Nullable ByteBuffer message)
 
void send (@NonNull String channel, @Nullable ByteBuffer message, @Nullable BinaryReply callback)
 
void setMessageHandler (@NonNull String channel, @Nullable BinaryMessageHandler handler)
 
default void setMessageHandler ( @NonNull String channel, @Nullable BinaryMessageHandler handler, @Nullable TaskQueue taskQueue)
 
default void enableBufferingIncomingMessages ()
 
default void disableBufferingIncomingMessages ()
 
void handleMessageFromDart ( @NonNull final String channel, @Nullable ByteBuffer message, final int replyId, long messageData)
 
void handlePlatformMessageResponse (int replyId, @Nullable ByteBuffer reply)
 

Package Functions

 DartMessenger (@NonNull FlutterJNI flutterJNI, @NonNull TaskQueueFactory taskQueueFactory)
 
 DartMessenger (@NonNull FlutterJNI flutterJNI)
 

Detailed Description

Message conduit for 2-way communication between Android and Dart.

See BinaryMessenger, which sends messages from Android to Dart

See PlatformMessageHandler, which handles messages to Android from Dart

Definition at line 32 of file DartMessenger.java.

Constructor & Destructor Documentation

◆ DartMessenger() [1/2]

io.flutter.embedding.engine.dart.DartMessenger.DartMessenger ( @NonNull FlutterJNI  flutterJNI,
@NonNull TaskQueueFactory  taskQueueFactory 
)
inlinepackage

Definition at line 66 of file DartMessenger.java.

66 {
67 this.flutterJNI = flutterJNI;
68 this.taskQueueFactory = taskQueueFactory;
69 }

◆ DartMessenger() [2/2]

io.flutter.embedding.engine.dart.DartMessenger.DartMessenger ( @NonNull FlutterJNI  flutterJNI)
inlinepackage

Definition at line 71 of file DartMessenger.java.

71 {
72 this(flutterJNI, new DefaultTaskQueueFactory());
73 }

Member Function Documentation

◆ disableBufferingIncomingMessages()

void io.flutter.embedding.engine.dart.DartMessenger.disableBufferingIncomingMessages ( )
inline

Disables the ability to queue messages received from Dart.

This can be used after all pending channel handlers have been registered.

Implements io.flutter.plugin.common.BinaryMessenger.

Definition at line 245 of file DartMessenger.java.

245 {
246 Map<String, List<BufferedMessageInfo>> pendingMessages;
247 synchronized (handlersLock) {
248 enableBufferingIncomingMessages.set(false);
249 pendingMessages = bufferedMessages;
250 bufferedMessages = new HashMap<>();
251 }
252 for (Map.Entry<String, List<BufferedMessageInfo>> channel : pendingMessages.entrySet()) {
253 for (BufferedMessageInfo info : channel.getValue()) {
254 dispatchMessageToQueue(
255 channel.getKey(), null, info.message, info.replyId, info.messageData);
256 }
257 }
258 }
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213

◆ enableBufferingIncomingMessages()

void io.flutter.embedding.engine.dart.DartMessenger.enableBufferingIncomingMessages ( )
inline

Enables the ability to queue messages received from Dart.

This is useful when there are pending channel handler registrations. For example, Dart may be initialized concurrently, and prior to the registration of the channel handlers. This implies that Dart may start sending messages while plugins are being registered.

Implements io.flutter.plugin.common.BinaryMessenger.

Definition at line 240 of file DartMessenger.java.

240 {
241 enableBufferingIncomingMessages.set(true);
242 }

◆ getPendingChannelResponseCount()

int io.flutter.embedding.engine.dart.DartMessenger.getPendingChannelResponseCount ( )
inline

Returns the number of pending channel callback replies.

When sending messages to the Flutter application using ByteBuffer, io.flutter.plugin.common.BinaryMessenger.BinaryReply), developers can optionally specify a reply callback if they expect a reply from the Flutter application.

This method tracks all the pending callbacks that are waiting for response, and is supposed to be called from the main thread (as other methods). Calling from a different thread could possibly capture an indeterministic internal state, so don't do it.

Definition at line 402 of file DartMessenger.java.

402 {
403 return pendingReplies.size();
404 }

◆ handleMessageFromDart()

void io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart ( @NonNull String  channel,
@Nullable ByteBuffer  message,
int  replyId,
long  messageData 
)
inline

Called from any thread.

Implements io.flutter.embedding.engine.dart.PlatformMessageHandler.

Definition at line 336 of file DartMessenger.java.

337 {
338 // Called from any thread.
339 Log.v(TAG, "Received message from Dart over channel '" + channel + "'");
340
341 HandlerInfo handlerInfo;
342 boolean messageDeferred;
343 // This lock can potentially be a bottleneck and could replaced with a
344 // read/write lock.
345 synchronized (handlersLock) {
346 handlerInfo = messageHandlers.get(channel);
347 messageDeferred = (enableBufferingIncomingMessages.get() && handlerInfo == null);
348 if (messageDeferred) {
349 // The channel is not defined when the Dart VM sends a message before the channels are
350 // registered.
351 //
352 // This is possible if the Dart VM starts before channel registration, and if the thread
353 // that registers the channels is busy or slow at registering the channel handlers.
354 //
355 // In such cases, the task dispatchers are queued, and processed when the channel is
356 // defined.
357 if (!bufferedMessages.containsKey(channel)) {
358 bufferedMessages.put(channel, new LinkedList<>());
359 }
360 List<BufferedMessageInfo> buffer = bufferedMessages.get(channel);
361 buffer.add(new BufferedMessageInfo(message, replyId, messageData));
362 }
363 }
364 if (!messageDeferred) {
365 dispatchMessageToQueue(channel, handlerInfo, message, replyId, messageData);
366 }
367 }
Win32Message message
void Log(const char *format,...) SK_PRINTF_LIKE(1
Definition: TestRunner.cpp:137
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126

◆ handlePlatformMessageResponse()

void io.flutter.embedding.engine.dart.DartMessenger.handlePlatformMessageResponse ( int  replyId,
@Nullable ByteBuffer  reply 
)
inline

Implements io.flutter.embedding.engine.dart.PlatformMessageHandler.

Definition at line 370 of file DartMessenger.java.

370 {
371 Log.v(TAG, "Received message reply from Dart.");
372 BinaryMessenger.BinaryReply callback = pendingReplies.remove(replyId);
373 if (callback != null) {
374 try {
375 Log.v(TAG, "Invoking registered callback for reply from Dart.");
376 callback.reply(reply);
377 if (reply != null && reply.isDirect()) {
378 // This ensures that if a user retains an instance to the ByteBuffer and it happens to
379 // be direct they will get a deterministic error.
380 reply.limit(0);
381 }
382 } catch (Exception ex) {
383 Log.e(TAG, "Uncaught exception in binary message reply handler", ex);
384 } catch (Error err) {
385 handleError(err);
386 }
387 }
388 }
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback

◆ makeBackgroundTaskQueue()

TaskQueue io.flutter.embedding.engine.dart.DartMessenger.makeBackgroundTaskQueue ( TaskQueueOptions  options)
inline

Creates a TaskQueue that executes the tasks serially on a background thread.

TaskQueueOptions can be used to configure the task queue to execute tasks concurrently. Doing so can be more performant, though users need to ensure that the task handlers are thread-safe.

Implements io.flutter.plugin.common.BinaryMessenger.

Definition at line 190 of file DartMessenger.java.

190 {
191 DartMessengerTaskQueue taskQueue = taskQueueFactory.makeBackgroundTaskQueue(options);
192 TaskQueueToken token = new TaskQueueToken();
193 createdTaskQueues.put(token, taskQueue);
194 return token;
195 }
const char * options
DartMessengerTaskQueue makeBackgroundTaskQueue(TaskQueueOptions options)

◆ send() [1/2]

void io.flutter.embedding.engine.dart.DartMessenger.send ( @NonNull String  channel,
@Nullable ByteBuffer  message,
@Nullable BinaryMessenger.BinaryReply  callback 
)
inline

Definition at line 268 of file DartMessenger.java.

271 {
272 try (TraceSection e = TraceSection.scoped("DartMessenger#send on " + channel)) {
273 Log.v(TAG, "Sending message with callback over channel '" + channel + "'");
274 int replyId = nextReplyId++;
275 if (callback != null) {
276 pendingReplies.put(replyId, callback);
277 }
278 if (message == null) {
279 flutterJNI.dispatchEmptyPlatformMessage(channel, replyId);
280 } else {
281 flutterJNI.dispatchPlatformMessage(channel, message, message.position(), replyId);
282 }
283 }
284 }
void dispatchEmptyPlatformMessage(@NonNull String channel, int responseId)
void dispatchPlatformMessage( @NonNull String channel, @Nullable ByteBuffer message, int position, int responseId)

◆ send() [2/2]

void io.flutter.embedding.engine.dart.DartMessenger.send ( @NonNull String  channel,
@NonNull ByteBuffer  message 
)
inline

Definition at line 262 of file DartMessenger.java.

262 {
263 Log.v(TAG, "Sending message over channel '" + channel + "'");
264 send(channel, message, null);
265 }
void send(@NonNull String channel, @NonNull ByteBuffer message)

◆ setMessageHandler() [1/2]

void io.flutter.embedding.engine.dart.DartMessenger.setMessageHandler ( @NonNull String  channel,
@Nullable BinaryMessenger.BinaryMessageHandler  handler 
)
inline

Definition at line 198 of file DartMessenger.java.

199 {
200 setMessageHandler(channel, handler, null);
201 }
void setMessageHandler( @NonNull String channel, @Nullable BinaryMessenger.BinaryMessageHandler handler)

◆ setMessageHandler() [2/2]

void io.flutter.embedding.engine.dart.DartMessenger.setMessageHandler ( @NonNull String  channel,
@Nullable BinaryMessenger.BinaryMessageHandler  handler,
@Nullable TaskQueue  taskQueue 
)
inline

Definition at line 204 of file DartMessenger.java.

207 {
208 if (handler == null) {
209 Log.v(TAG, "Removing handler for channel '" + channel + "'");
210 synchronized (handlersLock) {
211 messageHandlers.remove(channel);
212 }
213 return;
214 }
215 DartMessengerTaskQueue dartMessengerTaskQueue = null;
216 if (taskQueue != null) {
217 dartMessengerTaskQueue = createdTaskQueues.get(taskQueue);
218 if (dartMessengerTaskQueue == null) {
219 throw new IllegalArgumentException(
220 "Unrecognized TaskQueue, use BinaryMessenger to create your TaskQueue (ex makeBackgroundTaskQueue).");
221 }
222 }
223 Log.v(TAG, "Setting handler for channel '" + channel + "'");
224
226 synchronized (handlersLock) {
227 messageHandlers.put(channel, new HandlerInfo(handler, dartMessengerTaskQueue));
228 list = bufferedMessages.remove(channel);
229 if (list == null) {
230 return;
231 }
232 }
233 for (BufferedMessageInfo info : list) {
234 dispatchMessageToQueue(
235 channel, messageHandlers.get(channel), info.message, info.replyId, info.messageData);
236 }
237 }

The documentation for this class was generated from the following file: