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

Commit 6479df76 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Fix the context leak in the ContentCapture" into rvc-dev am: 44b233c5 am: 17ad352d

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/11882491

Change-Id: I19143c9ad81f891646634237d0c35f14be06bab1
parents d861765f 17ad352d
Loading
Loading
Loading
Loading
+45 −27
Original line number Original line Diff line number Diff line
@@ -52,6 +52,7 @@ import android.view.contentcapture.ViewNode.ViewStructureImpl;
import com.android.internal.os.IResultReceiver;
import com.android.internal.os.IResultReceiver;


import java.io.PrintWriter;
import java.io.PrintWriter;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Collections;
import java.util.List;
import java.util.List;
@@ -146,45 +147,57 @@ public final class MainContentCaptureSession extends ContentCaptureSession {
     * Binder object used to update the session state.
     * Binder object used to update the session state.
     */
     */
    @NonNull
    @NonNull
    private final IResultReceiver.Stub mSessionStateReceiver;
    private final SessionStateReceiver mSessionStateReceiver;


    protected MainContentCaptureSession(@NonNull Context context,
    private static class SessionStateReceiver extends IResultReceiver.Stub {
            @NonNull ContentCaptureManager manager, @NonNull Handler handler,
        private final WeakReference<MainContentCaptureSession> mMainSession;
            @NonNull IContentCaptureManager systemServerInterface) {
        mContext = context;
        mManager = manager;
        mHandler = handler;
        mSystemServerInterface = systemServerInterface;


        final int logHistorySize = mManager.mOptions.logHistorySize;
        SessionStateReceiver(MainContentCaptureSession session) {
        mFlushHistory = logHistorySize > 0 ? new LocalLog(logHistorySize) : null;
            mMainSession = new WeakReference<>(session);
        }


        mSessionStateReceiver = new IResultReceiver.Stub() {
        @Override
        @Override
        public void send(int resultCode, Bundle resultData) {
        public void send(int resultCode, Bundle resultData) {
            final MainContentCaptureSession mainSession = mMainSession.get();
            if (mainSession == null) {
                Log.w(TAG, "received result after mina session released");
                return;
            }
            final IBinder binder;
            final IBinder binder;
            if (resultData != null) {
            if (resultData != null) {
                // Change in content capture enabled.
                // Change in content capture enabled.
                final boolean hasEnabled = resultData.getBoolean(EXTRA_ENABLED_STATE);
                final boolean hasEnabled = resultData.getBoolean(EXTRA_ENABLED_STATE);
                if (hasEnabled) {
                if (hasEnabled) {
                    final boolean disabled = (resultCode == RESULT_CODE_FALSE);
                    final boolean disabled = (resultCode == RESULT_CODE_FALSE);
                        mDisabled.set(disabled);
                    mainSession.mDisabled.set(disabled);
                    return;
                    return;
                }
                }
                binder = resultData.getBinder(EXTRA_BINDER);
                binder = resultData.getBinder(EXTRA_BINDER);
                if (binder == null) {
                if (binder == null) {
                    Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result");
                    Log.wtf(TAG, "No " + EXTRA_BINDER + " extra result");
                        mHandler.post(() -> resetSession(
                    mainSession.mHandler.post(() -> mainSession.resetSession(
                            STATE_DISABLED | STATE_INTERNAL_ERROR));
                            STATE_DISABLED | STATE_INTERNAL_ERROR));
                    return;
                    return;
                }
                }
            } else {
            } else {
                binder = null;
                binder = null;
            }
            }
                mHandler.post(() -> onSessionStarted(resultCode, binder));
            mainSession.mHandler.post(() -> mainSession.onSessionStarted(resultCode, binder));
        }
        }
        };
    }

    protected MainContentCaptureSession(@NonNull Context context,
            @NonNull ContentCaptureManager manager, @NonNull Handler handler,
            @NonNull IContentCaptureManager systemServerInterface) {
        mContext = context;
        mManager = manager;
        mHandler = handler;
        mSystemServerInterface = systemServerInterface;


        final int logHistorySize = mManager.mOptions.logHistorySize;
        mFlushHistory = logHistorySize > 0 ? new LocalLog(logHistorySize) : null;

        mSessionStateReceiver = new SessionStateReceiver(this);
    }
    }


    @Override
    @Override
@@ -543,6 +556,11 @@ public final class MainContentCaptureSession extends ContentCaptureSession {
            Log.e(TAG, "Error destroying system-service session " + mId + " for "
            Log.e(TAG, "Error destroying system-service session " + mId + " for "
                    + getDebugState() + ": " + e);
                    + getDebugState() + ": " + e);
        }
        }

        if (mDirectServiceInterface != null) {
            mDirectServiceInterface.asBinder().unlinkToDeath(mDirectServiceVulture, 0);
        }
        mDirectServiceInterface = null;
    }
    }


    // TODO(b/122454205): once we support multiple sessions, we might need to move some of these
    // TODO(b/122454205): once we support multiple sessions, we might need to move some of these