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