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
 
194
 
195
     webView.setDownloadListener(new DownloadListener() {
195
     webView.setDownloadListener(new DownloadListener() {
196
       public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
196
       public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
197
+        webView.setIgnoreErrFailedForThisURL(url);
198
+
197
         RNCWebViewModule module = getModule(reactContext);
199
         RNCWebViewModule module = getModule(reactContext);
198
 
200
 
199
         DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
201
         DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
714
     protected boolean mLastLoadFailed = false;
716
     protected boolean mLastLoadFailed = false;
715
     protected @Nullable
717
     protected @Nullable
716
     ReadableArray mUrlPrefixesForDefaultIntent;
718
     ReadableArray mUrlPrefixesForDefaultIntent;
719
+    protected @Nullable String ignoreErrFailedForThisURL = null;
720
+
721
+    public void setIgnoreErrFailedForThisURL(@Nullable String url) {
722
+      ignoreErrFailedForThisURL = url;
723
+    }
717
 
724
 
718
     @Override
725
     @Override
719
     public void onPageFinished(WebView webView, String url) {
726
     public void onPageFinished(WebView webView, String url) {
765
       int errorCode,
772
       int errorCode,
766
       String description,
773
       String description,
767
       String failingUrl) {
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
       super.onReceivedError(webView, errorCode, description, failingUrl);
790
       super.onReceivedError(webView, errorCode, description, failingUrl);
769
       mLastLoadFailed = true;
791
       mLastLoadFailed = true;
770
 
792
 
988
       super(reactContext);
1010
       super(reactContext);
989
     }
1011
     }
990
 
1012
 
1013
+    public void setIgnoreErrFailedForThisURL(String url) {
1014
+      mRNCWebViewClient.setIgnoreErrFailedForThisURL(url);
1015
+    }
1016
+
991
     public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
1017
     public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
992
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
1018
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
993
     }
1019
     }