Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define ATRACE_TAG ATRACE_TAG_RESOURCES |
| 18 | |
| 19 | #include "androidfw/AssetManager2.h" |
| 20 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 21 | #include <iterator> |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 22 | #include <set> |
| 23 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 24 | #include "android-base/logging.h" |
| 25 | #include "android-base/stringprintf.h" |
| 26 | #include "utils/ByteOrder.h" |
| 27 | #include "utils/Trace.h" |
| 28 | |
| 29 | #ifdef _WIN32 |
| 30 | #ifdef ERROR |
| 31 | #undef ERROR |
| 32 | #endif |
| 33 | #endif |
| 34 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 35 | #include "androidfw/ResourceUtils.h" |
| 36 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 37 | namespace android { |
| 38 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 39 | AssetManager2::AssetManager2() { |
| 40 | memset(&configuration_, 0, sizeof(configuration_)); |
| 41 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 42 | |
| 43 | bool AssetManager2::SetApkAssets(const std::vector<const ApkAssets*>& apk_assets, |
| 44 | bool invalidate_caches) { |
| 45 | apk_assets_ = apk_assets; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 46 | BuildDynamicRefTable(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 47 | if (invalidate_caches) { |
| 48 | InvalidateCaches(static_cast<uint32_t>(-1)); |
| 49 | } |
| 50 | return true; |
| 51 | } |
| 52 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 53 | void AssetManager2::BuildDynamicRefTable() { |
| 54 | package_groups_.clear(); |
| 55 | package_ids_.fill(0xff); |
| 56 | |
| 57 | // 0x01 is reserved for the android package. |
| 58 | int next_package_id = 0x02; |
| 59 | const size_t apk_assets_count = apk_assets_.size(); |
| 60 | for (size_t i = 0; i < apk_assets_count; i++) { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 61 | const LoadedArsc* loaded_arsc = apk_assets_[i]->GetLoadedArsc(); |
| 62 | |
| 63 | for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 64 | // Get the package ID or assign one if a shared library. |
| 65 | int package_id; |
| 66 | if (package->IsDynamic()) { |
| 67 | package_id = next_package_id++; |
| 68 | } else { |
| 69 | package_id = package->GetPackageId(); |
| 70 | } |
| 71 | |
| 72 | // Add the mapping for package ID to index if not present. |
| 73 | uint8_t idx = package_ids_[package_id]; |
| 74 | if (idx == 0xff) { |
| 75 | package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size()); |
| 76 | package_groups_.push_back({}); |
Adam Lesinski | 9ad287c | 2018-01-30 17:11:48 -0800 | [diff] [blame^] | 77 | DynamicRefTable& ref_table = package_groups_.back().dynamic_ref_table; |
| 78 | ref_table.mAssignedPackageId = package_id; |
| 79 | ref_table.mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 80 | } |
| 81 | PackageGroup* package_group = &package_groups_[idx]; |
| 82 | |
| 83 | // Add the package and to the set of packages with the same ID. |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 84 | package_group->packages_.push_back(package.get()); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 85 | package_group->cookies_.push_back(static_cast<ApkAssetsCookie>(i)); |
| 86 | |
| 87 | // Add the package name -> build time ID mappings. |
| 88 | for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) { |
| 89 | String16 package_name(entry.package_name.c_str(), entry.package_name.size()); |
| 90 | package_group->dynamic_ref_table.mEntries.replaceValueFor( |
| 91 | package_name, static_cast<uint8_t>(entry.package_id)); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Now assign the runtime IDs so that we have a build-time to runtime ID map. |
| 97 | const auto package_groups_end = package_groups_.end(); |
| 98 | for (auto iter = package_groups_.begin(); iter != package_groups_end; ++iter) { |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 99 | const std::string& package_name = iter->packages_[0]->GetPackageName(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 100 | for (auto iter2 = package_groups_.begin(); iter2 != package_groups_end; ++iter2) { |
| 101 | iter2->dynamic_ref_table.addMapping(String16(package_name.c_str(), package_name.size()), |
| 102 | iter->dynamic_ref_table.mAssignedPackageId); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void AssetManager2::DumpToLog() const { |
| 108 | base::ScopedLogSeverity _log(base::INFO); |
| 109 | |
Adam Lesinski | 9ad287c | 2018-01-30 17:11:48 -0800 | [diff] [blame^] | 110 | LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this); |
| 111 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 112 | std::string list; |
Adam Lesinski | 9ad287c | 2018-01-30 17:11:48 -0800 | [diff] [blame^] | 113 | for (const auto& apk_assets : apk_assets_) { |
| 114 | base::StringAppendF(&list, "%s,", apk_assets->GetPath().c_str()); |
| 115 | } |
| 116 | LOG(INFO) << "ApkAssets: " << list; |
| 117 | |
| 118 | list = ""; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 119 | for (size_t i = 0; i < package_ids_.size(); i++) { |
| 120 | if (package_ids_[i] != 0xff) { |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 121 | base::StringAppendF(&list, "%02x -> %d, ", (int) i, package_ids_[i]); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | LOG(INFO) << "Package ID map: " << list; |
| 125 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 126 | for (const auto& package_group: package_groups_) { |
| 127 | list = ""; |
| 128 | for (const auto& package : package_group.packages_) { |
Adam Lesinski | 9ad287c | 2018-01-30 17:11:48 -0800 | [diff] [blame^] | 129 | base::StringAppendF(&list, "%s(%02x%s), ", package->GetPackageName().c_str(), |
| 130 | package->GetPackageId(), (package->IsDynamic() ? " dynamic" : "")); |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 131 | } |
Adam Lesinski | 9ad287c | 2018-01-30 17:11:48 -0800 | [diff] [blame^] | 132 | LOG(INFO) << base::StringPrintf("PG (%02x): ", |
| 133 | package_group.dynamic_ref_table.mAssignedPackageId) |
| 134 | << list; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 135 | } |
| 136 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 137 | |
| 138 | const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const { |
| 139 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 140 | return nullptr; |
| 141 | } |
| 142 | return apk_assets_[cookie]->GetLoadedArsc()->GetStringPool(); |
| 143 | } |
| 144 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 145 | const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const { |
| 146 | if (package_id >= package_ids_.size()) { |
| 147 | return nullptr; |
| 148 | } |
| 149 | |
| 150 | const size_t idx = package_ids_[package_id]; |
| 151 | if (idx == 0xff) { |
| 152 | return nullptr; |
| 153 | } |
| 154 | return &package_groups_[idx].dynamic_ref_table; |
| 155 | } |
| 156 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 157 | const DynamicRefTable* AssetManager2::GetDynamicRefTableForCookie(ApkAssetsCookie cookie) const { |
| 158 | for (const PackageGroup& package_group : package_groups_) { |
| 159 | for (const ApkAssetsCookie& package_cookie : package_group.cookies_) { |
| 160 | if (package_cookie == cookie) { |
| 161 | return &package_group.dynamic_ref_table; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | return nullptr; |
| 166 | } |
| 167 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 168 | void AssetManager2::SetConfiguration(const ResTable_config& configuration) { |
| 169 | const int diff = configuration_.diff(configuration); |
| 170 | configuration_ = configuration; |
| 171 | |
| 172 | if (diff) { |
| 173 | InvalidateCaches(static_cast<uint32_t>(diff)); |
| 174 | } |
| 175 | } |
| 176 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 177 | std::set<ResTable_config> AssetManager2::GetResourceConfigurations(bool exclude_system, |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 178 | bool exclude_mipmap) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 179 | ATRACE_CALL(); |
| 180 | std::set<ResTable_config> configurations; |
| 181 | for (const PackageGroup& package_group : package_groups_) { |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 182 | for (const LoadedPackage* package : package_group.packages_) { |
| 183 | if (exclude_system && package->IsSystem()) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 184 | continue; |
| 185 | } |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 186 | package->CollectConfigurations(exclude_mipmap, &configurations); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | return configurations; |
| 190 | } |
| 191 | |
| 192 | std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system, |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 193 | bool merge_equivalent_languages) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 194 | ATRACE_CALL(); |
| 195 | std::set<std::string> locales; |
| 196 | for (const PackageGroup& package_group : package_groups_) { |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 197 | for (const LoadedPackage* package : package_group.packages_) { |
| 198 | if (exclude_system && package->IsSystem()) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 199 | continue; |
| 200 | } |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 201 | package->CollectLocales(merge_equivalent_languages, &locales); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | return locales; |
| 205 | } |
| 206 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 207 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, Asset::AccessMode mode) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 208 | const std::string new_path = "assets/" + filename; |
| 209 | return OpenNonAsset(new_path, mode); |
| 210 | } |
| 211 | |
| 212 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie, |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 213 | Asset::AccessMode mode) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 214 | const std::string new_path = "assets/" + filename; |
| 215 | return OpenNonAsset(new_path, cookie, mode); |
| 216 | } |
| 217 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 218 | std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 219 | ATRACE_CALL(); |
| 220 | |
| 221 | std::string full_path = "assets/" + dirname; |
| 222 | std::unique_ptr<SortedVector<AssetDir::FileInfo>> files = |
| 223 | util::make_unique<SortedVector<AssetDir::FileInfo>>(); |
| 224 | |
| 225 | // Start from the back. |
| 226 | for (auto iter = apk_assets_.rbegin(); iter != apk_assets_.rend(); ++iter) { |
| 227 | const ApkAssets* apk_assets = *iter; |
| 228 | |
| 229 | auto func = [&](const StringPiece& name, FileType type) { |
| 230 | AssetDir::FileInfo info; |
| 231 | info.setFileName(String8(name.data(), name.size())); |
| 232 | info.setFileType(type); |
| 233 | info.setSourceName(String8(apk_assets->GetPath().c_str())); |
| 234 | files->add(info); |
| 235 | }; |
| 236 | |
| 237 | if (!apk_assets->ForEachFile(full_path, func)) { |
| 238 | return {}; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>(); |
| 243 | asset_dir->setFileList(files.release()); |
| 244 | return asset_dir; |
| 245 | } |
| 246 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 247 | // Search in reverse because that's how we used to do it and we need to preserve behaviour. |
| 248 | // This is unfortunate, because ClassLoaders delegate to the parent first, so the order |
| 249 | // is inconsistent for split APKs. |
| 250 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
| 251 | Asset::AccessMode mode, |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 252 | ApkAssetsCookie* out_cookie) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 253 | ATRACE_CALL(); |
| 254 | for (int32_t i = apk_assets_.size() - 1; i >= 0; i--) { |
| 255 | std::unique_ptr<Asset> asset = apk_assets_[i]->Open(filename, mode); |
| 256 | if (asset) { |
| 257 | if (out_cookie != nullptr) { |
| 258 | *out_cookie = i; |
| 259 | } |
| 260 | return asset; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (out_cookie != nullptr) { |
| 265 | *out_cookie = kInvalidCookie; |
| 266 | } |
| 267 | return {}; |
| 268 | } |
| 269 | |
| 270 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 271 | ApkAssetsCookie cookie, Asset::AccessMode mode) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 272 | ATRACE_CALL(); |
| 273 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 274 | return {}; |
| 275 | } |
| 276 | return apk_assets_[cookie]->Open(filename, mode); |
| 277 | } |
| 278 | |
| 279 | ApkAssetsCookie AssetManager2::FindEntry(uint32_t resid, uint16_t density_override, |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 280 | bool stop_at_first_match, FindEntryResult* out_entry) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 281 | // Might use this if density_override != 0. |
| 282 | ResTable_config density_override_config; |
| 283 | |
| 284 | // Select our configuration or generate a density override configuration. |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 285 | ResTable_config* desired_config = &configuration_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 286 | if (density_override != 0 && density_override != configuration_.density) { |
| 287 | density_override_config = configuration_; |
| 288 | density_override_config.density = density_override; |
| 289 | desired_config = &density_override_config; |
| 290 | } |
| 291 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 292 | if (!is_valid_resid(resid)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 293 | LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid); |
| 294 | return kInvalidCookie; |
| 295 | } |
| 296 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 297 | const uint32_t package_id = get_package_id(resid); |
| 298 | const uint8_t type_idx = get_type_id(resid) - 1; |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 299 | const uint16_t entry_id = get_entry_id(resid); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 300 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 301 | const uint8_t idx = package_ids_[package_id]; |
| 302 | if (idx == 0xff) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 303 | LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.", package_id, resid); |
| 304 | return kInvalidCookie; |
| 305 | } |
| 306 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 307 | FindEntryResult best_entry; |
Adam Lesinski | 64ee69d | 2018-01-08 17:38:30 -0800 | [diff] [blame] | 308 | ApkAssetsCookie best_cookie = kInvalidCookie; |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 309 | uint32_t cumulated_flags = 0u; |
Adam Lesinski | 64ee69d | 2018-01-08 17:38:30 -0800 | [diff] [blame] | 310 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 311 | const PackageGroup& package_group = package_groups_[idx]; |
| 312 | const size_t package_count = package_group.packages_.size(); |
| 313 | FindEntryResult current_entry; |
| 314 | for (size_t i = 0; i < package_count; i++) { |
| 315 | const LoadedPackage* loaded_package = package_group.packages_[i]; |
| 316 | if (!loaded_package->FindEntry(type_idx, entry_id, *desired_config, ¤t_entry)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 317 | continue; |
| 318 | } |
| 319 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 320 | cumulated_flags |= current_entry.type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 321 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 322 | const ResTable_config* current_config = current_entry.config; |
| 323 | const ResTable_config* best_config = best_entry.config; |
| 324 | if (best_cookie == kInvalidCookie || |
| 325 | current_config->isBetterThan(*best_config, desired_config) || |
| 326 | (loaded_package->IsOverlay() && current_config->compare(*best_config) == 0)) { |
| 327 | best_entry = current_entry; |
| 328 | best_cookie = package_group.cookies_[i]; |
| 329 | if (stop_at_first_match) { |
| 330 | break; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 335 | if (best_cookie == kInvalidCookie) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 336 | return kInvalidCookie; |
| 337 | } |
| 338 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 339 | *out_entry = best_entry; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 340 | out_entry->dynamic_ref_table = &package_group.dynamic_ref_table; |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 341 | out_entry->type_flags = cumulated_flags; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 342 | return best_cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 345 | bool AssetManager2::GetResourceName(uint32_t resid, ResourceName* out_name) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 346 | ATRACE_CALL(); |
| 347 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 348 | FindEntryResult entry; |
| 349 | ApkAssetsCookie cookie = |
| 350 | FindEntry(resid, 0u /* density_override */, true /* stop_at_first_match */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 351 | if (cookie == kInvalidCookie) { |
| 352 | return false; |
| 353 | } |
| 354 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 355 | const LoadedPackage* package = apk_assets_[cookie]->GetLoadedArsc()->GetPackageForId(resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 356 | if (package == nullptr) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 360 | out_name->package = package->GetPackageName().data(); |
| 361 | out_name->package_len = package->GetPackageName().size(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 362 | |
| 363 | out_name->type = entry.type_string_ref.string8(&out_name->type_len); |
| 364 | out_name->type16 = nullptr; |
| 365 | if (out_name->type == nullptr) { |
| 366 | out_name->type16 = entry.type_string_ref.string16(&out_name->type_len); |
| 367 | if (out_name->type16 == nullptr) { |
| 368 | return false; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | out_name->entry = entry.entry_string_ref.string8(&out_name->entry_len); |
| 373 | out_name->entry16 = nullptr; |
| 374 | if (out_name->entry == nullptr) { |
| 375 | out_name->entry16 = entry.entry_string_ref.string16(&out_name->entry_len); |
| 376 | if (out_name->entry16 == nullptr) { |
| 377 | return false; |
| 378 | } |
| 379 | } |
| 380 | return true; |
| 381 | } |
| 382 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 383 | bool AssetManager2::GetResourceFlags(uint32_t resid, uint32_t* out_flags) { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 384 | FindEntryResult entry; |
| 385 | ApkAssetsCookie cookie = |
| 386 | FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, &entry); |
| 387 | if (cookie != kInvalidCookie) { |
| 388 | *out_flags = entry.type_flags; |
| 389 | return cookie; |
| 390 | } |
| 391 | return kInvalidCookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | ApkAssetsCookie AssetManager2::GetResource(uint32_t resid, bool may_be_bag, |
| 395 | uint16_t density_override, Res_value* out_value, |
| 396 | ResTable_config* out_selected_config, |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 397 | uint32_t* out_flags) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 398 | ATRACE_CALL(); |
| 399 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 400 | FindEntryResult entry; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 401 | ApkAssetsCookie cookie = |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 402 | FindEntry(resid, density_override, false /* stop_at_first_match */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 403 | if (cookie == kInvalidCookie) { |
| 404 | return kInvalidCookie; |
| 405 | } |
| 406 | |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 407 | if (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 408 | if (!may_be_bag) { |
| 409 | LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 410 | return kInvalidCookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 411 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 412 | |
| 413 | // Create a reference since we can't represent this complex type as a Res_value. |
| 414 | out_value->dataType = Res_value::TYPE_REFERENCE; |
| 415 | out_value->data = resid; |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 416 | *out_selected_config = *entry.config; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 417 | *out_flags = entry.type_flags; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 418 | return cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | const Res_value* device_value = reinterpret_cast<const Res_value*>( |
| 422 | reinterpret_cast<const uint8_t*>(entry.entry) + dtohs(entry.entry->size)); |
| 423 | out_value->copyFrom_dtoh(*device_value); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 424 | |
| 425 | // Convert the package ID to the runtime assigned package ID. |
| 426 | entry.dynamic_ref_table->lookupResourceValue(out_value); |
| 427 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 428 | *out_selected_config = *entry.config; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 429 | *out_flags = entry.type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 430 | return cookie; |
| 431 | } |
| 432 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 433 | ApkAssetsCookie AssetManager2::ResolveReference(ApkAssetsCookie cookie, Res_value* in_out_value, |
| 434 | ResTable_config* in_out_selected_config, |
| 435 | uint32_t* in_out_flags, |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 436 | uint32_t* out_last_reference) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 437 | ATRACE_CALL(); |
| 438 | constexpr const int kMaxIterations = 20; |
| 439 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 440 | for (size_t iteration = 0u; in_out_value->dataType == Res_value::TYPE_REFERENCE && |
| 441 | in_out_value->data != 0u && iteration < kMaxIterations; |
| 442 | iteration++) { |
Adam Lesinski | 1187590 | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 443 | *out_last_reference = in_out_value->data; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 444 | uint32_t new_flags = 0u; |
| 445 | cookie = GetResource(in_out_value->data, true /*may_be_bag*/, 0u /*density_override*/, |
| 446 | in_out_value, in_out_selected_config, &new_flags); |
| 447 | if (cookie == kInvalidCookie) { |
| 448 | return kInvalidCookie; |
| 449 | } |
| 450 | if (in_out_flags != nullptr) { |
| 451 | *in_out_flags |= new_flags; |
| 452 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 453 | if (*out_last_reference == in_out_value->data) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 454 | // This reference can't be resolved, so exit now and let the caller deal with it. |
| 455 | return cookie; |
| 456 | } |
| 457 | } |
| 458 | return cookie; |
| 459 | } |
| 460 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 461 | const ResolvedBag* AssetManager2::GetBag(uint32_t resid) { |
| 462 | ATRACE_CALL(); |
| 463 | |
| 464 | auto cached_iter = cached_bags_.find(resid); |
| 465 | if (cached_iter != cached_bags_.end()) { |
| 466 | return cached_iter->second.get(); |
| 467 | } |
| 468 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 469 | FindEntryResult entry; |
| 470 | ApkAssetsCookie cookie = |
| 471 | FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 472 | if (cookie == kInvalidCookie) { |
| 473 | return nullptr; |
| 474 | } |
| 475 | |
| 476 | // Check that the size of the entry header is at least as big as |
| 477 | // the desired ResTable_map_entry. Also verify that the entry |
| 478 | // was intended to be a map. |
| 479 | if (dtohs(entry.entry->size) < sizeof(ResTable_map_entry) || |
| 480 | (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) == 0) { |
| 481 | // Not a bag, nothing to do. |
| 482 | return nullptr; |
| 483 | } |
| 484 | |
| 485 | const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry.entry); |
| 486 | const ResTable_map* map_entry = |
| 487 | reinterpret_cast<const ResTable_map*>(reinterpret_cast<const uint8_t*>(map) + map->size); |
| 488 | const ResTable_map* const map_entry_end = map_entry + dtohl(map->count); |
| 489 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 490 | uint32_t parent_resid = dtohl(map->parent.ident); |
| 491 | if (parent_resid == 0) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 492 | // There is no parent, meaning there is nothing to inherit and we can do a simple |
| 493 | // copy of the entries in the map. |
| 494 | const size_t entry_count = map_entry_end - map_entry; |
| 495 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 496 | malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))}; |
| 497 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 498 | for (; map_entry != map_entry_end; ++map_entry) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 499 | uint32_t new_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 500 | if (!is_internal_resid(new_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 501 | // Attributes, arrays, etc don't have a resource id as the name. They specify |
| 502 | // other data, which would be wrong to change via a lookup. |
| 503 | if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) { |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 504 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 505 | return nullptr; |
| 506 | } |
| 507 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 508 | new_entry->cookie = cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 509 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 510 | new_entry->key_pool = nullptr; |
| 511 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 512 | new_entry->value.copyFrom_dtoh(map_entry->value); |
| 513 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 514 | if (err != NO_ERROR) { |
| 515 | LOG(ERROR) << base::StringPrintf( |
| 516 | "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType, |
| 517 | new_entry->value.data, new_key); |
| 518 | return nullptr; |
| 519 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 520 | ++new_entry; |
| 521 | } |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 522 | new_bag->type_spec_flags = entry.type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 523 | new_bag->entry_count = static_cast<uint32_t>(entry_count); |
| 524 | ResolvedBag* result = new_bag.get(); |
| 525 | cached_bags_[resid] = std::move(new_bag); |
| 526 | return result; |
| 527 | } |
| 528 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 529 | // In case the parent is a dynamic reference, resolve it. |
| 530 | entry.dynamic_ref_table->lookupResourceId(&parent_resid); |
| 531 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 532 | // Get the parent and do a merge of the keys. |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 533 | const ResolvedBag* parent_bag = GetBag(parent_resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 534 | if (parent_bag == nullptr) { |
| 535 | // Failed to get the parent that should exist. |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 536 | LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid, resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 537 | return nullptr; |
| 538 | } |
| 539 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 540 | // Create the max possible entries we can make. Once we construct the bag, |
| 541 | // we will realloc to fit to size. |
| 542 | const size_t max_count = parent_bag->entry_count + dtohl(map->count); |
George Burgess IV | 09b119f | 2017-07-25 15:00:04 -0700 | [diff] [blame] | 543 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 544 | malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 545 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 546 | |
| 547 | const ResolvedBag::Entry* parent_entry = parent_bag->entries; |
| 548 | const ResolvedBag::Entry* const parent_entry_end = parent_entry + parent_bag->entry_count; |
| 549 | |
| 550 | // The keys are expected to be in sorted order. Merge the two bags. |
| 551 | while (map_entry != map_entry_end && parent_entry != parent_entry_end) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 552 | uint32_t child_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 553 | if (!is_internal_resid(child_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 554 | if (entry.dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR) { |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 555 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key, resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 556 | return nullptr; |
| 557 | } |
| 558 | } |
| 559 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 560 | if (child_key <= parent_entry->key) { |
| 561 | // Use the child key if it comes before the parent |
| 562 | // or is equal to the parent (overrides). |
| 563 | new_entry->cookie = cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 564 | new_entry->key = child_key; |
| 565 | new_entry->key_pool = nullptr; |
| 566 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 567 | new_entry->value.copyFrom_dtoh(map_entry->value); |
| 568 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 569 | if (err != NO_ERROR) { |
| 570 | LOG(ERROR) << base::StringPrintf( |
| 571 | "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType, |
| 572 | new_entry->value.data, child_key); |
| 573 | return nullptr; |
| 574 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 575 | ++map_entry; |
| 576 | } else { |
| 577 | // Take the parent entry as-is. |
| 578 | memcpy(new_entry, parent_entry, sizeof(*new_entry)); |
| 579 | } |
| 580 | |
| 581 | if (child_key >= parent_entry->key) { |
| 582 | // Move to the next parent entry if we used it or it was overridden. |
| 583 | ++parent_entry; |
| 584 | } |
| 585 | // Increment to the next entry to fill. |
| 586 | ++new_entry; |
| 587 | } |
| 588 | |
| 589 | // Finish the child entries if they exist. |
| 590 | while (map_entry != map_entry_end) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 591 | uint32_t new_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 592 | if (!is_internal_resid(new_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 593 | if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) { |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 594 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 595 | return nullptr; |
| 596 | } |
| 597 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 598 | new_entry->cookie = cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 599 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 600 | new_entry->key_pool = nullptr; |
| 601 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 602 | new_entry->value.copyFrom_dtoh(map_entry->value); |
| 603 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 604 | if (err != NO_ERROR) { |
| 605 | LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", |
| 606 | new_entry->value.dataType, new_entry->value.data, new_key); |
| 607 | return nullptr; |
| 608 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 609 | ++map_entry; |
| 610 | ++new_entry; |
| 611 | } |
| 612 | |
| 613 | // Finish the parent entries if they exist. |
| 614 | if (parent_entry != parent_entry_end) { |
| 615 | // Take the rest of the parent entries as-is. |
| 616 | const size_t num_entries_to_copy = parent_entry_end - parent_entry; |
| 617 | memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry)); |
| 618 | new_entry += num_entries_to_copy; |
| 619 | } |
| 620 | |
| 621 | // Resize the resulting array to fit. |
| 622 | const size_t actual_count = new_entry - new_bag->entries; |
| 623 | if (actual_count != max_count) { |
George Burgess IV | 09b119f | 2017-07-25 15:00:04 -0700 | [diff] [blame] | 624 | new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc( |
| 625 | new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry))))); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 628 | // Combine flags from the parent and our own bag. |
| 629 | new_bag->type_spec_flags = entry.type_flags | parent_bag->type_spec_flags; |
George Burgess IV | 09b119f | 2017-07-25 15:00:04 -0700 | [diff] [blame] | 630 | new_bag->entry_count = static_cast<uint32_t>(actual_count); |
| 631 | ResolvedBag* result = new_bag.get(); |
| 632 | cached_bags_[resid] = std::move(new_bag); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 633 | return result; |
| 634 | } |
| 635 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 636 | static bool Utf8ToUtf16(const StringPiece& str, std::u16string* out) { |
| 637 | ssize_t len = |
| 638 | utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false); |
| 639 | if (len < 0) { |
| 640 | return false; |
| 641 | } |
| 642 | out->resize(static_cast<size_t>(len)); |
| 643 | utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(), |
| 644 | static_cast<size_t>(len + 1)); |
| 645 | return true; |
| 646 | } |
| 647 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 648 | uint32_t AssetManager2::GetResourceId(const std::string& resource_name, |
| 649 | const std::string& fallback_type, |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 650 | const std::string& fallback_package) { |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 651 | StringPiece package_name, type, entry; |
| 652 | if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) { |
| 653 | return 0u; |
| 654 | } |
| 655 | |
| 656 | if (entry.empty()) { |
| 657 | return 0u; |
| 658 | } |
| 659 | |
| 660 | if (package_name.empty()) { |
| 661 | package_name = fallback_package; |
| 662 | } |
| 663 | |
| 664 | if (type.empty()) { |
| 665 | type = fallback_type; |
| 666 | } |
| 667 | |
| 668 | std::u16string type16; |
| 669 | if (!Utf8ToUtf16(type, &type16)) { |
| 670 | return 0u; |
| 671 | } |
| 672 | |
| 673 | std::u16string entry16; |
| 674 | if (!Utf8ToUtf16(entry, &entry16)) { |
| 675 | return 0u; |
| 676 | } |
| 677 | |
| 678 | const StringPiece16 kAttr16 = u"attr"; |
| 679 | const static std::u16string kAttrPrivate16 = u"^attr-private"; |
| 680 | |
| 681 | for (const PackageGroup& package_group : package_groups_) { |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 682 | for (const LoadedPackage* package : package_group.packages_) { |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 683 | if (package_name != package->GetPackageName()) { |
| 684 | // All packages in the same group are expected to have the same package name. |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | uint32_t resid = package->FindEntryByName(type16, entry16); |
| 689 | if (resid == 0u && kAttr16 == type16) { |
| 690 | // Private attributes in libraries (such as the framework) are sometimes encoded |
| 691 | // under the type '^attr-private' in order to leave the ID space of public 'attr' |
| 692 | // free for future additions. Check '^attr-private' for the same name. |
| 693 | resid = package->FindEntryByName(kAttrPrivate16, entry16); |
| 694 | } |
| 695 | |
| 696 | if (resid != 0u) { |
| 697 | return fix_package_id(resid, package_group.dynamic_ref_table.mAssignedPackageId); |
| 698 | } |
| 699 | } |
| 700 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 701 | return 0u; |
| 702 | } |
| 703 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 704 | void AssetManager2::InvalidateCaches(uint32_t diff) { |
| 705 | if (diff == 0xffffffffu) { |
| 706 | // Everything must go. |
| 707 | cached_bags_.clear(); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | // Be more conservative with what gets purged. Only if the bag has other possible |
| 712 | // variations with respect to what changed (diff) should we remove it. |
| 713 | for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) { |
| 714 | if (diff & iter->second->type_spec_flags) { |
| 715 | iter = cached_bags_.erase(iter); |
| 716 | } else { |
| 717 | ++iter; |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 722 | std::unique_ptr<Theme> AssetManager2::NewTheme() { |
| 723 | return std::unique_ptr<Theme>(new Theme(this)); |
| 724 | } |
| 725 | |
| 726 | Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) { |
| 727 | } |
| 728 | |
| 729 | Theme::~Theme() = default; |
| 730 | |
| 731 | namespace { |
| 732 | |
| 733 | struct ThemeEntry { |
| 734 | ApkAssetsCookie cookie; |
| 735 | uint32_t type_spec_flags; |
| 736 | Res_value value; |
| 737 | }; |
| 738 | |
| 739 | struct ThemeType { |
| 740 | int entry_count; |
| 741 | ThemeEntry entries[0]; |
| 742 | }; |
| 743 | |
| 744 | constexpr size_t kTypeCount = std::numeric_limits<uint8_t>::max() + 1; |
| 745 | |
| 746 | } // namespace |
| 747 | |
| 748 | struct Theme::Package { |
| 749 | // Each element of Type will be a dynamically sized object |
| 750 | // allocated to have the entries stored contiguously with the Type. |
| 751 | std::array<util::unique_cptr<ThemeType>, kTypeCount> types; |
| 752 | }; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 753 | |
| 754 | bool Theme::ApplyStyle(uint32_t resid, bool force) { |
| 755 | ATRACE_CALL(); |
| 756 | |
| 757 | const ResolvedBag* bag = asset_manager_->GetBag(resid); |
| 758 | if (bag == nullptr) { |
| 759 | return false; |
| 760 | } |
| 761 | |
| 762 | // Merge the flags from this style. |
| 763 | type_spec_flags_ |= bag->type_spec_flags; |
| 764 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 765 | int last_type_idx = -1; |
| 766 | int last_package_idx = -1; |
| 767 | Package* last_package = nullptr; |
| 768 | ThemeType* last_type = nullptr; |
| 769 | |
| 770 | // Iterate backwards, because each bag is sorted in ascending key ID order, meaning we will only |
| 771 | // need to perform one resize per type. |
| 772 | using reverse_bag_iterator = std::reverse_iterator<const ResolvedBag::Entry*>; |
| 773 | const auto bag_iter_end = reverse_bag_iterator(begin(bag)); |
| 774 | for (auto bag_iter = reverse_bag_iterator(end(bag)); bag_iter != bag_iter_end; ++bag_iter) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 775 | const uint32_t attr_resid = bag_iter->key; |
| 776 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 777 | // If the resource ID passed in is not a style, the key can be some other identifier that is not |
| 778 | // a resource ID. We should fail fast instead of operating with strange resource IDs. |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 779 | if (!is_valid_resid(attr_resid)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 780 | return false; |
| 781 | } |
| 782 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 783 | // We don't use the 0-based index for the type so that we can avoid doing ID validation |
| 784 | // upon lookup. Instead, we keep space for the type ID 0 in our data structures. Since |
| 785 | // the construction of this type is guarded with a resource ID check, it will never be |
| 786 | // populated, and querying type ID 0 will always fail. |
| 787 | const int package_idx = get_package_id(attr_resid); |
| 788 | const int type_idx = get_type_id(attr_resid); |
| 789 | const int entry_idx = get_entry_id(attr_resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 790 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 791 | if (last_package_idx != package_idx) { |
| 792 | std::unique_ptr<Package>& package = packages_[package_idx]; |
| 793 | if (package == nullptr) { |
| 794 | package.reset(new Package()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 795 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 796 | last_package_idx = package_idx; |
| 797 | last_package = package.get(); |
| 798 | last_type_idx = -1; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 799 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 800 | |
| 801 | if (last_type_idx != type_idx) { |
| 802 | util::unique_cptr<ThemeType>& type = last_package->types[type_idx]; |
| 803 | if (type == nullptr) { |
| 804 | // Allocate enough memory to contain this entry_idx. Since we're iterating in reverse over |
| 805 | // a sorted list of attributes, this shouldn't be resized again during this method call. |
| 806 | type.reset(reinterpret_cast<ThemeType*>( |
| 807 | calloc(sizeof(ThemeType) + (entry_idx + 1) * sizeof(ThemeEntry), 1))); |
| 808 | type->entry_count = entry_idx + 1; |
| 809 | } else if (entry_idx >= type->entry_count) { |
| 810 | // Reallocate the memory to contain this entry_idx. Since we're iterating in reverse over |
| 811 | // a sorted list of attributes, this shouldn't be resized again during this method call. |
| 812 | const int new_count = entry_idx + 1; |
| 813 | type.reset(reinterpret_cast<ThemeType*>( |
| 814 | realloc(type.release(), sizeof(ThemeType) + (new_count * sizeof(ThemeEntry))))); |
| 815 | |
| 816 | // Clear out the newly allocated space (which isn't zeroed). |
| 817 | memset(type->entries + type->entry_count, 0, |
| 818 | (new_count - type->entry_count) * sizeof(ThemeEntry)); |
| 819 | type->entry_count = new_count; |
| 820 | } |
| 821 | last_type_idx = type_idx; |
| 822 | last_type = type.get(); |
| 823 | } |
| 824 | |
| 825 | ThemeEntry& entry = last_type->entries[entry_idx]; |
| 826 | if (force || (entry.value.dataType == Res_value::TYPE_NULL && |
| 827 | entry.value.data != Res_value::DATA_NULL_EMPTY)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 828 | entry.cookie = bag_iter->cookie; |
| 829 | entry.type_spec_flags |= bag->type_spec_flags; |
| 830 | entry.value = bag_iter->value; |
| 831 | } |
| 832 | } |
| 833 | return true; |
| 834 | } |
| 835 | |
| 836 | ApkAssetsCookie Theme::GetAttribute(uint32_t resid, Res_value* out_value, |
| 837 | uint32_t* out_flags) const { |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 838 | int cnt = 20; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 839 | |
| 840 | uint32_t type_spec_flags = 0u; |
| 841 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 842 | do { |
| 843 | const int package_idx = get_package_id(resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 844 | const Package* package = packages_[package_idx].get(); |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 845 | if (package != nullptr) { |
| 846 | // The themes are constructed with a 1-based type ID, so no need to decrement here. |
| 847 | const int type_idx = get_type_id(resid); |
| 848 | const ThemeType* type = package->types[type_idx].get(); |
| 849 | if (type != nullptr) { |
| 850 | const int entry_idx = get_entry_id(resid); |
| 851 | if (entry_idx < type->entry_count) { |
| 852 | const ThemeEntry& entry = type->entries[entry_idx]; |
| 853 | type_spec_flags |= entry.type_spec_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 854 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 855 | if (entry.value.dataType == Res_value::TYPE_ATTRIBUTE) { |
| 856 | if (cnt > 0) { |
| 857 | cnt--; |
| 858 | resid = entry.value.data; |
| 859 | continue; |
| 860 | } |
| 861 | return kInvalidCookie; |
| 862 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 863 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 864 | // @null is different than @empty. |
| 865 | if (entry.value.dataType == Res_value::TYPE_NULL && |
| 866 | entry.value.data != Res_value::DATA_NULL_EMPTY) { |
| 867 | return kInvalidCookie; |
| 868 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 869 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 870 | *out_value = entry.value; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 871 | *out_flags = type_spec_flags; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 872 | return entry.cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 873 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 874 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 875 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 876 | break; |
| 877 | } while (true); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 878 | return kInvalidCookie; |
| 879 | } |
| 880 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 881 | ApkAssetsCookie Theme::ResolveAttributeReference(ApkAssetsCookie cookie, Res_value* in_out_value, |
| 882 | ResTable_config* in_out_selected_config, |
| 883 | uint32_t* in_out_type_spec_flags, |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 884 | uint32_t* out_last_ref) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 885 | if (in_out_value->dataType == Res_value::TYPE_ATTRIBUTE) { |
| 886 | uint32_t new_flags; |
| 887 | cookie = GetAttribute(in_out_value->data, in_out_value, &new_flags); |
| 888 | if (cookie == kInvalidCookie) { |
| 889 | return kInvalidCookie; |
| 890 | } |
| 891 | |
| 892 | if (in_out_type_spec_flags != nullptr) { |
| 893 | *in_out_type_spec_flags |= new_flags; |
| 894 | } |
| 895 | } |
| 896 | return asset_manager_->ResolveReference(cookie, in_out_value, in_out_selected_config, |
| 897 | in_out_type_spec_flags, out_last_ref); |
| 898 | } |
| 899 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 900 | void Theme::Clear() { |
| 901 | type_spec_flags_ = 0u; |
| 902 | for (std::unique_ptr<Package>& package : packages_) { |
| 903 | package.reset(); |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | bool Theme::SetTo(const Theme& o) { |
| 908 | if (this == &o) { |
| 909 | return true; |
| 910 | } |
| 911 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 912 | type_spec_flags_ = o.type_spec_flags_; |
| 913 | |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 914 | const bool copy_only_system = asset_manager_ != o.asset_manager_; |
| 915 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 916 | for (size_t p = 0; p < packages_.size(); p++) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 917 | const Package* package = o.packages_[p].get(); |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 918 | if (package == nullptr || (copy_only_system && p != 0x01)) { |
| 919 | // The other theme doesn't have this package, clear ours. |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 920 | packages_[p].reset(); |
| 921 | continue; |
| 922 | } |
| 923 | |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 924 | if (packages_[p] == nullptr) { |
| 925 | // The other theme has this package, but we don't. Make one. |
| 926 | packages_[p].reset(new Package()); |
| 927 | } |
| 928 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 929 | for (size_t t = 0; t < package->types.size(); t++) { |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 930 | const ThemeType* type = package->types[t].get(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 931 | if (type == nullptr) { |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 932 | // The other theme doesn't have this type, clear ours. |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 933 | packages_[p]->types[t].reset(); |
| 934 | continue; |
| 935 | } |
| 936 | |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 937 | // Create a new type and update it to theirs. |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 938 | const size_t type_alloc_size = sizeof(ThemeType) + (type->entry_count * sizeof(ThemeEntry)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 939 | void* copied_data = malloc(type_alloc_size); |
| 940 | memcpy(copied_data, type, type_alloc_size); |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 941 | packages_[p]->types[t].reset(reinterpret_cast<ThemeType*>(copied_data)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | return true; |
| 945 | } |
| 946 | |
| 947 | } // namespace android |