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

Commit 80c9a975 authored by Jeffrey Huang's avatar Jeffrey Huang
Browse files

Update getRegisteredExperimentIds

Bug: 146384076
Test: GTS Tests
Change-Id: I9da3526d97d5adef423c69366f50fac59a1d6247
parent 4f2e6bd6
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -85,4 +85,11 @@ interface IStatsManagerService {
     * Requires Manifest.permission.DUMP and Manifest.permission.PACKAGE_USAGE_STATS.
     */
    void unsetBroadcastSubscriber(long configKey, long subscriberId, in String packageName);

    /**
     * Returns the most recently registered experiment IDs.
     *
     * Requires Manifest.permission.DUMP and Manifest.permission.PACKAGE_USAGE_STATS.
     */
    long[] getRegisteredExperimentIds();
}
 No newline at end of file
+30 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.stats;
import static com.android.server.stats.StatsCompanion.PendingIntentRef;

import android.Manifest;
import android.annotation.Nullable;
import android.app.AppOpsManager;
import android.app.PendingIntent;
import android.content.Context;
@@ -245,20 +246,48 @@ public class StatsManagerService extends IStatsManagerService.Stub {
        }
    }

    @Override
    public long[] getRegisteredExperimentIds() throws IllegalStateException {
        enforceDumpAndUsageStatsPermission(null);
        final long token = Binder.clearCallingIdentity();
        try {
            IStatsd statsd = waitForStatsd();
            if (statsd != null) {
                return statsd.getRegisteredExperimentIds();
            }
        } catch (RemoteException e) {
            Slog.e(TAG, "Failed to getRegisteredExperimentIds with statsd");
            throw new IllegalStateException(e.getMessage(), e);
        } finally {
            Binder.restoreCallingIdentity(token);
        }
        throw new IllegalStateException("Failed to connect to statsd to registerExperimentIds");
    }

    void setStatsCompanionService(StatsCompanionService statsCompanionService) {
        mStatsCompanionService = statsCompanionService;
    }

    private void enforceDumpAndUsageStatsPermission(String packageName) {
    /**
     * Checks that the caller has both DUMP and PACKAGE_USAGE_STATS permissions. Also checks that
     * the caller has USAGE_STATS_PERMISSION_OPS for the specified packageName if it is not null.
     *
     * @param packageName The packageName to check USAGE_STATS_PERMISSION_OPS.
     */
    private void enforceDumpAndUsageStatsPermission(@Nullable String packageName) {
        int callingUid = Binder.getCallingUid();
        int callingPid = Binder.getCallingPid();

        if (callingPid == Process.myPid()) {
            return;
        }

        mContext.enforceCallingPermission(Manifest.permission.DUMP, null);
        mContext.enforceCallingPermission(Manifest.permission.PACKAGE_USAGE_STATS, null);

        if (packageName == null) {
            return;
        }
        AppOpsManager appOpsManager = (AppOpsManager) mContext
                .getSystemService(Context.APP_OPS_SERVICE);
        switch (appOpsManager.noteOp(USAGE_STATS_PERMISSION_OPS,
+1 −11
Original line number Diff line number Diff line
@@ -1477,17 +1477,7 @@ Status StatsService::sendWatchdogRollbackOccurredAtom(const int32_t rollbackType


Status StatsService::getRegisteredExperimentIds(std::vector<int64_t>* experimentIdsOut) {
    uid_t uid = IPCThreadState::self()->getCallingUid();

    // Caller must be granted these permissions
    if (!checkCallingPermission(String16(kPermissionDump))) {
        return exception(binder::Status::EX_SECURITY,
                         StringPrintf("UID %d lacks permission %s", uid, kPermissionDump));
    }
    if (!checkCallingPermission(String16(kPermissionUsage))) {
        return exception(binder::Status::EX_SECURITY,
                         StringPrintf("UID %d lacks permission %s", uid, kPermissionUsage));
    }
    ENFORCE_UID(AID_SYSTEM);
    // TODO: add verifier permission

    // Read the latest train info
+5 −7
Original line number Diff line number Diff line
@@ -462,21 +462,19 @@ public final class StatsManager {
            throws StatsUnavailableException {
        synchronized (sLock) {
            try {
                IStatsd service = getIStatsdLocked();
                IStatsManagerService service = getIStatsManagerServiceLocked();
                if (service == null) {
                    if (DEBUG) {
                        Slog.d(TAG, "Failed to find statsd when getting experiment IDs");
                    }
                    return new long[0];
                    throw new StatsUnavailableException("Failed to find statsmanager when "
                                                              + "getting experiment IDs");
                }
                return service.getRegisteredExperimentIds();
            } catch (RemoteException e) {
                if (DEBUG) {
                    Slog.d(TAG,
                            "Failed to connect to StatsCompanionService when getting "
                            "Failed to connect to StatsManagerService when getting "
                                    + "registered experiment IDs");
                }
                return new long[0];
                throw new StatsUnavailableException("could not connect", e);
            }
        }
    }