2021-02-04 20:54:09 +00:00
import ' dart:async ' ;
2023-11-17 22:28:11 +00:00
import ' package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart ' ;
2021-02-04 20:54:09 +00:00
2022-04-20 00:18:36 +00:00
///Class that manages Service Workers used by [WebView].
///
///**NOTE**: available on Android 24+.
///
///**Supported Platforms/Implementations**:
///- Android native WebView ([Official API - ServiceWorkerControllerCompat](https://developer.android.com/reference/androidx/webkit/ServiceWorkerControllerCompat))
class ServiceWorkerController {
2023-11-17 22:28:11 +00:00
/// Constructs a [ServiceWorkerController].
2022-05-02 01:37:02 +00:00
///
2023-11-17 22:28:11 +00:00
/// See [ServiceWorkerController.fromPlatformCreationParams] for setting
/// parameters for a specific platform.
ServiceWorkerController ( )
: this . fromPlatformCreationParams (
const PlatformServiceWorkerControllerCreationParams ( ) ,
) ;
2022-04-20 00:18:36 +00:00
2023-11-17 22:28:11 +00:00
/// Constructs a [ServiceWorkerController] from creation params for a specific
/// platform.
ServiceWorkerController . fromPlatformCreationParams (
PlatformServiceWorkerControllerCreationParams params ,
) : this . fromPlatform ( PlatformServiceWorkerController ( params ) ) ;
2022-04-22 14:57:29 +00:00
2023-11-17 22:28:11 +00:00
/// Constructs a [ServiceWorkerController] from a specific platform
/// implementation.
ServiceWorkerController . fromPlatform ( this . platform ) ;
2022-04-22 14:57:29 +00:00
2023-11-17 22:28:11 +00:00
/// Implementation of [PlatformWebViewServiceWorkerController] for the current platform.
final PlatformServiceWorkerController platform ;
static ServiceWorkerController ? _instance ;
2022-04-22 14:57:29 +00:00
2023-11-17 22:28:11 +00:00
///Gets the [ServiceWorkerController] shared instance.
static ServiceWorkerController instance ( ) {
if ( _instance = = null ) {
_instance = ServiceWorkerController ( ) ;
}
2022-04-20 00:18:36 +00:00
return _instance ! ;
}
2023-11-17 22:28:11 +00:00
ServiceWorkerClient ? get serviceWorkerClient = > platform . serviceWorkerClient ;
2022-04-20 00:18:36 +00:00
2023-11-17 22:28:11 +00:00
///Sets the service worker client
setServiceWorkerClient ( ServiceWorkerClient ? value ) = >
platform . setServiceWorkerClient ( value ) ;
2022-04-20 00:18:36 +00:00
///Gets whether Service Workers support content URL access.
///This method should only be called if [WebViewFeature.isFeatureSupported] returns `true` for [WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#getAllowContentAccess()
2023-11-17 22:28:11 +00:00
static Future < bool > getAllowContentAccess ( ) = >
PlatformServiceWorkerController . static ( ) . getAllowContentAccess ( ) ;
2022-04-20 00:18:36 +00:00
///Gets whether Service Workers support file access.
///This method should only be called if [WebViewFeature.isFeatureSupported] returns `true` for [WebViewFeature.SERVICE_WORKER_FILE_ACCESS].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#getAllowFileAccess()
2023-11-17 22:28:11 +00:00
static Future < bool > getAllowFileAccess ( ) = >
PlatformServiceWorkerController . static ( ) . getAllowFileAccess ( ) ;
2022-04-20 00:18:36 +00:00
///Gets whether Service Workers are prohibited from loading any resources from the network.
///This method should only be called if [WebViewFeature.isFeatureSupported] returns `true` for [WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#getBlockNetworkLoads()
2023-11-17 22:28:11 +00:00
static Future < bool > getBlockNetworkLoads ( ) = >
PlatformServiceWorkerController . static ( ) . getBlockNetworkLoads ( ) ;
2022-04-20 00:18:36 +00:00
///Gets the current setting for overriding the cache mode.
///This method should only be called if [WebViewFeature.isFeatureSupported] returns `true` for [WebViewFeature.SERVICE_WORKER_CACHE_MODE].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#getCacheMode()
2023-11-17 22:28:11 +00:00
static Future < CacheMode ? > getCacheMode ( ) = >
PlatformServiceWorkerController . static ( ) . getCacheMode ( ) ;
2022-04-20 00:18:36 +00:00
///Enables or disables content URL access from Service Workers.
///This method should only be called if [WebViewFeature.isFeatureSupported] returns `true` for [WebViewFeature.SERVICE_WORKER_CONTENT_ACCESS].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#setAllowContentAccess(boolean)
2023-11-17 22:28:11 +00:00
static Future < void > setAllowContentAccess ( bool allow ) = >
PlatformServiceWorkerController . static ( ) . setAllowContentAccess ( allow ) ;
2022-04-20 00:18:36 +00:00
///Enables or disables file access within Service Workers.
///This method should only be called if [WebViewFeature.isFeatureSupported] returns `true` for [WebViewFeature.SERVICE_WORKER_FILE_ACCESS].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#setAllowFileAccess(boolean)
2023-11-17 22:28:11 +00:00
static Future < void > setAllowFileAccess ( bool allow ) = >
PlatformServiceWorkerController . static ( ) . setAllowFileAccess ( allow ) ;
2022-04-20 00:18:36 +00:00
///Sets whether Service Workers should not load resources from the network.
///This method should only be called if [WebViewFeature.isFeatureSupported] returns `true` for [WebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#setBlockNetworkLoads(boolean)
2023-11-17 22:28:11 +00:00
static Future < void > setBlockNetworkLoads ( bool flag ) = >
PlatformServiceWorkerController . static ( ) . setBlockNetworkLoads ( flag ) ;
2022-04-20 00:18:36 +00:00
///Overrides the way the cache is used.
///This method should only be called if [WebViewFeature.isFeatureSupported] returns `true` for [WebViewFeature.SERVICE_WORKER_CACHE_MODE].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#setCacheMode(int)
2023-11-17 22:28:11 +00:00
static Future < void > setCacheMode ( CacheMode mode ) = >
PlatformServiceWorkerController . static ( ) . setCacheMode ( mode ) ;
2022-04-20 00:18:36 +00:00
}
2021-02-04 20:54:09 +00:00
///Class that represents an Android-specific class that manages Service Workers used by [WebView].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerControllerCompat
2022-04-20 00:18:36 +00:00
///
///Use [ServiceWorkerController] instead.
@ Deprecated ( " Use ServiceWorkerController instead " )
2021-02-04 20:54:09 +00:00
class AndroidServiceWorkerController {
static AndroidServiceWorkerController ? _instance ;
2022-04-22 14:24:41 +00:00
AndroidServiceWorkerClient ? _serviceWorkerClient ;
AndroidServiceWorkerClient ? get serviceWorkerClient = > _serviceWorkerClient ;
2021-02-04 20:54:09 +00:00
///Gets the [AndroidServiceWorkerController] shared instance.
static AndroidServiceWorkerController instance ( ) {
return ( _instance ! = null ) ? _instance ! : _init ( ) ;
}
static AndroidServiceWorkerController _init ( ) {
_instance = AndroidServiceWorkerController ( ) ;
return _instance ! ;
}
2023-11-17 22:28:11 +00:00
@ Deprecated ( " Use setServiceWorkerClient instead " )
set serviceWorkerClient ( AndroidServiceWorkerClient ? value ) {
setServiceWorkerClient ( value ) ;
}
2021-02-04 20:54:09 +00:00
2023-11-17 22:28:11 +00:00
///Sets the service worker client
setServiceWorkerClient ( AndroidServiceWorkerClient ? value ) async {
await ServiceWorkerController . instance ( ) . setServiceWorkerClient (
value ! = null
? ServiceWorkerClient (
shouldInterceptRequest: value . shouldInterceptRequest )
: null ) ;
_serviceWorkerClient = value ;
2021-02-04 20:54:09 +00:00
}
///Gets whether Service Workers support content URL access.
///This method should only be called if [AndroidWebViewFeature.isFeatureSupported] returns `true` for [AndroidWebViewFeature.SERVICE_WORKER_CONTENT_ACCESS].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#getAllowContentAccess()
static Future < bool > getAllowContentAccess ( ) async {
2023-11-17 22:28:11 +00:00
return await ServiceWorkerController . getAllowContentAccess ( ) ;
2021-02-04 20:54:09 +00:00
}
///Gets whether Service Workers support file access.
///This method should only be called if [AndroidWebViewFeature.isFeatureSupported] returns `true` for [AndroidWebViewFeature.SERVICE_WORKER_FILE_ACCESS].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#getAllowFileAccess()
static Future < bool > getAllowFileAccess ( ) async {
2023-11-17 22:28:11 +00:00
return await ServiceWorkerController . getAllowFileAccess ( ) ;
2021-02-04 20:54:09 +00:00
}
///Gets whether Service Workers are prohibited from loading any resources from the network.
///This method should only be called if [AndroidWebViewFeature.isFeatureSupported] returns `true` for [AndroidWebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#getBlockNetworkLoads()
static Future < bool > getBlockNetworkLoads ( ) async {
2023-11-17 22:28:11 +00:00
return await ServiceWorkerController . getBlockNetworkLoads ( ) ;
2021-02-04 20:54:09 +00:00
}
///Gets the current setting for overriding the cache mode.
///This method should only be called if [AndroidWebViewFeature.isFeatureSupported] returns `true` for [AndroidWebViewFeature.SERVICE_WORKER_CACHE_MODE].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#getCacheMode()
static Future < AndroidCacheMode ? > getCacheMode ( ) async {
2022-10-05 11:52:07 +00:00
return AndroidCacheMode . fromNativeValue (
2023-11-17 22:28:11 +00:00
( await ServiceWorkerController . getCacheMode ( ) ) ? . toNativeValue ( ) ) ;
2021-02-04 20:54:09 +00:00
}
///Enables or disables content URL access from Service Workers.
///This method should only be called if [AndroidWebViewFeature.isFeatureSupported] returns `true` for [AndroidWebViewFeature.SERVICE_WORKER_CONTENT_ACCESS].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#setAllowContentAccess(boolean)
static Future < void > setAllowContentAccess ( bool allow ) async {
2023-11-17 22:28:11 +00:00
await ServiceWorkerController . setAllowContentAccess ( allow ) ;
2021-02-04 20:54:09 +00:00
}
///Enables or disables file access within Service Workers.
///This method should only be called if [AndroidWebViewFeature.isFeatureSupported] returns `true` for [AndroidWebViewFeature.SERVICE_WORKER_FILE_ACCESS].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#setAllowFileAccess(boolean)
static Future < void > setAllowFileAccess ( bool allow ) async {
2023-11-17 22:28:11 +00:00
await ServiceWorkerController . setAllowFileAccess ( allow ) ;
2021-02-04 20:54:09 +00:00
}
///Sets whether Service Workers should not load resources from the network.
///This method should only be called if [AndroidWebViewFeature.isFeatureSupported] returns `true` for [AndroidWebViewFeature.SERVICE_WORKER_BLOCK_NETWORK_LOADS].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#setBlockNetworkLoads(boolean)
static Future < void > setBlockNetworkLoads ( bool flag ) async {
2023-11-17 22:28:11 +00:00
await ServiceWorkerController . setBlockNetworkLoads ( flag ) ;
2021-02-04 20:54:09 +00:00
}
///Overrides the way the cache is used.
///This method should only be called if [AndroidWebViewFeature.isFeatureSupported] returns `true` for [AndroidWebViewFeature.SERVICE_WORKER_CACHE_MODE].
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerWebSettingsCompat#setCacheMode(int)
static Future < void > setCacheMode ( AndroidCacheMode mode ) async {
2023-11-17 22:28:11 +00:00
await ServiceWorkerController . setCacheMode (
CacheMode . fromNativeValue ( mode . toNativeValue ( ) ) ! ) ;
2021-02-04 20:54:09 +00:00
}
}
///Class that represents an Android-specific class for clients to capture Service Worker related callbacks.
///
///**NOTE**: available on Android 24+.
///
///**Official Android API**: https://developer.android.com/reference/androidx/webkit/ServiceWorkerClientCompat
2022-04-20 00:18:36 +00:00
///Use [ServiceWorkerClient] instead.
@ Deprecated ( " Use ServiceWorkerClient instead " )
2021-02-04 20:54:09 +00:00
class AndroidServiceWorkerClient {
///Notify the host application of a resource request and allow the application to return the data.
///If the return value is `null`, the Service Worker will continue to load the resource as usual.
///Otherwise, the return response and data will be used.
///
///This method is called only if [AndroidWebViewFeature.SERVICE_WORKER_SHOULD_INTERCEPT_REQUEST] is supported.
///You can check whether that flag is supported using [AndroidWebViewFeature.isFeatureSupported].
///
///[request] represents an object containing the details of the request.
///
///**NOTE**: available on Android 24+.
final Future < WebResourceResponse ? > Function ( WebResourceRequest request ) ?
2021-02-22 22:54:09 +00:00
shouldInterceptRequest ;
2021-02-04 20:54:09 +00:00
2021-02-22 22:54:09 +00:00
AndroidServiceWorkerClient ( { this . shouldInterceptRequest } ) ;
}