BasePermission.java revision b3a6defec84ab1d420b049c575cb37b1151e095a
1/*
2 * Copyright (C) 2006 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
17package com.android.server.pm;
18
19import android.content.pm.PackageParser;
20import android.content.pm.PermissionInfo;
21import android.os.UserHandle;
22
23import com.android.internal.util.ArrayUtils;
24
25final class BasePermission {
26    final static int TYPE_NORMAL = 0;
27
28    final static int TYPE_BUILTIN = 1;
29
30    final static int TYPE_DYNAMIC = 2;
31
32    final String name;
33
34    String sourcePackage;
35
36    PackageSettingBase packageSetting;
37
38    final int type;
39
40    int protectionLevel;
41
42    PackageParser.Permission perm;
43
44    PermissionInfo pendingInfo;
45
46    /** UID that owns the definition of this permission */
47    int uid;
48
49    /** Additional GIDs given to apps granted this permission */
50    private int[] gids;
51
52    /**
53     * Flag indicating that {@link #gids} should be adjusted based on the
54     * {@link UserHandle} the granted app is running as.
55     */
56    private boolean perUser;
57
58    BasePermission(String _name, String _sourcePackage, int _type) {
59        name = _name;
60        sourcePackage = _sourcePackage;
61        type = _type;
62        // Default to most conservative protection level.
63        protectionLevel = PermissionInfo.PROTECTION_SIGNATURE;
64    }
65
66    @Override
67    public String toString() {
68        return "BasePermission{" + Integer.toHexString(System.identityHashCode(this)) + " " + name
69                + "}";
70    }
71
72    public void setGids(int[] gids, boolean perUser) {
73        this.gids = gids;
74        this.perUser = perUser;
75    }
76
77    public int[] computeGids(int userId) {
78        if (perUser) {
79            final int[] userGids = new int[gids.length];
80            for (int i = 0; i < gids.length; i++) {
81                userGids[i] = UserHandle.getUid(userId, gids[i]);
82            }
83            return userGids;
84        } else {
85            return gids;
86        }
87    }
88
89    public boolean isRuntime() {
90        return (protectionLevel & PermissionInfo.PROTECTION_MASK_BASE)
91                == PermissionInfo.PROTECTION_DANGEROUS;
92    }
93}
94