Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d5e29379 authored by John Reck's avatar John Reck
Browse files

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
parent 4c38fe36
Loading
Loading
Loading
Loading
+40 −29
Original line number Diff line number Diff line
@@ -2065,31 +2065,31 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc
     */
    @Override
    public void destroy() {
        destroyImpl();
    }

    private void destroyImpl() {
        ViewRootImpl viewRoot = mWebView.getViewRootImpl();
        if (viewRoot != null) {
        if (mWebView.getViewRootImpl() != null) {
            Log.e(LOGTAG, "Error: WebView.destroy() called while still attached!");
        }
        ensureFunctorDetached();
        destroyJava();
        destroyNative();
    }

    private void ensureFunctorDetached() {
        if (mWebView.isHardwareAccelerated()) {
            int drawGLFunction = nativeGetDrawGLFunction(mNativeClass);
            ViewRootImpl viewRoot = mWebView.getViewRootImpl();
            if (drawGLFunction != 0 && viewRoot != null) {
                // functor should have been detached in onDetachedFromWindow, do
                // additionally here for safety
                viewRoot.detachFunctor(drawGLFunction);
            }
        }
    }

    private void destroyJava() {
        mCallbackProxy.blockMessages();
        clearHelpers();
        if (mListBoxDialog != null) {
            mListBoxDialog.dismiss();
            mListBoxDialog = null;
        }
        if (mNativeClass != 0) nativeStopGL();
        if (mWebViewCore != null) {
            // Tell WebViewCore to destroy itself
            synchronized (this) {
@@ -2100,12 +2100,36 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc
            // Remove any pending messages that might not be serviced yet.
            mPrivateHandler.removeCallbacksAndMessages(null);
        }
        if (mNativeClass != 0) {
            nativeDestroy();
    }

    private void destroyNative() {
        if (mNativeClass == 0) return;
        int nptr = mNativeClass;
        mNativeClass = 0;
        if (Thread.currentThread() == mPrivateHandler.getLooper().getThread()) {
            // We are on the main thread and can safely delete
            nativeDestroy(nptr);
        } else {
            mPrivateHandler.post(new DestroyNativeRunnable(nptr));
        }
    }

    private static class DestroyNativeRunnable implements Runnable {

        private int mNativePtr;

        public DestroyNativeRunnable(int nativePtr) {
            mNativePtr = nativePtr;
        }

        @Override
        public void run() {
            // nativeDestroy also does a stopGL()
            nativeDestroy(mNativePtr);
        }

    }

    /**
     * See {@link WebView#enablePlatformNotifications()}
     */
@@ -4099,14 +4123,7 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc
    @Override
    protected void finalize() throws Throwable {
        try {
            if (mNativeClass != 0) {
                mPrivateHandler.post(new Runnable() {
                    @Override
                    public void run() {
            destroy();
                    }
                });
            }
        } finally {
            super.finalize();
        }
@@ -5313,13 +5330,7 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc

        updateHwAccelerated();

        if (mWebView.isHardwareAccelerated()) {
            int drawGLFunction = nativeGetDrawGLFunction(mNativeClass);
            ViewRootImpl viewRoot = mWebView.getViewRootImpl();
            if (drawGLFunction != 0 && viewRoot != null) {
                viewRoot.detachFunctor(drawGLFunction);
            }
        }
        ensureFunctorDetached();
    }

    @Override
@@ -8507,7 +8518,7 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc

    private native void     nativeCreate(int ptr, String drawableDir, boolean isHighEndGfx);
    private native void     nativeDebugDump();
    private native void     nativeDestroy();
    private static native void nativeDestroy(int ptr);

    private native void nativeDraw(Canvas canvas, RectF visibleRect,
            int color, int extra);
@@ -8526,7 +8537,7 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc
    private native int      nativeGetBaseLayer(int nativeInstance);
    private native void     nativeCopyBaseContentToPicture(Picture pict);
    private native boolean  nativeHasContent();
    private native void     nativeStopGL();
    private native void     nativeStopGL(int ptr);
    private native void     nativeDiscardAllTextures();
    private native void     nativeTileProfilingStart();
    private native float    nativeTileProfilingStop();