blob: 8e7269cc6020c3f7816ae63e8ca282f66464e07a [file] [log] [blame]
David Brazdildcfa89b2018-10-31 11:04:10 +00001/*
2 * Copyright (C) 2018 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_LIBARTBASE_BASE_HIDDENAPI_FLAGS_H_
18#define ART_LIBARTBASE_BASE_HIDDENAPI_FLAGS_H_
19
20#include "sdk_version.h"
21
22#include "android-base/logging.h"
23
24namespace art {
25namespace hiddenapi {
26
27/*
28 * This class represents the information whether a field/method is in
29 * public API (whitelist) or if it isn't, apps targeting which SDK
30 * versions are allowed to access it.
31 */
32class ApiList {
33 private:
34 using IntValueType = uint32_t;
35
36 enum class Value : IntValueType {
37 // Values independent of target SDK version of app
38 kWhitelist = 0,
39 kGreylist = 1,
40 kBlacklist = 2,
41
42 // Values dependent on target SDK version of app. Put these last as
43 // their list will be extended in future releases.
44 // The max release code implicitly includes all maintenance releases,
45 // e.g. GreylistMaxO is accessible to targetSdkVersion <= 27 (O_MR1).
46 kGreylistMaxO = 3,
47
48 // Special values
49 kInvalid = static_cast<uint32_t>(-1),
50 kMinValue = kWhitelist,
51 kMaxValue = kGreylistMaxO,
52 };
53
54 static constexpr const char* kNames[] = {
55 "whitelist",
56 "greylist",
57 "blacklist",
58 "greylist-max-o",
59 };
60
61 static constexpr SdkVersion kMaxSdkVersions[] {
62 /* whitelist */ SdkVersion::kMax,
63 /* greylist */ SdkVersion::kMax,
64 /* blacklist */ SdkVersion::kMin,
65 /* greylist-max-o */ SdkVersion::kO_MR1,
66 };
67
68 static ApiList MinValue() { return ApiList(Value::kMinValue); }
69 static ApiList MaxValue() { return ApiList(Value::kMaxValue); }
70
71 explicit ApiList(Value value) : value_(value) {}
72
73 const Value value_;
74
75 public:
76 static ApiList Whitelist() { return ApiList(Value::kWhitelist); }
77 static ApiList Greylist() { return ApiList(Value::kGreylist); }
78 static ApiList Blacklist() { return ApiList(Value::kBlacklist); }
79 static ApiList GreylistMaxO() { return ApiList(Value::kGreylistMaxO); }
80 static ApiList Invalid() { return ApiList(Value::kInvalid); }
81
82 // Decodes ApiList from dex hiddenapi flags.
83 static ApiList FromDexFlags(uint32_t dex_flags) {
84 if (MinValue().GetIntValue() <= dex_flags && dex_flags <= MaxValue().GetIntValue()) {
85 return ApiList(static_cast<Value>(dex_flags));
86 }
87 return Invalid();
88 }
89
90 // Returns the ApiList with a given name.
91 static ApiList FromName(const std::string& str) {
92 for (IntValueType i = MinValue().GetIntValue(); i <= MaxValue().GetIntValue(); i++) {
93 ApiList current = ApiList(static_cast<Value>(i));
94 if (str == current.GetName()) {
95 return current;
96 }
97 }
98 return Invalid();
99 }
100
101 bool operator==(const ApiList other) const { return value_ == other.value_; }
102 bool operator!=(const ApiList other) const { return !(*this == other); }
103
104 bool IsValid() const { return *this != Invalid(); }
105
106 IntValueType GetIntValue() const {
107 DCHECK(IsValid());
108 return static_cast<IntValueType>(value_);
109 }
110
111 const char* GetName() const { return kNames[GetIntValue()]; }
112
113 SdkVersion GetMaxAllowedSdkVersion() const { return kMaxSdkVersions[GetIntValue()]; }
114
115 static constexpr size_t kValueCount = static_cast<size_t>(Value::kMaxValue) + 1;
116};
117
118inline std::ostream& operator<<(std::ostream& os, ApiList value) {
119 os << value.GetName();
120 return os;
121}
122
123inline bool AreValidDexFlags(uint32_t dex_flags) {
124 return ApiList::FromDexFlags(dex_flags).IsValid();
125}
126
127} // namespace hiddenapi
128} // namespace art
129
130
131#endif // ART_LIBARTBASE_BASE_HIDDENAPI_FLAGS_H_