Przeglądaj źródła

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 lat temu
rodzic
commit
5d88af44fa
No account linked to committer's email address

+ 26
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java Wyświetl plik

197
 
197
 
198
     webView.setDownloadListener(new DownloadListener() {
198
     webView.setDownloadListener(new DownloadListener() {
199
       public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
199
       public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
200
+        webView.setIgnoreErrFailedForThisURL(url);
201
+
200
         RNCWebViewModule module = getModule(reactContext);
202
         RNCWebViewModule module = getModule(reactContext);
201
 
203
 
202
         DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
204
         DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
721
     protected boolean mLastLoadFailed = false;
723
     protected boolean mLastLoadFailed = false;
722
     protected @Nullable
724
     protected @Nullable
723
     ReadableArray mUrlPrefixesForDefaultIntent;
725
     ReadableArray mUrlPrefixesForDefaultIntent;
726
+    protected @Nullable String ignoreErrFailedForThisURL = null;
727
+
728
+    public void setIgnoreErrFailedForThisURL(@Nullable String url) {
729
+      ignoreErrFailedForThisURL = url;
730
+    }
724
 
731
 
725
     @Override
732
     @Override
726
     public void onPageFinished(WebView webView, String url) {
733
     public void onPageFinished(WebView webView, String url) {
772
       int errorCode,
779
       int errorCode,
773
       String description,
780
       String description,
774
       String failingUrl) {
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
       super.onReceivedError(webView, errorCode, description, failingUrl);
797
       super.onReceivedError(webView, errorCode, description, failingUrl);
776
       mLastLoadFailed = true;
798
       mLastLoadFailed = true;
777
 
799
 
999
       super(reactContext);
1021
       super(reactContext);
1000
     }
1022
     }
1001
 
1023
 
1024
+    public void setIgnoreErrFailedForThisURL(String url) {
1025
+      mRNCWebViewClient.setIgnoreErrFailedForThisURL(url);
1026
+    }
1027
+
1002
     public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
1028
     public void setSendContentSizeChangeEvents(boolean sendContentSizeChangeEvents) {
1003
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
1029
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
1004
     }
1030
     }