2023-11-17 22:28:11 +00:00
import ' package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart ' ;
2022-10-22 02:05:41 +00:00
///A handler that produces responses for a registered path.
///
///Implement this interface to handle other use-cases according to your app's needs.
2023-11-23 15:01:14 +00:00
abstract class PathHandler implements IPathHandler , PlatformPathHandlerEvents {
2023-11-17 22:28:11 +00:00
/// Constructs a [PathHandler].
///
/// See [PathHandler.fromPlatformCreationParams] for setting
/// parameters for a specific platform.
PathHandler ( { required String path } )
: this . fromPlatformCreationParams (
params: PlatformPathHandlerCreationParams ( path: path ) ) ;
/// Constructs a [PathHandler].
///
/// See [PathHandler.fromPlatformCreationParams] for setting parameters for
/// a specific platform.
PathHandler . fromPlatformCreationParams ( {
required PlatformPathHandlerCreationParams params ,
} ) : this . fromPlatform ( platform: PlatformPathHandler ( params ) ) ;
/// Constructs a [PathHandler] from a specific platform implementation.
PathHandler . fromPlatform ( { required this . platform } ) {
this . platform . eventHandler = this ;
}
/// Implementation of [PlatformPathHandler] for the current platform.
final PlatformPathHandler platform ;
2022-10-22 02:05:41 +00:00
///The suffix path to be handled.
///
///The path should start and end with a `"/"` and it shouldn't collide with a real web path.
2023-11-17 22:28:11 +00:00
String get path = > platform . path ;
2022-10-22 02:05:41 +00:00
///Handles the requested URL by returning the appropriate response.
///
///Returning a `null` value means that the handler decided not to handle this path.
///In this case, [WebViewAssetLoader] will try the next handler registered on this path or pass to [WebView] that will fall back to network to try to resolve the URL.
///
///However, if the handler wants to save unnecessary processing either by another handler or by falling back to network,
///in cases like a file cannot be found, it may return a `WebResourceResponse(data: null)`
///which is received as an HTTP response with status code `404` and no body.
Future < WebResourceResponse ? > handle ( String path ) async {
return null ;
}
2023-11-23 15:01:14 +00:00
@ override
Map < String , dynamic > toMap ( ) = > platform . toMap ( ) ;
@ override
Map < String , dynamic > toJson ( ) = > platform . toJson ( ) ;
2022-10-22 02:05:41 +00:00
}
///Handler class to open a file from assets directory in the application APK.
///
///Opens the requested file from the application's assets directory.
///
///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.
class AssetsPathHandler extends PathHandler {
2023-11-17 22:28:11 +00:00
/// Constructs a [AssetsPathHandler].
///
/// See [AssetsPathHandler.fromPlatformCreationParams] for setting
/// parameters for a specific platform.
AssetsPathHandler ( { required String path } )
: this . fromPlatformCreationParams (
params: PlatformAssetsPathHandlerCreationParams (
PlatformPathHandlerCreationParams ( path: path ) ) ) ;
/// Constructs a [AssetsPathHandler].
///
/// See [AssetsPathHandler.fromPlatformCreationParams] for setting parameters for
/// a specific platform.
AssetsPathHandler . fromPlatformCreationParams ( {
required PlatformAssetsPathHandlerCreationParams params ,
} ) : this . fromPlatform ( platform: PlatformAssetsPathHandler ( params ) ) ;
/// Constructs a [AssetsPathHandler] from a specific platform implementation.
AssetsPathHandler . fromPlatform ( { required this . platform } )
: super . fromPlatform ( platform: platform ) ;
/// Implementation of [PlatformAssetsPathHandler] for the current platform.
final PlatformAssetsPathHandler platform ;
2022-10-22 02:05:41 +00:00
}
///Handler class to open a file from resources directory in the application APK.
///
///Opens the requested file from application's resources directory.
///
///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.
class ResourcesPathHandler extends PathHandler {
2023-11-17 22:28:11 +00:00
/// Constructs a [ResourcesPathHandler].
///
/// See [ResourcesPathHandler.fromPlatformCreationParams] for setting
/// parameters for a specific platform.
ResourcesPathHandler ( { required String path } )
: this . fromPlatformCreationParams (
2023-11-18 00:17:45 +00:00
params: PlatformResourcesPathHandlerCreationParams (
PlatformPathHandlerCreationParams ( path: path ) ) ) ;
2023-11-17 22:28:11 +00:00
/// Constructs a [ResourcesPathHandler].
///
/// See [ResourcesPathHandler.fromPlatformCreationParams] for setting parameters for
/// a specific platform.
ResourcesPathHandler . fromPlatformCreationParams ( {
required PlatformResourcesPathHandlerCreationParams params ,
} ) : this . fromPlatform ( platform: PlatformResourcesPathHandler ( params ) ) ;
/// Constructs a [ResourcesPathHandler] from a specific platform implementation.
ResourcesPathHandler . fromPlatform ( { required this . platform } )
: super . fromPlatform ( platform: platform ) ;
/// Implementation of [PlatformResourcesPathHandler] for the current platform.
final PlatformResourcesPathHandler platform ;
2022-10-22 02:05:41 +00:00
}
///Handler class to open files from application internal storage.
///For more information about android storage please refer to
///[Android Developers Docs: Data and file storage overview](https://developer.android.com/guide/topics/data/data-storage).
///
///To avoid leaking user or app data to the web, make sure to choose [directory] carefully,
///and assume any file under this directory could be accessed by any web page subject to same-origin rules.
///
///Opens the requested file from the exposed data directory.
///
///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.
class InternalStoragePathHandler extends PathHandler {
2023-11-17 22:28:11 +00:00
/// Constructs a [InternalStoragePathHandler].
///
/// See [InternalStoragePathHandler.fromPlatformCreationParams] for setting
/// parameters for a specific platform.
InternalStoragePathHandler ( { required String path , required String directory } )
: this . fromPlatformCreationParams (
2023-11-18 00:17:45 +00:00
params: PlatformInternalStoragePathHandlerCreationParams (
PlatformPathHandlerCreationParams ( path: path ) ,
directory: directory ) ) ;
2023-11-17 22:28:11 +00:00
/// Constructs a [InternalStoragePathHandler].
///
/// See [InternalStoragePathHandler.fromPlatformCreationParams] for setting parameters for
/// a specific platform.
InternalStoragePathHandler . fromPlatformCreationParams ( {
required PlatformInternalStoragePathHandlerCreationParams params ,
} ) : this . fromPlatform ( platform: PlatformInternalStoragePathHandler ( params ) ) ;
2022-10-22 02:05:41 +00:00
2023-11-17 22:28:11 +00:00
/// Constructs a [InternalStoragePathHandler] from a specific platform implementation.
InternalStoragePathHandler . fromPlatform ( { required this . platform } )
: super . fromPlatform ( platform: platform ) ;
2022-10-22 02:05:41 +00:00
2023-11-17 22:28:11 +00:00
/// Implementation of [PlatformInternalStoragePathHandler] for the current platform.
final PlatformInternalStoragePathHandler platform ;
2022-10-22 02:05:41 +00:00
2023-11-17 22:28:11 +00:00
String get directory = > platform . directory ;
2022-10-22 02:05:41 +00:00
}