Home | History | Annotate | Download | only in firewall
      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;
     23 import android.os.RemoteException;
     24 import android.os.UserHandle;
     25 
     26 import org.xmlpull.v1.XmlPullParser;
     27 import org.xmlpull.v1.XmlPullParserException;
     28 
     29 import java.io.IOException;
     30 
     31 public class SenderPackageFilter implements Filter {
     32     private static final String ATTR_NAME = "name";
     33 
     34     public final String mPackageName;
     35 
     36     public SenderPackageFilter(String packageName) {
     37         mPackageName = packageName;
     38     }
     39 
     40     @Override
     41     public boolean matches(IntentFirewall ifw, ComponentName resolvedComponent, Intent intent,
     42             int callerUid, int callerPid, String resolvedType, int receivingUid) {
     43         IPackageManager pm = AppGlobals.getPackageManager();
     44 
     45         int packageUid = -1;
     46         try {
     47             packageUid = pm.getPackageUid(mPackageName, UserHandle.USER_OWNER);
     48         } catch (RemoteException ex) {
     49             // handled below
     50         }
     51 
     52         if (packageUid == -1)  {
     53             return false;
     54         }
     55 
     56         return UserHandle.isSameApp(packageUid, callerUid);
     57     }
     58 
     59     public static final FilterFactory FACTORY = new FilterFactory("sender-package") {
     60         @Override
     61         public Filter newFilter(XmlPullParser parser)
     62                 throws IOException, XmlPullParserException {
     63             String packageName = parser.getAttributeValue(null, ATTR_NAME);
     64 
     65             if (packageName == null) {
     66                 throw new XmlPullParserException(
     67                     "A package name must be specified.", parser, null);
     68             }
     69 
     70             return new SenderPackageFilter(packageName);
     71         }
     72     };
     73 }
     74