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

Commit 41c8acbd authored by Jing Ji's avatar Jing Ji Committed by Automerger Merge Worker
Browse files

Merge "Add client/service process states for broadcast/service/provider" into...

Merge "Add client/service process states for broadcast/service/provider" into udc-dev am: 0f529d03

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/23558767



Change-Id: Icab4c9f72e730dc36958a079d99ffef3034fc176
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 0377c68a 0f529d03
Loading
Loading
Loading
Loading
+49 −26
Original line number Diff line number Diff line
@@ -621,7 +621,8 @@ public final class ActiveServices {
                    try {
                        final ServiceRecord.StartItem si = r.pendingStarts.get(0);
                        startServiceInnerLocked(this, si.intent, r, false, true, si.callingId,
                                si.mCallingProcessName, r.startRequested, si.mCallingPackageName);
                                si.mCallingProcessName, si.mCallingProcessState,
                                r.startRequested, si.mCallingPackageName);
                    } catch (TransactionTooLargeException e) {
                        // Ignore, nobody upstack cares.
                    }
@@ -977,10 +978,22 @@ public final class ActiveServices {
            fgRequired = false;
        }

        final ProcessRecord callingApp;
        synchronized (mAm.mPidsSelfLocked) {
            callingApp = mAm.mPidsSelfLocked.get(callingPid);
        }
        final String callingProcessName = callingApp != null
                ? callingApp.processName : callingPackage;
        final int callingProcessState =
                callingApp != null && callingApp.getThread() != null && !callingApp.isKilled()
                ? callingApp.mState.getCurProcState() : ActivityManager.PROCESS_STATE_UNKNOWN;
        r.updateProcessStateOnRequest();

        // The package could be frozen (meaning it's doing surgery), defer the actual
        // start until the package is unfrozen.
        if (deferServiceBringupIfFrozenLocked(r, service, callingPackage, callingFeatureId,
                callingUid, callingPid, fgRequired, callerFg, userId,
                callingUid, callingPid, callingProcessName,
                callingProcessState, fgRequired, callerFg, userId,
                backgroundStartPrivileges, false, null)) {
            return null;
        }
@@ -1001,7 +1014,7 @@ public final class ActiveServices {
        // what realResult contains.
        final ComponentName realResult =
                startServiceInnerLocked(r, service, callingUid, callingPid,
                        getCallingProcessNameLocked(callingUid, callingPid, callingPackage),
                        callingProcessName, callingProcessState,
                        fgRequired, callerFg,
                        backgroundStartPrivileges, callingPackage);
        if (res.aliasComponent != null
@@ -1013,17 +1026,9 @@ public final class ActiveServices {
        }
    }

    private String getCallingProcessNameLocked(int callingUid, int callingPid,
            String callingPackage) {
        synchronized (mAm.mPidsSelfLocked) {
            final ProcessRecord callingApp = mAm.mPidsSelfLocked.get(callingPid);
            return callingApp != null ? callingApp.processName : callingPackage;
        }
    }

    private ComponentName startServiceInnerLocked(ServiceRecord r, Intent service,
            int callingUid, int callingPid, String callingProcessName, boolean fgRequired,
            boolean callerFg,
            int callingUid, int callingPid, String callingProcessName,
            int callingProcessState, boolean fgRequired, boolean callerFg,
            BackgroundStartPrivileges backgroundStartPrivileges, String callingPackage)
            throws TransactionTooLargeException {
        NeededUriGrants neededGrants = mAm.mUgmInternal.checkGrantUriPermissionFromIntent(
@@ -1037,7 +1042,8 @@ public final class ActiveServices {
        r.delayedStop = false;
        r.fgRequired = fgRequired;
        r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(),
                service, neededGrants, callingUid, callingProcessName, callingPackage));
                service, neededGrants, callingUid, callingProcessName, callingPackage,
                callingProcessState));

        // We want to allow scheduling user-initiated jobs when the app is running a
        // foreground service that was started in the same conditions that allows for scheduling
@@ -1140,7 +1146,8 @@ public final class ActiveServices {
            r.allowBgActivityStartsOnServiceStart(backgroundStartPrivileges);
        }
        ComponentName cmp = startServiceInnerLocked(smap, service, r, callerFg, addToStarting,
                callingUid, callingProcessName, wasStartRequested, callingPackage);
                callingUid, callingProcessName, callingProcessState,
                wasStartRequested, callingPackage);
        return cmp;
    }

@@ -1244,7 +1251,8 @@ public final class ActiveServices {
    @GuardedBy("mAm")
    private boolean deferServiceBringupIfFrozenLocked(ServiceRecord s, Intent serviceIntent,
            String callingPackage, @Nullable String callingFeatureId,
            int callingUid, int callingPid, boolean fgRequired, boolean callerFg, int userId,
            int callingUid, int callingPid, String callingProcessName,
            int callingProcessState, boolean fgRequired, boolean callerFg, int userId,
            BackgroundStartPrivileges backgroundStartPrivileges,
            boolean isBinding, IServiceConnection connection) {
        final PackageManagerInternal pm = mAm.getPackageManagerInternal();
@@ -1258,8 +1266,6 @@ public final class ActiveServices {
            curPendingBringups = new ArrayList<>();
            mPendingBringups.put(s, curPendingBringups);
        }
        final String callingProcessName = getCallingProcessNameLocked(
                callingUid, callingPid, callingPackage);
        curPendingBringups.add(new Runnable() {
            @Override
            public void run() {
@@ -1291,7 +1297,7 @@ public final class ActiveServices {
                    } else { // Starting a service
                        try {
                            startServiceInnerLocked(s, serviceIntent, callingUid, callingPid,
                                    callingProcessName, fgRequired, callerFg,
                                    callingProcessName, callingProcessState, fgRequired, callerFg,
                                    backgroundStartPrivileges, callingPackage);
                        } catch (TransactionTooLargeException e) {
                            /* ignore - local call */
@@ -1338,7 +1344,8 @@ public final class ActiveServices {

    ComponentName startServiceInnerLocked(ServiceMap smap, Intent service, ServiceRecord r,
            boolean callerFg, boolean addToStarting, int callingUid, String callingProcessName,
            boolean wasStartRequested, String callingPackage) throws TransactionTooLargeException {
            int callingProcessState, boolean wasStartRequested, String callingPackage)
            throws TransactionTooLargeException {
        synchronized (mAm.mProcessStats.mLock) {
            final ServiceState stracker = r.getTracker();
            if (stracker != null) {
@@ -1381,7 +1388,9 @@ public final class ActiveServices {
                getShortServiceNameForStats(r),
                packageState,
                packageName,
                callingPackage);
                callingPackage,
                callingProcessState,
                r.mProcessStateOnRequest);

        if (r.startRequested && addToStarting) {
            boolean first = smap.mStartingBackground.size() == 0;
@@ -3611,11 +3620,22 @@ public final class ActiveServices {
            return 0;
        }

        final ProcessRecord callingApp;
        synchronized (mAm.mPidsSelfLocked) {
            callingApp = mAm.mPidsSelfLocked.get(callingPid);
        }
        final String callingProcessName = callingApp != null
                ? callingApp.processName : callingPackage;
        final int callingProcessState =
                callingApp != null && callingApp.getThread() != null && !callingApp.isKilled()
                ? callingApp.mState.getCurProcState() : ActivityManager.PROCESS_STATE_UNKNOWN;
        s.updateProcessStateOnRequest();

        // The package could be frozen (meaning it's doing surgery), defer the actual
        // binding until the package is unfrozen.
        boolean packageFrozen = deferServiceBringupIfFrozenLocked(s, service, callingPackage, null,
                callingUid, callingPid, false, callerFg, userId, BackgroundStartPrivileges.NONE,
                true, connection);
                callingUid, callingPid, callingProcessName, callingProcessState,
                false, callerFg, userId, BackgroundStartPrivileges.NONE, true, connection);

        // If permissions need a review before any of the app components can run,
        // we schedule binding to the service but do not start its process, then
@@ -3756,7 +3776,9 @@ public final class ActiveServices {
                    getShortServiceNameForStats(s),
                    packageState,
                    s.packageName,
                    callerApp.info.packageName);
                    callerApp.info.packageName,
                    callerApp.mState.getCurProcState(),
                    s.mProcessStateOnRequest);

            if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Bind " + s + " with " + b
                    + ": received=" + b.intent.received
@@ -5355,7 +5377,7 @@ public final class ActiveServices {
        // be called.
        if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) {
            r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(),
                    null, null, 0, null, null));
                    null, null, 0, null, null, ActivityManager.PROCESS_STATE_UNKNOWN));
        }

        sendServiceArgsLocked(r, execInFg, true);
@@ -6351,7 +6373,8 @@ public final class ActiveServices {
                    stopServiceLocked(sr, true);
                } else {
                    sr.pendingStarts.add(new ServiceRecord.StartItem(sr, true,
                            sr.getLastStartId(), baseIntent, null, 0, null, null));
                            sr.getLastStartId(), baseIntent, null, 0, null, null,
                            ActivityManager.PROCESS_STATE_UNKNOWN));
                    if (sr.app != null && sr.app.getThread() != null) {
                        // We always run in the foreground, since this is called as
                        // part of the "remove task" UI operation.
+29 −7
Original line number Diff line number Diff line
@@ -1176,20 +1176,24 @@ public class ActivityManagerService extends IActivityManager.Stub
        public Intent intent;
        public boolean deferUntilActive;
        public int originalCallingUid;
        /** The snapshot process state of the app who sent this broadcast */
        public int originalCallingAppProcessState;
        public static StickyBroadcast create(Intent intent, boolean deferUntilActive,
                int originalCallingUid) {
                int originalCallingUid, int originalCallingAppProcessState) {
            final StickyBroadcast b = new StickyBroadcast();
            b.intent = intent;
            b.deferUntilActive = deferUntilActive;
            b.originalCallingUid = originalCallingUid;
            b.originalCallingAppProcessState = originalCallingAppProcessState;
            return b;
        }
        @Override
        public String toString() {
            return "{intent=" + intent + ", defer=" + deferUntilActive + ", originalCallingUid="
                    + originalCallingUid + "}";
                    + originalCallingUid + ", originalCallingAppProcessState="
                    + originalCallingAppProcessState + "}";
        }
    }
@@ -14042,7 +14046,8 @@ public class ActivityManagerService extends IActivityManager.Stub
                            receivers, null, null, 0, null, null, false, true, true, -1,
                            originalStickyCallingUid, BackgroundStartPrivileges.NONE,
                            false /* only PRE_BOOT_COMPLETED should be exempt, no stickies */,
                            null /* filterExtrasForReceiver */);
                            null /* filterExtrasForReceiver */,
                            broadcast.originalCallingAppProcessState);
                    queue.enqueueBroadcastLocked(r);
                }
            }
@@ -14857,6 +14862,7 @@ public class ActivityManagerService extends IActivityManager.Stub
            }
        }
        final int callerAppProcessState = getRealProcessStateLocked(callerApp, realCallingPid);
        // Add to the sticky list if requested.
        if (sticky) {
            if (checkPermission(android.Manifest.permission.BROADCAST_STICKY,
@@ -14919,12 +14925,13 @@ public class ActivityManagerService extends IActivityManager.Stub
                if (intent.filterEquals(list.get(i).intent)) {
                    // This sticky already exists, replace it.
                    list.set(i, StickyBroadcast.create(new Intent(intent), deferUntilActive,
                            callingUid));
                            callingUid, callerAppProcessState));
                    break;
                }
            }
            if (i >= stickiesCount) {
                list.add(StickyBroadcast.create(new Intent(intent), deferUntilActive, callingUid));
                list.add(StickyBroadcast.create(new Intent(intent), deferUntilActive, callingUid,
                        callerAppProcessState));
            }
        }
@@ -15007,7 +15014,8 @@ public class ActivityManagerService extends IActivityManager.Stub
                    requiredPermissions, excludedPermissions, excludedPackages, appOp, brOptions,
                    registeredReceivers, resultToApp, resultTo, resultCode, resultData,
                    resultExtras, ordered, sticky, false, userId,
                    backgroundStartPrivileges, timeoutExempt, filterExtrasForReceiver);
                    backgroundStartPrivileges, timeoutExempt, filterExtrasForReceiver,
                    callerAppProcessState);
            if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Enqueueing parallel broadcast " + r);
            queue.enqueueBroadcastLocked(r);
            registeredReceivers = null;
@@ -15101,7 +15109,8 @@ public class ActivityManagerService extends IActivityManager.Stub
                    requiredPermissions, excludedPermissions, excludedPackages, appOp, brOptions,
                    receivers, resultToApp, resultTo, resultCode, resultData, resultExtras,
                    ordered, sticky, false, userId,
                    backgroundStartPrivileges, timeoutExempt, filterExtrasForReceiver);
                    backgroundStartPrivileges, timeoutExempt, filterExtrasForReceiver,
                    callerAppProcessState);
            if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Enqueueing ordered broadcast " + r);
            queue.enqueueBroadcastLocked(r);
@@ -15118,6 +15127,19 @@ public class ActivityManagerService extends IActivityManager.Stub
        return ActivityManager.BROADCAST_SUCCESS;
    }
    @GuardedBy("this")
    private int getRealProcessStateLocked(ProcessRecord app, int pid) {
        if (app == null) {
            synchronized (mPidsSelfLocked) {
                app = mPidsSelfLocked.get(pid);
            }
        }
        if (app != null && app.getThread() != null && !app.isKilled()) {
            return app.mState.getCurProcState();
        }
        return PROCESS_STATE_NONEXISTENT;
    }
    @VisibleForTesting
    ArrayList<StickyBroadcast> getStickyBroadcasts(String action, int userId) {
        final ArrayMap<String, ArrayList<StickyBroadcast>> stickyBroadcasts =
+6 −0
Original line number Diff line number Diff line
@@ -105,6 +105,12 @@ class BroadcastProcessQueue {
     */
    long lastCpuDelayTime;

    /**
     * Snapshotted value of {@link ProcessStateRecord#getCurProcState()} before
     * dispatching the current broadcast to the receiver in this process.
     */
    int lastProcessState;

    /**
     * Ordered collection of broadcasts that are waiting to be dispatched to
     * this process, as a pair of {@link BroadcastRecord} and the index into
+22 −6
Original line number Diff line number Diff line
@@ -269,7 +269,10 @@ public class BroadcastQueueImpl extends BroadcastQueue {
                                Activity.RESULT_CANCELED, null, null,
                                false, false, oldRecord.shareIdentity, oldRecord.userId,
                                oldRecord.callingUid, r.callingUid, r.callerPackage,
                                SystemClock.uptimeMillis() - oldRecord.enqueueTime, 0, 0);
                                SystemClock.uptimeMillis() - oldRecord.enqueueTime, 0, 0,
                                oldRecord.resultToApp != null
                                        ? oldRecord.resultToApp.mState.getCurProcState()
                                        : ActivityManager.PROCESS_STATE_UNKNOWN);
                    } catch (RemoteException e) {
                        Slog.w(TAG, "Failure ["
                                + mQueueName + "] sending broadcast result of "
@@ -367,6 +370,7 @@ public class BroadcastQueueImpl extends BroadcastQueue {
        }

        r.curApp = app;
        r.curAppLastProcessState = app.mState.getCurProcState();
        final ProcessReceiverRecord prr = app.mReceivers;
        prr.addCurReceiver(r);
        app.mState.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER);
@@ -418,6 +422,7 @@ public class BroadcastQueueImpl extends BroadcastQueue {
                if (DEBUG_BROADCAST)  Slog.v(TAG_BROADCAST,
                        "Process cur broadcast " + r + ": NOT STARTED!");
                r.curApp = null;
                r.curAppLastProcessState = ActivityManager.PROCESS_STATE_UNKNOWN;
                prr.removeCurReceiver(r);
            }
        }
@@ -620,7 +625,8 @@ public class BroadcastQueueImpl extends BroadcastQueue {
                    r.getDeliveryGroupPolicy(),
                    r.intent.getFlags(),
                    BroadcastRecord.getReceiverPriority(curReceiver),
                    r.callerProcState);
                    r.callerProcState,
                    r.curAppLastProcessState);
        }
        if (state == BroadcastRecord.IDLE) {
            Slog.w(TAG_BROADCAST, "finishReceiver [" + mQueueName + "] called but state is IDLE");
@@ -682,6 +688,7 @@ public class BroadcastQueueImpl extends BroadcastQueue {
        r.curFilter = null;
        r.curReceiver = null;
        r.curApp = null;
        r.curAppLastProcessState = ActivityManager.PROCESS_STATE_UNKNOWN;
        r.curFilteredExtras = null;
        r.mWasReceiverAppStopped = false;
        mPendingBroadcast = null;
@@ -751,7 +758,8 @@ public class BroadcastQueueImpl extends BroadcastQueue {
            Intent intent, int resultCode, String data, Bundle extras,
            boolean ordered, boolean sticky, boolean shareIdentity, int sendingUser,
            int receiverUid, int callingUid, String callingPackage,
            long dispatchDelay, long receiveDelay, int priority) throws RemoteException {
            long dispatchDelay, long receiveDelay, int priority,
            int receiverProcessState) throws RemoteException {
        // If the broadcaster opted-in to sharing their identity, then expose package visibility for
        // the receiver.
        if (shareIdentity) {
@@ -802,7 +810,7 @@ public class BroadcastQueueImpl extends BroadcastQueue {
                    SERVICE_REQUEST_EVENT_REPORTED__PACKAGE_STOPPED_STATE__PACKAGE_STATE_NORMAL,
                    app != null ? app.info.packageName : null, callingPackage,
                    r.calculateTypeForLogging(), r.getDeliveryGroupPolicy(), r.intent.getFlags(),
                    priority, r.callerProcState);
                    priority, r.callerProcState, receiverProcessState);
        }
    }

@@ -849,6 +857,7 @@ public class BroadcastQueueImpl extends BroadcastQueue {
                // things that directly call the IActivityManager API, which
                // are already core system stuff so don't matter for this.
                r.curApp = filter.receiverList.app;
                r.curAppLastProcessState = r.curApp.mState.getCurProcState();
                filter.receiverList.app.mReceivers.addCurReceiver(r);
                mService.enqueueOomAdjTargetLocked(r.curApp);
                mService.updateOomAdjPendingTargetsLocked(
@@ -883,7 +892,10 @@ public class BroadcastQueueImpl extends BroadcastQueue {
                        r.resultExtras, r.ordered, r.initialSticky, r.shareIdentity, r.userId,
                        filter.receiverList.uid, r.callingUid, r.callerPackage,
                        r.dispatchTime - r.enqueueTime,
                        r.receiverTime - r.dispatchTime, filter.getPriority());
                        r.receiverTime - r.dispatchTime, filter.getPriority(),
                        filter.receiverList.app != null
                                ? filter.receiverList.app.mState.getCurProcState()
                                : ActivityManager.PROCESS_STATE_UNKNOWN);
                // parallel broadcasts are fire-and-forget, not bookended by a call to
                // finishReceiverLocked(), so we manage their activity-start token here
                if (filter.receiverList.app != null
@@ -1174,7 +1186,10 @@ public class BroadcastQueueImpl extends BroadcastQueue {
                                    r.resultData, r.resultExtras, false, false, r.shareIdentity,
                                    r.userId, r.callingUid, r.callingUid, r.callerPackage,
                                    r.dispatchTime - r.enqueueTime,
                                    now - r.dispatchTime, 0);
                                    now - r.dispatchTime, 0,
                                    r.resultToApp != null
                                            ? r.resultToApp.mState.getCurProcState()
                                            : ActivityManager.PROCESS_STATE_UNKNOWN);
                            logBootCompletedBroadcastCompletionLatencyIfPossible(r);
                            // Set this to null so that the reference
                            // (local and remote) isn't kept in the mBroadcastHistory.
@@ -1480,6 +1495,7 @@ public class BroadcastQueueImpl extends BroadcastQueue {
                        r.intent.getAction(), r.getHostingRecordTriggerType()),
                isActivityCapable ? ZYGOTE_POLICY_FLAG_LATENCY_SENSITIVE : ZYGOTE_POLICY_FLAG_EMPTY,
                (r.intent.getFlags() & Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false);
        r.curAppLastProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
        if (r.curApp == null) {
            // Ah, this recipient is unavailable.  Finish it if necessary,
            // and mark the broadcast record as ready for the next.
+7 −1
Original line number Diff line number Diff line
@@ -1002,6 +1002,7 @@ class BroadcastQueueModernImpl extends BroadcastQueue {
                    mService.mPackageManagerInt.grantImplicitAccess(r.userId, r.intent,
                            UserHandle.getAppId(app.uid), r.callingUid, true);
                }
                queue.lastProcessState = app.mState.getCurProcState();
                if (receiver instanceof BroadcastFilter) {
                    notifyScheduleRegisteredReceiver(app, r, (BroadcastFilter) receiver);
                    thread.scheduleRegisteredReceiver(
@@ -1914,12 +1915,16 @@ class BroadcastQueueModernImpl extends BroadcastQueue {
                ? BROADCAST_DELIVERY_EVENT_REPORTED__RECEIVER_TYPE__RUNTIME
                : BROADCAST_DELIVERY_EVENT_REPORTED__RECEIVER_TYPE__MANIFEST;
        final int type;
        final int receiverProcessState;
        if (queue == null) {
            type = BROADCAST_DELIVERY_EVENT_REPORTED__PROC_START_TYPE__PROCESS_START_TYPE_UNKNOWN;
            receiverProcessState = ActivityManager.PROCESS_STATE_UNKNOWN;
        } else if (queue.getActiveViaColdStart()) {
            type = BROADCAST_DELIVERY_EVENT_REPORTED__PROC_START_TYPE__PROCESS_START_TYPE_COLD;
            receiverProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
        } else {
            type = BROADCAST_DELIVERY_EVENT_REPORTED__PROC_START_TYPE__PROCESS_START_TYPE_WARM;
            receiverProcessState = queue.lastProcessState;
        }
        // With the new per-process queues, there's no delay between being
        // "dispatched" and "scheduled", so we report no "receive delay"
@@ -1934,7 +1939,8 @@ class BroadcastQueueModernImpl extends BroadcastQueue {
                    receiverType, type, dispatchDelay, receiveDelay, finishDelay, packageState,
                    app != null ? app.info.packageName : null, r.callerPackage,
                    r.calculateTypeForLogging(), r.getDeliveryGroupPolicy(), r.intent.getFlags(),
                    BroadcastRecord.getReceiverPriority(receiver), r.callerProcState);
                    BroadcastRecord.getReceiverPriority(receiver), r.callerProcState,
                    receiverProcessState);
        }
    }

Loading