From 5cd8c9396671b01dc7636b31d2da4be59254dd50 Mon Sep 17 00:00:00 2001 From: Maurice Parrish <10687576+bparrishMines@users.noreply.github.com> Date: Wed, 15 Jun 2022 09:58:11 -0700 Subject: [PATCH] [webview_flutter_wkwebview] Adds the dispose method to NSObjectFlutterApi (#5970) --- .../ios/Classes/FWFGeneratedWebKitApis.h | 2 ++ .../ios/Classes/FWFGeneratedWebKitApis.m | 11 ++++++++++ .../lib/src/common/web_kit.pigeon.dart | 20 +++++++++++++++++++ .../src/foundation/foundation_api_impls.dart | 5 +++++ .../pigeons/web_kit.dart | 3 +++ .../test/src/foundation/foundation_test.dart | 16 ++++++++++++++- 6 files changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FWFGeneratedWebKitApis.h b/packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FWFGeneratedWebKitApis.h index b0856ae11038..8cbd2c7c194c 100644 --- a/packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FWFGeneratedWebKitApis.h +++ b/packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FWFGeneratedWebKitApis.h @@ -451,6 +451,8 @@ NSObject *FWFNSObjectFlutterApiGetCodec(void); changeKeys:(NSArray *)changeKeys changeValues:(NSArray *)changeValues completion:(void (^)(NSError *_Nullable))completion; +- (void)disposeObjectWithIdentifier:(NSNumber *)identifier + completion:(void (^)(NSError *_Nullable))completion; @end /// The codec used by FWFWKWebViewHostApi. NSObject *FWFWKWebViewHostApiGetCodec(void); diff --git a/packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FWFGeneratedWebKitApis.m b/packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FWFGeneratedWebKitApis.m index 96c59987e841..10680227ee43 100644 --- a/packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FWFGeneratedWebKitApis.m +++ b/packages/webview_flutter/webview_flutter_wkwebview/ios/Classes/FWFGeneratedWebKitApis.m @@ -1989,6 +1989,17 @@ - (void)observeValueForObjectWithIdentifier:(NSNumber *)arg_identifier completion(nil); }]; } +- (void)disposeObjectWithIdentifier:(NSNumber *)arg_identifier + completion:(void (^)(NSError *_Nullable))completion { + FlutterBasicMessageChannel *channel = [FlutterBasicMessageChannel + messageChannelWithName:@"dev.flutter.pigeon.NSObjectFlutterApi.dispose" + binaryMessenger:self.binaryMessenger + codec:FWFNSObjectFlutterApiGetCodec()]; + [channel sendMessage:@[ arg_identifier ?: [NSNull null] ] + reply:^(id reply) { + completion(nil); + }]; +} @end @interface FWFWKWebViewHostApiCodecReader : FlutterStandardReader @end diff --git a/packages/webview_flutter/webview_flutter_wkwebview/lib/src/common/web_kit.pigeon.dart b/packages/webview_flutter/webview_flutter_wkwebview/lib/src/common/web_kit.pigeon.dart index 1640c61211cf..54bb3015af64 100644 --- a/packages/webview_flutter/webview_flutter_wkwebview/lib/src/common/web_kit.pigeon.dart +++ b/packages/webview_flutter/webview_flutter_wkwebview/lib/src/common/web_kit.pigeon.dart @@ -1770,6 +1770,7 @@ abstract class NSObjectFlutterApi { int objectIdentifier, List changeKeys, List changeValues); + void dispose(int identifier); static void setup(NSObjectFlutterApi? api, {BinaryMessenger? binaryMessenger}) { { @@ -1806,6 +1807,25 @@ abstract class NSObjectFlutterApi { }); } } + { + final BasicMessageChannel channel = BasicMessageChannel( + 'dev.flutter.pigeon.NSObjectFlutterApi.dispose', codec, + binaryMessenger: binaryMessenger); + if (api == null) { + channel.setMessageHandler(null); + } else { + channel.setMessageHandler((Object? message) async { + assert(message != null, + 'Argument for dev.flutter.pigeon.NSObjectFlutterApi.dispose was null.'); + final List args = (message as List?)!; + final int? arg_identifier = (args[0] as int?); + assert(arg_identifier != null, + 'Argument for dev.flutter.pigeon.NSObjectFlutterApi.dispose was null, expected non-null int.'); + api.dispose(arg_identifier!); + return; + }); + } + } } } diff --git a/packages/webview_flutter/webview_flutter_wkwebview/lib/src/foundation/foundation_api_impls.dart b/packages/webview_flutter/webview_flutter_wkwebview/lib/src/foundation/foundation_api_impls.dart index f9efe3616ca7..de760ec5b36e 100644 --- a/packages/webview_flutter/webview_flutter_wkwebview/lib/src/foundation/foundation_api_impls.dart +++ b/packages/webview_flutter/webview_flutter_wkwebview/lib/src/foundation/foundation_api_impls.dart @@ -183,4 +183,9 @@ class NSObjectFlutterApiImpl extends NSObjectFlutterApi { ), changeValues), ); } + + @override + void dispose(int identifier) { + instanceManager.remove(identifier); + } } diff --git a/packages/webview_flutter/webview_flutter_wkwebview/pigeons/web_kit.dart b/packages/webview_flutter/webview_flutter_wkwebview/pigeons/web_kit.dart index dfd271633573..c20a10ebfadd 100644 --- a/packages/webview_flutter/webview_flutter_wkwebview/pigeons/web_kit.dart +++ b/packages/webview_flutter/webview_flutter_wkwebview/pigeons/web_kit.dart @@ -510,6 +510,9 @@ abstract class NSObjectFlutterApi { List changeKeys, List changeValues, ); + + @ObjCSelector('disposeObjectWithIdentifier:') + void dispose(int identifier); } /// Mirror of WKWebView. diff --git a/packages/webview_flutter/webview_flutter_wkwebview/test/src/foundation/foundation_test.dart b/packages/webview_flutter/webview_flutter_wkwebview/test/src/foundation/foundation_test.dart index 8d15d65f3598..d97d152739c4 100644 --- a/packages/webview_flutter/webview_flutter_wkwebview/test/src/foundation/foundation_test.dart +++ b/packages/webview_flutter/webview_flutter_wkwebview/test/src/foundation/foundation_test.dart @@ -90,7 +90,7 @@ void main() { )); }); - test('dispose', () async { + test('NSObjectHostApi.dispose', () async { int? callbackIdentifier; final InstanceManager instanceManager = InstanceManager(onWeakReferenceRemoved: (int identifier) { @@ -145,6 +145,20 @@ void main() { ]), ); }); + + test('NSObjectFlutterApi.dispose', () { + FoundationFlutterApis.instance = FoundationFlutterApis( + instanceManager: instanceManager, + ); + + object = NSObject(instanceManager: instanceManager); + instanceManager.addHostCreatedInstance(object, 1); + + instanceManager.removeWeakReference(object); + FoundationFlutterApis.instance.object.dispose(1); + + expect(instanceManager.containsIdentifier(1), isFalse); + }); }); }); }