1 /*
2  * Copyright (C) 2019 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 #ifndef ART_TOOLS_VERIDEX_API_LIST_FILTER_H_
18 #define ART_TOOLS_VERIDEX_API_LIST_FILTER_H_
19 
20 #include <algorithm>
21 #include <android-base/strings.h>
22 
23 #include "base/hiddenapi_flags.h"
24 
25 namespace art {
26 
27 class ApiListFilter {
28  public:
ApiListFilter(const std::vector<std::string> & exclude_api_lists)29   explicit ApiListFilter(const std::vector<std::string>& exclude_api_lists) {
30     std::set<hiddenapi::ApiList> exclude_set;
31     bool include_invalid_list = true;
32     for (const std::string& name : exclude_api_lists) {
33       if (name.empty()) {
34         continue;
35       }
36       if (name == "invalid") {
37         include_invalid_list = false;
38         continue;
39       }
40       hiddenapi::ApiList list = hiddenapi::ApiList::FromName(name);
41       if (!list.IsValid()) {
42         LOG(ERROR) << "Unknown ApiList::Value " << name
43                    << ". See valid values in art/libartbase/base/hiddenapi_flags.h.";
44       }
45       exclude_set.insert(list);
46     }
47 
48     if (include_invalid_list) {
49       lists_.push_back(hiddenapi::ApiList());
50     }
51     for (size_t i = 0; i < hiddenapi::ApiList::kValueCount; ++i) {
52       hiddenapi::ApiList list = hiddenapi::ApiList(i);
53       if (exclude_set.find(list) == exclude_set.end()) {
54           lists_.push_back(list);
55       }
56     }
57   }
58 
Matches(hiddenapi::ApiList list)59   bool Matches(hiddenapi::ApiList list) const {
60     for (const auto& it : lists_) {
61       if (list.GetIntValue() == it.GetIntValue()) {
62         return true;
63       }
64     }
65     return false;
66   }
67 
68  private:
69   std::vector<hiddenapi::ApiList> lists_;
70 };
71 
72 }  // namespace art
73 
74 #endif  // ART_TOOLS_VERIDEX_API_LIST_FILTER_H_
75