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> |
Yurii Zubrytskyi | 59dab3b | 2022-11-03 00:08:49 -0700 | [diff] [blame] | 25 | #include <span> |
Jeremy Meyer | e00958a | 2025-02-19 16:55:19 -0800 | [diff] [blame] | 26 | #include <sstream> |
Yurii Zubrytskyi | 1e5452a | 2024-06-13 19:30:36 -0700 | [diff] [blame] | 27 | #include <utility> |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 28 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 29 | #include "android-base/logging.h" |
| 30 | #include "android-base/stringprintf.h" |
Yurii Zubrytskyi | 1e5452a | 2024-06-13 19:30:36 -0700 | [diff] [blame] | 31 | #include "androidfw/CombinedIterator.h" |
Jackal Guo | 552b45d | 2021-09-29 10:52:19 +0800 | [diff] [blame] | 32 | #include "androidfw/ResourceTypes.h" |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 33 | #include "androidfw/ResourceUtils.h" |
Ryan Mitchell | 31b1105 | 2019-06-13 13:47:26 -0700 | [diff] [blame] | 34 | #include "androidfw/Util.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 35 | #include "utils/ByteOrder.h" |
| 36 | #include "utils/Trace.h" |
| 37 | |
| 38 | #ifdef _WIN32 |
| 39 | #ifdef ERROR |
| 40 | #undef ERROR |
| 41 | #endif |
| 42 | #endif |
| 43 | |
| 44 | namespace android { |
| 45 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 46 | namespace { |
| 47 | |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 48 | constexpr int32_t kDefaultDisplayId = 0; |
| 49 | constexpr int32_t kDefaultDeviceId = 0; |
| 50 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 51 | using EntryValue = std::variant<Res_value, incfs::verified_map_ptr<ResTable_map_entry>>; |
| 52 | |
Eric Miao | 368cd19 | 2022-09-09 15:46:14 -0700 | [diff] [blame] | 53 | /* NOTE: table_entry has been verified in LoadedPackage::GetEntryFromOffset(), |
| 54 | * and so access to ->value() and ->map_entry() are safe here |
| 55 | */ |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 56 | base::expected<EntryValue, IOError> GetEntryValue( |
| 57 | incfs::verified_map_ptr<ResTable_entry> table_entry) { |
Eric Miao | 368cd19 | 2022-09-09 15:46:14 -0700 | [diff] [blame] | 58 | const uint16_t entry_size = table_entry->size(); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 59 | |
| 60 | // Check if the entry represents a bag value. |
Eric Miao | 368cd19 | 2022-09-09 15:46:14 -0700 | [diff] [blame] | 61 | if (entry_size >= sizeof(ResTable_map_entry) && table_entry->is_complex()) { |
| 62 | return table_entry.convert<ResTable_map_entry>().verified(); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Eric Miao | 368cd19 | 2022-09-09 15:46:14 -0700 | [diff] [blame] | 65 | return table_entry->value(); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Jeremy Meyer | e00958a | 2025-02-19 16:55:19 -0800 | [diff] [blame] | 68 | } // namespace |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 69 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 70 | struct FindEntryResult { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 71 | // The cookie representing the ApkAssets in which the value resides. |
| 72 | ApkAssetsCookie cookie; |
| 73 | |
| 74 | // The value of the resource table entry. Either an android::Res_value for non-bag types or an |
| 75 | // incfs::verified_map_ptr<ResTable_map_entry> for bag types. |
| 76 | EntryValue entry; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 77 | |
| 78 | // The configuration for which the resulting entry was defined. This is already swapped to host |
| 79 | // endianness. |
| 80 | ResTable_config config; |
| 81 | |
| 82 | // The bitmask of configuration axis with which the resource value varies. |
| 83 | uint32_t type_flags; |
| 84 | |
Jeremy Meyer | b76f745 | 2025-03-12 10:55:53 -0700 | [diff] [blame] | 85 | // The bitmask of ResTable_entry flags |
| 86 | uint16_t entry_flags; |
| 87 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 88 | // The dynamic package ID map for the package from which this resource came from. |
| 89 | const DynamicRefTable* dynamic_ref_table; |
| 90 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 91 | // The package name of the resource. |
| 92 | const std::string* package_name; |
| 93 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 94 | // The string pool reference to the type's name. This uses a different string pool than |
| 95 | // the global string pool, but this is hidden from the caller. |
| 96 | StringPoolRef type_string_ref; |
| 97 | |
| 98 | // The string pool reference to the entry's name. This uses a different string pool than |
| 99 | // the global string pool, but this is hidden from the caller. |
| 100 | StringPoolRef entry_string_ref; |
| 101 | }; |
| 102 | |
Ryan Prichard | 41e15a0 | 2023-08-30 22:19:30 -0700 | [diff] [blame] | 103 | struct Theme::Entry { |
| 104 | ApkAssetsCookie cookie; |
| 105 | uint32_t type_spec_flags; |
| 106 | Res_value value; |
| 107 | }; |
| 108 | |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 109 | AssetManager2::AssetManager2(ApkAssetsList apk_assets, const ResTable_config& configuration) |
| 110 | : display_id_(kDefaultDisplayId), device_id_(kDefaultDeviceId) { |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 111 | configurations_.push_back(configuration); |
| 112 | |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 113 | // Don't invalidate caches here as there's nothing cached yet. |
| 114 | SetApkAssets(apk_assets, false); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 115 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 116 | |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 117 | AssetManager2::AssetManager2() : display_id_(kDefaultDisplayId), device_id_(kDefaultDeviceId) { |
Yurii Zubrytskyi | c4dadee | 2024-08-15 12:08:47 -0700 | [diff] [blame] | 118 | configurations_.emplace_back(); |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 121 | bool AssetManager2::SetApkAssets(ApkAssetsList apk_assets, bool invalidate_caches) { |
| 122 | BuildDynamicRefTable(apk_assets); |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 123 | RebuildFilterList(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 124 | if (invalidate_caches) { |
| 125 | InvalidateCaches(static_cast<uint32_t>(-1)); |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
Michael Hoisie | 7b43333 | 2024-02-13 21:42:21 +0000 | [diff] [blame] | 130 | void AssetManager2::PresetApkAssets(ApkAssetsList apk_assets) { |
| 131 | BuildDynamicRefTable(apk_assets); |
| 132 | } |
| 133 | |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 134 | bool AssetManager2::SetApkAssets(std::initializer_list<ApkAssetsPtr> apk_assets, |
| 135 | bool invalidate_caches) { |
| 136 | return SetApkAssets(ApkAssetsList(apk_assets.begin(), apk_assets.size()), invalidate_caches); |
| 137 | } |
| 138 | |
| 139 | void AssetManager2::BuildDynamicRefTable(ApkAssetsList apk_assets) { |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 140 | auto op = StartOperation(); |
| 141 | |
| 142 | apk_assets_.resize(apk_assets.size()); |
| 143 | for (size_t i = 0; i != apk_assets.size(); ++i) { |
| 144 | apk_assets_[i].first = apk_assets[i]; |
| 145 | // Let's populate the locked assets right away as we're going to need them here later. |
| 146 | apk_assets_[i].second = apk_assets[i]; |
| 147 | } |
| 148 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 149 | package_groups_.clear(); |
| 150 | package_ids_.fill(0xff); |
| 151 | |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 152 | // A mapping from path of apk assets that could be target packages of overlays to the runtime |
| 153 | // package id of its first loaded package. Overlays currently can only override resources in the |
| 154 | // first package in the target resource table. |
Yurii Zubrytskyi | e3f9be6 | 2022-11-14 18:30:10 -0800 | [diff] [blame] | 155 | std::unordered_map<std::string_view, uint8_t> target_assets_package_ids; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 156 | |
Ryan Mitchell | 824cc49 | 2020-02-12 10:48:14 -0800 | [diff] [blame] | 157 | // Overlay resources are not directly referenced by an application so their resource ids |
| 158 | // can change throughout the application's lifetime. Assign overlay package ids last. |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 159 | std::vector<const ApkAssets*> sorted_apk_assets; |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 160 | sorted_apk_assets.reserve(apk_assets.size()); |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 161 | for (auto& asset : apk_assets) { |
| 162 | sorted_apk_assets.push_back(asset.get()); |
| 163 | } |
| 164 | std::stable_partition(sorted_apk_assets.begin(), sorted_apk_assets.end(), |
| 165 | [](auto a) { return !a->IsOverlay(); }); |
Ryan Mitchell | 824cc49 | 2020-02-12 10:48:14 -0800 | [diff] [blame] | 166 | |
| 167 | // The assets cookie must map to the position of the apk assets in the unsorted apk assets list. |
| 168 | std::unordered_map<const ApkAssets*, ApkAssetsCookie> apk_assets_cookies; |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 169 | apk_assets_cookies.reserve(apk_assets.size()); |
| 170 | for (size_t i = 0, n = apk_assets.size(); i < n; i++) { |
| 171 | apk_assets_cookies[apk_assets[i].get()] = static_cast<ApkAssetsCookie>(i); |
Ryan Mitchell | 824cc49 | 2020-02-12 10:48:14 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Ryan Mitchell | b894c27 | 2020-02-12 10:31:44 -0800 | [diff] [blame] | 174 | // 0x01 is reserved for the android package. |
| 175 | int next_package_id = 0x02; |
Ryan Mitchell | 824cc49 | 2020-02-12 10:48:14 -0800 | [diff] [blame] | 176 | for (const ApkAssets* apk_assets : sorted_apk_assets) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 177 | std::shared_ptr<OverlayDynamicRefTable> overlay_ref_table; |
| 178 | if (auto loaded_idmap = apk_assets->GetLoadedIdmap(); loaded_idmap != nullptr) { |
| 179 | // The target package must precede the overlay package in the apk assets paths in order |
| 180 | // to take effect. |
Yurii Zubrytskyi | e3f9be6 | 2022-11-14 18:30:10 -0800 | [diff] [blame] | 181 | auto iter = target_assets_package_ids.find(loaded_idmap->TargetApkPath()); |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 182 | if (iter == target_assets_package_ids.end()) { |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 183 | LOG(INFO) << "failed to find target package for overlay " << loaded_idmap->OverlayApkPath(); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 184 | } else { |
| 185 | uint8_t target_package_id = iter->second; |
| 186 | |
| 187 | // Create a special dynamic reference table for the overlay to rewrite references to |
| 188 | // overlay resources as references to the target resources they overlay. |
| 189 | overlay_ref_table = std::make_shared<OverlayDynamicRefTable>( |
| 190 | loaded_idmap->GetOverlayDynamicRefTable(target_package_id)); |
| 191 | |
| 192 | // Add the overlay resource map to the target package's set of overlays. |
| 193 | const uint8_t target_idx = package_ids_[target_package_id]; |
| 194 | CHECK(target_idx != 0xff) << "overlay target '" << loaded_idmap->TargetApkPath() |
| 195 | << "'added to apk_assets_package_ids but does not have an" |
| 196 | << " assigned package group"; |
| 197 | |
| 198 | PackageGroup& target_package_group = package_groups_[target_idx]; |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 199 | target_package_group.overlays_.push_back(ConfiguredOverlay{ |
| 200 | loaded_idmap->GetTargetResourcesMap(target_package_id, overlay_ref_table.get()), |
| 201 | apk_assets_cookies[apk_assets], |
| 202 | IsAnyOverlayConstraintSatisfied(loaded_idmap->GetConstraints()) |
| 203 | }); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Ryan Mitchell | b894c27 | 2020-02-12 10:31:44 -0800 | [diff] [blame] | 207 | const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc(); |
Ryan Mitchell | b894c27 | 2020-02-12 10:31:44 -0800 | [diff] [blame] | 208 | for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) { |
| 209 | // Get the package ID or assign one if a shared library. |
| 210 | int package_id; |
| 211 | if (package->IsDynamic()) { |
| 212 | package_id = next_package_id++; |
| 213 | } else { |
| 214 | package_id = package->GetPackageId(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 217 | uint8_t idx = package_ids_[package_id]; |
| 218 | if (idx == 0xff) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 219 | // Add the mapping for package ID to index if not present. |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 220 | package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size()); |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 221 | PackageGroup& new_group = package_groups_.emplace_back(); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 222 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 223 | if (overlay_ref_table != nullptr) { |
| 224 | // If this package is from an overlay, use a dynamic reference table that can rewrite |
| 225 | // overlay resource ids to their corresponding target resource ids. |
Yurii Zubrytskyi | e3f9be6 | 2022-11-14 18:30:10 -0800 | [diff] [blame] | 226 | new_group.dynamic_ref_table = std::move(overlay_ref_table); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 229 | DynamicRefTable* ref_table = new_group.dynamic_ref_table.get(); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 230 | ref_table->mAssignedPackageId = package_id; |
| 231 | ref_table->mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 232 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 233 | |
Yurii Zubrytskyi | e3f9be6 | 2022-11-14 18:30:10 -0800 | [diff] [blame] | 234 | // Add the package to the set of packages with the same ID. |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 235 | PackageGroup* package_group = &package_groups_[idx]; |
Yurii Zubrytskyi | e3f9be6 | 2022-11-14 18:30:10 -0800 | [diff] [blame] | 236 | package_group->packages_.emplace_back().loaded_package_ = package.get(); |
Ryan Mitchell | 824cc49 | 2020-02-12 10:48:14 -0800 | [diff] [blame] | 237 | package_group->cookies_.push_back(apk_assets_cookies[apk_assets]); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 238 | |
| 239 | // Add the package name -> build time ID mappings. |
| 240 | for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) { |
| 241 | String16 package_name(entry.package_name.c_str(), entry.package_name.size()); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 242 | package_group->dynamic_ref_table->mEntries.replaceValueFor( |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 243 | package_name, static_cast<uint8_t>(entry.package_id)); |
| 244 | } |
Ryan Mitchell | b894c27 | 2020-02-12 10:31:44 -0800 | [diff] [blame] | 245 | |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 246 | if (auto apk_assets_path = apk_assets->GetPath()) { |
| 247 | // Overlay target ApkAssets must have been created using path based load apis. |
Yurii Zubrytskyi | e3f9be6 | 2022-11-14 18:30:10 -0800 | [diff] [blame] | 248 | target_assets_package_ids.emplace(*apk_assets_path, package_id); |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 249 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | // Now assign the runtime IDs so that we have a build-time to runtime ID map. |
Yurii Zubrytskyi | 59dab3b | 2022-11-03 00:08:49 -0700 | [diff] [blame] | 254 | DynamicRefTable::AliasMap aliases; |
| 255 | for (const auto& group : package_groups_) { |
| 256 | const std::string& package_name = group.packages_[0].loaded_package_->GetPackageName(); |
| 257 | const auto name_16 = String16(package_name.c_str(), package_name.size()); |
| 258 | for (auto&& inner_group : package_groups_) { |
| 259 | inner_group.dynamic_ref_table->addMapping(name_16, |
| 260 | group.dynamic_ref_table->mAssignedPackageId); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 261 | } |
Yurii Zubrytskyi | 59dab3b | 2022-11-03 00:08:49 -0700 | [diff] [blame] | 262 | |
| 263 | for (const auto& package : group.packages_) { |
| 264 | const auto& package_aliases = package.loaded_package_->GetAliasResourceIdMap(); |
Yurii Zubrytskyi | e3f9be6 | 2022-11-14 18:30:10 -0800 | [diff] [blame] | 265 | aliases.insert(aliases.end(), package_aliases.begin(), package_aliases.end()); |
Yurii Zubrytskyi | 59dab3b | 2022-11-03 00:08:49 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | if (!aliases.empty()) { |
Yurii Zubrytskyi | e3f9be6 | 2022-11-14 18:30:10 -0800 | [diff] [blame] | 270 | std::sort(aliases.begin(), aliases.end(), [](auto&& l, auto&& r) { return l.first < r.first; }); |
| 271 | |
Yurii Zubrytskyi | 59dab3b | 2022-11-03 00:08:49 -0700 | [diff] [blame] | 272 | // Add the alias resources to the dynamic reference table of every package group. Since |
| 273 | // staging aliases can only be defined by the framework package (which is not a shared |
| 274 | // library), the compile-time package id of the framework is the same across all packages |
| 275 | // that compile against the framework. |
| 276 | for (auto& group : std::span(package_groups_.data(), package_groups_.size() - 1)) { |
| 277 | group.dynamic_ref_table->setAliases(aliases); |
| 278 | } |
| 279 | package_groups_.back().dynamic_ref_table->setAliases(std::move(aliases)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | void AssetManager2::DumpToLog() const { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 284 | LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this); |
| 285 | |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 286 | auto op = StartOperation(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 287 | std::string list; |
Yurii Zubrytskyi | 7d70bc5 | 2023-05-12 13:27:54 -0700 | [diff] [blame] | 288 | for (size_t i = 0, s = apk_assets_.size(); i < s; ++i) { |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 289 | const auto& assets = GetApkAssets(i); |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 290 | base::StringAppendF(&list, "%s,", assets ? assets->GetDebugName().c_str() : "nullptr"); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 291 | } |
| 292 | LOG(INFO) << "ApkAssets: " << list; |
| 293 | |
| 294 | list = ""; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 295 | for (size_t i = 0; i < package_ids_.size(); i++) { |
| 296 | if (package_ids_[i] != 0xff) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 297 | base::StringAppendF(&list, "%02x -> %d, ", (int)i, package_ids_[i]); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | LOG(INFO) << "Package ID map: " << list; |
| 301 | |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 302 | for (const auto& package_group : package_groups_) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 303 | list = ""; |
| 304 | for (const auto& package : package_group.packages_) { |
| 305 | const LoadedPackage* loaded_package = package.loaded_package_; |
| 306 | base::StringAppendF(&list, "%s(%02x%s), ", loaded_package->GetPackageName().c_str(), |
| 307 | loaded_package->GetPackageId(), |
| 308 | (loaded_package->IsDynamic() ? " dynamic" : "")); |
| 309 | } |
| 310 | LOG(INFO) << base::StringPrintf("PG (%02x): ", |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 311 | package_group.dynamic_ref_table->mAssignedPackageId) |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 312 | << list; |
Ryan Mitchell | 5db396d | 2018-11-05 15:56:15 -0800 | [diff] [blame] | 313 | |
| 314 | for (size_t i = 0; i < 256; i++) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 315 | if (package_group.dynamic_ref_table->mLookupTable[i] != 0) { |
Ryan Mitchell | 5db396d | 2018-11-05 15:56:15 -0800 | [diff] [blame] | 316 | LOG(INFO) << base::StringPrintf(" e[0x%02x] -> 0x%02x", (uint8_t) i, |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 317 | package_group.dynamic_ref_table->mLookupTable[i]); |
Ryan Mitchell | 5db396d | 2018-11-05 15:56:15 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 320 | } |
| 321 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 322 | |
| 323 | const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const { |
| 324 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 325 | return nullptr; |
| 326 | } |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 327 | auto op = StartOperation(); |
| 328 | const auto& assets = GetApkAssets(cookie); |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 329 | return assets ? assets->GetLoadedArsc()->GetStringPool() : nullptr; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 332 | const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const { |
| 333 | if (package_id >= package_ids_.size()) { |
| 334 | return nullptr; |
| 335 | } |
| 336 | |
| 337 | const size_t idx = package_ids_[package_id]; |
| 338 | if (idx == 0xff) { |
| 339 | return nullptr; |
| 340 | } |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 341 | return package_groups_[idx].dynamic_ref_table.get(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 342 | } |
| 343 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 344 | std::shared_ptr<const DynamicRefTable> AssetManager2::GetDynamicRefTableForCookie( |
| 345 | ApkAssetsCookie cookie) const { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 346 | for (const PackageGroup& package_group : package_groups_) { |
| 347 | for (const ApkAssetsCookie& package_cookie : package_group.cookies_) { |
| 348 | if (package_cookie == cookie) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 349 | return package_group.dynamic_ref_table; |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | } |
| 353 | return nullptr; |
| 354 | } |
| 355 | |
MÃ¥rten Kongstad | c92c4dd | 2019-02-05 01:29:59 +0100 | [diff] [blame] | 356 | const std::unordered_map<std::string, std::string>* |
| 357 | AssetManager2::GetOverlayableMapForPackage(uint32_t package_id) const { |
MÃ¥rten Kongstad | c92c4dd | 2019-02-05 01:29:59 +0100 | [diff] [blame] | 358 | if (package_id >= package_ids_.size()) { |
| 359 | return nullptr; |
| 360 | } |
| 361 | |
| 362 | const size_t idx = package_ids_[package_id]; |
| 363 | if (idx == 0xff) { |
| 364 | return nullptr; |
| 365 | } |
| 366 | |
| 367 | const PackageGroup& package_group = package_groups_[idx]; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 368 | if (package_group.packages_.empty()) { |
MÃ¥rten Kongstad | c92c4dd | 2019-02-05 01:29:59 +0100 | [diff] [blame] | 369 | return nullptr; |
| 370 | } |
| 371 | |
| 372 | const auto loaded_package = package_group.packages_[0].loaded_package_; |
| 373 | return &loaded_package->GetOverlayableMap(); |
| 374 | } |
| 375 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 376 | bool AssetManager2::GetOverlayablesToString(android::StringPiece package_name, |
Ryan Mitchell | 2e39422 | 2019-08-28 12:10:51 -0700 | [diff] [blame] | 377 | std::string* out) const { |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 378 | auto op = StartOperation(); |
Ryan Mitchell | 2e39422 | 2019-08-28 12:10:51 -0700 | [diff] [blame] | 379 | uint8_t package_id = 0U; |
Yurii Zubrytskyi | 7d70bc5 | 2023-05-12 13:27:54 -0700 | [diff] [blame] | 380 | for (size_t i = 0, s = apk_assets_.size(); i != s; ++i) { |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 381 | const auto& assets = GetApkAssets(i); |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 382 | if (!assets) { |
| 383 | continue; |
| 384 | } |
| 385 | const LoadedArsc* loaded_arsc = assets->GetLoadedArsc(); |
Ryan Mitchell | 2e39422 | 2019-08-28 12:10:51 -0700 | [diff] [blame] | 386 | if (loaded_arsc == nullptr) { |
| 387 | continue; |
| 388 | } |
| 389 | |
| 390 | const auto& loaded_packages = loaded_arsc->GetPackages(); |
| 391 | if (loaded_packages.empty()) { |
| 392 | continue; |
| 393 | } |
| 394 | |
| 395 | const auto& loaded_package = loaded_packages[0]; |
| 396 | if (loaded_package->GetPackageName() == package_name) { |
| 397 | package_id = GetAssignedPackageId(loaded_package.get()); |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (package_id == 0U) { |
| 403 | ANDROID_LOG(ERROR) << base::StringPrintf("No package with name '%s", package_name.data()); |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | const size_t idx = package_ids_[package_id]; |
| 408 | if (idx == 0xff) { |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | std::string output; |
| 413 | for (const ConfiguredPackage& package : package_groups_[idx].packages_) { |
| 414 | const LoadedPackage* loaded_package = package.loaded_package_; |
| 415 | for (auto it = loaded_package->begin(); it != loaded_package->end(); it++) { |
| 416 | const OverlayableInfo* info = loaded_package->GetOverlayableInfo(*it); |
| 417 | if (info != nullptr) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 418 | auto res_name = GetResourceName(*it); |
| 419 | if (!res_name.has_value()) { |
Ryan Mitchell | 2e39422 | 2019-08-28 12:10:51 -0700 | [diff] [blame] | 420 | ANDROID_LOG(ERROR) << base::StringPrintf( |
| 421 | "Unable to retrieve name of overlayable resource 0x%08x", *it); |
| 422 | return false; |
| 423 | } |
| 424 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 425 | const std::string name = ToFormattedResourceString(*res_name); |
Ryan Mitchell | 2e39422 | 2019-08-28 12:10:51 -0700 | [diff] [blame] | 426 | output.append(base::StringPrintf( |
| 427 | "resource='%s' overlayable='%s' actor='%s' policy='0x%08x'\n", |
Yurii Zubrytskyi | 9d22537 | 2022-11-29 11:12:18 -0800 | [diff] [blame] | 428 | name.c_str(), info->name.data(), info->actor.data(), info->policy_flags)); |
Ryan Mitchell | 2e39422 | 2019-08-28 12:10:51 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | *out = std::move(output); |
| 434 | return true; |
| 435 | } |
| 436 | |
Ryan Mitchell | 192400c | 2020-04-02 09:54:23 -0700 | [diff] [blame] | 437 | bool AssetManager2::ContainsAllocatedTable() const { |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 438 | auto op = StartOperation(); |
Yurii Zubrytskyi | 7d70bc5 | 2023-05-12 13:27:54 -0700 | [diff] [blame] | 439 | for (size_t i = 0, s = apk_assets_.size(); i != s; ++i) { |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 440 | const auto& assets = GetApkAssets(i); |
| 441 | if (assets && assets->IsTableAllocated()) { |
| 442 | return true; |
| 443 | } |
| 444 | } |
| 445 | return false; |
Ryan Mitchell | 192400c | 2020-04-02 09:54:23 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Jeremy Meyer | e00958a | 2025-02-19 16:55:19 -0800 | [diff] [blame] | 448 | static std::string ConfigVecToString(std::span<const ResTable_config> configurations) { |
| 449 | std::stringstream ss; |
| 450 | ss << "["; |
| 451 | bool first = true; |
| 452 | for (const auto& config : configurations) { |
| 453 | if (!first) { |
| 454 | ss << ","; |
| 455 | } |
| 456 | char out[RESTABLE_MAX_LOCALE_LEN] = {}; |
| 457 | config.getBcp47Locale(out); |
| 458 | ss << out; |
| 459 | first = false; |
| 460 | } |
| 461 | ss << "]"; |
| 462 | return ss.str(); |
| 463 | } |
| 464 | |
| 465 | |
Yurii Zubrytskyi | c4dadee | 2024-08-15 12:08:47 -0700 | [diff] [blame] | 466 | void AssetManager2::SetConfigurations(std::span<const ResTable_config> configurations, |
| 467 | bool force_refresh) { |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 468 | int diff = 0; |
Michael Hoisie | 7b43333 | 2024-02-13 21:42:21 +0000 | [diff] [blame] | 469 | if (force_refresh) { |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 470 | diff = -1; |
| 471 | } else { |
Michael Hoisie | 7b43333 | 2024-02-13 21:42:21 +0000 | [diff] [blame] | 472 | if (configurations_.size() != configurations.size()) { |
| 473 | diff = -1; |
| 474 | } else { |
| 475 | for (int i = 0; i < configurations_.size(); i++) { |
| 476 | diff |= configurations_[i].diff(configurations[i]); |
| 477 | } |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
Jeremy Meyer | e00958a | 2025-02-19 16:55:19 -0800 | [diff] [blame] | 480 | |
| 481 | // Log the locale list change to investigate b/392255526 |
| 482 | if (diff & ConfigDescription::CONFIG_LOCALE) { |
| 483 | auto oldstr = ConfigVecToString(configurations_); |
| 484 | auto newstr = ConfigVecToString(configurations); |
| 485 | if (oldstr != newstr) { |
| 486 | LOG(INFO) << "AssetManager2(" << this << ") locale list changing from " |
| 487 | << oldstr << " to " << newstr; |
| 488 | } |
| 489 | } |
| 490 | |
Yurii Zubrytskyi | c4dadee | 2024-08-15 12:08:47 -0700 | [diff] [blame] | 491 | configurations_.clear(); |
| 492 | for (auto&& config : configurations) { |
| 493 | configurations_.emplace_back(config); |
| 494 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 495 | if (diff) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 496 | RebuildFilterList(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 497 | InvalidateCaches(static_cast<uint32_t>(diff)); |
| 498 | } |
| 499 | } |
| 500 | |
Jeremy Meyer | e00958a | 2025-02-19 16:55:19 -0800 | [diff] [blame] | 501 | void AssetManager2::SetDefaultLocale(std::optional<ResTable_config> default_locale) { |
| 502 | int diff = 0; |
| 503 | if (default_locale_ && default_locale) { |
| 504 | diff = default_locale_->diff(default_locale.value()); |
| 505 | } else if (default_locale_ || default_locale) { |
| 506 | diff = -1; |
| 507 | } |
| 508 | if (diff & ConfigDescription::CONFIG_LOCALE) { |
| 509 | char old_loc[RESTABLE_MAX_LOCALE_LEN] = {}; |
| 510 | char new_loc[RESTABLE_MAX_LOCALE_LEN] = {}; |
| 511 | if (default_locale_) { |
| 512 | default_locale_->getBcp47Locale(old_loc); |
| 513 | } |
| 514 | if (default_locale) { |
| 515 | default_locale->getBcp47Locale(new_loc); |
| 516 | } |
| 517 | LOG(INFO) << "AssetManager2(" << this << ") default locale changing from '" |
| 518 | << old_loc << "' to '" << new_loc << "'"; |
| 519 | } |
| 520 | default_locale_ = default_locale; |
| 521 | } |
| 522 | |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 523 | void AssetManager2::SetOverlayConstraints(int32_t display_id, int32_t device_id) { |
| 524 | bool changed = false; |
| 525 | if (display_id_ != display_id) { |
| 526 | display_id_ = display_id; |
| 527 | changed = true; |
| 528 | } |
| 529 | if (device_id_ != device_id) { |
| 530 | device_id_ = device_id; |
| 531 | changed = true; |
| 532 | } |
| 533 | if (changed) { |
| 534 | // Enable/disable overlays based on current constraints |
| 535 | for (PackageGroup& group : package_groups_) { |
| 536 | for (auto &overlay: group.overlays_) { |
| 537 | overlay.enabled = IsAnyOverlayConstraintSatisfied( |
| 538 | overlay.overlay_res_maps_.GetConstraints()); |
| 539 | } |
| 540 | } |
| 541 | InvalidateCaches(static_cast<uint32_t>(-1)); |
| 542 | } |
| 543 | } |
| 544 | |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 545 | std::set<AssetManager2::ApkAssetsPtr> AssetManager2::GetNonSystemOverlays() const { |
| 546 | std::set<ApkAssetsPtr> non_system_overlays; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 547 | for (const PackageGroup& package_group : package_groups_) { |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 548 | bool found_system_package = false; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 549 | for (const ConfiguredPackage& package : package_group.packages_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 550 | if (package.loaded_package_->IsSystem()) { |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 551 | found_system_package = true; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 552 | break; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | if (!found_system_package) { |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 557 | auto op = StartOperation(); |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 558 | // Return all overlays, including the disabled ones as this is used for static info |
| 559 | // collection only. |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 560 | for (const ConfiguredOverlay& overlay : package_group.overlays_) { |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 561 | if (const auto& asset = GetApkAssets(overlay.cookie)) { |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 562 | non_system_overlays.insert(std::move(asset)); |
| 563 | } |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | return non_system_overlays; |
| 569 | } |
| 570 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 571 | base::expected<std::set<ResTable_config>, IOError> AssetManager2::GetResourceConfigurations( |
| 572 | bool exclude_system, bool exclude_mipmap) const { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 573 | ATRACE_NAME("AssetManager::GetResourceConfigurations"); |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 574 | auto op = StartOperation(); |
| 575 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 576 | const auto non_system_overlays = |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 577 | exclude_system ? GetNonSystemOverlays() : std::set<ApkAssetsPtr>(); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 578 | |
| 579 | std::set<ResTable_config> configurations; |
| 580 | for (const PackageGroup& package_group : package_groups_) { |
| 581 | for (size_t i = 0; i < package_group.packages_.size(); i++) { |
| 582 | const ConfiguredPackage& package = package_group.packages_[i]; |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 583 | if (exclude_system) { |
| 584 | if (package.loaded_package_->IsSystem()) { |
| 585 | continue; |
| 586 | } |
| 587 | if (!non_system_overlays.empty()) { |
| 588 | // Exclude overlays that target only system resources. |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 589 | const auto& apk_assets = GetApkAssets(package_group.cookies_[i]); |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 590 | if (apk_assets && apk_assets->IsOverlay() && |
| 591 | non_system_overlays.find(apk_assets) == non_system_overlays.end()) { |
| 592 | continue; |
| 593 | } |
| 594 | } |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 597 | auto result = package.loaded_package_->CollectConfigurations(exclude_mipmap, &configurations); |
| 598 | if (UNLIKELY(!result.has_value())) { |
| 599 | return base::unexpected(result.error()); |
| 600 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | return configurations; |
| 604 | } |
| 605 | |
Yurii Zubrytskyi | 984a84e | 2025-03-20 12:17:05 -0700 | [diff] [blame] | 606 | LoadedPackage::Locales AssetManager2::GetResourceLocales( |
| 607 | bool exclude_system, bool merge_equivalent_languages) const { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 608 | ATRACE_NAME("AssetManager::GetResourceLocales"); |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 609 | auto op = StartOperation(); |
| 610 | |
Yurii Zubrytskyi | 984a84e | 2025-03-20 12:17:05 -0700 | [diff] [blame] | 611 | LoadedPackage::Locales locales; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 612 | const auto non_system_overlays = |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 613 | exclude_system ? GetNonSystemOverlays() : std::set<ApkAssetsPtr>(); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 614 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 615 | for (const PackageGroup& package_group : package_groups_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 616 | for (size_t i = 0; i < package_group.packages_.size(); i++) { |
| 617 | const ConfiguredPackage& package = package_group.packages_[i]; |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 618 | if (exclude_system) { |
| 619 | if (package.loaded_package_->IsSystem()) { |
| 620 | continue; |
| 621 | } |
| 622 | if (!non_system_overlays.empty()) { |
| 623 | // Exclude overlays that target only system resources. |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 624 | const auto& apk_assets = GetApkAssets(package_group.cookies_[i]); |
Yurii Zubrytskyi | 984a84e | 2025-03-20 12:17:05 -0700 | [diff] [blame] | 625 | if (apk_assets && apk_assets->IsOverlay() && !non_system_overlays.contains(apk_assets)) { |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 626 | continue; |
| 627 | } |
| 628 | } |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 631 | package.loaded_package_->CollectLocales(merge_equivalent_languages, &locales); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | return locales; |
| 635 | } |
| 636 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 637 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, |
| 638 | Asset::AccessMode mode) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 639 | const std::string new_path = "assets/" + filename; |
| 640 | return OpenNonAsset(new_path, mode); |
| 641 | } |
| 642 | |
| 643 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 644 | Asset::AccessMode mode) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 645 | const std::string new_path = "assets/" + filename; |
| 646 | return OpenNonAsset(new_path, cookie, mode); |
| 647 | } |
| 648 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 649 | std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) const { |
| 650 | ATRACE_NAME("AssetManager::OpenDir"); |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 651 | auto op = StartOperation(); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 652 | |
| 653 | std::string full_path = "assets/" + dirname; |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 654 | auto files = util::make_unique<SortedVector<AssetDir::FileInfo>>(); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 655 | |
| 656 | // Start from the back. |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 657 | for (size_t i = apk_assets_.size(); i > 0; --i) { |
| 658 | const auto& apk_assets = GetApkAssets(i - 1); |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 659 | if (!apk_assets || apk_assets->IsOverlay()) { |
MÃ¥rten Kongstad | dbf343b | 2019-02-21 07:54:18 +0100 | [diff] [blame] | 660 | continue; |
| 661 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 662 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 663 | auto func = [&](StringPiece name, FileType type) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 664 | AssetDir::FileInfo info; |
| 665 | info.setFileName(String8(name.data(), name.size())); |
| 666 | info.setFileType(type); |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 667 | info.setSourceName(String8(apk_assets->GetDebugName().c_str())); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 668 | files->add(info); |
| 669 | }; |
| 670 | |
Ryan Mitchell | c07aa70 | 2020-03-10 13:49:12 -0700 | [diff] [blame] | 671 | if (!apk_assets->GetAssetsProvider()->ForEachFile(full_path, func)) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 672 | return {}; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>(); |
| 677 | asset_dir->setFileList(files.release()); |
| 678 | return asset_dir; |
| 679 | } |
| 680 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 681 | // Search in reverse because that's how we used to do it and we need to preserve behaviour. |
| 682 | // This is unfortunate, because ClassLoaders delegate to the parent first, so the order |
| 683 | // is inconsistent for split APKs. |
| 684 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
| 685 | Asset::AccessMode mode, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 686 | ApkAssetsCookie* out_cookie) const { |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 687 | auto op = StartOperation(); |
| 688 | for (size_t i = apk_assets_.size(); i > 0; i--) { |
| 689 | const auto& assets = GetApkAssets(i - 1); |
MÃ¥rten Kongstad | dbf343b | 2019-02-21 07:54:18 +0100 | [diff] [blame] | 690 | // Prevent RRO from modifying assets and other entries accessed by file |
| 691 | // path. Explicitly asking for a path in a given package (denoted by a |
| 692 | // cookie) is still OK. |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 693 | if (!assets || assets->IsOverlay()) { |
MÃ¥rten Kongstad | dbf343b | 2019-02-21 07:54:18 +0100 | [diff] [blame] | 694 | continue; |
| 695 | } |
| 696 | |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 697 | std::unique_ptr<Asset> asset = assets->GetAssetsProvider()->Open(filename, mode); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 698 | if (asset) { |
| 699 | if (out_cookie != nullptr) { |
Yurii Zubrytskyi | f48a56f | 2023-11-21 08:15:13 -0800 | [diff] [blame] | 700 | *out_cookie = i - 1; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 701 | } |
| 702 | return asset; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | if (out_cookie != nullptr) { |
| 707 | *out_cookie = kInvalidCookie; |
| 708 | } |
| 709 | return {}; |
| 710 | } |
| 711 | |
| 712 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 713 | ApkAssetsCookie cookie, |
| 714 | Asset::AccessMode mode) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 715 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 716 | return {}; |
| 717 | } |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 718 | auto op = StartOperation(); |
| 719 | const auto& assets = GetApkAssets(cookie); |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 720 | return assets ? assets->GetAssetsProvider()->Open(filename, mode) : nullptr; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 723 | base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntry( |
| 724 | uint32_t resid, uint16_t density_override, bool stop_at_first_match, |
| 725 | bool ignore_configuration) const { |
| 726 | const bool logging_enabled = resource_resolution_logging_enabled_; |
| 727 | if (UNLIKELY(logging_enabled)) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 728 | // Clear the last logged resource resolution. |
| 729 | ResetResourceResolution(); |
| 730 | last_resolution_.resid = resid; |
| 731 | } |
| 732 | |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 733 | auto op = StartOperation(); |
| 734 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 735 | // Retrieve the package group from the package id of the resource id. |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 736 | if (UNLIKELY(!is_valid_resid(resid))) { |
Mark Hansen | b406b0e | 2022-10-14 02:18:37 +0000 | [diff] [blame] | 737 | LOG(ERROR) << base::StringPrintf("Invalid resource ID 0x%08x.", resid); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 738 | return base::unexpected(std::nullopt); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 739 | } |
| 740 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 741 | const uint32_t package_id = get_package_id(resid); |
| 742 | const uint8_t type_idx = get_type_id(resid) - 1; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 743 | const uint16_t entry_idx = get_entry_id(resid); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 744 | uint8_t package_idx = package_ids_[package_id]; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 745 | if (UNLIKELY(package_idx == 0xff)) { |
Mark Hansen | b406b0e | 2022-10-14 02:18:37 +0000 | [diff] [blame] | 746 | ANDROID_LOG(ERROR) << base::StringPrintf("No package ID %02x found for resource ID 0x%08x.", |
Ryan Mitchell | 2fe2347 | 2019-02-27 09:43:01 -0800 | [diff] [blame] | 747 | package_id, resid); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 748 | return base::unexpected(std::nullopt); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 749 | } |
| 750 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 751 | const PackageGroup& package_group = package_groups_[package_idx]; |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 752 | std::optional<FindEntryResult> final_result; |
| 753 | bool final_has_locale = false; |
| 754 | bool final_overlaid = false; |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 755 | for (auto& config : configurations_) { |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 756 | // Might use this if density_override != 0. |
| 757 | ResTable_config density_override_config; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 758 | |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 759 | // Select our configuration or generate a density override configuration. |
| 760 | const ResTable_config* desired_config = &config; |
| 761 | if (density_override != 0 && density_override != config.density) { |
| 762 | density_override_config = config; |
| 763 | density_override_config.density = density_override; |
| 764 | desired_config = &density_override_config; |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 765 | } |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 766 | |
| 767 | auto result = FindEntryInternal(package_group, type_idx, entry_idx, *desired_config, |
| 768 | stop_at_first_match, ignore_configuration); |
| 769 | if (UNLIKELY(!result.has_value())) { |
| 770 | return base::unexpected(result.error()); |
| 771 | } |
| 772 | bool overlaid = false; |
| 773 | if (!stop_at_first_match && !ignore_configuration) { |
| 774 | const auto& assets = GetApkAssets(result->cookie); |
| 775 | if (!assets) { |
| 776 | ALOGE("Found expired ApkAssets #%d for resource ID 0x%08x.", result->cookie, resid); |
| 777 | return base::unexpected(std::nullopt); |
| 778 | } |
| 779 | if (!assets->IsLoader()) { |
| 780 | for (const auto& id_map : package_group.overlays_) { |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 781 | auto overlay_entry = id_map.enabled ? |
| 782 | id_map.overlay_res_maps_.Lookup(resid) : IdmapResMap::Result(); |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 783 | if (!overlay_entry) { |
| 784 | // No id map entry exists for this target resource. |
Jeremy Meyer | 04cf00d | 2023-07-20 22:17:27 +0000 | [diff] [blame] | 785 | continue; |
| 786 | } |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 787 | if (overlay_entry.IsInlineValue()) { |
| 788 | // The target resource is overlaid by an inline value not represented by a resource. |
| 789 | ConfigDescription best_frro_config; |
| 790 | Res_value best_frro_value; |
| 791 | bool frro_found = false; |
Jeremy Meyer | e00958a | 2025-02-19 16:55:19 -0800 | [diff] [blame] | 792 | for(const auto& [config, value] : overlay_entry.GetInlineValue()) { |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 793 | if ((!frro_found || config.isBetterThan(best_frro_config, desired_config)) |
| 794 | && config.match(*desired_config)) { |
| 795 | frro_found = true; |
| 796 | best_frro_config = config; |
| 797 | best_frro_value = value; |
| 798 | } |
| 799 | } |
| 800 | if (!frro_found) { |
| 801 | continue; |
| 802 | } |
| 803 | result->entry = best_frro_value; |
| 804 | result->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable(); |
| 805 | result->cookie = id_map.cookie; |
| 806 | |
| 807 | if (UNLIKELY(logging_enabled)) { |
| 808 | last_resolution_.steps.push_back(Resolution::Step{ |
| 809 | Resolution::Step::Type::OVERLAID_INLINE, result->cookie, String8()}); |
| 810 | if (auto path = assets->GetPath()) { |
| 811 | const std::string overlay_path = path->data(); |
| 812 | if (IsFabricatedOverlay(overlay_path)) { |
| 813 | // FRRO don't have package name so we use the creating package here. |
| 814 | String8 frro_name = String8("FRRO"); |
| 815 | // Get the first part of it since the expected one should be like |
| 816 | // {overlayPackageName}-{overlayName}-{4 alphanumeric chars}.frro |
| 817 | // under /data/resource-cache/. |
| 818 | const std::string name = overlay_path.substr(overlay_path.rfind('/') + 1); |
| 819 | const size_t end = name.find('-'); |
| 820 | if (frro_name.size() != overlay_path.size() && end != std::string::npos) { |
| 821 | frro_name.append(base::StringPrintf(" created by %s", |
| 822 | name.substr(0 /* pos */, |
| 823 | end).c_str()).c_str()); |
| 824 | } |
| 825 | last_resolution_.best_package_name = frro_name; |
| 826 | } else { |
| 827 | last_resolution_.best_package_name = result->package_name->c_str(); |
| 828 | } |
| 829 | } |
| 830 | overlaid = true; |
| 831 | } |
| 832 | continue; |
| 833 | } |
| 834 | |
| 835 | auto overlay_result = FindEntry(overlay_entry.GetResourceId(), density_override, |
| 836 | false /* stop_at_first_match */, |
| 837 | false /* ignore_configuration */); |
| 838 | if (UNLIKELY(IsIOError(overlay_result))) { |
| 839 | return base::unexpected(overlay_result.error()); |
| 840 | } |
| 841 | if (!overlay_result.has_value()) { |
| 842 | continue; |
| 843 | } |
| 844 | |
| 845 | if (!overlay_result->config.isBetterThan(result->config, desired_config) |
| 846 | && overlay_result->config.compare(result->config) != 0) { |
| 847 | // The configuration of the entry for the overlay must be equal to or better than the |
| 848 | // target configuration to be chosen as the better value. |
| 849 | continue; |
| 850 | } |
| 851 | |
| 852 | result->cookie = overlay_result->cookie; |
| 853 | result->entry = overlay_result->entry; |
| 854 | result->config = overlay_result->config; |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 855 | result->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable(); |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 856 | |
| 857 | if (UNLIKELY(logging_enabled)) { |
| 858 | last_resolution_.steps.push_back( |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 859 | Resolution::Step{Resolution::Step::Type::OVERLAID, overlay_result->cookie, |
| 860 | overlay_result->config.toString()}); |
| 861 | last_resolution_.best_package_name = |
| 862 | overlay_result->package_name->c_str(); |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 863 | overlaid = true; |
| 864 | } |
Ryan Mitchell | bdc0ae1 | 2021-03-01 15:18:15 -0800 | [diff] [blame] | 865 | } |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 866 | } |
| 867 | } |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 868 | |
| 869 | bool has_locale = false; |
| 870 | if (result->config.locale == 0) { |
Jeremy Meyer | e00958a | 2025-02-19 16:55:19 -0800 | [diff] [blame] | 871 | // The default_locale_ is the locale used for any resources with no locale in the config |
| 872 | if (default_locale_) { |
| 873 | // Since we know default_locale_ has a locale and only a locale, match will tell us if that |
| 874 | // locale matches |
| 875 | has_locale = default_locale_->match(config); |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 876 | } |
| 877 | } else { |
| 878 | has_locale = true; |
| 879 | } |
| 880 | |
Jeremy Meyer | 2ae6ed59 | 2023-09-13 12:39:53 -0700 | [diff] [blame] | 881 | // if we don't have a result yet |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 882 | if (!final_result || |
| 883 | // or this config is better before the locale than the existing result |
Yurii Zubrytskyi | 2e609b1 | 2025-03-17 17:00:04 -0700 | [diff] [blame] | 884 | result->config.isBetterThanBeforeLocale(final_result->config, *desired_config) || |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 885 | // or the existing config isn't better before locale and this one specifies a locale |
| 886 | // whereas the existing one doesn't |
Yurii Zubrytskyi | 2e609b1 | 2025-03-17 17:00:04 -0700 | [diff] [blame] | 887 | (!final_result->config.isBetterThanBeforeLocale(result->config, *desired_config) |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 888 | && has_locale && !final_has_locale)) { |
| 889 | final_result = result.value(); |
| 890 | final_overlaid = overlaid; |
| 891 | final_has_locale = has_locale; |
| 892 | } |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 895 | if (UNLIKELY(logging_enabled)) { |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 896 | last_resolution_.cookie = final_result->cookie; |
| 897 | last_resolution_.type_string_ref = final_result->type_string_ref; |
| 898 | last_resolution_.entry_string_ref = final_result->entry_string_ref; |
| 899 | last_resolution_.best_config_name = final_result->config.toString(); |
| 900 | if (!final_overlaid) { |
| 901 | last_resolution_.best_package_name = final_result->package_name->c_str(); |
Jackal Guo | 552b45d | 2021-09-29 10:52:19 +0800 | [diff] [blame] | 902 | } |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 905 | return *final_result; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 908 | base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntryInternal( |
| 909 | const PackageGroup& package_group, uint8_t type_idx, uint16_t entry_idx, |
| 910 | const ResTable_config& desired_config, bool stop_at_first_match, |
| 911 | bool ignore_configuration) const { |
| 912 | const bool logging_enabled = resource_resolution_logging_enabled_; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 913 | ApkAssetsCookie best_cookie = kInvalidCookie; |
| 914 | const LoadedPackage* best_package = nullptr; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 915 | incfs::verified_map_ptr<ResTable_type> best_type; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 916 | const ResTable_config* best_config = nullptr; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 917 | uint32_t best_offset = 0U; |
| 918 | uint32_t type_flags = 0U; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 919 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 920 | // If `desired_config` is not the same as the set configuration or the caller will accept a value |
| 921 | // from any configuration, then we cannot use our filtered list of types since it only it contains |
| 922 | // types matched to the set configuration. |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 923 | const bool use_filtered = !ignore_configuration && std::find_if( |
| 924 | configurations_.begin(), configurations_.end(), |
| 925 | [&desired_config](auto& value) { return &desired_config == &value; }) |
| 926 | != configurations_.end(); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 927 | const size_t package_count = package_group.packages_.size(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 928 | for (size_t pi = 0; pi < package_count; pi++) { |
| 929 | const ConfiguredPackage& loaded_package_impl = package_group.packages_[pi]; |
| 930 | const LoadedPackage* loaded_package = loaded_package_impl.loaded_package_; |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 931 | const ApkAssetsCookie cookie = package_group.cookies_[pi]; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 932 | |
| 933 | // If the type IDs are offset in this package, we need to take that into account when searching |
| 934 | // for a type. |
| 935 | const TypeSpec* type_spec = loaded_package->GetTypeSpecByTypeIndex(type_idx); |
| 936 | if (UNLIKELY(type_spec == nullptr)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 937 | continue; |
| 938 | } |
| 939 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 940 | // Allow custom loader packages to overlay resource values with configurations equivalent to the |
| 941 | // current best configuration. |
| 942 | const bool package_is_loader = loaded_package->IsCustomLoader(); |
| 943 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 944 | auto entry_flags = type_spec->GetFlagsForEntryIndex(entry_idx); |
Bernie Innocenti | 58cf8e3 | 2020-12-19 15:31:52 +0900 | [diff] [blame] | 945 | if (UNLIKELY(!entry_flags.has_value())) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 946 | return base::unexpected(entry_flags.error()); |
| 947 | } |
| 948 | type_flags |= entry_flags.value(); |
| 949 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 950 | const FilteredConfigGroup& filtered_group = loaded_package_impl.filtered_configs_[type_idx]; |
| 951 | const size_t type_entry_count = (use_filtered) ? filtered_group.type_entries.size() |
| 952 | : type_spec->type_entries.size(); |
| 953 | for (size_t i = 0; i < type_entry_count; i++) { |
| 954 | const TypeSpec::TypeEntry* type_entry = (use_filtered) ? filtered_group.type_entries[i] |
| 955 | : &type_spec->type_entries[i]; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 956 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 957 | // We can skip calling ResTable_config::match() if the caller does not care for the |
| 958 | // configuration to match or if we're using the list of types that have already had their |
Jeremy Meyer | 2ae6ed59 | 2023-09-13 12:39:53 -0700 | [diff] [blame] | 959 | // configuration matched. The exception to this is when the user has multiple locales set |
| 960 | // because the filtered list will then have values from multiple locales and we will need to |
| 961 | // call match() to make sure the current entry matches the config we are currently checking. |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 962 | const ResTable_config& this_config = type_entry->config; |
Jeremy Meyer | 2ae6ed59 | 2023-09-13 12:39:53 -0700 | [diff] [blame] | 963 | if (!((use_filtered && (configurations_.size() == 1)) |
| 964 | || ignore_configuration || this_config.match(desired_config))) { |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 965 | continue; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 966 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 967 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 968 | Resolution::Step::Type resolution_type; |
| 969 | if (best_config == nullptr) { |
| 970 | resolution_type = Resolution::Step::Type::INITIAL; |
| 971 | } else if (this_config.isBetterThan(*best_config, &desired_config)) { |
| 972 | resolution_type = Resolution::Step::Type::BETTER_MATCH; |
| 973 | } else if (package_is_loader && this_config.compare(*best_config) == 0) { |
| 974 | resolution_type = Resolution::Step::Type::OVERLAID; |
| 975 | } else { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 976 | if (UNLIKELY(logging_enabled)) { |
Jackal Guo | 552b45d | 2021-09-29 10:52:19 +0800 | [diff] [blame] | 977 | last_resolution_.steps.push_back(Resolution::Step{Resolution::Step::Type::SKIPPED, |
Yurii Zubrytskyi | ff9cba7 | 2023-03-29 12:53:51 -0700 | [diff] [blame] | 978 | cookie, this_config.toString()}); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 979 | } |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 980 | continue; |
| 981 | } |
| 982 | |
| 983 | // The configuration matches and is better than the previous selection. |
| 984 | // Find the entry value if it exists for this configuration. |
| 985 | const auto& type = type_entry->type; |
| 986 | const auto offset = LoadedPackage::GetEntryOffset(type, entry_idx); |
| 987 | if (UNLIKELY(IsIOError(offset))) { |
| 988 | return base::unexpected(offset.error()); |
| 989 | } |
| 990 | |
| 991 | if (!offset.has_value()) { |
| 992 | if (UNLIKELY(logging_enabled)) { |
Jackal Guo | 552b45d | 2021-09-29 10:52:19 +0800 | [diff] [blame] | 993 | last_resolution_.steps.push_back(Resolution::Step{Resolution::Step::Type::NO_ENTRY, |
Yurii Zubrytskyi | ff9cba7 | 2023-03-29 12:53:51 -0700 | [diff] [blame] | 994 | cookie, this_config.toString()}); |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 995 | } |
| 996 | continue; |
| 997 | } |
| 998 | |
| 999 | best_cookie = cookie; |
| 1000 | best_package = loaded_package; |
| 1001 | best_type = type; |
| 1002 | best_config = &this_config; |
| 1003 | best_offset = offset.value(); |
| 1004 | |
| 1005 | if (UNLIKELY(logging_enabled)) { |
| 1006 | last_resolution_.steps.push_back(Resolution::Step{resolution_type, |
Yurii Zubrytskyi | ff9cba7 | 2023-03-29 12:53:51 -0700 | [diff] [blame] | 1007 | cookie, this_config.toString()}); |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | // Any configuration will suffice, so break. |
| 1011 | if (stop_at_first_match) { |
| 1012 | break; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1017 | if (UNLIKELY(best_cookie == kInvalidCookie)) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1018 | return base::unexpected(std::nullopt); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Eric Miao | 368cd19 | 2022-09-09 15:46:14 -0700 | [diff] [blame] | 1021 | auto best_entry_verified = LoadedPackage::GetEntryFromOffset(best_type, best_offset); |
| 1022 | if (!best_entry_verified.has_value()) { |
| 1023 | return base::unexpected(best_entry_verified.error()); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
Eric Miao | 368cd19 | 2022-09-09 15:46:14 -0700 | [diff] [blame] | 1026 | const auto entry = GetEntryValue(*best_entry_verified); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1027 | if (!entry.has_value()) { |
| 1028 | return base::unexpected(entry.error()); |
| 1029 | } |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1030 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1031 | return FindEntryResult{ |
| 1032 | .cookie = best_cookie, |
| 1033 | .entry = *entry, |
| 1034 | .config = *best_config, |
| 1035 | .type_flags = type_flags, |
Jeremy Meyer | b76f745 | 2025-03-12 10:55:53 -0700 | [diff] [blame] | 1036 | .entry_flags = (*best_entry_verified)->flags(), |
Tomasz Wasilczyk | 9e039b9 | 2023-06-29 12:08:24 -0700 | [diff] [blame] | 1037 | .dynamic_ref_table = package_group.dynamic_ref_table.get(), |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1038 | .package_name = &best_package->GetPackageName(), |
| 1039 | .type_string_ref = StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1), |
| 1040 | .entry_string_ref = StringPoolRef(best_package->GetKeyStringPool(), |
Eric Miao | 368cd19 | 2022-09-09 15:46:14 -0700 | [diff] [blame] | 1041 | (*best_entry_verified)->key()), |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1042 | }; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 1045 | void AssetManager2::ResetResourceResolution() const { |
Yurii Zubrytskyi | ff9cba7 | 2023-03-29 12:53:51 -0700 | [diff] [blame] | 1046 | last_resolution_ = Resolution{}; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1049 | void AssetManager2::SetResourceResolutionLoggingEnabled(bool enabled) { |
| 1050 | resource_resolution_logging_enabled_ = enabled; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1051 | if (!enabled) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 1052 | ResetResourceResolution(); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | std::string AssetManager2::GetLastResourceResolution() const { |
| 1057 | if (!resource_resolution_logging_enabled_) { |
| 1058 | LOG(ERROR) << "Must enable resource resolution logging before getting path."; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1059 | return {}; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 1062 | const ApkAssetsCookie cookie = last_resolution_.cookie; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1063 | if (cookie == kInvalidCookie) { |
| 1064 | LOG(ERROR) << "AssetManager hasn't resolved a resource to read resolution path."; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1065 | return {}; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1066 | } |
| 1067 | |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 1068 | auto op = StartOperation(); |
| 1069 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 1070 | const uint32_t resid = last_resolution_.resid; |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 1071 | const auto& assets = GetApkAssets(cookie); |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 1072 | const auto package = |
| 1073 | assets ? assets->GetLoadedArsc()->GetPackageById(get_package_id(resid)) : nullptr; |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 1074 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1075 | std::string resource_name_string; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1076 | if (package != nullptr) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1077 | auto resource_name = ToResourceName(last_resolution_.type_string_ref, |
| 1078 | last_resolution_.entry_string_ref, |
| 1079 | package->GetPackageName()); |
| 1080 | resource_name_string = resource_name.has_value() ? |
| 1081 | ToFormattedResourceString(resource_name.value()) : "<unknown>"; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | std::stringstream log_stream; |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 1085 | if (configurations_.size() == 1) { |
| 1086 | log_stream << base::StringPrintf("Resolution for 0x%08x %s\n" |
| 1087 | "\tFor config - %s", resid, resource_name_string.c_str(), |
| 1088 | configurations_[0].toString().c_str()); |
| 1089 | } else { |
| 1090 | ResTable_config conf = configurations_[0]; |
| 1091 | conf.clearLocale(); |
| 1092 | log_stream << base::StringPrintf("Resolution for 0x%08x %s\n\tFor config - %s and locales", |
| 1093 | resid, resource_name_string.c_str(), conf.toString().c_str()); |
| 1094 | char str[40]; |
| 1095 | str[0] = '\0'; |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 1096 | for (auto iter = configurations_.begin(); iter < configurations_.end(); iter++) { |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 1097 | iter->getBcp47Locale(str); |
| 1098 | log_stream << base::StringPrintf(" %s%s", str, iter < configurations_.end() ? "," : ""); |
| 1099 | } |
| 1100 | } |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 1101 | for (const Resolution::Step& step : last_resolution_.steps) { |
Yurii Zubrytskyi | ff9cba7 | 2023-03-29 12:53:51 -0700 | [diff] [blame] | 1102 | constexpr static std::array kStepStrings = { |
| 1103 | "Found initial", |
| 1104 | "Found better", |
| 1105 | "Overlaid", |
| 1106 | "Overlaid inline", |
| 1107 | "Skipped", |
| 1108 | "No entry" |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 1109 | }; |
| 1110 | |
Yurii Zubrytskyi | ff9cba7 | 2023-03-29 12:53:51 -0700 | [diff] [blame] | 1111 | if (step.type < Resolution::Step::Type::INITIAL |
| 1112 | || step.type > Resolution::Step::Type::NO_ENTRY) { |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 1113 | continue; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1114 | } |
Yurii Zubrytskyi | ff9cba7 | 2023-03-29 12:53:51 -0700 | [diff] [blame] | 1115 | const auto prefix = kStepStrings[int(step.type) - int(Resolution::Step::Type::INITIAL)]; |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 1116 | const auto& assets = GetApkAssets(step.cookie); |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 1117 | log_stream << "\n\t" << prefix << ": " << (assets ? assets->GetDebugName() : "<null>") |
| 1118 | << " #" << step.cookie; |
Tomasz Wasilczyk | 8f74b2a | 2023-08-24 19:02:33 +0000 | [diff] [blame] | 1119 | if (!step.config_name.empty()) { |
Ryan Mitchell | bdc0ae1 | 2021-03-01 15:18:15 -0800 | [diff] [blame] | 1120 | log_stream << " - " << step.config_name; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | |
Jackal Guo | 552b45d | 2021-09-29 10:52:19 +0800 | [diff] [blame] | 1124 | log_stream << "\nBest matching is from " |
Tomasz Wasilczyk | 8f74b2a | 2023-08-24 19:02:33 +0000 | [diff] [blame] | 1125 | << (last_resolution_.best_config_name.empty() ? "default" |
Tomasz Wasilczyk | 835dfe5 | 2023-08-17 16:27:22 +0000 | [diff] [blame] | 1126 | : last_resolution_.best_config_name.c_str()) |
Jackal Guo | 552b45d | 2021-09-29 10:52:19 +0800 | [diff] [blame] | 1127 | << " configuration of " << last_resolution_.best_package_name; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 1128 | return log_stream.str(); |
| 1129 | } |
| 1130 | |
Felka Chang | 00964e9 | 2021-12-10 01:19:08 +0800 | [diff] [blame] | 1131 | base::expected<uint32_t, NullOrIOError> AssetManager2::GetParentThemeResourceId(uint32_t resid) |
| 1132 | const { |
| 1133 | auto entry = FindEntry(resid, 0u /* density_override */, |
| 1134 | false /* stop_at_first_match */, |
| 1135 | false /* ignore_configuration */); |
| 1136 | if (!entry.has_value()) { |
| 1137 | return base::unexpected(entry.error()); |
| 1138 | } |
| 1139 | |
| 1140 | auto entry_map = std::get_if<incfs::verified_map_ptr<ResTable_map_entry>>(&entry->entry); |
| 1141 | if (entry_map == nullptr) { |
| 1142 | // Not a bag, nothing to do. |
| 1143 | return base::unexpected(std::nullopt); |
| 1144 | } |
| 1145 | |
| 1146 | auto map = *entry_map; |
| 1147 | const uint32_t parent_resid = dtohl(map->parent.ident); |
| 1148 | |
| 1149 | return parent_resid; |
| 1150 | } |
| 1151 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1152 | base::expected<AssetManager2::ResourceName, NullOrIOError> AssetManager2::GetResourceName( |
| 1153 | uint32_t resid) const { |
| 1154 | auto result = FindEntry(resid, 0u /* density_override */, true /* stop_at_first_match */, |
| 1155 | true /* ignore_configuration */); |
| 1156 | if (!result.has_value()) { |
| 1157 | return base::unexpected(result.error()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1160 | return ToResourceName(result->type_string_ref, |
| 1161 | result->entry_string_ref, |
| 1162 | *result->package_name); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 1165 | base::expected<uint32_t, NullOrIOError> AssetManager2::GetResourceTypeSpecFlags( |
| 1166 | uint32_t resid) const { |
| 1167 | auto result = FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, |
| 1168 | true /* ignore_configuration */); |
| 1169 | if (!result.has_value()) { |
| 1170 | return base::unexpected(result.error()); |
| 1171 | } |
| 1172 | return result->type_flags; |
| 1173 | } |
| 1174 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1175 | base::expected<AssetManager2::SelectedValue, NullOrIOError> AssetManager2::GetResource( |
| 1176 | uint32_t resid, bool may_be_bag, uint16_t density_override) const { |
| 1177 | auto result = FindEntry(resid, density_override, false /* stop_at_first_match */, |
| 1178 | false /* ignore_configuration */); |
| 1179 | if (!result.has_value()) { |
| 1180 | return base::unexpected(result.error()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1183 | 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] | 1184 | if (result_map_entry != nullptr) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1185 | if (!may_be_bag) { |
| 1186 | LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1187 | return base::unexpected(std::nullopt); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1188 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 1189 | |
| 1190 | // Create a reference since we can't represent this complex type as a Res_value. |
Jeremy Meyer | b76f745 | 2025-03-12 10:55:53 -0700 | [diff] [blame] | 1191 | return SelectedValue(Res_value::TYPE_REFERENCE, resid, result->cookie, result->entry_flags, |
| 1192 | result->type_flags, resid, result->config); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1195 | // Convert the package ID to the runtime assigned package ID. |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1196 | Res_value value = std::get<Res_value>(result->entry); |
| 1197 | result->dynamic_ref_table->lookupResourceValue(&value); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1198 | |
Jeremy Meyer | b76f745 | 2025-03-12 10:55:53 -0700 | [diff] [blame] | 1199 | return SelectedValue(value.dataType, value.data, result->cookie, result->entry_flags, |
| 1200 | result->type_flags, resid, result->config); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1203 | base::expected<std::monostate, NullOrIOError> AssetManager2::ResolveReference( |
Ryan Mitchell | a45506e | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1204 | AssetManager2::SelectedValue& value, bool cache_value) const { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1205 | if (value.type != Res_value::TYPE_REFERENCE || value.data == 0U) { |
| 1206 | // Not a reference. Nothing to do. |
| 1207 | return {}; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 1208 | } |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1209 | |
Ryan Mitchell | a45506e | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1210 | const uint32_t original_flags = value.flags; |
| 1211 | const uint32_t original_resid = value.data; |
| 1212 | if (cache_value) { |
| 1213 | auto cached_value = cached_resolved_values_.find(value.data); |
| 1214 | if (cached_value != cached_resolved_values_.end()) { |
| 1215 | value = cached_value->second; |
| 1216 | value.flags |= original_flags; |
| 1217 | return {}; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | uint32_t combined_flags = 0U; |
| 1222 | uint32_t resolve_resid = original_resid; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1223 | constexpr const uint32_t kMaxIterations = 20; |
| 1224 | for (uint32_t i = 0U;; i++) { |
| 1225 | auto result = GetResource(resolve_resid, true /*may_be_bag*/); |
| 1226 | if (!result.has_value()) { |
Ryan Mitchell | e7ab627 | 2020-11-13 18:06:15 -0800 | [diff] [blame] | 1227 | value.resid = resolve_resid; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1228 | return base::unexpected(result.error()); |
| 1229 | } |
| 1230 | |
Ryan Mitchell | e7ab627 | 2020-11-13 18:06:15 -0800 | [diff] [blame] | 1231 | // If resource resolution fails, the value should be set to the last reference that was able to |
| 1232 | // be resolved successfully. |
| 1233 | value = *result; |
| 1234 | value.flags |= combined_flags; |
| 1235 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1236 | if (result->type != Res_value::TYPE_REFERENCE || |
| 1237 | result->data == Res_value::DATA_NULL_UNDEFINED || |
| 1238 | result->data == resolve_resid || i == kMaxIterations) { |
| 1239 | // 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] | 1240 | if (cache_value) { |
| 1241 | cached_resolved_values_[original_resid] = value; |
| 1242 | } |
| 1243 | |
| 1244 | // Above value is cached without original_flags to ensure they don't get included in future |
| 1245 | // queries that hit the cache |
| 1246 | value.flags |= original_flags; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1247 | return {}; |
| 1248 | } |
| 1249 | |
Ryan Mitchell | e7ab627 | 2020-11-13 18:06:15 -0800 | [diff] [blame] | 1250 | combined_flags = result->flags; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1251 | resolve_resid = result->data; |
| 1252 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
Yurii Zubrytskyi | 02a555f | 2023-05-10 13:22:55 -0700 | [diff] [blame] | 1255 | base::expected<const std::vector<uint32_t>*, NullOrIOError> AssetManager2::GetBagResIdStack( |
| 1256 | uint32_t resid) const { |
Yurii Zubrytskyi | 0914488 | 2023-06-15 23:23:15 -0700 | [diff] [blame] | 1257 | auto it = cached_bag_resid_stacks_.find(resid); |
| 1258 | if (it != cached_bag_resid_stacks_.end()) { |
| 1259 | return &it->second; |
Aurimas Liutikas | 8f004c8 | 2019-01-17 17:20:10 -0800 | [diff] [blame] | 1260 | } |
Yurii Zubrytskyi | 0914488 | 2023-06-15 23:23:15 -0700 | [diff] [blame] | 1261 | std::vector<uint32_t> stacks; |
| 1262 | if (auto maybe_bag = GetBag(resid, stacks); UNLIKELY(IsIOError(maybe_bag))) { |
| 1263 | return base::unexpected(maybe_bag.error()); |
| 1264 | } |
| 1265 | |
| 1266 | it = cached_bag_resid_stacks_.emplace(resid, std::move(stacks)).first; |
Yurii Zubrytskyi | 02a555f | 2023-05-10 13:22:55 -0700 | [diff] [blame] | 1267 | return &it->second; |
Aurimas Liutikas | 8f004c8 | 2019-01-17 17:20:10 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1270 | base::expected<const ResolvedBag*, NullOrIOError> AssetManager2::ResolveBag( |
| 1271 | AssetManager2::SelectedValue& value) const { |
| 1272 | if (UNLIKELY(value.type != Res_value::TYPE_REFERENCE)) { |
| 1273 | return base::unexpected(std::nullopt); |
| 1274 | } |
Aurimas Liutikas | 8f004c8 | 2019-01-17 17:20:10 -0800 | [diff] [blame] | 1275 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1276 | auto bag = GetBag(value.data); |
| 1277 | if (bag.has_value()) { |
| 1278 | value.flags |= (*bag)->type_spec_flags; |
Aurimas Liutikas | 8f004c8 | 2019-01-17 17:20:10 -0800 | [diff] [blame] | 1279 | } |
| 1280 | return bag; |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1283 | base::expected<const ResolvedBag*, NullOrIOError> AssetManager2::GetBag(uint32_t resid) const { |
Yurii Zubrytskyi | 0914488 | 2023-06-15 23:23:15 -0700 | [diff] [blame] | 1284 | auto resid_stacks_it = cached_bag_resid_stacks_.find(resid); |
Yurii Zubrytskyi | 533e8cc | 2023-06-16 16:38:49 -0700 | [diff] [blame] | 1285 | if (resid_stacks_it == cached_bag_resid_stacks_.end()) { |
Yurii Zubrytskyi | 0914488 | 2023-06-15 23:23:15 -0700 | [diff] [blame] | 1286 | resid_stacks_it = cached_bag_resid_stacks_.emplace(resid, std::vector<uint32_t>{}).first; |
| 1287 | } |
Yurii Zubrytskyi | bd1db5d | 2023-05-23 18:32:47 -0700 | [diff] [blame] | 1288 | const auto bag = GetBag(resid, resid_stacks_it->second); |
| 1289 | if (UNLIKELY(IsIOError(bag))) { |
| 1290 | cached_bag_resid_stacks_.erase(resid_stacks_it); |
| 1291 | return base::unexpected(bag.error()); |
| 1292 | } |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1293 | return bag; |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1294 | } |
| 1295 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1296 | base::expected<const ResolvedBag*, NullOrIOError> AssetManager2::GetBag( |
| 1297 | uint32_t resid, std::vector<uint32_t>& child_resids) const { |
| 1298 | 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] | 1299 | return cached_iter->second.get(); |
| 1300 | } |
| 1301 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1302 | auto entry = FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, |
| 1303 | false /* ignore_configuration */); |
| 1304 | if (!entry.has_value()) { |
| 1305 | return base::unexpected(entry.error()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1308 | auto entry_map = std::get_if<incfs::verified_map_ptr<ResTable_map_entry>>(&entry->entry); |
| 1309 | if (entry_map == nullptr) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1310 | // Not a bag, nothing to do. |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1311 | return base::unexpected(std::nullopt); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1314 | auto map = *entry_map; |
| 1315 | auto map_entry = map.offset(dtohs(map->size)).convert<ResTable_map>(); |
| 1316 | const auto map_entry_end = map_entry + dtohl(map->count); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1317 | |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 1318 | // 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] | 1319 | // dependencies between bags. |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 1320 | child_resids.push_back(resid); |
| 1321 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1322 | uint32_t parent_resid = dtohl(map->parent.ident); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1323 | if (parent_resid == 0U || |
| 1324 | std::find(child_resids.begin(), child_resids.end(), parent_resid) != child_resids.end()) { |
| 1325 | // There is no parent or a circular parental dependency exist, meaning there is nothing to |
| 1326 | // 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] | 1327 | const size_t entry_count = map_entry_end - map_entry; |
| 1328 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 1329 | malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))}; |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1330 | |
| 1331 | bool sort_entries = false; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1332 | for (auto new_entry = new_bag->entries; map_entry != map_entry_end; ++map_entry) { |
| 1333 | if (UNLIKELY(!map_entry)) { |
| 1334 | return base::unexpected(IOError::PAGES_MISSING); |
| 1335 | } |
| 1336 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1337 | uint32_t new_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1338 | if (!is_internal_resid(new_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1339 | // Attributes, arrays, etc don't have a resource id as the name. They specify |
| 1340 | // other data, which would be wrong to change via a lookup. |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1341 | if (UNLIKELY(entry->dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1342 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, |
| 1343 | resid); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1344 | return base::unexpected(std::nullopt); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1345 | } |
| 1346 | } |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1347 | |
| 1348 | new_entry->cookie = entry->cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1349 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1350 | new_entry->key_pool = nullptr; |
| 1351 | new_entry->type_pool = nullptr; |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 1352 | new_entry->style = resid; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1353 | new_entry->value.copyFrom_dtoh(map_entry->value); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1354 | status_t err = entry->dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 1355 | if (UNLIKELY(err != NO_ERROR)) { |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1356 | LOG(ERROR) << base::StringPrintf( |
| 1357 | "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType, |
| 1358 | new_entry->value.data, new_key); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1359 | return base::unexpected(std::nullopt); |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1360 | } |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1361 | |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1362 | sort_entries = sort_entries || |
| 1363 | (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1364 | ++new_entry; |
| 1365 | } |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1366 | |
| 1367 | if (sort_entries) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1368 | std::sort(new_bag->entries, new_bag->entries + entry_count, |
| 1369 | [](auto&& lhs, auto&& rhs) { return lhs.key < rhs.key; }); |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1370 | } |
| 1371 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1372 | new_bag->type_spec_flags = entry->type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1373 | new_bag->entry_count = static_cast<uint32_t>(entry_count); |
| 1374 | ResolvedBag* result = new_bag.get(); |
| 1375 | cached_bags_[resid] = std::move(new_bag); |
| 1376 | return result; |
| 1377 | } |
| 1378 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1379 | // In case the parent is a dynamic reference, resolve it. |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1380 | entry->dynamic_ref_table->lookupResourceId(&parent_resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1381 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1382 | // Get the parent and do a merge of the keys. |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1383 | const auto parent_bag = GetBag(parent_resid, child_resids); |
| 1384 | if (UNLIKELY(!parent_bag.has_value())) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1385 | // Failed to get the parent that should exist. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1386 | LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid, |
| 1387 | resid); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1388 | return base::unexpected(parent_bag.error()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1391 | // Create the max possible entries we can make. Once we construct the bag, |
| 1392 | // we will realloc to fit to size. |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1393 | 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] | 1394 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 1395 | malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1396 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 1397 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1398 | const ResolvedBag::Entry* parent_entry = (*parent_bag)->entries; |
| 1399 | 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] | 1400 | |
| 1401 | // 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] | 1402 | bool sort_entries = false; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1403 | while (map_entry != map_entry_end && parent_entry != parent_entry_end) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1404 | if (UNLIKELY(!map_entry)) { |
| 1405 | return base::unexpected(IOError::PAGES_MISSING); |
| 1406 | } |
| 1407 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1408 | uint32_t child_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1409 | if (!is_internal_resid(child_key)) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1410 | if (UNLIKELY(entry->dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1411 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key, |
| 1412 | resid); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1413 | return base::unexpected(std::nullopt); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1417 | if (child_key <= parent_entry->key) { |
| 1418 | // Use the child key if it comes before the parent |
| 1419 | // or is equal to the parent (overrides). |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1420 | new_entry->cookie = entry->cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1421 | new_entry->key = child_key; |
| 1422 | new_entry->key_pool = nullptr; |
| 1423 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1424 | new_entry->value.copyFrom_dtoh(map_entry->value); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 1425 | new_entry->style = resid; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1426 | status_t err = entry->dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 1427 | if (UNLIKELY(err != NO_ERROR)) { |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1428 | LOG(ERROR) << base::StringPrintf( |
| 1429 | "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType, |
| 1430 | new_entry->value.data, child_key); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1431 | return base::unexpected(std::nullopt); |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1432 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1433 | ++map_entry; |
| 1434 | } else { |
| 1435 | // Take the parent entry as-is. |
| 1436 | memcpy(new_entry, parent_entry, sizeof(*new_entry)); |
| 1437 | } |
| 1438 | |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1439 | sort_entries = sort_entries || |
| 1440 | (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1441 | if (child_key >= parent_entry->key) { |
| 1442 | // Move to the next parent entry if we used it or it was overridden. |
| 1443 | ++parent_entry; |
| 1444 | } |
| 1445 | // Increment to the next entry to fill. |
| 1446 | ++new_entry; |
| 1447 | } |
| 1448 | |
| 1449 | // Finish the child entries if they exist. |
| 1450 | while (map_entry != map_entry_end) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1451 | if (UNLIKELY(!map_entry)) { |
| 1452 | return base::unexpected(IOError::PAGES_MISSING); |
| 1453 | } |
| 1454 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1455 | uint32_t new_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1456 | if (!is_internal_resid(new_key)) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1457 | if (UNLIKELY(entry->dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1458 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, |
| 1459 | resid); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1460 | return base::unexpected(std::nullopt); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1461 | } |
| 1462 | } |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1463 | new_entry->cookie = entry->cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1464 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1465 | new_entry->key_pool = nullptr; |
| 1466 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1467 | new_entry->value.copyFrom_dtoh(map_entry->value); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 1468 | new_entry->style = resid; |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1469 | status_t err = entry->dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 1470 | if (UNLIKELY(err != NO_ERROR)) { |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1471 | LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", |
| 1472 | new_entry->value.dataType, new_entry->value.data, new_key); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1473 | return base::unexpected(std::nullopt); |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1474 | } |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1475 | sort_entries = sort_entries || |
| 1476 | (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1477 | ++map_entry; |
| 1478 | ++new_entry; |
| 1479 | } |
| 1480 | |
| 1481 | // Finish the parent entries if they exist. |
| 1482 | if (parent_entry != parent_entry_end) { |
| 1483 | // Take the rest of the parent entries as-is. |
| 1484 | const size_t num_entries_to_copy = parent_entry_end - parent_entry; |
| 1485 | memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry)); |
| 1486 | new_entry += num_entries_to_copy; |
| 1487 | } |
| 1488 | |
| 1489 | // Resize the resulting array to fit. |
| 1490 | const size_t actual_count = new_entry - new_bag->entries; |
| 1491 | if (actual_count != max_count) { |
George Burgess IV | 09b119f | 2017-07-25 15:00:04 -0700 | [diff] [blame] | 1492 | new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc( |
| 1493 | new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry))))); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1496 | if (sort_entries) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1497 | std::sort(new_bag->entries, new_bag->entries + actual_count, |
| 1498 | [](auto&& lhs, auto&& rhs) { return lhs.key < rhs.key; }); |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1499 | } |
| 1500 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 1501 | // Combine flags from the parent and our own bag. |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1502 | 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] | 1503 | new_bag->entry_count = static_cast<uint32_t>(actual_count); |
| 1504 | ResolvedBag* result = new_bag.get(); |
| 1505 | cached_bags_[resid] = std::move(new_bag); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1506 | return result; |
| 1507 | } |
| 1508 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 1509 | static bool Utf8ToUtf16(StringPiece str, std::u16string* out) { |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1510 | ssize_t len = |
| 1511 | utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false); |
| 1512 | if (len < 0) { |
| 1513 | return false; |
| 1514 | } |
| 1515 | out->resize(static_cast<size_t>(len)); |
| 1516 | utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(), |
| 1517 | static_cast<size_t>(len + 1)); |
| 1518 | return true; |
| 1519 | } |
| 1520 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1521 | base::expected<uint32_t, NullOrIOError> AssetManager2::GetResourceId( |
| 1522 | const std::string& resource_name, const std::string& fallback_type, |
| 1523 | const std::string& fallback_package) const { |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1524 | StringPiece package_name, type, entry; |
| 1525 | if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1526 | return base::unexpected(std::nullopt); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | if (entry.empty()) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1530 | return base::unexpected(std::nullopt); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | if (package_name.empty()) { |
| 1534 | package_name = fallback_package; |
| 1535 | } |
| 1536 | |
| 1537 | if (type.empty()) { |
| 1538 | type = fallback_type; |
| 1539 | } |
| 1540 | |
| 1541 | std::u16string type16; |
| 1542 | if (!Utf8ToUtf16(type, &type16)) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1543 | return base::unexpected(std::nullopt); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | std::u16string entry16; |
| 1547 | if (!Utf8ToUtf16(entry, &entry16)) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1548 | return base::unexpected(std::nullopt); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | const StringPiece16 kAttr16 = u"attr"; |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1552 | for (const PackageGroup& package_group : package_groups_) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1553 | for (const ConfiguredPackage& package_impl : package_group.packages_) { |
| 1554 | const LoadedPackage* package = package_impl.loaded_package_; |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1555 | if (package_name != package->GetPackageName()) { |
| 1556 | // All packages in the same group are expected to have the same package name. |
| 1557 | break; |
| 1558 | } |
| 1559 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1560 | base::expected<uint32_t, NullOrIOError> resid = package->FindEntryByName(type16, entry16); |
| 1561 | if (UNLIKELY(IsIOError(resid))) { |
| 1562 | return base::unexpected(resid.error()); |
Jeremy Meyer | e00958a | 2025-02-19 16:55:19 -0800 | [diff] [blame] | 1563 | } |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1564 | |
| 1565 | if (!resid.has_value() && kAttr16 == type16) { |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1566 | // Private attributes in libraries (such as the framework) are sometimes encoded |
| 1567 | // under the type '^attr-private' in order to leave the ID space of public 'attr' |
| 1568 | // free for future additions. Check '^attr-private' for the same name. |
Yurii Zubrytskyi | e5c50e7 | 2025-02-12 19:47:09 -0800 | [diff] [blame] | 1569 | const static std::u16string kAttrPrivate16 = u"^attr-private"; |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1570 | resid = package->FindEntryByName(kAttrPrivate16, entry16); |
| 1571 | } |
| 1572 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1573 | if (resid.has_value()) { |
| 1574 | return fix_package_id(*resid, package_group.dynamic_ref_table->mAssignedPackageId); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | } |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1578 | return base::unexpected(std::nullopt); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 1579 | } |
| 1580 | |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 1581 | void AssetManager2::RebuildFilterList() { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1582 | for (PackageGroup& group : package_groups_) { |
Yurii Zubrytskyi | dbce356 | 2022-11-14 22:26:10 -0800 | [diff] [blame] | 1583 | for (ConfiguredPackage& package : group.packages_) { |
| 1584 | package.filtered_configs_.forEachItem([](auto, auto& fcg) { fcg.type_entries.clear(); }); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1585 | // Create the filters here. |
Yurii Zubrytskyi | dbce356 | 2022-11-14 22:26:10 -0800 | [diff] [blame] | 1586 | package.loaded_package_->ForEachTypeSpec([&](const TypeSpec& type_spec, uint8_t type_id) { |
Yurii Zubrytskyi | 59dab3b | 2022-11-03 00:08:49 -0700 | [diff] [blame] | 1587 | FilteredConfigGroup* group = nullptr; |
Ryan Mitchell | 14e8ade | 2021-01-11 16:01:35 -0800 | [diff] [blame] | 1588 | for (const auto& type_entry : type_spec.type_entries) { |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 1589 | for (auto& config : configurations_) { |
Jeremy Meyer | ccf5cd7 | 2023-08-15 21:42:03 +0000 | [diff] [blame] | 1590 | if (type_entry.config.match(config)) { |
| 1591 | if (!group) { |
| 1592 | group = &package.filtered_configs_.editItemAt(type_id - 1); |
| 1593 | } |
| 1594 | group->type_entries.push_back(&type_entry); |
| 1595 | break; |
Yurii Zubrytskyi | 59dab3b | 2022-11-03 00:08:49 -0700 | [diff] [blame] | 1596 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | }); |
Yurii Zubrytskyi | dbce356 | 2022-11-14 22:26:10 -0800 | [diff] [blame] | 1600 | package.filtered_configs_.trimBuckets( |
| 1601 | [](const auto& fcg) { return fcg.type_entries.empty(); }); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1602 | } |
| 1603 | } |
| 1604 | } |
| 1605 | |
Biswarup Pal | bc306b4 | 2025-02-06 23:31:30 +0000 | [diff] [blame] | 1606 | bool AssetManager2::IsAnyOverlayConstraintSatisfied(const Idmap_constraints& constraints) const { |
| 1607 | if (constraints.constraint_count == 0) { |
| 1608 | // There are no constraints, return true. |
| 1609 | return true; |
| 1610 | } |
| 1611 | |
| 1612 | for (uint32_t i = 0; i < constraints.constraint_count; i++) { |
| 1613 | auto constraint = constraints.constraint_entries[i]; |
| 1614 | if (constraint.constraint_type == kOverlayConstraintTypeDisplayId && |
| 1615 | constraint.constraint_value == display_id_) { |
| 1616 | return true; |
| 1617 | } |
| 1618 | if (constraint.constraint_type == kOverlayConstraintTypeDeviceId && |
| 1619 | constraint.constraint_value == device_id_) { |
| 1620 | return true; |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | return false; |
| 1625 | } |
| 1626 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1627 | void AssetManager2::InvalidateCaches(uint32_t diff) { |
Yurii Zubrytskyi | 0914488 | 2023-06-15 23:23:15 -0700 | [diff] [blame] | 1628 | cached_resolved_values_.clear(); |
Ryan Mitchell | 2c4d874 | 2019-03-04 09:41:00 -0800 | [diff] [blame] | 1629 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1630 | if (diff == 0xffffffffu) { |
| 1631 | // Everything must go. |
| 1632 | cached_bags_.clear(); |
Yurii Zubrytskyi | 0914488 | 2023-06-15 23:23:15 -0700 | [diff] [blame] | 1633 | cached_bag_resid_stacks_.clear(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1634 | return; |
| 1635 | } |
| 1636 | |
| 1637 | // Be more conservative with what gets purged. Only if the bag has other possible |
| 1638 | // variations with respect to what changed (diff) should we remove it. |
Yurii Zubrytskyi | 0914488 | 2023-06-15 23:23:15 -0700 | [diff] [blame] | 1639 | for (auto stack_it = cached_bag_resid_stacks_.begin(); |
| 1640 | stack_it != cached_bag_resid_stacks_.end();) { |
| 1641 | const auto it = cached_bags_.find(stack_it->first); |
| 1642 | if (it == cached_bags_.end()) { |
| 1643 | stack_it = cached_bag_resid_stacks_.erase(stack_it); |
| 1644 | } else if ((diff & it->second->type_spec_flags) != 0) { |
| 1645 | cached_bags_.erase(it); |
| 1646 | stack_it = cached_bag_resid_stacks_.erase(stack_it); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1647 | } else { |
Yurii Zubrytskyi | 0914488 | 2023-06-15 23:23:15 -0700 | [diff] [blame] | 1648 | ++stack_it; // Keep the item in both caches. |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1649 | } |
| 1650 | } |
Ryan Mitchell | a45506e | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1651 | |
Yurii Zubrytskyi | 0914488 | 2023-06-15 23:23:15 -0700 | [diff] [blame] | 1652 | // Need to ensure that both bag caches are consistent, as we populate them in the same function. |
| 1653 | // Iterate over the cached bags to erase the items without the corresponding resid_stack cache |
| 1654 | // items. |
| 1655 | for (auto it = cached_bags_.begin(); it != cached_bags_.end();) { |
| 1656 | if ((diff & it->second->type_spec_flags) != 0) { |
| 1657 | it = cached_bags_.erase(it); |
| 1658 | } else { |
| 1659 | ++it; |
| 1660 | } |
| 1661 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
Ryan Mitchell | 2e39422 | 2019-08-28 12:10:51 -0700 | [diff] [blame] | 1664 | uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) const { |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1665 | for (auto& package_group : package_groups_) { |
| 1666 | for (auto& package2 : package_group.packages_) { |
| 1667 | if (package2.loaded_package_ == package) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 1668 | return package_group.dynamic_ref_table->mAssignedPackageId; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1675 | std::unique_ptr<Theme> AssetManager2::NewTheme() { |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1676 | constexpr size_t kInitialReserveSize = 32; |
| 1677 | auto theme = std::unique_ptr<Theme>(new Theme(this)); |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1678 | theme->keys_.reserve(kInitialReserveSize); |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1679 | theme->entries_.reserve(kInitialReserveSize); |
| 1680 | return theme; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Yurii Zubrytskyi | 9eb44c9 | 2022-11-14 23:44:52 -0800 | [diff] [blame] | 1683 | void AssetManager2::ForEachPackage(base::function_ref<bool(const std::string&, uint8_t)> func, |
| 1684 | package_property_t excluded_property_flags) const { |
| 1685 | for (const PackageGroup& package_group : package_groups_) { |
| 1686 | const auto loaded_package = package_group.packages_.front().loaded_package_; |
| 1687 | if ((loaded_package->GetPropertyFlags() & excluded_property_flags) == 0U |
| 1688 | && !func(loaded_package->GetPackageName(), |
| 1689 | package_group.dynamic_ref_table->mAssignedPackageId)) { |
| 1690 | return; |
| 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 | |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 1695 | AssetManager2::ScopedOperation AssetManager2::StartOperation() const { |
| 1696 | ++number_of_running_scoped_operations_; |
| 1697 | return ScopedOperation(*this); |
| 1698 | } |
| 1699 | |
| 1700 | void AssetManager2::FinishOperation() const { |
| 1701 | if (number_of_running_scoped_operations_ < 1) { |
| 1702 | ALOGW("Invalid FinishOperation() call when there's none happening"); |
| 1703 | return; |
| 1704 | } |
| 1705 | if (--number_of_running_scoped_operations_ == 0) { |
| 1706 | for (auto&& [_, assets] : apk_assets_) { |
| 1707 | assets.clear(); |
| 1708 | } |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | const AssetManager2::ApkAssetsPtr& AssetManager2::GetApkAssets(ApkAssetsCookie cookie) const { |
| 1713 | DCHECK(number_of_running_scoped_operations_ > 0) << "Must have an operation running"; |
| 1714 | |
| 1715 | if (cookie < 0 || cookie >= apk_assets_.size()) { |
| 1716 | static const ApkAssetsPtr empty{}; |
| 1717 | return empty; |
| 1718 | } |
| 1719 | auto& [wptr, res] = apk_assets_[cookie]; |
| 1720 | if (!res) { |
| 1721 | res = wptr.promote(); |
| 1722 | } |
| 1723 | return res; |
| 1724 | } |
| 1725 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1726 | Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) { |
| 1727 | } |
| 1728 | |
| 1729 | Theme::~Theme() = default; |
| 1730 | |
Yurii Zubrytskyi | 1e5452a | 2024-06-13 19:30:36 -0700 | [diff] [blame] | 1731 | static bool IsUndefined(const Res_value& value) { |
| 1732 | // DATA_NULL_EMPTY (@empty) is a valid resource value and DATA_NULL_UNDEFINED represents |
| 1733 | // an absence of a valid value. |
| 1734 | return value.dataType == Res_value::TYPE_NULL && value.data != Res_value::DATA_NULL_EMPTY; |
| 1735 | } |
| 1736 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1737 | base::expected<std::monostate, NullOrIOError> Theme::ApplyStyle(uint32_t resid, bool force) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1738 | ATRACE_NAME("Theme::ApplyStyle"); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1739 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1740 | auto bag = asset_manager_->GetBag(resid); |
| 1741 | if (!bag.has_value()) { |
| 1742 | return base::unexpected(bag.error()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | // Merge the flags from this style. |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1746 | type_spec_flags_ |= (*bag)->type_spec_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1747 | |
Yurii Zubrytskyi | 1e5452a | 2024-06-13 19:30:36 -0700 | [diff] [blame] | 1748 | // |
| 1749 | // This function is the most expensive part of applying an frro to the existing app resources, |
| 1750 | // and needs to be as efficient as possible. |
| 1751 | // The data structure we're working with is two parallel sorted arrays of keys (resource IDs) |
| 1752 | // and entries (resource value + some attributes). |
| 1753 | // The styles get applied in sequence, starting with an empty set of attributes. Each style |
| 1754 | // contains its values for the theme attributes, and gets applied in either normal or forced way: |
| 1755 | // - normal way never overrides the existing attribute, so only unique style attributes are added |
| 1756 | // - forced way overrides anything for that attribute, and if it's undefined it removes the |
| 1757 | // previous value completely |
| 1758 | // |
| 1759 | // Style attributes come in a Bag data type - a sorted array of attributes with their values. This |
| 1760 | // means we don't need to re-sort the attributes ever, and instead: |
| 1761 | // - for an already existing attribute just skip it or apply the forced value |
| 1762 | // - if the forced value is undefined, mark it undefined as well to get rid of it later |
| 1763 | // - for a new attribute append it to the array, forming a new sorted section of new attributes |
| 1764 | // past the end of the original ones (ignore undefined ones here) |
| 1765 | // - inplace merge two sorted sections to form a single sorted array again. |
| 1766 | // - run the last pass to remove all undefined elements |
| 1767 | // |
| 1768 | // Using this algorithm performs better than a repeated binary search + insert in the middle, |
| 1769 | // as that keeps shifting the tail end of the arrays and wasting CPU cycles in memcpy(). |
| 1770 | // |
| 1771 | const auto starting_size = keys_.size(); |
| 1772 | if (starting_size == 0) { |
| 1773 | keys_.reserve((*bag)->entry_count); |
| 1774 | entries_.reserve((*bag)->entry_count); |
| 1775 | } |
| 1776 | bool wrote_undefined = false; |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1777 | for (auto it = begin(*bag); it != end(*bag); ++it) { |
| 1778 | const uint32_t attr_res_id = it->key; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1779 | // If the resource ID passed in is not a style, the key can be some other identifier that is not |
| 1780 | // a resource ID. We should fail fast instead of operating with strange resource IDs. |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1781 | if (!is_valid_resid(attr_res_id)) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1782 | return base::unexpected(std::nullopt); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1783 | } |
Yurii Zubrytskyi | 1e5452a | 2024-06-13 19:30:36 -0700 | [diff] [blame] | 1784 | const bool is_undefined = IsUndefined(it->value); |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1785 | if (!force && is_undefined) { |
| 1786 | continue; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1787 | } |
Yurii Zubrytskyi | 1e5452a | 2024-06-13 19:30:36 -0700 | [diff] [blame] | 1788 | const auto key_it = std::lower_bound(keys_.begin(), keys_.begin() + starting_size, attr_res_id); |
| 1789 | if (key_it != keys_.begin() + starting_size && *key_it == attr_res_id) { |
| 1790 | const auto entry_it = entries_.begin() + (key_it - keys_.begin()); |
| 1791 | if (force || IsUndefined(entry_it->value)) { |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1792 | *entry_it = Entry{it->cookie, (*bag)->type_spec_flags, it->value}; |
Yurii Zubrytskyi | 1e5452a | 2024-06-13 19:30:36 -0700 | [diff] [blame] | 1793 | wrote_undefined |= is_undefined; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1794 | } |
Yurii Zubrytskyi | 1e5452a | 2024-06-13 19:30:36 -0700 | [diff] [blame] | 1795 | } else if (!is_undefined) { |
| 1796 | keys_.emplace_back(attr_res_id); |
| 1797 | entries_.emplace_back(it->cookie, (*bag)->type_spec_flags, it->value); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1798 | } |
| 1799 | } |
Yurii Zubrytskyi | 1e5452a | 2024-06-13 19:30:36 -0700 | [diff] [blame] | 1800 | |
| 1801 | if (starting_size && keys_.size() != starting_size) { |
| 1802 | std::inplace_merge( |
| 1803 | CombinedIterator(keys_.begin(), entries_.begin()), |
| 1804 | CombinedIterator(keys_.begin() + starting_size, entries_.begin() + starting_size), |
| 1805 | CombinedIterator(keys_.end(), entries_.end())); |
| 1806 | } |
| 1807 | if (wrote_undefined) { |
| 1808 | auto new_end = std::remove_if(CombinedIterator(keys_.begin(), entries_.begin()), |
| 1809 | CombinedIterator(keys_.end(), entries_.end()), |
| 1810 | [](const auto& pair) { return IsUndefined(pair.second.value); }); |
| 1811 | keys_.erase(new_end.it1, keys_.end()); |
| 1812 | entries_.erase(new_end.it2, entries_.end()); |
| 1813 | } |
| 1814 | if (android::base::kEnableDChecks && !std::is_sorted(keys_.begin(), keys_.end())) { |
| 1815 | ALOGW("Bag %u was unsorted in the apk?", unsigned(resid)); |
| 1816 | return base::unexpected(std::nullopt); |
| 1817 | } |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1818 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Ryan Mitchell | 767e34f | 2021-06-07 12:29:05 -0700 | [diff] [blame] | 1821 | void Theme::Rebase(AssetManager2* am, const uint32_t* style_ids, const uint8_t* force, |
| 1822 | size_t style_count) { |
| 1823 | ATRACE_NAME("Theme::Rebase"); |
| 1824 | // Reset the entries without changing the vector capacity to prevent reallocations during |
| 1825 | // ApplyStyle. |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1826 | keys_.clear(); |
Ryan Mitchell | 767e34f | 2021-06-07 12:29:05 -0700 | [diff] [blame] | 1827 | entries_.clear(); |
| 1828 | asset_manager_ = am; |
| 1829 | for (size_t i = 0; i < style_count; i++) { |
| 1830 | ApplyStyle(style_ids[i], force[i]); |
| 1831 | } |
| 1832 | } |
| 1833 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1834 | std::optional<AssetManager2::SelectedValue> Theme::GetAttribute(uint32_t resid) const { |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1835 | constexpr const uint32_t kMaxIterations = 20; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1836 | uint32_t type_spec_flags = 0u; |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1837 | for (uint32_t i = 0; i <= kMaxIterations; i++) { |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1838 | const auto key_it = std::lower_bound(keys_.begin(), keys_.end(), resid); |
| 1839 | if (key_it == keys_.end() || *key_it != resid) { |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1840 | return std::nullopt; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1841 | } |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1842 | const auto entry_it = entries_.begin() + (key_it - keys_.begin()); |
Yurii Zubrytskyi | 1e5452a | 2024-06-13 19:30:36 -0700 | [diff] [blame] | 1843 | if (IsUndefined(entry_it->value)) { |
| 1844 | return std::nullopt; |
| 1845 | } |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1846 | type_spec_flags |= entry_it->type_spec_flags; |
| 1847 | if (entry_it->value.dataType == Res_value::TYPE_ATTRIBUTE) { |
| 1848 | resid = entry_it->value.data; |
| 1849 | continue; |
| 1850 | } |
| 1851 | |
| 1852 | return AssetManager2::SelectedValue(entry_it->value.dataType, entry_it->value.data, |
Jeremy Meyer | b76f745 | 2025-03-12 10:55:53 -0700 | [diff] [blame] | 1853 | entry_it->cookie, 0U /* entry flags*/, type_spec_flags, |
| 1854 | 0U /* resid */, {} /* config */); |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1855 | } |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1856 | return std::nullopt; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1859 | base::expected<std::monostate, NullOrIOError> Theme::ResolveAttributeReference( |
| 1860 | AssetManager2::SelectedValue& value) const { |
| 1861 | if (value.type != Res_value::TYPE_ATTRIBUTE) { |
| 1862 | return asset_manager_->ResolveReference(value); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 1863 | } |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1864 | |
| 1865 | std::optional<AssetManager2::SelectedValue> result = GetAttribute(value.data); |
| 1866 | if (!result.has_value()) { |
| 1867 | return base::unexpected(std::nullopt); |
| 1868 | } |
| 1869 | |
Ryan Mitchell | a45506e | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1870 | auto resolve_result = asset_manager_->ResolveReference(*result, true /* cache_value */); |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1871 | if (resolve_result.has_value()) { |
| 1872 | result->flags |= value.flags; |
| 1873 | value = *result; |
| 1874 | } |
| 1875 | return resolve_result; |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 1876 | } |
| 1877 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1878 | void Theme::Clear() { |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1879 | keys_.clear(); |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1880 | entries_.clear(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1883 | base::expected<std::monostate, IOError> Theme::SetTo(const Theme& source) { |
| 1884 | if (this == &source) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 1885 | return {}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1888 | type_spec_flags_ = source.type_spec_flags_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1889 | |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1890 | if (asset_manager_ == source.asset_manager_) { |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1891 | keys_ = source.keys_; |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1892 | entries_ = source.entries_; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1893 | } else { |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1894 | std::unordered_map<ApkAssetsCookie, ApkAssetsCookie> src_to_dest_asset_cookies; |
| 1895 | using SourceToDestinationRuntimePackageMap = std::unordered_map<int, int>; |
| 1896 | std::unordered_map<ApkAssetsCookie, SourceToDestinationRuntimePackageMap> src_asset_cookie_id_map; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1897 | |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 1898 | auto op_src = source.asset_manager_->StartOperation(); |
| 1899 | auto op_dst = asset_manager_->StartOperation(); |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1900 | |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 1901 | for (size_t i = 0; i < source.asset_manager_->GetApkAssetsCount(); i++) { |
| 1902 | const auto& src_asset = source.asset_manager_->GetApkAssets(i); |
| 1903 | if (!src_asset) { |
Yurii Zubrytskyi | b345519 | 2023-05-01 14:35:48 -0700 | [diff] [blame] | 1904 | continue; |
| 1905 | } |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 1906 | for (int j = 0; j < asset_manager_->GetApkAssetsCount(); j++) { |
| 1907 | const auto& dest_asset = asset_manager_->GetApkAssets(j); |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 1908 | if (src_asset != dest_asset) { |
| 1909 | // ResourcesManager caches and reuses ApkAssets when the same apk must be present in |
| 1910 | // multiple AssetManagers. Two ApkAssets point to the same version of the same resources |
| 1911 | // if they are the same instance. |
| 1912 | continue; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1913 | } |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 1914 | |
| 1915 | // Map the package ids of the asset in the source AssetManager to the package ids of the |
| 1916 | // asset in th destination AssetManager. |
| 1917 | SourceToDestinationRuntimePackageMap package_map; |
| 1918 | for (const auto& loaded_package : src_asset->GetLoadedArsc()->GetPackages()) { |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1919 | const int src_package_id = source.asset_manager_->GetAssignedPackageId( |
| 1920 | loaded_package.get()); |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 1921 | const int dest_package_id = asset_manager_->GetAssignedPackageId(loaded_package.get()); |
| 1922 | package_map[src_package_id] = dest_package_id; |
| 1923 | } |
| 1924 | |
| 1925 | src_to_dest_asset_cookies.insert(std::make_pair(i, j)); |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1926 | src_asset_cookie_id_map.insert(std::make_pair(i, std::move(package_map))); |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 1927 | break; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1928 | } |
| 1929 | } |
| 1930 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1931 | // Reset the data in the destination theme. |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1932 | keys_.clear(); |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1933 | entries_.clear(); |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1934 | |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1935 | for (size_t i = 0, size = source.entries_.size(); i != size; ++i) { |
| 1936 | const auto& entry = source.entries_[i]; |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1937 | bool is_reference = (entry.value.dataType == Res_value::TYPE_ATTRIBUTE |
| 1938 | || entry.value.dataType == Res_value::TYPE_REFERENCE |
| 1939 | || entry.value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE |
| 1940 | || entry.value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE) |
| 1941 | && entry.value.data != 0x0; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1942 | |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1943 | // If the attribute value represents an attribute or reference, the package id of the |
| 1944 | // value needs to be rewritten to the package id of the value in the destination. |
| 1945 | uint32_t attribute_data = entry.value.data; |
| 1946 | if (is_reference) { |
| 1947 | // Determine the package id of the reference in the destination AssetManager. |
| 1948 | auto value_package_map = src_asset_cookie_id_map.find(entry.cookie); |
| 1949 | if (value_package_map == src_asset_cookie_id_map.end()) { |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1950 | continue; |
| 1951 | } |
| 1952 | |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1953 | auto value_dest_package = value_package_map->second.find( |
| 1954 | get_package_id(entry.value.data)); |
| 1955 | if (value_dest_package == value_package_map->second.end()) { |
| 1956 | continue; |
| 1957 | } |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1958 | |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1959 | attribute_data = fix_package_id(entry.value.data, value_dest_package->second); |
| 1960 | } |
Ryan Mitchell | b85d9b2 | 2018-11-19 12:11:38 -0800 | [diff] [blame] | 1961 | |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1962 | // Find the cookie of the value in the destination. If the source apk is not loaded in the |
| 1963 | // destination, only copy resources that do not reference resources in the source. |
| 1964 | ApkAssetsCookie data_dest_cookie; |
| 1965 | auto value_dest_cookie = src_to_dest_asset_cookies.find(entry.cookie); |
| 1966 | if (value_dest_cookie != src_to_dest_asset_cookies.end()) { |
| 1967 | data_dest_cookie = value_dest_cookie->second; |
| 1968 | } else { |
| 1969 | if (is_reference || entry.value.dataType == Res_value::TYPE_STRING) { |
| 1970 | continue; |
| 1971 | } else { |
| 1972 | data_dest_cookie = 0x0; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1973 | } |
| 1974 | } |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1975 | |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1976 | const auto source_res_id = source.keys_[i]; |
| 1977 | |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1978 | // The package id of the attribute needs to be rewritten to the package id of the |
| 1979 | // attribute in the destination. |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1980 | int attribute_dest_package_id = get_package_id(source_res_id); |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1981 | if (attribute_dest_package_id != 0x01) { |
| 1982 | // Find the cookie of the attribute resource id in the source AssetManager |
| 1983 | base::expected<FindEntryResult, NullOrIOError> attribute_entry_result = |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 1984 | source.asset_manager_->FindEntry(source_res_id, 0 /* density_override */ , |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 1985 | true /* stop_at_first_match */, |
| 1986 | true /* ignore_configuration */); |
| 1987 | if (UNLIKELY(IsIOError(attribute_entry_result))) { |
| 1988 | return base::unexpected(GetIOError(attribute_entry_result.error())); |
| 1989 | } |
| 1990 | if (!attribute_entry_result.has_value()) { |
| 1991 | continue; |
| 1992 | } |
| 1993 | |
| 1994 | // Determine the package id of the attribute in the destination AssetManager. |
| 1995 | auto attribute_package_map = src_asset_cookie_id_map.find( |
| 1996 | attribute_entry_result->cookie); |
| 1997 | if (attribute_package_map == src_asset_cookie_id_map.end()) { |
| 1998 | continue; |
| 1999 | } |
| 2000 | auto attribute_dest_package = attribute_package_map->second.find( |
| 2001 | attribute_dest_package_id); |
| 2002 | if (attribute_dest_package == attribute_package_map->second.end()) { |
| 2003 | continue; |
| 2004 | } |
| 2005 | attribute_dest_package_id = attribute_dest_package->second; |
| 2006 | } |
| 2007 | |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 2008 | auto dest_attr_id = make_resid(attribute_dest_package_id, get_type_id(source_res_id), |
| 2009 | get_entry_id(source_res_id)); |
| 2010 | const auto key_it = std::lower_bound(keys_.begin(), keys_.end(), dest_attr_id); |
| 2011 | const auto entry_it = entries_.begin() + (key_it - keys_.begin()); |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 2012 | // Since the entries were cleared, the attribute resource id has yet been mapped to any value. |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 2013 | keys_.insert(key_it, dest_attr_id); |
| 2014 | entries_.insert(entry_it, Entry{data_dest_cookie, entry.type_spec_flags, |
| 2015 | Res_value{.dataType = entry.value.dataType, |
| 2016 | .data = attribute_data}}); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 2017 | } |
| 2018 | } |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 2019 | return {}; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | void Theme::Dump() const { |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 2023 | LOG(INFO) << base::StringPrintf("Theme(this=%p, AssetManager2=%p)", this, asset_manager_); |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 2024 | for (size_t i = 0, size = keys_.size(); i != size; ++i) { |
| 2025 | auto res_id = keys_[i]; |
| 2026 | const auto& entry = entries_[i]; |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 2027 | LOG(INFO) << base::StringPrintf(" entry(0x%08x)=(0x%08x) type=(0x%02x), cookie(%d)", |
Yurii Zubrytskyi | 591895b | 2022-11-14 22:06:30 -0800 | [diff] [blame] | 2028 | res_id, entry.value.data, entry.value.dataType, |
Ryan Mitchell | 3c6480c | 2021-06-07 11:22:30 -0700 | [diff] [blame] | 2029 | entry.cookie); |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 2030 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Yurii Zubrytskyi | bdcbf55 | 2023-05-10 13:16:23 -0700 | [diff] [blame] | 2033 | AssetManager2::ScopedOperation::ScopedOperation(const AssetManager2& am) : am_(am) { |
| 2034 | } |
| 2035 | |
| 2036 | AssetManager2::ScopedOperation::~ScopedOperation() { |
| 2037 | am_.FinishOperation(); |
| 2038 | } |
| 2039 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 2040 | } // namespace android |