Skip to content

Commit

Permalink
fix #1151
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed Apr 25, 2022
1 parent f511e4d commit 951b974
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.4.1+2

- Fixed "Android ServiceWorkerControllerCompat.setServiceWorkerClient(null) makes Webivew Plugin Crashes" [#1151](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1151)

## 5.4.1+1

- Fixed Android default context menu over custom context menu on API Level 31+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {

private void setServiceWorkerClient(Boolean isNull) {
if (serviceWorkerController != null) {
serviceWorkerController.setServiceWorkerClient(isNull ? null : new ServiceWorkerClientCompat() {
// set ServiceWorkerClient as null makes the app crashes, so just set a dummy ServiceWorkerClientCompat.
// https://github.com/pichillilorenzo/flutter_inappwebview/issues/1151
serviceWorkerController.setServiceWorkerClient(isNull ? dummyServiceWorkerClientCompat() : new ServiceWorkerClientCompat() {
@Nullable
@Override
public WebResourceResponse shouldInterceptRequest(@NonNull WebResourceRequest request) {
Expand Down Expand Up @@ -165,10 +167,20 @@ else if (flutterResult.result != null) {
}
}

private ServiceWorkerClientCompat dummyServiceWorkerClientCompat() {
return new ServiceWorkerClientCompat() {
@Nullable
@Override
public WebResourceResponse shouldInterceptRequest(@NonNull WebResourceRequest request) {
return null;
}
};
}

public void dispose() {
channel.setMethodCallHandler(null);
if (serviceWorkerController != null) {
serviceWorkerController.setServiceWorkerClient(null);
serviceWorkerController.setServiceWorkerClient(dummyServiceWorkerClientCompat());
serviceWorkerController = null;
}
plugin = null;
Expand Down
35 changes: 34 additions & 1 deletion example/integration_test/webview_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5449,7 +5449,7 @@ setTimeout(function() {
});

group('Service Worker', () {
testWidgets('AndroidInAppWebViewController', (WidgetTester tester) async {
testWidgets('shouldInterceptRequest', (WidgetTester tester) async {
final Completer completer = Completer();

var swAvailable = await AndroidWebViewFeature.isFeatureSupported(
Expand Down Expand Up @@ -5487,6 +5487,39 @@ setTimeout(function() {

expect(completer.future, completes);
}, skip: !Platform.isAndroid);

testWidgets('setServiceWorkerClient to null', (WidgetTester tester) async {
final Completer<String> pageLoaded = Completer<String>();

var swAvailable = await AndroidWebViewFeature.isFeatureSupported(
AndroidWebViewFeature.SERVICE_WORKER_BASIC_USAGE);
var swInterceptAvailable = await AndroidWebViewFeature.isFeatureSupported(
AndroidWebViewFeature.SERVICE_WORKER_SHOULD_INTERCEPT_REQUEST);

if (swAvailable && swInterceptAvailable) {
AndroidServiceWorkerController serviceWorkerController =
AndroidServiceWorkerController.instance();

await serviceWorkerController.setServiceWorkerClient(null);
}

await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest:
URLRequest(url: Uri.parse('https://mdn.github.io/sw-test/')),
onLoadStop: (controller, url) {
pageLoaded.complete(url!.toString());
},
),
),
);

final String url = await pageLoaded.future;
expect(url, "https://mdn.github.io/sw-test/");
}, skip: !Platform.isAndroid);
});

group('Cookie Manager', () {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_inappwebview
description: A Flutter plugin that allows you to add an inline webview, to use an headless webview, and to open an in-app browser window.
version: 5.4.1+1
version: 5.4.1+2
homepage: https://github.com/pichillilorenzo/flutter_inappwebview

environment:
Expand Down

0 comments on commit 951b974

Please sign in to comment.