iosWebViewFix/lib/src/in_app_webview/in_app_webview_options.dart

341 lines
13 KiB
Dart
Raw Normal View History

import 'package:flutter/foundation.dart';
import 'android/in_app_webview_options.dart';
import 'ios/in_app_webview_options.dart';
import '../content_blocker.dart';
import '../types.dart';
import '../in_app_browser/in_app_browser_options.dart';
import 'webview.dart';
class WebViewOptions {
Map<String, dynamic> toMap() {
return {};
}
static WebViewOptions fromMap(Map<String, dynamic> map) {
return new WebViewOptions();
}
WebViewOptions copy() {
return WebViewOptions.fromMap(this.toMap());
}
Map<String, dynamic> toJson() {
return this.toMap();
}
@override
String toString() {
return toMap().toString();
}
}
///Class that represents the options that can be used for a [WebView].
class InAppWebViewGroupOptions {
///Cross-platform options.
late InAppWebViewOptions crossPlatform;
///Android-specific options.
late AndroidInAppWebViewOptions android;
///iOS-specific options.
late IOSInAppWebViewOptions ios;
2021-02-22 22:54:09 +00:00
InAppWebViewGroupOptions(
{InAppWebViewOptions? crossPlatform,
AndroidInAppWebViewOptions? android,
IOSInAppWebViewOptions? ios}) {
this.crossPlatform = crossPlatform ?? InAppWebViewOptions();
this.android = android ?? AndroidInAppWebViewOptions();
this.ios = ios ?? IOSInAppWebViewOptions();
}
Map<String, dynamic> toMap() {
Map<String, dynamic> options = {};
options.addAll(this.crossPlatform.toMap());
if (defaultTargetPlatform == TargetPlatform.android)
options.addAll(this.android.toMap());
2021-02-22 22:54:09 +00:00
else if (defaultTargetPlatform == TargetPlatform.iOS)
options.addAll(this.ios.toMap());
return options;
}
static InAppWebViewGroupOptions fromMap(Map<String, dynamic> options) {
InAppWebViewGroupOptions inAppWebViewGroupOptions =
2021-02-22 22:54:09 +00:00
InAppWebViewGroupOptions();
inAppWebViewGroupOptions.crossPlatform =
InAppWebViewOptions.fromMap(options);
if (defaultTargetPlatform == TargetPlatform.android)
inAppWebViewGroupOptions.android =
AndroidInAppWebViewOptions.fromMap(options);
else if (defaultTargetPlatform == TargetPlatform.iOS)
inAppWebViewGroupOptions.ios = IOSInAppWebViewOptions.fromMap(options);
return inAppWebViewGroupOptions;
}
Map<String, dynamic> toJson() {
return this.toMap();
}
@override
String toString() {
return toMap().toString();
}
InAppWebViewGroupOptions copy() {
return InAppWebViewGroupOptions.fromMap(this.toMap());
}
}
///This class represents all the cross-platform WebView options available.
class InAppWebViewOptions
implements WebViewOptions, BrowserOptions, AndroidOptions, IosOptions {
///Set to `true` to be able to listen at the [WebView.shouldOverrideUrlLoading] event. The default value is `false`.
bool useShouldOverrideUrlLoading;
///Set to `true` to be able to listen at the [WebView.onLoadResource] event. The default value is `false`.
bool useOnLoadResource;
2022-05-05 19:23:00 +00:00
///Set to `true` to be able to listen at the [WebView.onDownloadStartRequest] event. The default value is `false`.
bool useOnDownloadStart;
///Set to `true` to have all the browser's cache cleared before the new WebView is opened. The default value is `false`.
bool clearCache;
///Sets the user-agent for the WebView.
///
///**NOTE**: available on iOS 9.0+.
String userAgent;
///Append to the existing user-agent. Setting userAgent will override this.
///
///**NOTE**: available on Android 17+ and on iOS 9.0+.
String applicationNameForUserAgent;
///Set to `true` to enable JavaScript. The default value is `true`.
bool javaScriptEnabled;
///Set to `true` to allow JavaScript open windows without user interaction. The default value is `false`.
bool javaScriptCanOpenWindowsAutomatically;
///Set to `true` to prevent HTML5 audio or video from autoplaying. The default value is `true`.
///
///**NOTE**: available on iOS 10.0+.
bool mediaPlaybackRequiresUserGesture;
///Sets the minimum font size. The default value is `8` for Android, `0` for iOS.
int? minimumFontSize;
///Define whether the vertical scrollbar should be drawn or not. The default value is `true`.
bool verticalScrollBarEnabled;
///Define whether the horizontal scrollbar should be drawn or not. The default value is `true`.
bool horizontalScrollBarEnabled;
///List of custom schemes that the WebView must handle. Use the [WebView.onLoadResourceCustomScheme] event to intercept resource requests with custom scheme.
///
///**NOTE**: available on iOS 11.0+.
List<String> resourceCustomSchemes;
///List of [ContentBlocker] that are a set of rules used to block content in the browser window.
///
///**NOTE**: available on iOS 11.0+.
List<ContentBlocker> contentBlockers;
///Sets the content mode that the WebView needs to use when loading and rendering a webpage. The default value is [UserPreferredContentMode.RECOMMENDED].
///
///**NOTE**: available on iOS 13.0+.
UserPreferredContentMode? preferredContentMode;
///Set to `true` to be able to listen at the [WebView.shouldInterceptAjaxRequest] event. The default value is `false`.
bool useShouldInterceptAjaxRequest;
///Set to `true` to be able to listen at the [WebView.shouldInterceptFetchRequest] event. The default value is `false`.
bool useShouldInterceptFetchRequest;
///Set to `true` to open a browser window with incognito mode. The default value is `false`.
///
///**NOTE**: available on iOS 9.0+.
///On Android, by setting this option to `true`, it will clear all the cookies of all WebView instances,
///because there isn't any way to make the website data store non-persistent for the specific WebView instance such as on iOS.
bool incognito;
///Sets whether WebView should use browser caching. The default value is `true`.
///
///**NOTE**: available on iOS 9.0+.
bool cacheEnabled;
///Set to `true` to make the background of the WebView transparent. If your app has a dark theme, this can prevent a white flash on initialization. The default value is `false`.
bool transparentBackground;
///Set to `true` to disable vertical scroll. The default value is `false`.
bool disableVerticalScroll;
///Set to `true` to disable horizontal scroll. The default value is `false`.
bool disableHorizontalScroll;
///Set to `true` to disable context menu. The default value is `false`.
bool disableContextMenu;
///Set to `false` if the WebView should not support zooming using its on-screen zoom controls and gestures. The default value is `true`.
bool supportZoom;
///Sets whether cross-origin requests in the context of a file scheme URL should be allowed to access content from other file scheme URLs.
///Note that some accesses such as image HTML elements don't follow same-origin rules and aren't affected by this setting.
///
///Don't enable this setting if you open files that may be created or altered by external sources.
///Enabling this setting allows malicious scripts loaded in a `file://` context to access arbitrary local files including WebView cookies and app private data.
///
///Note that the value of this setting is ignored if the value of [allowUniversalAccessFromFileURLs] is `true`.
///
///The default value is `false`.
bool allowFileAccessFromFileURLs;
///Sets whether cross-origin requests in the context of a file scheme URL should be allowed to access content from any origin.
///This includes access to content from other file scheme URLs or web contexts.
///Note that some access such as image HTML elements doesn't follow same-origin rules and isn't affected by this setting.
///
///Don't enable this setting if you open files that may be created or altered by external sources.
///Enabling this setting allows malicious scripts loaded in a `file://` context to launch cross-site scripting attacks,
///either accessing arbitrary local files including WebView cookies, app private data or even credentials used on arbitrary web sites.
///
///The default value is `false`.
bool allowUniversalAccessFromFileURLs;
InAppWebViewOptions(
{this.useShouldOverrideUrlLoading = false,
this.useOnLoadResource = false,
this.useOnDownloadStart = false,
this.clearCache = false,
this.userAgent = "",
this.applicationNameForUserAgent = "",
this.javaScriptEnabled = true,
this.javaScriptCanOpenWindowsAutomatically = false,
this.mediaPlaybackRequiresUserGesture = true,
this.minimumFontSize,
this.verticalScrollBarEnabled = true,
this.horizontalScrollBarEnabled = true,
this.resourceCustomSchemes = const [],
this.contentBlockers = const [],
this.preferredContentMode = UserPreferredContentMode.RECOMMENDED,
this.useShouldInterceptAjaxRequest = false,
this.useShouldInterceptFetchRequest = false,
this.incognito = false,
this.cacheEnabled = true,
this.transparentBackground = false,
this.disableVerticalScroll = false,
this.disableHorizontalScroll = false,
this.disableContextMenu = false,
this.supportZoom = true,
this.allowFileAccessFromFileURLs = false,
this.allowUniversalAccessFromFileURLs = false}) {
if (this.minimumFontSize == null)
2021-02-22 22:54:09 +00:00
this.minimumFontSize =
defaultTargetPlatform == TargetPlatform.android ? 8 : 0;
assert(!this.resourceCustomSchemes.contains("http") &&
!this.resourceCustomSchemes.contains("https"));
}
@override
Map<String, dynamic> toMap() {
List<Map<String, Map<String, dynamic>>> contentBlockersMapList = [];
contentBlockers.forEach((contentBlocker) {
contentBlockersMapList.add(contentBlocker.toMap());
});
return {
"useShouldOverrideUrlLoading": useShouldOverrideUrlLoading,
"useOnLoadResource": useOnLoadResource,
"useOnDownloadStart": useOnDownloadStart,
"clearCache": clearCache,
"userAgent": userAgent,
"applicationNameForUserAgent": applicationNameForUserAgent,
"javaScriptEnabled": javaScriptEnabled,
"javaScriptCanOpenWindowsAutomatically":
javaScriptCanOpenWindowsAutomatically,
"mediaPlaybackRequiresUserGesture": mediaPlaybackRequiresUserGesture,
"verticalScrollBarEnabled": verticalScrollBarEnabled,
"horizontalScrollBarEnabled": horizontalScrollBarEnabled,
"resourceCustomSchemes": resourceCustomSchemes,
"contentBlockers": contentBlockersMapList,
"preferredContentMode": preferredContentMode?.toValue(),
"useShouldInterceptAjaxRequest": useShouldInterceptAjaxRequest,
"useShouldInterceptFetchRequest": useShouldInterceptFetchRequest,
"incognito": incognito,
"cacheEnabled": cacheEnabled,
"transparentBackground": transparentBackground,
"disableVerticalScroll": disableVerticalScroll,
"disableHorizontalScroll": disableHorizontalScroll,
"disableContextMenu": disableContextMenu,
"supportZoom": supportZoom,
"allowFileAccessFromFileURLs": allowFileAccessFromFileURLs,
"allowUniversalAccessFromFileURLs": allowUniversalAccessFromFileURLs
};
}
static InAppWebViewOptions fromMap(Map<String, dynamic> map) {
List<ContentBlocker> contentBlockers = [];
List<dynamic>? contentBlockersMapList = map["contentBlockers"];
if (contentBlockersMapList != null) {
contentBlockersMapList.forEach((contentBlocker) {
contentBlockers.add(ContentBlocker.fromMap(
Map<dynamic, Map<dynamic, dynamic>>.from(
Map<dynamic, dynamic>.from(contentBlocker))));
});
}
InAppWebViewOptions options = InAppWebViewOptions();
options.useShouldOverrideUrlLoading = map["useShouldOverrideUrlLoading"];
options.useOnLoadResource = map["useOnLoadResource"];
options.useOnDownloadStart = map["useOnDownloadStart"];
options.clearCache = map["clearCache"];
options.userAgent = map["userAgent"];
options.applicationNameForUserAgent = map["applicationNameForUserAgent"];
options.javaScriptEnabled = map["javaScriptEnabled"];
options.javaScriptCanOpenWindowsAutomatically =
map["javaScriptCanOpenWindowsAutomatically"];
options.mediaPlaybackRequiresUserGesture =
map["mediaPlaybackRequiresUserGesture"];
options.verticalScrollBarEnabled = map["verticalScrollBarEnabled"];
options.horizontalScrollBarEnabled = map["horizontalScrollBarEnabled"];
options.resourceCustomSchemes =
List<String>.from(map["resourceCustomSchemes"] ?? []);
options.contentBlockers = contentBlockers;
options.preferredContentMode =
UserPreferredContentMode.fromValue(map["preferredContentMode"]);
options.useShouldInterceptAjaxRequest =
map["useShouldInterceptAjaxRequest"];
options.useShouldInterceptFetchRequest =
map["useShouldInterceptFetchRequest"];
options.incognito = map["incognito"];
options.cacheEnabled = map["cacheEnabled"];
options.transparentBackground = map["transparentBackground"];
options.disableVerticalScroll = map["disableVerticalScroll"];
options.disableHorizontalScroll = map["disableHorizontalScroll"];
options.disableContextMenu = map["disableContextMenu"];
options.supportZoom = map["supportZoom"];
options.allowFileAccessFromFileURLs = map["allowFileAccessFromFileURLs"];
2021-02-22 22:54:09 +00:00
options.allowUniversalAccessFromFileURLs =
map["allowUniversalAccessFromFileURLs"];
return options;
}
@override
Map<String, dynamic> toJson() {
return this.toMap();
}
@override
String toString() {
return toMap().toString();
}
@override
InAppWebViewOptions copy() {
return InAppWebViewOptions.fromMap(this.toMap());
}
}