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