Flutter iOS Embedder
FlutterCodecs.mm
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 
6 
7 #include <cstring>
8 
10 
11 @implementation FlutterBinaryCodec
12 + (instancetype)sharedInstance {
13  static id _sharedInstance = nil;
14  if (!_sharedInstance) {
15  _sharedInstance = [[FlutterBinaryCodec alloc] init];
16  }
17  return _sharedInstance;
18 }
19 
20 - (NSData*)encode:(id)message {
21  NSAssert(!message || [message isKindOfClass:[NSData class]], @"");
22  return message;
23 }
24 
25 - (NSData*)decode:(NSData*)message {
26  return message;
27 }
28 @end
29 
30 @implementation FlutterStringCodec
31 + (instancetype)sharedInstance {
32  static id _sharedInstance = nil;
33  if (!_sharedInstance) {
34  _sharedInstance = [[FlutterStringCodec alloc] init];
35  }
36  return _sharedInstance;
37 }
38 
39 - (NSData*)encode:(id)message {
40  if (message == nil) {
41  return nil;
42  }
43  NSAssert([message isKindOfClass:[NSString class]], @"");
44  NSString* stringMessage = message;
45  const char* utf8 = stringMessage.UTF8String;
46  return [NSData dataWithBytes:utf8 length:strlen(utf8)];
47 }
48 
49 - (NSString*)decode:(NSData*)message {
50  if (message == nil) {
51  return nil;
52  }
53  return [[NSString alloc] initWithData:message encoding:NSUTF8StringEncoding];
54 }
55 @end
56 
57 @implementation FlutterJSONMessageCodec
58 + (instancetype)sharedInstance {
59  static id _sharedInstance = nil;
60  if (!_sharedInstance) {
61  _sharedInstance = [[FlutterJSONMessageCodec alloc] init];
62  }
63  return _sharedInstance;
64 }
65 
66 - (NSData*)encode:(id)message {
67  if (message == nil) {
68  return nil;
69  }
70  NSData* encoding;
71  NSError* error;
72  if ([message isKindOfClass:[NSArray class]] || [message isKindOfClass:[NSDictionary class]]) {
73  encoding = [NSJSONSerialization dataWithJSONObject:message options:0 error:&error];
74  } else {
75  // NSJSONSerialization does not support top-level simple values.
76  // We encode as singleton array, then extract the relevant bytes.
77  encoding = [NSJSONSerialization dataWithJSONObject:@[ message ] options:0 error:&error];
78  if (encoding) {
79  encoding = [encoding subdataWithRange:NSMakeRange(1, encoding.length - 2)];
80  }
81  }
82  NSAssert(encoding, @"Invalid JSON message, encoding failed: %@", error);
83  return encoding;
84 }
85 
86 - (id)decode:(NSData*)message {
87  if ([message length] == 0) {
88  return nil;
89  }
90  BOOL isSimpleValue = NO;
91  id decoded = nil;
92  if (0 < message.length) {
93  UInt8 first;
94  [message getBytes:&first length:1];
95  isSimpleValue = first != '{' && first != '[';
96  if (isSimpleValue) {
97  // NSJSONSerialization does not support top-level simple values.
98  // We expand encoding to singleton array, then decode that and extract
99  // the single entry.
100  UInt8 begin = '[';
101  UInt8 end = ']';
102  NSMutableData* expandedMessage = [NSMutableData dataWithLength:message.length + 2];
103  [expandedMessage replaceBytesInRange:NSMakeRange(0, 1) withBytes:&begin];
104  [expandedMessage replaceBytesInRange:NSMakeRange(1, message.length) withBytes:message.bytes];
105  [expandedMessage replaceBytesInRange:NSMakeRange(message.length + 1, 1) withBytes:&end];
106  message = expandedMessage;
107  }
108  decoded = [NSJSONSerialization JSONObjectWithData:message options:0 error:nil];
109  }
110  NSAssert(decoded, @"Invalid JSON message, decoding failed");
111  return isSimpleValue ? ((NSArray*)decoded)[0] : decoded;
112 }
113 @end
114 
115 @implementation FlutterJSONMethodCodec
116 + (instancetype)sharedInstance {
117  static id _sharedInstance = nil;
118  if (!_sharedInstance) {
119  _sharedInstance = [[FlutterJSONMethodCodec alloc] init];
120  }
121  return _sharedInstance;
122 }
123 
124 - (NSData*)encodeMethodCall:(FlutterMethodCall*)call {
125  return [[FlutterJSONMessageCodec sharedInstance] encode:@{
126  @"method" : call.method,
127  @"args" : [self wrapNil:call.arguments],
128  }];
129 }
130 
131 - (NSData*)encodeSuccessEnvelope:(id)result {
132  return [[FlutterJSONMessageCodec sharedInstance] encode:@[ [self wrapNil:result] ]];
133 }
134 
135 - (NSData*)encodeErrorEnvelope:(FlutterError*)error {
136  return [[FlutterJSONMessageCodec sharedInstance] encode:@[
137  error.code,
138  [self wrapNil:error.message],
139  [self wrapNil:error.details],
140  ]];
141 }
142 
143 - (FlutterMethodCall*)decodeMethodCall:(NSData*)message {
144  NSDictionary* dictionary = [[FlutterJSONMessageCodec sharedInstance] decode:message];
145  id method = dictionary[@"method"];
146  id arguments = [self unwrapNil:dictionary[@"args"]];
147  NSAssert([method isKindOfClass:[NSString class]], @"Invalid JSON method call");
148  return [FlutterMethodCall methodCallWithMethodName:method arguments:arguments];
149 }
150 
151 - (id)decodeEnvelope:(NSData*)envelope {
152  NSArray* array = [[FlutterJSONMessageCodec sharedInstance] decode:envelope];
153  if (array.count == 1) {
154  return [self unwrapNil:array[0]];
155  }
156  NSAssert(array.count == 3, @"Invalid JSON envelope");
157  id code = array[0];
158  id message = [self unwrapNil:array[1]];
159  id details = [self unwrapNil:array[2]];
160  NSAssert([code isKindOfClass:[NSString class]], @"Invalid JSON envelope");
161  NSAssert(message == nil || [message isKindOfClass:[NSString class]], @"Invalid JSON envelope");
162  return [FlutterError errorWithCode:code message:message details:details];
163 }
164 
165 - (id)wrapNil:(id)value {
166  return value == nil ? [NSNull null] : value;
167 }
168 - (id)unwrapNil:(id)value {
169  return value == [NSNull null] ? nil : value;
170 }
171 @end
+[FlutterMethodCall methodCallWithMethodName:arguments:]
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
FlutterError
Definition: FlutterCodecs.h:246
+[FlutterError errorWithCode:message:details:]
instancetype errorWithCode:message:details:(NSString *code,[message] NSString *_Nullable message,[details] id _Nullable details)
FlutterMethodCall
Definition: FlutterCodecs.h:220
FlutterStringCodec
Definition: FlutterCodecs.h:63
FlutterCodecs.h
FlutterJSONMethodCodec
Definition: FlutterCodecs.h:455
FLUTTER_ASSERT_ARC
Definition: VsyncWaiterIosTest.mm:15
FlutterBinaryCodec
Definition: FlutterCodecs.h:52
+[FlutterMessageCodec-p sharedInstance]
instancetype sharedInstance()
FlutterJSONMessageCodec
Definition: FlutterCodecs.h:81