美文网首页
BroadcastReceiver原理

BroadcastReceiver原理

作者: gaoqi03 | 来源:发表于2019-01-16 16:56 被阅读10次

    BroadcastReceiver:

    Activity 通过注册,在ContextImpl中调研AMS registerReceiver();

    AMS registerReceiver()中;

    1 检查 : 

      检查注册的app,是否还存在。

    if (callerApp.info.uid != SYSTEM_UID &&

            !callerApp.pkgList.containsKey(callerPackage) {

    …..

    }

    申请注册的参数是否来自应用自身:

    if (callerApp.info.uid != SYSTEM_UID &&

            !callerApp.pkgList.containsKey(callerPackage) &&

            !"android".equals(callerPackage)) {

        throw new SecurityException("Given caller package " + callerPackage

                + " is not running in process " + callerApp);

    }

    是否是 instantApp

    final HashMap<IBinder, ReceiverList> mRegisteredReceivers = new HashMap<>();

    receiver 放到mRegisteredReceivers 的hashMap中

    receiver被封装成dispatcher注册到BroadcastQueue,

    BroadcastReceiver的注册基本完毕。

    在Activity sendBroadcast 发送广播,最终在contextImpl 中调研AMS的方法集中处理:

    AMS中处理broadcastIntentLocked()

    在这个过程中做了如下判断, 判断是否是系统广播,不是系统应用无法发送系统广播等等

    最后:BroadcastQueue中 scheduleBroadcastsLocked 向handler 发BROADCAST_INTENT_MSG的消息,processNextBroadcast 方法中

    如果是动态注册的receiver,其实可以知道,运行进程是活的,接收对象是存在的,deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false),

    如果是静态注册的receiver,运行进程不确定是否存活,接收对象不存在,如果进程未拉起,mService.startProcessLocked(),启动接收进程然后继续scheduleBroadcastsLocked()循环,下次进入processCurBroadcastLocked(r, app)。既如果目标进程未启动,这里是会拉起来的。如果进程已启动,则processCurBroadcastLocked(r, app)分发广播。

    private void deliverToRegisteredReceiverLocked(BroadcastRecord r,

            BroadcastFilter filter, boolean ordered, int index){

        boolean skip = false;

        if (filter.requiredPermission != null) {

            int perm = mService.checkComponentPermission(filter.requiredPermission,

                    r.callingPid, r.callingUid, -1, true);

            if (perm != PackageManager.PERMISSION_GRANTED) {

                Slog.w(TAG, "Permission Denial: broadcasting "

                        + r.intent.toString()

                        + " from " + r.callerPackage + " (pid="

                        + r.callingPid + ", uid=" + r.callingUid + ")"

                        + " requires " + filter.requiredPermission

                        + " due to registered receiver " + filter);

                skip = true;

            } else {

                final int opCode = AppOpsManager.permissionToOpCode(filter.requiredPermission);

                if (opCode != AppOpsManager.OP_NONE

    && mService.mAppOpsService.noteOperation(opCode, r.callingUid,

                                r.callerPackage) != AppOpsManager.MODE_ALLOWED) {

                    Slog.w(TAG, "Appop Denial: broadcasting "

                            + r.intent.toString()

                            + " from " + r.callerPackage + " (pid="

                            + r.callingPid + ", uid=" + r.callingUid + ")"

                            + " requires appop " + AppOpsManager.permissionToOp(

                                    filter.requiredPermission)

                            + " due to registered receiver " + filter);

                    skip = true;

                }

    }

    }

        if (!skip && r.requiredPermissions != null && r.requiredPermissions.length > 0) {

            for (int i = 0; i < r.requiredPermissions.length; i++) {

                String requiredPermission = r.requiredPermissions[i];

                int perm = mService.checkComponentPermission(requiredPermission,

                        filter.receiverList.pid, filter.receiverList.uid, -1, true);

                if (perm != PackageManager.PERMISSION_GRANTED) {

                    Slog.w(TAG, "Permission Denial: receiving "

                            + r.intent.toString()

                            + " to " + filter.receiverList.app

    + " (pid=" + filter.receiverList.pid

    + ", uid=" + filter.receiverList.uid+ ")"

                            + " requires " + requiredPermission

                            + " due to sender " + r.callerPackage

                            + " (uid " + r.callingUid + ")");

                    skip = true;

    break;

                }

                int appOp = AppOpsManager.permissionToOpCode(requiredPermission);

                if (appOp != AppOpsManager.OP_NONE&& appOp != r.appOp

                        && mService.mAppOpsService.noteOperation(appOp,

                        filter.receiverList.uid, filter.packageName)

                        != AppOpsManager.MODE_ALLOWED) {

                    Slog.w(TAG, "Appop Denial: receiving "

                            + r.intent.toString()

                            + " to " + filter.receiverList.app

    + " (pid=" + filter.receiverList.pid

    + ", uid=" + filter.receiverList.uid+ ")"

                            + " requires appop " + AppOpsManager.permissionToOp(

                            requiredPermission)

                            + " due to sender " + r.callerPackage

                            + " (uid " + r.callingUid + ")");

                    skip = true;

    break;

                }

    }

    }

        if (!skip && (r.requiredPermissions == null || r.requiredPermissions.length == 0)) {

            int perm = mService.checkComponentPermission(null,

                    filter.receiverList.pid, filter.receiverList.uid, -1, true);

            if (perm != PackageManager.PERMISSION_GRANTED) {

                Slog.w(TAG, "Permission Denial: security check failed when receiving "

                        + r.intent.toString()

                        + " to " + filter.receiverList.app

    + " (pid=" + filter.receiverList.pid

    + ", uid=" + filter.receiverList.uid+ ")"

                        + " due to sender " + r.callerPackage

                        + " (uid " + r.callingUid + ")");

                skip = true;

            }

    }

        if (!skip && r.appOp != AppOpsManager.OP_NONE

    && mService.mAppOpsService.noteOperation(r.appOp,

                filter.receiverList.uid, filter.packageName)

                != AppOpsManager.MODE_ALLOWED) {

            Slog.w(TAG, "Appop Denial: receiving "

                    + r.intent.toString()

                    + " to " + filter.receiverList.app

    + " (pid=" + filter.receiverList.pid

    + ", uid=" + filter.receiverList.uid+ ")"

                    + " requires appop " + AppOpsManager.opToName(r.appOp)

                    + " due to sender " + r.callerPackage

                    + " (uid " + r.callingUid + ")");

            skip = true;

        }

        if (!mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid,

                r.callingPid, r.resolvedType, filter.receiverList.uid)) {

            skip = true;

        }

        if (!skip && (filter.receiverList.app== null || filter.receiverList.app.killed

    || filter.receiverList.app.crashing)) {

            Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r

                    + " to " + filter.receiverList + ": process gone or crashing");

            skip = true;

        }

        // Ensure that broadcasts are only sent to other Instant Apps if they are marked as

    // visible to Instant Apps.

        final boolean visibleToInstantApps =

                (r.intent.getFlags() & Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS) != 0;

        if (!skip && !visibleToInstantApps && filter.instantApp

                && filter.receiverList.uid!= r.callingUid) {

            Slog.w(TAG, "Instant App Denial: receiving "

                    + r.intent.toString()

                    + " to " + filter.receiverList.app

    + " (pid=" + filter.receiverList.pid

    + ", uid=" + filter.receiverList.uid+ ")"

                    + " due to sender " + r.callerPackage

                    + " (uid " + r.callingUid + ")"

                    + " not specifying FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS");

            skip = true;

        }

        if (!skip && !filter.visibleToInstantApp && r.callerInstantApp

                && filter.receiverList.uid!= r.callingUid) {

            Slog.w(TAG, "Instant App Denial: receiving "

                    + r.intent.toString()

                    + " to " + filter.receiverList.app

    + " (pid=" + filter.receiverList.pid

    + ", uid=" + filter.receiverList.uid+ ")"

                    + " requires receiver be visible to instant apps"

                    + " due to sender " + r.callerPackage

                    + " (uid " + r.callingUid + ")");

            skip = true;

        }

        if (skip) {

            r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED;

    return;

        }

        // If permissions need a review before any of the app components can run, we drop

    // the broadcast and if the calling app is in the foreground and the broadcast is

    // explicit we launch the review UI passing it a pending intent to send the skipped

    // broadcast.

        if (mService.mPermissionReviewRequired) {

            if (!requestStartTargetPermissionsReviewIfNeededLocked(r, filter.packageName,

                    filter.owningUserId)) {

                r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED;

    return;

            }

    }

        r.delivery[index] = BroadcastRecord.DELIVERY_DELIVERED;

        // If this is not being sent as an ordered broadcast, then we

    // don't want to touch the fields that keep track of the current

    // state of ordered broadcasts.

        if (ordered) {

            r.receiver = filter.receiverList.receiver.asBinder();

            r.curFilter = filter;

            filter.receiverList.curBroadcast= r;

            r.state = BroadcastRecord.CALL_IN_RECEIVE;

            if (filter.receiverList.app!= null) {

                // Bump hosting application to no longer be in background

    // scheduling class.  Note that we can't do that if there

    // isn't an app...  but we can only be in that case for

    // things that directly call the IActivityManager API, which

    // are already core system stuff so don't matter for this.

                r.curApp = filter.receiverList.app;

                filter.receiverList.app.curReceivers.add(r);

                mService.updateOomAdjLocked(r.curApp, true);

            }

    }

        try {

            if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST,

                    "Delivering to " + filter + " : " + r);

            if (filter.receiverList.app!= null && filter.receiverList.app.inFullBackup) {

                // Skip delivery if full backup in progress

    // If it's an ordered broadcast, we need to continue to the next receiver.

                if (ordered) {

                    skipReceiverLocked(r);

                }

            } else {

                performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,

                        new Intent(r.intent), r.resultCode, r.resultData,

                        r.resultExtras, r.ordered, r.initialSticky, r.userId);

            }

            if (ordered) {

                r.state = BroadcastRecord.CALL_DONE_RECEIVE;

            }

        } catch (RemoteException e) {

            Slog.w(TAG, "Failure sending broadcast " + r.intent, e);

            if (ordered) {

                r.receiver = null;

                r.curFilter = null;

                filter.receiverList.curBroadcast= null;

                if (filter.receiverList.app!= null) {

                    filter.receiverList.app.curReceivers.remove(r);

                }

    }

    }

    }

    相关文章

      网友评论

          本文标题:BroadcastReceiver原理

          本文链接:https://www.haomeiwen.com/subject/hsbmdqtx.html