The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.server.am; |
| 18 | |
| 19 | import android.content.Intent; |
| 20 | import android.net.Uri; |
| 21 | |
| 22 | import java.io.PrintWriter; |
| 23 | import java.util.HashSet; |
| 24 | |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 25 | /** |
| 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 Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 30 | * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/ |
| 31 | * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java |
Dianne Hackborn | 7e26964 | 2010-08-25 19:50:20 -0700 | [diff] [blame] | 32 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | class UriPermission { |
| 34 | final int uid; |
| 35 | final Uri uri; |
| 36 | int modeFlags = 0; |
| 37 | int globalModeFlags = 0; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 38 | final HashSet<UriPermissionOwner> readOwners = new HashSet<UriPermissionOwner>(); |
| 39 | final HashSet<UriPermissionOwner> writeOwners = new HashSet<UriPermissionOwner>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 41 | String stringName; |
| 42 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | UriPermission(int _uid, Uri _uri) { |
| 44 | uid = _uid; |
| 45 | uri = _uri; |
| 46 | } |
| 47 | |
Steve Howard | 0de8ddb | 2010-09-14 16:29:26 -0700 | [diff] [blame] | 48 | void clearModes(int modeFlagsToClear) { |
| 49 | if ((modeFlagsToClear&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | globalModeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION; |
| 51 | modeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION; |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 52 | if (readOwners.size() > 0) { |
| 53 | for (UriPermissionOwner r : readOwners) { |
| 54 | r.removeReadPermission(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 56 | readOwners.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
Steve Howard | 0de8ddb | 2010-09-14 16:29:26 -0700 | [diff] [blame] | 59 | if ((modeFlagsToClear&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | globalModeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION; |
| 61 | modeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION; |
Vairavan Srinivasan | 2ed5249 | 2012-05-22 00:06:15 -0700 | [diff] [blame] | 62 | if (writeOwners.size() > 0) { |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 63 | for (UriPermissionOwner r : writeOwners) { |
| 64 | r.removeWritePermission(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | } |
Vairavan Srinivasan | 2ed5249 | 2012-05-22 00:06:15 -0700 | [diff] [blame] | 66 | writeOwners.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public String toString() { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 72 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void dump(PrintWriter pw, String prefix) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 85 | 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 Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 90 | 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 Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 95 | } |
Dianne Hackborn | 39792d2 | 2010-08-19 18:01:52 -0700 | [diff] [blame] | 96 | 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 Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 101 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | } |
| 103 | } |