FlutterCodecs.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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_FLUTTERCODECS_H_
  5. #define FLUTTER_FLUTTERCODECS_H_
  6. #import <Foundation/Foundation.h>
  7. #include "FlutterMacros.h"
  8. NS_ASSUME_NONNULL_BEGIN
  9. /**
  10. * A message encoding/decoding mechanism.
  11. */
  12. FLUTTER_EXPORT
  13. @protocol FlutterMessageCodec
  14. /**
  15. * Returns a shared instance of this `FlutterMessageCodec`.
  16. */
  17. + (instancetype)sharedInstance;
  18. /**
  19. * Encodes the specified message into binary.
  20. *
  21. * @param message The message.
  22. * @return The binary encoding, or `nil`, if `message` was `nil`.
  23. */
  24. - (NSData* _Nullable)encode:(id _Nullable)message;
  25. /**
  26. * Decodes the specified message from binary.
  27. *
  28. * @param message The message.
  29. * @return The decoded message, or `nil`, if `message` was `nil`.
  30. */
  31. - (id _Nullable)decode:(NSData* _Nullable)message;
  32. @end
  33. /**
  34. * A `FlutterMessageCodec` using unencoded binary messages, represented as
  35. * `NSData` instances.
  36. *
  37. * This codec is guaranteed to be compatible with the corresponding
  38. * [BinaryCodec](https://docs.flutter.io/flutter/services/BinaryCodec-class.html)
  39. * on the Dart side. These parts of the Flutter SDK are evolved synchronously.
  40. *
  41. * On the Dart side, messages are represented using `ByteData`.
  42. */
  43. FLUTTER_EXPORT
  44. @interface FlutterBinaryCodec : NSObject <FlutterMessageCodec>
  45. @end
  46. /**
  47. * A `FlutterMessageCodec` using UTF-8 encoded `NSString` messages.
  48. *
  49. * This codec is guaranteed to be compatible with the corresponding
  50. * [StringCodec](https://docs.flutter.io/flutter/services/StringCodec-class.html)
  51. * on the Dart side. These parts of the Flutter SDK are evolved synchronously.
  52. */
  53. FLUTTER_EXPORT
  54. @interface FlutterStringCodec : NSObject <FlutterMessageCodec>
  55. @end
  56. /**
  57. * A `FlutterMessageCodec` using UTF-8 encoded JSON messages.
  58. *
  59. * This codec is guaranteed to be compatible with the corresponding
  60. * [JSONMessageCodec](https://docs.flutter.io/flutter/services/JSONMessageCodec-class.html)
  61. * on the Dart side. These parts of the Flutter SDK are evolved synchronously.
  62. *
  63. * Supports values accepted by `NSJSONSerialization` plus top-level
  64. * `nil`, `NSNumber`, and `NSString`.
  65. *
  66. * On the Dart side, JSON messages are handled by the JSON facilities of the
  67. * [`dart:convert`](https://api.dartlang.org/stable/dart-convert/JSON-constant.html)
  68. * package.
  69. */
  70. FLUTTER_EXPORT
  71. @interface FlutterJSONMessageCodec : NSObject <FlutterMessageCodec>
  72. @end
  73. /**
  74. * A writer of the Flutter standard binary encoding.
  75. *
  76. * See `FlutterStandardMessageCodec` for details on the encoding.
  77. *
  78. * The encoding is extensible via subclasses overriding `writeValue`.
  79. */
  80. FLUTTER_EXPORT
  81. @interface FlutterStandardWriter : NSObject
  82. /**
  83. * Create a `FlutterStandardWriter` who will write to \p data.
  84. */
  85. - (instancetype)initWithData:(NSMutableData*)data;
  86. /** Write a 8-bit byte. */
  87. - (void)writeByte:(UInt8)value;
  88. /** Write an array of \p bytes of size \p length. */
  89. - (void)writeBytes:(const void*)bytes length:(NSUInteger)length;
  90. /** Write an array of bytes contained in \p data. */
  91. - (void)writeData:(NSData*)data;
  92. /** Write 32-bit unsigned integer that represents a \p size of a collection. */
  93. - (void)writeSize:(UInt32)size;
  94. /** Write zero padding until data is aligned with \p alignment. */
  95. - (void)writeAlignment:(UInt8)alignment;
  96. /** Write a string with UTF-8 encoding. */
  97. - (void)writeUTF8:(NSString*)value;
  98. /** Introspects into an object and writes its representation.
  99. *
  100. * Supported Data Types:
  101. * - NSNull
  102. * - NSNumber
  103. * - NSString (as UTF-8)
  104. * - FlutterStandardTypedData
  105. * - NSArray of supported types
  106. * - NSDictionary of supporte types
  107. *
  108. * NSAsserts on failure.
  109. */
  110. - (void)writeValue:(id)value;
  111. @end
  112. /**
  113. * A reader of the Flutter standard binary encoding.
  114. *
  115. * See `FlutterStandardMessageCodec` for details on the encoding.
  116. *
  117. * The encoding is extensible via subclasses overriding `readValueOfType`.
  118. */
  119. FLUTTER_EXPORT
  120. @interface FlutterStandardReader : NSObject
  121. /**
  122. * Create a new `FlutterStandardReader` who reads from \p data.
  123. */
  124. - (instancetype)initWithData:(NSData*)data;
  125. /** Returns YES when the reader hasn't reached the end of its data. */
  126. - (BOOL)hasMore;
  127. /** Reads a byte value and increments the position. */
  128. - (UInt8)readByte;
  129. /** Reads a sequence of byte values of \p length and increments the position. */
  130. - (void)readBytes:(void*)destination length:(NSUInteger)length;
  131. /** Reads a sequence of byte values of \p length and increments the position. */
  132. - (NSData*)readData:(NSUInteger)length;
  133. /** Reads a 32-bit unsigned integer representing a collection size and increments the position.*/
  134. - (UInt32)readSize;
  135. /** Advances the read position until it is aligned with \p alignment. */
  136. - (void)readAlignment:(UInt8)alignment;
  137. /** Read a null terminated string encoded with UTF-8/ */
  138. - (NSString*)readUTF8;
  139. /**
  140. * Reads a byte for `FlutterStandardField` the decodes a value matching that type.
  141. *
  142. * See also: -[FlutterStandardWriter writeValue]
  143. */
  144. - (nullable id)readValue;
  145. /**
  146. * Decodes a value matching the \p type specified.
  147. *
  148. * See also:
  149. * - `FlutterStandardField`
  150. * - `-[FlutterStandardWriter writeValue]`
  151. */
  152. - (nullable id)readValueOfType:(UInt8)type;
  153. @end
  154. /**
  155. * A factory of compatible reader/writer instances using the Flutter standard
  156. * binary encoding or extensions thereof.
  157. */
  158. FLUTTER_EXPORT
  159. @interface FlutterStandardReaderWriter : NSObject
  160. /**
  161. * Create a new `FlutterStandardWriter` for writing to \p data.
  162. */
  163. - (FlutterStandardWriter*)writerWithData:(NSMutableData*)data;
  164. /**
  165. * Create a new `FlutterStandardReader` for reading from \p data.
  166. */
  167. - (FlutterStandardReader*)readerWithData:(NSData*)data;
  168. @end
  169. /**
  170. * A `FlutterMessageCodec` using the Flutter standard binary encoding.
  171. *
  172. * This codec is guaranteed to be compatible with the corresponding
  173. * [StandardMessageCodec](https://docs.flutter.io/flutter/services/StandardMessageCodec-class.html)
  174. * on the Dart side. These parts of the Flutter SDK are evolved synchronously.
  175. *
  176. * Supported messages are acyclic values of these forms:
  177. *
  178. * - `nil` or `NSNull`
  179. * - `NSNumber` (including their representation of Boolean values)
  180. * - `NSString`
  181. * - `FlutterStandardTypedData`
  182. * - `NSArray` of supported values
  183. * - `NSDictionary` with supported keys and values
  184. *
  185. * On the Dart side, these values are represented as follows:
  186. *
  187. * - `nil` or `NSNull`: null
  188. * - `NSNumber`: `bool`, `int`, or `double`, depending on the contained value.
  189. * - `NSString`: `String`
  190. * - `FlutterStandardTypedData`: `Uint8List`, `Int32List`, `Int64List`, or `Float64List`
  191. * - `NSArray`: `List`
  192. * - `NSDictionary`: `Map`
  193. */
  194. FLUTTER_EXPORT
  195. @interface FlutterStandardMessageCodec : NSObject <FlutterMessageCodec>
  196. /**
  197. * Create a `FlutterStandardMessageCodec` who will read and write to \p readerWriter.
  198. */
  199. + (instancetype)codecWithReaderWriter:(FlutterStandardReaderWriter*)readerWriter;
  200. @end
  201. /**
  202. * Command object representing a method call on a `FlutterMethodChannel`.
  203. */
  204. FLUTTER_EXPORT
  205. @interface FlutterMethodCall : NSObject
  206. /**
  207. * Creates a method call for invoking the specified named method with the
  208. * specified arguments.
  209. *
  210. * @param method the name of the method to call.
  211. * @param arguments the arguments value.
  212. */
  213. + (instancetype)methodCallWithMethodName:(NSString*)method arguments:(id _Nullable)arguments;
  214. /**
  215. * The method name.
  216. */
  217. @property(readonly, nonatomic) NSString* method;
  218. /**
  219. * The arguments.
  220. */
  221. @property(readonly, nonatomic, nullable) id arguments;
  222. @end
  223. /**
  224. * Error object representing an unsuccessful outcome of invoking a method
  225. * on a `FlutterMethodChannel`, or an error event on a `FlutterEventChannel`.
  226. */
  227. FLUTTER_EXPORT
  228. @interface FlutterError : NSObject
  229. /**
  230. * Creates a `FlutterError` with the specified error code, message, and details.
  231. *
  232. * @param code An error code string for programmatic use.
  233. * @param message A human-readable error message.
  234. * @param details Custom error details.
  235. */
  236. + (instancetype)errorWithCode:(NSString*)code
  237. message:(NSString* _Nullable)message
  238. details:(id _Nullable)details;
  239. /**
  240. The error code.
  241. */
  242. @property(readonly, nonatomic) NSString* code;
  243. /**
  244. The error message.
  245. */
  246. @property(readonly, nonatomic, nullable) NSString* message;
  247. /**
  248. The error details.
  249. */
  250. @property(readonly, nonatomic, nullable) id details;
  251. @end
  252. /**
  253. * Type of numeric data items encoded in a `FlutterStandardDataType`.
  254. *
  255. * - FlutterStandardDataTypeUInt8: plain bytes
  256. * - FlutterStandardDataTypeInt32: 32-bit signed integers
  257. * - FlutterStandardDataTypeInt64: 64-bit signed integers
  258. * - FlutterStandardDataTypeFloat64: 64-bit floats
  259. */
  260. typedef NS_ENUM(NSInteger, FlutterStandardDataType) {
  261. FlutterStandardDataTypeUInt8,
  262. FlutterStandardDataTypeInt32,
  263. FlutterStandardDataTypeInt64,
  264. FlutterStandardDataTypeFloat64,
  265. };
  266. /**
  267. * A byte buffer holding `UInt8`, `SInt32`, `SInt64`, or `Float64` values, used
  268. * with `FlutterStandardMessageCodec` and `FlutterStandardMethodCodec`.
  269. *
  270. * Two's complement encoding is used for signed integers. IEEE754
  271. * double-precision representation is used for floats. The platform's native
  272. * endianness is assumed.
  273. */
  274. FLUTTER_EXPORT
  275. @interface FlutterStandardTypedData : NSObject
  276. /**
  277. * Creates a `FlutterStandardTypedData` which interprets the specified data
  278. * as plain bytes.
  279. *
  280. * @param data the byte data.
  281. */
  282. + (instancetype)typedDataWithBytes:(NSData*)data;
  283. /**
  284. * Creates a `FlutterStandardTypedData` which interprets the specified data
  285. * as 32-bit signed integers.
  286. *
  287. * @param data the byte data. The length must be divisible by 4.
  288. */
  289. + (instancetype)typedDataWithInt32:(NSData*)data;
  290. /**
  291. * Creates a `FlutterStandardTypedData` which interprets the specified data
  292. * as 64-bit signed integers.
  293. *
  294. * @param data the byte data. The length must be divisible by 8.
  295. */
  296. + (instancetype)typedDataWithInt64:(NSData*)data;
  297. /**
  298. * Creates a `FlutterStandardTypedData` which interprets the specified data
  299. * as 64-bit floats.
  300. *
  301. * @param data the byte data. The length must be divisible by 8.
  302. */
  303. + (instancetype)typedDataWithFloat64:(NSData*)data;
  304. /**
  305. * The raw underlying data buffer.
  306. */
  307. @property(readonly, nonatomic) NSData* data;
  308. /**
  309. * The type of the encoded values.
  310. */
  311. @property(readonly, nonatomic) FlutterStandardDataType type;
  312. /**
  313. * The number of value items encoded.
  314. */
  315. @property(readonly, nonatomic) UInt32 elementCount;
  316. /**
  317. * The number of bytes used by the encoding of a single value item.
  318. */
  319. @property(readonly, nonatomic) UInt8 elementSize;
  320. @end
  321. /**
  322. * An arbitrarily large integer value, used with `FlutterStandardMessageCodec`
  323. * and `FlutterStandardMethodCodec`.
  324. */
  325. FLUTTER_EXPORT
  326. FLUTTER_UNAVAILABLE("Unavailable on 2018-08-31. Deprecated on 2018-01-09. "
  327. "FlutterStandardBigInteger was needed because the Dart 1.0 int type had no "
  328. "size limit. With Dart 2.0, the int type is a fixed-size, 64-bit signed "
  329. "integer. If you need to communicate larger integers, use NSString encoding "
  330. "instead.")
  331. @interface FlutterStandardBigInteger : NSObject
  332. @end
  333. /**
  334. * A codec for method calls and enveloped results.
  335. *
  336. * Method calls are encoded as binary messages with enough structure that the
  337. * codec can extract a method name `NSString` and an arguments `NSObject`,
  338. * possibly `nil`. These data items are used to populate a `FlutterMethodCall`.
  339. *
  340. * Result envelopes are encoded as binary messages with enough structure that
  341. * the codec can determine whether the result was successful or an error. In
  342. * the former case, the codec can extract the result `NSObject`, possibly `nil`.
  343. * In the latter case, the codec can extract an error code `NSString`, a
  344. * human-readable `NSString` error message (possibly `nil`), and a custom
  345. * error details `NSObject`, possibly `nil`. These data items are used to
  346. * populate a `FlutterError`.
  347. */
  348. FLUTTER_EXPORT
  349. @protocol FlutterMethodCodec
  350. /**
  351. * Provides access to a shared instance this codec.
  352. *
  353. * @return The shared instance.
  354. */
  355. + (instancetype)sharedInstance;
  356. /**
  357. * Encodes the specified method call into binary.
  358. *
  359. * @param methodCall The method call. The arguments value
  360. * must be supported by this codec.
  361. * @return The binary encoding.
  362. */
  363. - (NSData*)encodeMethodCall:(FlutterMethodCall*)methodCall;
  364. /**
  365. * Decodes the specified method call from binary.
  366. *
  367. * @param methodCall The method call to decode.
  368. * @return The decoded method call.
  369. */
  370. - (FlutterMethodCall*)decodeMethodCall:(NSData*)methodCall;
  371. /**
  372. * Encodes the specified successful result into binary.
  373. *
  374. * @param result The result. Must be a value supported by this codec.
  375. * @return The binary encoding.
  376. */
  377. - (NSData*)encodeSuccessEnvelope:(id _Nullable)result;
  378. /**
  379. * Encodes the specified error result into binary.
  380. *
  381. * @param error The error object. The error details value must be supported
  382. * by this codec.
  383. * @return The binary encoding.
  384. */
  385. - (NSData*)encodeErrorEnvelope:(FlutterError*)error;
  386. /**
  387. * Deccodes the specified result envelope from binary.
  388. *
  389. * @param envelope The error object.
  390. * @return The result value, if the envelope represented a successful result,
  391. * or a `FlutterError` instance, if not.
  392. */
  393. - (id _Nullable)decodeEnvelope:(NSData*)envelope;
  394. @end
  395. /**
  396. * A `FlutterMethodCodec` using UTF-8 encoded JSON method calls and result
  397. * envelopes.
  398. *
  399. * This codec is guaranteed to be compatible with the corresponding
  400. * [JSONMethodCodec](https://docs.flutter.io/flutter/services/JSONMethodCodec-class.html)
  401. * on the Dart side. These parts of the Flutter SDK are evolved synchronously.
  402. *
  403. * Values supported as methods arguments and result payloads are
  404. * those supported as top-level or leaf values by `FlutterJSONMessageCodec`.
  405. */
  406. FLUTTER_EXPORT
  407. @interface FlutterJSONMethodCodec : NSObject <FlutterMethodCodec>
  408. @end
  409. /**
  410. * A `FlutterMethodCodec` using the Flutter standard binary encoding.
  411. *
  412. * This codec is guaranteed to be compatible with the corresponding
  413. * [StandardMethodCodec](https://docs.flutter.io/flutter/services/StandardMethodCodec-class.html)
  414. * on the Dart side. These parts of the Flutter SDK are evolved synchronously.
  415. *
  416. * Values supported as method arguments and result payloads are those supported by
  417. * `FlutterStandardMessageCodec`.
  418. */
  419. FLUTTER_EXPORT
  420. @interface FlutterStandardMethodCodec : NSObject <FlutterMethodCodec>
  421. /**
  422. * Create a `FlutterStandardMethodCodec` who will read and write to \p readerWriter.
  423. */
  424. + (instancetype)codecWithReaderWriter:(FlutterStandardReaderWriter*)readerWriter;
  425. @end
  426. NS_ASSUME_NONNULL_END
  427. #endif // FLUTTER_FLUTTERCODECS_H_