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

Commit 8e6b83f6 authored by Felix Oghina's avatar Felix Oghina Committed by Android (Google) Code Review
Browse files

Merge "[speech] limit number of sessions per calling uid" into main

parents 666bde7f 7cd38755
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -536,8 +536,17 @@ public class SpeechRecognizer {
    @MainThread
    public void setRecognitionListener(RecognitionListener listener) {
        checkIsCalledFromMainThread();
        if (mListener.mInternalListener == null) {
            // This shortcut is needed because otherwise, if there's an error connecting, it never
            // gets delivered. I.e., the onSuccess callback set up in connectToSystemService does
            // not get called, MSG_CHANGE_LISTENER does not get executed, so the onError in the same
            // place does not get forwarded anywhere.
            // Thread-wise, this is safe as both this method and the handler are on the UI thread.
            handleChangeListener(listener);
        } else {
            putMessage(Message.obtain(mHandler, MSG_CHANGE_LISTENER, listener));
        }
    }

    /**
     * Starts listening for speech. Please note that
+40 −0
Original line number Diff line number Diff line
@@ -39,8 +39,10 @@ import android.speech.IRecognitionSupportCallback;
import android.speech.RecognitionService;
import android.speech.SpeechRecognizer;
import android.util.Slog;
import android.util.SparseIntArray;

import com.android.internal.annotations.GuardedBy;
import com.android.modules.expresslog.Counter;
import com.android.server.infra.AbstractPerUserSystemService;

import java.util.HashMap;
@@ -64,6 +66,9 @@ final class SpeechRecognitionManagerServiceImpl extends
    private final Map<Integer, Set<RemoteSpeechRecognitionService>> mRemoteServicesByUid =
            new HashMap<>();

    @GuardedBy("mLock")
    private final SparseIntArray mSessionCountByUid = new SparseIntArray();

    SpeechRecognitionManagerServiceImpl(
            @NonNull SpeechRecognitionManagerService master,
            @NonNull Object lock, @UserIdInt int userId) {
@@ -216,6 +221,7 @@ final class SpeechRecognitionManagerServiceImpl extends
            service.shutdown(clientToken);
        }
        synchronized (mLock) {
            decrementSessionCountForUidLocked(callingUid);
            if (!service.hasActiveSessions()) {
                removeService(callingUid, service);
            }
@@ -239,6 +245,26 @@ final class SpeechRecognitionManagerServiceImpl extends
        return ComponentName.unflattenFromString(serviceName);
    }

    @GuardedBy("mLock")
    private int getSessionCountByUidLocked(int uid) {
        return mSessionCountByUid.get(uid, 0);
    }

    @GuardedBy("mLock")
    private void incrementSessionCountForUidLocked(int uid) {
        mSessionCountByUid.put(uid, mSessionCountByUid.get(uid, 0) + 1);
    }

    @GuardedBy("mLock")
    private void decrementSessionCountForUidLocked(int uid) {
        int newCount = mSessionCountByUid.get(uid, 1) - 1;
        if (newCount > 0) {
            mSessionCountByUid.put(uid, newCount);
        } else {
            mSessionCountByUid.delete(uid);
        }
    }

    private RemoteSpeechRecognitionService createService(
            int callingUid, ComponentName serviceComponent) {
        synchronized (mLock) {
@@ -247,6 +273,18 @@ final class SpeechRecognitionManagerServiceImpl extends

            if (servicesForClient != null
                    && servicesForClient.size() >= MAX_CONCURRENT_CONNECTIONS_BY_CLIENT) {
                Slog.w(TAG, "Number of remote services exceeded for uid: " + callingUid);
                Counter.logIncrementWithUid(
                        "speech_recognition.value_exceed_service_connections_count",
                        callingUid);
                return null;
            }

            if (getSessionCountByUidLocked(callingUid) >= MAX_CONCURRENT_CONNECTIONS_BY_CLIENT) {
                Slog.w(TAG, "Number of sessions exceeded for uid: " + callingUid);
                Counter.logIncrementWithUid(
                        "speech_recognition.value_exceed_session_count",
                        callingUid);
                return null;
            }

@@ -262,6 +300,7 @@ final class SpeechRecognitionManagerServiceImpl extends
                        Slog.i(TAG, "Reused existing connection to " + serviceComponent);
                    }

                    incrementSessionCountForUidLocked(callingUid);
                    return existingService.get();
                }
            }
@@ -282,6 +321,7 @@ final class SpeechRecognitionManagerServiceImpl extends
                Slog.i(TAG, "Creating a new connection to " + serviceComponent);
            }

            incrementSessionCountForUidLocked(callingUid);
            return service;
        }
    }