Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
BinaryMessenger.java
Go to the documentation of this file.
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
5package io.flutter.plugin.common;
6
7import androidx.annotation.NonNull;
8import androidx.annotation.Nullable;
9import androidx.annotation.UiThread;
10import java.nio.ByteBuffer;
11
12/**
13 * Facility for communicating with Flutter using asynchronous message passing with binary messages.
14 * The Flutter Dart code should use <a
15 * href="https://api.flutter.dev/flutter/services/BinaryMessages-class.html">BinaryMessages</a> to
16 * participate.
17 *
18 * <p>{@code BinaryMessenger} is expected to be utilized from a single thread throughout the
19 * duration of its existence. If created on the main thread, then all invocations should take place
20 * on the main thread. If created on a background thread, then all invocations should take place on
21 * that background thread.
22 *
23 * @see BasicMessageChannel , which supports message passing with Strings and semi-structured
24 * messages.
25 * @see MethodChannel , which supports communication using asynchronous method invocation.
26 * @see EventChannel , which supports communication using event streams.
27 */
28public interface BinaryMessenger {
29 /**
30 * An abstraction over the threading policy used to invoke message handlers.
31 *
32 * <p>These are generated by calling methods like {@link
33 * BinaryMessenger#makeBackgroundTaskQueue(TaskQueueOptions)} and can be passed into platform
34 * channels' constructors to control the threading policy for handling platform channels'
35 * messages.
36 */
37 public interface TaskQueue {}
38
39 /** Options that control how a TaskQueue should operate and be created. */
40 public static class TaskQueueOptions {
41 private boolean isSerial = true;
42
43 public boolean getIsSerial() {
44 return isSerial;
45 }
46
47 /**
48 * Setter for `isSerial` property.
49 *
50 * <p>When this is true all tasks performed by the TaskQueue will be forced to happen serially
51 * (one completes before the other begins).
52 */
53 public TaskQueueOptions setIsSerial(boolean isSerial) {
54 this.isSerial = isSerial;
55 return this;
56 }
57 }
58
59 /**
60 * Creates a TaskQueue that executes the tasks serially on a background thread.
61 *
62 * <p>There is no guarantee that the tasks will execute on the same thread, just that execution is
63 * serial. This is could be problematic if your code relies on ThreadLocal storage or
64 * introspection about what thread is actually executing.
65 */
66 @UiThread
70
71 /**
72 * Creates a TaskQueue that executes the tasks serially on a background thread.
73 *
74 * <p>{@link TaskQueueOptions} can be used to configure the task queue to execute tasks
75 * concurrently. Doing so can be more performant, though users need to ensure that the task
76 * handlers are thread-safe.
77 */
78 @UiThread
80 // TODO(92582): Remove default implementation when it is safe for Google Flutter users.
81 throw new UnsupportedOperationException("makeBackgroundTaskQueue not implemented.");
82 }
83
84 /**
85 * Sends a binary message to the Flutter application.
86 *
87 * @param channel the name {@link String} of the logical channel used for the message.
88 * @param message the message payload, a direct-allocated {@link ByteBuffer} with the message
89 * bytes between position zero and current position, or null.
90 */
91 @UiThread
92 void send(@NonNull String channel, @Nullable ByteBuffer message);
93
94 /**
95 * Sends a binary message to the Flutter application, optionally expecting a reply.
96 *
97 * <p>Any uncaught exception thrown by the reply callback will be caught and logged.
98 *
99 * @param channel the name {@link String} of the logical channel used for the message.
100 * @param message the message payload, a direct-allocated {@link ByteBuffer} with the message
101 * bytes between position zero and current position, or null.
102 * @param callback a {@link BinaryReply} callback invoked when the Flutter application responds to
103 * the message, possibly null.
104 */
105 @UiThread
106 void send(@NonNull String channel, @Nullable ByteBuffer message, @Nullable BinaryReply callback);
107
108 /**
109 * Registers a handler to be invoked when the Flutter application sends a message to its host
110 * platform.
111 *
112 * <p>Registration overwrites any previous registration for the same channel name. Use a null
113 * handler to deregister.
114 *
115 * <p>If no handler has been registered for a particular channel, any incoming message on that
116 * channel will be handled silently by sending a null reply.
117 *
118 * @param channel the name {@link String} of the channel.
119 * @param handler a {@link BinaryMessageHandler} to be invoked on incoming messages, or null.
120 */
121 @UiThread
122 void setMessageHandler(@NonNull String channel, @Nullable BinaryMessageHandler handler);
123
124 /**
125 * Registers a handler to be invoked when the Flutter application sends a message to its host
126 * platform.
127 *
128 * <p>Registration overwrites any previous registration for the same channel name. Use a null
129 * handler to deregister.
130 *
131 * <p>If no handler has been registered for a particular channel, any incoming message on that
132 * channel will be handled silently by sending a null reply.
133 *
134 * @param channel the name {@link String} of the channel.
135 * @param handler a {@link BinaryMessageHandler} to be invoked on incoming messages, or null.
136 * @param taskQueue a {@link BinaryMessenger.TaskQueue} that specifies what thread will execute
137 * the handler. Specifying null means execute on the platform thread.
138 */
139 @UiThread
140 default void setMessageHandler(
141 @NonNull String channel,
142 @Nullable BinaryMessageHandler handler,
143 @Nullable TaskQueue taskQueue) {
144 // TODO(92582): Remove default implementation when it is safe for Google Flutter users.
145 if (taskQueue != null) {
146 throw new UnsupportedOperationException(
147 "setMessageHandler called with nonnull taskQueue is not supported.");
148 }
149 setMessageHandler(channel, handler);
150 }
151
152 /**
153 * Enables the ability to queue messages received from Dart.
154 *
155 * <p>This is useful when there are pending channel handler registrations. For example, Dart may
156 * be initialized concurrently, and prior to the registration of the channel handlers. This
157 * implies that Dart may start sending messages while plugins are being registered.
158 */
160 throw new UnsupportedOperationException("enableBufferingIncomingMessages not implemented.");
161 }
162
163 /**
164 * Disables the ability to queue messages received from Dart.
165 *
166 * <p>This can be used after all pending channel handlers have been registered.
167 */
169 throw new UnsupportedOperationException("disableBufferingIncomingMessages not implemented.");
170 }
171
172 /** Handler for incoming binary messages from Flutter. */
174 /**
175 * Handles the specified message.
176 *
177 * <p>Handler implementations must reply to all incoming messages, by submitting a single reply
178 * message to the given {@link BinaryReply}. Failure to do so will result in lingering Flutter
179 * reply handlers. The reply may be submitted asynchronously.
180 *
181 * <p>Any uncaught exception thrown by this method will be caught by the messenger
182 * implementation and logged, and a null reply message will be sent back to Flutter.
183 *
184 * @param message the message {@link ByteBuffer} payload, possibly null.
185 * @param reply A {@link BinaryReply} used for submitting a reply back to Flutter.
186 */
187 @UiThread
188 void onMessage(@Nullable ByteBuffer message, @NonNull BinaryReply reply);
189 }
190
191 /**
192 * Binary message reply callback. Used to submit a reply to an incoming message from Flutter. Also
193 * used in the dual capacity to handle a reply received from Flutter after sending a message.
194 */
195 interface BinaryReply {
196 /**
197 * Handles the specified reply.
198 *
199 * @param reply the reply payload, a direct-allocated {@link ByteBuffer} or null. Senders of
200 * outgoing replies must place the reply bytes between position zero and current position.
201 * Reply receivers can read from the buffer directly.
202 */
203 void reply(@Nullable ByteBuffer reply);
204 }
205}
const char * options
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
void onMessage(@Nullable ByteBuffer message, @NonNull BinaryReply reply)
void setMessageHandler(@NonNull String channel, @Nullable BinaryMessageHandler handler)
void send(@NonNull String channel, @Nullable ByteBuffer message, @Nullable BinaryReply callback)
default TaskQueue makeBackgroundTaskQueue(TaskQueueOptions options)
default void setMessageHandler( @NonNull String channel, @Nullable BinaryMessageHandler handler, @Nullable TaskQueue taskQueue)
void send(@NonNull String channel, @Nullable ByteBuffer message)
Win32Message message