From ed450ad6786d7594280be62c8d1bacba7be1a9cf Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Mon, 18 Dec 2023 12:40:16 +0100 Subject: [PATCH] Added CustomPathHandler class to be able to implement Android custom path handlers for WebViewAssetLoader --- flutter_inappwebview/CHANGELOG.md | 5 ++ flutter_inappwebview/example/lib/main.dart | 3 +- flutter_inappwebview/example/pubspec.yaml | 1 + .../lib/src/webview_asset_loader.dart | 24 ++++++ flutter_inappwebview/pubspec.yaml | 12 +-- flutter_inappwebview_android/CHANGELOG.md | 5 ++ .../example/pubspec.lock | 7 +- .../lib/src/inappwebview_platform.dart | 11 +++ .../lib/src/webview_asset_loader.dart | 37 +++++++++ flutter_inappwebview_android/pubspec.yaml | 4 +- flutter_inappwebview_ios/CHANGELOG.md | 4 + flutter_inappwebview_ios/pubspec.yaml | 4 +- flutter_inappwebview_macos/CHANGELOG.md | 4 + flutter_inappwebview_macos/pubspec.yaml | 4 +- .../CHANGELOG.md | 4 + .../lib/src/inappwebview_platform.dart | 11 +++ .../src/platform_webview_asset_loader.dart | 78 ++++++++++++++++++- .../pubspec.yaml | 2 +- flutter_inappwebview_web/CHANGELOG.md | 4 + flutter_inappwebview_web/pubspec.yaml | 4 +- 20 files changed, 204 insertions(+), 24 deletions(-) diff --git a/flutter_inappwebview/CHANGELOG.md b/flutter_inappwebview/CHANGELOG.md index 4cb26fb5..f828f2e5 100755 --- a/flutter_inappwebview/CHANGELOG.md +++ b/flutter_inappwebview/CHANGELOG.md @@ -1,3 +1,8 @@ +## 6.0.0-rc.2 + +- Updated minimum platform interface and implementation versions +- Added `CustomPathHandler` class to be able to implement Android custom path handlers for `WebViewAssetLoader` + ## 6.0.0-rc.1 - Updated minimum platform interface and implementation versions diff --git a/flutter_inappwebview/example/lib/main.dart b/flutter_inappwebview/example/lib/main.dart index d4655513..21760f26 100755 --- a/flutter_inappwebview/example/lib/main.dart +++ b/flutter_inappwebview/example/lib/main.dart @@ -14,8 +14,7 @@ import 'package:pointer_interceptor/pointer_interceptor.dart'; // import 'package:path_provider/path_provider.dart'; // import 'package:permission_handler/permission_handler.dart'; -InAppLocalhostServer localhostServer = - InAppLocalhostServer(documentRoot: 'assets'); +final localhostServer = InAppLocalhostServer(documentRoot: 'assets'); Future main() async { WidgetsFlutterBinding.ensureInitialized(); diff --git a/flutter_inappwebview/example/pubspec.yaml b/flutter_inappwebview/example/pubspec.yaml index ef088c4f..fa737046 100755 --- a/flutter_inappwebview/example/pubspec.yaml +++ b/flutter_inappwebview/example/pubspec.yaml @@ -27,6 +27,7 @@ dependencies: permission_handler: 10.2.0 url_launcher: 6.1.11 pointer_interceptor: ^0.9.3+4 + # mime: ^1.0.4 # connectivity: ^0.4.5+6 flutter_inappwebview: path: ../ diff --git a/flutter_inappwebview/lib/src/webview_asset_loader.dart b/flutter_inappwebview/lib/src/webview_asset_loader.dart index ce3cfe44..7ccc155a 100644 --- a/flutter_inappwebview/lib/src/webview_asset_loader.dart +++ b/flutter_inappwebview/lib/src/webview_asset_loader.dart @@ -106,3 +106,27 @@ class InternalStoragePathHandler extends PathHandler { String get directory => platform.directory; } + +///{@macro flutter_inappwebview_platform_interface.PlatformCustomPathHandler} +abstract class CustomPathHandler extends PathHandler { + ///{@macro flutter_inappwebview_platform_interface.PlatformCustomPathHandler} + CustomPathHandler({required String path}) + : this.fromPlatformCreationParams( + params: PlatformCustomPathHandlerCreationParams( + PlatformPathHandlerCreationParams(path: path))); + + /// Constructs a [CustomPathHandler]. + /// + /// See [CustomPathHandler.fromPlatformCreationParams] for setting parameters for + /// a specific platform. + CustomPathHandler.fromPlatformCreationParams({ + required PlatformCustomPathHandlerCreationParams params, + }) : this.fromPlatform(platform: PlatformCustomPathHandler(params)); + + /// Constructs a [CustomPathHandler] from a specific platform implementation. + CustomPathHandler.fromPlatform({required this.platform}) + : super.fromPlatform(platform: platform); + + /// Implementation of [PlatformCustomPathHandler] for the current platform. + final PlatformCustomPathHandler platform; +} diff --git a/flutter_inappwebview/pubspec.yaml b/flutter_inappwebview/pubspec.yaml index 026818d0..d13b204f 100755 --- a/flutter_inappwebview/pubspec.yaml +++ b/flutter_inappwebview/pubspec.yaml @@ -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: 6.0.0-rc.1 +version: 6.0.0-rc.2 homepage: https://inappwebview.dev/ repository: https://github.com/pichillilorenzo/flutter_inappwebview issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues @@ -18,11 +18,11 @@ environment: dependencies: flutter: sdk: flutter - flutter_inappwebview_platform_interface: ^1.0.7 - flutter_inappwebview_android: ^1.0.9 - flutter_inappwebview_ios: ^1.0.10 - flutter_inappwebview_macos: ^1.0.8 - flutter_inappwebview_web: ^1.0.5 + flutter_inappwebview_platform_interface: ^1.0.8 + flutter_inappwebview_android: ^1.0.10 + flutter_inappwebview_ios: ^1.0.11 + flutter_inappwebview_macos: ^1.0.9 + flutter_inappwebview_web: ^1.0.6 dev_dependencies: flutter_test: diff --git a/flutter_inappwebview_android/CHANGELOG.md b/flutter_inappwebview_android/CHANGELOG.md index 023d7325..523621ac 100644 --- a/flutter_inappwebview_android/CHANGELOG.md +++ b/flutter_inappwebview_android/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.0.10 + +- Updated `flutter_inappwebview_platform_interface` version dependency to `^1.0.8` +- Implemented `PlatformCustomPathHandler` class + ## 1.0.9 - Updated `flutter_inappwebview_platform_interface` version dependency to `^1.0.7` diff --git a/flutter_inappwebview_android/example/pubspec.lock b/flutter_inappwebview_android/example/pubspec.lock index 1063ef86..3cbe7f04 100644 --- a/flutter_inappwebview_android/example/pubspec.lock +++ b/flutter_inappwebview_android/example/pubspec.lock @@ -93,10 +93,9 @@ packages: flutter_inappwebview_platform_interface: dependency: transitive description: - name: flutter_inappwebview_platform_interface - sha256: b4db31621880c4f4a60c9ddfc01d2b9f0133bf7776ddbd3a1d34062c076afec7 - url: "https://pub.dev" - source: hosted + path: "../../flutter_inappwebview_platform_interface" + relative: true + source: path version: "1.0.7" flutter_lints: dependency: "direct dev" diff --git a/flutter_inappwebview_android/lib/src/inappwebview_platform.dart b/flutter_inappwebview_android/lib/src/inappwebview_platform.dart index 5d11fca8..65e1b426 100644 --- a/flutter_inappwebview_android/lib/src/inappwebview_platform.dart +++ b/flutter_inappwebview_android/lib/src/inappwebview_platform.dart @@ -313,6 +313,17 @@ class AndroidInAppWebViewPlatform extends InAppWebViewPlatform { return AndroidInternalStoragePathHandler(params); } + /// Creates a new [AndroidCustomPathHandler]. + /// + /// This function should only be called by the app-facing package. + /// Look at using [CustomPathHandler] in `flutter_inappwebview` instead. + @override + AndroidCustomPathHandler createPlatformCustomPathHandler( + PlatformCustomPathHandlerCreationParams params, + ) { + return AndroidCustomPathHandler(params); + } + /// Creates a new [wv.AndroidWebViewFeature]. /// /// This function should only be called by the app-facing package. diff --git a/flutter_inappwebview_android/lib/src/webview_asset_loader.dart b/flutter_inappwebview_android/lib/src/webview_asset_loader.dart index db56f1a5..b54a7dcf 100644 --- a/flutter_inappwebview_android/lib/src/webview_asset_loader.dart +++ b/flutter_inappwebview_android/lib/src/webview_asset_loader.dart @@ -198,3 +198,40 @@ class AndroidInternalStoragePathHandler return {...toMap(), 'directory': directory}; } } + +/// Object specifying creation parameters for creating a [AndroidCustomPathHandler]. +/// +/// When adding additional fields make sure they can be null or have a default +/// value to avoid breaking changes. See [PlatformCustomPathHandlerCreationParams] for +/// more information. +@immutable +class AndroidCustomPathHandlerCreationParams + extends PlatformCustomPathHandlerCreationParams { + /// Creates a new [AndroidCustomPathHandlerCreationParams] instance. + AndroidCustomPathHandlerCreationParams( + // This parameter prevents breaking changes later. + // ignore: avoid_unused_constructor_parameters + PlatformCustomPathHandlerCreationParams params, + ) : super(params); + + /// Creates a [AndroidCustomPathHandlerCreationParams] instance based on [PlatformCustomPathHandlerCreationParams]. + factory AndroidCustomPathHandlerCreationParams.fromPlatformCustomPathHandlerCreationParams( + PlatformCustomPathHandlerCreationParams params) { + return AndroidCustomPathHandlerCreationParams(params); + } +} + +///{@macro flutter_inappwebview_platform_interface.PlatformCustomPathHandler} +class AndroidCustomPathHandler extends PlatformCustomPathHandler + with AndroidPathHandler, ChannelController { + /// Constructs a [AndroidCustomPathHandler]. + AndroidCustomPathHandler(PlatformCustomPathHandlerCreationParams params) + : super.implementation( + params is AndroidCustomPathHandlerCreationParams + ? params + : AndroidCustomPathHandlerCreationParams + .fromPlatformCustomPathHandlerCreationParams(params), + ) { + _init(params); + } +} diff --git a/flutter_inappwebview_android/pubspec.yaml b/flutter_inappwebview_android/pubspec.yaml index a3f16328..9a63ab7d 100644 --- a/flutter_inappwebview_android/pubspec.yaml +++ b/flutter_inappwebview_android/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_inappwebview_android description: Android implementation of the flutter_inappwebview plugin. -version: 1.0.9 +version: 1.0.10 homepage: https://inappwebview.dev/ repository: https://github.com/pichillilorenzo/flutter_inappwebview/tree/master/flutter_inappwebview_android issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues @@ -18,7 +18,7 @@ environment: dependencies: flutter: sdk: flutter - flutter_inappwebview_platform_interface: ^1.0.7 + flutter_inappwebview_platform_interface: ^1.0.8 dev_dependencies: flutter_test: diff --git a/flutter_inappwebview_ios/CHANGELOG.md b/flutter_inappwebview_ios/CHANGELOG.md index deaedb77..db5fec34 100644 --- a/flutter_inappwebview_ios/CHANGELOG.md +++ b/flutter_inappwebview_ios/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.0.11 + +- Updated `flutter_inappwebview_platform_interface` version dependency to `^1.0.8` + ## 1.0.10 - Updated `flutter_inappwebview_platform_interface` version dependency to `^1.0.7` diff --git a/flutter_inappwebview_ios/pubspec.yaml b/flutter_inappwebview_ios/pubspec.yaml index 2a48a708..0b1c750e 100644 --- a/flutter_inappwebview_ios/pubspec.yaml +++ b/flutter_inappwebview_ios/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_inappwebview_ios description: iOS implementation of the flutter_inappwebview plugin. -version: 1.0.10 +version: 1.0.11 homepage: https://inappwebview.dev/ repository: https://github.com/pichillilorenzo/flutter_inappwebview/tree/master/flutter_inappwebview_ios issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues @@ -18,7 +18,7 @@ environment: dependencies: flutter: sdk: flutter - flutter_inappwebview_platform_interface: ^1.0.7 + flutter_inappwebview_platform_interface: ^1.0.8 dev_dependencies: flutter_test: diff --git a/flutter_inappwebview_macos/CHANGELOG.md b/flutter_inappwebview_macos/CHANGELOG.md index 20914bcc..e88610f4 100644 --- a/flutter_inappwebview_macos/CHANGELOG.md +++ b/flutter_inappwebview_macos/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.0.9 + +- Updated `flutter_inappwebview_platform_interface` version dependency to `^1.0.8` + ## 1.0.8 - Updated `flutter_inappwebview_platform_interface` version dependency to `^1.0.7` diff --git a/flutter_inappwebview_macos/pubspec.yaml b/flutter_inappwebview_macos/pubspec.yaml index 44eb0d99..d25878af 100644 --- a/flutter_inappwebview_macos/pubspec.yaml +++ b/flutter_inappwebview_macos/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_inappwebview_macos description: macOS implementation of the flutter_inappwebview plugin. -version: 1.0.8 +version: 1.0.9 homepage: https://inappwebview.dev/ repository: https://github.com/pichillilorenzo/flutter_inappwebview/tree/master/flutter_inappwebview_macos issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues @@ -18,7 +18,7 @@ environment: dependencies: flutter: sdk: flutter - flutter_inappwebview_platform_interface: ^1.0.7 + flutter_inappwebview_platform_interface: ^1.0.8 dev_dependencies: flutter_test: diff --git a/flutter_inappwebview_platform_interface/CHANGELOG.md b/flutter_inappwebview_platform_interface/CHANGELOG.md index 9eff28f5..a0b91031 100644 --- a/flutter_inappwebview_platform_interface/CHANGELOG.md +++ b/flutter_inappwebview_platform_interface/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.0.8 + +- Added `PlatformCustomPathHandler` class to be able to implement custom path handlers for `WebViewAssetLoader` + ## 1.0.7 - Added `InAppBrowser.onMainWindowWillClose` event diff --git a/flutter_inappwebview_platform_interface/lib/src/inappwebview_platform.dart b/flutter_inappwebview_platform_interface/lib/src/inappwebview_platform.dart index ef96c5d2..a216e5b9 100644 --- a/flutter_inappwebview_platform_interface/lib/src/inappwebview_platform.dart +++ b/flutter_inappwebview_platform_interface/lib/src/inappwebview_platform.dart @@ -371,6 +371,17 @@ abstract class InAppWebViewPlatform extends PlatformInterface { 'createPlatformInternalStoragePathHandler is not implemented on the current platform.'); } + /// Creates a new [PlatformCustomPathHandler]. + /// + /// This function should only be called by the app-facing package. + /// Look at using [CustomPathHandler] in `flutter_inappwebview` instead. + PlatformCustomPathHandler createPlatformCustomPathHandler( + PlatformCustomPathHandlerCreationParams params, + ) { + throw UnimplementedError( + 'createPlatformCustomPathHandler is not implemented on the current platform.'); + } + /// Creates a new [PlatformWebViewFeature]. /// /// This function should only be called by the app-facing package. diff --git a/flutter_inappwebview_platform_interface/lib/src/platform_webview_asset_loader.dart b/flutter_inappwebview_platform_interface/lib/src/platform_webview_asset_loader.dart index 0bd6c851..b70fcee8 100644 --- a/flutter_inappwebview_platform_interface/lib/src/platform_webview_asset_loader.dart +++ b/flutter_inappwebview_platform_interface/lib/src/platform_webview_asset_loader.dart @@ -1,5 +1,6 @@ import 'package:flutter/foundation.dart'; import 'package:flutter_inappwebview_internal_annotations/flutter_inappwebview_internal_annotations.dart'; +import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; import 'inappwebview_platform.dart'; @@ -122,7 +123,7 @@ class PlatformAssetsPathHandlerCreationParams PlatformPathHandlerCreationParams params, ) : super(path: params.path); - /// Creates a [AndroidCookieManagerCreationParams] instance based on [PlatformCookieManagerCreationParams]. + /// Creates a [PlatformAssetsPathHandlerCreationParams] instance based on [PlatformPathHandlerCreationParams]. factory PlatformAssetsPathHandlerCreationParams.fromPlatformPathHandlerCreationParams( PlatformPathHandlerCreationParams params) { return PlatformAssetsPathHandlerCreationParams(params); @@ -195,7 +196,7 @@ class PlatformResourcesPathHandlerCreationParams PlatformPathHandlerCreationParams params, ) : super(path: params.path); - /// Creates a [AndroidCookieManagerCreationParams] instance based on [PlatformCookieManagerCreationParams]. + /// Creates a [PlatformResourcesPathHandlerCreationParams] instance based on [PlatformPathHandlerCreationParams]. factory PlatformResourcesPathHandlerCreationParams.fromPlatformPathHandlerCreationParams( PlatformPathHandlerCreationParams params) { return PlatformResourcesPathHandlerCreationParams(params); @@ -271,7 +272,7 @@ class PlatformInternalStoragePathHandlerCreationParams {required this.directory}) : super(path: params.path); - /// Creates a [AndroidCookieManagerCreationParams] instance based on [PlatformCookieManagerCreationParams]. + /// Creates a [PlatformInternalStoragePathHandlerCreationParams] instance based on [PlatformPathHandlerCreationParams]. factory PlatformInternalStoragePathHandlerCreationParams.fromPlatformPathHandlerCreationParams( PlatformPathHandlerCreationParams params, {required String directory}) { @@ -342,3 +343,74 @@ abstract class PlatformInternalStoragePathHandler extends PlatformInterface String get directory => params.directory; } + +/// Object specifying creation parameters for creating a [PlatformCustomPathHandler]. +/// +/// Platform specific implementations can add additional fields by extending +/// this class. +@immutable +class PlatformCustomPathHandlerCreationParams + extends PlatformPathHandlerCreationParams { + /// Used by the platform implementation to create a new [PlatformCustomPathHandler]. + PlatformCustomPathHandlerCreationParams( + // This parameter prevents breaking changes later. + // ignore: avoid_unused_constructor_parameters + PlatformPathHandlerCreationParams params, + ) : super(path: params.path); + + /// Creates a [PlatformCustomPathHandlerCreationParams] instance based on [PlatformPathHandlerCreationParams]. + factory PlatformCustomPathHandlerCreationParams.fromPlatformPathHandlerCreationParams( + PlatformPathHandlerCreationParams params) { + return PlatformCustomPathHandlerCreationParams(params); + } +} + +///{@template flutter_inappwebview_platform_interface.PlatformCustomPathHandler} +///Custom handler class used to implement a custom logic to open a file. +/// +///The matched prefix path used shouldn't be a prefix of a real web path. +///Thus, if the requested file cannot be found a [WebResourceResponse] object with a `null` data will be returned instead of `null`. +///This saves the time of falling back to network and trying to resolve a path that doesn't exist. +///A [WebResourceResponse] with `null` data will be received as an HTTP response with status code `404` and no body. +/// +///The MIME type for the file will be determined from the file's extension using +///[guessContentTypeFromName](https://developer.android.com/reference/java/net/URLConnection.html#guessContentTypeFromName-java.lang.String-). +///Developers should ensure that asset files are named using standard file extensions. +///If the file does not have a recognised extension, `text/plain` will be used by default. +///{@endtemplate} +abstract class PlatformCustomPathHandler extends PlatformInterface + implements PlatformPathHandler { + /// Creates a new [PlatformCustomPathHandler] + factory PlatformCustomPathHandler( + PlatformCustomPathHandlerCreationParams params) { + assert( + InAppWebViewPlatform.instance != null, + 'A platform implementation for `flutter_inappwebview` has not been set. Please ' + 'ensure that an implementation of `InAppWebViewPlatform` has been set to ' + '`InAppWebViewPlatform.instance` before use. For unit testing, ' + '`InAppWebViewPlatform.instance` can be set with your own test implementation.', + ); + final PlatformCustomPathHandler customPathHandler = + InAppWebViewPlatform.instance!.createPlatformCustomPathHandler(params); + PlatformInterface.verify(customPathHandler, _token); + return customPathHandler; + } + + /// Used by the platform implementation to create a new [PlatformCustomPathHandler]. + /// + /// Should only be used by platform implementations because they can't extend + /// a class that only contains a factory constructor. + @protected + PlatformCustomPathHandler.implementation(this.params) : super(token: _token); + + static final Object _token = Object(); + + /// The parameters used to initialize the [PlatformCustomPathHandler]. + final PlatformCustomPathHandlerCreationParams params; + + @override + String get type => 'CustomPathHandler'; + + @override + String get path => params.path; +} diff --git a/flutter_inappwebview_platform_interface/pubspec.yaml b/flutter_inappwebview_platform_interface/pubspec.yaml index 920fb721..416e2f89 100644 --- a/flutter_inappwebview_platform_interface/pubspec.yaml +++ b/flutter_inappwebview_platform_interface/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_inappwebview_platform_interface description: A common platform interface for the flutter_inappwebview plugin. -version: 1.0.7 +version: 1.0.8 homepage: https://inappwebview.dev/ repository: https://github.com/pichillilorenzo/flutter_inappwebview/tree/master/flutter_inappwebview_platform_interface issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues diff --git a/flutter_inappwebview_web/CHANGELOG.md b/flutter_inappwebview_web/CHANGELOG.md index b220715c..cfd49b8f 100644 --- a/flutter_inappwebview_web/CHANGELOG.md +++ b/flutter_inappwebview_web/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.0.6 + +- Updated `flutter_inappwebview_platform_interface` version dependency to `^1.0.8` + ## 1.0.5 - Updated `flutter_inappwebview_platform_interface` version dependency to `^1.0.7` diff --git a/flutter_inappwebview_web/pubspec.yaml b/flutter_inappwebview_web/pubspec.yaml index 5b47eb5a..57290173 100644 --- a/flutter_inappwebview_web/pubspec.yaml +++ b/flutter_inappwebview_web/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_inappwebview_web description: Web implementation of the flutter_inappwebview plugin. -version: 1.0.5 +version: 1.0.6 homepage: https://inappwebview.dev/ repository: https://github.com/pichillilorenzo/flutter_inappwebview/tree/master/flutter_inappwebview_web issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues @@ -21,7 +21,7 @@ dependencies: flutter_web_plugins: sdk: flutter js: ^0.6.4 - flutter_inappwebview_platform_interface: ^1.0.7 + flutter_inappwebview_platform_interface: ^1.0.8 dev_dependencies: flutter_test: