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