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

Commit a6646529 authored by Haoran Zhang's avatar Haoran Zhang Committed by Android (Google) Code Review
Browse files

Merge "Fix Memory Leak: Part II of rewrite render info callback" into main

parents 3df0f3ae 274343db
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.autofill;

import android.util.Slog;
import android.view.inputmethod.InlineSuggestionsRequest;

import java.lang.ref.WeakReference;
import java.util.function.Consumer;

class InlineSuggestionRequestConsumer implements Consumer<InlineSuggestionsRequest> {

    static final String TAG = "InlineSuggestionRequestConsumer";

    private final WeakReference<Session.AssistDataReceiverImpl> mAssistDataReceiverWeakReference;
    private final WeakReference<ViewState>  mViewStateWeakReference;

    InlineSuggestionRequestConsumer(WeakReference<Session.AssistDataReceiverImpl>
            assistDataReceiverWeakReference,
            WeakReference<ViewState>  viewStateWeakReference) {
        mAssistDataReceiverWeakReference = assistDataReceiverWeakReference;
        mViewStateWeakReference = viewStateWeakReference;
    }

    @Override
    public void accept(InlineSuggestionsRequest inlineSuggestionsRequest) {
        Session.AssistDataReceiverImpl assistDataReceiver = mAssistDataReceiverWeakReference.get();
        ViewState viewState = mViewStateWeakReference.get();
        if (assistDataReceiver == null) {
            Slog.wtf(TAG, "assistDataReceiver is null when accepting new inline suggestion"
                    + "requests");
            return;
        }

        if (viewState == null) {
            Slog.wtf(TAG, "view state is null when accepting new inline suggestion requests");
            return;
        }
        assistDataReceiver.handleInlineSuggestionRequest(inlineSuggestionsRequest, viewState);
    }
}
+23 −13
Original line number Diff line number Diff line
@@ -326,7 +326,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
     * Id of the View currently being displayed.
     */
    @GuardedBy("mLock")
    @Nullable AutofillId mCurrentViewId;
    private @Nullable AutofillId mCurrentViewId;

    @GuardedBy("mLock")
    private IAutoFillManagerClient mClient;
@@ -623,7 +623,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
     * TODO(b/151867668): improve how asynchronous data dependencies are handled, without using
     * CountDownLatch.
     */
    private final class AssistDataReceiverImpl extends IAssistDataReceiver.Stub {
    final class AssistDataReceiverImpl extends IAssistDataReceiver.Stub {
        @GuardedBy("mLock")
        private boolean mWaitForInlineRequest;
        @GuardedBy("mLock")
@@ -638,18 +638,28 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
            mPendingFillRequest = null;
            mWaitForInlineRequest = isInlineRequest;
            mPendingInlineSuggestionsRequest = null;
            return isInlineRequest ? (inlineSuggestionsRequest) -> {
            if (isInlineRequest) {
                WeakReference<AssistDataReceiverImpl> assistDataReceiverWeakReference =
                    new WeakReference<AssistDataReceiverImpl>(this);
                WeakReference<ViewState> viewStateWeakReference =
                    new WeakReference<ViewState>(viewState);
                return new InlineSuggestionRequestConsumer(assistDataReceiverWeakReference,
                    viewStateWeakReference);
            }
            return null;
        }

        void handleInlineSuggestionRequest(InlineSuggestionsRequest inlineSuggestionsRequest,
                ViewState viewState) {
            synchronized (mLock) {
                if (!mWaitForInlineRequest || mPendingInlineSuggestionsRequest != null) {
                    return;
                }
                mWaitForInlineRequest = inlineSuggestionsRequest != null;
                mPendingInlineSuggestionsRequest = inlineSuggestionsRequest;
                    mWaitForInlineRequest = inlineSuggestionsRequest != null;
                maybeRequestFillFromServiceLocked();
                viewState.resetState(ViewState.STATE_PENDING_CREATE_INLINE_REQUEST);
            }
            } : null;
        }

        void newAutofillRequestLocked(@Nullable InlineSuggestionsRequest inlineRequest) {