From 5d88af44fabc1b061f5814659aacce2e993705f1 Mon Sep 17 00:00:00 2001 From: trcoffman Date: Fri, 10 Apr 2020 13:41:53 -0700 Subject: [PATCH] fix(Android): Workaround for chromium bugs 1023678 and 1050635. (#1221) There is a bug in the WebView that causes a spurious call to onReceivedError whenever you download a file. This commit is a workaround for that bug. The idea here is to try and detect these spurious errors and drop them before they cause problems. This commit should be reverted once those chromium bugs are fixed. --- .../webview/RNCWebViewManager.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java index 437ac73..1995d87 100644 --- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java +++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java @@ -197,6 +197,8 @@ public class RNCWebViewManager extends SimpleViewManager { webView.setDownloadListener(new DownloadListener() { public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { + webView.setIgnoreErrFailedForThisURL(url); + RNCWebViewModule module = getModule(reactContext); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); @@ -721,6 +723,11 @@ public class RNCWebViewManager extends SimpleViewManager { protected boolean mLastLoadFailed = false; protected @Nullable ReadableArray mUrlPrefixesForDefaultIntent; + protected @Nullable String ignoreErrFailedForThisURL = null; + + public void setIgnoreErrFailedForThisURL(@Nullable String url) { + ignoreErrFailedForThisURL = url; + } @Override public void onPageFinished(WebView webView, String url) { @@ -772,6 +779,21 @@ public class RNCWebViewManager extends SimpleViewManager { int errorCode, String description, String failingUrl) { + + if (ignoreErrFailedForThisURL != null + && failingUrl.equals(ignoreErrFailedForThisURL) + && errorCode == -1 + && description.equals("net::ERR_FAILED")) { + + // This is a workaround for a bug in the WebView. + // See these chromium issues for more context: + // https://bugs.chromium.org/p/chromium/issues/detail?id=1023678 + // https://bugs.chromium.org/p/chromium/issues/detail?id=1050635 + // This entire commit should be reverted once this bug is resolved in chromium. + setIgnoreErrFailedForThisURL(null); + return; + } + super.onReceivedError(webView, errorCode, description, failingUrl); mLastLoadFailed = true; @@ -999,6 +1021,10 @@ public class RNCWebViewManager extends SimpleViewManager { super(reactContext); } + public void setIgnoreErrFailedForThisURL(String url) { + mRNCWebViewClient.setIgnoreErrFailedForThisURL(url); + } + public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) { this.sendContentSizeChangeEvents = sendContentSizeChangeEvents; }