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

Commit 49ca81b0 authored by Adam He's avatar Adam He
Browse files

Rename session ending method calls to use consistent names.

* Session contains removeFromService() and destroy().
removeFromService() removes the session and also calls destroy()
* AFManagerServiceImpl has renamed methods to remove*Sessions() since
it's actually calling into removeFromService instead of just destroy().

BYPASS_INCLUSIVE_LANGUAGE_REASON=will_do_in_separate_cl

Bug: 162357598
Test: atest CtsAutoFillServiceTestCases
Change-Id: I4ddc4b863b2f3c962408dc0e54f8be5b34ee32b4
parent b39ce193
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -84,7 +84,6 @@ import com.android.internal.util.Preconditions;
import com.android.internal.util.SyncResultReceiver;
import com.android.server.FgThread;
import com.android.server.LocalServices;
import com.android.server.SystemService.TargetUser;
import com.android.server.autofill.ui.AutoFillUI;
import com.android.server.infra.AbstractMasterSystemService;
import com.android.server.infra.FrameworkResourcesServiceNameResolver;
@@ -170,7 +169,7 @@ public final class AutofillManagerService
                // beneath it is brought back to top. Ideally, we should just hide the UI and
                // bring it back when the activity resumes.
                synchronized (mLock) {
                    visitServicesLocked((s) -> s.destroyFinishedSessionsLocked());
                    visitServicesLocked((s) -> s.forceRemoveFinishedSessionsLocked());
                }
                mUi.hideAll(null);
            }
@@ -386,18 +385,18 @@ public final class AutofillManagerService
    }

    // Called by Shell command.
    void destroySessions(@UserIdInt int userId, IResultReceiver receiver) {
        Slog.i(TAG, "destroySessions() for userId " + userId);
    void removeAllSessions(@UserIdInt int userId, IResultReceiver receiver) {
        Slog.i(TAG, "removeAllSessions() for userId " + userId);
        enforceCallingPermissionForManagement();

        synchronized (mLock) {
            if (userId != UserHandle.USER_ALL) {
                AutofillManagerServiceImpl service = peekServiceForUserLocked(userId);
                if (service != null) {
                    service.destroySessionsLocked();
                    service.forceRemoveAllSessionsLocked();
                }
            } else {
                visitServicesLocked((s) -> s.destroySessionsLocked());
                visitServicesLocked((s) -> s.forceRemoveAllSessionsLocked());
            }
        }

+23 −17
Original line number Diff line number Diff line
@@ -215,14 +215,14 @@ final class AutofillManagerServiceImpl
    @GuardedBy("mLock")
    @Override // from PerUserSystemService
    protected boolean updateLocked(boolean disabled) {
        destroySessionsLocked();
        forceRemoveAllSessionsLocked();
        final boolean enabledChanged = super.updateLocked(disabled);
        if (enabledChanged) {
            if (!isEnabledLocked()) {
                final int sessionCount = mSessions.size();
                for (int i = sessionCount - 1; i >= 0; i--) {
                    final Session session = mSessions.valueAt(i);
                    session.removeSelfLocked();
                    session.removeFromServiceLocked();
                }
            }
            sendStateToClients(/* resetClient= */ false);
@@ -442,7 +442,7 @@ final class AutofillManagerServiceImpl
        if (sVerbose) Slog.v(TAG, "finishSessionLocked(): session finished on save? " + finished);

        if (finished) {
            session.removeSelfLocked();
            session.removeFromServiceLocked();
        }
    }

@@ -457,7 +457,7 @@ final class AutofillManagerServiceImpl
            Slog.w(TAG, "cancelSessionLocked(): no session for " + sessionId + "(" + uid + ")");
            return;
        }
        session.removeSelfLocked();
        session.removeFromServiceLocked();
    }

    @GuardedBy("mLock")
@@ -483,7 +483,7 @@ final class AutofillManagerServiceImpl
                        componentName.getPackageName());
                Settings.Secure.putStringForUser(getContext().getContentResolver(),
                        Settings.Secure.AUTOFILL_SERVICE, null, mUserId);
                destroySessionsLocked();
                forceRemoveAllSessionsLocked();
            } else {
                Slog.w(TAG, "disableOwnedServices(): ignored because current service ("
                        + serviceInfo + ") does not match Settings (" + autoFillService + ")");
@@ -1107,35 +1107,41 @@ final class AutofillManagerServiceImpl
    }

    @GuardedBy("mLock")
    void destroySessionsLocked() {
        if (mSessions.size() == 0) {
    void forceRemoveAllSessionsLocked() {
        final int sessionCount = mSessions.size();
        if (sessionCount == 0) {
            mUi.destroyAll(null, null, false);
            return;
        }
        while (mSessions.size() > 0) {
            mSessions.valueAt(0).forceRemoveSelfLocked();

        for (int i = sessionCount - 1; i >= 0; i--) {
            mSessions.valueAt(i).forceRemoveFromServiceLocked();
        }
    }

    @GuardedBy("mLock")
    void destroySessionsForAugmentedAutofillOnlyLocked() {
    void forceRemoveForAugmentedOnlySessionsLocked() {
        final int sessionCount = mSessions.size();
        for (int i = sessionCount - 1; i >= 0; i--) {
            mSessions.valueAt(i).forceRemoveSelfIfForAugmentedAutofillOnlyLocked();
            mSessions.valueAt(i).forceRemoveFromServiceIfForAugmentedOnlyLocked();
        }
    }

    /**
     * This method is called exclusively in response to {@code Intent.ACTION_CLOSE_SYSTEM_DIALOGS}.
     * The method removes all sessions that are finished but showing SaveUI due to how SaveUI is
     * managed (see b/64940307). Otherwise it will remove any augmented autofill generated windows.
     */
    // TODO(b/64940307): remove this method if SaveUI is refactored to be attached on activities
    @GuardedBy("mLock")
    void destroyFinishedSessionsLocked() {
    void forceRemoveFinishedSessionsLocked() {
        final int sessionCount = mSessions.size();
        for (int i = sessionCount - 1; i >= 0; i--) {
            final Session session = mSessions.valueAt(i);
            if (session.isSavingLocked()) {
                if (sDebug) Slog.d(TAG, "destroyFinishedSessionsLocked(): " + session.id);
                session.forceRemoveSelfLocked();
            }
            else {
                session.forceRemoveFromServiceLocked();
            } else {
                session.destroyAugmentedAutofillWindowsLocked();
            }
        }
@@ -1261,7 +1267,7 @@ final class AutofillManagerServiceImpl
                    Slog.v(TAG, "updateRemoteAugmentedAutofillService(): "
                            + "destroying old remote service");
                }
                destroySessionsForAugmentedAutofillOnlyLocked();
                forceRemoveForAugmentedOnlySessionsLocked();
                mRemoteAugmentedAutofillService.unbind();
                mRemoteAugmentedAutofillService = null;
                mRemoteAugmentedAutofillServiceInfo = null;
@@ -1663,7 +1669,7 @@ final class AutofillManagerServiceImpl
                                Slog.i(TAG, "Prune session " + sessionToRemove.id + " ("
                                    + sessionToRemove.getActivityTokenLocked() + ")");
                            }
                            sessionToRemove.removeSelfLocked();
                            sessionToRemove.removeFromServiceLocked();
                        }
                    }
                }
+1 −1
Original line number Diff line number Diff line
@@ -355,7 +355,7 @@ public final class AutofillManagerServiceShellCommand extends ShellCommand {
                latch.countDown();
            }
        };
        return requestSessionCommon(pw, latch, () -> mService.destroySessions(userId, receiver));
        return requestSessionCommon(pw, latch, () -> mService.removeAllSessions(userId, receiver));
    }

    private int requestList(PrintWriter pw) {
+50 −50
Original line number Diff line number Diff line
@@ -226,8 +226,8 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
    private boolean mHasCallback;

    /**
     * Extras sent by service on {@code onFillRequest()} calls; the first non-null extra is saved
     * and used on subsequent {@code onFillRequest()} and {@code onSaveRequest()} calls.
     * Extras sent by service on {@code onFillRequest()} calls; the most recent non-null extra is
     * saved and used on subsequent {@code onFillRequest()} and {@code onSaveRequest()} calls.
     */
    @GuardedBy("mLock")
    private Bundle mClientState;
@@ -1085,7 +1085,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
        if (showMessage) {
            getUiForShowing().showError(message, this);
        }
        removeSelf();
        removeFromService();
    }

    // FillServiceCallbacks
@@ -1110,7 +1110,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
        }

        // Nothing left to do...
        removeSelf();
        removeFromService();
    }

    // FillServiceCallbacks
@@ -1146,7 +1146,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
        if (showMessage) {
            getUiForShowing().showError(message, this);
        }
        removeSelf();
        removeFromService();
    }

    /**
@@ -1178,7 +1178,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
    @Override
    public void onServiceDied(@NonNull RemoteFillService service) {
        Slog.w(TAG, "removing session because service died");
        forceRemoveSelfLocked();
        forceRemoveFromServiceLocked();
    }

    // AutoFillUiCallback
@@ -1198,7 +1198,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
            }
            fillInIntent = createAuthFillInIntentLocked(requestId, extras);
            if (fillInIntent == null) {
                forceRemoveSelfLocked();
                forceRemoveFromServiceLocked();
                return;
            }
        }
@@ -1254,7 +1254,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
            }
        }
        mHandler.sendMessage(obtainMessage(
                Session::removeSelf, this));
                Session::removeFromService, this));
    }

    // AutoFillUiCallback
@@ -1326,7 +1326,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
    @Override
    public void cancelSession() {
        synchronized (mLock) {
            removeSelfLocked();
            removeFromServiceLocked();
        }
    }

@@ -1346,7 +1346,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
                return;
            }
            if (intent == null) {
                removeSelfLocked();
                removeFromServiceLocked();
            }
        }
        mHandler.sendMessage(obtainMessage(
@@ -1400,13 +1400,13 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
            // Typically happens when app explicitly called cancel() while the service was showing
            // the auth UI.
            Slog.w(TAG, "setAuthenticationResultLocked(" + authenticationId + "): no responses");
            removeSelf();
            removeFromService();
            return;
        }
        final FillResponse authenticatedResponse = mResponses.get(requestId);
        if (authenticatedResponse == null || data == null) {
            Slog.w(TAG, "no authenticated response");
            removeSelf();
            removeFromService();
            return;
        }

@@ -1417,7 +1417,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
            final Dataset dataset = authenticatedResponse.getDatasets().get(datasetIdx);
            if (dataset == null) {
                Slog.w(TAG, "no dataset with index " + datasetIdx + " on fill response");
                removeSelf();
                removeFromService();
                return;
            }
        }
@@ -1502,7 +1502,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
                Slog.d(TAG, "Rejecting empty/invalid auth result");
            }
            mService.resetLastAugmentedAutofillResponse();
            removeSelfLocked();
            removeFromServiceLocked();
            return;
        }
        final List<AutofillId> fieldIds = dataset.getFieldIds();
@@ -2712,7 +2712,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
                        return;
                    }
                    if (sDebug) Slog.d(TAG, "Finishing session because URL bar changed");
                    forceRemoveSelfLocked(AutofillManager.STATE_UNKNOWN_COMPAT_MODE);
                    forceRemoveFromServiceLocked(AutofillManager.STATE_UNKNOWN_COMPAT_MODE);
                    return;
                }
                if (!Objects.equals(value, viewState.getCurrentValue())) {
@@ -3223,7 +3223,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
            }
            // Nothing to be done, but need to notify client.
            notifyUnavailableToClient(AutofillManager.STATE_FINISHED, autofillableIds);
            removeSelf();
            removeFromService();
        } else {
            if ((flags & FLAG_PASSWORD_INPUT_TYPE) != 0) {
                if (sVerbose) {
@@ -3389,20 +3389,6 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
        return mAugmentedAutofillDestroyer;
    }

    @GuardedBy("mLock")
    private void logAugmentedAutofillRequestLocked(int mode,
            ComponentName augmentedRemoteServiceName, AutofillId focusedId, boolean isWhitelisted,
            Boolean isInline) {
        final String historyItem =
                "aug:id=" + id + " u=" + uid + " m=" + mode
                        + " a=" + ComponentName.flattenToShortString(mComponentName)
                        + " f=" + focusedId
                        + " s=" + augmentedRemoteServiceName
                        + " w=" + isWhitelisted
                        + " i=" + isInline;
        mService.getMaster().logRequestLocked(historyItem);
    }

    @GuardedBy("mLock")
    private void cancelAugmentedAutofillLocked() {
        final RemoteAugmentedAutofillService remoteService = mService
@@ -3571,7 +3557,7 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
            setViewStatesLocked(null, dataset, ViewState.STATE_WAITING_DATASET_AUTH, false);
            final Intent fillInIntent = createAuthFillInIntentLocked(requestId, mClientState);
            if (fillInIntent == null) {
                forceRemoveSelfLocked();
                forceRemoveFromServiceLocked();
                return;
            }
            final int authenticationId = AutofillManager.makeAuthenticationId(requestId,
@@ -3920,12 +3906,12 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
    }

    /**
     * Cleans up this session.
     * Destroy this session and perform any clean up work.
     *
     * <p>Typically called in 2 scenarios:
     *
     * <ul>
     *   <li>When the session naturally finishes (i.e., from {@link #removeSelfLocked()}.
     *   <li>When the session naturally finishes (i.e., from {@link #removeFromServiceLocked()}.
     *   <li>When the service hosting the session is finished (for example, because the user
     *       disabled it).
     * </ul>
@@ -3987,32 +3973,32 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
    }

    /**
     * Cleans up this session and remove it from the service always, even if it does have a pending
     * Destroy this session and remove it from the service always, even if it does have a pending
     * Save UI.
     */
    @GuardedBy("mLock")
    void forceRemoveSelfLocked() {
        forceRemoveSelfLocked(AutofillManager.STATE_UNKNOWN);
    void forceRemoveFromServiceLocked() {
        forceRemoveFromServiceLocked(AutofillManager.STATE_UNKNOWN);
    }

    @GuardedBy("mLock")
    void forceRemoveSelfIfForAugmentedAutofillOnlyLocked() {
    void forceRemoveFromServiceIfForAugmentedOnlyLocked() {
        if (sVerbose) {
            Slog.v(TAG, "forceRemoveSelfIfForAugmentedAutofillOnly(" + this.id + "): "
            Slog.v(TAG, "forceRemoveFromServiceIfForAugmentedOnlyLocked(" + this.id + "): "
                    + mForAugmentedAutofillOnly);
        }
        if (!mForAugmentedAutofillOnly) return;

        forceRemoveSelfLocked();
        forceRemoveFromServiceLocked();
    }

    @GuardedBy("mLock")
    void forceRemoveSelfLocked(int clientState) {
        if (sVerbose) Slog.v(TAG, "forceRemoveSelfLocked(): " + mPendingSaveUi);
    void forceRemoveFromServiceLocked(int clientState) {
        if (sVerbose) Slog.v(TAG, "forceRemoveFromServiceLocked(): " + mPendingSaveUi);

        final boolean isPendingSaveUi = isSaveUiPendingLocked();
        mPendingSaveUi = null;
        removeSelfLocked();
        removeFromServiceLocked();
        mUi.destroyAll(mPendingSaveUi, this, false);
        if (!isPendingSaveUi) {
            try {
@@ -4033,28 +4019,28 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
    }

    /**
     * Thread-safe version of {@link #removeSelfLocked()}.
     * Thread-safe version of {@link #removeFromServiceLocked()}.
     */
    private void removeSelf() {
    private void removeFromService() {
        synchronized (mLock) {
            removeSelfLocked();
            removeFromServiceLocked();
        }
    }

    /**
     * Cleans up this session and remove it from the service, but but only if it does not have a
     * Destroy this session and remove it from the service, but but only if it does not have a
     * pending Save UI.
     */
    @GuardedBy("mLock")
    void removeSelfLocked() {
        if (sVerbose) Slog.v(TAG, "removeSelfLocked(" + this.id + "): " + mPendingSaveUi);
    void removeFromServiceLocked() {
        if (sVerbose) Slog.v(TAG, "removeFromServiceLocked(" + this.id + "): " + mPendingSaveUi);
        if (mDestroyed) {
            Slog.w(TAG, "Call to Session#removeSelfLocked() rejected - session: "
            Slog.w(TAG, "Call to Session#removeFromServiceLocked() rejected - session: "
                    + id + " destroyed");
            return;
        }
        if (isSaveUiPendingLocked()) {
            Slog.i(TAG, "removeSelfLocked() ignored, waiting for pending save ui");
            Slog.i(TAG, "removeFromServiceLocked() ignored, waiting for pending save ui");
            return;
        }

@@ -4136,6 +4122,20 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
        requestLog.addTaggedData(tag, value);
    }

    @GuardedBy("mLock")
    private void logAugmentedAutofillRequestLocked(int mode,
            ComponentName augmentedRemoteServiceName, AutofillId focusedId, boolean isWhitelisted,
            Boolean isInline) {
        final String historyItem =
                "aug:id=" + id + " u=" + uid + " m=" + mode
                        + " a=" + ComponentName.flattenToShortString(mComponentName)
                        + " f=" + focusedId
                        + " s=" + augmentedRemoteServiceName
                        + " w=" + isWhitelisted
                        + " i=" + isInline;
        mService.getMaster().logRequestLocked(historyItem);
    }

    private void wtf(@Nullable Exception e, String fmt, Object...args) {
        final String message = String.format(fmt, args);
        synchronized (mLock) {