Skip to content

Commit 459875f

Browse files
Updated code docs, Using Android WebViewClientCompat for Chromium-based WebView if the WebView package major version is >= 73 (https://bugs.chromium.org/p/chromium/issues/detail?id=925887), fix #1422
1 parent fc98712 commit 459875f

File tree

161 files changed

+6328
-5103
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+6328
-5103
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 6.0.0-beta.20
2+
3+
- Using Android `WebViewClientCompat` for Chromium-based WebView if the WebView package major version is >= 73 (https://bugs.chromium.org/p/chromium/issues/detail?id=925887)
4+
- Updated code docs
5+
- Fixed "Unexpected addWebMessageListener behaviour" [#1422](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1422)
6+
17
## 6.0.0-beta.19
28

39
- Updated code docs
@@ -164,6 +170,10 @@
164170
- Removed `URLProtectionSpace.iosIsProxy` property
165171
- `historyUrl` and `baseUrl` of `InAppWebViewInitialData` can be `null`
166172

173+
## 5.7.2+2
174+
175+
- Fixed "Unexpected addWebMessageListener behaviour" [#1422](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1422)
176+
167177
## 5.7.2+1
168178

169179
- Fixed "Cannot Grant Permission at Android 21" [#1447](https://github.com/pichillilorenzo/flutter_inappwebview/issues/1447)

android/src/main/java/com/pichillilorenzo/flutter_inappwebview/InAppWebViewStatic.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pichillilorenzo.flutter_inappwebview;
22

3+
import android.annotation.SuppressLint;
34
import android.content.Context;
45
import android.content.pm.PackageInfo;
56
import android.os.Build;
@@ -82,28 +83,15 @@ public void onReceiveValue(Boolean value) {
8283
result.success(false);
8384
break;
8485
case "getCurrentWebViewPackage":
85-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && plugin != null && (plugin.activity != null || plugin.applicationContext != null)) {
86-
Context context = plugin.activity;
86+
Context context = null;
87+
if (plugin != null) {
88+
context = plugin.activity;
8789
if (context == null) {
8890
context = plugin.applicationContext;
8991
}
90-
result.success(convertWebViewPackageToMap(WebViewCompat.getCurrentWebViewPackage(context)));
91-
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
92-
//with Android Lollipop (API 21) they started to update the WebView
93-
//as a separate APK with the PlayStore and they added the
94-
//getLoadedPackageInfo() method to the WebViewFactory class and this
95-
//should handle the Android 7.0 behaviour changes too
96-
try {
97-
Class webViewFactory = Class.forName("android.webkit.WebViewFactory");
98-
Method method = webViewFactory.getMethod("getLoadedPackageInfo");
99-
PackageInfo pInfo = (PackageInfo) method.invoke(null);
100-
result.success(convertWebViewPackageToMap(pInfo));
101-
} catch (Exception e) {
102-
result.success(null);
103-
}
104-
} else {
105-
result.success(null);
10692
}
93+
PackageInfo packageInfo = context != null ? WebViewCompat.getCurrentWebViewPackage(context) : null;
94+
result.success(packageInfo != null ? convertWebViewPackageToMap(packageInfo) : null);
10795
break;
10896
case "setWebContentsDebuggingEnabled":
10997
{
@@ -135,10 +123,8 @@ public void onReceiveValue(Boolean value) {
135123
}
136124
}
137125

138-
public Map<String, Object> convertWebViewPackageToMap(PackageInfo webViewPackageInfo) {
139-
if (webViewPackageInfo == null) {
140-
return null;
141-
}
126+
@NonNull
127+
public Map<String, Object> convertWebViewPackageToMap(@NonNull PackageInfo webViewPackageInfo) {
142128
HashMap<String, Object> webViewPackageInfoMap = new HashMap<>();
143129

144130
webViewPackageInfoMap.put("versionName", webViewPackageInfo.versionName);

android/src/main/java/com/pichillilorenzo/flutter_inappwebview/types/WebResourceErrorExt.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import android.os.Build;
44
import android.webkit.WebResourceError;
5+
import android.webkit.WebView;
56

67
import androidx.annotation.NonNull;
78
import androidx.annotation.RequiresApi;
9+
import androidx.webkit.WebResourceErrorCompat;
10+
import androidx.webkit.WebViewFeature;
811

912
import java.util.HashMap;
1013
import java.util.Map;
@@ -24,6 +27,19 @@ static public WebResourceErrorExt fromWebResourceError(@NonNull WebResourceError
2427
return new WebResourceErrorExt(error.getErrorCode(), error.getDescription().toString());
2528
}
2629

30+
@RequiresApi(Build.VERSION_CODES.M)
31+
static public WebResourceErrorExt fromWebResourceError(@NonNull WebResourceErrorCompat error) {
32+
int type = -1;
33+
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_RESOURCE_ERROR_GET_CODE)) {
34+
type = error.getErrorCode();
35+
}
36+
String description = "";
37+
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_RESOURCE_ERROR_GET_DESCRIPTION)) {
38+
description = error.getDescription().toString();
39+
}
40+
return new WebResourceErrorExt(type, description);
41+
}
42+
2743
public Map<String, Object> toMap() {
2844
Map<String, Object> webResourceErrorMap = new HashMap<>();
2945
webResourceErrorMap.put("type", getType());

android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/FlutterWebView.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ public void onPageFinished(WebView view, String url) {
146146
if (webView.inAppWebViewChromeClient != null) {
147147
webView.inAppWebViewChromeClient.dispose();
148148
}
149+
if (webView.inAppWebViewClientCompat != null) {
150+
webView.inAppWebViewClientCompat.dispose();
151+
}
149152
if (webView.inAppWebViewClient != null) {
150153
webView.inAppWebViewClient.dispose();
151154
}

android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebView.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.annotation.SuppressLint;
99
import android.annotation.TargetApi;
1010
import android.content.Context;
11+
import android.content.pm.PackageInfo;
1112
import android.graphics.Bitmap;
1213
import android.graphics.Canvas;
1314
import android.graphics.Color;
@@ -47,6 +48,7 @@
4748
import android.webkit.WebHistoryItem;
4849
import android.webkit.WebSettings;
4950
import android.webkit.WebStorage;
51+
import android.webkit.WebViewClient;
5052
import android.widget.HorizontalScrollView;
5153
import android.widget.LinearLayout;
5254
import android.widget.TextView;
@@ -59,6 +61,7 @@
5961
import androidx.webkit.WebViewFeature;
6062

6163
import com.pichillilorenzo.flutter_inappwebview.InAppWebViewFlutterPlugin;
64+
import com.pichillilorenzo.flutter_inappwebview.InAppWebViewStatic;
6265
import com.pichillilorenzo.flutter_inappwebview.R;
6366
import com.pichillilorenzo.flutter_inappwebview.Util;
6467
import com.pichillilorenzo.flutter_inappwebview.content_blocker.ContentBlocker;
@@ -124,6 +127,8 @@ final public class InAppWebView extends InputAwareWebView implements InAppWebVie
124127
@Nullable
125128
public InAppWebViewClient inAppWebViewClient;
126129
@Nullable
130+
public InAppWebViewClientCompat inAppWebViewClientCompat;
131+
@Nullable
127132
public InAppWebViewChromeClient inAppWebViewChromeClient;
128133
@Nullable
129134
public InAppWebViewRenderProcessClient inAppWebViewRenderProcessClient;
@@ -199,6 +204,36 @@ public InAppWebView(Context context, @NonNull InAppWebViewFlutterPlugin plugin,
199204
}
200205
}
201206

207+
public WebViewClient createWebViewClient(InAppBrowserDelegate inAppBrowserDelegate) {
208+
// bug https://bugs.chromium.org/p/chromium/issues/detail?id=925887
209+
PackageInfo packageInfo = WebViewCompat.getCurrentWebViewPackage(getContext());
210+
if (packageInfo == null) {
211+
Log.d(LOG_TAG, "Using InAppWebViewClient implementation");
212+
return new InAppWebViewClient(inAppBrowserDelegate);
213+
}
214+
215+
boolean isChromiumWebView = "com.android.webview".equals(packageInfo.packageName) ||
216+
"com.google.android.webview".equals(packageInfo.packageName) ||
217+
"com.android.chrome".equals(packageInfo.packageName);
218+
boolean isChromiumWebViewBugFixed = false;
219+
if (isChromiumWebView) {
220+
String versionName = packageInfo.versionName != null ? packageInfo.versionName : "";
221+
try {
222+
int majorVersion = versionName.contains(".") ?
223+
Integer.parseInt(versionName.split("\\.")[0]) : 0;
224+
isChromiumWebViewBugFixed = majorVersion >= 73;
225+
} catch (NumberFormatException ignored) {}
226+
}
227+
228+
if (isChromiumWebViewBugFixed || !isChromiumWebView) {
229+
Log.d(LOG_TAG, "Using InAppWebViewClientCompat implementation");
230+
return new InAppWebViewClientCompat(inAppBrowserDelegate);
231+
} else {
232+
Log.d(LOG_TAG, "Using InAppWebViewClient implementation");
233+
return new InAppWebViewClient(inAppBrowserDelegate);
234+
}
235+
}
236+
202237
@SuppressLint("RestrictedApi")
203238
public void prepare() {
204239
if (plugin != null) {
@@ -211,8 +246,14 @@ public void prepare() {
211246
inAppWebViewChromeClient = new InAppWebViewChromeClient(plugin, this, inAppBrowserDelegate);
212247
setWebChromeClient(inAppWebViewChromeClient);
213248

214-
inAppWebViewClient = new InAppWebViewClient(inAppBrowserDelegate);
215-
setWebViewClient(inAppWebViewClient);
249+
WebViewClient webViewClient = createWebViewClient(inAppBrowserDelegate);
250+
if (webViewClient instanceof InAppWebViewClientCompat) {
251+
inAppWebViewClientCompat = (InAppWebViewClientCompat) webViewClient;
252+
setWebViewClient(inAppWebViewClientCompat);
253+
} else if (webViewClient instanceof InAppWebViewClient) {
254+
inAppWebViewClient = (InAppWebViewClient) webViewClient;
255+
setWebViewClient(inAppWebViewClient);
256+
}
216257

217258
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && WebViewFeature.isFeatureSupported(WebViewFeature.WEB_VIEW_RENDERER_CLIENT_BASIC_USAGE)) {
218259
inAppWebViewRenderProcessClient = new InAppWebViewRenderProcessClient();
@@ -1982,6 +2023,7 @@ public void dispose() {
19822023
evaluateJavaScriptContentWorldCallbacks.clear();
19832024
inAppBrowserDelegate = null;
19842025
inAppWebViewChromeClient = null;
2026+
inAppWebViewClientCompat = null;
19852027
inAppWebViewClient = null;
19862028
javaScriptBridgeInterface = null;
19872029
inAppWebViewRenderProcessClient = null;

android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebViewChromeClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,9 @@ public void onProgressChanged(WebView view, int progress) {
731731

732732
InAppWebView webView = (InAppWebView) view;
733733

734-
if (webView.inAppWebViewClient != null) {
734+
if (webView.inAppWebViewClientCompat != null) {
735+
webView.inAppWebViewClientCompat.loadCustomJavaScriptOnPageStarted(view);
736+
} else if (webView.inAppWebViewClient != null) {
735737
webView.inAppWebViewClient.loadCustomJavaScriptOnPageStarted(view);
736738
}
737739

android/src/main/java/com/pichillilorenzo/flutter_inappwebview/webview/in_app_webview/InAppWebViewClient.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ public boolean shouldOverrideUrlLoading(WebView webView, String url) {
114114
return false;
115115
}
116116

117-
private void allowShouldOverrideUrlLoading(WebView webView, String url, Map<String, String> headers, boolean isForMainFrame) {
117+
private void allowShouldOverrideUrlLoading(WebView webView, String url,
118+
@Nullable Map<String, String> headers,
119+
boolean isForMainFrame) {
118120
if (isForMainFrame) {
119121
// There isn't any way to load an URL for a frame that is not the main frame,
120122
// so call this only on main frame.
@@ -124,8 +126,11 @@ private void allowShouldOverrideUrlLoading(WebView webView, String url, Map<Stri
124126
webView.loadUrl(url);
125127
}
126128
}
127-
public void onShouldOverrideUrlLoading(final InAppWebView webView, final String url, final String method, final Map<String, String> headers,
128-
final boolean isForMainFrame, boolean hasGesture, boolean isRedirect) {
129+
public void onShouldOverrideUrlLoading(final InAppWebView webView, final String url,
130+
final String method,
131+
@Nullable final Map<String, String> headers,
132+
final boolean isForMainFrame, boolean hasGesture,
133+
boolean isRedirect) {
129134
URLRequest request = new URLRequest(url, method, null, headers);
130135
NavigationAction navigationAction = new NavigationAction(
131136
request,

0 commit comments

Comments
 (0)