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