Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[webview_flutter_wkwebview] Prevents NSObject.removeObserver from b…
Browse files Browse the repository at this point in the history
…eing called without calling `addObserver` first (#5975)
  • Loading branch information
bparrishMines authored Jun 15, 2022
1 parent 95a1f2c commit ec8a69b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class WebKitWebViewPlatformController extends WebViewPlatformController {

bool _zoomEnabled = true;
bool _hasNavigationDelegate = false;
bool _progressObserverSet = false;

final Map<String, WKScriptMessageHandler> _scriptMessageHandlers =
<String, WKScriptMessageHandler>{};
Expand Down Expand Up @@ -455,17 +456,20 @@ class WebKitWebViewPlatformController extends WebViewPlatformController {
await _resetUserScripts(removedJavaScriptChannels: javascriptChannelNames);
}

Future<void> _setHasProgressTracking(bool hasProgressTracking) {
Future<void> _setHasProgressTracking(bool hasProgressTracking) async {
if (hasProgressTracking) {
return webView.addObserver(
_progressObserverSet = true;
await webView.addObserver(
webView,
keyPath: 'estimatedProgress',
options: <NSKeyValueObservingOptions>{
NSKeyValueObservingOptions.newValue,
},
);
} else {
return webView.removeObserver(webView, keyPath: 'estimatedProgress');
} else if (_progressObserverSet) {
// Calls to removeObserver before addObserver causes a crash.
_progressObserverSet = false;
await webView.removeObserver(webView, keyPath: 'estimatedProgress');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,16 @@ void main() {

verify(mockCallbacksHandler.onProgress(32));
});

testWidgets('progress observer is not removed without being set first',
(WidgetTester tester) async {
await buildWidget(tester, hasProgressTracking: false);

verifyNever(mockWebView.removeObserver(
mockWebView,
keyPath: 'estimatedProgress',
));
});
});

group('JavascriptChannelRegistry', () {
Expand Down

0 comments on commit ec8a69b

Please sign in to comment.