Merge "[Autofill] Fix memory leak" into udc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
5734096da8
@@ -4857,16 +4857,16 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
final boolean isWhitelisted = mService
|
final boolean isAllowlisted = mService
|
||||||
.isWhitelistedForAugmentedAutofillLocked(mComponentName);
|
.isWhitelistedForAugmentedAutofillLocked(mComponentName);
|
||||||
|
|
||||||
if (!isWhitelisted) {
|
if (!isAllowlisted) {
|
||||||
if (sVerbose) {
|
if (sVerbose) {
|
||||||
Slog.v(TAG, "triggerAugmentedAutofillLocked(): "
|
Slog.v(TAG, "triggerAugmentedAutofillLocked(): "
|
||||||
+ ComponentName.flattenToShortString(mComponentName) + " not whitelisted ");
|
+ ComponentName.flattenToShortString(mComponentName) + " not whitelisted ");
|
||||||
}
|
}
|
||||||
logAugmentedAutofillRequestLocked(mode, remoteService.getComponentName(),
|
logAugmentedAutofillRequestLocked(mode, remoteService.getComponentName(),
|
||||||
mCurrentViewId, isWhitelisted, /* isInline= */ null);
|
mCurrentViewId, isAllowlisted, /* isInline= */ null);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4899,32 +4899,9 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
|
|||||||
|
|
||||||
final AutofillId focusedId = mCurrentViewId;
|
final AutofillId focusedId = mCurrentViewId;
|
||||||
|
|
||||||
final Function<InlineFillUi, Boolean> inlineSuggestionsResponseCallback =
|
|
||||||
response -> {
|
|
||||||
synchronized (mLock) {
|
|
||||||
return mInlineSessionController.setInlineFillUiLocked(response);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
final Consumer<InlineSuggestionsRequest> requestAugmentedAutofill =
|
final Consumer<InlineSuggestionsRequest> requestAugmentedAutofill =
|
||||||
(inlineSuggestionsRequest) -> {
|
new AugmentedAutofillInlineSuggestionRequestConsumer(
|
||||||
synchronized (mLock) {
|
this, focusedId, isAllowlisted, mode, currentValue);
|
||||||
logAugmentedAutofillRequestLocked(mode, remoteService.getComponentName(),
|
|
||||||
focusedId, isWhitelisted, inlineSuggestionsRequest != null);
|
|
||||||
remoteService.onRequestAutofillLocked(id, mClient,
|
|
||||||
taskId, mComponentName, mActivityToken,
|
|
||||||
AutofillId.withoutSession(focusedId), currentValue,
|
|
||||||
inlineSuggestionsRequest, inlineSuggestionsResponseCallback,
|
|
||||||
/*onErrorCallback=*/ () -> {
|
|
||||||
synchronized (mLock) {
|
|
||||||
cancelAugmentedAutofillLocked();
|
|
||||||
|
|
||||||
// Also cancel augmented in IME
|
|
||||||
mInlineSessionController.setInlineFillUiLocked(
|
|
||||||
InlineFillUi.emptyUi(mCurrentViewId));
|
|
||||||
}
|
|
||||||
}, mService.getRemoteInlineSuggestionRenderServiceLocked(), userId);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// When the inline suggestion render service is available and the view is focused, there
|
// When the inline suggestion render service is available and the view is focused, there
|
||||||
// are 3 cases when augmented autofill should ask IME for inline suggestion request,
|
// are 3 cases when augmented autofill should ask IME for inline suggestion request,
|
||||||
@@ -4942,14 +4919,11 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
|
|||||||
|| mSessionFlags.mExpiredResponse)
|
|| mSessionFlags.mExpiredResponse)
|
||||||
&& (isViewFocusedLocked(flags) || isRequestSupportFillDialog(flags))) {
|
&& (isViewFocusedLocked(flags) || isRequestSupportFillDialog(flags))) {
|
||||||
if (sDebug) Slog.d(TAG, "Create inline request for augmented autofill");
|
if (sDebug) Slog.d(TAG, "Create inline request for augmented autofill");
|
||||||
remoteRenderService.getInlineSuggestionsRendererInfo(new RemoteCallback(
|
remoteRenderService.getInlineSuggestionsRendererInfo(
|
||||||
(extras) -> {
|
new RemoteCallback(
|
||||||
synchronized (mLock) {
|
new AugmentedAutofillInlineSuggestionRendererOnResultListener(
|
||||||
mInlineSessionController.onCreateInlineSuggestionsRequestLocked(
|
this, focusedId, requestAugmentedAutofill),
|
||||||
focusedId, /*requestConsumer=*/ requestAugmentedAutofill,
|
mHandler));
|
||||||
extras);
|
|
||||||
}
|
|
||||||
}, mHandler));
|
|
||||||
} else {
|
} else {
|
||||||
requestAugmentedAutofill.accept(
|
requestAugmentedAutofill.accept(
|
||||||
mInlineSessionController.getInlineSuggestionsRequestLocked().orElse(null));
|
mInlineSessionController.getInlineSuggestionsRequestLocked().orElse(null));
|
||||||
@@ -4960,6 +4934,169 @@ final class Session implements RemoteFillService.FillServiceCallbacks, ViewState
|
|||||||
return mAugmentedAutofillDestroyer;
|
return mAugmentedAutofillDestroyer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class AugmentedAutofillInlineSuggestionRendererOnResultListener
|
||||||
|
implements RemoteCallback.OnResultListener {
|
||||||
|
|
||||||
|
WeakReference<Session> mSessionWeakRef;
|
||||||
|
final AutofillId mFocusedId;
|
||||||
|
Consumer<InlineSuggestionsRequest> mRequestAugmentedAutofill;
|
||||||
|
|
||||||
|
AugmentedAutofillInlineSuggestionRendererOnResultListener(
|
||||||
|
Session session,
|
||||||
|
AutofillId focussedId,
|
||||||
|
Consumer<InlineSuggestionsRequest> requestAugmentedAutofill) {
|
||||||
|
mSessionWeakRef = new WeakReference<>(session);
|
||||||
|
mFocusedId = focussedId;
|
||||||
|
mRequestAugmentedAutofill = requestAugmentedAutofill;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResult(@Nullable Bundle result) {
|
||||||
|
Session session = mSessionWeakRef.get();
|
||||||
|
|
||||||
|
if (logIfSessionNull(
|
||||||
|
session, "AugmentedAutofillInlineSuggestionRendererOnResultListener:")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
synchronized (session.mLock) {
|
||||||
|
session.mInlineSessionController.onCreateInlineSuggestionsRequestLocked(
|
||||||
|
mFocusedId, /*requestConsumer=*/ mRequestAugmentedAutofill,
|
||||||
|
result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class AugmentedAutofillInlineSuggestionRequestConsumer
|
||||||
|
implements Consumer<InlineSuggestionsRequest> {
|
||||||
|
|
||||||
|
WeakReference<Session> mSessionWeakRef;
|
||||||
|
final AutofillId mFocusedId;
|
||||||
|
final boolean mIsAllowlisted;
|
||||||
|
final int mMode;
|
||||||
|
final AutofillValue mCurrentValue;
|
||||||
|
|
||||||
|
AugmentedAutofillInlineSuggestionRequestConsumer(
|
||||||
|
Session session,
|
||||||
|
AutofillId focussedId,
|
||||||
|
boolean isAllowlisted,
|
||||||
|
int mode,
|
||||||
|
AutofillValue currentValue) {
|
||||||
|
mSessionWeakRef = new WeakReference<>(session);
|
||||||
|
mFocusedId = focussedId;
|
||||||
|
mIsAllowlisted = isAllowlisted;
|
||||||
|
mMode = mode;
|
||||||
|
mCurrentValue = currentValue;
|
||||||
|
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void accept(InlineSuggestionsRequest inlineSuggestionsRequest) {
|
||||||
|
Session session = mSessionWeakRef.get();
|
||||||
|
|
||||||
|
if (logIfSessionNull(
|
||||||
|
session, "AugmentedAutofillInlineSuggestionRequestConsumer:")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
session.onAugmentedAutofillInlineSuggestionAccept(
|
||||||
|
inlineSuggestionsRequest, mFocusedId, mIsAllowlisted, mMode, mCurrentValue);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class AugmentedAutofillInlineSuggestionsResponseCallback
|
||||||
|
implements Function<InlineFillUi, Boolean> {
|
||||||
|
|
||||||
|
WeakReference<Session> mSessionWeakRef;
|
||||||
|
|
||||||
|
AugmentedAutofillInlineSuggestionsResponseCallback(Session session) {
|
||||||
|
this.mSessionWeakRef = new WeakReference<>(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean apply(InlineFillUi inlineFillUi) {
|
||||||
|
Session session = mSessionWeakRef.get();
|
||||||
|
|
||||||
|
if (logIfSessionNull(
|
||||||
|
session, "AugmentedAutofillInlineSuggestionsResponseCallback:")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized (session.mLock) {
|
||||||
|
return session.mInlineSessionController.setInlineFillUiLocked(inlineFillUi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class AugmentedAutofillErrorCallback implements Runnable {
|
||||||
|
|
||||||
|
WeakReference<Session> mSessionWeakRef;
|
||||||
|
|
||||||
|
AugmentedAutofillErrorCallback(Session session) {
|
||||||
|
this.mSessionWeakRef = new WeakReference<>(session);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Session session = mSessionWeakRef.get();
|
||||||
|
|
||||||
|
if (logIfSessionNull(session, "AugmentedAutofillErrorCallback:")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
session.onAugmentedAutofillErrorCallback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the session is null or has been destroyed, log the error msg, and return true.
|
||||||
|
* This is a helper function intended to be called when de-referencing from a weak reference.
|
||||||
|
* @param session
|
||||||
|
* @param logPrefix
|
||||||
|
* @return true if the session is null, false otherwise.
|
||||||
|
*/
|
||||||
|
private static boolean logIfSessionNull(Session session, String logPrefix) {
|
||||||
|
if (session == null) {
|
||||||
|
Slog.wtf(TAG, logPrefix + " Session null");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (session.mDestroyed) {
|
||||||
|
// TODO: Update this to return in this block. We aren't doing this to preserve the
|
||||||
|
// behavior, but can be modified once we have more time to soak the changes.
|
||||||
|
Slog.w(TAG, logPrefix + " Session destroyed, but following through");
|
||||||
|
// Follow-through
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onAugmentedAutofillInlineSuggestionAccept(
|
||||||
|
InlineSuggestionsRequest inlineSuggestionsRequest,
|
||||||
|
AutofillId focussedId,
|
||||||
|
boolean isAllowlisted,
|
||||||
|
int mode,
|
||||||
|
AutofillValue currentValue) {
|
||||||
|
synchronized (mLock) {
|
||||||
|
final RemoteAugmentedAutofillService remoteService =
|
||||||
|
mService.getRemoteAugmentedAutofillServiceLocked();
|
||||||
|
logAugmentedAutofillRequestLocked(mode, remoteService.getComponentName(),
|
||||||
|
focussedId, isAllowlisted, inlineSuggestionsRequest != null);
|
||||||
|
remoteService.onRequestAutofillLocked(id, mClient,
|
||||||
|
taskId, mComponentName, mActivityToken,
|
||||||
|
AutofillId.withoutSession(focussedId), currentValue,
|
||||||
|
inlineSuggestionsRequest,
|
||||||
|
new AugmentedAutofillInlineSuggestionsResponseCallback(this),
|
||||||
|
new AugmentedAutofillErrorCallback(this),
|
||||||
|
mService.getRemoteInlineSuggestionRenderServiceLocked(), userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onAugmentedAutofillErrorCallback() {
|
||||||
|
synchronized (mLock) {
|
||||||
|
cancelAugmentedAutofillLocked();
|
||||||
|
|
||||||
|
// Also cancel augmented in IME
|
||||||
|
mInlineSessionController.setInlineFillUiLocked(
|
||||||
|
InlineFillUi.emptyUi(mCurrentViewId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@GuardedBy("mLock")
|
@GuardedBy("mLock")
|
||||||
private void cancelAugmentedAutofillLocked() {
|
private void cancelAugmentedAutofillLocked() {
|
||||||
final RemoteAugmentedAutofillService remoteService = mService
|
final RemoteAugmentedAutofillService remoteService = mService
|
||||||
|
|||||||
Reference in New Issue
Block a user