blob: 71ea56b56f3c1b6fedd6d7ba60dd6f84f6909cb6 [file] [log] [blame]
Nicolas Geoffray534a0a12018-03-24 20:02:25 +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#include "hidden_api.h"
18
19#include <fstream>
20#include <sstream>
21
David Brazdil91690d32018-11-04 18:07:23 +000022#include "android-base/strings.h"
Nicolas Geoffray534a0a12018-03-24 20:02:25 +000023#include "dex/dex_file-inl.h"
24
25namespace art {
26
Artur Satayev39c399a2019-09-13 16:09:09 +010027HiddenApi::HiddenApi(const char* filename, const ApiListFilter& api_list_filter)
28 : api_list_filter_(api_list_filter) {
David Brazdil91690d32018-11-04 18:07:23 +000029 CHECK(filename != nullptr);
30
31 std::ifstream in(filename);
32 for (std::string str; std::getline(in, str);) {
33 std::vector<std::string> values = android::base::Split(str, ",");
David Brazdil91690d32018-11-04 18:07:23 +000034 const std::string& signature = values[0];
David Brazdil91690d32018-11-04 18:07:23 +000035
David Brazdil90faceb2018-12-14 14:36:15 +000036 hiddenapi::ApiList membership;
37 bool success = hiddenapi::ApiList::FromNames(values.begin() + 1, values.end(), &membership);
38 CHECK(success) << "Unknown ApiList flag: " << str;
39 CHECK(membership.IsValid()) << "Invalid ApiList: " << membership;
David Brazdil91690d32018-11-04 18:07:23 +000040
David Brazdil91690d32018-11-04 18:07:23 +000041 AddSignatureToApiList(signature, membership);
42 size_t pos = signature.find("->");
43 if (pos != std::string::npos) {
44 // Add the class name.
45 AddSignatureToApiList(signature.substr(0, pos), membership);
46 pos = signature.find('(');
47 if (pos != std::string::npos) {
48 // Add the class->method name (so stripping the signature).
49 AddSignatureToApiList(signature.substr(0, pos), membership);
50 }
51 pos = signature.find(':');
52 if (pos != std::string::npos) {
53 // Add the class->field name (so stripping the type).
54 AddSignatureToApiList(signature.substr(0, pos), membership);
55 }
56 }
57 }
58}
59
60void HiddenApi::AddSignatureToApiList(const std::string& signature, hiddenapi::ApiList membership) {
61 auto it = api_list_.find(signature);
62 if (it == api_list_.end()) {
63 // Does not exist yet. Add it to list.
64 api_list_.emplace(signature, membership);
65 } else if (membership.GetMaxAllowedSdkVersion() < it->second.GetMaxAllowedSdkVersion()) {
66 // Already exist but `membership` is more restrictive.
67 it->second = membership;
68 } else {
69 // Already exists and `membership` is equally or less restrictive.
70 }
71}
72
Nicolas Geoffray534a0a12018-03-24 20:02:25 +000073std::string HiddenApi::GetApiMethodName(const DexFile& dex_file, uint32_t method_index) {
74 std::stringstream ss;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080075 const dex::MethodId& method_id = dex_file.GetMethodId(method_index);
Nicolas Geoffray534a0a12018-03-24 20:02:25 +000076 ss << dex_file.StringByTypeIdx(method_id.class_idx_)
77 << "->"
78 << dex_file.GetMethodName(method_id)
79 << dex_file.GetMethodSignature(method_id).ToString();
80 return ss.str();
81}
82
83std::string HiddenApi::GetApiFieldName(const DexFile& dex_file, uint32_t field_index) {
84 std::stringstream ss;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080085 const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
Nicolas Geoffray534a0a12018-03-24 20:02:25 +000086 ss << dex_file.StringByTypeIdx(field_id.class_idx_)
87 << "->"
88 << dex_file.GetFieldName(field_id)
89 << ":"
90 << dex_file.GetFieldTypeDescriptor(field_id);
91 return ss.str();
92}
93
Nicolas Geoffray534a0a12018-03-24 20:02:25 +000094} // namespace art