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