Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): add plugin hooks for WebViewClient.onRenderProcessGone #6416

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.webkit.RenderProcessGoneDetail;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
Expand Down Expand Up @@ -397,6 +398,22 @@ public boolean launchIntent(Uri url) {
return false;
}

public boolean onRenderProcessGone(final WebView view, RenderProcessGoneDetail detail) {
/*
* Give plugins the chance to handle or record render process removal
*/
boolean result = false;
for (Map.Entry<String, PluginHandle> entry : plugins.entrySet()) {
Plugin plugin = entry.getValue().getInstance();
if (plugin != null) {
if (plugin.onRenderProcessGone(view, detail)) {
result = true;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop continues irrespective of the return value so that any reporting plugins can find out about the crash even if one of the plugins is attempting to handle it gracefully.

}
}
}
return result;
}

private boolean isNewBinary() {
String versionCode = "";
String versionName = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.graphics.Bitmap;
import android.net.Uri;
import android.webkit.RenderProcessGoneDetail;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
Expand Down Expand Up @@ -92,4 +93,9 @@ public void onReceivedHttpError(WebView view, WebResourceRequest request, WebRes
view.loadUrl(errorPath);
}
}

@Override
public boolean onRenderProcessGone(final WebView view, RenderProcessGoneDetail detail) {
return bridge.onRenderProcessGone(view, detail);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The webview is passed down as the onRenderProcessGone method is called for any render crashes within the app (including a pop-up tab, for example).

}
}
19 changes: 19 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.RenderProcessGoneDetail;
import android.webkit.WebView;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
Expand Down Expand Up @@ -1006,6 +1008,23 @@ public Boolean shouldOverrideLoad(Uri url) {
return null;
}


/**
* Give the plugins a chance to record or respond to a WebView render process terminating.
*
* Note: A plugin must not attempt to recover a webview that it does not own/manage.
* Returning true without proper recovery will leave the webview in
* an invalid state. See {@link android.webkit.WebViewClient#onRenderProcessGone} for information about
* how to properly recover a webview.
*
* Returning true indicates the WebView termination has been handled by the plugin.
* Returning false indicates the WebView termination has not been handled.
*/
@SuppressWarnings("unused")
public boolean onRenderProcessGone(final WebView view, RenderProcessGoneDetail detail) {
return false;
}

/**
* Start a new Activity.
*
Expand Down