Skip to content

Commit d5e2937

Browse files
committed
Eliminate gap between finalize() and destroy()
Bug: 6569073 Only nativeDestroy and nativeStopGL need to be called on the UI thread, so split up destroyImpl into 3 functions, and only have the native destroy be pushed to the UI thread if necessary. Also make the work that is delayed be static without references to the finalizing WebView to allow it to be fully deleted immediately after finalization. Change-Id: I4e424051e69df0bc409af95ca3f3d2b9e58a6b75
1 parent 4c38fe3 commit d5e2937

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

core/java/android/webkit/WebViewClassic.java

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,31 +2065,31 @@ private void cancelSelectDialog() {
20652065
*/
20662066
@Override
20672067
public void destroy() {
2068-
destroyImpl();
2069-
}
2070-
2071-
private void destroyImpl() {
2072-
ViewRootImpl viewRoot = mWebView.getViewRootImpl();
2073-
if (viewRoot != null) {
2068+
if (mWebView.getViewRootImpl() != null) {
20742069
Log.e(LOGTAG, "Error: WebView.destroy() called while still attached!");
20752070
}
2071+
ensureFunctorDetached();
2072+
destroyJava();
2073+
destroyNative();
2074+
}
20762075

2076+
private void ensureFunctorDetached() {
20772077
if (mWebView.isHardwareAccelerated()) {
20782078
int drawGLFunction = nativeGetDrawGLFunction(mNativeClass);
2079+
ViewRootImpl viewRoot = mWebView.getViewRootImpl();
20792080
if (drawGLFunction != 0 && viewRoot != null) {
2080-
// functor should have been detached in onDetachedFromWindow, do
2081-
// additionally here for safety
20822081
viewRoot.detachFunctor(drawGLFunction);
20832082
}
20842083
}
2084+
}
20852085

2086+
private void destroyJava() {
20862087
mCallbackProxy.blockMessages();
20872088
clearHelpers();
20882089
if (mListBoxDialog != null) {
20892090
mListBoxDialog.dismiss();
20902091
mListBoxDialog = null;
20912092
}
2092-
if (mNativeClass != 0) nativeStopGL();
20932093
if (mWebViewCore != null) {
20942094
// Tell WebViewCore to destroy itself
20952095
synchronized (this) {
@@ -2100,12 +2100,36 @@ private void destroyImpl() {
21002100
// Remove any pending messages that might not be serviced yet.
21012101
mPrivateHandler.removeCallbacksAndMessages(null);
21022102
}
2103-
if (mNativeClass != 0) {
2104-
nativeDestroy();
2105-
mNativeClass = 0;
2103+
}
2104+
2105+
private void destroyNative() {
2106+
if (mNativeClass == 0) return;
2107+
int nptr = mNativeClass;
2108+
mNativeClass = 0;
2109+
if (Thread.currentThread() == mPrivateHandler.getLooper().getThread()) {
2110+
// We are on the main thread and can safely delete
2111+
nativeDestroy(nptr);
2112+
} else {
2113+
mPrivateHandler.post(new DestroyNativeRunnable(nptr));
21062114
}
21072115
}
21082116

2117+
private static class DestroyNativeRunnable implements Runnable {
2118+
2119+
private int mNativePtr;
2120+
2121+
public DestroyNativeRunnable(int nativePtr) {
2122+
mNativePtr = nativePtr;
2123+
}
2124+
2125+
@Override
2126+
public void run() {
2127+
// nativeDestroy also does a stopGL()
2128+
nativeDestroy(mNativePtr);
2129+
}
2130+
2131+
}
2132+
21092133
/**
21102134
* See {@link WebView#enablePlatformNotifications()}
21112135
*/
@@ -4099,14 +4123,7 @@ public void refreshPlugins(boolean reloadOpenPages) {
40994123
@Override
41004124
protected void finalize() throws Throwable {
41014125
try {
4102-
if (mNativeClass != 0) {
4103-
mPrivateHandler.post(new Runnable() {
4104-
@Override
4105-
public void run() {
4106-
destroy();
4107-
}
4108-
});
4109-
}
4126+
destroy();
41104127
} finally {
41114128
super.finalize();
41124129
}
@@ -5313,13 +5330,7 @@ public void onDetachedFromWindow() {
53135330

53145331
updateHwAccelerated();
53155332

5316-
if (mWebView.isHardwareAccelerated()) {
5317-
int drawGLFunction = nativeGetDrawGLFunction(mNativeClass);
5318-
ViewRootImpl viewRoot = mWebView.getViewRootImpl();
5319-
if (drawGLFunction != 0 && viewRoot != null) {
5320-
viewRoot.detachFunctor(drawGLFunction);
5321-
}
5322-
}
5333+
ensureFunctorDetached();
53235334
}
53245335

53255336
@Override
@@ -8507,7 +8518,7 @@ public static void setShouldMonitorWebCoreThread() {
85078518

85088519
private native void nativeCreate(int ptr, String drawableDir, boolean isHighEndGfx);
85098520
private native void nativeDebugDump();
8510-
private native void nativeDestroy();
8521+
private static native void nativeDestroy(int ptr);
85118522

85128523
private native void nativeDraw(Canvas canvas, RectF visibleRect,
85138524
int color, int extra);
@@ -8526,7 +8537,7 @@ private native boolean nativeSetBaseLayer(int nativeInstance,
85268537
private native int nativeGetBaseLayer(int nativeInstance);
85278538
private native void nativeCopyBaseContentToPicture(Picture pict);
85288539
private native boolean nativeHasContent();
8529-
private native void nativeStopGL();
8540+
private native void nativeStopGL(int ptr);
85308541
private native void nativeDiscardAllTextures();
85318542
private native void nativeTileProfilingStart();
85328543
private native float nativeTileProfilingStop();

0 commit comments

Comments
 (0)