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

Commit 6e8f1166 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Finish impl of job queue: handle URI permissions." into oc-dev

parents 950fa06a 342e6037
Loading
Loading
Loading
Loading
+15 −0
Original line number Original line Diff line number Diff line
@@ -27,6 +27,7 @@ import android.os.Parcelable;
final public class JobWorkItem implements Parcelable {
final public class JobWorkItem implements Parcelable {
    final Intent mIntent;
    final Intent mIntent;
    int mWorkId;
    int mWorkId;
    Object mGrants;


    /**
    /**
     * Create a new piece of work.
     * Create a new piece of work.
@@ -57,6 +58,20 @@ final public class JobWorkItem implements Parcelable {
        return mWorkId;
        return mWorkId;
    }
    }


    /**
     * @hide
     */
    public void setGrants(Object grants) {
        mGrants = grants;
    }

    /**
     * @hide
     */
    public Object getGrants() {
        return mGrants;
    }

    public String toString() {
    public String toString() {
        return "JobWorkItem{id=" + mWorkId + " intent=" + mIntent + "}";
        return "JobWorkItem{id=" + mWorkId + " intent=" + mIntent + "}";
    }
    }
+1 −1
Original line number Original line Diff line number Diff line
@@ -1787,7 +1787,7 @@ public class IntentFilter implements Parcelable {
                    sb.append(", mHasPartialTypes="); sb.append(mHasPartialTypes);
                    sb.append(", mHasPartialTypes="); sb.append(mHasPartialTypes);
            du.println(sb.toString());
            du.println(sb.toString());
        }
        }
        {
        if (getAutoVerify()) {
            sb.setLength(0);
            sb.setLength(0);
            sb.append(prefix); sb.append("AutoVerify="); sb.append(getAutoVerify());
            sb.append(prefix); sb.append("AutoVerify="); sb.append(getAutoVerify());
            du.println(sb.toString());
            du.println(sb.toString());
+156 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.server.job;

import android.app.IActivityManager;
import android.content.ClipData;
import android.content.ContentProvider;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.Slog;

import java.io.PrintWriter;
import java.util.ArrayList;

public class GrantedUriPermissions {
    private final int mGrantFlags;
    private final int mSourceUserId;
    private final String mTag;
    private final IBinder mPermissionOwner;
    private final ArrayList<Uri> mUris = new ArrayList<>();

    private GrantedUriPermissions(IActivityManager am, int grantFlags, int uid, String tag)
            throws RemoteException {
        mGrantFlags = grantFlags;
        mSourceUserId = UserHandle.getUserId(uid);
        mTag = tag;
        mPermissionOwner = am.newUriPermissionOwner("job: " + tag);
    }

    public void revoke(IActivityManager am) {
        for (int i = mUris.size()-1; i >= 0; i--) {
            try {
                am.revokeUriPermissionFromOwner(mPermissionOwner, mUris.get(i),
                        mGrantFlags, mSourceUserId);
            } catch (RemoteException e) {
            }
        }
        mUris.clear();
    }

    public static boolean checkGrantFlags(int grantFlags) {
        return (grantFlags & (Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                |Intent.FLAG_GRANT_READ_URI_PERMISSION)) != 0;
    }

    public static GrantedUriPermissions createFromIntent(IActivityManager am, Intent intent,
            int sourceUid, String targetPackage, int targetUserId, String tag) {
        int grantFlags = intent.getFlags();
        if (!checkGrantFlags(grantFlags)) {
            return null;
        }

        GrantedUriPermissions perms = null;

        Uri data = intent.getData();
        if (data != null) {
            perms = grantUri(am, data, sourceUid, targetPackage, targetUserId, grantFlags, tag,
                    perms);
        }

        ClipData clip = intent.getClipData();
        if (clip != null) {
            perms = grantClip(am, clip, sourceUid, targetPackage, targetUserId, grantFlags, tag,
                    perms);
        }

        return perms;
    }

    public static GrantedUriPermissions createFromClip(IActivityManager am, ClipData clip,
            int sourceUid, String targetPackage, int targetUserId, int grantFlags, String tag) {
        if (!checkGrantFlags(grantFlags)) {
            return null;
        }
        GrantedUriPermissions perms = null;
        if (clip != null) {
            perms = grantClip(am, clip, sourceUid, targetPackage, targetUserId, grantFlags,
                    tag, perms);
        }
        return perms;
    }

    private static GrantedUriPermissions grantClip(IActivityManager am, ClipData clip,
            int sourceUid, String targetPackage, int targetUserId, int grantFlags, String tag,
            GrantedUriPermissions curPerms) {
        final int N = clip.getItemCount();
        for (int i = 0; i < N; i++) {
            curPerms = grantItem(am, clip.getItemAt(i), sourceUid, targetPackage, targetUserId,
                    grantFlags, tag, curPerms);
        }
        return curPerms;
    }

    private static GrantedUriPermissions grantUri(IActivityManager am, Uri uri,
            int sourceUid, String targetPackage, int targetUserId, int grantFlags, String tag,
            GrantedUriPermissions curPerms) {
        try {
            int sourceUserId = ContentProvider.getUserIdFromUri(uri,
                    UserHandle.getUserId(sourceUid));
            uri = ContentProvider.getUriWithoutUserId(uri);
            if (curPerms == null) {
                curPerms = new GrantedUriPermissions(am, grantFlags, sourceUid, tag);
            }
            am.grantUriPermissionFromOwner(curPerms.mPermissionOwner, sourceUid, targetPackage,
                    uri, grantFlags, sourceUserId, targetUserId);
            curPerms.mUris.add(uri);
        } catch (RemoteException e) {
            Slog.e("JobScheduler", "AM dead");
        }
        return curPerms;
    }

    private static GrantedUriPermissions grantItem(IActivityManager am, ClipData.Item item,
            int sourceUid, String targetPackage, int targetUserId, int grantFlags, String tag,
            GrantedUriPermissions curPerms) {
        if (item.getUri() != null) {
            curPerms = grantUri(am, item.getUri(), sourceUid, targetPackage, targetUserId,
                    grantFlags, tag, curPerms);
        }
        Intent intent = item.getIntent();
        if (intent != null && intent.getData() != null) {
            curPerms = grantUri(am, intent.getData(), sourceUid, targetPackage, targetUserId,
                    grantFlags, tag, curPerms);
        }
        return curPerms;
    }

    // Dumpsys infrastructure
    public void dump(PrintWriter pw, String prefix) {
        pw.print(prefix); pw.print("mGrantFlags=0x"); pw.print(Integer.toHexString(mGrantFlags));
        pw.print(" mSourceUserId="); pw.println(mSourceUserId);
        pw.print(prefix); pw.print("mTag="); pw.println(mTag);
        pw.print(prefix); pw.print("mPermissionOwner="); pw.println(mPermissionOwner);
        for (int i = 0; i < mUris.size(); i++) {
            pw.print(prefix); pw.print("#"); pw.print(i); pw.print(": ");
            pw.println(mUris.get(i));
        }
    }
}
+6 −6
Original line number Original line Diff line number Diff line
@@ -601,7 +601,7 @@ public final class JobSchedulerService extends com.android.server.SystemService
                // Fast path: we are adding work to an existing job, and the JobInfo is not
                // Fast path: we are adding work to an existing job, and the JobInfo is not
                // changing.  We can just directly enqueue this work in to the job.
                // changing.  We can just directly enqueue this work in to the job.
                if (toCancel.getJob().equals(job)) {
                if (toCancel.getJob().equals(job)) {
                    toCancel.enqueueWorkLocked(work);
                    toCancel.enqueueWorkLocked(ActivityManager.getService(), work);
                    return JobScheduler.RESULT_SUCCESS;
                    return JobScheduler.RESULT_SUCCESS;
                }
                }
            }
            }
@@ -625,7 +625,7 @@ public final class JobSchedulerService extends com.android.server.SystemService
            }
            }
            if (work != null) {
            if (work != null) {
                // If work has been supplied, enqueue it into the new job.
                // If work has been supplied, enqueue it into the new job.
                jobStatus.enqueueWorkLocked(work);
                jobStatus.enqueueWorkLocked(ActivityManager.getService(), work);
            }
            }
            startTrackingJobLocked(jobStatus, toCancel);
            startTrackingJobLocked(jobStatus, toCancel);
            mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
            mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
@@ -758,7 +758,7 @@ public final class JobSchedulerService extends com.android.server.SystemService
                    final JobStatus executing = jsc.getRunningJob();
                    final JobStatus executing = jsc.getRunningJob();
                    if (executing != null
                    if (executing != null
                            && (executing.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) == 0) {
                            && (executing.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) == 0) {
                        jsc.cancelExecutingJob(JobParameters.REASON_DEVICE_IDLE);
                        jsc.cancelExecutingJobLocked(JobParameters.REASON_DEVICE_IDLE);
                    }
                    }
                }
                }
            } else {
            } else {
@@ -921,7 +921,7 @@ public final class JobSchedulerService extends com.android.server.SystemService
    private boolean stopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob,
    private boolean stopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob,
            boolean writeBack) {
            boolean writeBack) {
        // Deal with any remaining work items in the old job.
        // Deal with any remaining work items in the old job.
        jobStatus.stopTrackingJobLocked(incomingJob);
        jobStatus.stopTrackingJobLocked(ActivityManager.getService(), incomingJob);


        // Remove from store as well as controllers.
        // Remove from store as well as controllers.
        final boolean removed = mJobs.remove(jobStatus, writeBack);
        final boolean removed = mJobs.remove(jobStatus, writeBack);
@@ -939,7 +939,7 @@ public final class JobSchedulerService extends com.android.server.SystemService
            JobServiceContext jsc = mActiveServices.get(i);
            JobServiceContext jsc = mActiveServices.get(i);
            final JobStatus executing = jsc.getRunningJob();
            final JobStatus executing = jsc.getRunningJob();
            if (executing != null && executing.matches(job.getUid(), job.getJobId())) {
            if (executing != null && executing.matches(job.getUid(), job.getJobId())) {
                jsc.cancelExecutingJob(reason);
                jsc.cancelExecutingJobLocked(reason);
                return true;
                return true;
            }
            }
        }
        }
@@ -1564,7 +1564,7 @@ public final class JobSchedulerService extends com.android.server.SystemService
                        Slog.d(TAG, "preempting job: " + mActiveServices.get(i).getRunningJob());
                        Slog.d(TAG, "preempting job: " + mActiveServices.get(i).getRunningJob());
                    }
                    }
                    // preferredUid will be set to uid of currently running job.
                    // preferredUid will be set to uid of currently running job.
                    mActiveServices.get(i).preemptExecutingJob();
                    mActiveServices.get(i).preemptExecutingJobLocked();
                    preservePreferredUid = true;
                    preservePreferredUid = true;
                } else {
                } else {
                    final JobStatus pendingJob = contextIdToJobMap[i];
                    final JobStatus pendingJob = contextIdToJobMap[i];
+296 −335

File changed.

Preview size limit exceeded, changes collapsed.

Loading