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: add plugin hooks for WebViewClient.onRenderProcessGone #1574

Merged
merged 2 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions framework/src/org/apache/cordova/CordovaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.webkit.RenderProcessGoneDetail;
import android.webkit.WebView;

import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -442,4 +444,19 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions,
public CordovaPluginPathHandler getPathHandler() {
return null;
}

/**
* Called when the WebView's render process has exited. Can be used to collect information regarding the crash for crashlytics or optionally attempt to gracefully handle/recover the crashed webview by recreating it.
*
* See <a href="https://developer.android.com/reference/android/webkit/WebViewClient#onRenderProcessGone(android.webkit.WebView,%20android.webkit.RenderProcessGoneDetail)">WebViewClient#onRenderProcessGone</a>
*
* Note: A plugin must not attempt to recover a webview that it does not own/manage.
*
* @return true if the host application handled the situation that process has exited,
* otherwise, application will crash if render process crashed, or be killed
* if render process was killed by the system.
*/
public boolean onRenderProcessGone(final WebView view, RenderProcessGoneDetail detail) {
return false;
}
}
27 changes: 27 additions & 0 deletions framework/src/org/apache/cordova/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.os.Bundle;
import android.os.Debug;
import android.os.Build;
import android.webkit.RenderProcessGoneDetail;
import android.webkit.WebView;

/**
* PluginManager is exposed to JavaScript in the Cordova WebView.
Expand Down Expand Up @@ -606,4 +608,29 @@ public ArrayList<CordovaPluginPathHandler> getPluginPathHandlers() {
}
return handlers;
}

/**
* Called when the WebView's render process has exited.
*
* See https://developer.android.com/reference/android/webkit/WebViewClient#onRenderProcessGone(android.webkit.WebView,%20android.webkit.RenderProcessGoneDetail)
*
* @return true if the host application handled the situation that process has exited,
* otherwise, application will crash if render process crashed, or be killed
* if render process was killed by the system.
*/
public boolean onRenderProcessGone(final WebView view, RenderProcessGoneDetail detail) {
boolean result = false;
synchronized (this.entryMap) {
for (PluginEntry entry : this.entryMap.values()) {
CordovaPlugin plugin = pluginMap.get(entry.service);
if (plugin != null) {
if (plugin.onRenderProcessGone(view, detail)) {
result = true;
}
}
}
}

return result;
}
}
12 changes: 12 additions & 0 deletions framework/src/org/apache/cordova/engine/SystemWebViewClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.webkit.ClientCertRequest;
import android.webkit.HttpAuthHandler;
import android.webkit.MimeTypeMap;
import android.webkit.RenderProcessGoneDetail;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
Expand Down Expand Up @@ -422,4 +423,15 @@ private static boolean needsSpecialsInAssetUrlFix(Uri uri) {
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return this.assetLoader.shouldInterceptRequest(request.getUrl());
}

@Override
public boolean onRenderProcessGone(final WebView view, RenderProcessGoneDetail detail) {
// Check if there is some plugin which can handle this event
PluginManager pluginManager = this.parentEngine.pluginManager;
if (pluginManager != null && pluginManager.onRenderProcessGone(view, detail)) {
return true;
}

return super.onRenderProcessGone(view, detail);
}
}