2019-10-26 02:42:50 +00:00
import ' dart:async ' ;
import ' dart:collection ' ;
2019-11-04 00:39:23 +00:00
import ' dart:io ' ;
2019-10-26 02:42:50 +00:00
2019-11-10 13:11:30 +00:00
import ' package:flutter/foundation.dart ' ;
2019-10-26 02:42:50 +00:00
import ' package:flutter/services.dart ' ;
2020-05-21 01:34:39 +00:00
import ' context_menu.dart ' ;
2020-05-11 00:48:41 +00:00
import ' in_app_webview_controller.dart ' ;
2019-11-29 15:59:18 +00:00
import ' webview_options.dart ' ;
2019-10-26 02:42:50 +00:00
import ' types.dart ' ;
///This class uses the native WebView of the platform.
2020-05-11 00:48:41 +00:00
///The [webViewController] field can be used to access the [InAppWebViewController] API.
2019-10-26 02:42:50 +00:00
class InAppBrowser {
2020-05-21 01:34:39 +00:00
///Browser's UUID
2019-10-26 02:42:50 +00:00
String uuid ;
2020-05-21 01:34:39 +00:00
///Context menu used by the browser
ContextMenu contextMenu ;
2019-12-01 11:55:06 +00:00
Map < String , JavaScriptHandlerCallback > javaScriptHandlersMap =
HashMap < String , JavaScriptHandlerCallback > ( ) ;
2019-10-26 02:42:50 +00:00
bool _isOpened = false ;
2019-12-18 20:34:40 +00:00
MethodChannel _channel ;
2020-05-29 17:56:03 +00:00
static const MethodChannel _sharedChannel =
const MethodChannel ( ' com.pichillilorenzo/flutter_inappbrowser ' ) ;
2019-12-01 11:55:06 +00:00
2020-05-11 00:48:41 +00:00
/// WebView Controller that can be used to access the [InAppWebViewController] API.
2019-10-26 02:42:50 +00:00
InAppWebViewController webViewController ;
///
2019-12-01 11:55:06 +00:00
InAppBrowser ( ) {
2019-10-26 02:42:50 +00:00
uuid = uuidGenerator . v4 ( ) ;
2019-12-18 20:34:40 +00:00
this . _channel =
MethodChannel ( ' com.pichillilorenzo/flutter_inappbrowser_ $ uuid ' ) ;
this . _channel . setMethodCallHandler ( handleMethod ) ;
2019-10-26 02:42:50 +00:00
_isOpened = false ;
2020-05-29 17:56:03 +00:00
webViewController =
new InAppWebViewController . fromInAppBrowser ( uuid , this . _channel , this ) ;
2019-10-26 02:42:50 +00:00
}
Future < dynamic > handleMethod ( MethodCall call ) async {
2019-12-01 11:55:06 +00:00
switch ( call . method ) {
2019-10-26 02:42:50 +00:00
case " onBrowserCreated " :
this . _isOpened = true ;
onBrowserCreated ( ) ;
break ;
case " onExit " :
this . _isOpened = false ;
onExit ( ) ;
break ;
default :
return webViewController . handleMethod ( call ) ;
}
}
///Opens an [url] in a new [InAppBrowser] instance.
///
2019-11-10 13:11:30 +00:00
///[url]: The [url] to load. Call `encodeUriComponent()` on this if the [url] contains Unicode characters. The default value is `about:blank`.
///
///[headers]: The additional headers to be used in the HTTP request for this URL, specified as a map from name to value.
///
///[options]: Options for the [InAppBrowser].
2019-12-18 20:34:40 +00:00
Future < void > openUrl (
{ @ required String url ,
2019-12-01 11:55:06 +00:00
Map < String , String > headers = const { } ,
InAppBrowserClassOptions options } ) async {
2019-10-26 02:42:50 +00:00
assert ( url ! = null & & url . isNotEmpty ) ;
this . throwIsAlreadyOpened ( message: ' Cannot open $ url ! ' ) ;
2019-10-27 03:35:05 +00:00
2019-10-26 02:42:50 +00:00
Map < String , dynamic > args = < String , dynamic > { } ;
args . putIfAbsent ( ' uuid ' , ( ) = > uuid ) ;
args . putIfAbsent ( ' url ' , ( ) = > url ) ;
args . putIfAbsent ( ' headers ' , ( ) = > headers ) ;
2020-05-11 00:48:41 +00:00
args . putIfAbsent ( ' options ' , ( ) = > options ? . toMap ( ) ? ? { } ) ;
2020-05-21 01:34:39 +00:00
args . putIfAbsent ( ' contextMenu ' , ( ) = > contextMenu ? . toMap ( ) ? ? { } ) ;
2019-12-18 20:34:40 +00:00
await _sharedChannel . invokeMethod ( ' openUrl ' , args ) ;
2019-10-26 02:42:50 +00:00
}
2019-12-18 20:34:40 +00:00
///Opens the given [assetFilePath] file in a new [InAppBrowser] instance. The other arguments are the same of [InAppBrowser.openUrl].
2019-10-26 02:42:50 +00:00
///
///To be able to load your local files (assets, js, css, etc.), you need to add them in the `assets` section of the `pubspec.yaml` file, otherwise they cannot be found!
///
///Example of a `pubspec.yaml` file:
///```yaml
///...
///
///# The following section is specific to Flutter.
///flutter:
///
/// # The following line ensures that the Material Icons font is
/// # included with your application, so that you can use the icons in
/// # the material Icons class.
/// uses-material-design: true
///
/// assets:
2019-11-16 11:41:30 +00:00
/// - assets/index.html
2019-10-26 02:42:50 +00:00
/// - assets/css/
/// - assets/images/
///
///...
///```
///Example of a `main.dart` file:
///```dart
///...
2019-11-16 11:41:30 +00:00
///inAppBrowser.openFile("assets/index.html");
2019-10-26 02:42:50 +00:00
///...
///```
2019-11-10 13:11:30 +00:00
///
///[headers]: The additional headers to be used in the HTTP request for this URL, specified as a map from name to value.
///
///[options]: Options for the [InAppBrowser].
2019-12-01 11:55:06 +00:00
Future < void > openFile (
{ @ required String assetFilePath ,
Map < String , String > headers = const { } ,
InAppBrowserClassOptions options } ) async {
2019-10-26 02:42:50 +00:00
assert ( assetFilePath ! = null & & assetFilePath . isNotEmpty ) ;
this . throwIsAlreadyOpened ( message: ' Cannot open $ assetFilePath ! ' ) ;
2019-10-27 03:35:05 +00:00
2019-10-26 02:42:50 +00:00
Map < String , dynamic > args = < String , dynamic > { } ;
args . putIfAbsent ( ' uuid ' , ( ) = > uuid ) ;
args . putIfAbsent ( ' url ' , ( ) = > assetFilePath ) ;
args . putIfAbsent ( ' headers ' , ( ) = > headers ) ;
2020-05-11 00:48:41 +00:00
args . putIfAbsent ( ' options ' , ( ) = > options ? . toMap ( ) ? ? { } ) ;
2020-05-21 01:34:39 +00:00
args . putIfAbsent ( ' contextMenu ' , ( ) = > contextMenu ? . toMap ( ) ? ? { } ) ;
2019-12-18 20:34:40 +00:00
await _sharedChannel . invokeMethod ( ' openFile ' , args ) ;
2019-10-26 02:42:50 +00:00
}
///Opens a new [InAppBrowser] instance with [data] as a content, using [baseUrl] as the base URL for it.
2019-11-10 13:11:30 +00:00
///
2019-12-02 23:07:29 +00:00
///The [mimeType] parameter specifies the format of the data. The default value is `"text/html"`.
2019-11-10 13:11:30 +00:00
///
2019-12-02 23:07:29 +00:00
///The [encoding] parameter specifies the encoding of the data. The default value is `"utf8"`.
///
2019-12-16 22:58:10 +00:00
///The [androidHistoryUrl] parameter is the URL to use as the history entry. The default value is `about:blank`. If non-null, this must be a valid URL. This parameter is used only on Android.
2019-11-10 13:11:30 +00:00
///
///The [options] parameter specifies the options for the [InAppBrowser].
2019-12-01 11:55:06 +00:00
Future < void > openData (
{ @ required String data ,
String mimeType = " text/html " ,
String encoding = " utf8 " ,
String baseUrl = " about:blank " ,
2019-12-16 22:58:10 +00:00
String androidHistoryUrl = " about:blank " ,
2019-12-01 11:55:06 +00:00
InAppBrowserClassOptions options } ) async {
2019-10-26 02:42:50 +00:00
assert ( data ! = null ) ;
2019-10-27 03:35:05 +00:00
2019-10-26 02:42:50 +00:00
Map < String , dynamic > args = < String , dynamic > { } ;
args . putIfAbsent ( ' uuid ' , ( ) = > uuid ) ;
2020-05-11 00:48:41 +00:00
args . putIfAbsent ( ' options ' , ( ) = > options ? . toMap ( ) ? ? { } ) ;
2019-10-26 02:42:50 +00:00
args . putIfAbsent ( ' data ' , ( ) = > data ) ;
args . putIfAbsent ( ' mimeType ' , ( ) = > mimeType ) ;
args . putIfAbsent ( ' encoding ' , ( ) = > encoding ) ;
args . putIfAbsent ( ' baseUrl ' , ( ) = > baseUrl ) ;
2019-12-18 20:34:40 +00:00
args . putIfAbsent ( ' historyUrl ' , ( ) = > androidHistoryUrl ) ;
2020-05-21 01:34:39 +00:00
args . putIfAbsent ( ' contextMenu ' , ( ) = > contextMenu ? . toMap ( ) ? ? { } ) ;
2019-12-18 20:34:40 +00:00
await _sharedChannel . invokeMethod ( ' openData ' , args ) ;
2019-10-26 02:42:50 +00:00
}
///This is a static method that opens an [url] in the system browser. You wont be able to use the [InAppBrowser] methods here!
2019-11-10 13:11:30 +00:00
static Future < void > openWithSystemBrowser ( { @ required String url } ) async {
2019-10-26 02:42:50 +00:00
assert ( url ! = null & & url . isNotEmpty ) ;
Map < String , dynamic > args = < String , dynamic > { } ;
args . putIfAbsent ( ' url ' , ( ) = > url ) ;
2019-12-18 20:34:40 +00:00
return await _sharedChannel . invokeMethod ( ' openWithSystemBrowser ' , args ) ;
2019-10-26 02:42:50 +00:00
}
///Displays an [InAppBrowser] window that was opened hidden. Calling this has no effect if the [InAppBrowser] was already visible.
Future < void > show ( ) async {
this . throwIsNotOpened ( ) ;
Map < String , dynamic > args = < String , dynamic > { } ;
args . putIfAbsent ( ' uuid ' , ( ) = > uuid ) ;
2019-12-18 20:34:40 +00:00
await _channel . invokeMethod ( ' show ' , args ) ;
2019-10-26 02:42:50 +00:00
}
///Hides the [InAppBrowser] window. Calling this has no effect if the [InAppBrowser] was already hidden.
Future < void > hide ( ) async {
this . throwIsNotOpened ( ) ;
Map < String , dynamic > args = < String , dynamic > { } ;
2019-12-18 20:34:40 +00:00
await _channel . invokeMethod ( ' hide ' , args ) ;
2019-10-26 02:42:50 +00:00
}
///Closes the [InAppBrowser] window.
Future < void > close ( ) async {
this . throwIsNotOpened ( ) ;
Map < String , dynamic > args = < String , dynamic > { } ;
2019-12-18 20:34:40 +00:00
await _channel . invokeMethod ( ' close ' , args ) ;
2019-10-26 02:42:50 +00:00
}
///Check if the Web View of the [InAppBrowser] instance is hidden.
Future < bool > isHidden ( ) async {
this . throwIsNotOpened ( ) ;
Map < String , dynamic > args = < String , dynamic > { } ;
2019-12-18 20:34:40 +00:00
return await _channel . invokeMethod ( ' isHidden ' , args ) ;
2019-10-26 02:42:50 +00:00
}
///Sets the [InAppBrowser] options with the new [options] and evaluates them.
2019-11-10 13:11:30 +00:00
Future < void > setOptions ( { @ required InAppBrowserClassOptions options } ) async {
2019-10-26 02:42:50 +00:00
this . throwIsNotOpened ( ) ;
2019-10-27 03:35:05 +00:00
2019-10-26 02:42:50 +00:00
Map < String , dynamic > args = < String , dynamic > { } ;
2020-05-11 00:48:41 +00:00
args . putIfAbsent ( ' options ' , ( ) = > options ? . toMap ( ) ? ? { } ) ;
2019-12-18 20:34:40 +00:00
await _channel . invokeMethod ( ' setOptions ' , args ) ;
2019-10-26 02:42:50 +00:00
}
2019-10-27 03:35:05 +00:00
///Gets the current [InAppBrowser] options as a `Map`. Returns `null` if the options are not setted yet.
2019-11-04 00:39:23 +00:00
Future < InAppBrowserClassOptions > getOptions ( ) async {
2019-10-26 02:42:50 +00:00
this . throwIsNotOpened ( ) ;
Map < String , dynamic > args = < String , dynamic > { } ;
2019-11-04 00:39:23 +00:00
2019-12-01 11:55:06 +00:00
InAppBrowserClassOptions inAppBrowserClassOptions =
InAppBrowserClassOptions ( ) ;
Map < dynamic , dynamic > options =
2019-12-18 20:34:40 +00:00
await _channel . invokeMethod ( ' getOptions ' , args ) ;
2019-11-04 00:39:23 +00:00
if ( options ! = null ) {
2019-11-02 18:58:01 +00:00
options = options . cast < String , dynamic > ( ) ;
2019-12-16 22:58:10 +00:00
inAppBrowserClassOptions . crossPlatform =
2019-12-01 11:55:06 +00:00
InAppBrowserOptions . fromMap ( options ) ;
2020-05-29 17:56:03 +00:00
inAppBrowserClassOptions . inAppWebViewGroupOptions =
InAppWebViewGroupOptions ( ) ;
2020-05-11 00:48:41 +00:00
inAppBrowserClassOptions . inAppWebViewGroupOptions . crossPlatform =
2019-12-01 11:55:06 +00:00
InAppWebViewOptions . fromMap ( options ) ;
2019-11-04 00:39:23 +00:00
if ( Platform . isAndroid ) {
2019-12-16 22:58:10 +00:00
inAppBrowserClassOptions . android =
2019-12-01 11:55:06 +00:00
AndroidInAppBrowserOptions . fromMap ( options ) ;
2020-05-29 17:56:03 +00:00
inAppBrowserClassOptions . inAppWebViewGroupOptions . android =
2019-12-01 11:55:06 +00:00
AndroidInAppWebViewOptions . fromMap ( options ) ;
} else if ( Platform . isIOS ) {
2020-05-29 17:56:03 +00:00
inAppBrowserClassOptions . ios = IOSInAppBrowserOptions . fromMap ( options ) ;
inAppBrowserClassOptions . inAppWebViewGroupOptions . ios =
IOSInAppWebViewOptions . fromMap ( options ) ;
2019-11-04 00:39:23 +00:00
}
}
return inAppBrowserClassOptions ;
2019-10-26 02:42:50 +00:00
}
///Returns `true` if the [InAppBrowser] instance is opened, otherwise `false`.
bool isOpened ( ) {
return this . _isOpened ;
}
2019-11-25 22:04:17 +00:00
///Event fired when the [InAppBrowser] is created.
2019-12-01 11:55:06 +00:00
void onBrowserCreated ( ) { }
2019-10-26 02:42:50 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when the [InAppBrowser] window is closed.
2019-12-01 11:55:06 +00:00
void onExit ( ) { }
2019-11-25 22:04:17 +00:00
///Event fired when the [InAppBrowser] starts to load an [url].
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#onPageStarted(android.webkit.WebView,%20java.lang.String,%20android.graphics.Bitmap)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455621-webview
2019-12-01 11:55:06 +00:00
void onLoadStart ( String url ) { }
2019-10-26 02:42:50 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when the [InAppBrowser] finishes loading an [url].
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#onPageFinished(android.webkit.WebView,%20java.lang.String)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455629-webview
2019-12-01 11:55:06 +00:00
void onLoadStop ( String url ) { }
2019-10-26 02:42:50 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when the [InAppBrowser] encounters an error loading an [url].
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#onReceivedError(android.webkit.WebView,%20int,%20java.lang.String,%20java.lang.String)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455623-webview
2019-12-01 11:55:06 +00:00
void onLoadError ( String url , int code , String message ) { }
2019-10-26 02:42:50 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when the [InAppBrowser] main page receives an HTTP error.
2019-11-21 01:19:43 +00:00
///
///[url] represents the url of the main page that received the HTTP error.
///
///[statusCode] represents the status code of the response. HTTP errors have status codes >= 400.
///
///[description] represents the description of the HTTP error. On iOS, it is always an empty string.
///
///**NOTE**: available on Android 23+.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#onReceivedHttpError(android.webkit.WebView,%20android.webkit.WebResourceRequest,%20android.webkit.WebResourceResponse)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455643-webview
2019-12-01 11:55:06 +00:00
void onLoadHttpError ( String url , int statusCode , String description ) { }
2019-11-21 01:19:43 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when the current [progress] (range 0-100) of loading a page is changed.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebChromeClient#onProgressChanged(android.webkit.WebView,%20int)
2019-12-01 11:55:06 +00:00
void onProgressChanged ( int progress ) { }
2019-10-26 02:42:50 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when the [InAppBrowser] webview receives a [ConsoleMessage].
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebChromeClient#onConsoleMessage(android.webkit.ConsoleMessage)
2019-12-01 11:55:06 +00:00
void onConsoleMessage ( ConsoleMessage consoleMessage ) { }
2019-10-26 02:42:50 +00:00
2019-12-09 23:29:09 +00:00
///Give the host application a chance to take control when a URL is about to be loaded in the current WebView. This event is not called on the initial load of the WebView.
///
///Note that on Android there isn't any way to load an URL for a frame that is not the main frame, so if the request is not for the main frame, the navigation is allowed by default.
///However, if you want to cancel requests for subframes, you can use the [AndroidInAppWebViewOptions.regexToCancelSubFramesLoading] option
///to write a Regular Expression that, if the url request of a subframe matches, then the request of that subframe is canceled.
///
///Also, on Android, this method is not called for POST requests.
///
///[shouldOverrideUrlLoadingRequest] represents the navigation request.
2019-10-26 02:42:50 +00:00
///
2019-11-08 18:12:21 +00:00
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewOptions.useShouldOverrideUrlLoading] option to `true`.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView,%20java.lang.String)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455641-webview
2019-12-09 23:29:09 +00:00
// ignore: missing_return
2020-05-29 17:56:03 +00:00
Future < ShouldOverrideUrlLoadingAction > shouldOverrideUrlLoading (
ShouldOverrideUrlLoadingRequest shouldOverrideUrlLoadingRequest ) { }
2019-10-26 02:42:50 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when the [InAppBrowser] webview loads a resource.
2019-10-26 02:42:50 +00:00
///
2019-11-08 18:12:21 +00:00
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewOptions.useOnLoadResource] and [InAppWebViewOptions.javaScriptEnabled] options to `true`.
2019-12-01 11:55:06 +00:00
void onLoadResource ( LoadedResource resource ) { }
2019-10-26 02:42:50 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when the [InAppBrowser] webview scrolls.
2019-10-29 02:03:50 +00:00
///
2019-10-26 02:42:50 +00:00
///[x] represents the current horizontal scroll origin in pixels.
2019-10-29 02:03:50 +00:00
///
2019-10-26 02:42:50 +00:00
///[y] represents the current vertical scroll origin in pixels.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebView#onScrollChanged(int,%20int,%20int,%20int)
///**Official iOS API**: https://developer.apple.com/documentation/uikit/uiscrollviewdelegate/1619392-scrollviewdidscroll
2019-12-01 11:55:06 +00:00
void onScrollChanged ( int x , int y ) { }
2019-10-26 02:42:50 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when [InAppBrowser] recognizes and starts a downloadable file.
2019-10-29 02:03:50 +00:00
///
2019-10-26 02:42:50 +00:00
///[url] represents the url of the file.
2019-11-08 18:12:21 +00:00
///
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewOptions.useOnDownloadStart] option to `true`.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebView#setDownloadListener(android.webkit.DownloadListener)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455643-webview
2019-12-01 11:55:06 +00:00
void onDownloadStart ( String url ) { }
2019-10-26 02:42:50 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when the [InAppBrowser] webview finds the `custom-scheme` while loading a resource. Here you can handle the url request and return a [CustomSchemeResponse] to load a specific resource encoded to `base64`.
2019-10-29 02:03:50 +00:00
///
2019-10-26 02:42:50 +00:00
///[scheme] represents the scheme of the url.
2019-10-29 02:03:50 +00:00
///
2019-10-26 02:42:50 +00:00
///[url] represents the url of the request.
2020-05-28 23:03:45 +00:00
///
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wkurlschemehandler
2019-11-10 13:11:30 +00:00
// ignore: missing_return
2019-12-01 11:55:06 +00:00
Future < CustomSchemeResponse > onLoadResourceCustomScheme (
String scheme , String url ) { }
2019-10-26 02:42:50 +00:00
2019-12-09 23:29:09 +00:00
///Event fired when the [InAppBrowser] webview requests the host application to create a new window,
///for example when trying to open a link with `target="_blank"` or when `window.open()` is called by JavaScript side.
2019-10-29 02:03:50 +00:00
///
2019-12-16 22:58:10 +00:00
///[onCreateWindowRequest] represents the request.
2019-11-08 18:12:21 +00:00
///
2019-12-09 23:29:09 +00:00
///**NOTE**: on Android you need to set [AndroidInAppWebViewOptions.supportMultipleWindows] option to `true`.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebChromeClient#onCreateWindow(android.webkit.WebView,%20boolean,%20boolean,%20android.os.Message)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wkuidelegate/1536907-webview
2019-12-16 22:58:10 +00:00
void onCreateWindow ( OnCreateWindowRequest onCreateWindowRequest ) { }
2019-10-29 11:16:31 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when javascript calls the `alert()` method to display an alert dialog.
2019-10-29 02:03:50 +00:00
///If [JsAlertResponse.handledByClient] is `true`, the webview will assume that the client will handle the dialog.
///
///[message] represents the message to be displayed in the alert dialog.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebChromeClient#onJsAlert(android.webkit.WebView,%20java.lang.String,%20java.lang.String,%20android.webkit.JsResult)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wkuidelegate/1537406-webview
2019-11-10 13:11:30 +00:00
// ignore: missing_return
2019-12-01 11:55:06 +00:00
Future < JsAlertResponse > onJsAlert ( String message ) { }
2019-10-29 02:03:50 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when javascript calls the `confirm()` method to display a confirm dialog.
2019-10-29 02:03:50 +00:00
///If [JsConfirmResponse.handledByClient] is `true`, the webview will assume that the client will handle the dialog.
///
///[message] represents the message to be displayed in the alert dialog.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebChromeClient#onJsConfirm(android.webkit.WebView,%20java.lang.String,%20java.lang.String,%20android.webkit.JsResult)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wkuidelegate/1536489-webview
2019-11-10 13:11:30 +00:00
// ignore: missing_return
2019-12-01 11:55:06 +00:00
Future < JsConfirmResponse > onJsConfirm ( String message ) { }
2019-10-29 02:03:50 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when javascript calls the `prompt()` method to display a prompt dialog.
2019-10-29 02:03:50 +00:00
///If [JsPromptResponse.handledByClient] is `true`, the webview will assume that the client will handle the dialog.
///
///[message] represents the message to be displayed in the alert dialog.
///[defaultValue] represents the default value displayed in the prompt dialog.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebChromeClient#onJsPrompt(android.webkit.WebView,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20android.webkit.JsPromptResult)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wkuidelegate/1538086-webview
2019-11-10 13:11:30 +00:00
// ignore: missing_return
2019-12-01 11:55:06 +00:00
Future < JsPromptResponse > onJsPrompt ( String message , String defaultValue ) { }
2019-10-29 02:03:50 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when the WebView received an HTTP authentication request. The default behavior is to cancel the request.
2019-10-29 16:51:55 +00:00
///
2019-11-08 18:12:21 +00:00
///[challenge] contains data about host, port, protocol, realm, etc. as specified in the [HttpAuthChallenge].
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#onReceivedHttpAuthRequest(android.webkit.WebView,%20android.webkit.HttpAuthHandler,%20java.lang.String,%20java.lang.String)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455638-webview
2019-11-10 13:11:30 +00:00
// ignore: missing_return
2019-12-01 11:55:06 +00:00
Future < HttpAuthResponse > onReceivedHttpAuthRequest (
HttpAuthChallenge challenge ) { }
2019-10-31 22:09:54 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when the WebView need to perform server trust authentication (certificate validation).
2019-11-09 22:35:18 +00:00
///The host application must return either [ServerTrustAuthResponse] instance with [ServerTrustAuthResponseAction.CANCEL] or [ServerTrustAuthResponseAction.PROCEED].
2019-10-31 22:09:54 +00:00
///
2019-11-08 18:12:21 +00:00
///[challenge] contains data about host, port, protocol, realm, etc. as specified in the [ServerTrustChallenge].
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#onReceivedSslError(android.webkit.WebView,%20android.webkit.SslErrorHandler,%20android.net.http.SslError)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455638-webview
2019-11-10 13:11:30 +00:00
// ignore: missing_return
2019-12-01 11:55:06 +00:00
Future < ServerTrustAuthResponse > onReceivedServerTrustAuthRequest (
ServerTrustChallenge challenge ) { }
2019-10-31 22:09:54 +00:00
2019-12-01 11:55:06 +00:00
///Notify the host application to handle an SSL client certificate request.
2019-11-08 18:12:21 +00:00
///Webview stores the response in memory (for the life of the application) if [ClientCertResponseAction.PROCEED] or [ClientCertResponseAction.CANCEL]
///is called and does not call [onReceivedClientCertRequest] again for the same host and port pair.
///Note that, multiple layers in chromium network stack might be caching the responses.
2019-10-31 22:09:54 +00:00
///
2019-11-08 18:12:21 +00:00
///[challenge] contains data about host, port, protocol, realm, etc. as specified in the [ClientCertChallenge].
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#onReceivedClientCertRequest(android.webkit.WebView,%20android.webkit.ClientCertRequest)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455638-webview
2019-11-10 13:11:30 +00:00
// ignore: missing_return
2019-12-01 11:55:06 +00:00
Future < ClientCertResponse > onReceivedClientCertRequest (
ClientCertChallenge challenge ) { }
2019-10-29 16:51:55 +00:00
2019-11-02 03:16:47 +00:00
///Event fired as find-on-page operations progress.
///The listener may be notified multiple times while the operation is underway, and the numberOfMatches value should not be considered final unless [isDoneCounting] is true.
///
///[activeMatchOrdinal] represents the zero-based ordinal of the currently selected match.
///
///[numberOfMatches] represents how many matches have been found.
///
///[isDoneCounting] whether the find operation has actually completed.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebView#setFindListener(android.webkit.WebView.FindListener)
2019-12-01 11:55:06 +00:00
void onFindResultReceived (
int activeMatchOrdinal , int numberOfMatches , bool isDoneCounting ) { }
2019-11-02 03:16:47 +00:00
2019-11-08 18:12:21 +00:00
///Event fired when an `XMLHttpRequest` is sent to a server.
///It gives the host application a chance to take control over the request before sending it.
2019-11-05 23:23:24 +00:00
///
2019-11-08 18:12:21 +00:00
///[ajaxRequest] represents the `XMLHttpRequest`.
///
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewOptions.useShouldInterceptAjaxRequest] option to `true`.
2019-11-10 13:11:30 +00:00
// ignore: missing_return
2019-12-01 11:55:06 +00:00
Future < AjaxRequest > shouldInterceptAjaxRequest ( AjaxRequest ajaxRequest ) { }
2019-11-05 23:23:24 +00:00
2019-11-08 18:12:21 +00:00
///Event fired whenever the `readyState` attribute of an `XMLHttpRequest` changes.
///It gives the host application a chance to abort the request.
///
///[ajaxRequest] represents the [XMLHttpRequest].
2019-11-05 23:23:24 +00:00
///
2019-11-08 18:12:21 +00:00
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewOptions.useShouldInterceptAjaxRequest] option to `true`.
2019-11-10 13:11:30 +00:00
// ignore: missing_return
2019-12-01 11:55:06 +00:00
Future < AjaxRequestAction > onAjaxReadyStateChange ( AjaxRequest ajaxRequest ) { }
2019-11-05 23:23:24 +00:00
2019-11-08 18:12:21 +00:00
///Event fired as an `XMLHttpRequest` progress.
///It gives the host application a chance to abort the request.
2019-11-05 23:23:24 +00:00
///
2019-11-08 18:12:21 +00:00
///[ajaxRequest] represents the [XMLHttpRequest].
///
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewOptions.useShouldInterceptAjaxRequest] option to `true`.
2019-11-10 13:11:30 +00:00
// ignore: missing_return
2019-12-01 11:55:06 +00:00
Future < AjaxRequestAction > onAjaxProgress ( AjaxRequest ajaxRequest ) { }
2019-11-05 23:23:24 +00:00
2019-11-25 22:04:17 +00:00
///Event fired when a request is sent to a server through [Fetch API](https://developer.mozilla.org/it/docs/Web/API/Fetch_API).
2019-11-08 18:12:21 +00:00
///It gives the host application a chance to take control over the request before sending it.
///
///[fetchRequest] represents a resource request.
2019-11-05 23:23:24 +00:00
///
2019-11-08 18:12:21 +00:00
///**NOTE**: In order to be able to listen this event, you need to set [InAppWebViewOptions.useShouldInterceptFetchRequest] option to `true`.
2019-11-10 13:11:30 +00:00
// ignore: missing_return
2019-12-01 11:55:06 +00:00
Future < FetchRequest > shouldInterceptFetchRequest ( FetchRequest fetchRequest ) { }
2019-11-05 23:23:24 +00:00
2019-12-16 22:58:10 +00:00
///Event fired when the host application updates its visited links database.
///This event is also fired when the navigation state of the [InAppWebView] changes through the usage of
///javascript **[History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API)** functions (`pushState()`, `replaceState()`) and `onpopstate` event
///or, also, when the javascript `window.location` changes without reloading the webview (for example appending or modifying an hash to the url).
2019-11-05 23:23:24 +00:00
///
2019-12-16 22:58:10 +00:00
///[url] represents the url being visited.
2019-11-05 23:23:24 +00:00
///
2019-12-16 22:58:10 +00:00
///[androidIsReload] indicates if this url is being reloaded. Available only on Android.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#doUpdateVisitedHistory(android.webkit.WebView,%20java.lang.String,%20boolean)
2019-12-16 22:58:10 +00:00
void onUpdateVisitedHistory ( String url , bool androidIsReload ) { }
///Event fired when `window.print()` is called from JavaScript side.
///
///[url] represents the url on which is called.
///
///**NOTE**: available on Android 21+.
void onPrint ( String url ) { }
2020-05-09 02:36:07 +00:00
///Event fired when an HTML element of the webview has been clicked and held.
///
///[hitTestResult] represents the hit result for hitting an HTML elements.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/view/View#setOnLongClickListener(android.view.View.OnLongClickListener)
///**Official iOS API**: https://developer.apple.com/documentation/uikit/uilongpressgesturerecognizer
2020-05-21 01:34:39 +00:00
void onLongPressHitTestResult ( InAppWebViewHitTestResult hitTestResult ) { }
2020-05-09 02:36:07 +00:00
2020-05-23 17:33:54 +00:00
///Event fired when the current page has entered full screen mode.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebChromeClient#onShowCustomView(android.view.View,%20android.webkit.WebChromeClient.CustomViewCallback)
///**Official iOS API**: https://developer.apple.com/documentation/uikit/uiwindow/1621621-didbecomevisiblenotification
2020-05-23 17:33:54 +00:00
void onEnterFullscreen ( ) { }
///Event fired when the current page has exited full screen mode.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebChromeClient#onHideCustomView()
///**Official iOS API**: https://developer.apple.com/documentation/uikit/uiwindow/1621617-didbecomehiddennotification
2020-05-23 17:33:54 +00:00
void onExitFullscreen ( ) { }
2020-05-28 23:03:45 +00:00
///Called when the web view begins to receive web content.
///
///This event occurs early in the document loading process, and as such
///you should expect that linked resources (for example, CSS and images) may not be available.
///
///[url] represents the URL corresponding to the page navigation that triggered this callback.
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#onPageCommitVisible(android.webkit.WebView,%20java.lang.String)
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455635-webview
void onPageCommitVisible ( String url ) { }
2019-12-16 22:58:10 +00:00
///Event fired when the WebView notifies that a loading URL has been flagged by Safe Browsing.
///The default behavior is to show an interstitial to the user, with the reporting checkbox visible.
///
///[url] represents the url of the request.
///
///[threatType] represents the reason the resource was caught by Safe Browsing, corresponding to a [SafeBrowsingThreat].
///
///**NOTE**: available only on Android 27+.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#onSafeBrowsingHit(android.webkit.WebView,%20android.webkit.WebResourceRequest,%20int,%20android.webkit.SafeBrowsingResponse)
2019-12-16 22:58:10 +00:00
// ignore: missing_return
Future < SafeBrowsingResponse > androidOnSafeBrowsingHit (
String url , SafeBrowsingThreat threatType ) { }
2019-11-05 23:23:24 +00:00
2019-11-28 01:39:06 +00:00
///Event fired when the WebView is requesting permission to access the specified resources and the permission currently isn't granted or denied.
///
///[origin] represents the origin of the web page which is trying to access the restricted resources.
///
///[resources] represents the array of resources the web content wants to access.
///
///**NOTE**: available only on Android 23+.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebChromeClient#onPermissionRequest(android.webkit.PermissionRequest)
2019-12-01 11:55:06 +00:00
// ignore: missing_return
2019-12-16 22:58:10 +00:00
Future < PermissionRequestResponse > androidOnPermissionRequest (
2019-12-01 11:55:06 +00:00
String origin , List < String > resources ) { }
2019-11-28 01:39:06 +00:00
2019-12-16 22:58:10 +00:00
///Event that notifies the host application that web content from the specified origin is attempting to use the Geolocation API, but no permission state is currently set for that origin.
///Note that for applications targeting Android N and later SDKs (API level > `Build.VERSION_CODES.M`) this method is only called for requests originating from secure origins such as https.
///On non-secure origins geolocation requests are automatically denied.
2019-12-09 23:29:09 +00:00
///
2019-12-16 22:58:10 +00:00
///[origin] represents the origin of the web content attempting to use the Geolocation API.
2019-12-09 23:29:09 +00:00
///
2019-12-16 22:58:10 +00:00
///**NOTE**: available only on Android.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebChromeClient#onGeolocationPermissionsShowPrompt(java.lang.String,%20android.webkit.GeolocationPermissions.Callback)
2019-12-16 22:58:10 +00:00
Future < GeolocationPermissionShowPromptResponse >
2020-05-29 17:56:03 +00:00
// ignore: missing_return
androidOnGeolocationPermissionsShowPrompt ( String origin ) { }
2019-12-16 22:58:10 +00:00
///Notify the host application that a request for Geolocation permissions, made with a previous call to [androidOnGeolocationPermissionsShowPrompt] has been canceled.
///Any related UI should therefore be hidden.
///
///**NOTE**: available only on Android.
2020-05-28 23:03:45 +00:00
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebChromeClient#onGeolocationPermissionsHidePrompt()
2019-12-16 22:58:10 +00:00
void androidOnGeolocationPermissionsHidePrompt ( ) { }
2019-12-09 23:29:09 +00:00
2020-05-28 23:03:45 +00:00
///Notify the host application of a resource request and allow the application to return the data.
///If the return value is `null`, the WebView will continue to load the resource as usual.
///Otherwise, the return response and data will be used.
2020-05-09 02:36:07 +00:00
///
2020-05-28 23:03:45 +00:00
///This callback is invoked for a variety of URL schemes (e.g., `http(s):`, `data:`, `file:`, etc.),
///not only those schemes which send requests over the network.
///This is not called for `javascript:` URLs, `blob:` URLs, or for assets accessed via `file:///android_asset/` or `file:///android_res/` URLs.
///
///In the case of redirects, this is only called for the initial resource URL, not any subsequent redirect URLs.
///
///[request] Object containing the details of the request.
///
///**NOTE**: available only on Android.
///
///**Official Android API**:
///- https://developer.android.com/reference/android/webkit/WebViewClient#shouldInterceptRequest(android.webkit.WebView,%20android.webkit.WebResourceRequest)
///- https://developer.android.com/reference/android/webkit/WebViewClient#shouldInterceptRequest(android.webkit.WebView,%20java.lang.String)
Future < WebResourceResponse >
2020-05-29 17:56:03 +00:00
// ignore: missing_return
androidShouldInterceptRequest ( WebResourceRequest request ) { }
2020-05-09 02:36:07 +00:00
2020-05-28 23:03:45 +00:00
///Event called when the renderer currently associated with the WebView becomes unresponsive as a result of a long running blocking task such as the execution of JavaScript.
///
///If a WebView fails to process an input event, or successfully navigate to a new URL within a reasonable time frame, the renderer is considered to be unresponsive, and this callback will be called.
///
///This callback will continue to be called at regular intervals as long as the renderer remains unresponsive.
///If the renderer becomes responsive again, [androidOnRenderProcessResponsive] will be called once,
///and this method will not subsequently be called unless another period of unresponsiveness is detected.
///
///The minimum interval between successive calls to `androidOnRenderProcessUnresponsive` is 5 seconds.
///
///No action is taken by WebView as a result of this method call.
///Applications may choose to terminate the associated renderer via the object that is passed to this callback,
///if in multiprocess mode, however this must be accompanied by correctly handling [androidOnRenderProcessGone] for this WebView,
///and all other WebViews associated with the same renderer. Failure to do so will result in application termination.
///
///**NOTE**: available only on Android 29+.
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewRenderProcessClient#onRenderProcessUnresponsive(android.webkit.WebView,%20android.webkit.WebViewRenderProcess)
Future < WebViewRenderProcessAction >
2020-05-29 17:56:03 +00:00
// ignore: missing_return
androidOnRenderProcessUnresponsive ( String url ) { }
2020-05-28 23:03:45 +00:00
///Event called once when an unresponsive renderer currently associated with the WebView becomes responsive.
///
///After a WebView renderer becomes unresponsive, which is notified to the application by [androidOnRenderProcessUnresponsive],
///it is possible for the blocking renderer task to complete, returning the renderer to a responsive state.
///In that case, this method is called once to indicate responsiveness.
///
///No action is taken by WebView as a result of this method call.
///
///**NOTE**: available only on Android 29+.
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewRenderProcessClient#onRenderProcessResponsive(android.webkit.WebView,%20android.webkit.WebViewRenderProcess)
Future < WebViewRenderProcessAction >
2020-05-29 17:56:03 +00:00
// ignore: missing_return
androidOnRenderProcessResponsive ( String url ) { }
2020-05-28 23:03:45 +00:00
///Event fired when the given WebView's render process has exited.
///The application's implementation of this callback should only attempt to clean up the WebView.
///The WebView should be removed from the view hierarchy, all references to it should be cleaned up.
///
///[detail] the reason why it exited.
///
///**NOTE**: available only on Android 26+.
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#onRenderProcessGone(android.webkit.WebView,%20android.webkit.RenderProcessGoneDetail)
void androidOnRenderProcessGone ( RenderProcessGoneDetail detail ) { }
///As the host application if the browser should resend data as the requested page was a result of a POST. The default is to not resend the data.
///
///**NOTE**: available only on Android.
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#onFormResubmission(android.webkit.WebView,%20android.os.Message,%20android.os.Message)
Future < FormResubmissionAction >
2020-05-29 17:56:03 +00:00
// ignore: missing_return
androidOnFormResubmission ( String url ) { }
2020-05-28 23:03:45 +00:00
///Event fired when the scale applied to the WebView has changed.
///
///[oldScale] The old scale factor.
///
///[newScale] The new scale factor.
///
///**NOTE**: available only on Android.
///
///**Official Android API**: https://developer.android.com/reference/android/webkit/WebViewClient#onScaleChanged(android.webkit.WebView,%20float,%20float)
void androidOnScaleChanged ( double oldScale , double newScale ) { }
///Invoked when the web view's web content process is terminated.
2020-05-09 02:36:07 +00:00
///
///**NOTE**: available only on iOS.
2020-05-28 23:03:45 +00:00
///
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455639-webviewwebcontentprocessdidtermi
void iosOnWebContentProcessDidTerminate ( ) { }
2020-05-09 02:36:07 +00:00
///Called when a web view receives a server redirect.
///
///**NOTE**: available only on iOS.
2020-05-28 23:03:45 +00:00
///
///**Official iOS API**: https://developer.apple.com/documentation/webkit/wknavigationdelegate/1455627-webview
2020-05-09 02:36:07 +00:00
void iosOnDidReceiveServerRedirectForProvisionalNavigation ( ) { }
2019-10-26 02:42:50 +00:00
void throwIsAlreadyOpened ( { String message = ' ' } ) {
if ( this . isOpened ( ) ) {
2019-12-01 11:55:06 +00:00
throw Exception ( [
' Error: ${ ( message . isEmpty ) ? ' ' : message + ' ' } The browser is already opened. '
] ) ;
2019-10-26 02:42:50 +00:00
}
}
void throwIsNotOpened ( { String message = ' ' } ) {
if ( ! this . isOpened ( ) ) {
2019-12-01 11:55:06 +00:00
throw Exception ( [
' Error: ${ ( message . isEmpty ) ? ' ' : message + ' ' } The browser is not opened. '
] ) ;
2019-10-26 02:42:50 +00:00
}
}
}