From 8af84b92dc8c53d8cf2ab2f97834b92be3cbcd4b Mon Sep 17 00:00:00 2001 From: Swanseo0 <98388769+Swanseo0@users.noreply.github.com> Date: Mon, 4 Dec 2023 12:34:47 +0900 Subject: [PATCH] [webview_flutter_lwe] Update webivew_flutter to 4.4.2 (#633) --- packages/webview_flutter_lwe/CHANGELOG.md | 8 +- packages/webview_flutter_lwe/README.md | 4 +- .../webview_flutter_test.dart | 205 ++++++++---------- .../webview_flutter_lwe/example/lib/main.dart | 54 ++++- .../webview_flutter_lwe/example/pubspec.yaml | 2 +- .../lib/src/lwe_webview.dart | 8 +- .../lib/src/lwe_webview_controller.dart | 35 +++ packages/webview_flutter_lwe/pubspec.yaml | 11 +- .../webview_flutter_lwe/tizen/src/webview.cc | 5 +- 9 files changed, 209 insertions(+), 123 deletions(-) diff --git a/packages/webview_flutter_lwe/CHANGELOG.md b/packages/webview_flutter_lwe/CHANGELOG.md index bb9ad14ec..0858a2891 100644 --- a/packages/webview_flutter_lwe/CHANGELOG.md +++ b/packages/webview_flutter_lwe/CHANGELOG.md @@ -1,5 +1,11 @@ -## NEXT +## 0.3.0 +* Update webivew_flutter to 4.4.2. +* Update webview_flutter_platform_interface to 2.6.0. +* Update integration_test. +* Add pub topics to package metadata. +* Add support to retrieve the user agent. See `LweWebViewController.getUserAgent`. +* Apply Platform API change. * Increase the minimum Flutter version to 3.3. ## 0.2.0 diff --git a/packages/webview_flutter_lwe/README.md b/packages/webview_flutter_lwe/README.md index 918637efd..bea45df01 100644 --- a/packages/webview_flutter_lwe/README.md +++ b/packages/webview_flutter_lwe/README.md @@ -20,8 +20,8 @@ This package is not an _endorsed_ implementation of `webview_flutter`. Therefore ```yaml dependencies: - webview_flutter: ^4.0.2 - webview_flutter_lwe: ^0.2.0 + webview_flutter: ^4.4.2 + webview_flutter_lwe: ^0.3.0 ``` ## Example diff --git a/packages/webview_flutter_lwe/example/integration_test/webview_flutter_test.dart b/packages/webview_flutter_lwe/example/integration_test/webview_flutter_test.dart index e92333058..1cf0d9fe1 100644 --- a/packages/webview_flutter_lwe/example/integration_test/webview_flutter_test.dart +++ b/packages/webview_flutter_lwe/example/integration_test/webview_flutter_test.dart @@ -10,7 +10,6 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; @@ -20,7 +19,7 @@ Future main() async { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); final HttpServer server = await HttpServer.bind(InternetAddress.anyIPv4, 0); - server.forEach((HttpRequest request) { + unawaited(server.forEach((HttpRequest request) { if (request.uri.path == '/hello.txt') { request.response.writeln('Hello, world.'); } else if (request.uri.path == '/secondary.txt') { @@ -33,7 +32,7 @@ Future main() async { fail('unexpected request: ${request.method} ${request.uri}'); } request.response.close(); - }); + })); final String prefixUrl = 'http://${server.address.address}:${server.port}'; final String primaryUrl = '$prefixUrl/hello.txt'; final String secondaryUrl = '$prefixUrl/secondary.txt'; @@ -41,11 +40,11 @@ Future main() async { testWidgets('loadRequest', (WidgetTester tester) async { final Completer pageFinished = Completer(); - final WebViewController controller = WebViewController() - ..setNavigationDelegate( - NavigationDelegate(onPageFinished: (_) => pageFinished.complete()), - ) - ..loadRequest(Uri.parse(primaryUrl)); + final WebViewController controller = WebViewController(); + unawaited(controller.setNavigationDelegate( + NavigationDelegate(onPageFinished: (_) => pageFinished.complete()), + )); + unawaited(controller.loadRequest(Uri.parse(primaryUrl))); await tester.pumpWidget(WebViewWidget(controller: controller)); @@ -58,12 +57,12 @@ Future main() async { testWidgets('runJavaScriptReturningResult', (WidgetTester tester) async { final Completer pageFinished = Completer(); - final WebViewController controller = WebViewController() - ..setJavaScriptMode(JavaScriptMode.unrestricted) - ..setNavigationDelegate( - NavigationDelegate(onPageFinished: (_) => pageFinished.complete()), - ) - ..loadRequest(Uri.parse(primaryUrl)); + final WebViewController controller = WebViewController(); + unawaited(controller.setJavaScriptMode(JavaScriptMode.unrestricted)); + unawaited(controller.setNavigationDelegate( + NavigationDelegate(onPageFinished: (_) => pageFinished.complete()), + )); + unawaited(controller.loadRequest(Uri.parse(primaryUrl))); await tester.pumpWidget(WebViewWidget(controller: controller)); @@ -77,11 +76,11 @@ Future main() async { testWidgets('JavascriptChannel', (WidgetTester tester) async { final Completer pageFinished = Completer(); - final WebViewController controller = WebViewController() - ..setJavaScriptMode(JavaScriptMode.unrestricted) - ..setNavigationDelegate( - NavigationDelegate(onPageFinished: (_) => pageFinished.complete()), - ); + final WebViewController controller = WebViewController(); + unawaited(controller.setJavaScriptMode(JavaScriptMode.unrestricted)); + unawaited(controller.setNavigationDelegate( + NavigationDelegate(onPageFinished: (_) => pageFinished.complete()), + )); final Completer channelCompleter = Completer(); await controller.addJavaScriptChannel( @@ -138,19 +137,19 @@ Future main() async { testWidgets('set custom userAgent', (WidgetTester tester) async { final Completer pageFinished = Completer(); - final WebViewController controller = WebViewController() - ..setJavaScriptMode(JavaScriptMode.unrestricted) - ..setNavigationDelegate(NavigationDelegate( - onPageFinished: (_) => pageFinished.complete(), - )) - ..setUserAgent('Custom_User_Agent1') - ..loadRequest(Uri.parse('about:blank')); + final WebViewController controller = WebViewController(); + unawaited(controller.setJavaScriptMode(JavaScriptMode.unrestricted)); + unawaited(controller.setNavigationDelegate(NavigationDelegate( + onPageFinished: (_) => pageFinished.complete(), + ))); + unawaited(controller.setUserAgent('Custom_User_Agent1')); + unawaited(controller.loadRequest(Uri.parse('about:blank'))); await tester.pumpWidget(WebViewWidget(controller: controller)); await pageFinished.future; - final String customUserAgent = await _getUserAgent(controller); + final String? customUserAgent = await controller.getUserAgent(); expect(customUserAgent, 'Custom_User_Agent1'); }); @@ -167,14 +166,14 @@ Future main() async { base64Encode(const Utf8Encoder().convert(getTitleTest)); final Completer pageLoaded = Completer(); - final WebViewController controller = WebViewController() - ..setJavaScriptMode(JavaScriptMode.unrestricted) - ..setNavigationDelegate(NavigationDelegate( - onPageFinished: (_) => pageLoaded.complete(), - )) - ..loadRequest( - Uri.parse('data:text/html;charset=utf-8;base64,$getTitleTestBase64'), - ); + final WebViewController controller = WebViewController(); + unawaited(controller.setJavaScriptMode(JavaScriptMode.unrestricted)); + unawaited(controller.setNavigationDelegate(NavigationDelegate( + onPageFinished: (_) => pageLoaded.complete(), + ))); + unawaited(controller.loadRequest( + Uri.parse('data:text/html;charset=utf-8;base64,$getTitleTestBase64'), + )); await tester.pumpWidget(WebViewWidget(controller: controller)); @@ -217,14 +216,14 @@ Future main() async { base64Encode(const Utf8Encoder().convert(scrollTestPage)); final Completer pageLoaded = Completer(); - final WebViewController controller = WebViewController() - ..setJavaScriptMode(JavaScriptMode.unrestricted) - ..setNavigationDelegate(NavigationDelegate( - onPageFinished: (_) => pageLoaded.complete(), - )) - ..loadRequest(Uri.parse( - 'data:text/html;charset=utf-8;base64,$scrollTestPageBase64', - )); + final WebViewController controller = WebViewController(); + unawaited(controller.setJavaScriptMode(JavaScriptMode.unrestricted)); + unawaited(controller.setNavigationDelegate(NavigationDelegate( + onPageFinished: (_) => pageLoaded.complete(), + ))); + unawaited(controller.loadRequest(Uri.parse( + 'data:text/html;charset=utf-8;base64,$scrollTestPageBase64', + ))); await tester.pumpWidget(WebViewWidget(controller: controller)); @@ -264,20 +263,20 @@ Future main() async { testWidgets('can allow requests', (WidgetTester tester) async { Completer pageLoaded = Completer(); - final WebViewController controller = WebViewController() - ..setJavaScriptMode(JavaScriptMode.unrestricted) - ..setNavigationDelegate(NavigationDelegate( - onPageFinished: (_) => pageLoaded.complete(), - onNavigationRequest: (NavigationRequest navigationRequest) { - return (navigationRequest.url.contains('youtube.com')) - ? NavigationDecision.prevent - : NavigationDecision.navigate; - }, - )); + final WebViewController controller = WebViewController(); + unawaited(controller.setJavaScriptMode(JavaScriptMode.unrestricted)); + unawaited(controller.setNavigationDelegate(NavigationDelegate( + onPageFinished: (_) => pageLoaded.complete(), + onNavigationRequest: (NavigationRequest navigationRequest) { + return (navigationRequest.url.contains('youtube.com')) + ? NavigationDecision.prevent + : NavigationDecision.navigate; + }, + ))); await tester.pumpWidget(WebViewWidget(controller: controller)); - controller.loadRequest(Uri.parse(blankPageEncoded)); + unawaited(controller.loadRequest(Uri.parse(blankPageEncoded))); await pageLoaded.future; // Wait for initial page load. @@ -293,13 +292,14 @@ Future main() async { final Completer errorCompleter = Completer(); - final WebViewController controller = WebViewController() - ..setJavaScriptMode(JavaScriptMode.unrestricted) - ..setNavigationDelegate( - NavigationDelegate(onWebResourceError: (WebResourceError error) { - errorCompleter.complete(error); - })) - ..loadRequest(Uri.parse('https://www.notawebsite..com')); + final WebViewController controller = WebViewController(); + unawaited(controller.setJavaScriptMode(JavaScriptMode.unrestricted)); + unawaited(controller.setNavigationDelegate( + NavigationDelegate(onWebResourceError: (WebResourceError error) { + errorCompleter.complete(error); + }))); + unawaited( + controller.loadRequest(Uri.parse('https://www.notawebsite..com'))); await tester.pumpWidget(WebViewWidget(controller: controller)); @@ -313,17 +313,17 @@ Future main() async { Completer(); final Completer pageFinishCompleter = Completer(); - final WebViewController controller = WebViewController() - ..setJavaScriptMode(JavaScriptMode.unrestricted) - ..setNavigationDelegate(NavigationDelegate( - onPageFinished: (_) => pageFinishCompleter.complete(), - onWebResourceError: (WebResourceError error) { - errorCompleter.complete(error); - }, - )) - ..loadRequest( - Uri.parse('data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+'), - ); + final WebViewController controller = WebViewController(); + unawaited(controller.setJavaScriptMode(JavaScriptMode.unrestricted)); + unawaited(controller.setNavigationDelegate(NavigationDelegate( + onPageFinished: (_) => pageFinishCompleter.complete(), + onWebResourceError: (WebResourceError error) { + errorCompleter.complete(error); + }, + ))); + unawaited(controller.loadRequest( + Uri.parse('data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+'), + )); await tester.pumpWidget(WebViewWidget(controller: controller)); @@ -334,19 +334,19 @@ Future main() async { testWidgets('can block requests', (WidgetTester tester) async { Completer pageLoaded = Completer(); - final WebViewController controller = WebViewController() - ..setJavaScriptMode(JavaScriptMode.unrestricted) - ..setNavigationDelegate(NavigationDelegate( - onPageFinished: (_) => pageLoaded.complete(), - onNavigationRequest: (NavigationRequest navigationRequest) { - return (navigationRequest.url.contains('youtube.com')) - ? NavigationDecision.prevent - : NavigationDecision.navigate; - })); + final WebViewController controller = WebViewController(); + unawaited(controller.setJavaScriptMode(JavaScriptMode.unrestricted)); + unawaited(controller.setNavigationDelegate(NavigationDelegate( + onPageFinished: (_) => pageLoaded.complete(), + onNavigationRequest: (NavigationRequest navigationRequest) { + return (navigationRequest.url.contains('youtube.com')) + ? NavigationDecision.prevent + : NavigationDecision.navigate; + }))); await tester.pumpWidget(WebViewWidget(controller: controller)); - controller.loadRequest(Uri.parse(blankPageEncoded)); + unawaited(controller.loadRequest(Uri.parse(blankPageEncoded))); await pageLoaded.future; // Wait for initial page load. @@ -366,21 +366,21 @@ Future main() async { testWidgets('supports asynchronous decisions', (WidgetTester tester) async { Completer pageLoaded = Completer(); - final WebViewController controller = WebViewController() - ..setJavaScriptMode(JavaScriptMode.unrestricted) - ..setNavigationDelegate(NavigationDelegate( - onPageFinished: (_) => pageLoaded.complete(), - onNavigationRequest: (NavigationRequest navigationRequest) async { - NavigationDecision decision = NavigationDecision.prevent; - decision = await Future.delayed( - const Duration(milliseconds: 10), - () => NavigationDecision.navigate); - return decision; - })); + final WebViewController controller = WebViewController(); + unawaited(controller.setJavaScriptMode(JavaScriptMode.unrestricted)); + unawaited(controller.setNavigationDelegate(NavigationDelegate( + onPageFinished: (_) => pageLoaded.complete(), + onNavigationRequest: (NavigationRequest navigationRequest) async { + NavigationDecision decision = NavigationDecision.prevent; + decision = await Future.delayed( + const Duration(milliseconds: 10), + () => NavigationDecision.navigate); + return decision; + }))); await tester.pumpWidget(WebViewWidget(controller: controller)); - controller.loadRequest(Uri.parse(blankPageEncoded)); + unawaited(controller.loadRequest(Uri.parse(blankPageEncoded))); await pageLoaded.future; // Wait for initial page load. @@ -394,23 +394,6 @@ Future main() async { }); } -/// Returns the value used for the HTTP User-Agent: request header in subsequent HTTP requests. -Future _getUserAgent(WebViewController controller) async { - return _runJavascriptReturningResult(controller, 'navigator.userAgent;'); -} - -Future _runJavascriptReturningResult( - WebViewController controller, - String js, -) async { - if (defaultTargetPlatform == TargetPlatform.iOS || - defaultTargetPlatform == TargetPlatform.linux) { - return await controller.runJavaScriptReturningResult(js) as String; - } - return jsonDecode(await controller.runJavaScriptReturningResult(js) as String) - as String; -} - class ResizableWebView extends StatefulWidget { const ResizableWebView({ super.key, diff --git a/packages/webview_flutter_lwe/example/lib/main.dart b/packages/webview_flutter_lwe/example/lib/main.dart index d4c5f846b..068c4479a 100644 --- a/packages/webview_flutter_lwe/example/lib/main.dart +++ b/packages/webview_flutter_lwe/example/lib/main.dart @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// ignore_for_file: public_member_api_docs, avoid_print +// ignore_for_file: public_member_api_docs import 'dart:async'; import 'dart:convert'; @@ -70,6 +70,36 @@ const String kTransparentBackgroundPage = ''' '''; +const String kLogExamplePage = ''' + + + +Load file or HTML string example + + +

Local demo page

+

+ This page is used to test the forwarding of console logs to Dart. +

+ +
+ + + + + +
+ + +'''; + class WebViewExample extends StatefulWidget { const WebViewExample({super.key}); @@ -150,7 +180,7 @@ Page resource error: return FloatingActionButton( onPressed: () async { final String? url = await _controller.currentUrl(); - if (context.mounted) { + if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Favorited $url')), ); @@ -175,6 +205,7 @@ enum MenuOptions { loadHtmlString, transparentBackground, setCookie, + logExample, } class SampleMenu extends StatelessWidget { @@ -231,6 +262,9 @@ class SampleMenu extends StatelessWidget { case MenuOptions.setCookie: _onSetCookie(); break; + case MenuOptions.logExample: + _onLogExample(); + break; } }, itemBuilder: (BuildContext context) => >[ @@ -287,6 +321,10 @@ class SampleMenu extends StatelessWidget { value: MenuOptions.setCookie, child: Text('Set cookie'), ), + const PopupMenuItem( + value: MenuOptions.logExample, + child: Text('Log example'), + ), ], ); } @@ -408,7 +446,7 @@ class SampleMenu extends StatelessWidget { } Widget _getCookieList(String cookies) { - if (cookies == null || cookies == '""') { + if (cookies == '""') { return Container(); } final List cookieList = cookies.split(';'); @@ -431,6 +469,16 @@ class SampleMenu extends StatelessWidget { return indexFile.path; } + + Future _onLogExample() { + webViewController + .setOnConsoleMessage((JavaScriptConsoleMessage consoleMessage) { + debugPrint( + '== JS == ${consoleMessage.level.name}: ${consoleMessage.message}'); + }); + + return webViewController.loadHtmlString(kLogExamplePage); + } } class NavigationControls extends StatelessWidget { diff --git a/packages/webview_flutter_lwe/example/pubspec.yaml b/packages/webview_flutter_lwe/example/pubspec.yaml index 3d9805195..bcb8f807a 100644 --- a/packages/webview_flutter_lwe/example/pubspec.yaml +++ b/packages/webview_flutter_lwe/example/pubspec.yaml @@ -12,7 +12,7 @@ dependencies: path_provider: ^2.0.7 path_provider_tizen: path: ../../path_provider/ - webview_flutter: ^4.0.2 + webview_flutter: ^4.4.2 webview_flutter_lwe: path: ../ diff --git a/packages/webview_flutter_lwe/lib/src/lwe_webview.dart b/packages/webview_flutter_lwe/lib/src/lwe_webview.dart index 6f7f38e4d..7241fa8b5 100644 --- a/packages/webview_flutter_lwe/lib/src/lwe_webview.dart +++ b/packages/webview_flutter_lwe/lib/src/lwe_webview.dart @@ -195,5 +195,11 @@ class LweWebView { /// Sets the value used for the HTTP `User-Agent:` request header. Future setUserAgent(String? userAgent) => - _invokeChannelMethod('userAgent', userAgent); + _invokeChannelMethod('setUserAgent', userAgent); + + /// Gets the HTTP 'User-Agent:' request header. + Future getUserAgent() async { + final String? result = await _invokeChannelMethod('getUserAgent'); + return result; + } } diff --git a/packages/webview_flutter_lwe/lib/src/lwe_webview_controller.dart b/packages/webview_flutter_lwe/lib/src/lwe_webview_controller.dart index 8ea9a13ba..ddb76466a 100644 --- a/packages/webview_flutter_lwe/lib/src/lwe_webview_controller.dart +++ b/packages/webview_flutter_lwe/lib/src/lwe_webview_controller.dart @@ -159,6 +159,33 @@ class LweWebViewController extends PlatformWebViewController { @override Future setUserAgent(String? userAgent) => _webview.setUserAgent(userAgent); + + @override + Future getUserAgent() => _webview.getUserAgent(); + + @override + Future setOnPlatformPermissionRequest( + void Function( + PlatformWebViewPermissionRequest request, + ) onPermissionRequest, + ) async { + // The current version of LWE does not provide any functionality related to a 'permission request'. + throw UnimplementedError( + 'This version of `LweWebViewController` currently has no ' + 'implementation of `setOnPlatformPermissionRequest`.'); + } + + @override + Future setOnConsoleMessage( + void Function( + JavaScriptConsoleMessage consoleMessage, + ) onConsoleMessage, + ) async { + // The current version of LWE does not provide any functionality related to a 'console message'. + throw UnimplementedError( + 'This version of `LweWebViewController` currently has no ' + 'implementation of `setOnConsoleMessage`.'); + } } /// An implementation of [PlatformWebViewWidget] with the Lightweight Web Engine. @@ -402,4 +429,12 @@ class LweNavigationDelegate extends PlatformNavigationDelegate { ) async { _onWebResourceError = onWebResourceError; } + + @override + Future setOnUrlChange(UrlChangeCallback onUrlChange) async { + // The current version of LWE does not provide any functionality related to a 'url change'. + throw UnimplementedError( + 'This version of `LweNavigationDelegate` currently has no ' + 'implementation of `setOnConsoleMessage`.'); + } } diff --git a/packages/webview_flutter_lwe/pubspec.yaml b/packages/webview_flutter_lwe/pubspec.yaml index fc7e5d3aa..d94577ea3 100644 --- a/packages/webview_flutter_lwe/pubspec.yaml +++ b/packages/webview_flutter_lwe/pubspec.yaml @@ -2,7 +2,7 @@ name: webview_flutter_lwe description: Tizen implementation of the webview_flutter plugin backed by Lightweight Web Engine. homepage: https://github.com/flutter-tizen/plugins repository: https://github.com/flutter-tizen/plugins/tree/master/packages/webview_flutter_lwe -version: 0.2.0 +version: 0.3.0 environment: sdk: ">=2.18.0 <4.0.0" @@ -20,5 +20,10 @@ dependencies: flutter: sdk: flutter flutter_tizen: ^0.2.1 - webview_flutter: ^4.0.2 - webview_flutter_platform_interface: ^2.0.1 + webview_flutter: ^4.4.2 + webview_flutter_platform_interface: ^2.6.0 + +topics: + - html + - webview + - webview-flutter diff --git a/packages/webview_flutter_lwe/tizen/src/webview.cc b/packages/webview_flutter_lwe/tizen/src/webview.cc index 8da28c138..7b9e7b3f7 100644 --- a/packages/webview_flutter_lwe/tizen/src/webview.cc +++ b/packages/webview_flutter_lwe/tizen/src/webview.cc @@ -710,7 +710,7 @@ void WebView::HandleWebViewMethodCall(const FlMethodCall& method_call, webview_instance_->SetSettings(settings); result->Success(); } - } else if (method_name == "userAgent") { + } else if (method_name == "setUserAgent") { const auto* user_agent = std::get_if(arguments); if (user_agent) { LWE::Settings settings = webview_instance_->GetSettings(); @@ -718,6 +718,9 @@ void WebView::HandleWebViewMethodCall(const FlMethodCall& method_call, webview_instance_->SetSettings(settings); } result->Success(); + } else if (method_name == "getUserAgent") { + LWE::Settings settings = webview_instance_->GetSettings(); + result->Success(flutter::EncodableValue(settings.GetUserAgentString())); } else if (method_name == "setCookie") { result->NotImplemented(); } else {