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
17package com.android.server.pm;
18
19import android.content.pm.PackageManager;
20import android.content.pm.PackageParser;
21import android.util.ArraySet;
22import android.util.Slog;
23
24import java.util.ArrayList;
25
26public 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
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
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
61    public int getState() {
62        return mState;
63    }
64
65    public void setPendingState() {
66        setState(STATE_VERIFICATION_PENDING);
67    }
68
69    public ArrayList<PackageParser.ActivityIntentInfo> getFilters() {
70        return mFilters;
71    }
72
73    public boolean isVerificationComplete() {
74        return mVerificationComplete;
75    }
76
77    public boolean isVerified() {
78        if (mVerificationComplete) {
79            return (mState == STATE_VERIFICATION_SUCCESS);
80        }
81        return false;
82    }
83
84    public int getUserId() {
85        return mUserId;
86    }
87
88    public String getPackageName() {
89        return mPackageName;
90    }
91
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            String host = mHosts.valueAt(i);
100            // "*.example.tld" is validated via https://example.tld
101            if (host.startsWith("*.")) {
102                host = host.substring(2);
103            }
104            sb.append(host);
105        }
106        return sb.toString();
107    }
108
109    public boolean setVerifierResponse(int callerUid, int code) {
110        if (mRequiredVerifierUid == callerUid) {
111            int state = STATE_UNDEFINED;
112            if (code == PackageManager.INTENT_FILTER_VERIFICATION_SUCCESS) {
113                state = STATE_VERIFICATION_SUCCESS;
114            } else if (code == PackageManager.INTENT_FILTER_VERIFICATION_FAILURE) {
115                state = STATE_VERIFICATION_FAILURE;
116            }
117            mVerificationComplete = true;
118            setState(state);
119            return true;
120        }
121        Slog.d(TAG, "Cannot set verifier response with callerUid:" + callerUid + " and code:" +
122                code + " as required verifierUid is:" + mRequiredVerifierUid);
123        return false;
124    }
125
126    public void addFilter(PackageParser.ActivityIntentInfo filter) {
127        mFilters.add(filter);
128        mHosts.addAll(filter.getHostsList());
129    }
130}
131