Skip to content

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 (flutter#5975)
  • Loading branch information
bparrishMines authored and yutaaraki-toydium committed Aug 12, 2022
1 parent b5e7bfb commit 743666a
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 743666a

Please sign in to comment.