FlutterChannels.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. #ifndef FLUTTER_FLUTTERCHANNELS_H_
  5. #define FLUTTER_FLUTTERCHANNELS_H_
  6. #include "FlutterBinaryMessenger.h"
  7. #include "FlutterCodecs.h"
  8. NS_ASSUME_NONNULL_BEGIN
  9. /**
  10. * A message reply callback.
  11. *
  12. * Used for submitting a reply back to a Flutter message sender. Also used in
  13. * the dual capacity for handling a message reply received from Flutter.
  14. *
  15. * @param reply The reply.
  16. */
  17. typedef void (^FlutterReply)(id _Nullable reply);
  18. /**
  19. * A strategy for handling incoming messages from Flutter and to send
  20. * asynchronous replies back to Flutter.
  21. *
  22. * @param message The message.
  23. * @param callback A callback for submitting a reply to the sender.
  24. */
  25. typedef void (^FlutterMessageHandler)(id _Nullable message, FlutterReply callback);
  26. /**
  27. * A channel for communicating with the Flutter side using basic, asynchronous
  28. * message passing.
  29. */
  30. FLUTTER_EXPORT
  31. @interface FlutterBasicMessageChannel : NSObject
  32. /**
  33. * Creates a `FlutterBasicMessageChannel` with the specified name and binary
  34. * messenger.
  35. *
  36. * The channel name logically identifies the channel; identically named channels
  37. * interfere with each other's communication.
  38. *
  39. * The binary messenger is a facility for sending raw, binary messages to the
  40. * Flutter side. This protocol is implemented by `FlutterEngine` and `FlutterViewController`.
  41. *
  42. * The channel uses `FlutterStandardMessageCodec` to encode and decode messages.
  43. *
  44. * @param name The channel name.
  45. * @param messenger The binary messenger.
  46. */
  47. + (instancetype)messageChannelWithName:(NSString*)name
  48. binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger;
  49. /**
  50. * Creates a `FlutterBasicMessageChannel` with the specified name, binary
  51. * messenger, and message codec.
  52. *
  53. * The channel name logically identifies the channel; identically named channels
  54. * interfere with each other's communication.
  55. *
  56. * The binary messenger is a facility for sending raw, binary messages to the
  57. * Flutter side. This protocol is implemented by `FlutterEngine` and `FlutterViewController`.
  58. *
  59. * @param name The channel name.
  60. * @param messenger The binary messenger.
  61. * @param codec The message codec.
  62. */
  63. + (instancetype)messageChannelWithName:(NSString*)name
  64. binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
  65. codec:(NSObject<FlutterMessageCodec>*)codec;
  66. /**
  67. * Initializes a `FlutterBasicMessageChannel` with the specified name, binary
  68. * messenger, and message codec.
  69. *
  70. * The channel name logically identifies the channel; identically named channels
  71. * interfere with each other's communication.
  72. *
  73. * The binary messenger is a facility for sending raw, binary messages to the
  74. * Flutter side. This protocol is implemented by `FlutterEngine` and `FlutterViewController`.
  75. *
  76. * @param name The channel name.
  77. * @param messenger The binary messenger.
  78. * @param codec The message codec.
  79. */
  80. - (instancetype)initWithName:(NSString*)name
  81. binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
  82. codec:(NSObject<FlutterMessageCodec>*)codec;
  83. /**
  84. * Sends the specified message to the Flutter side, ignoring any reply.
  85. *
  86. * @param message The message. Must be supported by the codec of this
  87. * channel.
  88. */
  89. - (void)sendMessage:(id _Nullable)message;
  90. /**
  91. * Sends the specified message to the Flutter side, expecting an asynchronous
  92. * reply.
  93. *
  94. * @param message The message. Must be supported by the codec of this channel.
  95. * @param callback A callback to be invoked with the message reply from Flutter.
  96. */
  97. - (void)sendMessage:(id _Nullable)message reply:(FlutterReply _Nullable)callback;
  98. /**
  99. * Registers a message handler with this channel.
  100. *
  101. * Replaces any existing handler. Use a `nil` handler for unregistering the
  102. * existing handler.
  103. *
  104. * @param handler The message handler.
  105. */
  106. - (void)setMessageHandler:(FlutterMessageHandler _Nullable)handler;
  107. /**
  108. * Adjusts the number of messages that will get buffered when sending messages to
  109. * channels that aren't fully setup yet. For example, the engine isn't running
  110. * yet or the channel's message handler isn't setup on the Dart side yet.
  111. */
  112. - (void)resizeChannelBuffer:(NSInteger)newSize;
  113. @end
  114. /**
  115. * A method call result callback.
  116. *
  117. * Used for submitting a method call result back to a Flutter caller. Also used in
  118. * the dual capacity for handling a method call result received from Flutter.
  119. *
  120. * @param result The result.
  121. */
  122. typedef void (^FlutterResult)(id _Nullable result);
  123. /**
  124. * A strategy for handling method calls.
  125. *
  126. * @param call The incoming method call.
  127. * @param result A callback to asynchronously submit the result of the call.
  128. * Invoke the callback with a `FlutterError` to indicate that the call failed.
  129. * Invoke the callback with `FlutterMethodNotImplemented` to indicate that the
  130. * method was unknown. Any other values, including `nil`, are interpreted as
  131. * successful results.
  132. */
  133. typedef void (^FlutterMethodCallHandler)(FlutterMethodCall* call, FlutterResult result);
  134. /**
  135. * A constant used with `FlutterMethodCallHandler` to respond to the call of an
  136. * unknown method.
  137. */
  138. FLUTTER_EXPORT
  139. extern NSObject const* FlutterMethodNotImplemented;
  140. /**
  141. * A channel for communicating with the Flutter side using invocation of
  142. * asynchronous methods.
  143. */
  144. FLUTTER_EXPORT
  145. @interface FlutterMethodChannel : NSObject
  146. /**
  147. * Creates a `FlutterMethodChannel` with the specified name and binary messenger.
  148. *
  149. * The channel name logically identifies the channel; identically named channels
  150. * interfere with each other's communication.
  151. *
  152. * The binary messenger is a facility for sending raw, binary messages to the
  153. * Flutter side. This protocol is implemented by `FlutterEngine` and `FlutterViewController`.
  154. *
  155. * The channel uses `FlutterStandardMethodCodec` to encode and decode method calls
  156. * and result envelopes.
  157. *
  158. * @param name The channel name.
  159. * @param messenger The binary messenger.
  160. */
  161. + (instancetype)methodChannelWithName:(NSString*)name
  162. binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger;
  163. /**
  164. * Creates a `FlutterMethodChannel` with the specified name, binary messenger, and
  165. * method codec.
  166. *
  167. * The channel name logically identifies the channel; identically named channels
  168. * interfere with each other's communication.
  169. *
  170. * The binary messenger is a facility for sending raw, binary messages to the
  171. * Flutter side. This protocol is implemented by `FlutterEngine` and `FlutterViewController`.
  172. *
  173. * @param name The channel name.
  174. * @param messenger The binary messenger.
  175. * @param codec The method codec.
  176. */
  177. + (instancetype)methodChannelWithName:(NSString*)name
  178. binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
  179. codec:(NSObject<FlutterMethodCodec>*)codec;
  180. /**
  181. * Initializes a `FlutterMethodChannel` with the specified name, binary messenger,
  182. * and method codec.
  183. *
  184. * The channel name logically identifies the channel; identically named channels
  185. * interfere with each other's communication.
  186. *
  187. * The binary messenger is a facility for sending raw, binary messages to the
  188. * Flutter side. This protocol is implemented by `FlutterEngine` and `FlutterViewController`.
  189. *
  190. * @param name The channel name.
  191. * @param messenger The binary messenger.
  192. * @param codec The method codec.
  193. */
  194. - (instancetype)initWithName:(NSString*)name
  195. binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
  196. codec:(NSObject<FlutterMethodCodec>*)codec;
  197. // clang-format off
  198. /**
  199. * Invokes the specified Flutter method with the specified arguments, expecting
  200. * no results.
  201. *
  202. * @see [MethodChannel.setMethodCallHandler](https://docs.flutter.io/flutter/services/MethodChannel/setMethodCallHandler.html)
  203. *
  204. * @param method The name of the method to invoke.
  205. * @param arguments The arguments. Must be a value supported by the codec of this
  206. * channel.
  207. */
  208. // clang-format on
  209. - (void)invokeMethod:(NSString*)method arguments:(id _Nullable)arguments;
  210. /**
  211. * Invokes the specified Flutter method with the specified arguments, expecting
  212. * an asynchronous result.
  213. *
  214. * @param method The name of the method to invoke.
  215. * @param arguments The arguments. Must be a value supported by the codec of this
  216. * channel.
  217. * @param callback A callback that will be invoked with the asynchronous result.
  218. * The result will be a `FlutterError` instance, if the method call resulted
  219. * in an error on the Flutter side. Will be `FlutterMethodNotImplemented`, if
  220. * the method called was not implemented on the Flutter side. Any other value,
  221. * including `nil`, should be interpreted as successful results.
  222. */
  223. - (void)invokeMethod:(NSString*)method
  224. arguments:(id _Nullable)arguments
  225. result:(FlutterResult _Nullable)callback;
  226. /**
  227. * Registers a handler for method calls from the Flutter side.
  228. *
  229. * Replaces any existing handler. Use a `nil` handler for unregistering the
  230. * existing handler.
  231. *
  232. * @param handler The method call handler.
  233. */
  234. - (void)setMethodCallHandler:(FlutterMethodCallHandler _Nullable)handler;
  235. /**
  236. * Adjusts the number of messages that will get buffered when sending messages to
  237. * channels that aren't fully setup yet. For example, the engine isn't running
  238. * yet or the channel's message handler isn't setup on the Dart side yet.
  239. */
  240. - (void)resizeChannelBuffer:(NSInteger)newSize;
  241. @end
  242. /**
  243. * An event sink callback.
  244. *
  245. * @param event The event.
  246. */
  247. typedef void (^FlutterEventSink)(id _Nullable event);
  248. /**
  249. * A strategy for exposing an event stream to the Flutter side.
  250. */
  251. FLUTTER_EXPORT
  252. @protocol FlutterStreamHandler
  253. /**
  254. * Sets up an event stream and begin emitting events.
  255. *
  256. * Invoked when the first listener is registered with the Stream associated to
  257. * this channel on the Flutter side.
  258. *
  259. * @param arguments Arguments for the stream.
  260. * @param events A callback to asynchronously emit events. Invoke the
  261. * callback with a `FlutterError` to emit an error event. Invoke the
  262. * callback with `FlutterEndOfEventStream` to indicate that no more
  263. * events will be emitted. Any other value, including `nil` are emitted as
  264. * successful events.
  265. * @return A FlutterError instance, if setup fails.
  266. */
  267. - (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
  268. eventSink:(FlutterEventSink)events;
  269. /**
  270. * Tears down an event stream.
  271. *
  272. * Invoked when the last listener is deregistered from the Stream associated to
  273. * this channel on the Flutter side.
  274. *
  275. * The channel implementation may call this method with `nil` arguments
  276. * to separate a pair of two consecutive set up requests. Such request pairs
  277. * may occur during Flutter hot restart.
  278. *
  279. * @param arguments Arguments for the stream.
  280. * @return A FlutterError instance, if teardown fails.
  281. */
  282. - (FlutterError* _Nullable)onCancelWithArguments:(id _Nullable)arguments;
  283. @end
  284. /**
  285. * A constant used with `FlutterEventChannel` to indicate end of stream.
  286. */
  287. FLUTTER_EXPORT
  288. extern NSObject const* FlutterEndOfEventStream;
  289. /**
  290. * A channel for communicating with the Flutter side using event streams.
  291. */
  292. FLUTTER_EXPORT
  293. @interface FlutterEventChannel : NSObject
  294. /**
  295. * Creates a `FlutterEventChannel` with the specified name and binary messenger.
  296. *
  297. * The channel name logically identifies the channel; identically named channels
  298. * interfere with each other's communication.
  299. *
  300. * The binary messenger is a facility for sending raw, binary messages to the
  301. * Flutter side. This protocol is implemented by `FlutterViewController`.
  302. *
  303. * The channel uses `FlutterStandardMethodCodec` to decode stream setup and
  304. * teardown requests, and to encode event envelopes.
  305. *
  306. * @param name The channel name.
  307. * @param messenger The binary messenger.
  308. */
  309. + (instancetype)eventChannelWithName:(NSString*)name
  310. binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger;
  311. /**
  312. * Creates a `FlutterEventChannel` with the specified name, binary messenger,
  313. * and method codec.
  314. *
  315. * The channel name logically identifies the channel; identically named channels
  316. * interfere with each other's communication.
  317. *
  318. * The binary messenger is a facility for sending raw, binary messages to the
  319. * Flutter side. This protocol is implemented by `FlutterViewController`.
  320. *
  321. * @param name The channel name.
  322. * @param messenger The binary messenger.
  323. * @param codec The method codec.
  324. */
  325. + (instancetype)eventChannelWithName:(NSString*)name
  326. binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
  327. codec:(NSObject<FlutterMethodCodec>*)codec;
  328. /**
  329. * Initializes a `FlutterEventChannel` with the specified name, binary messenger,
  330. * and method codec.
  331. *
  332. * The channel name logically identifies the channel; identically named channels
  333. * interfere with each other's communication.
  334. *
  335. * The binary messenger is a facility for sending raw, binary messages to the
  336. * Flutter side. This protocol is implemented by `FlutterEngine` and `FlutterViewController`.
  337. *
  338. * @param name The channel name.
  339. * @param messenger The binary messenger.
  340. * @param codec The method codec.
  341. */
  342. - (instancetype)initWithName:(NSString*)name
  343. binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger
  344. codec:(NSObject<FlutterMethodCodec>*)codec;
  345. /**
  346. * Registers a handler for stream setup requests from the Flutter side.
  347. *
  348. * Replaces any existing handler. Use a `nil` handler for unregistering the
  349. * existing handler.
  350. *
  351. * @param handler The stream handler.
  352. */
  353. - (void)setStreamHandler:(NSObject<FlutterStreamHandler>* _Nullable)handler;
  354. @end
  355. NS_ASSUME_NONNULL_END
  356. #endif // FLUTTER_FLUTTERCHANNELS_H_