Преглед на файлове

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.
trcoffman преди 4 години
родител
ревизия
5d88af44fa
No account linked to committer's email address
променени са 1 файла, в които са добавени 26 реда и са изтрити 0 реда
  1. 26
    0
      android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java

+ 26
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java Целия файл

@@ -197,6 +197,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
197 197
 
198 198
     webView.setDownloadListener(new DownloadListener() {
199 199
       public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
200
+        webView.setIgnoreErrFailedForThisURL(url);
201
+
200 202
         RNCWebViewModule module = getModule(reactContext);
201 203
 
202 204
         DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
@@ -721,6 +723,11 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
721 723
     protected boolean mLastLoadFailed = false;
722 724
     protected @Nullable
723 725
     ReadableArray mUrlPrefixesForDefaultIntent;
726
+    protected @Nullable String ignoreErrFailedForThisURL = null;
727
+
728
+    public void setIgnoreErrFailedForThisURL(@Nullable String url) {
729
+      ignoreErrFailedForThisURL = url;
730
+    }
724 731
 
725 732
     @Override
726 733
     public void onPageFinished(WebView webView, String url) {
@@ -772,6 +779,21 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
772 779
       int errorCode,
773 780
       String description,
774 781
       String failingUrl) {
782
+
783
+      if (ignoreErrFailedForThisURL != null
784
+          && failingUrl.equals(ignoreErrFailedForThisURL)
785
+          && errorCode == -1
786
+          && description.equals("net::ERR_FAILED")) {
787
+
788
+        // This is a workaround for a bug in the WebView.
789
+        // See these chromium issues for more context:
790
+        // https://bugs.chromium.org/p/chromium/issues/detail?id=1023678
791
+        // https://bugs.chromium.org/p/chromium/issues/detail?id=1050635
792
+        // This entire commit should be reverted once this bug is resolved in chromium.
793
+        setIgnoreErrFailedForThisURL(null);
794
+        return;
795
+      }
796
+
775 797
       super.onReceivedError(webView, errorCode, description, failingUrl);
776 798
       mLastLoadFailed = true;
777 799
 
@@ -999,6 +1021,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
999 1021
       super(reactContext);
1000 1022
     }
1001 1023
 
1024
+    public void setIgnoreErrFailedForThisURL(String url) {
1025
+      mRNCWebViewClient.setIgnoreErrFailedForThisURL(url);
1026
+    }
1027
+
1002 1028
     public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
1003 1029
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
1004 1030
     }