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 | |
| 21 | #include "android-base/logging.h" |
| 22 | #include "android-base/stringprintf.h" |
| 23 | #include "utils/ByteOrder.h" |
| 24 | #include "utils/Trace.h" |
| 25 | |
| 26 | #ifdef _WIN32 |
| 27 | #ifdef ERROR |
| 28 | #undef ERROR |
| 29 | #endif |
| 30 | #endif |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | AssetManager2::AssetManager2() { memset(&configuration_, 0, sizeof(configuration_)); } |
| 35 | |
| 36 | bool AssetManager2::SetApkAssets(const std::vector<const ApkAssets*>& apk_assets, |
| 37 | bool invalidate_caches) { |
| 38 | apk_assets_ = apk_assets; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 39 | BuildDynamicRefTable(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 40 | if (invalidate_caches) { |
| 41 | InvalidateCaches(static_cast<uint32_t>(-1)); |
| 42 | } |
| 43 | return true; |
| 44 | } |
| 45 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 46 | void AssetManager2::BuildDynamicRefTable() { |
| 47 | package_groups_.clear(); |
| 48 | package_ids_.fill(0xff); |
| 49 | |
| 50 | // 0x01 is reserved for the android package. |
| 51 | int next_package_id = 0x02; |
| 52 | const size_t apk_assets_count = apk_assets_.size(); |
| 53 | for (size_t i = 0; i < apk_assets_count; i++) { |
| 54 | const ApkAssets* apk_asset = apk_assets_[i]; |
| 55 | for (const std::unique_ptr<const LoadedPackage>& package : |
| 56 | apk_asset->GetLoadedArsc()->GetPackages()) { |
| 57 | // Get the package ID or assign one if a shared library. |
| 58 | int package_id; |
| 59 | if (package->IsDynamic()) { |
| 60 | package_id = next_package_id++; |
| 61 | } else { |
| 62 | package_id = package->GetPackageId(); |
| 63 | } |
| 64 | |
| 65 | // Add the mapping for package ID to index if not present. |
| 66 | uint8_t idx = package_ids_[package_id]; |
| 67 | if (idx == 0xff) { |
| 68 | package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size()); |
| 69 | package_groups_.push_back({}); |
| 70 | package_groups_.back().dynamic_ref_table.mAssignedPackageId = package_id; |
| 71 | } |
| 72 | PackageGroup* package_group = &package_groups_[idx]; |
| 73 | |
| 74 | // Add the package and to the set of packages with the same ID. |
| 75 | package_group->packages_.push_back(package.get()); |
| 76 | package_group->cookies_.push_back(static_cast<ApkAssetsCookie>(i)); |
| 77 | |
| 78 | // Add the package name -> build time ID mappings. |
| 79 | for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) { |
| 80 | String16 package_name(entry.package_name.c_str(), entry.package_name.size()); |
| 81 | package_group->dynamic_ref_table.mEntries.replaceValueFor( |
| 82 | package_name, static_cast<uint8_t>(entry.package_id)); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Now assign the runtime IDs so that we have a build-time to runtime ID map. |
| 88 | const auto package_groups_end = package_groups_.end(); |
| 89 | for (auto iter = package_groups_.begin(); iter != package_groups_end; ++iter) { |
| 90 | const std::string& package_name = iter->packages_[0]->GetPackageName(); |
| 91 | for (auto iter2 = package_groups_.begin(); iter2 != package_groups_end; ++iter2) { |
| 92 | iter2->dynamic_ref_table.addMapping(String16(package_name.c_str(), package_name.size()), |
| 93 | iter->dynamic_ref_table.mAssignedPackageId); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void AssetManager2::DumpToLog() const { |
| 99 | base::ScopedLogSeverity _log(base::INFO); |
| 100 | |
| 101 | std::string list; |
| 102 | for (size_t i = 0; i < package_ids_.size(); i++) { |
| 103 | if (package_ids_[i] != 0xff) { |
| 104 | base::StringAppendF(&list, "%02x -> %d, ", (int) i, package_ids_[i]); |
| 105 | } |
| 106 | } |
| 107 | LOG(INFO) << "Package ID map: " << list; |
| 108 | |
| 109 | for (const auto& package_group: package_groups_) { |
| 110 | list = ""; |
| 111 | for (const auto& package : package_group.packages_) { |
| 112 | base::StringAppendF(&list, "%s(%02x), ", package->GetPackageName().c_str(), package->GetPackageId()); |
| 113 | } |
| 114 | LOG(INFO) << base::StringPrintf("PG (%02x): ", package_group.dynamic_ref_table.mAssignedPackageId) << list; |
| 115 | } |
| 116 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 117 | |
| 118 | const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const { |
| 119 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 120 | return nullptr; |
| 121 | } |
| 122 | return apk_assets_[cookie]->GetLoadedArsc()->GetStringPool(); |
| 123 | } |
| 124 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 125 | const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const { |
| 126 | if (package_id >= package_ids_.size()) { |
| 127 | return nullptr; |
| 128 | } |
| 129 | |
| 130 | const size_t idx = package_ids_[package_id]; |
| 131 | if (idx == 0xff) { |
| 132 | return nullptr; |
| 133 | } |
| 134 | return &package_groups_[idx].dynamic_ref_table; |
| 135 | } |
| 136 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 137 | void AssetManager2::SetConfiguration(const ResTable_config& configuration) { |
| 138 | const int diff = configuration_.diff(configuration); |
| 139 | configuration_ = configuration; |
| 140 | |
| 141 | if (diff) { |
| 142 | InvalidateCaches(static_cast<uint32_t>(diff)); |
| 143 | } |
| 144 | } |
| 145 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 146 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, Asset::AccessMode mode) { |
| 147 | const std::string new_path = "assets/" + filename; |
| 148 | return OpenNonAsset(new_path, mode); |
| 149 | } |
| 150 | |
| 151 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie, |
| 152 | Asset::AccessMode mode) { |
| 153 | const std::string new_path = "assets/" + filename; |
| 154 | return OpenNonAsset(new_path, cookie, mode); |
| 155 | } |
| 156 | |
| 157 | // Search in reverse because that's how we used to do it and we need to preserve behaviour. |
| 158 | // This is unfortunate, because ClassLoaders delegate to the parent first, so the order |
| 159 | // is inconsistent for split APKs. |
| 160 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
| 161 | Asset::AccessMode mode, |
| 162 | ApkAssetsCookie* out_cookie) { |
| 163 | ATRACE_CALL(); |
| 164 | for (int32_t i = apk_assets_.size() - 1; i >= 0; i--) { |
| 165 | std::unique_ptr<Asset> asset = apk_assets_[i]->Open(filename, mode); |
| 166 | if (asset) { |
| 167 | if (out_cookie != nullptr) { |
| 168 | *out_cookie = i; |
| 169 | } |
| 170 | return asset; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (out_cookie != nullptr) { |
| 175 | *out_cookie = kInvalidCookie; |
| 176 | } |
| 177 | return {}; |
| 178 | } |
| 179 | |
| 180 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
| 181 | ApkAssetsCookie cookie, Asset::AccessMode mode) { |
| 182 | ATRACE_CALL(); |
| 183 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 184 | return {}; |
| 185 | } |
| 186 | return apk_assets_[cookie]->Open(filename, mode); |
| 187 | } |
| 188 | |
| 189 | ApkAssetsCookie AssetManager2::FindEntry(uint32_t resid, uint16_t density_override, |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 190 | bool stop_at_first_match, LoadedArscEntry* out_entry, |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 191 | ResTable_config* out_selected_config, |
| 192 | uint32_t* out_flags) { |
| 193 | ATRACE_CALL(); |
| 194 | |
| 195 | // Might use this if density_override != 0. |
| 196 | ResTable_config density_override_config; |
| 197 | |
| 198 | // Select our configuration or generate a density override configuration. |
| 199 | ResTable_config* desired_config = &configuration_; |
| 200 | if (density_override != 0 && density_override != configuration_.density) { |
| 201 | density_override_config = configuration_; |
| 202 | density_override_config.density = density_override; |
| 203 | desired_config = &density_override_config; |
| 204 | } |
| 205 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 206 | const uint32_t package_id = util::get_package_id(resid); |
| 207 | const uint8_t type_id = util::get_type_id(resid); |
| 208 | const uint16_t entry_id = util::get_entry_id(resid); |
| 209 | |
| 210 | if (type_id == 0) { |
| 211 | LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid); |
| 212 | return kInvalidCookie; |
| 213 | } |
| 214 | |
| 215 | const uint8_t idx = package_ids_[package_id]; |
| 216 | if (idx == 0xff) { |
| 217 | LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.", package_id, resid); |
| 218 | return kInvalidCookie; |
| 219 | } |
| 220 | |
| 221 | LoadedArscEntry best_entry; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 222 | ResTable_config best_config; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 223 | ApkAssetsCookie best_cookie = kInvalidCookie; |
| 224 | uint32_t cumulated_flags = 0u; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 225 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 226 | const PackageGroup& package_group = package_groups_[idx]; |
| 227 | const size_t package_count = package_group.packages_.size(); |
| 228 | for (size_t i = 0; i < package_count; i++) { |
| 229 | LoadedArscEntry current_entry; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 230 | ResTable_config current_config; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 231 | uint32_t current_flags = 0; |
| 232 | |
| 233 | const LoadedPackage* loaded_package = package_group.packages_[i]; |
| 234 | if (!loaded_package->FindEntry(type_id - 1, entry_id, *desired_config, ¤t_entry, |
| 235 | ¤t_config, ¤t_flags)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 236 | continue; |
| 237 | } |
| 238 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 239 | cumulated_flags |= current_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 240 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 241 | if (best_cookie == kInvalidCookie || current_config.isBetterThan(best_config, desired_config)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 242 | best_entry = current_entry; |
| 243 | best_config = current_config; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 244 | best_cookie = package_group.cookies_[i]; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 245 | if (stop_at_first_match) { |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 251 | if (best_cookie == kInvalidCookie) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 252 | return kInvalidCookie; |
| 253 | } |
| 254 | |
| 255 | *out_entry = best_entry; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 256 | out_entry->dynamic_ref_table = &package_group.dynamic_ref_table; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 257 | *out_selected_config = best_config; |
| 258 | *out_flags = cumulated_flags; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 259 | return best_cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | bool AssetManager2::GetResourceName(uint32_t resid, ResourceName* out_name) { |
| 263 | ATRACE_CALL(); |
| 264 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 265 | LoadedArscEntry entry; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 266 | ResTable_config config; |
| 267 | uint32_t flags = 0u; |
| 268 | ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */, |
| 269 | true /* stop_at_first_match */, &entry, &config, &flags); |
| 270 | if (cookie == kInvalidCookie) { |
| 271 | return false; |
| 272 | } |
| 273 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 274 | const LoadedPackage* package = apk_assets_[cookie]->GetLoadedArsc()->GetPackageForId(resid); |
| 275 | if (package == nullptr) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 276 | return false; |
| 277 | } |
| 278 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 279 | out_name->package = package->GetPackageName().data(); |
| 280 | out_name->package_len = package->GetPackageName().size(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 281 | |
| 282 | out_name->type = entry.type_string_ref.string8(&out_name->type_len); |
| 283 | out_name->type16 = nullptr; |
| 284 | if (out_name->type == nullptr) { |
| 285 | out_name->type16 = entry.type_string_ref.string16(&out_name->type_len); |
| 286 | if (out_name->type16 == nullptr) { |
| 287 | return false; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | out_name->entry = entry.entry_string_ref.string8(&out_name->entry_len); |
| 292 | out_name->entry16 = nullptr; |
| 293 | if (out_name->entry == nullptr) { |
| 294 | out_name->entry16 = entry.entry_string_ref.string16(&out_name->entry_len); |
| 295 | if (out_name->entry16 == nullptr) { |
| 296 | return false; |
| 297 | } |
| 298 | } |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | bool AssetManager2::GetResourceFlags(uint32_t resid, uint32_t* out_flags) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 303 | LoadedArscEntry entry; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 304 | ResTable_config config; |
| 305 | ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */, |
| 306 | false /* stop_at_first_match */, &entry, &config, out_flags); |
| 307 | return cookie != kInvalidCookie; |
| 308 | } |
| 309 | |
| 310 | ApkAssetsCookie AssetManager2::GetResource(uint32_t resid, bool may_be_bag, |
| 311 | uint16_t density_override, Res_value* out_value, |
| 312 | ResTable_config* out_selected_config, |
| 313 | uint32_t* out_flags) { |
| 314 | ATRACE_CALL(); |
| 315 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 316 | LoadedArscEntry entry; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 317 | ResTable_config config; |
| 318 | uint32_t flags = 0u; |
| 319 | ApkAssetsCookie cookie = |
| 320 | FindEntry(resid, density_override, false /* stop_at_first_match */, &entry, &config, &flags); |
| 321 | if (cookie == kInvalidCookie) { |
| 322 | return kInvalidCookie; |
| 323 | } |
| 324 | |
| 325 | if (dtohl(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) { |
| 326 | if (!may_be_bag) { |
| 327 | LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid); |
| 328 | } |
| 329 | return kInvalidCookie; |
| 330 | } |
| 331 | |
| 332 | const Res_value* device_value = reinterpret_cast<const Res_value*>( |
| 333 | reinterpret_cast<const uint8_t*>(entry.entry) + dtohs(entry.entry->size)); |
| 334 | out_value->copyFrom_dtoh(*device_value); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 335 | |
| 336 | // Convert the package ID to the runtime assigned package ID. |
| 337 | entry.dynamic_ref_table->lookupResourceValue(out_value); |
| 338 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 339 | *out_selected_config = config; |
| 340 | *out_flags = flags; |
| 341 | return cookie; |
| 342 | } |
| 343 | |
| 344 | const ResolvedBag* AssetManager2::GetBag(uint32_t resid) { |
| 345 | ATRACE_CALL(); |
| 346 | |
| 347 | auto cached_iter = cached_bags_.find(resid); |
| 348 | if (cached_iter != cached_bags_.end()) { |
| 349 | return cached_iter->second.get(); |
| 350 | } |
| 351 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 352 | LoadedArscEntry entry; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 353 | ResTable_config config; |
| 354 | uint32_t flags = 0u; |
| 355 | ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */, |
| 356 | false /* stop_at_first_match */, &entry, &config, &flags); |
| 357 | if (cookie == kInvalidCookie) { |
| 358 | return nullptr; |
| 359 | } |
| 360 | |
| 361 | // Check that the size of the entry header is at least as big as |
| 362 | // the desired ResTable_map_entry. Also verify that the entry |
| 363 | // was intended to be a map. |
| 364 | if (dtohs(entry.entry->size) < sizeof(ResTable_map_entry) || |
| 365 | (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) == 0) { |
| 366 | // Not a bag, nothing to do. |
| 367 | return nullptr; |
| 368 | } |
| 369 | |
| 370 | const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry.entry); |
| 371 | const ResTable_map* map_entry = |
| 372 | reinterpret_cast<const ResTable_map*>(reinterpret_cast<const uint8_t*>(map) + map->size); |
| 373 | const ResTable_map* const map_entry_end = map_entry + dtohl(map->count); |
| 374 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 375 | uint32_t parent_resid = dtohl(map->parent.ident); |
| 376 | if (parent_resid == 0) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 377 | // There is no parent, meaning there is nothing to inherit and we can do a simple |
| 378 | // copy of the entries in the map. |
| 379 | const size_t entry_count = map_entry_end - map_entry; |
| 380 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 381 | malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))}; |
| 382 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 383 | for (; map_entry != map_entry_end; ++map_entry) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 384 | uint32_t new_key = dtohl(map_entry->name.ident); |
| 385 | if (!util::is_internal_resid(new_key)) { |
| 386 | // Attributes, arrays, etc don't have a resource id as the name. They specify |
| 387 | // other data, which would be wrong to change via a lookup. |
| 388 | if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) { |
| 389 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, resid); |
| 390 | return nullptr; |
| 391 | } |
| 392 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 393 | new_entry->cookie = cookie; |
| 394 | new_entry->value.copyFrom_dtoh(map_entry->value); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 395 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 396 | new_entry->key_pool = nullptr; |
| 397 | new_entry->type_pool = nullptr; |
| 398 | ++new_entry; |
| 399 | } |
| 400 | new_bag->type_spec_flags = flags; |
| 401 | new_bag->entry_count = static_cast<uint32_t>(entry_count); |
| 402 | ResolvedBag* result = new_bag.get(); |
| 403 | cached_bags_[resid] = std::move(new_bag); |
| 404 | return result; |
| 405 | } |
| 406 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 407 | // In case the parent is a dynamic reference, resolve it. |
| 408 | entry.dynamic_ref_table->lookupResourceId(&parent_resid); |
| 409 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 410 | // Get the parent and do a merge of the keys. |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 411 | const ResolvedBag* parent_bag = GetBag(parent_resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 412 | if (parent_bag == nullptr) { |
| 413 | // Failed to get the parent that should exist. |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 414 | 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] | 415 | return nullptr; |
| 416 | } |
| 417 | |
| 418 | // Combine flags from the parent and our own bag. |
| 419 | flags |= parent_bag->type_spec_flags; |
| 420 | |
| 421 | // Create the max possible entries we can make. Once we construct the bag, |
| 422 | // we will realloc to fit to size. |
| 423 | const size_t max_count = parent_bag->entry_count + dtohl(map->count); |
| 424 | ResolvedBag* new_bag = reinterpret_cast<ResolvedBag*>( |
| 425 | malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry)))); |
| 426 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 427 | |
| 428 | const ResolvedBag::Entry* parent_entry = parent_bag->entries; |
| 429 | const ResolvedBag::Entry* const parent_entry_end = parent_entry + parent_bag->entry_count; |
| 430 | |
| 431 | // The keys are expected to be in sorted order. Merge the two bags. |
| 432 | while (map_entry != map_entry_end && parent_entry != parent_entry_end) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 433 | uint32_t child_key = dtohl(map_entry->name.ident); |
| 434 | if (!util::is_internal_resid(child_key)) { |
| 435 | if (entry.dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR) { |
| 436 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key, resid); |
| 437 | return nullptr; |
| 438 | } |
| 439 | } |
| 440 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 441 | if (child_key <= parent_entry->key) { |
| 442 | // Use the child key if it comes before the parent |
| 443 | // or is equal to the parent (overrides). |
| 444 | new_entry->cookie = cookie; |
| 445 | new_entry->value.copyFrom_dtoh(map_entry->value); |
| 446 | new_entry->key = child_key; |
| 447 | new_entry->key_pool = nullptr; |
| 448 | new_entry->type_pool = nullptr; |
| 449 | ++map_entry; |
| 450 | } else { |
| 451 | // Take the parent entry as-is. |
| 452 | memcpy(new_entry, parent_entry, sizeof(*new_entry)); |
| 453 | } |
| 454 | |
| 455 | if (child_key >= parent_entry->key) { |
| 456 | // Move to the next parent entry if we used it or it was overridden. |
| 457 | ++parent_entry; |
| 458 | } |
| 459 | // Increment to the next entry to fill. |
| 460 | ++new_entry; |
| 461 | } |
| 462 | |
| 463 | // Finish the child entries if they exist. |
| 464 | while (map_entry != map_entry_end) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 465 | uint32_t new_key = dtohl(map_entry->name.ident); |
| 466 | if (!util::is_internal_resid(new_key)) { |
| 467 | if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) { |
| 468 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, resid); |
| 469 | return nullptr; |
| 470 | } |
| 471 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 472 | new_entry->cookie = cookie; |
| 473 | new_entry->value.copyFrom_dtoh(map_entry->value); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 474 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 475 | new_entry->key_pool = nullptr; |
| 476 | new_entry->type_pool = nullptr; |
| 477 | ++map_entry; |
| 478 | ++new_entry; |
| 479 | } |
| 480 | |
| 481 | // Finish the parent entries if they exist. |
| 482 | if (parent_entry != parent_entry_end) { |
| 483 | // Take the rest of the parent entries as-is. |
| 484 | const size_t num_entries_to_copy = parent_entry_end - parent_entry; |
| 485 | memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry)); |
| 486 | new_entry += num_entries_to_copy; |
| 487 | } |
| 488 | |
| 489 | // Resize the resulting array to fit. |
| 490 | const size_t actual_count = new_entry - new_bag->entries; |
| 491 | if (actual_count != max_count) { |
| 492 | new_bag = reinterpret_cast<ResolvedBag*>( |
| 493 | realloc(new_bag, sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry)))); |
| 494 | } |
| 495 | |
| 496 | util::unique_cptr<ResolvedBag> final_bag{new_bag}; |
| 497 | final_bag->type_spec_flags = flags; |
| 498 | final_bag->entry_count = static_cast<uint32_t>(actual_count); |
| 499 | ResolvedBag* result = final_bag.get(); |
| 500 | cached_bags_[resid] = std::move(final_bag); |
| 501 | return result; |
| 502 | } |
| 503 | |
| 504 | void AssetManager2::InvalidateCaches(uint32_t diff) { |
| 505 | if (diff == 0xffffffffu) { |
| 506 | // Everything must go. |
| 507 | cached_bags_.clear(); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | // Be more conservative with what gets purged. Only if the bag has other possible |
| 512 | // variations with respect to what changed (diff) should we remove it. |
| 513 | for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) { |
| 514 | if (diff & iter->second->type_spec_flags) { |
| 515 | iter = cached_bags_.erase(iter); |
| 516 | } else { |
| 517 | ++iter; |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | std::unique_ptr<Theme> AssetManager2::NewTheme() { return std::unique_ptr<Theme>(new Theme(this)); } |
| 523 | |
| 524 | bool Theme::ApplyStyle(uint32_t resid, bool force) { |
| 525 | ATRACE_CALL(); |
| 526 | |
| 527 | const ResolvedBag* bag = asset_manager_->GetBag(resid); |
| 528 | if (bag == nullptr) { |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | // Merge the flags from this style. |
| 533 | type_spec_flags_ |= bag->type_spec_flags; |
| 534 | |
| 535 | // On the first iteration, verify the attribute IDs and |
| 536 | // update the entry count in each type. |
| 537 | const auto bag_iter_end = end(bag); |
| 538 | for (auto bag_iter = begin(bag); bag_iter != bag_iter_end; ++bag_iter) { |
| 539 | const uint32_t attr_resid = bag_iter->key; |
| 540 | |
| 541 | // If the resource ID passed in is not a style, the key can be |
| 542 | // some other identifier that is not a resource ID. |
| 543 | if (!util::is_valid_resid(attr_resid)) { |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | const uint32_t package_idx = util::get_package_id(attr_resid); |
| 548 | |
| 549 | // The type ID is 1-based, so subtract 1 to get an index. |
| 550 | const uint32_t type_idx = util::get_type_id(attr_resid) - 1; |
| 551 | const uint32_t entry_idx = util::get_entry_id(attr_resid); |
| 552 | |
| 553 | std::unique_ptr<Package>& package = packages_[package_idx]; |
| 554 | if (package == nullptr) { |
| 555 | package.reset(new Package()); |
| 556 | } |
| 557 | |
| 558 | util::unique_cptr<Type>& type = package->types[type_idx]; |
| 559 | if (type == nullptr) { |
| 560 | // Set the initial capacity to take up a total amount of 1024 bytes. |
| 561 | constexpr uint32_t kInitialCapacity = (1024u - sizeof(Type)) / sizeof(Entry); |
| 562 | const uint32_t initial_capacity = std::max(entry_idx, kInitialCapacity); |
| 563 | type.reset( |
| 564 | reinterpret_cast<Type*>(calloc(sizeof(Type) + (initial_capacity * sizeof(Entry)), 1))); |
| 565 | type->entry_capacity = initial_capacity; |
| 566 | } |
| 567 | |
| 568 | // Set the entry_count to include this entry. We will populate |
| 569 | // and resize the array as necessary in the next pass. |
| 570 | if (entry_idx + 1 > type->entry_count) { |
| 571 | // Increase the entry count to include this. |
| 572 | type->entry_count = entry_idx + 1; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | // On the second pass, we will realloc to fit the entry counts |
| 577 | // and populate the structures. |
| 578 | for (auto bag_iter = begin(bag); bag_iter != bag_iter_end; ++bag_iter) { |
| 579 | const uint32_t attr_resid = bag_iter->key; |
| 580 | const uint32_t package_idx = util::get_package_id(attr_resid); |
| 581 | const uint32_t type_idx = util::get_type_id(attr_resid) - 1; |
| 582 | const uint32_t entry_idx = util::get_entry_id(attr_resid); |
| 583 | Package* package = packages_[package_idx].get(); |
| 584 | util::unique_cptr<Type>& type = package->types[type_idx]; |
| 585 | if (type->entry_count != type->entry_capacity) { |
| 586 | // Resize to fit the actual entries that will be included. |
| 587 | Type* type_ptr = type.release(); |
| 588 | type.reset(reinterpret_cast<Type*>( |
| 589 | realloc(type_ptr, sizeof(Type) + (type_ptr->entry_count * sizeof(Entry))))); |
| 590 | if (type->entry_capacity < type->entry_count) { |
| 591 | // Clear the newly allocated memory (which does not get zero initialized). |
| 592 | // We need to do this because we |= type_spec_flags. |
| 593 | memset(type->entries + type->entry_capacity, 0, |
| 594 | sizeof(Entry) * (type->entry_count - type->entry_capacity)); |
| 595 | } |
| 596 | type->entry_capacity = type->entry_count; |
| 597 | } |
| 598 | Entry& entry = type->entries[entry_idx]; |
| 599 | if (force || entry.value.dataType == Res_value::TYPE_NULL) { |
| 600 | entry.cookie = bag_iter->cookie; |
| 601 | entry.type_spec_flags |= bag->type_spec_flags; |
| 602 | entry.value = bag_iter->value; |
| 603 | } |
| 604 | } |
| 605 | return true; |
| 606 | } |
| 607 | |
| 608 | ApkAssetsCookie Theme::GetAttribute(uint32_t resid, Res_value* out_value, |
| 609 | uint32_t* out_flags) const { |
| 610 | constexpr const int kMaxIterations = 20; |
| 611 | |
| 612 | uint32_t type_spec_flags = 0u; |
| 613 | |
| 614 | for (int iterations_left = kMaxIterations; iterations_left > 0; iterations_left--) { |
| 615 | if (!util::is_valid_resid(resid)) { |
| 616 | return kInvalidCookie; |
| 617 | } |
| 618 | |
| 619 | const uint32_t package_idx = util::get_package_id(resid); |
| 620 | |
| 621 | // Type ID is 1-based, subtract 1 to get the index. |
| 622 | const uint32_t type_idx = util::get_type_id(resid) - 1; |
| 623 | const uint32_t entry_idx = util::get_entry_id(resid); |
| 624 | |
| 625 | const Package* package = packages_[package_idx].get(); |
| 626 | if (package == nullptr) { |
| 627 | return kInvalidCookie; |
| 628 | } |
| 629 | |
| 630 | const Type* type = package->types[type_idx].get(); |
| 631 | if (type == nullptr) { |
| 632 | return kInvalidCookie; |
| 633 | } |
| 634 | |
| 635 | if (entry_idx >= type->entry_count) { |
| 636 | return kInvalidCookie; |
| 637 | } |
| 638 | |
| 639 | const Entry& entry = type->entries[entry_idx]; |
| 640 | type_spec_flags |= entry.type_spec_flags; |
| 641 | |
| 642 | switch (entry.value.dataType) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 643 | case Res_value::TYPE_NULL: |
| 644 | return kInvalidCookie; |
| 645 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 646 | case Res_value::TYPE_ATTRIBUTE: |
| 647 | resid = entry.value.data; |
| 648 | break; |
| 649 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 650 | case Res_value::TYPE_DYNAMIC_ATTRIBUTE: { |
| 651 | // Resolve the dynamic attribute to a normal attribute |
| 652 | // (with the right package ID). |
| 653 | resid = entry.value.data; |
| 654 | const DynamicRefTable* ref_table = |
| 655 | asset_manager_->GetDynamicRefTableForPackage(package_idx); |
| 656 | if (ref_table == nullptr || ref_table->lookupResourceId(&resid) != NO_ERROR) { |
| 657 | LOG(ERROR) << base::StringPrintf("Failed to resolve dynamic attribute 0x%08x", resid); |
| 658 | return kInvalidCookie; |
| 659 | } |
| 660 | } break; |
| 661 | |
| 662 | case Res_value::TYPE_DYNAMIC_REFERENCE: { |
| 663 | // Resolve the dynamic reference to a normal reference |
| 664 | // (with the right package ID). |
| 665 | out_value->dataType = Res_value::TYPE_REFERENCE; |
| 666 | out_value->data = entry.value.data; |
| 667 | const DynamicRefTable* ref_table = |
| 668 | asset_manager_->GetDynamicRefTableForPackage(package_idx); |
| 669 | if (ref_table == nullptr || ref_table->lookupResourceId(&out_value->data) != NO_ERROR) { |
| 670 | LOG(ERROR) << base::StringPrintf("Failed to resolve dynamic reference 0x%08x", |
| 671 | out_value->data); |
| 672 | return kInvalidCookie; |
| 673 | } |
| 674 | |
| 675 | if (out_flags != nullptr) { |
| 676 | *out_flags = type_spec_flags; |
| 677 | } |
| 678 | return entry.cookie; |
| 679 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 680 | |
| 681 | default: |
| 682 | *out_value = entry.value; |
| 683 | if (out_flags != nullptr) { |
| 684 | *out_flags = type_spec_flags; |
| 685 | } |
| 686 | return entry.cookie; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | LOG(WARNING) << base::StringPrintf("Too many (%d) attribute references, stopped at: 0x%08x", |
| 691 | kMaxIterations, resid); |
| 692 | return kInvalidCookie; |
| 693 | } |
| 694 | |
| 695 | void Theme::Clear() { |
| 696 | type_spec_flags_ = 0u; |
| 697 | for (std::unique_ptr<Package>& package : packages_) { |
| 698 | package.reset(); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | bool Theme::SetTo(const Theme& o) { |
| 703 | if (this == &o) { |
| 704 | return true; |
| 705 | } |
| 706 | |
| 707 | if (asset_manager_ != o.asset_manager_) { |
| 708 | return false; |
| 709 | } |
| 710 | |
| 711 | type_spec_flags_ = o.type_spec_flags_; |
| 712 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 713 | for (size_t p = 0; p < packages_.size(); p++) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 714 | const Package* package = o.packages_[p].get(); |
| 715 | if (package == nullptr) { |
| 716 | packages_[p].reset(); |
| 717 | continue; |
| 718 | } |
| 719 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 720 | for (size_t t = 0; t < package->types.size(); t++) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 721 | const Type* type = package->types[t].get(); |
| 722 | if (type == nullptr) { |
| 723 | packages_[p]->types[t].reset(); |
| 724 | continue; |
| 725 | } |
| 726 | |
| 727 | const size_t type_alloc_size = sizeof(Type) + (type->entry_capacity * sizeof(Entry)); |
| 728 | void* copied_data = malloc(type_alloc_size); |
| 729 | memcpy(copied_data, type, type_alloc_size); |
| 730 | packages_[p]->types[t].reset(reinterpret_cast<Type*>(copied_data)); |
| 731 | } |
| 732 | } |
| 733 | return true; |
| 734 | } |
| 735 | |
| 736 | } // namespace android |