Skip to content

Commit

Permalink
fix #1905, updated useShouldInterceptAjaxRequest automatic infer logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed Dec 7, 2023
1 parent 2a4313d commit e60fe17
Show file tree
Hide file tree
Showing 32 changed files with 402 additions and 161 deletions.
4 changes: 2 additions & 2 deletions dev_packages/generators/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ environment:
dependencies:
flutter:
sdk: flutter
build: 2.3.1
build: ^2.4.0
source_gen: ^1.3.1
flutter_inappwebview_internal_annotations: ^1.1.1

dev_dependencies:
build_runner: 2.3.3
build_runner: ^2.4.0
build_test: ^2.1.7
test: ^1.24.2
5 changes: 5 additions & 0 deletions flutter_inappwebview/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 6.0.0-beta.32

- Updated minimum platform interface and implementation versions
- Added `InAppWebViewSettings.interceptOnlyAsyncAjaxRequests` [#1905](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1905)

## 6.0.0-beta.31

- Updated minimum platform interface and implementation versions
Expand Down
6 changes: 3 additions & 3 deletions flutter_inappwebview/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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-beta.31
version: 6.0.0-beta.32
homepage: https://inappwebview.dev/
repository: https://github.com/pichillilorenzo/flutter_inappwebview
issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues
Expand All @@ -22,15 +22,15 @@ dependencies:
flutter_inappwebview_android: ^1.0.5
flutter_inappwebview_ios: ^1.0.5
flutter_inappwebview_macos: ^1.0.3
flutter_inappwebview_web: ^1.0.2
flutter_inappwebview_web: ^1.0.3

dev_dependencies:
flutter_test:
sdk: flutter
flutter_driver:
sdk: flutter
flutter_lints: ^2.0.1
build_runner: 2.3.3
build_runner: ^2.4.0
generators:
path: ../dev_packages/generators

Expand Down
5 changes: 5 additions & 0 deletions flutter_inappwebview_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.8

- Implemented `InAppWebViewSettings.interceptOnlyAsyncAjaxRequests`
- Updated `useShouldInterceptAjaxRequest` automatic infer logic

## 1.0.7

- Merged "Fixed error in InterceptAjaxRequestJS 'Failed to set responseType property'" [#1904](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1904) (thanks to [EArminjon](https://github.com/EArminjon))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class InterceptAjaxRequestJS {

public static final String INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT_GROUP_NAME = "IN_APP_WEBVIEW_INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT";
public static final String FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE = JavaScriptBridgeJS.JAVASCRIPT_BRIDGE_NAME + "._useShouldInterceptAjaxRequest";
public static final String FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE = JavaScriptBridgeJS.JAVASCRIPT_BRIDGE_NAME + "._interceptOnlyAsyncAjaxRequests";
public static final PluginScript INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT = new PluginScript(
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT_GROUP_NAME,
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_SOURCE,
Expand All @@ -16,6 +17,17 @@ public class InterceptAjaxRequestJS {
null
);

public static PluginScript createInterceptOnlyAsyncAjaxRequestsPluginScript(boolean onlyAsync) {
return new PluginScript(
InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT_GROUP_NAME,
"window." + FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE + " = " + onlyAsync +";",
UserScriptInjectionTime.AT_DOCUMENT_START,
null,
true,
null
);
}

public static final String INTERCEPT_AJAX_REQUEST_JS_SOURCE = "(function(ajax) {" +
" var w = (window.top == null || window.top === window) ? window : window.top;" +
" w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE + " = true;" +
Expand Down Expand Up @@ -122,7 +134,8 @@ public class InterceptAjaxRequestJS {
" ajax.prototype.send = function(data) {" +
" var self = this;" +
" var w = (window.top == null || window.top === window) ? window : window.top;" +
" if (w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE + " == null || w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE + " == true) {" +
" var canBeIntercepted = self._flutter_inappwebview_isAsync || w." + FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE + " === false;" +
" if (canBeIntercepted && (w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE + " == null || w." + FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_AJAX_REQUEST_JS_SOURCE + " == true)) {" +
" if (!this._flutter_inappwebview_already_onreadystatechange_wrapped) {" +
" this._flutter_inappwebview_already_onreadystatechange_wrapped = true;" +
" var onreadystatechange = this.onreadystatechange;" +
Expand Down Expand Up @@ -220,7 +233,7 @@ public class InterceptAjaxRequestJS {
" data = new Uint8Array(result.data);" +
" }" +
" self.withCredentials = result.withCredentials;" +
" if (result.responseType != null && self.isAsync) {" +
" if (result.responseType != null && self._flutter_inappwebview_isAsync) {" +
" self.responseType = result.responseType;" +
" };" +
" for (var header in result.headers) {" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie
@Nullable
public WebViewAssetLoaderExt webViewAssetLoaderExt;

@Nullable
private PluginScript interceptOnlyAsyncAjaxRequestsPluginScript;

public InAppWebView(Context context) {
super(context);
}
Expand Down Expand Up @@ -565,7 +568,9 @@ public void prepareAndAddUserScripts() {
userContentController.addPluginScript(PrintJS.PRINT_JS_PLUGIN_SCRIPT);
userContentController.addPluginScript(OnWindowBlurEventJS.ON_WINDOW_BLUR_EVENT_JS_PLUGIN_SCRIPT);
userContentController.addPluginScript(OnWindowFocusEventJS.ON_WINDOW_FOCUS_EVENT_JS_PLUGIN_SCRIPT);
interceptOnlyAsyncAjaxRequestsPluginScript = InterceptAjaxRequestJS.createInterceptOnlyAsyncAjaxRequestsPluginScript(customSettings.interceptOnlyAsyncAjaxRequests);
if (customSettings.useShouldInterceptAjaxRequest) {
userContentController.addPluginScript(interceptOnlyAsyncAjaxRequestsPluginScript);
userContentController.addPluginScript(InterceptAjaxRequestJS.INTERCEPT_AJAX_REQUEST_JS_PLUGIN_SCRIPT);
}
if (customSettings.useShouldInterceptFetchRequest) {
Expand Down Expand Up @@ -772,6 +777,14 @@ public void setSettings(InAppWebViewSettings newCustomSettings, HashMap<String,
);
}

if (newSettingsMap.get("interceptOnlyAsyncAjaxRequests") != null && customSettings.interceptOnlyAsyncAjaxRequests != newCustomSettings.interceptOnlyAsyncAjaxRequests) {
enablePluginScriptAtRuntime(
InterceptAjaxRequestJS.FLAG_VARIABLE_FOR_INTERCEPT_ONLY_ASYNC_AJAX_REQUESTS_JS_SOURCE,
newCustomSettings.interceptOnlyAsyncAjaxRequests,
interceptOnlyAsyncAjaxRequestsPluginScript
);
}

if (newSettingsMap.get("useShouldInterceptFetchRequest") != null && customSettings.useShouldInterceptFetchRequest != newCustomSettings.useShouldInterceptFetchRequest) {
enablePluginScriptAtRuntime(
InterceptFetchRequestJS.FLAG_VARIABLE_FOR_SHOULD_INTERCEPT_FETCH_REQUEST_JS_SOURCE,
Expand Down Expand Up @@ -2030,6 +2043,7 @@ public void onPageFinished(WebView view, String url) {
destroy();
}
});
interceptOnlyAsyncAjaxRequestsPluginScript = null;
userContentController.dispose();
if (findInteractionController != null) {
findInteractionController.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class InAppWebViewSettings implements ISettings<InAppWebViewInterface> {
public List<Map<String, Map<String, Object>>> contentBlockers = new ArrayList<>();
public Integer preferredContentMode = PreferredContentModeOptionType.RECOMMENDED.toValue();
public Boolean useShouldInterceptAjaxRequest = false;
public Boolean interceptOnlyAsyncAjaxRequests = true;
public Boolean useShouldInterceptFetchRequest = false;
public Boolean incognito = false;
public Boolean cacheEnabled = true;
Expand Down Expand Up @@ -184,6 +185,9 @@ public InAppWebViewSettings parse(@NonNull Map<String, Object> settings) {
case "useShouldInterceptAjaxRequest":
useShouldInterceptAjaxRequest = (Boolean) value;
break;
case "interceptOnlyAsyncAjaxRequests":
interceptOnlyAsyncAjaxRequests = (Boolean) value;
break;
case "useShouldInterceptFetchRequest":
useShouldInterceptFetchRequest = (Boolean) value;
break;
Expand Down Expand Up @@ -426,6 +430,7 @@ public Map<String, Object> toMap() {
settings.put("contentBlockers", contentBlockers);
settings.put("preferredContentMode", preferredContentMode);
settings.put("useShouldInterceptAjaxRequest", useShouldInterceptAjaxRequest);
settings.put("interceptOnlyAsyncAjaxRequests", interceptOnlyAsyncAjaxRequests);
settings.put("useShouldInterceptFetchRequest", useShouldInterceptFetchRequest);
settings.put("incognito", incognito);
settings.put("cacheEnabled", cacheEnabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,51 +281,49 @@ class AndroidInAppWebViewWidget extends PlatformInAppWebViewWidget {
AndroidInAppWebViewController? _controller;

AndroidHeadlessInAppWebView? get _androidHeadlessInAppWebView =>
_androidParams.headlessWebView as AndroidHeadlessInAppWebView?;
params.headlessWebView as AndroidHeadlessInAppWebView?;

@override
Widget build(BuildContext context) {
final initialSettings =
_androidParams.initialSettings ?? InAppWebViewSettings();
final initialSettings = params.initialSettings ?? InAppWebViewSettings();
_inferInitialSettings(initialSettings);

Map<String, dynamic> settingsMap = (_androidParams.initialSettings != null
? initialSettings.toMap()
: null) ??
// ignore: deprecated_member_use_from_same_package
_androidParams.initialOptions?.toMap() ??
initialSettings.toMap();
Map<String, dynamic> settingsMap =
(params.initialSettings != null ? initialSettings.toMap() : null) ??
// ignore: deprecated_member_use_from_same_package
params.initialOptions?.toMap() ??
initialSettings.toMap();

Map<String, dynamic> pullToRefreshSettings =
_androidParams.pullToRefreshController?.params.settings.toMap() ??
params.pullToRefreshController?.params.settings.toMap() ??
// ignore: deprecated_member_use_from_same_package
_androidParams.pullToRefreshController?.params.options.toMap() ??
params.pullToRefreshController?.params.options.toMap() ??
PullToRefreshSettings(enabled: false).toMap();

if ((_androidParams.headlessWebView?.isRunning() ?? false) &&
_androidParams.keepAlive != null) {
final headlessId = _androidParams.headlessWebView?.id;
if ((params.headlessWebView?.isRunning() ?? false) &&
params.keepAlive != null) {
final headlessId = params.headlessWebView?.id;
if (headlessId != null) {
// force keep alive id to match headless webview id
_androidParams.keepAlive?.id = headlessId;
params.keepAlive?.id = headlessId;
}
}

var useHybridComposition = (_androidParams.initialSettings != null
var useHybridComposition = (params.initialSettings != null
? initialSettings.useHybridComposition
: _androidParams.initialOptions?.android.useHybridComposition) ??
: params.initialOptions?.android.useHybridComposition) ??
true;

return PlatformViewLink(
key: _androidParams.key,
key: params.key,
viewType: 'com.pichillilorenzo/flutter_inappwebview',
surfaceFactory: (
BuildContext context,
PlatformViewController controller,
) {
return AndroidViewSurface(
controller: controller as AndroidViewController,
gestureRecognizers: _androidParams.gestureRecognizers ??
gestureRecognizers: params.gestureRecognizers ??
const <Factory<OneSequenceGestureRecognizer>>{},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
Expand All @@ -335,26 +333,28 @@ class AndroidInAppWebViewWidget extends PlatformInAppWebViewWidget {
hybridComposition: useHybridComposition,
id: params.id,
viewType: 'com.pichillilorenzo/flutter_inappwebview',
layoutDirection: _androidParams.layoutDirection ??
layoutDirection: this.params.layoutDirection ??
Directionality.maybeOf(context) ??
TextDirection.rtl,
creationParams: <String, dynamic>{
'initialUrlRequest': _androidParams.initialUrlRequest?.toMap(),
'initialFile': _androidParams.initialFile,
'initialData': _androidParams.initialData?.toMap(),
'initialUrlRequest': this.params.initialUrlRequest?.toMap(),
'initialFile': this.params.initialFile,
'initialData': this.params.initialData?.toMap(),
'initialSettings': settingsMap,
'contextMenu': _androidParams.contextMenu?.toMap() ?? {},
'windowId': _androidParams.windowId,
'contextMenu': this.params.contextMenu?.toMap() ?? {},
'windowId': this.params.windowId,
'headlessWebViewId':
_androidParams.headlessWebView?.isRunning() ?? false
? _androidParams.headlessWebView?.id
this.params.headlessWebView?.isRunning() ?? false
? this.params.headlessWebView?.id
: null,
'initialUserScripts': _androidParams.initialUserScripts
'initialUserScripts': this
.params
.initialUserScripts
?.map((e) => e.toMap())
.toList() ??
[],
'pullToRefreshSettings': pullToRefreshSettings,
'keepAliveId': _androidParams.keepAlive?.id
'keepAliveId': this.params.keepAlive?.id
},
)
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
Expand Down Expand Up @@ -391,10 +391,10 @@ class AndroidInAppWebViewWidget extends PlatformInAppWebViewWidget {

void _onPlatformViewCreated(int id) {
dynamic viewId = id;
if (_androidParams.headlessWebView?.isRunning() ?? false) {
viewId = _androidParams.headlessWebView?.id;
if (params.headlessWebView?.isRunning() ?? false) {
viewId = params.headlessWebView?.id;
}
viewId = _androidParams.keepAlive?.id ?? viewId ?? id;
viewId = params.keepAlive?.id ?? viewId ?? id;
_androidHeadlessInAppWebView?.internalDispose();
_controller = AndroidInAppWebViewController(
PlatformInAppWebViewControllerCreationParams(
Expand All @@ -408,42 +408,43 @@ class AndroidInAppWebViewWidget extends PlatformInAppWebViewWidget {
PlatformInAppWebViewController.debugLoggingSettings,
method: "onWebViewCreated",
args: []);
if (_androidParams.onWebViewCreated != null) {
_androidParams.onWebViewCreated!(
if (params.onWebViewCreated != null) {
params.onWebViewCreated!(
params.controllerFromPlatform?.call(_controller!) ?? _controller!);
}
}

void _inferInitialSettings(InAppWebViewSettings settings) {
if (_androidParams.shouldOverrideUrlLoading != null &&
if (params.shouldOverrideUrlLoading != null &&
settings.useShouldOverrideUrlLoading == null) {
settings.useShouldOverrideUrlLoading = true;
}
if (_androidParams.onLoadResource != null &&
settings.useOnLoadResource == null) {
if (params.onLoadResource != null && settings.useOnLoadResource == null) {
settings.useOnLoadResource = true;
}
if (_androidParams.onDownloadStartRequest != null &&
if (params.onDownloadStartRequest != null &&
settings.useOnDownloadStart == null) {
settings.useOnDownloadStart = true;
}
if (_androidParams.shouldInterceptAjaxRequest != null &&
if ((params.shouldInterceptAjaxRequest != null ||
params.onAjaxProgress != null ||
params.onAjaxReadyStateChange != null) &&
settings.useShouldInterceptAjaxRequest == null) {
settings.useShouldInterceptAjaxRequest = true;
}
if (_androidParams.shouldInterceptFetchRequest != null &&
if (params.shouldInterceptFetchRequest != null &&
settings.useShouldInterceptFetchRequest == null) {
settings.useShouldInterceptFetchRequest = true;
}
if (_androidParams.shouldInterceptRequest != null &&
if (params.shouldInterceptRequest != null &&
settings.useShouldInterceptRequest == null) {
settings.useShouldInterceptRequest = true;
}
if (_androidParams.onRenderProcessGone != null &&
if (params.onRenderProcessGone != null &&
settings.useOnRenderProcessGone == null) {
settings.useOnRenderProcessGone = true;
}
if (_androidParams.onNavigationResponse != null &&
if (params.onNavigationResponse != null &&
settings.useOnNavigationResponse == null) {
settings.useOnNavigationResponse = true;
}
Expand All @@ -459,11 +460,11 @@ class AndroidInAppWebViewWidget extends PlatformInAppWebViewWidget {
PlatformInAppWebViewController.debugLoggingSettings,
method: "dispose",
args: []);
final isKeepAlive = _androidParams.keepAlive != null;
final isKeepAlive = params.keepAlive != null;
_controller?.dispose(isKeepAlive: isKeepAlive);
_controller = null;
_androidParams.pullToRefreshController?.dispose(isKeepAlive: isKeepAlive);
_androidParams.findInteractionController?.dispose(isKeepAlive: isKeepAlive);
params.pullToRefreshController?.dispose(isKeepAlive: isKeepAlive);
params.findInteractionController?.dispose(isKeepAlive: isKeepAlive);
}

@override
Expand Down
4 changes: 2 additions & 2 deletions flutter_inappwebview_android/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_inappwebview_android
description: Android implementation of 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_android
issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues
Expand All @@ -18,7 +18,7 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_inappwebview_platform_interface: ^1.0.5
flutter_inappwebview_platform_interface: ^1.0.6

dev_dependencies:
flutter_test:
Expand Down
5 changes: 5 additions & 0 deletions flutter_inappwebview_ios/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.9

- Implemented `InAppWebViewSettings.interceptOnlyAsyncAjaxRequests`
- Updated `useShouldInterceptAjaxRequest` automatic infer logic

## 1.0.8

- Fixed error in InterceptAjaxRequestJS 'Failed to set responseType property'
Expand Down
Loading

0 comments on commit e60fe17

Please sign in to comment.