Ben Gruver | b2340f3 | 2014-02-11 16:37:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.firewall; |
| 18 | |
| 19 | import android.app.AppGlobals; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.IPackageManager; |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
Ben Gruver | b2340f3 | 2014-02-11 16:37:07 -0800 | [diff] [blame] | 24 | import android.os.RemoteException; |
| 25 | import android.os.UserHandle; |
| 26 | |
| 27 | import org.xmlpull.v1.XmlPullParser; |
| 28 | import org.xmlpull.v1.XmlPullParserException; |
| 29 | |
| 30 | import java.io.IOException; |
| 31 | |
| 32 | public class SenderPackageFilter implements Filter { |
| 33 | private static final String ATTR_NAME = "name"; |
| 34 | |
| 35 | public final String mPackageName; |
| 36 | |
| 37 | public SenderPackageFilter(String packageName) { |
| 38 | mPackageName = packageName; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public boolean matches(IntentFirewall ifw, ComponentName resolvedComponent, Intent intent, |
| 43 | int callerUid, int callerPid, String resolvedType, int receivingUid) { |
| 44 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 45 | |
| 46 | int packageUid = -1; |
| 47 | try { |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 48 | // USER_SYSTEM here is not important. Only app id is used and getPackageUid() will |
| 49 | // return a uid whether the app is installed for a user or not. |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 50 | packageUid = pm.getPackageUid(mPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 51 | UserHandle.USER_SYSTEM); |
Ben Gruver | b2340f3 | 2014-02-11 16:37:07 -0800 | [diff] [blame] | 52 | } catch (RemoteException ex) { |
| 53 | // handled below |
| 54 | } |
| 55 | |
| 56 | if (packageUid == -1) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return UserHandle.isSameApp(packageUid, callerUid); |
| 61 | } |
| 62 | |
| 63 | public static final FilterFactory FACTORY = new FilterFactory("sender-package") { |
| 64 | @Override |
| 65 | public Filter newFilter(XmlPullParser parser) |
| 66 | throws IOException, XmlPullParserException { |
| 67 | String packageName = parser.getAttributeValue(null, ATTR_NAME); |
| 68 | |
| 69 | if (packageName == null) { |
| 70 | throw new XmlPullParserException( |
| 71 | "A package name must be specified.", parser, null); |
| 72 | } |
| 73 | |
| 74 | return new SenderPackageFilter(packageName); |
| 75 | } |
| 76 | }; |
| 77 | } |