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

Commit fa65601a authored by Hall Liu's avatar Hall Liu Committed by Gerrit Code Review
Browse files

Merge "Add lock on mCallbacks in ServiceBinder"

parents 1b8babe9 73fa3809
Loading
Loading
Loading
Loading
+22 −8
Original line number Diff line number Diff line
@@ -73,13 +73,15 @@ public abstract class ServiceBinder {
            // Reset any abort request if we're asked to bind again.
            clearAbort();

            synchronized (mCallbacks) {
                if (!mCallbacks.isEmpty()) {
                    // Binding already in progress, append to the list of callbacks and bail out.
                    mCallbacks.add(callback);
                    return;
                }

                mCallbacks.add(callback);
            }

            if (mServiceConnection == null) {
                Intent serviceIntent = new Intent(mServiceAction).setComponent(mComponentName);
                ServiceConnection connection = new ServiceBinderConnection(call);
@@ -351,10 +353,16 @@ public abstract class ServiceBinder {
     * outstanding callbacks is cleared afterwards.
     */
    private void handleSuccessfulConnection() {
        for (BindCallback callback : mCallbacks) {
        // Make a copy so that we don't have a deadlock inside one of the callbacks.
        Set<BindCallback> callbacksCopy = new ArraySet<>();
        synchronized (mCallbacks) {
            callbacksCopy.addAll(mCallbacks);
            mCallbacks.clear();
        }

        for (BindCallback callback : callbacksCopy) {
            callback.onSuccess();
        }
        mCallbacks.clear();
    }

    /**
@@ -362,10 +370,16 @@ public abstract class ServiceBinder {
     * outstanding callbacks is cleared afterwards.
     */
    private void handleFailedConnection() {
        for (BindCallback callback : mCallbacks) {
        // Make a copy so that we don't have a deadlock inside one of the callbacks.
        Set<BindCallback> callbacksCopy = new ArraySet<>();
        synchronized (mCallbacks) {
            callbacksCopy.addAll(mCallbacks);
            mCallbacks.clear();
        }

        for (BindCallback callback : callbacksCopy) {
            callback.onFailure();
        }
        mCallbacks.clear();
    }

    /**