blob: 8c1ffd57398f787c63654d13b0eb4eb01982fdf2 [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,
David Brazdil80d16282018-11-01 09:55:09 +000047 kGreylistMaxP = 4,
David Brazdildcfa89b2018-10-31 11:04:10 +000048
49 // Special values
50 kInvalid = static_cast<uint32_t>(-1),
51 kMinValue = kWhitelist,
David Brazdil80d16282018-11-01 09:55:09 +000052 kMaxValue = kGreylistMaxP,
David Brazdildcfa89b2018-10-31 11:04:10 +000053 };
54
55 static constexpr const char* kNames[] = {
56 "whitelist",
57 "greylist",
58 "blacklist",
59 "greylist-max-o",
David Brazdil80d16282018-11-01 09:55:09 +000060 "greylist-max-p",
David Brazdildcfa89b2018-10-31 11:04:10 +000061 };
62
David Brazdil91690d32018-11-04 18:07:23 +000063 static constexpr const char* kInvalidName = "invalid";
64
David Brazdildcfa89b2018-10-31 11:04:10 +000065 static constexpr SdkVersion kMaxSdkVersions[] {
66 /* whitelist */ SdkVersion::kMax,
67 /* greylist */ SdkVersion::kMax,
68 /* blacklist */ SdkVersion::kMin,
69 /* greylist-max-o */ SdkVersion::kO_MR1,
David Brazdil80d16282018-11-01 09:55:09 +000070 /* greylist-max-p */ SdkVersion::kP,
David Brazdildcfa89b2018-10-31 11:04:10 +000071 };
72
73 static ApiList MinValue() { return ApiList(Value::kMinValue); }
74 static ApiList MaxValue() { return ApiList(Value::kMaxValue); }
75
76 explicit ApiList(Value value) : value_(value) {}
77
David Brazdil91690d32018-11-04 18:07:23 +000078 Value value_;
David Brazdildcfa89b2018-10-31 11:04:10 +000079
80 public:
81 static ApiList Whitelist() { return ApiList(Value::kWhitelist); }
82 static ApiList Greylist() { return ApiList(Value::kGreylist); }
83 static ApiList Blacklist() { return ApiList(Value::kBlacklist); }
84 static ApiList GreylistMaxO() { return ApiList(Value::kGreylistMaxO); }
David Brazdil80d16282018-11-01 09:55:09 +000085 static ApiList GreylistMaxP() { return ApiList(Value::kGreylistMaxP); }
David Brazdildcfa89b2018-10-31 11:04:10 +000086 static ApiList Invalid() { return ApiList(Value::kInvalid); }
87
88 // Decodes ApiList from dex hiddenapi flags.
89 static ApiList FromDexFlags(uint32_t dex_flags) {
90 if (MinValue().GetIntValue() <= dex_flags && dex_flags <= MaxValue().GetIntValue()) {
91 return ApiList(static_cast<Value>(dex_flags));
92 }
93 return Invalid();
94 }
95
David Brazdil91690d32018-11-04 18:07:23 +000096 // Decodes ApiList from its integer value.
97 static ApiList FromIntValue(IntValueType int_value) {
98 if (MinValue().GetIntValue() <= int_value && int_value <= MaxValue().GetIntValue()) {
99 return ApiList(static_cast<Value>(int_value));
100 }
101 return Invalid();
102 }
103
David Brazdildcfa89b2018-10-31 11:04:10 +0000104 // Returns the ApiList with a given name.
105 static ApiList FromName(const std::string& str) {
106 for (IntValueType i = MinValue().GetIntValue(); i <= MaxValue().GetIntValue(); i++) {
107 ApiList current = ApiList(static_cast<Value>(i));
108 if (str == current.GetName()) {
109 return current;
110 }
111 }
112 return Invalid();
113 }
114
115 bool operator==(const ApiList other) const { return value_ == other.value_; }
116 bool operator!=(const ApiList other) const { return !(*this == other); }
117
118 bool IsValid() const { return *this != Invalid(); }
119
120 IntValueType GetIntValue() const {
121 DCHECK(IsValid());
122 return static_cast<IntValueType>(value_);
123 }
124
David Brazdil91690d32018-11-04 18:07:23 +0000125 const char* GetName() const { return IsValid() ? kNames[GetIntValue()]: kInvalidName; }
David Brazdildcfa89b2018-10-31 11:04:10 +0000126
127 SdkVersion GetMaxAllowedSdkVersion() const { return kMaxSdkVersions[GetIntValue()]; }
128
129 static constexpr size_t kValueCount = static_cast<size_t>(Value::kMaxValue) + 1;
130};
131
132inline std::ostream& operator<<(std::ostream& os, ApiList value) {
133 os << value.GetName();
134 return os;
135}
136
137inline bool AreValidDexFlags(uint32_t dex_flags) {
138 return ApiList::FromDexFlags(dex_flags).IsValid();
139}
140
141} // namespace hiddenapi
142} // namespace art
143
144
145#endif // ART_LIBARTBASE_BASE_HIDDENAPI_FLAGS_H_