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