| Flutter iOS Embedder |

FlutterRestorationPlugin.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
5 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterRestorationPlugin.h"
6
7 #import <Foundation/Foundation.h>
8 #import <UIKit/UIKit.h>
9
10 #include "flutter/fml/logging.h"
11
12 FLUTTER_ASSERT_NOT_ARC
13
14 @interface FlutterRestorationPlugin ()
15 @property(nonatomic, copy) FlutterResult pendingRequest;
16 @end
17
18 @implementation FlutterRestorationPlugin {
19 BOOL _waitForData;
20 BOOL _restorationEnabled;
21 }
22
23 - (instancetype)initWithChannel:(FlutterMethodChannel*)channel
24 restorationEnabled:(BOOL)restorationEnabled {
25 FML_DCHECK(channel) << "channel must be set";
26 self = [super init];
27 if (self) {
28 [channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
29 [self handleMethodCall:call result:result];
30 }];
31 _restorationEnabled = restorationEnabled;
32 _waitForData = restorationEnabled;
33 }
34 return self;
35 }
36
37 - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
38 if ([[call method] isEqualToString:@"put"]) {
39 NSAssert(self.pendingRequest == nil, @"Cannot put data while a get request is pending.");
40 FlutterStandardTypedData* data = [call arguments];
41 self.restorationData = [data data];
42 result(nil);
43 } else if ([[call method] isEqualToString:@"get"]) {
44 if (!_restorationEnabled || !_waitForData) {
45 result([self dataForFramework]);
46 return;
47 }
48 NSAssert(self.pendingRequest == nil, @"There can only be one pending request.");
49 self.pendingRequest = result;
50 } else {
51 result(FlutterMethodNotImplemented);
52 }
53 }
54
55 - (void)setRestorationData:(NSData*)data {
56 if (data != _restorationData) {
57 [_restorationData release];
58 _restorationData = [data retain];
59 }
60 _waitForData = NO;
61 if (self.pendingRequest != nil) {
62 self.pendingRequest([self dataForFramework]);
63 self.pendingRequest = nil;
64 }
65 }
66
67 - (void)markRestorationComplete {
68 _waitForData = NO;
69 if (self.pendingRequest != nil) {
70 NSAssert(_restorationEnabled, @"No request can be pending when restoration is disabled.");
71 self.pendingRequest([self dataForFramework]);
72 self.pendingRequest = nil;
73 }
74 }
75
76 - (void)reset {
77 self.pendingRequest = nil;
78 self.restorationData = nil;
79 }
80
81 - (NSDictionary*)dataForFramework {
82 if (!_restorationEnabled) {
83 return @{@"enabled" : @NO};
84 }
85 if (self.restorationData == nil) {
86 return @{@"enabled" : @YES};
87 }
88 return @{
89 @"enabled" : @YES,
90 @"data" : [FlutterStandardTypedData typedDataWithBytes:self.restorationData]
91 };
92 }
93
94 - (void)dealloc {
95 [_restorationData release];
96 [_pendingRequest release];
97 [super dealloc];
98 }
99
100 @end
_restorationEnabled
BOOL _restorationEnabled
Definition: FlutterRestorationPlugin.mm:18
FlutterMethodChannel
Definition: FlutterChannels.h:222
FlutterMethodNotImplemented
FLUTTER_DARWIN_EXPORT NSObject const * FlutterMethodNotImplemented
FLUTTER_ASSERT_NOT_ARC
Definition: VsyncWaiterIosTest.mm:15
FlutterRestorationPlugin.h
FlutterRestorationPlugin
Definition: FlutterRestorationPlugin.h:12
+[FlutterStandardTypedData typedDataWithBytes:]
instancetype typedDataWithBytes:(NSData *data)
Definition: FlutterStandardCodec.mm:146
-[FlutterMethodChannel setMethodCallHandler:]
void setMethodCallHandler:(FlutterMethodCallHandler _Nullable handler)
FlutterMethodCall
Definition: FlutterCodecs.h:220
FlutterResult
void(^ FlutterResult)(id _Nullable result)
Definition: FlutterChannels.h:196
FlutterStandardTypedData
Definition: FlutterCodecs.h:298
FlutterStandardTypedData::data
NSData * data
Definition: FlutterCodecs.h:342
FlutterMethodCall::arguments
id arguments
Definition: FlutterCodecs.h:238