Added a plugin method that gets called when the engine is about to be deleted (flutter/engine#16336)
This commit is contained in:
@@ -223,6 +223,19 @@ typedef void (*FlutterPluginRegistrantCallback)(NSObject<FlutterPluginRegistry>*
|
||||
* @param result A callback for submitting the result of the call.
|
||||
*/
|
||||
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
|
||||
@optional
|
||||
/**
|
||||
* Called when a plugin is being removed from a `FlutterEngine`, which is
|
||||
* usually the result of the `FlutterEngine` being deallocated. This method
|
||||
* provides the opportunity to do necessary cleanup.
|
||||
*
|
||||
* You will only receive this method if you registered your plugin instance with
|
||||
* the `FlutterEngine` via `-[FlutterPluginRegistry publish:]`.
|
||||
*
|
||||
* @param registrar The registrar that was used to publish the plugin.
|
||||
*
|
||||
*/
|
||||
- (void)detachFromEngineForRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar;
|
||||
@end
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@@ -29,19 +29,21 @@
|
||||
|
||||
NSString* const FlutterDefaultDartEntrypoint = nil;
|
||||
|
||||
@interface FlutterEngineRegistrar : NSObject <FlutterPluginRegistrar>
|
||||
@property(nonatomic, assign) FlutterEngine* flutterEngine;
|
||||
- (instancetype)initWithPlugin:(NSString*)pluginKey flutterEngine:(FlutterEngine*)flutterEngine;
|
||||
@end
|
||||
|
||||
@interface FlutterEngine () <FlutterTextInputDelegate, FlutterBinaryMessenger>
|
||||
// Maintains a dictionary of plugin names that have registered with the engine. Used by
|
||||
// FlutterEngineRegistrar to implement a FlutterPluginRegistrar.
|
||||
@property(nonatomic, readonly) NSMutableDictionary* pluginPublications;
|
||||
@property(nonatomic, readonly) NSMutableDictionary<NSString*, FlutterEngineRegistrar*>* registrars;
|
||||
|
||||
@property(nonatomic, readwrite, copy) NSString* isolateId;
|
||||
@property(nonatomic, retain) id<NSObject> flutterViewControllerWillDeallocObserver;
|
||||
@end
|
||||
|
||||
@interface FlutterEngineRegistrar : NSObject <FlutterPluginRegistrar>
|
||||
- (instancetype)initWithPlugin:(NSString*)pluginKey flutterEngine:(FlutterEngine*)flutterEngine;
|
||||
@end
|
||||
|
||||
@implementation FlutterEngine {
|
||||
fml::scoped_nsobject<FlutterDartProject> _dartProject;
|
||||
flutter::ThreadHost _threadHost;
|
||||
@@ -98,6 +100,7 @@ NSString* const FlutterDefaultDartEntrypoint = nil;
|
||||
_dartProject.reset([project retain]);
|
||||
|
||||
_pluginPublications = [NSMutableDictionary new];
|
||||
_registrars = [[NSMutableDictionary alloc] init];
|
||||
_platformViewsController.reset(new flutter::FlutterPlatformViewsController());
|
||||
|
||||
_binaryMessenger = [[FlutterBinaryMessengerRelay alloc] initWithParent:self];
|
||||
@@ -122,8 +125,24 @@ NSString* const FlutterDefaultDartEntrypoint = nil;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
/// Notify plugins of dealloc. This should happen first in dealloc since the
|
||||
/// plugins may be talking to things like the binaryMessenger.
|
||||
[_pluginPublications enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL* stop) {
|
||||
if ([object respondsToSelector:@selector(detachFromEngineForRegistrar:)]) {
|
||||
NSObject<FlutterPluginRegistrar>* registrar = self.registrars[key];
|
||||
[object detachFromEngineForRegistrar:registrar];
|
||||
}
|
||||
}];
|
||||
|
||||
/// nil out weak references.
|
||||
[_registrars
|
||||
enumerateKeysAndObjectsUsingBlock:^(id key, FlutterEngineRegistrar* registrar, BOOL* stop) {
|
||||
registrar.flutterEngine = nil;
|
||||
}];
|
||||
|
||||
[_labelPrefix release];
|
||||
[_pluginPublications release];
|
||||
[_registrars release];
|
||||
_binaryMessenger.parent = nil;
|
||||
[_binaryMessenger release];
|
||||
|
||||
@@ -647,7 +666,10 @@ NSString* const FlutterDefaultDartEntrypoint = nil;
|
||||
- (NSObject<FlutterPluginRegistrar>*)registrarForPlugin:(NSString*)pluginKey {
|
||||
NSAssert(self.pluginPublications[pluginKey] == nil, @"Duplicate plugin key: %@", pluginKey);
|
||||
self.pluginPublications[pluginKey] = [NSNull null];
|
||||
return [[[FlutterEngineRegistrar alloc] initWithPlugin:pluginKey flutterEngine:self] autorelease];
|
||||
FlutterEngineRegistrar* result = [[FlutterEngineRegistrar alloc] initWithPlugin:pluginKey
|
||||
flutterEngine:self];
|
||||
self.registrars[pluginKey] = result;
|
||||
return [result autorelease];
|
||||
}
|
||||
|
||||
- (BOOL)hasPlugin:(NSString*)pluginKey {
|
||||
@@ -686,20 +708,18 @@ NSString* const FlutterDefaultDartEntrypoint = nil;
|
||||
|
||||
@implementation FlutterEngineRegistrar {
|
||||
NSString* _pluginKey;
|
||||
FlutterEngine* _flutterEngine;
|
||||
}
|
||||
|
||||
- (instancetype)initWithPlugin:(NSString*)pluginKey flutterEngine:(FlutterEngine*)flutterEngine {
|
||||
self = [super init];
|
||||
NSAssert(self, @"Super init cannot be nil");
|
||||
_pluginKey = [pluginKey retain];
|
||||
_flutterEngine = [flutterEngine retain];
|
||||
_pluginKey = [pluginKey copy];
|
||||
_flutterEngine = flutterEngine;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[_pluginKey release];
|
||||
[_flutterEngine release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
|
||||
@@ -47,4 +47,17 @@ FLUTTER_ASSERT_ARC
|
||||
}]);
|
||||
}
|
||||
|
||||
- (void)testNotifyPluginOfDealloc {
|
||||
id plugin = OCMProtocolMock(@protocol(FlutterPlugin));
|
||||
OCMStub([plugin detachFromEngineForRegistrar:[OCMArg any]]);
|
||||
{
|
||||
id project = OCMClassMock([FlutterDartProject class]);
|
||||
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"engine" project:project];
|
||||
NSObject<FlutterPluginRegistrar>* registrar = [engine registrarForPlugin:@"plugin"];
|
||||
[registrar publish:plugin];
|
||||
engine = nil;
|
||||
}
|
||||
OCMVerify([plugin detachFromEngineForRegistrar:[OCMArg any]]);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user