blob: c5b1c7ba0e9915e5ea7fe0879beb3f25b5e3b156 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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.am;
18
19import android.content.Intent;
20import android.net.Uri;
21
22import java.io.PrintWriter;
23import java.util.HashSet;
24
Dianne Hackborn7e269642010-08-25 19:50:20 -070025/**
26 * Description of a permission granted to an app to access a particular URI.
27 *
28 * CTS tests for this functionality can be run with "runtest cts-appsecurity".
29 *
Dianne Hackborncca1f0e2010-09-26 18:34:53 -070030 * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/
31 * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java
Dianne Hackborn7e269642010-08-25 19:50:20 -070032 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033class UriPermission {
34 final int uid;
35 final Uri uri;
36 int modeFlags = 0;
37 int globalModeFlags = 0;
Dianne Hackborn39792d22010-08-19 18:01:52 -070038 final HashSet<UriPermissionOwner> readOwners = new HashSet<UriPermissionOwner>();
39 final HashSet<UriPermissionOwner> writeOwners = new HashSet<UriPermissionOwner>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Dianne Hackborn1d442e02009-04-20 18:14:05 -070041 String stringName;
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 UriPermission(int _uid, Uri _uri) {
44 uid = _uid;
45 uri = _uri;
46 }
47
Steve Howard0de8ddb2010-09-14 16:29:26 -070048 void clearModes(int modeFlagsToClear) {
49 if ((modeFlagsToClear&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 globalModeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION;
51 modeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION;
Dianne Hackborn39792d22010-08-19 18:01:52 -070052 if (readOwners.size() > 0) {
53 for (UriPermissionOwner r : readOwners) {
54 r.removeReadPermission(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 }
Dianne Hackborn39792d22010-08-19 18:01:52 -070056 readOwners.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 }
58 }
Steve Howard0de8ddb2010-09-14 16:29:26 -070059 if ((modeFlagsToClear&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 globalModeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
61 modeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
Vairavan Srinivasan2ed52492012-05-22 00:06:15 -070062 if (writeOwners.size() > 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -070063 for (UriPermissionOwner r : writeOwners) {
64 r.removeWritePermission(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 }
Vairavan Srinivasan2ed52492012-05-22 00:06:15 -070066 writeOwners.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 }
68 }
69 }
70
71 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070072 if (stringName != null) {
73 return stringName;
74 }
75 StringBuilder sb = new StringBuilder(128);
76 sb.append("UriPermission{");
77 sb.append(Integer.toHexString(System.identityHashCode(this)));
78 sb.append(' ');
79 sb.append(uri);
80 sb.append('}');
81 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 }
83
84 void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070085 pw.print(prefix); pw.print("modeFlags=0x");
86 pw.print(Integer.toHexString(modeFlags));
87 pw.print(" uid="); pw.print(uid);
88 pw.print(" globalModeFlags=0x");
89 pw.println(Integer.toHexString(globalModeFlags));
Dianne Hackborn39792d22010-08-19 18:01:52 -070090 if (readOwners.size() != 0) {
91 pw.print(prefix); pw.println("readOwners:");
92 for (UriPermissionOwner owner : readOwners) {
93 pw.print(prefix); pw.print(" * "); pw.println(owner);
94 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070095 }
Dianne Hackborn39792d22010-08-19 18:01:52 -070096 if (writeOwners.size() != 0) {
97 pw.print(prefix); pw.println("writeOwners:");
98 for (UriPermissionOwner owner : writeOwners) {
99 pw.print(prefix); pw.print(" * "); pw.println(owner);
100 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 }
103}