Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
| 19 | import java.io.FileDescriptor; |
| 20 | import java.io.PrintWriter; |
| 21 | import java.util.ArrayList; |
| 22 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 24 | import android.app.AppGlobals; |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 25 | import android.app.AppOpsManager; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 26 | import android.content.ComponentName; |
| 27 | import android.content.IIntentReceiver; |
| 28 | import android.content.Intent; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 29 | import android.content.pm.ActivityInfo; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 30 | import android.content.pm.PackageManager; |
| 31 | import android.content.pm.ResolveInfo; |
| 32 | import android.os.Bundle; |
| 33 | import android.os.Handler; |
| 34 | import android.os.IBinder; |
| 35 | import android.os.Message; |
| 36 | import android.os.Process; |
| 37 | import android.os.RemoteException; |
| 38 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 39 | import android.os.UserHandle; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 40 | import android.util.EventLog; |
Dianne Hackborn | 6c5406a | 2012-11-29 16:18:01 -0800 | [diff] [blame] | 41 | import android.util.Log; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 42 | import android.util.Slog; |
| 43 | |
| 44 | /** |
| 45 | * BROADCASTS |
| 46 | * |
| 47 | * We keep two broadcast queues and associated bookkeeping, one for those at |
| 48 | * foreground priority, and one for normal (background-priority) broadcasts. |
| 49 | */ |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 50 | public final class BroadcastQueue { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 51 | static final String TAG = "BroadcastQueue"; |
| 52 | static final String TAG_MU = ActivityManagerService.TAG_MU; |
| 53 | static final boolean DEBUG_BROADCAST = ActivityManagerService.DEBUG_BROADCAST; |
| 54 | static final boolean DEBUG_BROADCAST_LIGHT = ActivityManagerService.DEBUG_BROADCAST_LIGHT; |
| 55 | static final boolean DEBUG_MU = ActivityManagerService.DEBUG_MU; |
| 56 | |
| 57 | static final int MAX_BROADCAST_HISTORY = 25; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 58 | static final int MAX_BROADCAST_SUMMARY_HISTORY = 100; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 59 | |
| 60 | final ActivityManagerService mService; |
| 61 | |
| 62 | /** |
| 63 | * Recognizable moniker for this queue |
| 64 | */ |
| 65 | final String mQueueName; |
| 66 | |
| 67 | /** |
| 68 | * Timeout period for this queue's broadcasts |
| 69 | */ |
| 70 | final long mTimeoutPeriod; |
| 71 | |
| 72 | /** |
| 73 | * Lists of all active broadcasts that are to be executed immediately |
| 74 | * (without waiting for another broadcast to finish). Currently this only |
| 75 | * contains broadcasts to registered receivers, to avoid spinning up |
| 76 | * a bunch of processes to execute IntentReceiver components. Background- |
| 77 | * and foreground-priority broadcasts are queued separately. |
| 78 | */ |
| 79 | final ArrayList<BroadcastRecord> mParallelBroadcasts |
| 80 | = new ArrayList<BroadcastRecord>(); |
| 81 | /** |
| 82 | * List of all active broadcasts that are to be executed one at a time. |
| 83 | * The object at the top of the list is the currently activity broadcasts; |
| 84 | * those after it are waiting for the top to finish. As with parallel |
| 85 | * broadcasts, separate background- and foreground-priority queues are |
| 86 | * maintained. |
| 87 | */ |
| 88 | final ArrayList<BroadcastRecord> mOrderedBroadcasts |
| 89 | = new ArrayList<BroadcastRecord>(); |
| 90 | |
| 91 | /** |
| 92 | * Historical data of past broadcasts, for debugging. |
| 93 | */ |
| 94 | final BroadcastRecord[] mBroadcastHistory |
| 95 | = new BroadcastRecord[MAX_BROADCAST_HISTORY]; |
| 96 | |
| 97 | /** |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 98 | * Summary of historical data of past broadcasts, for debugging. |
| 99 | */ |
| 100 | final Intent[] mBroadcastSummaryHistory |
| 101 | = new Intent[MAX_BROADCAST_SUMMARY_HISTORY]; |
| 102 | |
| 103 | /** |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 104 | * Set when we current have a BROADCAST_INTENT_MSG in flight. |
| 105 | */ |
| 106 | boolean mBroadcastsScheduled = false; |
| 107 | |
| 108 | /** |
| 109 | * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler. |
| 110 | */ |
| 111 | boolean mPendingBroadcastTimeoutMessage; |
| 112 | |
| 113 | /** |
| 114 | * Intent broadcasts that we have tried to start, but are |
| 115 | * waiting for the application's process to be created. We only |
| 116 | * need one per scheduling class (instead of a list) because we always |
| 117 | * process broadcasts one at a time, so no others can be started while |
| 118 | * waiting for this one. |
| 119 | */ |
| 120 | BroadcastRecord mPendingBroadcast = null; |
| 121 | |
| 122 | /** |
| 123 | * The receiver index that is pending, to restart the broadcast if needed. |
| 124 | */ |
| 125 | int mPendingBroadcastRecvIndex; |
| 126 | |
| 127 | static final int BROADCAST_INTENT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG; |
| 128 | static final int BROADCAST_TIMEOUT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 1; |
| 129 | |
| 130 | final Handler mHandler = new Handler() { |
| 131 | //public Handler() { |
| 132 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
| 133 | //} |
| 134 | |
| 135 | public void handleMessage(Message msg) { |
| 136 | switch (msg.what) { |
| 137 | case BROADCAST_INTENT_MSG: { |
| 138 | if (DEBUG_BROADCAST) Slog.v( |
| 139 | TAG, "Received BROADCAST_INTENT_MSG"); |
| 140 | processNextBroadcast(true); |
| 141 | } break; |
| 142 | case BROADCAST_TIMEOUT_MSG: { |
| 143 | synchronized (mService) { |
| 144 | broadcastTimeoutLocked(true); |
| 145 | } |
| 146 | } break; |
| 147 | } |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | private final class AppNotResponding implements Runnable { |
| 152 | private final ProcessRecord mApp; |
| 153 | private final String mAnnotation; |
| 154 | |
| 155 | public AppNotResponding(ProcessRecord app, String annotation) { |
| 156 | mApp = app; |
| 157 | mAnnotation = annotation; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void run() { |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 162 | mService.appNotResponding(mApp, null, null, false, mAnnotation); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | BroadcastQueue(ActivityManagerService service, String name, long timeoutPeriod) { |
| 167 | mService = service; |
| 168 | mQueueName = name; |
| 169 | mTimeoutPeriod = timeoutPeriod; |
| 170 | } |
| 171 | |
| 172 | public boolean isPendingBroadcastProcessLocked(int pid) { |
| 173 | return mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid; |
| 174 | } |
| 175 | |
| 176 | public void enqueueParallelBroadcastLocked(BroadcastRecord r) { |
| 177 | mParallelBroadcasts.add(r); |
| 178 | } |
| 179 | |
| 180 | public void enqueueOrderedBroadcastLocked(BroadcastRecord r) { |
| 181 | mOrderedBroadcasts.add(r); |
| 182 | } |
| 183 | |
| 184 | public final boolean replaceParallelBroadcastLocked(BroadcastRecord r) { |
| 185 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 186 | if (r.intent.filterEquals(mParallelBroadcasts.get(i).intent)) { |
| 187 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 188 | "***** DROPPING PARALLEL [" |
| 189 | + mQueueName + "]: " + r.intent); |
| 190 | mParallelBroadcasts.set(i, r); |
| 191 | return true; |
| 192 | } |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | public final boolean replaceOrderedBroadcastLocked(BroadcastRecord r) { |
| 198 | for (int i=mOrderedBroadcasts.size()-1; i>0; i--) { |
| 199 | if (r.intent.filterEquals(mOrderedBroadcasts.get(i).intent)) { |
| 200 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 201 | "***** DROPPING ORDERED [" |
| 202 | + mQueueName + "]: " + r.intent); |
| 203 | mOrderedBroadcasts.set(i, r); |
| 204 | return true; |
| 205 | } |
| 206 | } |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | private final void processCurBroadcastLocked(BroadcastRecord r, |
| 211 | ProcessRecord app) throws RemoteException { |
| 212 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 213 | "Process cur broadcast " + r + " for app " + app); |
| 214 | if (app.thread == null) { |
| 215 | throw new RemoteException(); |
| 216 | } |
| 217 | r.receiver = app.thread.asBinder(); |
| 218 | r.curApp = app; |
| 219 | app.curReceiver = r; |
Dianne Hackborn | a413dc0 | 2013-07-12 12:02:55 -0700 | [diff] [blame] | 220 | app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 221 | mService.updateLruProcessLocked(app, true); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 222 | |
| 223 | // Tell the application to launch this receiver. |
| 224 | r.intent.setComponent(r.curComponent); |
| 225 | |
| 226 | boolean started = false; |
| 227 | try { |
| 228 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, |
| 229 | "Delivering to component " + r.curComponent |
| 230 | + ": " + r); |
| 231 | mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName()); |
| 232 | app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver, |
| 233 | mService.compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo), |
Dianne Hackborn | a413dc0 | 2013-07-12 12:02:55 -0700 | [diff] [blame] | 234 | r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId, |
| 235 | app.repProcState); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 236 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 237 | "Process cur broadcast " + r + " DELIVERED for app " + app); |
| 238 | started = true; |
| 239 | } finally { |
| 240 | if (!started) { |
| 241 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 242 | "Process cur broadcast " + r + ": NOT STARTED!"); |
| 243 | r.receiver = null; |
| 244 | r.curApp = null; |
| 245 | app.curReceiver = null; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | public boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 251 | boolean didSomething = false; |
| 252 | final BroadcastRecord br = mPendingBroadcast; |
| 253 | if (br != null && br.curApp.pid == app.pid) { |
| 254 | try { |
| 255 | mPendingBroadcast = null; |
| 256 | processCurBroadcastLocked(br, app); |
| 257 | didSomething = true; |
| 258 | } catch (Exception e) { |
| 259 | Slog.w(TAG, "Exception in new application when starting receiver " |
| 260 | + br.curComponent.flattenToShortString(), e); |
| 261 | logBroadcastReceiverDiscardLocked(br); |
| 262 | finishReceiverLocked(br, br.resultCode, br.resultData, |
| 263 | br.resultExtras, br.resultAbort, true); |
| 264 | scheduleBroadcastsLocked(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 265 | // We need to reset the state if we failed to start the receiver. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 266 | br.state = BroadcastRecord.IDLE; |
| 267 | throw new RuntimeException(e.getMessage()); |
| 268 | } |
| 269 | } |
| 270 | return didSomething; |
| 271 | } |
| 272 | |
| 273 | public void skipPendingBroadcastLocked(int pid) { |
| 274 | final BroadcastRecord br = mPendingBroadcast; |
| 275 | if (br != null && br.curApp.pid == pid) { |
| 276 | br.state = BroadcastRecord.IDLE; |
| 277 | br.nextReceiver = mPendingBroadcastRecvIndex; |
| 278 | mPendingBroadcast = null; |
| 279 | scheduleBroadcastsLocked(); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | public void skipCurrentReceiverLocked(ProcessRecord app) { |
| 284 | boolean reschedule = false; |
| 285 | BroadcastRecord r = app.curReceiver; |
| 286 | if (r != null) { |
| 287 | // The current broadcast is waiting for this app's receiver |
| 288 | // to be finished. Looks like that's not going to happen, so |
| 289 | // let the broadcast continue. |
| 290 | logBroadcastReceiverDiscardLocked(r); |
| 291 | finishReceiverLocked(r, r.resultCode, r.resultData, |
| 292 | r.resultExtras, r.resultAbort, true); |
| 293 | reschedule = true; |
| 294 | } |
| 295 | |
| 296 | r = mPendingBroadcast; |
| 297 | if (r != null && r.curApp == app) { |
| 298 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 299 | "[" + mQueueName + "] skip & discard pending app " + r); |
| 300 | logBroadcastReceiverDiscardLocked(r); |
| 301 | finishReceiverLocked(r, r.resultCode, r.resultData, |
| 302 | r.resultExtras, r.resultAbort, true); |
| 303 | reschedule = true; |
| 304 | } |
| 305 | if (reschedule) { |
| 306 | scheduleBroadcastsLocked(); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | public void scheduleBroadcastsLocked() { |
| 311 | if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts [" |
| 312 | + mQueueName + "]: current=" |
| 313 | + mBroadcastsScheduled); |
| 314 | |
| 315 | if (mBroadcastsScheduled) { |
| 316 | return; |
| 317 | } |
| 318 | mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this)); |
| 319 | mBroadcastsScheduled = true; |
| 320 | } |
| 321 | |
| 322 | public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) { |
| 323 | if (mOrderedBroadcasts.size() > 0) { |
| 324 | final BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 325 | if (r != null && r.receiver == receiver) { |
| 326 | return r; |
| 327 | } |
| 328 | } |
| 329 | return null; |
| 330 | } |
| 331 | |
| 332 | public boolean finishReceiverLocked(BroadcastRecord r, int resultCode, |
| 333 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 334 | boolean explicit) { |
| 335 | int state = r.state; |
| 336 | r.state = BroadcastRecord.IDLE; |
| 337 | if (state == BroadcastRecord.IDLE) { |
| 338 | if (explicit) { |
| 339 | Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE"); |
| 340 | } |
| 341 | } |
| 342 | r.receiver = null; |
| 343 | r.intent.setComponent(null); |
| 344 | if (r.curApp != null) { |
| 345 | r.curApp.curReceiver = null; |
| 346 | } |
| 347 | if (r.curFilter != null) { |
| 348 | r.curFilter.receiverList.curBroadcast = null; |
| 349 | } |
| 350 | r.curFilter = null; |
| 351 | r.curApp = null; |
| 352 | r.curComponent = null; |
| 353 | r.curReceiver = null; |
| 354 | mPendingBroadcast = null; |
| 355 | |
| 356 | r.resultCode = resultCode; |
| 357 | r.resultData = resultData; |
| 358 | r.resultExtras = resultExtras; |
| 359 | r.resultAbort = resultAbort; |
| 360 | |
| 361 | // We will process the next receiver right now if this is finishing |
| 362 | // an app receiver (which is always asynchronous) or after we have |
| 363 | // come back from calling a receiver. |
| 364 | return state == BroadcastRecord.APP_RECEIVE |
| 365 | || state == BroadcastRecord.CALL_DONE_RECEIVE; |
| 366 | } |
| 367 | |
| 368 | private static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver, |
| 369 | Intent intent, int resultCode, String data, Bundle extras, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 370 | boolean ordered, boolean sticky, int sendingUser) throws RemoteException { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 371 | // Send the intent to the receiver asynchronously using one-way binder calls. |
| 372 | if (app != null && app.thread != null) { |
| 373 | // If we have an app thread, do the call through that so it is |
| 374 | // correctly ordered with other one-way calls. |
| 375 | app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode, |
Dianne Hackborn | a413dc0 | 2013-07-12 12:02:55 -0700 | [diff] [blame] | 376 | data, extras, ordered, sticky, sendingUser, app.repProcState); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 377 | } else { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 378 | receiver.performReceive(intent, resultCode, data, extras, ordered, |
| 379 | sticky, sendingUser); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | private final void deliverToRegisteredReceiverLocked(BroadcastRecord r, |
| 384 | BroadcastFilter filter, boolean ordered) { |
| 385 | boolean skip = false; |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 386 | if (filter.requiredPermission != null) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 387 | int perm = mService.checkComponentPermission(filter.requiredPermission, |
| 388 | r.callingPid, r.callingUid, -1, true); |
| 389 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 390 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 391 | + r.intent.toString() |
| 392 | + " from " + r.callerPackage + " (pid=" |
| 393 | + r.callingPid + ", uid=" + r.callingUid + ")" |
| 394 | + " requires " + filter.requiredPermission |
| 395 | + " due to registered receiver " + filter); |
| 396 | skip = true; |
| 397 | } |
| 398 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 399 | if (!skip && r.requiredPermission != null) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 400 | int perm = mService.checkComponentPermission(r.requiredPermission, |
| 401 | filter.receiverList.pid, filter.receiverList.uid, -1, true); |
| 402 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 403 | Slog.w(TAG, "Permission Denial: receiving " |
| 404 | + r.intent.toString() |
| 405 | + " to " + filter.receiverList.app |
| 406 | + " (pid=" + filter.receiverList.pid |
| 407 | + ", uid=" + filter.receiverList.uid + ")" |
| 408 | + " requires " + r.requiredPermission |
| 409 | + " due to sender " + r.callerPackage |
| 410 | + " (uid " + r.callingUid + ")"); |
| 411 | skip = true; |
| 412 | } |
| 413 | } |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 414 | if (r.appOp != AppOpsManager.OP_NONE) { |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 415 | int mode = mService.mAppOpsService.noteOperation(r.appOp, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 416 | filter.receiverList.uid, filter.packageName); |
| 417 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 418 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 419 | "App op " + r.appOp + " not allowed for broadcast to uid " |
| 420 | + filter.receiverList.uid + " pkg " + filter.packageName); |
| 421 | skip = true; |
| 422 | } |
| 423 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 424 | |
| 425 | if (!skip) { |
| 426 | // If this is not being sent as an ordered broadcast, then we |
| 427 | // don't want to touch the fields that keep track of the current |
| 428 | // state of ordered broadcasts. |
| 429 | if (ordered) { |
| 430 | r.receiver = filter.receiverList.receiver.asBinder(); |
| 431 | r.curFilter = filter; |
| 432 | filter.receiverList.curBroadcast = r; |
| 433 | r.state = BroadcastRecord.CALL_IN_RECEIVE; |
| 434 | if (filter.receiverList.app != null) { |
| 435 | // Bump hosting application to no longer be in background |
| 436 | // scheduling class. Note that we can't do that if there |
| 437 | // isn't an app... but we can only be in that case for |
| 438 | // things that directly call the IActivityManager API, which |
| 439 | // are already core system stuff so don't matter for this. |
| 440 | r.curApp = filter.receiverList.app; |
| 441 | filter.receiverList.app.curReceiver = r; |
Dianne Hackborn | a413dc0 | 2013-07-12 12:02:55 -0700 | [diff] [blame] | 442 | mService.updateOomAdjLocked(r.curApp, true); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | try { |
| 446 | if (DEBUG_BROADCAST_LIGHT) { |
| 447 | int seq = r.intent.getIntExtra("seq", -1); |
| 448 | Slog.i(TAG, "Delivering to " + filter |
| 449 | + " (seq=" + seq + "): " + r); |
| 450 | } |
| 451 | performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 452 | new Intent(r.intent), r.resultCode, r.resultData, |
| 453 | r.resultExtras, r.ordered, r.initialSticky, r.userId); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 454 | if (ordered) { |
| 455 | r.state = BroadcastRecord.CALL_DONE_RECEIVE; |
| 456 | } |
| 457 | } catch (RemoteException e) { |
| 458 | Slog.w(TAG, "Failure sending broadcast " + r.intent, e); |
| 459 | if (ordered) { |
| 460 | r.receiver = null; |
| 461 | r.curFilter = null; |
| 462 | filter.receiverList.curBroadcast = null; |
| 463 | if (filter.receiverList.app != null) { |
| 464 | filter.receiverList.app.curReceiver = null; |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | final void processNextBroadcast(boolean fromMsg) { |
| 472 | synchronized(mService) { |
| 473 | BroadcastRecord r; |
| 474 | |
| 475 | if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast [" |
| 476 | + mQueueName + "]: " |
| 477 | + mParallelBroadcasts.size() + " broadcasts, " |
| 478 | + mOrderedBroadcasts.size() + " ordered broadcasts"); |
| 479 | |
| 480 | mService.updateCpuStats(); |
| 481 | |
| 482 | if (fromMsg) { |
| 483 | mBroadcastsScheduled = false; |
| 484 | } |
| 485 | |
| 486 | // First, deliver any non-serialized broadcasts right away. |
| 487 | while (mParallelBroadcasts.size() > 0) { |
| 488 | r = mParallelBroadcasts.remove(0); |
| 489 | r.dispatchTime = SystemClock.uptimeMillis(); |
| 490 | r.dispatchClockTime = System.currentTimeMillis(); |
| 491 | final int N = r.receivers.size(); |
| 492 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast [" |
| 493 | + mQueueName + "] " + r); |
| 494 | for (int i=0; i<N; i++) { |
| 495 | Object target = r.receivers.get(i); |
| 496 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 497 | "Delivering non-ordered on [" + mQueueName + "] to registered " |
| 498 | + target + ": " + r); |
| 499 | deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false); |
| 500 | } |
| 501 | addBroadcastToHistoryLocked(r); |
| 502 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast [" |
| 503 | + mQueueName + "] " + r); |
| 504 | } |
| 505 | |
| 506 | // Now take care of the next serialized one... |
| 507 | |
| 508 | // If we are waiting for a process to come up to handle the next |
| 509 | // broadcast, then do nothing at this point. Just in case, we |
| 510 | // check that the process we're waiting for still exists. |
| 511 | if (mPendingBroadcast != null) { |
| 512 | if (DEBUG_BROADCAST_LIGHT) { |
| 513 | Slog.v(TAG, "processNextBroadcast [" |
| 514 | + mQueueName + "]: waiting for " |
| 515 | + mPendingBroadcast.curApp); |
| 516 | } |
| 517 | |
| 518 | boolean isDead; |
| 519 | synchronized (mService.mPidsSelfLocked) { |
| 520 | isDead = (mService.mPidsSelfLocked.get( |
| 521 | mPendingBroadcast.curApp.pid) == null); |
| 522 | } |
| 523 | if (!isDead) { |
| 524 | // It's still alive, so keep waiting |
| 525 | return; |
| 526 | } else { |
| 527 | Slog.w(TAG, "pending app [" |
| 528 | + mQueueName + "]" + mPendingBroadcast.curApp |
| 529 | + " died before responding to broadcast"); |
| 530 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 531 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
| 532 | mPendingBroadcast = null; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | boolean looped = false; |
| 537 | |
| 538 | do { |
| 539 | if (mOrderedBroadcasts.size() == 0) { |
| 540 | // No more broadcasts pending, so all done! |
| 541 | mService.scheduleAppGcsLocked(); |
| 542 | if (looped) { |
| 543 | // If we had finished the last ordered broadcast, then |
| 544 | // make sure all processes have correct oom and sched |
| 545 | // adjustments. |
| 546 | mService.updateOomAdjLocked(); |
| 547 | } |
| 548 | return; |
| 549 | } |
| 550 | r = mOrderedBroadcasts.get(0); |
| 551 | boolean forceReceive = false; |
| 552 | |
| 553 | // Ensure that even if something goes awry with the timeout |
| 554 | // detection, we catch "hung" broadcasts here, discard them, |
| 555 | // and continue to make progress. |
| 556 | // |
| 557 | // This is only done if the system is ready so that PRE_BOOT_COMPLETED |
| 558 | // receivers don't get executed with timeouts. They're intended for |
| 559 | // one time heavy lifting after system upgrades and can take |
| 560 | // significant amounts of time. |
| 561 | int numReceivers = (r.receivers != null) ? r.receivers.size() : 0; |
| 562 | if (mService.mProcessesReady && r.dispatchTime > 0) { |
| 563 | long now = SystemClock.uptimeMillis(); |
| 564 | if ((numReceivers > 0) && |
| 565 | (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) { |
| 566 | Slog.w(TAG, "Hung broadcast [" |
| 567 | + mQueueName + "] discarded after timeout failure:" |
| 568 | + " now=" + now |
| 569 | + " dispatchTime=" + r.dispatchTime |
| 570 | + " startTime=" + r.receiverTime |
| 571 | + " intent=" + r.intent |
| 572 | + " numReceivers=" + numReceivers |
| 573 | + " nextReceiver=" + r.nextReceiver |
| 574 | + " state=" + r.state); |
| 575 | broadcastTimeoutLocked(false); // forcibly finish this broadcast |
| 576 | forceReceive = true; |
| 577 | r.state = BroadcastRecord.IDLE; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if (r.state != BroadcastRecord.IDLE) { |
| 582 | if (DEBUG_BROADCAST) Slog.d(TAG, |
| 583 | "processNextBroadcast(" |
| 584 | + mQueueName + ") called when not idle (state=" |
| 585 | + r.state + ")"); |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | if (r.receivers == null || r.nextReceiver >= numReceivers |
| 590 | || r.resultAbort || forceReceive) { |
| 591 | // No more receivers for this broadcast! Send the final |
| 592 | // result if requested... |
| 593 | if (r.resultTo != null) { |
| 594 | try { |
| 595 | if (DEBUG_BROADCAST) { |
| 596 | int seq = r.intent.getIntExtra("seq", -1); |
| 597 | Slog.i(TAG, "Finishing broadcast [" |
| 598 | + mQueueName + "] " + r.intent.getAction() |
| 599 | + " seq=" + seq + " app=" + r.callerApp); |
| 600 | } |
| 601 | performReceiveLocked(r.callerApp, r.resultTo, |
| 602 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 603 | r.resultData, r.resultExtras, false, false, r.userId); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 604 | // Set this to null so that the reference |
| 605 | // (local and remote) isnt kept in the mBroadcastHistory. |
| 606 | r.resultTo = null; |
| 607 | } catch (RemoteException e) { |
| 608 | Slog.w(TAG, "Failure [" |
| 609 | + mQueueName + "] sending broadcast result of " |
| 610 | + r.intent, e); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG"); |
| 615 | cancelBroadcastTimeoutLocked(); |
| 616 | |
| 617 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast " |
| 618 | + r); |
| 619 | |
| 620 | // ... and on to the next... |
| 621 | addBroadcastToHistoryLocked(r); |
| 622 | mOrderedBroadcasts.remove(0); |
| 623 | r = null; |
| 624 | looped = true; |
| 625 | continue; |
| 626 | } |
| 627 | } while (r == null); |
| 628 | |
| 629 | // Get the next receiver... |
| 630 | int recIdx = r.nextReceiver++; |
| 631 | |
| 632 | // Keep track of when this receiver started, and make sure there |
| 633 | // is a timeout message pending to kill it if need be. |
| 634 | r.receiverTime = SystemClock.uptimeMillis(); |
| 635 | if (recIdx == 0) { |
| 636 | r.dispatchTime = r.receiverTime; |
| 637 | r.dispatchClockTime = System.currentTimeMillis(); |
| 638 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast [" |
| 639 | + mQueueName + "] " + r); |
| 640 | } |
| 641 | if (! mPendingBroadcastTimeoutMessage) { |
| 642 | long timeoutTime = r.receiverTime + mTimeoutPeriod; |
| 643 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 644 | "Submitting BROADCAST_TIMEOUT_MSG [" |
| 645 | + mQueueName + "] for " + r + " at " + timeoutTime); |
| 646 | setBroadcastTimeoutLocked(timeoutTime); |
| 647 | } |
| 648 | |
| 649 | Object nextReceiver = r.receivers.get(recIdx); |
| 650 | if (nextReceiver instanceof BroadcastFilter) { |
| 651 | // Simple case: this is a registered receiver who gets |
| 652 | // a direct call. |
| 653 | BroadcastFilter filter = (BroadcastFilter)nextReceiver; |
| 654 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 655 | "Delivering ordered [" |
| 656 | + mQueueName + "] to registered " |
| 657 | + filter + ": " + r); |
| 658 | deliverToRegisteredReceiverLocked(r, filter, r.ordered); |
| 659 | if (r.receiver == null || !r.ordered) { |
| 660 | // The receiver has already finished, so schedule to |
| 661 | // process the next one. |
| 662 | if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing [" |
| 663 | + mQueueName + "]: ordered=" |
| 664 | + r.ordered + " receiver=" + r.receiver); |
| 665 | r.state = BroadcastRecord.IDLE; |
| 666 | scheduleBroadcastsLocked(); |
| 667 | } |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | // Hard case: need to instantiate the receiver, possibly |
| 672 | // starting its application process to host it. |
| 673 | |
| 674 | ResolveInfo info = |
| 675 | (ResolveInfo)nextReceiver; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 676 | ComponentName component = new ComponentName( |
| 677 | info.activityInfo.applicationInfo.packageName, |
| 678 | info.activityInfo.name); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 679 | |
| 680 | boolean skip = false; |
| 681 | int perm = mService.checkComponentPermission(info.activityInfo.permission, |
| 682 | r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid, |
| 683 | info.activityInfo.exported); |
| 684 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 685 | if (!info.activityInfo.exported) { |
| 686 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 687 | + r.intent.toString() |
| 688 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 689 | + ", uid=" + r.callingUid + ")" |
| 690 | + " is not exported from uid " + info.activityInfo.applicationInfo.uid |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 691 | + " due to receiver " + component.flattenToShortString()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 692 | } else { |
| 693 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 694 | + r.intent.toString() |
| 695 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 696 | + ", uid=" + r.callingUid + ")" |
| 697 | + " requires " + info.activityInfo.permission |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 698 | + " due to receiver " + component.flattenToShortString()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 699 | } |
| 700 | skip = true; |
| 701 | } |
| 702 | if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID && |
| 703 | r.requiredPermission != null) { |
| 704 | try { |
| 705 | perm = AppGlobals.getPackageManager(). |
| 706 | checkPermission(r.requiredPermission, |
| 707 | info.activityInfo.applicationInfo.packageName); |
| 708 | } catch (RemoteException e) { |
| 709 | perm = PackageManager.PERMISSION_DENIED; |
| 710 | } |
| 711 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 712 | Slog.w(TAG, "Permission Denial: receiving " |
| 713 | + r.intent + " to " |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 714 | + component.flattenToShortString() |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 715 | + " requires " + r.requiredPermission |
| 716 | + " due to sender " + r.callerPackage |
| 717 | + " (uid " + r.callingUid + ")"); |
| 718 | skip = true; |
| 719 | } |
| 720 | } |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 721 | if (r.appOp != AppOpsManager.OP_NONE) { |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 722 | int mode = mService.mAppOpsService.noteOperation(r.appOp, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 723 | info.activityInfo.applicationInfo.uid, info.activityInfo.packageName); |
| 724 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 725 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 726 | "App op " + r.appOp + " not allowed for broadcast to uid " |
| 727 | + info.activityInfo.applicationInfo.uid + " pkg " |
| 728 | + info.activityInfo.packageName); |
| 729 | skip = true; |
| 730 | } |
| 731 | } |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 732 | boolean isSingleton = false; |
| 733 | try { |
| 734 | isSingleton = mService.isSingleton(info.activityInfo.processName, |
| 735 | info.activityInfo.applicationInfo, |
| 736 | info.activityInfo.name, info.activityInfo.flags); |
| 737 | } catch (SecurityException e) { |
| 738 | Slog.w(TAG, e.getMessage()); |
| 739 | skip = true; |
| 740 | } |
| 741 | if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 742 | if (ActivityManager.checkUidPermission( |
| 743 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 744 | info.activityInfo.applicationInfo.uid) |
| 745 | != PackageManager.PERMISSION_GRANTED) { |
| 746 | Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString() |
| 747 | + " requests FLAG_SINGLE_USER, but app does not hold " |
| 748 | + android.Manifest.permission.INTERACT_ACROSS_USERS); |
| 749 | skip = true; |
| 750 | } |
| 751 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 752 | if (r.curApp != null && r.curApp.crashing) { |
| 753 | // If the target process is crashing, just skip it. |
| 754 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 755 | "Skipping deliver ordered [" |
| 756 | + mQueueName + "] " + r + " to " + r.curApp |
| 757 | + ": process crashing"); |
| 758 | skip = true; |
| 759 | } |
| 760 | |
| 761 | if (skip) { |
| 762 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 763 | "Skipping delivery of ordered [" |
| 764 | + mQueueName + "] " + r + " for whatever reason"); |
| 765 | r.receiver = null; |
| 766 | r.curFilter = null; |
| 767 | r.state = BroadcastRecord.IDLE; |
| 768 | scheduleBroadcastsLocked(); |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | r.state = BroadcastRecord.APP_RECEIVE; |
| 773 | String targetProcess = info.activityInfo.processName; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 774 | r.curComponent = component; |
| 775 | if (r.callingUid != Process.SYSTEM_UID && isSingleton) { |
| 776 | info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 777 | } |
| 778 | r.curReceiver = info.activityInfo; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 779 | if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 780 | Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, " |
| 781 | + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = " |
| 782 | + info.activityInfo.applicationInfo.uid); |
| 783 | } |
| 784 | |
| 785 | // Broadcast is being executed, its package can't be stopped. |
| 786 | try { |
| 787 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 788 | r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid)); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 789 | } catch (RemoteException e) { |
| 790 | } catch (IllegalArgumentException e) { |
| 791 | Slog.w(TAG, "Failed trying to unstop package " |
| 792 | + r.curComponent.getPackageName() + ": " + e); |
| 793 | } |
| 794 | |
| 795 | // Is this receiver's application already running? |
| 796 | ProcessRecord app = mService.getProcessRecordLocked(targetProcess, |
| 797 | info.activityInfo.applicationInfo.uid); |
| 798 | if (app != null && app.thread != null) { |
| 799 | try { |
Dianne Hackborn | 78a369c | 2013-06-11 17:10:32 -0700 | [diff] [blame] | 800 | app.addPackage(info.activityInfo.packageName, mService.mProcessTracker); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 801 | processCurBroadcastLocked(r, app); |
| 802 | return; |
| 803 | } catch (RemoteException e) { |
| 804 | Slog.w(TAG, "Exception when sending broadcast to " |
| 805 | + r.curComponent, e); |
Dianne Hackborn | 6c5406a | 2012-11-29 16:18:01 -0800 | [diff] [blame] | 806 | } catch (RuntimeException e) { |
| 807 | Log.wtf(TAG, "Failed sending broadcast to " |
| 808 | + r.curComponent + " with " + r.intent, e); |
| 809 | // If some unexpected exception happened, just skip |
| 810 | // this broadcast. At this point we are not in the call |
| 811 | // from a client, so throwing an exception out from here |
| 812 | // will crash the entire system instead of just whoever |
| 813 | // sent the broadcast. |
| 814 | logBroadcastReceiverDiscardLocked(r); |
| 815 | finishReceiverLocked(r, r.resultCode, r.resultData, |
| 816 | r.resultExtras, r.resultAbort, true); |
| 817 | scheduleBroadcastsLocked(); |
| 818 | // We need to reset the state if we failed to start the receiver. |
| 819 | r.state = BroadcastRecord.IDLE; |
| 820 | return; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | // If a dead object exception was thrown -- fall through to |
| 824 | // restart the application. |
| 825 | } |
| 826 | |
| 827 | // Not running -- get it started, to be executed when the app comes up. |
| 828 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 829 | "Need to start app [" |
| 830 | + mQueueName + "] " + targetProcess + " for broadcast " + r); |
| 831 | if ((r.curApp=mService.startProcessLocked(targetProcess, |
| 832 | info.activityInfo.applicationInfo, true, |
| 833 | r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND, |
| 834 | "broadcast", r.curComponent, |
| 835 | (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false)) |
| 836 | == null) { |
| 837 | // Ah, this recipient is unavailable. Finish it if necessary, |
| 838 | // and mark the broadcast record as ready for the next. |
| 839 | Slog.w(TAG, "Unable to launch app " |
| 840 | + info.activityInfo.applicationInfo.packageName + "/" |
| 841 | + info.activityInfo.applicationInfo.uid + " for broadcast " |
| 842 | + r.intent + ": process is bad"); |
| 843 | logBroadcastReceiverDiscardLocked(r); |
| 844 | finishReceiverLocked(r, r.resultCode, r.resultData, |
| 845 | r.resultExtras, r.resultAbort, true); |
| 846 | scheduleBroadcastsLocked(); |
| 847 | r.state = BroadcastRecord.IDLE; |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | mPendingBroadcast = r; |
| 852 | mPendingBroadcastRecvIndex = recIdx; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | final void setBroadcastTimeoutLocked(long timeoutTime) { |
| 857 | if (! mPendingBroadcastTimeoutMessage) { |
| 858 | Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this); |
| 859 | mHandler.sendMessageAtTime(msg, timeoutTime); |
| 860 | mPendingBroadcastTimeoutMessage = true; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | final void cancelBroadcastTimeoutLocked() { |
| 865 | if (mPendingBroadcastTimeoutMessage) { |
| 866 | mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this); |
| 867 | mPendingBroadcastTimeoutMessage = false; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | final void broadcastTimeoutLocked(boolean fromMsg) { |
| 872 | if (fromMsg) { |
| 873 | mPendingBroadcastTimeoutMessage = false; |
| 874 | } |
| 875 | |
| 876 | if (mOrderedBroadcasts.size() == 0) { |
| 877 | return; |
| 878 | } |
| 879 | |
| 880 | long now = SystemClock.uptimeMillis(); |
| 881 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 882 | if (fromMsg) { |
| 883 | if (mService.mDidDexOpt) { |
| 884 | // Delay timeouts until dexopt finishes. |
| 885 | mService.mDidDexOpt = false; |
| 886 | long timeoutTime = SystemClock.uptimeMillis() + mTimeoutPeriod; |
| 887 | setBroadcastTimeoutLocked(timeoutTime); |
| 888 | return; |
| 889 | } |
| 890 | if (!mService.mProcessesReady) { |
| 891 | // Only process broadcast timeouts if the system is ready. That way |
| 892 | // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended |
| 893 | // to do heavy lifting for system up. |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | long timeoutTime = r.receiverTime + mTimeoutPeriod; |
| 898 | if (timeoutTime > now) { |
| 899 | // We can observe premature timeouts because we do not cancel and reset the |
| 900 | // broadcast timeout message after each receiver finishes. Instead, we set up |
| 901 | // an initial timeout then kick it down the road a little further as needed |
| 902 | // when it expires. |
| 903 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 904 | "Premature timeout [" |
| 905 | + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for " |
| 906 | + timeoutTime); |
| 907 | setBroadcastTimeoutLocked(timeoutTime); |
| 908 | return; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r.receiver |
| 913 | + ", started " + (now - r.receiverTime) + "ms ago"); |
| 914 | r.receiverTime = now; |
| 915 | r.anrCount++; |
| 916 | |
| 917 | // Current receiver has passed its expiration date. |
| 918 | if (r.nextReceiver <= 0) { |
| 919 | Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0"); |
| 920 | return; |
| 921 | } |
| 922 | |
| 923 | ProcessRecord app = null; |
| 924 | String anrMessage = null; |
| 925 | |
| 926 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 927 | Slog.w(TAG, "Receiver during timeout: " + curReceiver); |
| 928 | logBroadcastReceiverDiscardLocked(r); |
| 929 | if (curReceiver instanceof BroadcastFilter) { |
| 930 | BroadcastFilter bf = (BroadcastFilter)curReceiver; |
| 931 | if (bf.receiverList.pid != 0 |
| 932 | && bf.receiverList.pid != ActivityManagerService.MY_PID) { |
| 933 | synchronized (mService.mPidsSelfLocked) { |
| 934 | app = mService.mPidsSelfLocked.get( |
| 935 | bf.receiverList.pid); |
| 936 | } |
| 937 | } |
| 938 | } else { |
| 939 | app = r.curApp; |
| 940 | } |
| 941 | |
| 942 | if (app != null) { |
| 943 | anrMessage = "Broadcast of " + r.intent.toString(); |
| 944 | } |
| 945 | |
| 946 | if (mPendingBroadcast == r) { |
| 947 | mPendingBroadcast = null; |
| 948 | } |
| 949 | |
| 950 | // Move on to the next receiver. |
| 951 | finishReceiverLocked(r, r.resultCode, r.resultData, |
| 952 | r.resultExtras, r.resultAbort, true); |
| 953 | scheduleBroadcastsLocked(); |
| 954 | |
| 955 | if (anrMessage != null) { |
| 956 | // Post the ANR to the handler since we do not want to process ANRs while |
| 957 | // potentially holding our lock. |
| 958 | mHandler.post(new AppNotResponding(app, anrMessage)); |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | private final void addBroadcastToHistoryLocked(BroadcastRecord r) { |
| 963 | if (r.callingUid < 0) { |
| 964 | // This was from a registerReceiver() call; ignore it. |
| 965 | return; |
| 966 | } |
| 967 | System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1, |
| 968 | MAX_BROADCAST_HISTORY-1); |
| 969 | r.finishTime = SystemClock.uptimeMillis(); |
| 970 | mBroadcastHistory[0] = r; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 971 | System.arraycopy(mBroadcastSummaryHistory, 0, mBroadcastSummaryHistory, 1, |
| 972 | MAX_BROADCAST_SUMMARY_HISTORY-1); |
| 973 | mBroadcastSummaryHistory[0] = r.intent; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) { |
| 977 | if (r.nextReceiver > 0) { |
| 978 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 979 | if (curReceiver instanceof BroadcastFilter) { |
| 980 | BroadcastFilter bf = (BroadcastFilter) curReceiver; |
| 981 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 982 | bf.owningUserId, System.identityHashCode(r), |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 983 | r.intent.getAction(), |
| 984 | r.nextReceiver - 1, |
| 985 | System.identityHashCode(bf)); |
| 986 | } else { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 987 | ResolveInfo ri = (ResolveInfo)curReceiver; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 988 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 989 | UserHandle.getUserId(ri.activityInfo.applicationInfo.uid), |
| 990 | System.identityHashCode(r), r.intent.getAction(), |
| 991 | r.nextReceiver - 1, ri.toString()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 992 | } |
| 993 | } else { |
| 994 | Slog.w(TAG, "Discarding broadcast before first receiver is invoked: " |
| 995 | + r); |
| 996 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 997 | -1, System.identityHashCode(r), |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 998 | r.intent.getAction(), |
| 999 | r.nextReceiver, |
| 1000 | "NONE"); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 1005 | int opti, boolean dumpAll, String dumpPackage, boolean needSep) { |
| 1006 | if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0 |
| 1007 | || mPendingBroadcast != null) { |
| 1008 | boolean printed = false; |
| 1009 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 1010 | BroadcastRecord br = mParallelBroadcasts.get(i); |
| 1011 | if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { |
| 1012 | continue; |
| 1013 | } |
| 1014 | if (!printed) { |
| 1015 | if (needSep) { |
| 1016 | pw.println(); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1017 | } |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1018 | needSep = true; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1019 | printed = true; |
| 1020 | pw.println(" Active broadcasts [" + mQueueName + "]:"); |
| 1021 | } |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1022 | pw.println(" Active Broadcast " + mQueueName + " #" + i + ":"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1023 | br.dump(pw, " "); |
| 1024 | } |
| 1025 | printed = false; |
| 1026 | needSep = true; |
| 1027 | for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) { |
| 1028 | BroadcastRecord br = mOrderedBroadcasts.get(i); |
| 1029 | if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { |
| 1030 | continue; |
| 1031 | } |
| 1032 | if (!printed) { |
| 1033 | if (needSep) { |
| 1034 | pw.println(); |
| 1035 | } |
| 1036 | needSep = true; |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1037 | printed = true; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1038 | pw.println(" Active ordered broadcasts [" + mQueueName + "]:"); |
| 1039 | } |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1040 | pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1041 | mOrderedBroadcasts.get(i).dump(pw, " "); |
| 1042 | } |
| 1043 | if (dumpPackage == null || (mPendingBroadcast != null |
| 1044 | && dumpPackage.equals(mPendingBroadcast.callerPackage))) { |
| 1045 | if (needSep) { |
| 1046 | pw.println(); |
| 1047 | } |
| 1048 | pw.println(" Pending broadcast [" + mQueueName + "]:"); |
| 1049 | if (mPendingBroadcast != null) { |
| 1050 | mPendingBroadcast.dump(pw, " "); |
| 1051 | } else { |
| 1052 | pw.println(" (null)"); |
| 1053 | } |
| 1054 | needSep = true; |
| 1055 | } |
| 1056 | } |
| 1057 | |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1058 | int i; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1059 | boolean printed = false; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1060 | for (i=0; i<MAX_BROADCAST_HISTORY; i++) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1061 | BroadcastRecord r = mBroadcastHistory[i]; |
| 1062 | if (r == null) { |
| 1063 | break; |
| 1064 | } |
| 1065 | if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) { |
| 1066 | continue; |
| 1067 | } |
| 1068 | if (!printed) { |
| 1069 | if (needSep) { |
| 1070 | pw.println(); |
| 1071 | } |
| 1072 | needSep = true; |
| 1073 | pw.println(" Historical broadcasts [" + mQueueName + "]:"); |
| 1074 | printed = true; |
| 1075 | } |
| 1076 | if (dumpAll) { |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1077 | pw.print(" Historical Broadcast " + mQueueName + " #"); |
| 1078 | pw.print(i); pw.println(":"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1079 | r.dump(pw, " "); |
| 1080 | } else { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1081 | pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r); |
| 1082 | pw.print(" "); |
| 1083 | pw.println(r.intent.toShortString(false, true, true, false)); |
Dianne Hackborn | a40cfeb | 2013-03-25 17:49:36 -0700 | [diff] [blame] | 1084 | if (r.targetComp != null && r.targetComp != r.intent.getComponent()) { |
| 1085 | pw.print(" targetComp: "); pw.println(r.targetComp.toShortString()); |
| 1086 | } |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1087 | Bundle bundle = r.intent.getExtras(); |
| 1088 | if (bundle != null) { |
| 1089 | pw.print(" extras: "); pw.println(bundle.toString()); |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | if (dumpPackage == null) { |
| 1095 | if (dumpAll) { |
| 1096 | i = 0; |
| 1097 | printed = false; |
| 1098 | } |
| 1099 | for (; i<MAX_BROADCAST_SUMMARY_HISTORY; i++) { |
| 1100 | Intent intent = mBroadcastSummaryHistory[i]; |
| 1101 | if (intent == null) { |
| 1102 | break; |
| 1103 | } |
| 1104 | if (!printed) { |
| 1105 | if (needSep) { |
| 1106 | pw.println(); |
| 1107 | } |
| 1108 | needSep = true; |
| 1109 | pw.println(" Historical broadcasts summary [" + mQueueName + "]:"); |
| 1110 | printed = true; |
| 1111 | } |
| 1112 | if (!dumpAll && i >= 50) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1113 | pw.println(" ..."); |
| 1114 | break; |
| 1115 | } |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1116 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 1117 | pw.println(intent.toShortString(false, true, true, false)); |
| 1118 | Bundle bundle = intent.getExtras(); |
| 1119 | if (bundle != null) { |
| 1120 | pw.print(" extras: "); pw.println(bundle.toString()); |
| 1121 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | return needSep; |
| 1126 | } |
| 1127 | } |