-
Notifications
You must be signed in to change notification settings - Fork 78
/
FlutterIsolatePlugin.m
157 lines (113 loc) · 5.79 KB
/
FlutterIsolatePlugin.m
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
#import "FlutterIsolatePlugin.h"
#import <objc/message.h>
@interface IsolateHolder : NSObject
@property(nonatomic) FlutterEngine* engine;
@property(nonatomic) NSString* isolateId;
@property(nonatomic) long long entryPoint;
@property(nonatomic) FlutterResult result;
@property(nonatomic) FlutterEventChannel* startupChannel;
@property(nonatomic) FlutterMethodChannel* controlChannel;
@end
@implementation IsolateHolder
@end
static dispatch_once_t _initializeStaticPlugin = 0;
static NSMutableArray<IsolateHolder*>* _queuedIsolates;
static NSMutableDictionary<NSString*,IsolateHolder*>* _activeIsolates;
@interface FlutterIsolatePlugin()
@property(nonatomic) NSObject<FlutterPluginRegistrar> * registrar;
@property(nonatomic) FlutterMethodChannel* controlChannel;
@property FlutterEventSink sink;
@end
@interface GeneratedPluginRegistrant : NSObject
+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry;
@end
@implementation FlutterIsolatePlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
dispatch_once(&_initializeStaticPlugin, ^{
_queuedIsolates = [NSMutableArray<IsolateHolder*> new];
_activeIsolates = [NSMutableDictionary<NSString*,IsolateHolder*> new];
});
FlutterIsolatePlugin *plugin = [[FlutterIsolatePlugin alloc] init];
plugin.registrar = registrar;
plugin.controlChannel = [FlutterMethodChannel methodChannelWithName:FLUTTER_ISOLATE_NAMESPACE @"/control"
binaryMessenger:[registrar messenger]];
[registrar addMethodCallDelegate:plugin channel:plugin.controlChannel];
}
static NSString* _isolatePluginRegistrantClassName;
+ (NSString*)isolatePluginRegistrantClassName { return _isolatePluginRegistrantClassName; }
+ (void)setIsolatePluginRegistrantClassName:(NSString*)value { _isolatePluginRegistrantClassName = value; }
+ (nullable Class)lookupGeneratedPluginRegistrant {
NSString* classNameToCompare = @"GeneratedPluginRegistrant";
if (_isolatePluginRegistrantClassName != nil) {
classNameToCompare = _isolatePluginRegistrantClassName;
}
return NSClassFromString(classNameToCompare);
}
- (void)startNextIsolate {
IsolateHolder *isolate = _queuedIsolates.firstObject;
FlutterCallbackInformation *info = [FlutterCallbackCache lookupCallbackInformation:isolate.entryPoint];
isolate.engine = [FlutterEngine alloc];
if ([isolate.engine respondsToSelector:@selector(initWithName:project:allowHeadlessExecution:)]) {
((id(*)(id,SEL,id,id,id))objc_msgSend)(isolate.engine, @selector(initWithName:project:allowHeadlessExecution:) , isolate.isolateId, nil, @(YES));
}
else // older versions before above is available
[isolate.engine initWithName:isolate.isolateId project:nil];
/* not entire sure if a listen on an event channel will be queued
* as we cannot register the event channel until after runWithEntryPoint has been called. If it is not queued
* then this will be a race on the FlutterEventChannels initialization, and could deadlock. */
[isolate.engine runWithEntrypoint:info.callbackName libraryURI:info.callbackLibraryPath];
isolate.controlChannel = [FlutterMethodChannel methodChannelWithName:FLUTTER_ISOLATE_NAMESPACE @"/control"
binaryMessenger:isolate.engine];
isolate.startupChannel = [FlutterEventChannel eventChannelWithName:FLUTTER_ISOLATE_NAMESPACE @"/event"
binaryMessenger:isolate.engine];
[isolate.startupChannel setStreamHandler:self];
[_registrar addMethodCallDelegate:self channel:isolate.controlChannel];
[[FlutterIsolatePlugin lookupGeneratedPluginRegistrant] registerWithRegistry:isolate.engine];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"spawn_isolate" isEqualToString:call.method]) {
IsolateHolder* isolate = [IsolateHolder new];
isolate.entryPoint = [[call.arguments objectForKey:@"entry_point"] longLongValue];
isolate.isolateId = [call.arguments objectForKey:@"isolate_id"];
isolate.result = result;
[_queuedIsolates addObject:isolate];
if (_queuedIsolates.count == 1)
[self startNextIsolate];
} else if ([@"kill_isolate" isEqualToString:call.method]) {
NSString *isolateId = [call.arguments objectForKey:@"isolate_id"];
if ([_activeIsolates[isolateId].engine respondsToSelector:@selector(destroyContext)])
((void(*)(id,SEL))objc_msgSend)(_activeIsolates[isolateId].engine, @selector(destroyContext));
[_activeIsolates removeObjectForKey:isolateId];
} else if ([@"get_isolate_list" isEqualToString:call.method]) {
NSArray *output = [_activeIsolates allKeys];
result(output);
} else if ([@"kill_all_isolates" isEqualToString:call.method]) {
for (NSString *key in _activeIsolates) {
if ([_activeIsolates[key].engine respondsToSelector:@selector(destroyContext)])
((void(*)(id,SEL))objc_msgSend)(_activeIsolates[key].engine, @selector(destroyContext));
}
[_activeIsolates removeAllObjects];
[_queuedIsolates removeAllObjects];
} else {
result(FlutterMethodNotImplemented);
}
}
- (FlutterError*)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)sink {
IsolateHolder* isolate = _queuedIsolates.firstObject;
if (isolate != nil) {
sink(isolate.isolateId);
sink(FlutterEndOfEventStream);
_activeIsolates[isolate.isolateId] = isolate;
[_queuedIsolates removeObject:isolate];
isolate.result(@(YES));
isolate.startupChannel = nil;
isolate.result = nil;
}
if (_queuedIsolates.count != 0)
[self startNextIsolate];
return nil;
}
- (FlutterError*)onCancelWithArguments:(id)arguments {
return nil;
}
@end