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

Commit 16534475 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Make mCallbacks#broadcast() thread safe" into main

parents 99b80855 8fa358b8
Loading
Loading
Loading
Loading
+45 −42
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.Pair;
import android.util.Slog;
import android.util.SparseArray;

@@ -196,21 +197,13 @@ public class PackageMonitorCallbackHelper {

    private void doNotifyCallbacksByIntent(Intent intent, int userId,
            int[] broadcastAllowList, Handler handler) {
        RemoteCallbackList<IRemoteCallback> callbacks;
        synchronized (mLock) {
            callbacks = mCallbacks;
        }
        doNotifyCallbacks(callbacks, intent, userId, broadcastAllowList, handler,
        doNotifyCallbacks(intent, userId, broadcastAllowList, handler,
                null /* filterExtrasFunction */);
    }

    private void doNotifyCallbacksByAction(String action, String pkg, Bundle extras, int[] userIds,
            SparseArray<int[]> broadcastAllowList, Handler handler,
            BiFunction<Integer, Bundle, Bundle> filterExtrasFunction) {
        RemoteCallbackList<IRemoteCallback> callbacks;
        synchronized (mLock) {
            callbacks = mCallbacks;
        }
        for (int userId : userIds) {
            final Intent intent = new Intent(action,
                    pkg != null ? Uri.fromParts(PACKAGE_SCHEME, pkg, null) : null);
@@ -226,25 +219,27 @@ public class PackageMonitorCallbackHelper {

            final int[] allowUids =
                    broadcastAllowList != null ? broadcastAllowList.get(userId) : null;
            doNotifyCallbacks(callbacks, intent, userId, allowUids, handler, filterExtrasFunction);
            doNotifyCallbacks(intent, userId, allowUids, handler, filterExtrasFunction);
        }
    }

    private void doNotifyCallbacks(RemoteCallbackList<IRemoteCallback> callbacks,
            Intent intent, int userId, int[] allowUids, Handler handler,
    private void doNotifyCallbacks(Intent intent, int userId, int[] allowUids, Handler handler,
            BiFunction<Integer, Bundle, Bundle> filterExtrasFunction) {
        handler.post(() -> callbacks.broadcast((callback, user) -> {
        handler.post(() -> {
            final ArrayList<Pair<IRemoteCallback, Intent>> target = new ArrayList<>();
            synchronized (mLock) {
                mCallbacks.broadcast((callback, user) -> {
                    RegisterUser registerUser = (RegisterUser) user;
            if ((registerUser.getUserId() != UserHandle.USER_ALL) && (registerUser.getUserId()
                    != userId)) {
                    if ((registerUser.getUserId() != UserHandle.USER_ALL)
                            && (registerUser.getUserId() != userId)) {
                        return;
                    }
                    int registerUid = registerUser.getUid();
                    if (allowUids != null && !UserHandle.isSameApp(registerUid, Process.SYSTEM_UID)
                            && !ArrayUtils.contains(allowUids, registerUid)) {
                        if (DEBUG) {
                    Slog.w(TAG, "Skip invoke PackageMonitorCallback for " + intent.getAction()
                            + ", uid " + registerUid);
                            Slog.w(TAG, "Skip invoke PackageMonitorCallback for "
                                    + intent.getAction() + ", uid " + registerUid);
                        }
                        return;
                    }
@@ -252,12 +247,14 @@ public class PackageMonitorCallbackHelper {
                    if (filterExtrasFunction != null) {
                        final Bundle extras = intent.getExtras();
                        if (extras != null) {
                    final Bundle filteredExtras = filterExtrasFunction.apply(registerUid, extras);
                            final Bundle filteredExtras =
                                    filterExtrasFunction.apply(registerUid, extras);
                            if (filteredExtras == null) {
                                // caller is unable to access this intent
                                if (DEBUG) {
                                    Slog.w(TAG,
                                    "Skip invoke PackageMonitorCallback for " + intent.getAction()
                                            "Skip invoke PackageMonitorCallback for "
                                                    + intent.getAction()
                                                    + " because null filteredExtras");
                                }
                                return;
@@ -266,8 +263,14 @@ public class PackageMonitorCallbackHelper {
                            newIntent.replaceExtras(filteredExtras);
                        }
                    }
            invokeCallback(callback, newIntent);
        }));
                    target.add(new Pair<>(callback, newIntent));
                });
            }
            for (int i = 0; i < target.size(); i++) {
                Pair<IRemoteCallback, Intent> p = target.get(i);
                invokeCallback(p.first, p.second);
            }
        });
    }

    private void invokeCallback(IRemoteCallback callback, Intent intent) {