-
Notifications
You must be signed in to change notification settings - Fork 6k
/
FlutterStandardCodec.mm
444 lines (385 loc) · 15.4 KB
/
FlutterStandardCodec.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.h"
#import "flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodec_Internal.h"
FLUTTER_ASSERT_ARC
#pragma mark - Codec for basic message channel
@implementation FlutterStandardMessageCodec {
FlutterStandardReaderWriter* _readerWriter;
}
+ (instancetype)sharedInstance {
static id _sharedInstance = nil;
if (!_sharedInstance) {
FlutterStandardReaderWriter* readerWriter = [[FlutterStandardReaderWriter alloc] init];
_sharedInstance = [[FlutterStandardMessageCodec alloc] initWithReaderWriter:readerWriter];
}
return _sharedInstance;
}
+ (instancetype)codecWithReaderWriter:(FlutterStandardReaderWriter*)readerWriter {
return [[FlutterStandardMessageCodec alloc] initWithReaderWriter:readerWriter];
}
- (instancetype)initWithReaderWriter:(FlutterStandardReaderWriter*)readerWriter {
self = [super init];
NSAssert(self, @"Super init cannot be nil");
_readerWriter = readerWriter;
return self;
}
- (NSData*)encode:(id)message {
if (message == nil) {
return nil;
}
NSMutableData* data = [NSMutableData dataWithCapacity:32];
FlutterStandardWriter* writer = [_readerWriter writerWithData:data];
[writer writeValue:message];
return data;
}
- (id)decode:(NSData*)message {
if ([message length] == 0) {
return nil;
}
FlutterStandardReader* reader = [_readerWriter readerWithData:message];
id value = [reader readValue];
NSAssert(![reader hasMore], @"Corrupted standard message");
return value;
}
@end
#pragma mark - Codec for method channel
@implementation FlutterStandardMethodCodec {
FlutterStandardReaderWriter* _readerWriter;
}
+ (instancetype)sharedInstance {
static id _sharedInstance = nil;
if (!_sharedInstance) {
FlutterStandardReaderWriter* readerWriter = [[FlutterStandardReaderWriter alloc] init];
_sharedInstance = [[FlutterStandardMethodCodec alloc] initWithReaderWriter:readerWriter];
}
return _sharedInstance;
}
+ (instancetype)codecWithReaderWriter:(FlutterStandardReaderWriter*)readerWriter {
return [[FlutterStandardMethodCodec alloc] initWithReaderWriter:readerWriter];
}
- (instancetype)initWithReaderWriter:(FlutterStandardReaderWriter*)readerWriter {
self = [super init];
NSAssert(self, @"Super init cannot be nil");
_readerWriter = readerWriter;
return self;
}
- (NSData*)encodeMethodCall:(FlutterMethodCall*)call {
NSMutableData* data = [NSMutableData dataWithCapacity:32];
FlutterStandardWriter* writer = [_readerWriter writerWithData:data];
[writer writeValue:call.method];
[writer writeValue:call.arguments];
return data;
}
- (NSData*)encodeSuccessEnvelope:(id)result {
NSMutableData* data = [NSMutableData dataWithCapacity:32];
FlutterStandardWriter* writer = [_readerWriter writerWithData:data];
[writer writeByte:0];
[writer writeValue:result];
return data;
}
- (NSData*)encodeErrorEnvelope:(FlutterError*)error {
NSMutableData* data = [NSMutableData dataWithCapacity:32];
FlutterStandardWriter* writer = [_readerWriter writerWithData:data];
[writer writeByte:1];
[writer writeValue:error.code];
[writer writeValue:error.message];
[writer writeValue:error.details];
return data;
}
- (FlutterMethodCall*)decodeMethodCall:(NSData*)message {
FlutterStandardReader* reader = [_readerWriter readerWithData:message];
id value1 = [reader readValue];
id value2 = [reader readValue];
NSAssert(![reader hasMore], @"Corrupted standard method call");
NSAssert([value1 isKindOfClass:[NSString class]], @"Corrupted standard method call");
return [FlutterMethodCall methodCallWithMethodName:value1 arguments:value2];
}
- (id)decodeEnvelope:(NSData*)envelope {
FlutterStandardReader* reader = [_readerWriter readerWithData:envelope];
UInt8 flag = [reader readByte];
NSAssert(flag <= 1, @"Corrupted standard envelope");
id result;
switch (flag) {
case 0: {
result = [reader readValue];
NSAssert(![reader hasMore], @"Corrupted standard envelope");
} break;
case 1: {
id code = [reader readValue];
id message = [reader readValue];
id details = [reader readValue];
NSAssert(![reader hasMore], @"Corrupted standard envelope");
NSAssert([code isKindOfClass:[NSString class]], @"Invalid standard envelope");
NSAssert(message == nil || [message isKindOfClass:[NSString class]],
@"Invalid standard envelope");
result = [FlutterError errorWithCode:code message:message details:details];
} break;
}
return result;
}
@end
using namespace flutter;
#pragma mark - Standard serializable types
@implementation FlutterStandardTypedData
+ (instancetype)typedDataWithBytes:(NSData*)data {
return [FlutterStandardTypedData typedDataWithData:data type:FlutterStandardDataTypeUInt8];
}
+ (instancetype)typedDataWithInt32:(NSData*)data {
return [FlutterStandardTypedData typedDataWithData:data type:FlutterStandardDataTypeInt32];
}
+ (instancetype)typedDataWithInt64:(NSData*)data {
return [FlutterStandardTypedData typedDataWithData:data type:FlutterStandardDataTypeInt64];
}
+ (instancetype)typedDataWithFloat32:(NSData*)data {
return [FlutterStandardTypedData typedDataWithData:data type:FlutterStandardDataTypeFloat32];
}
+ (instancetype)typedDataWithFloat64:(NSData*)data {
return [FlutterStandardTypedData typedDataWithData:data type:FlutterStandardDataTypeFloat64];
}
+ (instancetype)typedDataWithData:(NSData*)data type:(FlutterStandardDataType)type {
return [[FlutterStandardTypedData alloc] initWithData:data type:type];
}
- (instancetype)initWithData:(NSData*)data type:(FlutterStandardDataType)type {
UInt8 elementSize = elementSizeForFlutterStandardDataType(type);
NSAssert(data, @"Data cannot be nil");
NSAssert(data.length % elementSize == 0, @"Data must contain integral number of elements");
self = [super init];
NSAssert(self, @"Super init cannot be nil");
_data = [data copy];
_type = type;
_elementSize = elementSize;
_elementCount = data.length / elementSize;
return self;
}
- (BOOL)isEqual:(id)object {
if (self == object) {
return YES;
}
if (![object isKindOfClass:[FlutterStandardTypedData class]]) {
return NO;
}
FlutterStandardTypedData* other = (FlutterStandardTypedData*)object;
return self.type == other.type && self.elementCount == other.elementCount &&
[self.data isEqual:other.data];
}
- (NSUInteger)hash {
return [self.data hash] ^ self.type;
}
@end
#pragma mark - Writer and reader of standard codec
@implementation FlutterStandardWriter {
NSMutableData* _data;
}
- (instancetype)initWithData:(NSMutableData*)data {
self = [super init];
NSAssert(self, @"Super init cannot be nil");
_data = data;
return self;
}
- (void)writeByte:(UInt8)value {
FlutterStandardCodecHelperWriteByte((__bridge CFMutableDataRef)_data, value);
}
- (void)writeBytes:(const void*)bytes length:(NSUInteger)length {
FlutterStandardCodecHelperWriteBytes((__bridge CFMutableDataRef)_data, bytes, length);
}
- (void)writeData:(NSData*)data {
FlutterStandardCodecHelperWriteData((__bridge CFMutableDataRef)_data, (__bridge CFDataRef)data);
}
- (void)writeSize:(UInt32)size {
FlutterStandardCodecHelperWriteSize((__bridge CFMutableDataRef)_data, size);
}
- (void)writeAlignment:(UInt8)alignment {
FlutterStandardCodecHelperWriteAlignment((__bridge CFMutableDataRef)_data, alignment);
}
- (void)writeUTF8:(NSString*)value {
FlutterStandardCodecHelperWriteUTF8((__bridge CFMutableDataRef)_data,
(__bridge CFStringRef)value);
}
static FlutterStandardCodecObjcType GetWriteType(id value) {
if (value == nil || (__bridge CFNullRef)value == kCFNull) {
return FlutterStandardCodecObjcTypeNil;
} else if ([value isKindOfClass:[NSNumber class]]) {
return FlutterStandardCodecObjcTypeNSNumber;
} else if ([value isKindOfClass:[NSString class]]) {
return FlutterStandardCodecObjcTypeNSString;
} else if ([value isKindOfClass:[FlutterStandardTypedData class]]) {
return FlutterStandardCodecObjcTypeFlutterStandardTypedData;
} else if ([value isKindOfClass:[NSData class]]) {
return FlutterStandardCodecObjcTypeNSData;
} else if ([value isKindOfClass:[NSArray class]]) {
return FlutterStandardCodecObjcTypeNSArray;
} else if ([value isKindOfClass:[NSDictionary class]]) {
return FlutterStandardCodecObjcTypeNSDictionary;
} else {
return FlutterStandardCodecObjcTypeUnknown;
}
}
struct WriteKeyValuesInfo {
CFTypeRef writer;
CFMutableDataRef data;
};
static void WriteKeyValues(CFTypeRef key, CFTypeRef value, void* context) {
WriteKeyValuesInfo* info = (WriteKeyValuesInfo*)context;
FastWriteValueOfType(info->writer, info->data, key);
FastWriteValueOfType(info->writer, info->data, value);
}
// Recurses into WriteValueOfType directly if it is writing a known type,
// otherwise recurses with objc_msgSend.
static void FastWriteValueOfType(CFTypeRef writer, CFMutableDataRef data, CFTypeRef value) {
FlutterStandardCodecObjcType type = GetWriteType((__bridge id)value);
if (type != FlutterStandardCodecObjcTypeUnknown) {
WriteValueOfType(writer, data, type, value);
} else {
[(__bridge FlutterStandardWriter*)writer writeValue:(__bridge id)value];
}
}
static void WriteValueOfType(CFTypeRef writer,
CFMutableDataRef data,
FlutterStandardCodecObjcType type,
CFTypeRef value) {
switch (type) {
case FlutterStandardCodecObjcTypeNil:
FlutterStandardCodecHelperWriteByte(data, FlutterStandardFieldNil);
break;
case FlutterStandardCodecObjcTypeNSNumber: {
CFNumberRef number = (CFNumberRef)value;
BOOL success = FlutterStandardCodecHelperWriteNumber(data, number);
if (!success) {
NSLog(@"Unsupported value: %@ of number type %ld", value, CFNumberGetType(number));
NSCAssert(NO, @"Unsupported value for standard codec");
}
break;
}
case FlutterStandardCodecObjcTypeNSString: {
CFStringRef string = (CFStringRef)value;
FlutterStandardCodecHelperWriteByte(data, FlutterStandardFieldString);
FlutterStandardCodecHelperWriteUTF8(data, string);
break;
}
case FlutterStandardCodecObjcTypeFlutterStandardTypedData: {
FlutterStandardTypedData* typedData = (__bridge FlutterStandardTypedData*)value;
FlutterStandardCodecHelperWriteByte(data, FlutterStandardFieldForDataType(typedData.type));
FlutterStandardCodecHelperWriteSize(data, typedData.elementCount);
FlutterStandardCodecHelperWriteAlignment(data, typedData.elementSize);
FlutterStandardCodecHelperWriteData(data, (__bridge CFDataRef)typedData.data);
break;
}
case FlutterStandardCodecObjcTypeNSData:
WriteValueOfType(writer, data, FlutterStandardCodecObjcTypeFlutterStandardTypedData,
(__bridge CFTypeRef)
[FlutterStandardTypedData typedDataWithBytes:(__bridge NSData*)value]);
break;
case FlutterStandardCodecObjcTypeNSArray: {
CFArrayRef array = (CFArrayRef)value;
FlutterStandardCodecHelperWriteByte(data, FlutterStandardFieldList);
CFIndex count = CFArrayGetCount(array);
FlutterStandardCodecHelperWriteSize(data, count);
for (CFIndex i = 0; i < count; ++i) {
FastWriteValueOfType(writer, data, CFArrayGetValueAtIndex(array, i));
}
break;
}
case FlutterStandardCodecObjcTypeNSDictionary: {
CFDictionaryRef dict = (CFDictionaryRef)value;
FlutterStandardCodecHelperWriteByte(data, FlutterStandardFieldMap);
CFIndex count = CFDictionaryGetCount(dict);
FlutterStandardCodecHelperWriteSize(data, count);
WriteKeyValuesInfo info = {
.writer = writer,
.data = data,
};
CFDictionaryApplyFunction(dict, WriteKeyValues, (void*)&info);
break;
}
case FlutterStandardCodecObjcTypeUnknown: {
id objc_value = (__bridge id)value;
NSLog(@"Unsupported value: %@ of type %@", objc_value, [objc_value class]);
NSCAssert(NO, @"Unsupported value for standard codec");
break;
}
}
}
- (void)writeValue:(id)value {
FlutterStandardCodecObjcType type = GetWriteType(value);
WriteValueOfType((__bridge CFTypeRef)self, (__bridge CFMutableDataRef)self->_data, type,
(__bridge CFTypeRef)value);
}
@end
@implementation FlutterStandardReader {
NSData* _data;
NSRange _range;
}
- (instancetype)initWithData:(NSData*)data {
self = [super init];
NSAssert(self, @"Super init cannot be nil");
_data = [data copy];
_range = NSMakeRange(0, 0);
return self;
}
- (BOOL)hasMore {
return _range.location < _data.length;
}
- (void)readBytes:(void*)destination length:(NSUInteger)length {
FlutterStandardCodecHelperReadBytes(&_range.location, length, destination,
(__bridge CFDataRef)_data);
}
- (UInt8)readByte {
return FlutterStandardCodecHelperReadByte(&_range.location, (__bridge CFDataRef)_data);
}
- (UInt32)readSize {
return FlutterStandardCodecHelperReadSize(&_range.location, (__bridge CFDataRef)_data);
}
- (NSData*)readData:(NSUInteger)length {
_range.length = length;
NSData* data = [_data subdataWithRange:_range];
_range.location += _range.length;
return data;
}
- (NSString*)readUTF8 {
return (__bridge NSString*)FlutterStandardCodecHelperReadUTF8(&_range.location,
(__bridge CFDataRef)_data);
}
- (void)readAlignment:(UInt8)alignment {
FlutterStandardCodecHelperReadAlignment(&_range.location, alignment);
}
- (nullable id)readValue {
return (__bridge id)ReadValue((__bridge CFTypeRef)self);
}
static CFTypeRef ReadValue(CFTypeRef user_data) {
FlutterStandardReader* reader = (__bridge FlutterStandardReader*)user_data;
uint8_t type = FlutterStandardCodecHelperReadByte(&reader->_range.location,
(__bridge CFDataRef)reader->_data);
return (__bridge CFTypeRef)[reader readValueOfType:type];
}
static CFTypeRef ReadTypedDataOfType(FlutterStandardField field, CFTypeRef user_data) {
FlutterStandardReader* reader = (__bridge FlutterStandardReader*)user_data;
unsigned long* location = &reader->_range.location;
CFDataRef data = (__bridge CFDataRef)reader->_data;
FlutterStandardDataType type = FlutterStandardDataTypeForField(field);
UInt64 elementCount = FlutterStandardCodecHelperReadSize(location, data);
UInt64 elementSize = elementSizeForFlutterStandardDataType(type);
FlutterStandardCodecHelperReadAlignment(location, elementSize);
UInt64 length = elementCount * elementSize;
NSRange range = NSMakeRange(*location, length);
// Note: subdataWithRange performs better than CFDataCreate and
// CFDataCreateBytesNoCopy crashes.
NSData* bytes = [(__bridge NSData*)data subdataWithRange:range];
*location += length;
return (__bridge CFTypeRef)[FlutterStandardTypedData typedDataWithData:bytes type:type];
}
- (nullable id)readValueOfType:(UInt8)type {
return (__bridge id)FlutterStandardCodecHelperReadValueOfType(
&_range.location, (__bridge CFDataRef)_data, type, ReadValue, ReadTypedDataOfType,
(__bridge CFTypeRef)self);
}
@end
@implementation FlutterStandardReaderWriter
- (FlutterStandardWriter*)writerWithData:(NSMutableData*)data {
return [[FlutterStandardWriter alloc] initWithData:data];
}
- (FlutterStandardReader*)readerWithData:(NSData*)data {
return [[FlutterStandardReader alloc] initWithData:data];
}
@end