From 11152d2bc8f366e2c638cdd2a96d5a1634357938 Mon Sep 17 00:00:00 2001 From: EunchulJeon Date: Fri, 9 Feb 2024 23:54:38 +0900 Subject: [PATCH] [webview_flutter] Add interface for showing javascript dialog message (#4704) * there are cases where Web calls System Popup with javascript on webview_flutter * At this time, the message comes in the WKUIDelegate part in iOS. * https://developer.apple.com/documentation/webkit/wkuidelegate/1537406-webview * https://developer.apple.com/documentation/webkit/wkuidelegate/1536489-webview * Android also has a interface on WebChromeClient * https://developer.android.com/reference/android/webkit/WebChromeClient#onJsAlert(android.webkit.WebView,%20java.lang.String,%20java.lang.String,%20android.webkit.JsResult) * It was implemented according to the requirements of the code review of the https://github.com/flutter/packages/pull/4555 * Related issue: https://github.com/flutter/flutter/issues/30358#issuecomment-1645347616 * Related Interface PR: https://github.com/flutter/packages/pull/5670 --- .../webview_flutter/CHANGELOG.md | 7 +++ .../webview_flutter/example/pubspec.yaml | 10 ++-- .../lib/src/webview_controller.dart | 24 ++++++++ .../webview_flutter/lib/webview_flutter.dart | 3 + .../webview_flutter/pubspec.yaml | 12 ++-- .../test/legacy/webview_flutter_test.dart | 10 ++-- .../test/webview_controller_test.dart | 56 +++++++++++++++++++ 7 files changed, 106 insertions(+), 16 deletions(-) diff --git a/packages/webview_flutter/webview_flutter/CHANGELOG.md b/packages/webview_flutter/webview_flutter/CHANGELOG.md index 2b10fb2efb1a..64cb02db86fa 100644 --- a/packages/webview_flutter/webview_flutter/CHANGELOG.md +++ b/packages/webview_flutter/webview_flutter/CHANGELOG.md @@ -1,3 +1,10 @@ +## 4.6.0 + +* Adds support for custom handling of JavaScript dialogs. See + `WebViewController.setOnJavaScriptAlertDialog`, `WebViewController.setOnJavaScriptConfirmDialog` + and `WebViewController.setOnJavaScriptTextInputDialog`. +* Updates minimum Dart version to 3.2.3 and minimum Flutter version to 3.16.6. + ## 4.5.0 * Adds support for HTTP basic authentication. See `NavigationDelegate(onReceivedHttpAuthRequest)`. diff --git a/packages/webview_flutter/webview_flutter/example/pubspec.yaml b/packages/webview_flutter/webview_flutter/example/pubspec.yaml index 24b41bf5833c..805324927ae3 100644 --- a/packages/webview_flutter/webview_flutter/example/pubspec.yaml +++ b/packages/webview_flutter/webview_flutter/example/pubspec.yaml @@ -3,8 +3,8 @@ description: Demonstrates how to use the webview_flutter plugin. publish_to: none environment: - sdk: ">=3.0.0 <4.0.0" - flutter: ">=3.10.0" + sdk: ^3.2.3 + flutter: ">=3.16.6" dependencies: flutter: @@ -17,8 +17,8 @@ dependencies: # The example app is bundled with the plugin so we use a path dependency on # the parent directory to use the current plugin's version. path: ../ - webview_flutter_android: ^3.13.0 - webview_flutter_wkwebview: ^3.10.0 + webview_flutter_android: ^3.14.0 + webview_flutter_wkwebview: ^3.11.0 dev_dependencies: build_runner: ^2.1.5 @@ -27,7 +27,7 @@ dev_dependencies: sdk: flutter integration_test: sdk: flutter - webview_flutter_platform_interface: ^2.7.0 + webview_flutter_platform_interface: ^2.10.0 flutter: uses-material-design: true diff --git a/packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart b/packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart index 0b81978d6fcc..d917d7637c2e 100644 --- a/packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart +++ b/packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart @@ -370,6 +370,30 @@ class WebViewController { return platform.setOnConsoleMessage(onConsoleMessage); } + /// Sets a callback that notifies the host application that the web page + /// wants to display a JavaScript alert() dialog. + Future setOnJavaScriptAlertDialog( + Future Function(JavaScriptAlertDialogRequest request) + onJavaScriptAlertDialog) async { + return platform.setOnJavaScriptAlertDialog(onJavaScriptAlertDialog); + } + + /// Sets a callback that notifies the host application that the web page + /// wants to display a JavaScript confirm() dialog. + Future setOnJavaScriptConfirmDialog( + Future Function(JavaScriptConfirmDialogRequest request) + onJavaScriptConfirmDialog) async { + return platform.setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog); + } + + /// Sets a callback that notifies the host application that the web page + /// wants to display a JavaScript prompt() dialog. + Future setOnJavaScriptTextInputDialog( + Future Function(JavaScriptTextInputDialogRequest request) + onJavaScriptTextInputDialog) async { + return platform.setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog); + } + /// Gets the value used for the HTTP `User-Agent:` request header. Future getUserAgent() { return platform.getUserAgent(); diff --git a/packages/webview_flutter/webview_flutter/lib/webview_flutter.dart b/packages/webview_flutter/webview_flutter/lib/webview_flutter.dart index 3b6495ca96f1..29a7d48cd570 100644 --- a/packages/webview_flutter/webview_flutter/lib/webview_flutter.dart +++ b/packages/webview_flutter/webview_flutter/lib/webview_flutter.dart @@ -5,10 +5,13 @@ export 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart' show HttpAuthRequest, + JavaScriptAlertDialogRequest, + JavaScriptConfirmDialogRequest, JavaScriptConsoleMessage, JavaScriptLogLevel, JavaScriptMessage, JavaScriptMode, + JavaScriptTextInputDialogRequest, LoadRequestMethod, NavigationDecision, NavigationRequest, diff --git a/packages/webview_flutter/webview_flutter/pubspec.yaml b/packages/webview_flutter/webview_flutter/pubspec.yaml index 0572df372d52..b6d8bf8663de 100644 --- a/packages/webview_flutter/webview_flutter/pubspec.yaml +++ b/packages/webview_flutter/webview_flutter/pubspec.yaml @@ -2,11 +2,11 @@ name: webview_flutter description: A Flutter plugin that provides a WebView widget on Android and iOS. repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22 -version: 4.5.0 +version: 4.6.0 environment: - sdk: ">=3.0.0 <4.0.0" - flutter: ">=3.10.0" + sdk: ^3.2.3 + flutter: ">=3.16.6" flutter: plugin: @@ -19,9 +19,9 @@ flutter: dependencies: flutter: sdk: flutter - webview_flutter_android: ^3.13.0 - webview_flutter_platform_interface: ^2.7.0 - webview_flutter_wkwebview: ^3.10.0 + webview_flutter_android: ^3.14.0 + webview_flutter_platform_interface: ^2.10.0 + webview_flutter_wkwebview: ^3.11.0 dev_dependencies: build_runner: ^2.1.5 diff --git a/packages/webview_flutter/webview_flutter/test/legacy/webview_flutter_test.dart b/packages/webview_flutter/webview_flutter/test/legacy/webview_flutter_test.dart index 008e91a8bb29..6abd2648377a 100644 --- a/packages/webview_flutter/webview_flutter/test/legacy/webview_flutter_test.dart +++ b/packages/webview_flutter/webview_flutter/test/legacy/webview_flutter_test.dart @@ -1319,13 +1319,13 @@ class MatchesWebSettings extends Matcher { bool matches( covariant WebSettings webSettings, Map matchState) { return _webSettings!.javascriptMode == webSettings.javascriptMode && - _webSettings!.hasNavigationDelegate == + _webSettings.hasNavigationDelegate == webSettings.hasNavigationDelegate && - _webSettings!.debuggingEnabled == webSettings.debuggingEnabled && - _webSettings!.gestureNavigationEnabled == + _webSettings.debuggingEnabled == webSettings.debuggingEnabled && + _webSettings.gestureNavigationEnabled == webSettings.gestureNavigationEnabled && - _webSettings!.userAgent == webSettings.userAgent && - _webSettings!.zoomEnabled == webSettings.zoomEnabled; + _webSettings.userAgent == webSettings.userAgent && + _webSettings.zoomEnabled == webSettings.zoomEnabled; } } diff --git a/packages/webview_flutter/webview_flutter/test/webview_controller_test.dart b/packages/webview_flutter/webview_flutter/test/webview_controller_test.dart index 695a781738e2..9eb5343f84df 100644 --- a/packages/webview_flutter/webview_flutter/test/webview_controller_test.dart +++ b/packages/webview_flutter/webview_flutter/test/webview_controller_test.dart @@ -403,6 +403,62 @@ void main() { verify(mockPlatformWebViewController.setOnConsoleMessage(onConsoleMessage)); }); + test('setOnJavaScriptAlertDialog', () async { + final MockPlatformWebViewController mockPlatformWebViewController = + MockPlatformWebViewController(); + + final WebViewController webViewController = WebViewController.fromPlatform( + mockPlatformWebViewController, + ); + + Future onJavaScriptAlertDialog( + JavaScriptAlertDialogRequest request) async { + return; + } + + await webViewController.setOnJavaScriptAlertDialog(onJavaScriptAlertDialog); + verify(mockPlatformWebViewController + .setOnJavaScriptAlertDialog(onJavaScriptAlertDialog)); + }); + + test('setOnJavaScriptConfirmDialog', () async { + final MockPlatformWebViewController mockPlatformWebViewController = + MockPlatformWebViewController(); + + final WebViewController webViewController = WebViewController.fromPlatform( + mockPlatformWebViewController, + ); + + Future onJavaScriptConfirmDialog( + JavaScriptConfirmDialogRequest request) async { + return true; + } + + await webViewController + .setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog); + verify(mockPlatformWebViewController + .setOnJavaScriptConfirmDialog(onJavaScriptConfirmDialog)); + }); + + test('setOnJavaScriptTextInputDialog', () async { + final MockPlatformWebViewController mockPlatformWebViewController = + MockPlatformWebViewController(); + + final WebViewController webViewController = WebViewController.fromPlatform( + mockPlatformWebViewController, + ); + + Future onJavaScriptTextInputDialog( + JavaScriptTextInputDialogRequest request) async { + return 'text'; + } + + await webViewController + .setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog); + verify(mockPlatformWebViewController + .setOnJavaScriptTextInputDialog(onJavaScriptTextInputDialog)); + }); + test('getUserAgent', () async { final MockPlatformWebViewController mockPlatformWebViewController = MockPlatformWebViewController();