1 /*
2  * Copyright (C) 2015 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.pm;
18 
19 import android.content.pm.PackageManager;
20 import android.content.pm.PackageParser;
21 import android.util.ArraySet;
22 import android.util.Slog;
23 
24 import java.util.ArrayList;
25 
26 public class IntentFilterVerificationState {
27     static final String TAG = IntentFilterVerificationState.class.getName();
28 
29     public final static int STATE_UNDEFINED = 0;
30     public final static int STATE_VERIFICATION_PENDING = 1;
31     public final static int STATE_VERIFICATION_SUCCESS = 2;
32     public final static int STATE_VERIFICATION_FAILURE = 3;
33 
34     private int mRequiredVerifierUid = 0;
35 
36     private int mState;
37 
38     private ArrayList<PackageParser.ActivityIntentInfo> mFilters = new ArrayList<>();
39     private ArraySet<String> mHosts = new ArraySet<>();
40     private int mUserId;
41 
42     private String mPackageName;
43     private boolean mVerificationComplete;
44 
IntentFilterVerificationState(int verifierUid, int userId, String packageName)45     public IntentFilterVerificationState(int verifierUid, int userId, String packageName) {
46         mRequiredVerifierUid = verifierUid;
47         mUserId = userId;
48         mPackageName = packageName;
49         mState = STATE_UNDEFINED;
50         mVerificationComplete = false;
51     }
52 
setState(int state)53     public void setState(int state) {
54         if (state > STATE_VERIFICATION_FAILURE || state < STATE_UNDEFINED) {
55             mState = STATE_UNDEFINED;
56         } else {
57             mState = state;
58         }
59     }
60 
getState()61     public int getState() {
62         return mState;
63     }
64 
setPendingState()65     public void setPendingState() {
66         setState(STATE_VERIFICATION_PENDING);
67     }
68 
getFilters()69     public ArrayList<PackageParser.ActivityIntentInfo> getFilters() {
70         return mFilters;
71     }
72 
isVerificationComplete()73     public boolean isVerificationComplete() {
74         return mVerificationComplete;
75     }
76 
isVerified()77     public boolean isVerified() {
78         if (mVerificationComplete) {
79             return (mState == STATE_VERIFICATION_SUCCESS);
80         }
81         return false;
82     }
83 
getUserId()84     public int getUserId() {
85         return mUserId;
86     }
87 
getPackageName()88     public String getPackageName() {
89         return mPackageName;
90     }
91 
getHostsString()92     public String getHostsString() {
93         StringBuilder sb = new StringBuilder();
94         final int count = mHosts.size();
95         for (int i=0; i<count; i++) {
96             if (i > 0) {
97                 sb.append(" ");
98             }
99             sb.append(mHosts.valueAt(i));
100         }
101         return sb.toString();
102     }
103 
setVerifierResponse(int callerUid, int code)104     public boolean setVerifierResponse(int callerUid, int code) {
105         if (mRequiredVerifierUid == callerUid) {
106             int state = STATE_UNDEFINED;
107             if (code == PackageManager.INTENT_FILTER_VERIFICATION_SUCCESS) {
108                 state = STATE_VERIFICATION_SUCCESS;
109             } else if (code == PackageManager.INTENT_FILTER_VERIFICATION_FAILURE) {
110                 state = STATE_VERIFICATION_FAILURE;
111             }
112             mVerificationComplete = true;
113             setState(state);
114             return true;
115         }
116         Slog.d(TAG, "Cannot set verifier response with callerUid:" + callerUid + " and code:" +
117                 code + " as required verifierUid is:" + mRequiredVerifierUid);
118         return false;
119     }
120 
addFilter(PackageParser.ActivityIntentInfo filter)121     public void addFilter(PackageParser.ActivityIntentInfo filter) {
122         mFilters.add(filter);
123         mHosts.addAll(filter.getHostsList());
124     }
125 }
126