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 | |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 22 | #include <iterator> |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 23 | #include <map> |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 24 | #include <set> |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 25 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 26 | #include "android-base/logging.h" |
| 27 | #include "android-base/stringprintf.h" |
Ryan Mitchell | fe50d73 | 2019-12-19 16:12:35 -0800 | [diff] [blame] | 28 | #include "androidfw/DynamicLibManager.h" |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 29 | #include "androidfw/ResourceUtils.h" |
Ryan Mitchell | 31b1105 | 2019-06-13 13:47:26 -0700 | [diff] [blame] | 30 | #include "androidfw/Util.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 31 | #include "utils/ByteOrder.h" |
| 32 | #include "utils/Trace.h" |
| 33 | |
| 34 | #ifdef _WIN32 |
| 35 | #ifdef ERROR |
| 36 | #undef ERROR |
| 37 | #endif |
| 38 | #endif |
| 39 | |
| 40 | namespace android { |
| 41 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 42 | struct FindEntryResult { |
| 43 | // A pointer to the resource table entry for this resource. |
| 44 | // If the size of the entry is > sizeof(ResTable_entry), it can be cast to |
| 45 | // a ResTable_map_entry and processed as a bag/map. |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 46 | ResTable_entry_handle entry; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 47 | |
| 48 | // The configuration for which the resulting entry was defined. This is already swapped to host |
| 49 | // endianness. |
| 50 | ResTable_config config; |
| 51 | |
| 52 | // The bitmask of configuration axis with which the resource value varies. |
| 53 | uint32_t type_flags; |
| 54 | |
| 55 | // The dynamic package ID map for the package from which this resource came from. |
| 56 | const DynamicRefTable* dynamic_ref_table; |
| 57 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 58 | // The package name of the resource. |
| 59 | const std::string* package_name; |
| 60 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 61 | // The string pool reference to the type's name. This uses a different string pool than |
| 62 | // the global string pool, but this is hidden from the caller. |
| 63 | StringPoolRef type_string_ref; |
| 64 | |
| 65 | // The string pool reference to the entry's name. This uses a different string pool than |
| 66 | // the global string pool, but this is hidden from the caller. |
| 67 | StringPoolRef entry_string_ref; |
| 68 | }; |
| 69 | |
Ryan Mitchell | fe50d73 | 2019-12-19 16:12:35 -0800 | [diff] [blame] | 70 | AssetManager2::AssetManager2() : dynamic_lib_manager_(std::make_unique<DynamicLibManager>()) { |
| 71 | memset(&configuration_, 0, sizeof(configuration_)); |
| 72 | } |
| 73 | |
| 74 | AssetManager2::AssetManager2(DynamicLibManager* dynamic_lib_manager) |
| 75 | : dynamic_lib_manager_(dynamic_lib_manager) { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 76 | memset(&configuration_, 0, sizeof(configuration_)); |
| 77 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 78 | |
| 79 | bool AssetManager2::SetApkAssets(const std::vector<const ApkAssets*>& apk_assets, |
Mårten Kongstad | 668ec5b | 2018-06-11 14:11:33 +0200 | [diff] [blame] | 80 | bool invalidate_caches, bool filter_incompatible_configs) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 81 | apk_assets_ = apk_assets; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 82 | BuildDynamicRefTable(); |
Mårten Kongstad | 668ec5b | 2018-06-11 14:11:33 +0200 | [diff] [blame] | 83 | RebuildFilterList(filter_incompatible_configs); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 84 | if (invalidate_caches) { |
| 85 | InvalidateCaches(static_cast<uint32_t>(-1)); |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 90 | void AssetManager2::BuildDynamicRefTable() { |
| 91 | package_groups_.clear(); |
| 92 | package_ids_.fill(0xff); |
| 93 | |
Ryan Mitchell | fe50d73 | 2019-12-19 16:12:35 -0800 | [diff] [blame] | 94 | // Overlay resources are not directly referenced by an application so their resource ids |
| 95 | // can change throughout the application's lifetime. Assign overlay package ids last. |
| 96 | std::vector<const ApkAssets*> sorted_apk_assets(apk_assets_); |
| 97 | std::stable_partition(sorted_apk_assets.begin(), sorted_apk_assets.end(), [](const ApkAssets* a) { |
| 98 | return !a->IsOverlay(); |
| 99 | }); |
| 100 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 101 | std::unordered_map<std::string, uint8_t> apk_assets_package_ids; |
Ryan Mitchell | fe50d73 | 2019-12-19 16:12:35 -0800 | [diff] [blame] | 102 | std::unordered_map<std::string, uint8_t> package_name_package_ids; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 103 | |
Ryan Mitchell | fe50d73 | 2019-12-19 16:12:35 -0800 | [diff] [blame] | 104 | // Assign stable package ids to application packages. |
| 105 | uint8_t next_available_package_id = 0U; |
| 106 | for (const auto& apk_assets : sorted_apk_assets) { |
| 107 | for (const auto& package : apk_assets->GetLoadedArsc()->GetPackages()) { |
| 108 | uint8_t package_id = package->GetPackageId(); |
| 109 | if (package->IsOverlay()) { |
| 110 | package_id = GetDynamicLibManager()->FindUnassignedId(next_available_package_id); |
| 111 | next_available_package_id = package_id + 1; |
| 112 | } else if (package->IsDynamic()) { |
| 113 | package_id = GetDynamicLibManager()->GetAssignedId(package->GetPackageName()); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Ryan Mitchell | fe50d73 | 2019-12-19 16:12:35 -0800 | [diff] [blame] | 116 | // Map the path of the apk assets to the package id of its first loaded package. |
| 117 | apk_assets_package_ids[apk_assets->GetPath()] = package_id; |
| 118 | |
| 119 | // Map the package name of the package to the first loaded package with that package id. |
| 120 | package_name_package_ids[package->GetPackageName()] = package_id; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | const int apk_assets_count = apk_assets_.size(); |
| 125 | for (int i = 0; i < apk_assets_count; i++) { |
| 126 | const auto& apk_assets = apk_assets_[i]; |
| 127 | for (const auto& package : apk_assets->GetLoadedArsc()->GetPackages()) { |
| 128 | const auto package_id_entry = package_name_package_ids.find(package->GetPackageName()); |
| 129 | CHECK(package_id_entry != package_name_package_ids.end()) |
| 130 | << "no package id assgined to package " << package->GetPackageName(); |
| 131 | const uint8_t package_id = package_id_entry->second; |
| 132 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 133 | // Add the mapping for package ID to index if not present. |
| 134 | uint8_t idx = package_ids_[package_id]; |
| 135 | if (idx == 0xff) { |
| 136 | package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size()); |
| 137 | package_groups_.push_back({}); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 138 | |
| 139 | if (apk_assets->IsOverlay()) { |
| 140 | // The target package must precede the overlay package in the apk assets paths in order |
| 141 | // to take effect. |
| 142 | const auto& loaded_idmap = apk_assets->GetLoadedIdmap(); |
| 143 | auto target_package_iter = apk_assets_package_ids.find(loaded_idmap->TargetApkPath()); |
Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 144 | if (target_package_iter == apk_assets_package_ids.end()) { |
| 145 | LOG(INFO) << "failed to find target package for overlay " |
| 146 | << loaded_idmap->OverlayApkPath(); |
| 147 | } else { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 148 | const uint8_t target_package_id = target_package_iter->second; |
| 149 | const uint8_t target_idx = package_ids_[target_package_id]; |
| 150 | CHECK(target_idx != 0xff) << "overlay added to apk_assets_package_ids but does not" |
| 151 | << " have an assigned package group"; |
| 152 | |
| 153 | PackageGroup& target_package_group = package_groups_[target_idx]; |
| 154 | |
Ryan Mitchell | fe50d73 | 2019-12-19 16:12:35 -0800 | [diff] [blame] | 155 | // Create a special dynamic reference table for the overlay to rewrite references to |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 156 | // overlay resources as references to the target resources they overlay. |
| 157 | auto overlay_table = std::make_shared<OverlayDynamicRefTable>( |
| 158 | loaded_idmap->GetOverlayDynamicRefTable(target_package_id)); |
| 159 | package_groups_.back().dynamic_ref_table = overlay_table; |
| 160 | |
| 161 | // Add the overlay resource map to the target package's set of overlays. |
| 162 | target_package_group.overlays_.push_back( |
| 163 | ConfiguredOverlay{loaded_idmap->GetTargetResourcesMap(target_package_id, |
| 164 | overlay_table.get()), |
| 165 | static_cast<ApkAssetsCookie>(i)}); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | DynamicRefTable* ref_table = package_groups_.back().dynamic_ref_table.get(); |
| 170 | ref_table->mAssignedPackageId = package_id; |
| 171 | ref_table->mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 172 | } |
| 173 | PackageGroup* package_group = &package_groups_[idx]; |
| 174 | |
| 175 | // Add the package and to the set of packages with the same ID. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 176 | package_group->packages_.push_back(ConfiguredPackage{package.get(), {}}); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 177 | package_group->cookies_.push_back(static_cast<ApkAssetsCookie>(i)); |
| 178 | |
| 179 | // Add the package name -> build time ID mappings. |
| 180 | for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) { |
| 181 | String16 package_name(entry.package_name.c_str(), entry.package_name.size()); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 182 | package_group->dynamic_ref_table->mEntries.replaceValueFor( |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 183 | package_name, static_cast<uint8_t>(entry.package_id)); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Now assign the runtime IDs so that we have a build-time to runtime ID map. |
| 189 | const auto package_groups_end = package_groups_.end(); |
| 190 | for (auto iter = package_groups_.begin(); iter != package_groups_end; ++iter) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 191 | const std::string& package_name = iter->packages_[0].loaded_package_->GetPackageName(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 192 | for (auto iter2 = package_groups_.begin(); iter2 != package_groups_end; ++iter2) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 193 | iter2->dynamic_ref_table->addMapping(String16(package_name.c_str(), package_name.size()), |
| 194 | iter->dynamic_ref_table->mAssignedPackageId); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void AssetManager2::DumpToLog() const { |
| 200 | base::ScopedLogSeverity _log(base::INFO); |
| 201 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 202 | LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this); |
| 203 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 204 | std::string list; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 205 | for (const auto& apk_assets : apk_assets_) { |
| 206 | base::StringAppendF(&list, "%s,", apk_assets->GetPath().c_str()); |
| 207 | } |
| 208 | LOG(INFO) << "ApkAssets: " << list; |
| 209 | |
| 210 | list = ""; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 211 | for (size_t i = 0; i < package_ids_.size(); i++) { |
| 212 | if (package_ids_[i] != 0xff) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 213 | base::StringAppendF(&list, "%02x -> %d, ", (int)i, package_ids_[i]); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | LOG(INFO) << "Package ID map: " << list; |
| 217 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 218 | for (const auto& package_group: package_groups_) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 219 | list = ""; |
| 220 | for (const auto& package : package_group.packages_) { |
| 221 | const LoadedPackage* loaded_package = package.loaded_package_; |
| 222 | base::StringAppendF(&list, "%s(%02x%s), ", loaded_package->GetPackageName().c_str(), |
| 223 | loaded_package->GetPackageId(), |
| 224 | (loaded_package->IsDynamic() ? " dynamic" : "")); |
| 225 | } |
| 226 | LOG(INFO) << base::StringPrintf("PG (%02x): ", |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 227 | package_group.dynamic_ref_table->mAssignedPackageId) |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 228 | << list; |
Ryan Mitchell | 5db396d | 2018-11-05 15:56:15 -0800 | [diff] [blame] | 229 | |
| 230 | for (size_t i = 0; i < 256; i++) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 231 | if (package_group.dynamic_ref_table->mLookupTable[i] != 0) { |
Ryan Mitchell | 5db396d | 2018-11-05 15:56:15 -0800 | [diff] [blame] | 232 | LOG(INFO) << base::StringPrintf(" e[0x%02x] -> 0x%02x", (uint8_t) i, |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 233 | package_group.dynamic_ref_table->mLookupTable[i]); |
Ryan Mitchell | 5db396d | 2018-11-05 15:56:15 -0800 | [diff] [blame] | 234 | } |
| 235 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 236 | } |
| 237 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 238 | |
| 239 | const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const { |
| 240 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 241 | return nullptr; |
| 242 | } |
| 243 | return apk_assets_[cookie]->GetLoadedArsc()->GetStringPool(); |
| 244 | } |
| 245 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 246 | const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const { |
| 247 | if (package_id >= package_ids_.size()) { |
| 248 | return nullptr; |
| 249 | } |
| 250 | |
| 251 | const size_t idx = package_ids_[package_id]; |
| 252 | if (idx == 0xff) { |
| 253 | return nullptr; |
| 254 | } |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 255 | return package_groups_[idx].dynamic_ref_table.get(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 256 | } |
| 257 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 258 | std::shared_ptr<const DynamicRefTable> AssetManager2::GetDynamicRefTableForCookie( |
| 259 | ApkAssetsCookie cookie) const { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 260 | for (const PackageGroup& package_group : package_groups_) { |
| 261 | for (const ApkAssetsCookie& package_cookie : package_group.cookies_) { |
| 262 | if (package_cookie == cookie) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 263 | return package_group.dynamic_ref_table; |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | return nullptr; |
| 268 | } |
| 269 | |
Mårten Kongstad | c92c4dd | 2019-02-05 01:29:59 +0100 | [diff] [blame] | 270 | const std::unordered_map<std::string, std::string>* |
| 271 | AssetManager2::GetOverlayableMapForPackage(uint32_t package_id) const { |
| 272 | |
| 273 | if (package_id >= package_ids_.size()) { |
| 274 | return nullptr; |
| 275 | } |
| 276 | |
| 277 | const size_t idx = package_ids_[package_id]; |
| 278 | if (idx == 0xff) { |
| 279 | return nullptr; |
| 280 | } |
| 281 | |
| 282 | const PackageGroup& package_group = package_groups_[idx]; |
| 283 | if (package_group.packages_.size() == 0) { |
| 284 | return nullptr; |
| 285 | } |
| 286 | |
| 287 | const auto loaded_package = package_group.packages_[0].loaded_package_; |
| 288 | return &loaded_package->GetOverlayableMap(); |
| 289 | } |
| 290 | |
Ryan Mitchell | 2e39422 | 2019-08-28 12:10:51 -0700 | [diff] [blame] | 291 | bool AssetManager2::GetOverlayablesToString(const android::StringPiece& package_name, |
| 292 | std::string* out) const { |
| 293 | uint8_t package_id = 0U; |
| 294 | for (const auto& apk_assets : apk_assets_) { |
| 295 | const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc(); |
| 296 | if (loaded_arsc == nullptr) { |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | const auto& loaded_packages = loaded_arsc->GetPackages(); |
| 301 | if (loaded_packages.empty()) { |
| 302 | continue; |
| 303 | } |
| 304 | |
| 305 | const auto& loaded_package = loaded_packages[0]; |
| 306 | if (loaded_package->GetPackageName() == package_name) { |
| 307 | package_id = GetAssignedPackageId(loaded_package.get()); |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if (package_id == 0U) { |
| 313 | ANDROID_LOG(ERROR) << base::StringPrintf("No package with name '%s", package_name.data()); |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | const size_t idx = package_ids_[package_id]; |
| 318 | if (idx == 0xff) { |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | std::string output; |
| 323 | for (const ConfiguredPackage& package : package_groups_[idx].packages_) { |
| 324 | const LoadedPackage* loaded_package = package.loaded_package_; |
| 325 | for (auto it = loaded_package->begin(); it != loaded_package->end(); it++) { |
| 326 | const OverlayableInfo* info = loaded_package->GetOverlayableInfo(*it); |
| 327 | if (info != nullptr) { |
| 328 | ResourceName res_name; |
| 329 | if (!GetResourceName(*it, &res_name)) { |
| 330 | ANDROID_LOG(ERROR) << base::StringPrintf( |
| 331 | "Unable to retrieve name of overlayable resource 0x%08x", *it); |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | const std::string name = ToFormattedResourceString(&res_name); |
| 336 | output.append(base::StringPrintf( |
| 337 | "resource='%s' overlayable='%s' actor='%s' policy='0x%08x'\n", |
| 338 | name.c_str(), info->name.c_str(), info->actor.c_str(), info->policy_flags)); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | *out = std::move(output); |
| 344 | return true; |
| 345 | } |
| 346 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 347 | void AssetManager2::SetConfiguration(const ResTable_config& configuration) { |
| 348 | const int diff = configuration_.diff(configuration); |
| 349 | configuration_ = configuration; |
| 350 | |
| 351 | if (diff) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 352 | RebuildFilterList(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 353 | InvalidateCaches(static_cast<uint32_t>(diff)); |
| 354 | } |
| 355 | } |
| 356 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 357 | std::set<std::string> AssetManager2::GetNonSystemOverlayPaths() const { |
| 358 | std::set<std::string> non_system_overlays; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 359 | for (const PackageGroup& package_group : package_groups_) { |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 360 | bool found_system_package = false; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 361 | for (const ConfiguredPackage& package : package_group.packages_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 362 | if (package.loaded_package_->IsSystem()) { |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 363 | found_system_package = true; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (!found_system_package) { |
| 369 | for (const ConfiguredOverlay& overlay : package_group.overlays_) { |
| 370 | non_system_overlays.insert(apk_assets_[overlay.cookie]->GetPath()); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return non_system_overlays; |
| 376 | } |
| 377 | |
| 378 | std::set<ResTable_config> AssetManager2::GetResourceConfigurations(bool exclude_system, |
| 379 | bool exclude_mipmap) const { |
| 380 | ATRACE_NAME("AssetManager::GetResourceConfigurations"); |
| 381 | const auto non_system_overlays = |
| 382 | (exclude_system) ? GetNonSystemOverlayPaths() : std::set<std::string>(); |
| 383 | |
| 384 | std::set<ResTable_config> configurations; |
| 385 | for (const PackageGroup& package_group : package_groups_) { |
| 386 | for (size_t i = 0; i < package_group.packages_.size(); i++) { |
| 387 | const ConfiguredPackage& package = package_group.packages_[i]; |
| 388 | if (exclude_system && package.loaded_package_->IsSystem()) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 389 | continue; |
| 390 | } |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 391 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 392 | auto apk_assets = apk_assets_[package_group.cookies_[i]]; |
| 393 | if (exclude_system && apk_assets->IsOverlay() |
| 394 | && non_system_overlays.find(apk_assets->GetPath()) == non_system_overlays.end()) { |
| 395 | // Exclude overlays that target system resources. |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 396 | continue; |
| 397 | } |
| 398 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 399 | package.loaded_package_->CollectConfigurations(exclude_mipmap, &configurations); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | return configurations; |
| 403 | } |
| 404 | |
| 405 | std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 406 | bool merge_equivalent_languages) const { |
| 407 | ATRACE_NAME("AssetManager::GetResourceLocales"); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 408 | std::set<std::string> locales; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 409 | const auto non_system_overlays = |
| 410 | (exclude_system) ? GetNonSystemOverlayPaths() : std::set<std::string>(); |
| 411 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 412 | for (const PackageGroup& package_group : package_groups_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 413 | for (size_t i = 0; i < package_group.packages_.size(); i++) { |
| 414 | const ConfiguredPackage& package = package_group.packages_[i]; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 415 | if (exclude_system && package.loaded_package_->IsSystem()) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 416 | continue; |
| 417 | } |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 418 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 419 | auto apk_assets = apk_assets_[package_group.cookies_[i]]; |
| 420 | if (exclude_system && apk_assets->IsOverlay() |
| 421 | && non_system_overlays.find(apk_assets->GetPath()) == non_system_overlays.end()) { |
| 422 | // Exclude overlays that target system resources. |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 423 | continue; |
| 424 | } |
| 425 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 426 | package.loaded_package_->CollectLocales(merge_equivalent_languages, &locales); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | return locales; |
| 430 | } |
| 431 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 432 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, |
| 433 | Asset::AccessMode mode) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 434 | const std::string new_path = "assets/" + filename; |
| 435 | return OpenNonAsset(new_path, mode); |
| 436 | } |
| 437 | |
| 438 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 439 | Asset::AccessMode mode) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 440 | const std::string new_path = "assets/" + filename; |
| 441 | return OpenNonAsset(new_path, cookie, mode); |
| 442 | } |
| 443 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 444 | std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) const { |
| 445 | ATRACE_NAME("AssetManager::OpenDir"); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 446 | |
| 447 | std::string full_path = "assets/" + dirname; |
| 448 | std::unique_ptr<SortedVector<AssetDir::FileInfo>> files = |
| 449 | util::make_unique<SortedVector<AssetDir::FileInfo>>(); |
| 450 | |
| 451 | // Start from the back. |
| 452 | for (auto iter = apk_assets_.rbegin(); iter != apk_assets_.rend(); ++iter) { |
| 453 | const ApkAssets* apk_assets = *iter; |
Mårten Kongstad | dbf343b | 2019-02-21 07:54:18 +0100 | [diff] [blame] | 454 | if (apk_assets->IsOverlay()) { |
| 455 | continue; |
| 456 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 457 | |
| 458 | auto func = [&](const StringPiece& name, FileType type) { |
| 459 | AssetDir::FileInfo info; |
| 460 | info.setFileName(String8(name.data(), name.size())); |
| 461 | info.setFileType(type); |
| 462 | info.setSourceName(String8(apk_assets->GetPath().c_str())); |
| 463 | files->add(info); |
| 464 | }; |
| 465 | |
| 466 | if (!apk_assets->ForEachFile(full_path, func)) { |
| 467 | return {}; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>(); |
| 472 | asset_dir->setFileList(files.release()); |
| 473 | return asset_dir; |
| 474 | } |
| 475 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 476 | // Search in reverse because that's how we used to do it and we need to preserve behaviour. |
| 477 | // This is unfortunate, because ClassLoaders delegate to the parent first, so the order |
| 478 | // is inconsistent for split APKs. |
| 479 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
| 480 | Asset::AccessMode mode, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 481 | ApkAssetsCookie* out_cookie) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 482 | for (int32_t i = apk_assets_.size() - 1; i >= 0; i--) { |
Mårten Kongstad | dbf343b | 2019-02-21 07:54:18 +0100 | [diff] [blame] | 483 | // Prevent RRO from modifying assets and other entries accessed by file |
| 484 | // path. Explicitly asking for a path in a given package (denoted by a |
| 485 | // cookie) is still OK. |
| 486 | if (apk_assets_[i]->IsOverlay()) { |
| 487 | continue; |
| 488 | } |
| 489 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 490 | std::unique_ptr<Asset> asset = apk_assets_[i]->Open(filename, mode); |
| 491 | if (asset) { |
| 492 | if (out_cookie != nullptr) { |
| 493 | *out_cookie = i; |
| 494 | } |
| 495 | return asset; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | if (out_cookie != nullptr) { |
| 500 | *out_cookie = kInvalidCookie; |
| 501 | } |
| 502 | return {}; |
| 503 | } |
| 504 | |
| 505 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 506 | ApkAssetsCookie cookie, |
| 507 | Asset::AccessMode mode) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 508 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 509 | return {}; |
| 510 | } |
| 511 | return apk_assets_[cookie]->Open(filename, mode); |
| 512 | } |
| 513 | |
| 514 | ApkAssetsCookie AssetManager2::FindEntry(uint32_t resid, uint16_t density_override, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 515 | bool /*stop_at_first_match*/, |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 516 | bool ignore_configuration, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 517 | FindEntryResult* out_entry) const { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 518 | if (resource_resolution_logging_enabled_) { |
| 519 | // Clear the last logged resource resolution. |
| 520 | ResetResourceResolution(); |
| 521 | last_resolution_.resid = resid; |
| 522 | } |
| 523 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 524 | // Might use this if density_override != 0. |
| 525 | ResTable_config density_override_config; |
| 526 | |
| 527 | // Select our configuration or generate a density override configuration. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 528 | const ResTable_config* desired_config = &configuration_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 529 | if (density_override != 0 && density_override != configuration_.density) { |
| 530 | density_override_config = configuration_; |
| 531 | density_override_config.density = density_override; |
| 532 | desired_config = &density_override_config; |
| 533 | } |
| 534 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 535 | // Retrieve the package group from the package id of the resource id. |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 536 | if (!is_valid_resid(resid)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 537 | LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid); |
| 538 | return kInvalidCookie; |
| 539 | } |
| 540 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 541 | const uint32_t package_id = get_package_id(resid); |
| 542 | const uint8_t type_idx = get_type_id(resid) - 1; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 543 | const uint16_t entry_idx = get_entry_id(resid); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 544 | uint8_t package_idx = package_ids_[package_id]; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 545 | if (package_idx == 0xff) { |
Ryan Mitchell | 2fe2347 | 2019-02-27 09:43:01 -0800 | [diff] [blame] | 546 | ANDROID_LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.", |
| 547 | package_id, resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 548 | return kInvalidCookie; |
| 549 | } |
| 550 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 551 | const PackageGroup& package_group = package_groups_[package_idx]; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 552 | ApkAssetsCookie cookie = FindEntryInternal(package_group, type_idx, entry_idx, *desired_config, |
| 553 | false /* stop_at_first_match */, |
| 554 | ignore_configuration, out_entry); |
| 555 | if (UNLIKELY(cookie == kInvalidCookie)) { |
| 556 | return kInvalidCookie; |
| 557 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 558 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 559 | if (!apk_assets_[cookie]->IsLoader()) { |
| 560 | for (const auto& id_map : package_group.overlays_) { |
| 561 | auto overlay_entry = id_map.overlay_res_maps_.Lookup(resid); |
| 562 | if (!overlay_entry) { |
| 563 | // No id map entry exists for this target resource. |
| 564 | continue; |
| 565 | } |
| 566 | |
| 567 | if (overlay_entry.IsTableEntry()) { |
| 568 | // The target resource is overlaid by an inline value not represented by a resource. |
| 569 | out_entry->entry = overlay_entry.GetTableEntry(); |
| 570 | out_entry->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable(); |
| 571 | cookie = id_map.cookie; |
| 572 | continue; |
| 573 | } |
| 574 | |
| 575 | FindEntryResult overlay_result; |
| 576 | ApkAssetsCookie overlay_cookie = FindEntry(overlay_entry.GetResourceId(), density_override, |
| 577 | false /* stop_at_first_match */, |
| 578 | ignore_configuration, &overlay_result); |
| 579 | if (UNLIKELY(overlay_cookie == kInvalidCookie)) { |
| 580 | continue; |
| 581 | } |
| 582 | |
| 583 | if (!overlay_result.config.isBetterThan(out_entry->config, desired_config) |
| 584 | && overlay_result.config.compare(out_entry->config) != 0) { |
| 585 | // The configuration of the entry for the overlay must be equal to or better than the target |
| 586 | // configuration to be chosen as the better value. |
| 587 | continue; |
| 588 | } |
| 589 | |
| 590 | cookie = overlay_cookie; |
| 591 | out_entry->entry = std::move(overlay_result.entry); |
| 592 | out_entry->config = overlay_result.config; |
| 593 | out_entry->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable(); |
| 594 | if (resource_resolution_logging_enabled_) { |
| 595 | last_resolution_.steps.push_back( |
| 596 | Resolution::Step{Resolution::Step::Type::OVERLAID, overlay_result.config.toString(), |
Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 597 | overlay_result.package_name}); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | if (resource_resolution_logging_enabled_) { |
| 603 | last_resolution_.cookie = cookie; |
| 604 | last_resolution_.type_string_ref = out_entry->type_string_ref; |
| 605 | last_resolution_.entry_string_ref = out_entry->entry_string_ref; |
| 606 | } |
| 607 | |
| 608 | return cookie; |
| 609 | } |
| 610 | |
| 611 | ApkAssetsCookie AssetManager2::FindEntryInternal(const PackageGroup& package_group, |
| 612 | uint8_t type_idx, uint16_t entry_idx, |
| 613 | const ResTable_config& desired_config, |
| 614 | bool /*stop_at_first_match*/, |
| 615 | bool ignore_configuration, |
| 616 | FindEntryResult* out_entry) const { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 617 | ApkAssetsCookie best_cookie = kInvalidCookie; |
| 618 | const LoadedPackage* best_package = nullptr; |
| 619 | const ResTable_type* best_type = nullptr; |
| 620 | const ResTable_config* best_config = nullptr; |
| 621 | ResTable_config best_config_copy; |
| 622 | uint32_t best_offset = 0u; |
| 623 | uint32_t type_flags = 0u; |
| 624 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 625 | Resolution::Step::Type resolution_type = Resolution::Step::Type::NO_ENTRY; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 626 | std::vector<Resolution::Step> resolution_steps; |
| 627 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 628 | // If desired_config is the same as the set configuration, then we can use our filtered list |
| 629 | // and we don't need to match the configurations, since they already matched. |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 630 | const bool use_fast_path = !ignore_configuration && &desired_config == &configuration_; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 631 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 632 | const size_t package_count = package_group.packages_.size(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 633 | for (size_t pi = 0; pi < package_count; pi++) { |
| 634 | const ConfiguredPackage& loaded_package_impl = package_group.packages_[pi]; |
| 635 | const LoadedPackage* loaded_package = loaded_package_impl.loaded_package_; |
| 636 | ApkAssetsCookie cookie = package_group.cookies_[pi]; |
| 637 | |
| 638 | // If the type IDs are offset in this package, we need to take that into account when searching |
| 639 | // for a type. |
| 640 | const TypeSpec* type_spec = loaded_package->GetTypeSpecByTypeIndex(type_idx); |
| 641 | if (UNLIKELY(type_spec == nullptr)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 642 | continue; |
| 643 | } |
| 644 | |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 645 | // If the package is an overlay or custom loader, |
| 646 | // then even configurations that are the same MUST be chosen. |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 647 | const bool package_is_loader = loaded_package->IsCustomLoader(); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 648 | type_flags |= type_spec->GetFlagsForEntryIndex(entry_idx); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 649 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 650 | if (use_fast_path) { |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 651 | const FilteredConfigGroup& filtered_group = loaded_package_impl.filtered_configs_[type_idx]; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 652 | const std::vector<ResTable_config>& candidate_configs = filtered_group.configurations; |
| 653 | const size_t type_count = candidate_configs.size(); |
| 654 | for (uint32_t i = 0; i < type_count; i++) { |
| 655 | const ResTable_config& this_config = candidate_configs[i]; |
| 656 | |
| 657 | // We can skip calling ResTable_config::match() because we know that all candidate |
| 658 | // configurations that do NOT match have been filtered-out. |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 659 | if (best_config == nullptr) { |
| 660 | resolution_type = Resolution::Step::Type::INITIAL; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 661 | } else if (this_config.isBetterThan(*best_config, &desired_config)) { |
| 662 | resolution_type = (package_is_loader) ? Resolution::Step::Type::BETTER_MATCH_LOADER |
| 663 | : Resolution::Step::Type::BETTER_MATCH; |
| 664 | } else if (package_is_loader && this_config.compare(*best_config) == 0) { |
| 665 | resolution_type = Resolution::Step::Type::OVERLAID_LOADER; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 666 | } else { |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 667 | if (resource_resolution_logging_enabled_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 668 | resolution_type = (package_is_loader) ? Resolution::Step::Type::SKIPPED_LOADER |
| 669 | : Resolution::Step::Type::SKIPPED; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 670 | resolution_steps.push_back(Resolution::Step{resolution_type, |
| 671 | this_config.toString(), |
| 672 | &loaded_package->GetPackageName()}); |
| 673 | } |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 674 | continue; |
| 675 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 676 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 677 | // The configuration matches and is better than the previous selection. |
| 678 | // Find the entry value if it exists for this configuration. |
| 679 | const ResTable_type* type = filtered_group.types[i]; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 680 | const uint32_t offset = LoadedPackage::GetEntryOffset(type, entry_idx); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 681 | if (offset == ResTable_type::NO_ENTRY) { |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 682 | if (resource_resolution_logging_enabled_) { |
| 683 | if (package_is_loader) { |
| 684 | resolution_type = Resolution::Step::Type::NO_ENTRY_LOADER; |
| 685 | } else { |
| 686 | resolution_type = Resolution::Step::Type::NO_ENTRY; |
| 687 | } |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 688 | resolution_steps.push_back(Resolution::Step{resolution_type, |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 689 | this_config.toString(), |
| 690 | &loaded_package->GetPackageName()}); |
| 691 | } |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 692 | continue; |
| 693 | } |
| 694 | |
| 695 | best_cookie = cookie; |
| 696 | best_package = loaded_package; |
| 697 | best_type = type; |
| 698 | best_config = &this_config; |
| 699 | best_offset = offset; |
| 700 | |
| 701 | if (resource_resolution_logging_enabled_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 702 | last_resolution_.steps.push_back(Resolution::Step{resolution_type, |
| 703 | this_config.toString(), |
| 704 | &loaded_package->GetPackageName()}); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | } else { |
| 708 | // This is the slower path, which doesn't use the filtered list of configurations. |
| 709 | // Here we must read the ResTable_config from the mmapped APK, convert it to host endianness |
| 710 | // and fill in any new fields that did not exist when the APK was compiled. |
| 711 | // Furthermore when selecting configurations we can't just record the pointer to the |
| 712 | // ResTable_config, we must copy it. |
| 713 | const auto iter_end = type_spec->types + type_spec->type_count; |
| 714 | for (auto iter = type_spec->types; iter != iter_end; ++iter) { |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 715 | ResTable_config this_config{}; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 716 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 717 | if (!ignore_configuration) { |
| 718 | this_config.copyFromDtoH((*iter)->config); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 719 | if (!this_config.match(desired_config)) { |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 720 | continue; |
| 721 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 722 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 723 | if (best_config == nullptr) { |
| 724 | resolution_type = Resolution::Step::Type::INITIAL; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 725 | } else if (this_config.isBetterThan(*best_config, &desired_config)) { |
| 726 | resolution_type = (package_is_loader) ? Resolution::Step::Type::BETTER_MATCH_LOADER |
| 727 | : Resolution::Step::Type::BETTER_MATCH; |
| 728 | } else if (package_is_loader && this_config.compare(*best_config) == 0) { |
| 729 | resolution_type = Resolution::Step::Type::OVERLAID_LOADER; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 730 | } else { |
| 731 | continue; |
| 732 | } |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | // The configuration matches and is better than the previous selection. |
| 736 | // Find the entry value if it exists for this configuration. |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 737 | const uint32_t offset = LoadedPackage::GetEntryOffset(*iter, entry_idx); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 738 | if (offset == ResTable_type::NO_ENTRY) { |
| 739 | continue; |
| 740 | } |
| 741 | |
| 742 | best_cookie = cookie; |
| 743 | best_package = loaded_package; |
| 744 | best_type = *iter; |
| 745 | best_config_copy = this_config; |
| 746 | best_config = &best_config_copy; |
| 747 | best_offset = offset; |
| 748 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 749 | if (ignore_configuration) { |
| 750 | // Any configuration will suffice, so break. |
| 751 | break; |
| 752 | } |
| 753 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 754 | if (resource_resolution_logging_enabled_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 755 | last_resolution_.steps.push_back(Resolution::Step{resolution_type, |
| 756 | this_config.toString(), |
| 757 | &loaded_package->GetPackageName()}); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 758 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 763 | if (UNLIKELY(best_cookie == kInvalidCookie)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 764 | return kInvalidCookie; |
| 765 | } |
| 766 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 767 | const ResTable_entry* best_entry = LoadedPackage::GetEntryFromOffset(best_type, best_offset); |
| 768 | if (UNLIKELY(best_entry == nullptr)) { |
| 769 | return kInvalidCookie; |
| 770 | } |
| 771 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 772 | out_entry->entry = ResTable_entry_handle::unmanaged(best_entry); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 773 | out_entry->config = *best_config; |
| 774 | out_entry->type_flags = type_flags; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 775 | out_entry->package_name = &best_package->GetPackageName(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 776 | out_entry->type_string_ref = StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1); |
| 777 | out_entry->entry_string_ref = |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 778 | StringPoolRef(best_package->GetKeyStringPool(), best_entry->key.index); |
| 779 | out_entry->dynamic_ref_table = package_group.dynamic_ref_table.get(); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 780 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 781 | return best_cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 784 | void AssetManager2::ResetResourceResolution() const { |
| 785 | last_resolution_.cookie = kInvalidCookie; |
| 786 | last_resolution_.resid = 0; |
| 787 | last_resolution_.steps.clear(); |
| 788 | last_resolution_.type_string_ref = StringPoolRef(); |
| 789 | last_resolution_.entry_string_ref = StringPoolRef(); |
| 790 | } |
| 791 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 792 | void AssetManager2::SetResourceResolutionLoggingEnabled(bool enabled) { |
| 793 | resource_resolution_logging_enabled_ = enabled; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 794 | if (!enabled) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 795 | ResetResourceResolution(); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
| 799 | std::string AssetManager2::GetLastResourceResolution() const { |
| 800 | if (!resource_resolution_logging_enabled_) { |
| 801 | LOG(ERROR) << "Must enable resource resolution logging before getting path."; |
| 802 | return std::string(); |
| 803 | } |
| 804 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 805 | auto cookie = last_resolution_.cookie; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 806 | if (cookie == kInvalidCookie) { |
| 807 | LOG(ERROR) << "AssetManager hasn't resolved a resource to read resolution path."; |
| 808 | return std::string(); |
| 809 | } |
| 810 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 811 | uint32_t resid = last_resolution_.resid; |
| 812 | std::vector<Resolution::Step>& steps = last_resolution_.steps; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 813 | |
| 814 | ResourceName resource_name; |
| 815 | std::string resource_name_string; |
| 816 | |
| 817 | const LoadedPackage* package = |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 818 | apk_assets_[cookie]->GetLoadedArsc()->GetPackageById(get_package_id(resid)); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 819 | |
| 820 | if (package != nullptr) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 821 | ToResourceName(last_resolution_.type_string_ref, |
| 822 | last_resolution_.entry_string_ref, |
Ryan Mitchell | 741e96f | 2019-01-23 16:56:51 -0800 | [diff] [blame] | 823 | package->GetPackageName(), |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 824 | &resource_name); |
| 825 | resource_name_string = ToFormattedResourceString(&resource_name); |
| 826 | } |
| 827 | |
| 828 | std::stringstream log_stream; |
| 829 | log_stream << base::StringPrintf("Resolution for 0x%08x ", resid) |
| 830 | << resource_name_string |
| 831 | << "\n\tFor config -" |
| 832 | << configuration_.toString(); |
| 833 | |
| 834 | std::string prefix; |
| 835 | for (Resolution::Step step : steps) { |
| 836 | switch (step.type) { |
| 837 | case Resolution::Step::Type::INITIAL: |
| 838 | prefix = "Found initial"; |
| 839 | break; |
| 840 | case Resolution::Step::Type::BETTER_MATCH: |
| 841 | prefix = "Found better"; |
| 842 | break; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 843 | case Resolution::Step::Type::BETTER_MATCH_LOADER: |
| 844 | prefix = "Found better in loader"; |
| 845 | break; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 846 | case Resolution::Step::Type::OVERLAID: |
| 847 | prefix = "Overlaid"; |
| 848 | break; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 849 | case Resolution::Step::Type::OVERLAID_LOADER: |
| 850 | prefix = "Overlaid by loader"; |
| 851 | break; |
| 852 | case Resolution::Step::Type::SKIPPED: |
| 853 | prefix = "Skipped"; |
| 854 | break; |
| 855 | case Resolution::Step::Type::SKIPPED_LOADER: |
| 856 | prefix = "Skipped loader"; |
| 857 | break; |
| 858 | case Resolution::Step::Type::NO_ENTRY: |
| 859 | prefix = "No entry"; |
| 860 | break; |
| 861 | case Resolution::Step::Type::NO_ENTRY_LOADER: |
| 862 | prefix = "No entry for loader"; |
| 863 | break; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | if (!prefix.empty()) { |
| 867 | log_stream << "\n\t" << prefix << ": " << *step.package_name; |
| 868 | |
| 869 | if (!step.config_name.isEmpty()) { |
| 870 | log_stream << " -" << step.config_name; |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | return log_stream.str(); |
| 876 | } |
| 877 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 878 | bool AssetManager2::GetResourceName(uint32_t resid, ResourceName* out_name) const { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 879 | FindEntryResult entry; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 880 | ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */, |
| 881 | true /* stop_at_first_match */, |
| 882 | true /* ignore_configuration */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 883 | if (cookie == kInvalidCookie) { |
| 884 | return false; |
| 885 | } |
| 886 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 887 | return ToResourceName(entry.type_string_ref, |
| 888 | entry.entry_string_ref, |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 889 | *entry.package_name, |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 890 | out_name); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 893 | bool AssetManager2::GetResourceFlags(uint32_t resid, uint32_t* out_flags) const { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 894 | FindEntryResult entry; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 895 | ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */, |
| 896 | false /* stop_at_first_match */, |
| 897 | true /* ignore_configuration */, &entry); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 898 | if (cookie != kInvalidCookie) { |
| 899 | *out_flags = entry.type_flags; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 900 | return true; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 901 | } |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 902 | return false; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | ApkAssetsCookie AssetManager2::GetResource(uint32_t resid, bool may_be_bag, |
| 906 | uint16_t density_override, Res_value* out_value, |
| 907 | ResTable_config* out_selected_config, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 908 | uint32_t* out_flags) const { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 909 | FindEntryResult entry; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 910 | ApkAssetsCookie cookie = FindEntry(resid, density_override, false /* stop_at_first_match */, |
| 911 | false /* ignore_configuration */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 912 | if (cookie == kInvalidCookie) { |
| 913 | return kInvalidCookie; |
| 914 | } |
| 915 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 916 | const ResTable_entry* table_entry = *entry.entry; |
| 917 | if (dtohs(table_entry->flags) & ResTable_entry::FLAG_COMPLEX) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 918 | if (!may_be_bag) { |
| 919 | LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 920 | return kInvalidCookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 921 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 922 | |
| 923 | // Create a reference since we can't represent this complex type as a Res_value. |
| 924 | out_value->dataType = Res_value::TYPE_REFERENCE; |
| 925 | out_value->data = resid; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 926 | *out_selected_config = entry.config; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 927 | *out_flags = entry.type_flags; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 928 | return cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | const Res_value* device_value = reinterpret_cast<const Res_value*>( |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 932 | reinterpret_cast<const uint8_t*>(table_entry) + dtohs(table_entry->size)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 933 | out_value->copyFrom_dtoh(*device_value); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 934 | |
| 935 | // Convert the package ID to the runtime assigned package ID. |
| 936 | entry.dynamic_ref_table->lookupResourceValue(out_value); |
| 937 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 938 | *out_selected_config = entry.config; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 939 | *out_flags = entry.type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 940 | return cookie; |
| 941 | } |
| 942 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 943 | ApkAssetsCookie AssetManager2::ResolveReference(ApkAssetsCookie cookie, Res_value* in_out_value, |
| 944 | ResTable_config* in_out_selected_config, |
| 945 | uint32_t* in_out_flags, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 946 | uint32_t* out_last_reference) const { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 947 | constexpr const int kMaxIterations = 20; |
| 948 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 949 | for (size_t iteration = 0u; in_out_value->dataType == Res_value::TYPE_REFERENCE && |
| 950 | in_out_value->data != 0u && iteration < kMaxIterations; |
| 951 | iteration++) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 952 | *out_last_reference = in_out_value->data; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 953 | uint32_t new_flags = 0u; |
| 954 | cookie = GetResource(in_out_value->data, true /*may_be_bag*/, 0u /*density_override*/, |
| 955 | in_out_value, in_out_selected_config, &new_flags); |
| 956 | if (cookie == kInvalidCookie) { |
| 957 | return kInvalidCookie; |
| 958 | } |
| 959 | if (in_out_flags != nullptr) { |
| 960 | *in_out_flags |= new_flags; |
| 961 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 962 | if (*out_last_reference == in_out_value->data) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 963 | // This reference can't be resolved, so exit now and let the caller deal with it. |
| 964 | return cookie; |
| 965 | } |
| 966 | } |
| 967 | return cookie; |
| 968 | } |
| 969 | |
Aurimas Liutikas | 8f004c8 | 2019-01-17 17:20:10 -0800 | [diff] [blame] | 970 | const std::vector<uint32_t> AssetManager2::GetBagResIdStack(uint32_t resid) { |
| 971 | auto cached_iter = cached_bag_resid_stacks_.find(resid); |
| 972 | if (cached_iter != cached_bag_resid_stacks_.end()) { |
| 973 | return cached_iter->second; |
| 974 | } else { |
| 975 | auto found_resids = std::vector<uint32_t>(); |
| 976 | GetBag(resid, found_resids); |
| 977 | // Cache style stacks if they are not already cached. |
| 978 | cached_bag_resid_stacks_[resid] = found_resids; |
| 979 | return found_resids; |
| 980 | } |
| 981 | } |
| 982 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 983 | const ResolvedBag* AssetManager2::GetBag(uint32_t resid) { |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 984 | auto found_resids = std::vector<uint32_t>(); |
Aurimas Liutikas | 8f004c8 | 2019-01-17 17:20:10 -0800 | [diff] [blame] | 985 | auto bag = GetBag(resid, found_resids); |
| 986 | |
| 987 | // Cache style stacks if they are not already cached. |
| 988 | auto cached_iter = cached_bag_resid_stacks_.find(resid); |
| 989 | if (cached_iter == cached_bag_resid_stacks_.end()) { |
| 990 | cached_bag_resid_stacks_[resid] = found_resids; |
| 991 | } |
| 992 | return bag; |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame^] | 995 | static bool compare_bag_entries(const ResolvedBag::Entry& entry1, |
| 996 | const ResolvedBag::Entry& entry2) { |
| 997 | return entry1.key < entry2.key; |
| 998 | } |
| 999 | |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 1000 | const ResolvedBag* AssetManager2::GetBag(uint32_t resid, std::vector<uint32_t>& child_resids) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1001 | auto cached_iter = cached_bags_.find(resid); |
| 1002 | if (cached_iter != cached_bags_.end()) { |
| 1003 | return cached_iter->second.get(); |
| 1004 | } |
| 1005 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 1006 | FindEntryResult entry; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 1007 | ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */, |
| 1008 | false /* stop_at_first_match */, |
| 1009 | false /* ignore_configuration */, |
| 1010 | &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1011 | if (cookie == kInvalidCookie) { |
| 1012 | return nullptr; |
| 1013 | } |
| 1014 | |
| 1015 | // Check that the size of the entry header is at least as big as |
| 1016 | // the desired ResTable_map_entry. Also verify that the entry |
| 1017 | // was intended to be a map. |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 1018 | const ResTable_entry* table_entry = *entry.entry; |
| 1019 | if (dtohs(table_entry->size) < sizeof(ResTable_map_entry) || |
| 1020 | (dtohs(table_entry->flags) & ResTable_entry::FLAG_COMPLEX) == 0) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1021 | // Not a bag, nothing to do. |
| 1022 | return nullptr; |
| 1023 | } |
| 1024 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 1025 | const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(table_entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1026 | const ResTable_map* map_entry = |
| 1027 | reinterpret_cast<const ResTable_map*>(reinterpret_cast<const uint8_t*>(map) + map->size); |
| 1028 | const ResTable_map* const map_entry_end = map_entry + dtohl(map->count); |
| 1029 | |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 1030 | // Keep track of ids that have already been seen to prevent infinite loops caused by circular |
| 1031 | // dependencies between bags |
| 1032 | child_resids.push_back(resid); |
| 1033 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1034 | uint32_t parent_resid = dtohl(map->parent.ident); |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame^] | 1035 | if (parent_resid == 0U || std::find(child_resids.begin(), child_resids.end(), parent_resid) |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 1036 | != child_resids.end()) { |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame^] | 1037 | // There is no parent or a circular dependency exist, meaning there is nothing to inherit and |
| 1038 | // we can do a simple copy of the entries in the map. |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1039 | const size_t entry_count = map_entry_end - map_entry; |
| 1040 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 1041 | malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))}; |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame^] | 1042 | |
| 1043 | bool sort_entries = false; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1044 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 1045 | for (; map_entry != map_entry_end; ++map_entry) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1046 | uint32_t new_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1047 | if (!is_internal_resid(new_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1048 | // Attributes, arrays, etc don't have a resource id as the name. They specify |
| 1049 | // other data, which would be wrong to change via a lookup. |
| 1050 | if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1051 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, |
| 1052 | resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1053 | return nullptr; |
| 1054 | } |
| 1055 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1056 | new_entry->cookie = cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1057 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1058 | new_entry->key_pool = nullptr; |
| 1059 | new_entry->type_pool = nullptr; |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 1060 | new_entry->style = resid; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1061 | new_entry->value.copyFrom_dtoh(map_entry->value); |
| 1062 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 1063 | if (err != NO_ERROR) { |
| 1064 | LOG(ERROR) << base::StringPrintf( |
| 1065 | "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType, |
| 1066 | new_entry->value.data, new_key); |
| 1067 | return nullptr; |
| 1068 | } |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame^] | 1069 | sort_entries = sort_entries || |
| 1070 | (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1071 | ++new_entry; |
| 1072 | } |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame^] | 1073 | |
| 1074 | if (sort_entries) { |
| 1075 | std::sort(new_bag->entries, new_bag->entries + entry_count, compare_bag_entries); |
| 1076 | } |
| 1077 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 1078 | new_bag->type_spec_flags = entry.type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1079 | new_bag->entry_count = static_cast<uint32_t>(entry_count); |
| 1080 | ResolvedBag* result = new_bag.get(); |
| 1081 | cached_bags_[resid] = std::move(new_bag); |
| 1082 | return result; |
| 1083 | } |
| 1084 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1085 | // In case the parent is a dynamic reference, resolve it. |
| 1086 | entry.dynamic_ref_table->lookupResourceId(&parent_resid); |
| 1087 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1088 | // Get the parent and do a merge of the keys. |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 1089 | const ResolvedBag* parent_bag = GetBag(parent_resid, child_resids); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1090 | if (parent_bag == nullptr) { |
| 1091 | // Failed to get the parent that should exist. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1092 | LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid, |
| 1093 | resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1094 | return nullptr; |
| 1095 | } |
| 1096 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1097 | // Create the max possible entries we can make. Once we construct the bag, |
| 1098 | // we will realloc to fit to size. |
| 1099 | 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] | 1100 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 1101 | malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1102 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 1103 | |
| 1104 | const ResolvedBag::Entry* parent_entry = parent_bag->entries; |
| 1105 | const ResolvedBag::Entry* const parent_entry_end = parent_entry + parent_bag->entry_count; |
| 1106 | |
| 1107 | // The keys are expected to be in sorted order. Merge the two bags. |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame^] | 1108 | bool sort_entries = false; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1109 | while (map_entry != map_entry_end && parent_entry != parent_entry_end) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1110 | uint32_t child_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1111 | if (!is_internal_resid(child_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1112 | if (entry.dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1113 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key, |
| 1114 | resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1115 | return nullptr; |
| 1116 | } |
| 1117 | } |
| 1118 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1119 | if (child_key <= parent_entry->key) { |
| 1120 | // Use the child key if it comes before the parent |
| 1121 | // or is equal to the parent (overrides). |
| 1122 | new_entry->cookie = cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1123 | new_entry->key = child_key; |
| 1124 | new_entry->key_pool = nullptr; |
| 1125 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1126 | new_entry->value.copyFrom_dtoh(map_entry->value); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 1127 | new_entry->style = resid; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1128 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 1129 | if (err != NO_ERROR) { |
| 1130 | LOG(ERROR) << base::StringPrintf( |
| 1131 | "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType, |
| 1132 | new_entry->value.data, child_key); |
| 1133 | return nullptr; |
| 1134 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1135 | ++map_entry; |
| 1136 | } else { |
| 1137 | // Take the parent entry as-is. |
| 1138 | memcpy(new_entry, parent_entry, sizeof(*new_entry)); |
| 1139 | } |
| 1140 | |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame^] | 1141 | sort_entries = sort_entries || |
| 1142 | (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1143 | if (child_key >= parent_entry->key) { |
| 1144 | // Move to the next parent entry if we used it or it was overridden. |
| 1145 | ++parent_entry; |
| 1146 | } |
| 1147 | // Increment to the next entry to fill. |
| 1148 | ++new_entry; |
| 1149 | } |
| 1150 | |
| 1151 | // Finish the child entries if they exist. |
| 1152 | while (map_entry != map_entry_end) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1153 | uint32_t new_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1154 | if (!is_internal_resid(new_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1155 | if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1156 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, |
| 1157 | resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1158 | return nullptr; |
| 1159 | } |
| 1160 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1161 | new_entry->cookie = cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1162 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1163 | new_entry->key_pool = nullptr; |
| 1164 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1165 | new_entry->value.copyFrom_dtoh(map_entry->value); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 1166 | new_entry->style = resid; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1167 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 1168 | if (err != NO_ERROR) { |
| 1169 | LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", |
| 1170 | new_entry->value.dataType, new_entry->value.data, new_key); |
| 1171 | return nullptr; |
| 1172 | } |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame^] | 1173 | sort_entries = sort_entries || |
| 1174 | (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1175 | ++map_entry; |
| 1176 | ++new_entry; |
| 1177 | } |
| 1178 | |
| 1179 | // Finish the parent entries if they exist. |
| 1180 | if (parent_entry != parent_entry_end) { |
| 1181 | // Take the rest of the parent entries as-is. |
| 1182 | const size_t num_entries_to_copy = parent_entry_end - parent_entry; |
| 1183 | memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry)); |
| 1184 | new_entry += num_entries_to_copy; |
| 1185 | } |
| 1186 | |
| 1187 | // Resize the resulting array to fit. |
| 1188 | const size_t actual_count = new_entry - new_bag->entries; |
| 1189 | if (actual_count != max_count) { |
George Burgess IV | 09b119f | 2017-07-25 15:00:04 -0700 | [diff] [blame] | 1190 | new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc( |
| 1191 | new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry))))); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame^] | 1194 | if (sort_entries) { |
| 1195 | std::sort(new_bag->entries, new_bag->entries + actual_count, compare_bag_entries); |
| 1196 | } |
| 1197 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 1198 | // Combine flags from the parent and our own bag. |
| 1199 | 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] | 1200 | new_bag->entry_count = static_cast<uint32_t>(actual_count); |
| 1201 | ResolvedBag* result = new_bag.get(); |
| 1202 | cached_bags_[resid] = std::move(new_bag); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1203 | return result; |
| 1204 | } |
| 1205 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1206 | static bool Utf8ToUtf16(const StringPiece& str, std::u16string* out) { |
| 1207 | ssize_t len = |
| 1208 | utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false); |
| 1209 | if (len < 0) { |
| 1210 | return false; |
| 1211 | } |
| 1212 | out->resize(static_cast<size_t>(len)); |
| 1213 | utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(), |
| 1214 | static_cast<size_t>(len + 1)); |
| 1215 | return true; |
| 1216 | } |
| 1217 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 1218 | uint32_t AssetManager2::GetResourceId(const std::string& resource_name, |
| 1219 | const std::string& fallback_type, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1220 | const std::string& fallback_package) const { |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1221 | StringPiece package_name, type, entry; |
| 1222 | if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) { |
| 1223 | return 0u; |
| 1224 | } |
| 1225 | |
| 1226 | if (entry.empty()) { |
| 1227 | return 0u; |
| 1228 | } |
| 1229 | |
| 1230 | if (package_name.empty()) { |
| 1231 | package_name = fallback_package; |
| 1232 | } |
| 1233 | |
| 1234 | if (type.empty()) { |
| 1235 | type = fallback_type; |
| 1236 | } |
| 1237 | |
| 1238 | std::u16string type16; |
| 1239 | if (!Utf8ToUtf16(type, &type16)) { |
| 1240 | return 0u; |
| 1241 | } |
| 1242 | |
| 1243 | std::u16string entry16; |
| 1244 | if (!Utf8ToUtf16(entry, &entry16)) { |
| 1245 | return 0u; |
| 1246 | } |
| 1247 | |
| 1248 | const StringPiece16 kAttr16 = u"attr"; |
| 1249 | const static std::u16string kAttrPrivate16 = u"^attr-private"; |
| 1250 | |
| 1251 | for (const PackageGroup& package_group : package_groups_) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1252 | for (const ConfiguredPackage& package_impl : package_group.packages_) { |
| 1253 | const LoadedPackage* package = package_impl.loaded_package_; |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1254 | if (package_name != package->GetPackageName()) { |
| 1255 | // All packages in the same group are expected to have the same package name. |
| 1256 | break; |
| 1257 | } |
| 1258 | |
| 1259 | uint32_t resid = package->FindEntryByName(type16, entry16); |
| 1260 | if (resid == 0u && kAttr16 == type16) { |
| 1261 | // Private attributes in libraries (such as the framework) are sometimes encoded |
| 1262 | // under the type '^attr-private' in order to leave the ID space of public 'attr' |
| 1263 | // free for future additions. Check '^attr-private' for the same name. |
| 1264 | resid = package->FindEntryByName(kAttrPrivate16, entry16); |
| 1265 | } |
| 1266 | |
| 1267 | if (resid != 0u) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 1268 | return fix_package_id(resid, package_group.dynamic_ref_table->mAssignedPackageId); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 1272 | return 0u; |
| 1273 | } |
| 1274 | |
Mårten Kongstad | 668ec5b | 2018-06-11 14:11:33 +0200 | [diff] [blame] | 1275 | void AssetManager2::RebuildFilterList(bool filter_incompatible_configs) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1276 | for (PackageGroup& group : package_groups_) { |
| 1277 | for (ConfiguredPackage& impl : group.packages_) { |
| 1278 | // Destroy it. |
| 1279 | impl.filtered_configs_.~ByteBucketArray(); |
| 1280 | |
| 1281 | // Re-create it. |
| 1282 | new (&impl.filtered_configs_) ByteBucketArray<FilteredConfigGroup>(); |
| 1283 | |
| 1284 | // Create the filters here. |
| 1285 | impl.loaded_package_->ForEachTypeSpec([&](const TypeSpec* spec, uint8_t type_index) { |
| 1286 | FilteredConfigGroup& group = impl.filtered_configs_.editItemAt(type_index); |
| 1287 | const auto iter_end = spec->types + spec->type_count; |
| 1288 | for (auto iter = spec->types; iter != iter_end; ++iter) { |
| 1289 | ResTable_config this_config; |
| 1290 | this_config.copyFromDtoH((*iter)->config); |
Mårten Kongstad | 668ec5b | 2018-06-11 14:11:33 +0200 | [diff] [blame] | 1291 | if (!filter_incompatible_configs || this_config.match(configuration_)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1292 | group.configurations.push_back(this_config); |
| 1293 | group.types.push_back(*iter); |
| 1294 | } |
| 1295 | } |
| 1296 | }); |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1301 | void AssetManager2::InvalidateCaches(uint32_t diff) { |
Ryan Mitchell | 2c4d874 | 2019-03-04 09:41:00 -0800 | [diff] [blame] | 1302 | cached_bag_resid_stacks_.clear(); |
| 1303 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1304 | if (diff == 0xffffffffu) { |
| 1305 | // Everything must go. |
| 1306 | cached_bags_.clear(); |
| 1307 | return; |
| 1308 | } |
| 1309 | |
| 1310 | // Be more conservative with what gets purged. Only if the bag has other possible |
| 1311 | // variations with respect to what changed (diff) should we remove it. |
| 1312 | for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) { |
| 1313 | if (diff & iter->second->type_spec_flags) { |
| 1314 | iter = cached_bags_.erase(iter); |
| 1315 | } else { |
| 1316 | ++iter; |
| 1317 | } |
| 1318 | } |
| 1319 | } |
| 1320 | |
Ryan Mitchell | 2e39422 | 2019-08-28 12:10:51 -0700 | [diff] [blame] | 1321 | uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) const { |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1322 | for (auto& package_group : package_groups_) { |
| 1323 | for (auto& package2 : package_group.packages_) { |
| 1324 | if (package2.loaded_package_ == package) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 1325 | return package_group.dynamic_ref_table->mAssignedPackageId; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1326 | } |
| 1327 | } |
| 1328 | } |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
Ryan Mitchell | fe50d73 | 2019-12-19 16:12:35 -0800 | [diff] [blame] | 1332 | DynamicLibManager* AssetManager2::GetDynamicLibManager() const { |
| 1333 | auto dynamic_lib_manager = |
| 1334 | std::get_if<std::unique_ptr<DynamicLibManager>>(&dynamic_lib_manager_); |
| 1335 | if (dynamic_lib_manager) { |
| 1336 | return (*dynamic_lib_manager).get(); |
| 1337 | } else { |
| 1338 | return *std::get_if<DynamicLibManager*>(&dynamic_lib_manager_); |
| 1339 | } |
| 1340 | } |
| 1341 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1342 | std::unique_ptr<Theme> AssetManager2::NewTheme() { |
| 1343 | return std::unique_ptr<Theme>(new Theme(this)); |
| 1344 | } |
| 1345 | |
| 1346 | Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) { |
| 1347 | } |
| 1348 | |
| 1349 | Theme::~Theme() = default; |
| 1350 | |
| 1351 | namespace { |
| 1352 | |
| 1353 | struct ThemeEntry { |
| 1354 | ApkAssetsCookie cookie; |
| 1355 | uint32_t type_spec_flags; |
| 1356 | Res_value value; |
| 1357 | }; |
| 1358 | |
| 1359 | struct ThemeType { |
| 1360 | int entry_count; |
| 1361 | ThemeEntry entries[0]; |
| 1362 | }; |
| 1363 | |
| 1364 | constexpr size_t kTypeCount = std::numeric_limits<uint8_t>::max() + 1; |
| 1365 | |
| 1366 | } // namespace |
| 1367 | |
| 1368 | struct Theme::Package { |
| 1369 | // Each element of Type will be a dynamically sized object |
| 1370 | // allocated to have the entries stored contiguously with the Type. |
| 1371 | std::array<util::unique_cptr<ThemeType>, kTypeCount> types; |
| 1372 | }; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1373 | |
| 1374 | bool Theme::ApplyStyle(uint32_t resid, bool force) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1375 | ATRACE_NAME("Theme::ApplyStyle"); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1376 | |
| 1377 | const ResolvedBag* bag = asset_manager_->GetBag(resid); |
| 1378 | if (bag == nullptr) { |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
| 1382 | // Merge the flags from this style. |
| 1383 | type_spec_flags_ |= bag->type_spec_flags; |
| 1384 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1385 | int last_type_idx = -1; |
| 1386 | int last_package_idx = -1; |
| 1387 | Package* last_package = nullptr; |
| 1388 | ThemeType* last_type = nullptr; |
| 1389 | |
| 1390 | // Iterate backwards, because each bag is sorted in ascending key ID order, meaning we will only |
| 1391 | // need to perform one resize per type. |
| 1392 | using reverse_bag_iterator = std::reverse_iterator<const ResolvedBag::Entry*>; |
| 1393 | const auto bag_iter_end = reverse_bag_iterator(begin(bag)); |
| 1394 | 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] | 1395 | const uint32_t attr_resid = bag_iter->key; |
| 1396 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1397 | // If the resource ID passed in is not a style, the key can be some other identifier that is not |
| 1398 | // 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] | 1399 | if (!is_valid_resid(attr_resid)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1400 | return false; |
| 1401 | } |
| 1402 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1403 | // We don't use the 0-based index for the type so that we can avoid doing ID validation |
| 1404 | // upon lookup. Instead, we keep space for the type ID 0 in our data structures. Since |
| 1405 | // the construction of this type is guarded with a resource ID check, it will never be |
| 1406 | // populated, and querying type ID 0 will always fail. |
| 1407 | const int package_idx = get_package_id(attr_resid); |
| 1408 | const int type_idx = get_type_id(attr_resid); |
| 1409 | const int entry_idx = get_entry_id(attr_resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1410 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1411 | if (last_package_idx != package_idx) { |
| 1412 | std::unique_ptr<Package>& package = packages_[package_idx]; |
| 1413 | if (package == nullptr) { |
| 1414 | package.reset(new Package()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1415 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1416 | last_package_idx = package_idx; |
| 1417 | last_package = package.get(); |
| 1418 | last_type_idx = -1; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1419 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1420 | |
| 1421 | if (last_type_idx != type_idx) { |
| 1422 | util::unique_cptr<ThemeType>& type = last_package->types[type_idx]; |
| 1423 | if (type == nullptr) { |
| 1424 | // Allocate enough memory to contain this entry_idx. Since we're iterating in reverse over |
| 1425 | // a sorted list of attributes, this shouldn't be resized again during this method call. |
| 1426 | type.reset(reinterpret_cast<ThemeType*>( |
| 1427 | calloc(sizeof(ThemeType) + (entry_idx + 1) * sizeof(ThemeEntry), 1))); |
| 1428 | type->entry_count = entry_idx + 1; |
| 1429 | } else if (entry_idx >= type->entry_count) { |
| 1430 | // Reallocate the memory to contain this entry_idx. Since we're iterating in reverse over |
| 1431 | // a sorted list of attributes, this shouldn't be resized again during this method call. |
| 1432 | const int new_count = entry_idx + 1; |
| 1433 | type.reset(reinterpret_cast<ThemeType*>( |
| 1434 | realloc(type.release(), sizeof(ThemeType) + (new_count * sizeof(ThemeEntry))))); |
| 1435 | |
| 1436 | // Clear out the newly allocated space (which isn't zeroed). |
| 1437 | memset(type->entries + type->entry_count, 0, |
| 1438 | (new_count - type->entry_count) * sizeof(ThemeEntry)); |
| 1439 | type->entry_count = new_count; |
| 1440 | } |
| 1441 | last_type_idx = type_idx; |
| 1442 | last_type = type.get(); |
| 1443 | } |
| 1444 | |
| 1445 | ThemeEntry& entry = last_type->entries[entry_idx]; |
| 1446 | if (force || (entry.value.dataType == Res_value::TYPE_NULL && |
| 1447 | entry.value.data != Res_value::DATA_NULL_EMPTY)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1448 | entry.cookie = bag_iter->cookie; |
| 1449 | entry.type_spec_flags |= bag->type_spec_flags; |
| 1450 | entry.value = bag_iter->value; |
| 1451 | } |
| 1452 | } |
| 1453 | return true; |
| 1454 | } |
| 1455 | |
| 1456 | ApkAssetsCookie Theme::GetAttribute(uint32_t resid, Res_value* out_value, |
| 1457 | uint32_t* out_flags) const { |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1458 | int cnt = 20; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1459 | |
| 1460 | uint32_t type_spec_flags = 0u; |
| 1461 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1462 | do { |
| 1463 | const int package_idx = get_package_id(resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1464 | const Package* package = packages_[package_idx].get(); |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1465 | if (package != nullptr) { |
| 1466 | // The themes are constructed with a 1-based type ID, so no need to decrement here. |
| 1467 | const int type_idx = get_type_id(resid); |
| 1468 | const ThemeType* type = package->types[type_idx].get(); |
| 1469 | if (type != nullptr) { |
| 1470 | const int entry_idx = get_entry_id(resid); |
| 1471 | if (entry_idx < type->entry_count) { |
| 1472 | const ThemeEntry& entry = type->entries[entry_idx]; |
| 1473 | type_spec_flags |= entry.type_spec_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1474 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1475 | if (entry.value.dataType == Res_value::TYPE_ATTRIBUTE) { |
| 1476 | if (cnt > 0) { |
| 1477 | cnt--; |
| 1478 | resid = entry.value.data; |
| 1479 | continue; |
| 1480 | } |
| 1481 | return kInvalidCookie; |
| 1482 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1483 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1484 | // @null is different than @empty. |
| 1485 | if (entry.value.dataType == Res_value::TYPE_NULL && |
| 1486 | entry.value.data != Res_value::DATA_NULL_EMPTY) { |
| 1487 | return kInvalidCookie; |
| 1488 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1489 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1490 | *out_value = entry.value; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1491 | *out_flags = type_spec_flags; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1492 | return entry.cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1493 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1494 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1495 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1496 | break; |
| 1497 | } while (true); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1498 | return kInvalidCookie; |
| 1499 | } |
| 1500 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 1501 | ApkAssetsCookie Theme::ResolveAttributeReference(ApkAssetsCookie cookie, Res_value* in_out_value, |
| 1502 | ResTable_config* in_out_selected_config, |
| 1503 | uint32_t* in_out_type_spec_flags, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1504 | uint32_t* out_last_ref) const { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 1505 | if (in_out_value->dataType == Res_value::TYPE_ATTRIBUTE) { |
| 1506 | uint32_t new_flags; |
| 1507 | cookie = GetAttribute(in_out_value->data, in_out_value, &new_flags); |
| 1508 | if (cookie == kInvalidCookie) { |
| 1509 | return kInvalidCookie; |
| 1510 | } |
| 1511 | |
| 1512 | if (in_out_type_spec_flags != nullptr) { |
| 1513 | *in_out_type_spec_flags |= new_flags; |
| 1514 | } |
| 1515 | } |
| 1516 | return asset_manager_->ResolveReference(cookie, in_out_value, in_out_selected_config, |
| 1517 | in_out_type_spec_flags, out_last_ref); |
| 1518 | } |
| 1519 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1520 | void Theme::Clear() { |
| 1521 | type_spec_flags_ = 0u; |
| 1522 | for (std::unique_ptr<Package>& package : packages_) { |
| 1523 | package.reset(); |
| 1524 | } |
| 1525 | } |
| 1526 | |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1527 | void Theme::SetTo(const Theme& o) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1528 | if (this == &o) { |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1529 | return; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1532 | type_spec_flags_ = o.type_spec_flags_; |
| 1533 | |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1534 | if (asset_manager_ == o.asset_manager_) { |
| 1535 | // The theme comes from the same asset manager so all theme data can be copied exactly |
| 1536 | for (size_t p = 0; p < packages_.size(); p++) { |
| 1537 | const Package *package = o.packages_[p].get(); |
| 1538 | if (package == nullptr) { |
| 1539 | // The other theme doesn't have this package, clear ours. |
| 1540 | packages_[p].reset(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1541 | continue; |
| 1542 | } |
| 1543 | |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1544 | if (packages_[p] == nullptr) { |
| 1545 | // The other theme has this package, but we don't. Make one. |
| 1546 | packages_[p].reset(new Package()); |
| 1547 | } |
| 1548 | |
| 1549 | for (size_t t = 0; t < package->types.size(); t++) { |
| 1550 | const ThemeType *type = package->types[t].get(); |
| 1551 | if (type == nullptr) { |
| 1552 | // The other theme doesn't have this type, clear ours. |
| 1553 | packages_[p]->types[t].reset(); |
| 1554 | continue; |
| 1555 | } |
| 1556 | |
| 1557 | // Create a new type and update it to theirs. |
| 1558 | const size_t type_alloc_size = sizeof(ThemeType) + (type->entry_count * sizeof(ThemeEntry)); |
| 1559 | void *copied_data = malloc(type_alloc_size); |
| 1560 | memcpy(copied_data, type, type_alloc_size); |
| 1561 | packages_[p]->types[t].reset(reinterpret_cast<ThemeType *>(copied_data)); |
| 1562 | } |
| 1563 | } |
| 1564 | } else { |
| 1565 | std::map<ApkAssetsCookie, ApkAssetsCookie> src_to_dest_asset_cookies; |
| 1566 | typedef std::map<int, int> SourceToDestinationRuntimePackageMap; |
| 1567 | std::map<ApkAssetsCookie, SourceToDestinationRuntimePackageMap> src_asset_cookie_id_map; |
| 1568 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1569 | // Determine which ApkAssets are loaded in both theme AssetManagers. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1570 | std::vector<const ApkAssets*> src_assets = o.asset_manager_->GetApkAssets(); |
| 1571 | for (size_t i = 0; i < src_assets.size(); i++) { |
| 1572 | const ApkAssets* src_asset = src_assets[i]; |
| 1573 | |
| 1574 | std::vector<const ApkAssets*> dest_assets = asset_manager_->GetApkAssets(); |
| 1575 | for (size_t j = 0; j < dest_assets.size(); j++) { |
| 1576 | const ApkAssets* dest_asset = dest_assets[j]; |
| 1577 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1578 | // Map the runtime package of the source apk asset to the destination apk asset. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1579 | if (src_asset->GetPath() == dest_asset->GetPath()) { |
| 1580 | const std::vector<std::unique_ptr<const LoadedPackage>>& src_packages = |
| 1581 | src_asset->GetLoadedArsc()->GetPackages(); |
| 1582 | const std::vector<std::unique_ptr<const LoadedPackage>>& dest_packages = |
| 1583 | dest_asset->GetLoadedArsc()->GetPackages(); |
| 1584 | |
| 1585 | SourceToDestinationRuntimePackageMap package_map; |
| 1586 | |
| 1587 | // The source and destination package should have the same number of packages loaded in |
| 1588 | // the same order. |
| 1589 | const size_t N = src_packages.size(); |
| 1590 | CHECK(N == dest_packages.size()) |
| 1591 | << " LoadedArsc " << src_asset->GetPath() << " differs number of packages."; |
| 1592 | for (size_t p = 0; p < N; p++) { |
| 1593 | auto& src_package = src_packages[p]; |
| 1594 | auto& dest_package = dest_packages[p]; |
| 1595 | CHECK(src_package->GetPackageName() == dest_package->GetPackageName()) |
| 1596 | << " Package " << src_package->GetPackageName() << " differs in load order."; |
| 1597 | |
| 1598 | int src_package_id = o.asset_manager_->GetAssignedPackageId(src_package.get()); |
| 1599 | int dest_package_id = asset_manager_->GetAssignedPackageId(dest_package.get()); |
| 1600 | package_map[src_package_id] = dest_package_id; |
| 1601 | } |
| 1602 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1603 | src_to_dest_asset_cookies.insert(std::make_pair(i, j)); |
| 1604 | src_asset_cookie_id_map.insert(std::make_pair(i, package_map)); |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1605 | break; |
| 1606 | } |
| 1607 | } |
| 1608 | } |
| 1609 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1610 | // Reset the data in the destination theme. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1611 | for (size_t p = 0; p < packages_.size(); p++) { |
| 1612 | if (packages_[p] != nullptr) { |
| 1613 | packages_[p].reset(); |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | for (size_t p = 0; p < packages_.size(); p++) { |
| 1618 | const Package *package = o.packages_[p].get(); |
| 1619 | if (package == nullptr) { |
| 1620 | continue; |
| 1621 | } |
| 1622 | |
| 1623 | for (size_t t = 0; t < package->types.size(); t++) { |
| 1624 | const ThemeType *type = package->types[t].get(); |
| 1625 | if (type == nullptr) { |
| 1626 | continue; |
| 1627 | } |
| 1628 | |
| 1629 | for (size_t e = 0; e < type->entry_count; e++) { |
| 1630 | const ThemeEntry &entry = type->entries[e]; |
| 1631 | if (entry.value.dataType == Res_value::TYPE_NULL && |
| 1632 | entry.value.data != Res_value::DATA_NULL_EMPTY) { |
| 1633 | continue; |
| 1634 | } |
| 1635 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1636 | bool is_reference = (entry.value.dataType == Res_value::TYPE_ATTRIBUTE |
| 1637 | || entry.value.dataType == Res_value::TYPE_REFERENCE |
| 1638 | || entry.value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE |
| 1639 | || entry.value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE) |
| 1640 | && entry.value.data != 0x0; |
Ryan Mitchell | b85d9b2 | 2018-11-19 12:11:38 -0800 | [diff] [blame] | 1641 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1642 | // If the attribute value represents an attribute or reference, the package id of the |
| 1643 | // value needs to be rewritten to the package id of the value in the destination. |
| 1644 | uint32_t attribute_data = entry.value.data; |
| 1645 | if (is_reference) { |
| 1646 | // Determine the package id of the reference in the destination AssetManager. |
Ryan Mitchell | b85d9b2 | 2018-11-19 12:11:38 -0800 | [diff] [blame] | 1647 | auto value_package_map = src_asset_cookie_id_map.find(entry.cookie); |
| 1648 | if (value_package_map == src_asset_cookie_id_map.end()) { |
| 1649 | continue; |
| 1650 | } |
| 1651 | |
| 1652 | auto value_dest_package = value_package_map->second.find( |
| 1653 | get_package_id(entry.value.data)); |
| 1654 | if (value_dest_package == value_package_map->second.end()) { |
| 1655 | continue; |
| 1656 | } |
| 1657 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1658 | attribute_data = fix_package_id(entry.value.data, value_dest_package->second); |
| 1659 | } |
| 1660 | |
| 1661 | // Find the cookie of the value in the destination. If the source apk is not loaded in the |
| 1662 | // destination, only copy resources that do not reference resources in the source. |
| 1663 | ApkAssetsCookie data_dest_cookie; |
| 1664 | auto value_dest_cookie = src_to_dest_asset_cookies.find(entry.cookie); |
| 1665 | if (value_dest_cookie != src_to_dest_asset_cookies.end()) { |
| 1666 | data_dest_cookie = value_dest_cookie->second; |
| 1667 | } else { |
| 1668 | if (is_reference || entry.value.dataType == Res_value::TYPE_STRING) { |
| 1669 | continue; |
| 1670 | } else { |
| 1671 | data_dest_cookie = 0x0; |
| 1672 | } |
Ryan Mitchell | b85d9b2 | 2018-11-19 12:11:38 -0800 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | // The package id of the attribute needs to be rewritten to the package id of the |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1676 | // attribute in the destination. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1677 | int attribute_dest_package_id = p; |
| 1678 | if (attribute_dest_package_id != 0x01) { |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1679 | // Find the cookie of the attribute resource id in the source AssetManager |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1680 | FindEntryResult attribute_entry_result; |
| 1681 | ApkAssetsCookie attribute_cookie = |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 1682 | o.asset_manager_->FindEntry(make_resid(p, t, e), 0 /* density_override */ , |
| 1683 | true /* stop_at_first_match */, |
| 1684 | true /* ignore_configuration */, |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1685 | &attribute_entry_result); |
| 1686 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1687 | // Determine the package id of the attribute in the destination AssetManager. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1688 | auto attribute_package_map = src_asset_cookie_id_map.find(attribute_cookie); |
| 1689 | if (attribute_package_map == src_asset_cookie_id_map.end()) { |
| 1690 | continue; |
| 1691 | } |
| 1692 | auto attribute_dest_package = attribute_package_map->second.find( |
| 1693 | attribute_dest_package_id); |
| 1694 | if (attribute_dest_package == attribute_package_map->second.end()) { |
| 1695 | continue; |
| 1696 | } |
| 1697 | attribute_dest_package_id = attribute_dest_package->second; |
| 1698 | } |
| 1699 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1700 | // Lazily instantiate the destination package. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1701 | std::unique_ptr<Package>& dest_package = packages_[attribute_dest_package_id]; |
| 1702 | if (dest_package == nullptr) { |
| 1703 | dest_package.reset(new Package()); |
| 1704 | } |
| 1705 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1706 | // Lazily instantiate and resize the destination type. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1707 | util::unique_cptr<ThemeType>& dest_type = dest_package->types[t]; |
| 1708 | if (dest_type == nullptr || dest_type->entry_count < type->entry_count) { |
| 1709 | const size_t type_alloc_size = sizeof(ThemeType) |
| 1710 | + (type->entry_count * sizeof(ThemeEntry)); |
| 1711 | void* dest_data = malloc(type_alloc_size); |
| 1712 | memset(dest_data, 0, type->entry_count * sizeof(ThemeEntry)); |
| 1713 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1714 | // Copy the existing destination type values if the type is resized. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1715 | if (dest_type != nullptr) { |
| 1716 | memcpy(dest_data, type, sizeof(ThemeType) |
| 1717 | + (dest_type->entry_count * sizeof(ThemeEntry))); |
| 1718 | } |
| 1719 | |
| 1720 | dest_type.reset(reinterpret_cast<ThemeType *>(dest_data)); |
| 1721 | dest_type->entry_count = type->entry_count; |
| 1722 | } |
| 1723 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1724 | dest_type->entries[e].cookie = data_dest_cookie; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1725 | dest_type->entries[e].value.dataType = entry.value.dataType; |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1726 | dest_type->entries[e].value.data = attribute_data; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1727 | dest_type->entries[e].type_spec_flags = entry.type_spec_flags; |
| 1728 | } |
| 1729 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1730 | } |
| 1731 | } |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | void Theme::Dump() const { |
| 1735 | base::ScopedLogSeverity _log(base::INFO); |
| 1736 | LOG(INFO) << base::StringPrintf("Theme(this=%p, AssetManager2=%p)", this, asset_manager_); |
| 1737 | |
| 1738 | for (int p = 0; p < packages_.size(); p++) { |
| 1739 | auto& package = packages_[p]; |
| 1740 | if (package == nullptr) { |
| 1741 | continue; |
| 1742 | } |
| 1743 | |
| 1744 | for (int t = 0; t < package->types.size(); t++) { |
| 1745 | auto& type = package->types[t]; |
| 1746 | if (type == nullptr) { |
| 1747 | continue; |
| 1748 | } |
| 1749 | |
| 1750 | for (int e = 0; e < type->entry_count; e++) { |
| 1751 | auto& entry = type->entries[e]; |
| 1752 | if (entry.value.dataType == Res_value::TYPE_NULL && |
| 1753 | entry.value.data != Res_value::DATA_NULL_EMPTY) { |
| 1754 | continue; |
| 1755 | } |
| 1756 | |
| 1757 | LOG(INFO) << base::StringPrintf(" entry(0x%08x)=(0x%08x) type=(0x%02x), cookie(%d)", |
| 1758 | make_resid(p, t, e), entry.value.data, |
| 1759 | entry.value.dataType, entry.cookie); |
| 1760 | } |
| 1761 | } |
| 1762 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | } // namespace android |