Browse Source

fix(Android): Workaround for chromium bugs 1023678 and 1050635.

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.
Tyler Coffman 4 years ago
parent
commit
b0b99b3699

+ 26
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

@@ -194,6 +194,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
194 194
 
195 195
     webView.setDownloadListener(new DownloadListener() {
196 196
       public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
197
+        webView.setIgnoreErrFailedForThisURL(url);
198
+
197 199
         RNCWebViewModule module = getModule(reactContext);
198 200
 
199 201
         DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
@@ -714,6 +716,11 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
714 716
     protected boolean mLastLoadFailed = false;
715 717
     protected @Nullable
716 718
     ReadableArray mUrlPrefixesForDefaultIntent;
719
+    protected @Nullable String ignoreErrFailedForThisURL = null;
720
+
721
+    public void setIgnoreErrFailedForThisURL(@Nullable String url) {
722
+      ignoreErrFailedForThisURL = url;
723
+    }
717 724
 
718 725
     @Override
719 726
     public void onPageFinished(WebView webView, String url) {
@@ -765,6 +772,21 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
765 772
       int errorCode,
766 773
       String description,
767 774
       String failingUrl) {
775
+
776
+      if (ignoreErrFailedForThisURL != null
777
+          && failingUrl.equals(ignoreErrFailedForThisURL)
778
+          && errorCode == -1
779
+          && description.equals("net::ERR_FAILED")) {
780
+
781
+        // This is a workaround for a bug in the WebView.
782
+        // See these chromium issues for more context:
783
+        // https://bugs.chromium.org/p/chromium/issues/detail?id=1023678
784
+        // https://bugs.chromium.org/p/chromium/issues/detail?id=1050635
785
+        // This entire commit should be reverted once this bug is resolved in chromium.
786
+        setIgnoreErrFailedForThisURL(null);
787
+        return;
788
+      }
789
+
768 790
       super.onReceivedError(webView, errorCode, description, failingUrl);
769 791
       mLastLoadFailed = true;
770 792
 
@@ -988,6 +1010,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
988 1010
       super(reactContext);
989 1011
     }
990 1012
 
1013
+    public void setIgnoreErrFailedForThisURL(String url) {
1014
+      mRNCWebViewClient.setIgnoreErrFailedForThisURL(url);
1015
+    }
1016
+
991 1017
     public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
992 1018
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
993 1019
     }