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

Commit 253c9f4d authored by Varun Shah's avatar Varun Shah
Browse files

Limit who the SyncStatusObserver broadcasts are sent to.

Changes for ContentResolver.SYNC_OBSERVER_TYPE_* will only be
broadcasted to the user who registered the listener.

Bug: 128599864
Test: atest SyncStorageEngineTest
Test: atest com.android.service.content.SyncManagerTest
Test: atest CtsSyncManagerTest
Change-Id: Ib2482192644d2f2c4247b4d841a8598908bc4592
parent d480edf1
Loading
Loading
Loading
Loading
+3 −1
Original line number Original line Diff line number Diff line
@@ -1104,11 +1104,13 @@ public final class ContentService extends IContentService.Stub {


    @Override
    @Override
    public void addStatusChangeListener(int mask, ISyncStatusObserver callback) {
    public void addStatusChangeListener(int mask, ISyncStatusObserver callback) {
        final int callingUid = Binder.getCallingUid();
        long identityToken = clearCallingIdentity();
        long identityToken = clearCallingIdentity();
        try {
        try {
            SyncManager syncManager = getSyncManager();
            SyncManager syncManager = getSyncManager();
            if (syncManager != null && callback != null) {
            if (syncManager != null && callback != null) {
                syncManager.getSyncStorageEngine().addStatusChangeListener(mask, callback);
                syncManager.getSyncStorageEngine().addStatusChangeListener(
                        mask, UserHandle.getUserId(callingUid), callback);
            }
            }
        } finally {
        } finally {
            restoreCallingIdentity(identityToken);
            restoreCallingIdentity(identityToken);
+4 −2
Original line number Original line Diff line number Diff line
@@ -3375,7 +3375,8 @@ public class SyncManager {
            }
            }


            scheduleSyncOperationH(op);
            scheduleSyncOperationH(op);
            mSyncStorageEngine.reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
            mSyncStorageEngine.reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS,
                    target.userId);
        }
        }


        /**
        /**
@@ -3877,7 +3878,8 @@ public class SyncManager {
            EventLog.writeEvent(2720,
            EventLog.writeEvent(2720,
                    syncOperation.toEventLog(SyncStorageEngine.EVENT_STOP));
                    syncOperation.toEventLog(SyncStorageEngine.EVENT_STOP));
            mSyncStorageEngine.stopSyncEvent(rowId, elapsedTime,
            mSyncStorageEngine.stopSyncEvent(rowId, elapsedTime,
                    resultMessage, downstreamActivity, upstreamActivity);
                    resultMessage, downstreamActivity, upstreamActivity,
                    syncOperation.target.userId);
        }
        }
    }
    }


+27 −22
Original line number Original line Diff line number Diff line
@@ -43,6 +43,7 @@ import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.AtomicFile;
import android.util.AtomicFile;
import android.util.EventLog;
import android.util.EventLog;
import android.util.Log;
import android.util.Log;
@@ -54,6 +55,7 @@ import android.util.Xml;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.FastXmlSerializer;
import com.android.internal.util.FastXmlSerializer;
import com.android.internal.util.IntPair;


import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserException;
@@ -588,9 +590,10 @@ public class SyncStorageEngine {
        return mSyncRandomOffset;
        return mSyncRandomOffset;
    }
    }


    public void addStatusChangeListener(int mask, ISyncStatusObserver callback) {
    public void addStatusChangeListener(int mask, int userId, ISyncStatusObserver callback) {
        synchronized (mAuthorities) {
        synchronized (mAuthorities) {
            mChangeListeners.register(callback, mask);
            final long cookie = IntPair.of(userId, mask);
            mChangeListeners.register(callback, cookie);
        }
        }
    }
    }


@@ -622,14 +625,16 @@ public class SyncStorageEngine {
        }
        }
    }
    }


    void reportChange(int which) {
    void reportChange(int which, int callingUserId) {
        ArrayList<ISyncStatusObserver> reports = null;
        ArrayList<ISyncStatusObserver> reports = null;
        synchronized (mAuthorities) {
        synchronized (mAuthorities) {
            int i = mChangeListeners.beginBroadcast();
            int i = mChangeListeners.beginBroadcast();
            while (i > 0) {
            while (i > 0) {
                i--;
                i--;
                Integer mask = (Integer)mChangeListeners.getBroadcastCookie(i);
                final long cookie = (long) mChangeListeners.getBroadcastCookie(i);
                if ((which & mask.intValue()) == 0) {
                final int userId = IntPair.first(cookie);
                final int mask = IntPair.second(cookie);
                if ((which & mask) == 0 || callingUserId != userId) {
                    continue;
                    continue;
                }
                }
                if (reports == null) {
                if (reports == null) {
@@ -719,7 +724,7 @@ public class SyncStorageEngine {
                    new Bundle(),
                    new Bundle(),
                    syncExemptionFlag, callingUid, callingPid);
                    syncExemptionFlag, callingUid, callingPid);
        }
        }
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, userId);
        queueBackup();
        queueBackup();
    }
    }


@@ -787,7 +792,7 @@ public class SyncStorageEngine {
            requestSync(aInfo, SyncOperation.REASON_IS_SYNCABLE, new Bundle(),
            requestSync(aInfo, SyncOperation.REASON_IS_SYNCABLE, new Bundle(),
                    ContentResolver.SYNC_EXEMPTION_NONE, callingUid, callingPid);
                    ContentResolver.SYNC_EXEMPTION_NONE, callingUid, callingPid);
        }
        }
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, target.userId);
    }
    }


    public Pair<Long, Long> getBackoff(EndPoint info) {
    public Pair<Long, Long> getBackoff(EndPoint info) {
@@ -833,7 +838,7 @@ public class SyncStorageEngine {
            }
            }
        }
        }
        if (changed) {
        if (changed) {
            reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
            reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, info.userId);
        }
        }
    }
    }


@@ -871,7 +876,7 @@ public class SyncStorageEngine {
    }
    }


    public void clearAllBackoffsLocked() {
    public void clearAllBackoffsLocked() {
        boolean changed = false;
        final ArraySet<Integer> changedUserIds = new ArraySet<>();
        synchronized (mAuthorities) {
        synchronized (mAuthorities) {
            // Clear backoff for all sync adapters.
            // Clear backoff for all sync adapters.
            for (AccountInfo accountInfo : mAccounts.values()) {
            for (AccountInfo accountInfo : mAccounts.values()) {
@@ -888,14 +893,14 @@ public class SyncStorageEngine {
                        }
                        }
                        authorityInfo.backoffTime = NOT_IN_BACKOFF_MODE;
                        authorityInfo.backoffTime = NOT_IN_BACKOFF_MODE;
                        authorityInfo.backoffDelay = NOT_IN_BACKOFF_MODE;
                        authorityInfo.backoffDelay = NOT_IN_BACKOFF_MODE;
                        changed = true;
                        changedUserIds.add(accountInfo.accountAndUser.userId);
                    }
                    }
                }
                }
            }
            }
        }
        }


        if (changed) {
        for (int i = changedUserIds.size() - 1; i > 0; i--) {
            reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
            reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, changedUserIds.valueAt(i));
        }
        }
    }
    }


@@ -921,7 +926,7 @@ public class SyncStorageEngine {
            }
            }
            authority.delayUntil = delayUntil;
            authority.delayUntil = delayUntil;
        }
        }
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, info.userId);
    }
    }


    /**
    /**
@@ -964,7 +969,7 @@ public class SyncStorageEngine {
                    new Bundle(),
                    new Bundle(),
                    syncExemptionFlag, callingUid, callingPid);
                    syncExemptionFlag, callingUid, callingPid);
        }
        }
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, userId);
        mContext.sendBroadcast(ContentResolver.ACTION_SYNC_CONN_STATUS_CHANGED);
        mContext.sendBroadcast(ContentResolver.ACTION_SYNC_CONN_STATUS_CHANGED);
        queueBackup();
        queueBackup();
    }
    }
@@ -1015,7 +1020,7 @@ public class SyncStorageEngine {
            SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident);
            SyncStatusInfo status = getOrCreateSyncStatusLocked(authority.ident);
            status.pending = pendingValue;
            status.pending = pendingValue;
        }
        }
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING);
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_PENDING, info.userId);
    }
    }


    /**
    /**
@@ -1103,7 +1108,7 @@ public class SyncStorageEngine {
                    activeSyncContext.mStartTime);
                    activeSyncContext.mStartTime);
            getCurrentSyncs(authorityInfo.target.userId).add(syncInfo);
            getCurrentSyncs(authorityInfo.target.userId).add(syncInfo);
        }
        }
        reportActiveChange();
        reportActiveChange(activeSyncContext.mSyncOperation.target.userId);
        return syncInfo;
        return syncInfo;
    }
    }


@@ -1120,14 +1125,14 @@ public class SyncStorageEngine {
            getCurrentSyncs(userId).remove(syncInfo);
            getCurrentSyncs(userId).remove(syncInfo);
        }
        }


        reportActiveChange();
        reportActiveChange(userId);
    }
    }


    /**
    /**
     * To allow others to send active change reports, to poke clients.
     * To allow others to send active change reports, to poke clients.
     */
     */
    public void reportActiveChange() {
    public void reportActiveChange(int userId) {
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE);
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE, userId);
    }
    }


    /**
    /**
@@ -1162,12 +1167,12 @@ public class SyncStorageEngine {
            if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "returning historyId " + id);
            if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "returning historyId " + id);
        }
        }


        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS);
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS, op.target.userId);
        return id;
        return id;
    }
    }


    public void stopSyncEvent(long historyId, long elapsedTime, String resultMessage,
    public void stopSyncEvent(long historyId, long elapsedTime, String resultMessage,
                              long downstreamActivity, long upstreamActivity) {
                              long downstreamActivity, long upstreamActivity, int userId) {
        synchronized (mAuthorities) {
        synchronized (mAuthorities) {
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
            if (Log.isLoggable(TAG, Log.VERBOSE)) {
                Slog.v(TAG, "stopSyncEvent: historyId=" + historyId);
                Slog.v(TAG, "stopSyncEvent: historyId=" + historyId);
@@ -1307,7 +1312,7 @@ public class SyncStorageEngine {
            }
            }
        }
        }


        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS);
        reportChange(ContentResolver.SYNC_OBSERVER_TYPE_STATUS, userId);
    }
    }


    /**
    /**