blob: d33b592bddc6712dd86dfd3bb85faa2c21befb7d [file] [log] [blame]
Adam Lesinski7ad11102016-10-28 16:39:15 -07001/*
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
y57cd1952018-04-12 14:26:23 -070021#include <algorithm>
Adam Lesinski30080e22017-10-16 16:18:09 -070022#include <iterator>
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -070023#include <map>
Winson2f3669b2019-01-11 11:28:34 -080024#include <set>
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -070025#include <span>
Adam Lesinski0c405242017-01-13 20:47:26 -080026
Adam Lesinski7ad11102016-10-28 16:39:15 -070027#include "android-base/logging.h"
28#include "android-base/stringprintf.h"
Jackal Guo552b45d2021-09-29 10:52:19 +080029#include "androidfw/ResourceTypes.h"
Ryan Mitchell8a891d82019-07-01 09:48:23 -070030#include "androidfw/ResourceUtils.h"
Ryan Mitchell31b11052019-06-13 13:47:26 -070031#include "androidfw/Util.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070032#include "utils/ByteOrder.h"
33#include "utils/Trace.h"
34
35#ifdef _WIN32
36#ifdef ERROR
37#undef ERROR
38#endif
39#endif
40
41namespace android {
42
Ryan Mitchell80094e32020-11-16 23:08:18 +000043namespace {
44
45using EntryValue = std::variant<Res_value, incfs::verified_map_ptr<ResTable_map_entry>>;
46
Eric Miao368cd192022-09-09 15:46:14 -070047/* NOTE: table_entry has been verified in LoadedPackage::GetEntryFromOffset(),
48 * and so access to ->value() and ->map_entry() are safe here
49 */
Ryan Mitchell80094e32020-11-16 23:08:18 +000050base::expected<EntryValue, IOError> GetEntryValue(
51 incfs::verified_map_ptr<ResTable_entry> table_entry) {
Eric Miao368cd192022-09-09 15:46:14 -070052 const uint16_t entry_size = table_entry->size();
Ryan Mitchell80094e32020-11-16 23:08:18 +000053
54 // Check if the entry represents a bag value.
Eric Miao368cd192022-09-09 15:46:14 -070055 if (entry_size >= sizeof(ResTable_map_entry) && table_entry->is_complex()) {
56 return table_entry.convert<ResTable_map_entry>().verified();
Ryan Mitchell80094e32020-11-16 23:08:18 +000057 }
58
Eric Miao368cd192022-09-09 15:46:14 -070059 return table_entry->value();
Ryan Mitchell80094e32020-11-16 23:08:18 +000060}
61
62} // namespace
63
Adam Lesinskibebfcc42018-02-12 14:27:46 -080064struct FindEntryResult {
Ryan Mitchell80094e32020-11-16 23:08:18 +000065 // 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 Lesinskibebfcc42018-02-12 14:27:46 -080071
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 Mitchell8a891d82019-07-01 09:48:23 -070082 // The package name of the resource.
83 const std::string* package_name;
84
Adam Lesinskibebfcc42018-02-12 14:27:46 -080085 // The string pool reference to the type's name. This uses a different string pool than
86 // the global string pool, but this is hidden from the caller.
87 StringPoolRef type_string_ref;
88
89 // The string pool reference to the entry's name. This uses a different string pool than
90 // the global string pool, but this is hidden from the caller.
91 StringPoolRef entry_string_ref;
92};
93
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -070094AssetManager2::AssetManager2(ApkAssetsList apk_assets, const ResTable_config& configuration)
95 : configuration_(configuration) {
96 // Don't invalidate caches here as there's nothing cached yet.
97 SetApkAssets(apk_assets, false);
Adam Lesinski970bd8d2017-09-25 13:21:55 -070098}
Adam Lesinski7ad11102016-10-28 16:39:15 -070099
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700100bool AssetManager2::SetApkAssets(ApkAssetsList apk_assets, bool invalidate_caches) {
101 BuildDynamicRefTable(apk_assets);
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800102 RebuildFilterList();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700103 if (invalidate_caches) {
104 InvalidateCaches(static_cast<uint32_t>(-1));
105 }
106 return true;
107}
108
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700109bool AssetManager2::SetApkAssets(std::initializer_list<ApkAssetsPtr> apk_assets,
110 bool invalidate_caches) {
111 return SetApkAssets(ApkAssetsList(apk_assets.begin(), apk_assets.size()), invalidate_caches);
112}
113
114void AssetManager2::BuildDynamicRefTable(ApkAssetsList apk_assets) {
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700115 auto op = StartOperation();
116
117 apk_assets_.resize(apk_assets.size());
118 for (size_t i = 0; i != apk_assets.size(); ++i) {
119 apk_assets_[i].first = apk_assets[i];
120 // Let's populate the locked assets right away as we're going to need them here later.
121 apk_assets_[i].second = apk_assets[i];
122 }
123
Adam Lesinskida431a22016-12-29 16:08:16 -0500124 package_groups_.clear();
125 package_ids_.fill(0xff);
126
Ryan Mitchellef538432021-03-01 14:52:14 -0800127 // A mapping from path of apk assets that could be target packages of overlays to the runtime
128 // package id of its first loaded package. Overlays currently can only override resources in the
129 // first package in the target resource table.
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800130 std::unordered_map<std::string_view, uint8_t> target_assets_package_ids;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700131
Ryan Mitchell824cc492020-02-12 10:48:14 -0800132 // Overlay resources are not directly referenced by an application so their resource ids
133 // can change throughout the application's lifetime. Assign overlay package ids last.
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700134 std::vector<const ApkAssets*> sorted_apk_assets;
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700135 sorted_apk_assets.reserve(apk_assets.size());
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700136 for (auto& asset : apk_assets) {
137 sorted_apk_assets.push_back(asset.get());
138 }
139 std::stable_partition(sorted_apk_assets.begin(), sorted_apk_assets.end(),
140 [](auto a) { return !a->IsOverlay(); });
Ryan Mitchell824cc492020-02-12 10:48:14 -0800141
142 // The assets cookie must map to the position of the apk assets in the unsorted apk assets list.
143 std::unordered_map<const ApkAssets*, ApkAssetsCookie> apk_assets_cookies;
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700144 apk_assets_cookies.reserve(apk_assets.size());
145 for (size_t i = 0, n = apk_assets.size(); i < n; i++) {
146 apk_assets_cookies[apk_assets[i].get()] = static_cast<ApkAssetsCookie>(i);
Ryan Mitchell824cc492020-02-12 10:48:14 -0800147 }
148
Ryan Mitchellb894c272020-02-12 10:31:44 -0800149 // 0x01 is reserved for the android package.
150 int next_package_id = 0x02;
Ryan Mitchell824cc492020-02-12 10:48:14 -0800151 for (const ApkAssets* apk_assets : sorted_apk_assets) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800152 std::shared_ptr<OverlayDynamicRefTable> overlay_ref_table;
153 if (auto loaded_idmap = apk_assets->GetLoadedIdmap(); loaded_idmap != nullptr) {
154 // The target package must precede the overlay package in the apk assets paths in order
155 // to take effect.
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800156 auto iter = target_assets_package_ids.find(loaded_idmap->TargetApkPath());
Ryan Mitchellef538432021-03-01 14:52:14 -0800157 if (iter == target_assets_package_ids.end()) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800158 LOG(INFO) << "failed to find target package for overlay "
159 << loaded_idmap->OverlayApkPath();
160 } else {
161 uint8_t target_package_id = iter->second;
162
163 // Create a special dynamic reference table for the overlay to rewrite references to
164 // overlay resources as references to the target resources they overlay.
165 overlay_ref_table = std::make_shared<OverlayDynamicRefTable>(
166 loaded_idmap->GetOverlayDynamicRefTable(target_package_id));
167
168 // Add the overlay resource map to the target package's set of overlays.
169 const uint8_t target_idx = package_ids_[target_package_id];
170 CHECK(target_idx != 0xff) << "overlay target '" << loaded_idmap->TargetApkPath()
171 << "'added to apk_assets_package_ids but does not have an"
172 << " assigned package group";
173
174 PackageGroup& target_package_group = package_groups_[target_idx];
175 target_package_group.overlays_.push_back(
176 ConfiguredOverlay{loaded_idmap->GetTargetResourcesMap(target_package_id,
177 overlay_ref_table.get()),
178 apk_assets_cookies[apk_assets]});
179 }
180 }
181
Ryan Mitchellb894c272020-02-12 10:31:44 -0800182 const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc();
Ryan Mitchellb894c272020-02-12 10:31:44 -0800183 for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) {
184 // Get the package ID or assign one if a shared library.
185 int package_id;
186 if (package->IsDynamic()) {
187 package_id = next_package_id++;
188 } else {
189 package_id = package->GetPackageId();
Adam Lesinskida431a22016-12-29 16:08:16 -0500190 }
191
Adam Lesinskida431a22016-12-29 16:08:16 -0500192 uint8_t idx = package_ids_[package_id];
193 if (idx == 0xff) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800194 // Add the mapping for package ID to index if not present.
Adam Lesinskida431a22016-12-29 16:08:16 -0500195 package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size());
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800196 PackageGroup& new_group = package_groups_.emplace_back();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700197
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800198 if (overlay_ref_table != nullptr) {
199 // If this package is from an overlay, use a dynamic reference table that can rewrite
200 // overlay resource ids to their corresponding target resource ids.
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800201 new_group.dynamic_ref_table = std::move(overlay_ref_table);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700202 }
203
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800204 DynamicRefTable* ref_table = new_group.dynamic_ref_table.get();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700205 ref_table->mAssignedPackageId = package_id;
206 ref_table->mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f;
Adam Lesinskida431a22016-12-29 16:08:16 -0500207 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500208
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800209 // Add the package to the set of packages with the same ID.
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800210 PackageGroup* package_group = &package_groups_[idx];
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800211 package_group->packages_.emplace_back().loaded_package_ = package.get();
Ryan Mitchell824cc492020-02-12 10:48:14 -0800212 package_group->cookies_.push_back(apk_assets_cookies[apk_assets]);
Adam Lesinskida431a22016-12-29 16:08:16 -0500213
214 // Add the package name -> build time ID mappings.
215 for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) {
216 String16 package_name(entry.package_name.c_str(), entry.package_name.size());
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700217 package_group->dynamic_ref_table->mEntries.replaceValueFor(
Adam Lesinskida431a22016-12-29 16:08:16 -0500218 package_name, static_cast<uint8_t>(entry.package_id));
219 }
Ryan Mitchellb894c272020-02-12 10:31:44 -0800220
Ryan Mitchellef538432021-03-01 14:52:14 -0800221 if (auto apk_assets_path = apk_assets->GetPath()) {
222 // Overlay target ApkAssets must have been created using path based load apis.
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800223 target_assets_package_ids.emplace(*apk_assets_path, package_id);
Ryan Mitchellef538432021-03-01 14:52:14 -0800224 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500225 }
226 }
227
228 // Now assign the runtime IDs so that we have a build-time to runtime ID map.
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -0700229 DynamicRefTable::AliasMap aliases;
230 for (const auto& group : package_groups_) {
231 const std::string& package_name = group.packages_[0].loaded_package_->GetPackageName();
232 const auto name_16 = String16(package_name.c_str(), package_name.size());
233 for (auto&& inner_group : package_groups_) {
234 inner_group.dynamic_ref_table->addMapping(name_16,
235 group.dynamic_ref_table->mAssignedPackageId);
Adam Lesinskida431a22016-12-29 16:08:16 -0500236 }
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -0700237
238 for (const auto& package : group.packages_) {
239 const auto& package_aliases = package.loaded_package_->GetAliasResourceIdMap();
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800240 aliases.insert(aliases.end(), package_aliases.begin(), package_aliases.end());
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -0700241 }
242 }
243
244 if (!aliases.empty()) {
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800245 std::sort(aliases.begin(), aliases.end(), [](auto&& l, auto&& r) { return l.first < r.first; });
246
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -0700247 // Add the alias resources to the dynamic reference table of every package group. Since
248 // staging aliases can only be defined by the framework package (which is not a shared
249 // library), the compile-time package id of the framework is the same across all packages
250 // that compile against the framework.
251 for (auto& group : std::span(package_groups_.data(), package_groups_.size() - 1)) {
252 group.dynamic_ref_table->setAliases(aliases);
253 }
254 package_groups_.back().dynamic_ref_table->setAliases(std::move(aliases));
Adam Lesinskida431a22016-12-29 16:08:16 -0500255 }
256}
257
258void AssetManager2::DumpToLog() const {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800259 LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this);
260
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700261 auto op = StartOperation();
Adam Lesinskida431a22016-12-29 16:08:16 -0500262 std::string list;
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700263 for (size_t i = 0; i < apk_assets_.size(); ++i) {
264 const auto& assets = GetApkAssets(i);
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700265 base::StringAppendF(&list, "%s,", assets ? assets->GetDebugName().c_str() : "nullptr");
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800266 }
267 LOG(INFO) << "ApkAssets: " << list;
268
269 list = "";
Adam Lesinskida431a22016-12-29 16:08:16 -0500270 for (size_t i = 0; i < package_ids_.size(); i++) {
271 if (package_ids_[i] != 0xff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800272 base::StringAppendF(&list, "%02x -> %d, ", (int)i, package_ids_[i]);
Adam Lesinskida431a22016-12-29 16:08:16 -0500273 }
274 }
275 LOG(INFO) << "Package ID map: " << list;
276
Adam Lesinski0dd369912018-01-25 15:38:38 -0800277 for (const auto& package_group: package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800278 list = "";
279 for (const auto& package : package_group.packages_) {
280 const LoadedPackage* loaded_package = package.loaded_package_;
281 base::StringAppendF(&list, "%s(%02x%s), ", loaded_package->GetPackageName().c_str(),
282 loaded_package->GetPackageId(),
283 (loaded_package->IsDynamic() ? " dynamic" : ""));
284 }
285 LOG(INFO) << base::StringPrintf("PG (%02x): ",
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700286 package_group.dynamic_ref_table->mAssignedPackageId)
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800287 << list;
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800288
289 for (size_t i = 0; i < 256; i++) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700290 if (package_group.dynamic_ref_table->mLookupTable[i] != 0) {
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800291 LOG(INFO) << base::StringPrintf(" e[0x%02x] -> 0x%02x", (uint8_t) i,
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700292 package_group.dynamic_ref_table->mLookupTable[i]);
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800293 }
294 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500295 }
296}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700297
298const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const {
299 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
300 return nullptr;
301 }
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700302 auto op = StartOperation();
303 const auto& assets = GetApkAssets(cookie);
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700304 return assets ? assets->GetLoadedArsc()->GetStringPool() : nullptr;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700305}
306
Adam Lesinskida431a22016-12-29 16:08:16 -0500307const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const {
308 if (package_id >= package_ids_.size()) {
309 return nullptr;
310 }
311
312 const size_t idx = package_ids_[package_id];
313 if (idx == 0xff) {
314 return nullptr;
315 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700316 return package_groups_[idx].dynamic_ref_table.get();
Adam Lesinskida431a22016-12-29 16:08:16 -0500317}
318
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700319std::shared_ptr<const DynamicRefTable> AssetManager2::GetDynamicRefTableForCookie(
320 ApkAssetsCookie cookie) const {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800321 for (const PackageGroup& package_group : package_groups_) {
322 for (const ApkAssetsCookie& package_cookie : package_group.cookies_) {
323 if (package_cookie == cookie) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700324 return package_group.dynamic_ref_table;
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800325 }
326 }
327 }
328 return nullptr;
329}
330
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100331const std::unordered_map<std::string, std::string>*
332 AssetManager2::GetOverlayableMapForPackage(uint32_t package_id) const {
333
334 if (package_id >= package_ids_.size()) {
335 return nullptr;
336 }
337
338 const size_t idx = package_ids_[package_id];
339 if (idx == 0xff) {
340 return nullptr;
341 }
342
343 const PackageGroup& package_group = package_groups_[idx];
Ryan Mitchell80094e32020-11-16 23:08:18 +0000344 if (package_group.packages_.empty()) {
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100345 return nullptr;
346 }
347
348 const auto loaded_package = package_group.packages_[0].loaded_package_;
349 return &loaded_package->GetOverlayableMap();
350}
351
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700352bool AssetManager2::GetOverlayablesToString(android::StringPiece package_name,
Ryan Mitchell2e394222019-08-28 12:10:51 -0700353 std::string* out) const {
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700354 auto op = StartOperation();
Ryan Mitchell2e394222019-08-28 12:10:51 -0700355 uint8_t package_id = 0U;
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700356 for (size_t i = 0; i != apk_assets_.size(); ++i) {
357 const auto& assets = GetApkAssets(i);
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700358 if (!assets) {
359 continue;
360 }
361 const LoadedArsc* loaded_arsc = assets->GetLoadedArsc();
Ryan Mitchell2e394222019-08-28 12:10:51 -0700362 if (loaded_arsc == nullptr) {
363 continue;
364 }
365
366 const auto& loaded_packages = loaded_arsc->GetPackages();
367 if (loaded_packages.empty()) {
368 continue;
369 }
370
371 const auto& loaded_package = loaded_packages[0];
372 if (loaded_package->GetPackageName() == package_name) {
373 package_id = GetAssignedPackageId(loaded_package.get());
374 break;
375 }
376 }
377
378 if (package_id == 0U) {
379 ANDROID_LOG(ERROR) << base::StringPrintf("No package with name '%s", package_name.data());
380 return false;
381 }
382
383 const size_t idx = package_ids_[package_id];
384 if (idx == 0xff) {
385 return false;
386 }
387
388 std::string output;
389 for (const ConfiguredPackage& package : package_groups_[idx].packages_) {
390 const LoadedPackage* loaded_package = package.loaded_package_;
391 for (auto it = loaded_package->begin(); it != loaded_package->end(); it++) {
392 const OverlayableInfo* info = loaded_package->GetOverlayableInfo(*it);
393 if (info != nullptr) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000394 auto res_name = GetResourceName(*it);
395 if (!res_name.has_value()) {
Ryan Mitchell2e394222019-08-28 12:10:51 -0700396 ANDROID_LOG(ERROR) << base::StringPrintf(
397 "Unable to retrieve name of overlayable resource 0x%08x", *it);
398 return false;
399 }
400
Ryan Mitchell80094e32020-11-16 23:08:18 +0000401 const std::string name = ToFormattedResourceString(*res_name);
Ryan Mitchell2e394222019-08-28 12:10:51 -0700402 output.append(base::StringPrintf(
403 "resource='%s' overlayable='%s' actor='%s' policy='0x%08x'\n",
Yurii Zubrytskyi9d225372022-11-29 11:12:18 -0800404 name.c_str(), info->name.data(), info->actor.data(), info->policy_flags));
Ryan Mitchell2e394222019-08-28 12:10:51 -0700405 }
406 }
407 }
408
409 *out = std::move(output);
410 return true;
411}
412
Ryan Mitchell192400c2020-04-02 09:54:23 -0700413bool AssetManager2::ContainsAllocatedTable() const {
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700414 auto op = StartOperation();
415 for (size_t i = 0; i != apk_assets_.size(); ++i) {
416 const auto& assets = GetApkAssets(i);
417 if (assets && assets->IsTableAllocated()) {
418 return true;
419 }
420 }
421 return false;
Ryan Mitchell192400c2020-04-02 09:54:23 -0700422}
423
Adam Lesinski7ad11102016-10-28 16:39:15 -0700424void AssetManager2::SetConfiguration(const ResTable_config& configuration) {
425 const int diff = configuration_.diff(configuration);
426 configuration_ = configuration;
427
428 if (diff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800429 RebuildFilterList();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700430 InvalidateCaches(static_cast<uint32_t>(diff));
431 }
432}
433
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700434std::set<AssetManager2::ApkAssetsPtr> AssetManager2::GetNonSystemOverlays() const {
435 std::set<ApkAssetsPtr> non_system_overlays;
Adam Lesinski0c405242017-01-13 20:47:26 -0800436 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800437 bool found_system_package = false;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800438 for (const ConfiguredPackage& package : package_group.packages_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700439 if (package.loaded_package_->IsSystem()) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800440 found_system_package = true;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700441 break;
442 }
443 }
444
445 if (!found_system_package) {
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700446 auto op = StartOperation();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700447 for (const ConfiguredOverlay& overlay : package_group.overlays_) {
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700448 if (const auto& asset = GetApkAssets(overlay.cookie)) {
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700449 non_system_overlays.insert(std::move(asset));
450 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700451 }
452 }
453 }
454
455 return non_system_overlays;
456}
457
Ryan Mitchell80094e32020-11-16 23:08:18 +0000458base::expected<std::set<ResTable_config>, IOError> AssetManager2::GetResourceConfigurations(
459 bool exclude_system, bool exclude_mipmap) const {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700460 ATRACE_NAME("AssetManager::GetResourceConfigurations");
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700461 auto op = StartOperation();
462
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700463 const auto non_system_overlays =
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700464 exclude_system ? GetNonSystemOverlays() : std::set<ApkAssetsPtr>();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700465
466 std::set<ResTable_config> configurations;
467 for (const PackageGroup& package_group : package_groups_) {
468 for (size_t i = 0; i < package_group.packages_.size(); i++) {
469 const ConfiguredPackage& package = package_group.packages_[i];
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700470 if (exclude_system) {
471 if (package.loaded_package_->IsSystem()) {
472 continue;
473 }
474 if (!non_system_overlays.empty()) {
475 // Exclude overlays that target only system resources.
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700476 const auto& apk_assets = GetApkAssets(package_group.cookies_[i]);
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700477 if (apk_assets && apk_assets->IsOverlay() &&
478 non_system_overlays.find(apk_assets) == non_system_overlays.end()) {
479 continue;
480 }
481 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800482 }
483
Ryan Mitchell80094e32020-11-16 23:08:18 +0000484 auto result = package.loaded_package_->CollectConfigurations(exclude_mipmap, &configurations);
485 if (UNLIKELY(!result.has_value())) {
486 return base::unexpected(result.error());
487 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800488 }
489 }
490 return configurations;
491}
492
493std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800494 bool merge_equivalent_languages) const {
495 ATRACE_NAME("AssetManager::GetResourceLocales");
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700496 auto op = StartOperation();
497
Adam Lesinski0c405242017-01-13 20:47:26 -0800498 std::set<std::string> locales;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700499 const auto non_system_overlays =
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700500 exclude_system ? GetNonSystemOverlays() : std::set<ApkAssetsPtr>();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700501
Adam Lesinski0c405242017-01-13 20:47:26 -0800502 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700503 for (size_t i = 0; i < package_group.packages_.size(); i++) {
504 const ConfiguredPackage& package = package_group.packages_[i];
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700505 if (exclude_system) {
506 if (package.loaded_package_->IsSystem()) {
507 continue;
508 }
509 if (!non_system_overlays.empty()) {
510 // Exclude overlays that target only system resources.
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700511 const auto& apk_assets = GetApkAssets(package_group.cookies_[i]);
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700512 if (apk_assets && apk_assets->IsOverlay() &&
513 non_system_overlays.find(apk_assets) == non_system_overlays.end()) {
514 continue;
515 }
516 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800517 }
518
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800519 package.loaded_package_->CollectLocales(merge_equivalent_languages, &locales);
Adam Lesinski0c405242017-01-13 20:47:26 -0800520 }
521 }
522 return locales;
523}
524
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800525std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename,
526 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700527 const std::string new_path = "assets/" + filename;
528 return OpenNonAsset(new_path, mode);
529}
530
531std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800532 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700533 const std::string new_path = "assets/" + filename;
534 return OpenNonAsset(new_path, cookie, mode);
535}
536
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800537std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) const {
538 ATRACE_NAME("AssetManager::OpenDir");
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700539 auto op = StartOperation();
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800540
541 std::string full_path = "assets/" + dirname;
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700542 auto files = util::make_unique<SortedVector<AssetDir::FileInfo>>();
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800543
544 // Start from the back.
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700545 for (size_t i = apk_assets_.size(); i > 0; --i) {
546 const auto& apk_assets = GetApkAssets(i - 1);
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700547 if (!apk_assets || apk_assets->IsOverlay()) {
MÃ¥rten Kongstaddbf343b2019-02-21 07:54:18 +0100548 continue;
549 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800550
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700551 auto func = [&](StringPiece name, FileType type) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800552 AssetDir::FileInfo info;
553 info.setFileName(String8(name.data(), name.size()));
554 info.setFileType(type);
Ryan Mitchellef538432021-03-01 14:52:14 -0800555 info.setSourceName(String8(apk_assets->GetDebugName().c_str()));
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800556 files->add(info);
557 };
558
Ryan Mitchellc07aa702020-03-10 13:49:12 -0700559 if (!apk_assets->GetAssetsProvider()->ForEachFile(full_path, func)) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800560 return {};
561 }
562 }
563
564 std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>();
565 asset_dir->setFileList(files.release());
566 return asset_dir;
567}
568
Adam Lesinski7ad11102016-10-28 16:39:15 -0700569// Search in reverse because that's how we used to do it and we need to preserve behaviour.
570// This is unfortunate, because ClassLoaders delegate to the parent first, so the order
571// is inconsistent for split APKs.
572std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
573 Asset::AccessMode mode,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800574 ApkAssetsCookie* out_cookie) const {
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700575 auto op = StartOperation();
576 for (size_t i = apk_assets_.size(); i > 0; i--) {
577 const auto& assets = GetApkAssets(i - 1);
MÃ¥rten Kongstaddbf343b2019-02-21 07:54:18 +0100578 // Prevent RRO from modifying assets and other entries accessed by file
579 // path. Explicitly asking for a path in a given package (denoted by a
580 // cookie) is still OK.
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700581 if (!assets || assets->IsOverlay()) {
MÃ¥rten Kongstaddbf343b2019-02-21 07:54:18 +0100582 continue;
583 }
584
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700585 std::unique_ptr<Asset> asset = assets->GetAssetsProvider()->Open(filename, mode);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700586 if (asset) {
587 if (out_cookie != nullptr) {
588 *out_cookie = i;
589 }
590 return asset;
591 }
592 }
593
594 if (out_cookie != nullptr) {
595 *out_cookie = kInvalidCookie;
596 }
597 return {};
598}
599
600std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800601 ApkAssetsCookie cookie,
602 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700603 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
604 return {};
605 }
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700606 auto op = StartOperation();
607 const auto& assets = GetApkAssets(cookie);
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700608 return assets ? assets->GetAssetsProvider()->Open(filename, mode) : nullptr;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700609}
610
Ryan Mitchell80094e32020-11-16 23:08:18 +0000611base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntry(
612 uint32_t resid, uint16_t density_override, bool stop_at_first_match,
613 bool ignore_configuration) const {
614 const bool logging_enabled = resource_resolution_logging_enabled_;
615 if (UNLIKELY(logging_enabled)) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700616 // Clear the last logged resource resolution.
617 ResetResourceResolution();
618 last_resolution_.resid = resid;
619 }
620
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700621 auto op = StartOperation();
622
Adam Lesinski7ad11102016-10-28 16:39:15 -0700623 // Might use this if density_override != 0.
624 ResTable_config density_override_config;
625
626 // Select our configuration or generate a density override configuration.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800627 const ResTable_config* desired_config = &configuration_;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700628 if (density_override != 0 && density_override != configuration_.density) {
629 density_override_config = configuration_;
630 density_override_config.density = density_override;
631 desired_config = &density_override_config;
632 }
633
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700634 // Retrieve the package group from the package id of the resource id.
Ryan Mitchell80094e32020-11-16 23:08:18 +0000635 if (UNLIKELY(!is_valid_resid(resid))) {
Mark Hansenb406b0e2022-10-14 02:18:37 +0000636 LOG(ERROR) << base::StringPrintf("Invalid resource ID 0x%08x.", resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +0000637 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -0500638 }
639
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800640 const uint32_t package_id = get_package_id(resid);
641 const uint8_t type_idx = get_type_id(resid) - 1;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800642 const uint16_t entry_idx = get_entry_id(resid);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700643 uint8_t package_idx = package_ids_[package_id];
Ryan Mitchell80094e32020-11-16 23:08:18 +0000644 if (UNLIKELY(package_idx == 0xff)) {
Mark Hansenb406b0e2022-10-14 02:18:37 +0000645 ANDROID_LOG(ERROR) << base::StringPrintf("No package ID %02x found for resource ID 0x%08x.",
Ryan Mitchell2fe23472019-02-27 09:43:01 -0800646 package_id, resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +0000647 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -0500648 }
649
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800650 const PackageGroup& package_group = package_groups_[package_idx];
Ryan Mitchell80094e32020-11-16 23:08:18 +0000651 auto result = FindEntryInternal(package_group, type_idx, entry_idx, *desired_config,
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800652 stop_at_first_match, ignore_configuration);
Ryan Mitchell80094e32020-11-16 23:08:18 +0000653 if (UNLIKELY(!result.has_value())) {
654 return base::unexpected(result.error());
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700655 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800656
Jackal Guo552b45d2021-09-29 10:52:19 +0800657 bool overlaid = false;
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700658 if (!stop_at_first_match && !ignore_configuration) {
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700659 const auto& assets = GetApkAssets(result->cookie);
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700660 if (!assets) {
661 ALOGE("Found expired ApkAssets #%d for resource ID 0x%08x.", result->cookie, resid);
662 return base::unexpected(std::nullopt);
663 }
664 if (!assets->IsLoader()) {
665 for (const auto& id_map : package_group.overlays_) {
666 auto overlay_entry = id_map.overlay_res_maps_.Lookup(resid);
667 if (!overlay_entry) {
668 // No id map entry exists for this target resource.
Jeremy Meyerbe2b7792022-08-23 17:42:50 +0000669 continue;
670 }
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700671 if (overlay_entry.IsInlineValue()) {
672 // The target resource is overlaid by an inline value not represented by a resource.
673 ConfigDescription best_frro_config;
674 Res_value best_frro_value;
675 bool frro_found = false;
676 for( const auto& [config, value] : overlay_entry.GetInlineValue()) {
677 if ((!frro_found || config.isBetterThan(best_frro_config, desired_config))
678 && config.match(*desired_config)) {
679 frro_found = true;
680 best_frro_config = config;
681 best_frro_value = value;
682 }
683 }
684 if (!frro_found) {
685 continue;
686 }
687 result->entry = best_frro_value;
688 result->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
689 result->cookie = id_map.cookie;
690
691 if (UNLIKELY(logging_enabled)) {
692 last_resolution_.steps.push_back(
693 Resolution::Step{Resolution::Step::Type::OVERLAID_INLINE, String8(), result->cookie});
694 if (auto path = assets->GetPath()) {
695 const std::string overlay_path = path->data();
696 if (IsFabricatedOverlay(overlay_path)) {
697 // FRRO don't have package name so we use the creating package here.
698 String8 frro_name = String8("FRRO");
699 // Get the first part of it since the expected one should be like
700 // {overlayPackageName}-{overlayName}-{4 alphanumeric chars}.frro
701 // under /data/resource-cache/.
702 const std::string name = overlay_path.substr(overlay_path.rfind('/') + 1);
703 const size_t end = name.find('-');
704 if (frro_name.size() != overlay_path.size() && end != std::string::npos) {
705 frro_name.append(base::StringPrintf(" created by %s",
706 name.substr(0 /* pos */,
707 end).c_str()).c_str());
708 }
709 last_resolution_.best_package_name = frro_name;
710 } else {
711 last_resolution_.best_package_name = result->package_name->c_str();
712 }
713 }
714 overlaid = true;
715 }
716 continue;
717 }
718
719 auto overlay_result = FindEntry(overlay_entry.GetResourceId(), density_override,
720 false /* stop_at_first_match */,
721 false /* ignore_configuration */);
722 if (UNLIKELY(IsIOError(overlay_result))) {
723 return base::unexpected(overlay_result.error());
724 }
725 if (!overlay_result.has_value()) {
726 continue;
727 }
728
729 if (!overlay_result->config.isBetterThan(result->config, desired_config)
730 && overlay_result->config.compare(result->config) != 0) {
731 // The configuration of the entry for the overlay must be equal to or better than the target
732 // configuration to be chosen as the better value.
733 continue;
734 }
735
736 result->cookie = overlay_result->cookie;
737 result->entry = overlay_result->entry;
738 result->config = overlay_result->config;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000739 result->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800740
741 if (UNLIKELY(logging_enabled)) {
742 last_resolution_.steps.push_back(
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700743 Resolution::Step{Resolution::Step::Type::OVERLAID, overlay_result->config.toString(),
744 overlay_result->cookie});
745 last_resolution_.best_package_name =
746 overlay_result->package_name->c_str();
Jackal Guo552b45d2021-09-29 10:52:19 +0800747 overlaid = true;
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800748 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700749 }
750 }
751 }
752
Ryan Mitchell80094e32020-11-16 23:08:18 +0000753 if (UNLIKELY(logging_enabled)) {
754 last_resolution_.cookie = result->cookie;
755 last_resolution_.type_string_ref = result->type_string_ref;
756 last_resolution_.entry_string_ref = result->entry_string_ref;
Jackal Guo552b45d2021-09-29 10:52:19 +0800757 last_resolution_.best_config_name = result->config.toString();
758 if (!overlaid) {
759 last_resolution_.best_package_name = result->package_name->c_str();
760 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700761 }
762
Ryan Mitchell80094e32020-11-16 23:08:18 +0000763 return result;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700764}
765
Ryan Mitchell80094e32020-11-16 23:08:18 +0000766base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntryInternal(
767 const PackageGroup& package_group, uint8_t type_idx, uint16_t entry_idx,
768 const ResTable_config& desired_config, bool stop_at_first_match,
769 bool ignore_configuration) const {
770 const bool logging_enabled = resource_resolution_logging_enabled_;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800771 ApkAssetsCookie best_cookie = kInvalidCookie;
772 const LoadedPackage* best_package = nullptr;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000773 incfs::verified_map_ptr<ResTable_type> best_type;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800774 const ResTable_config* best_config = nullptr;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000775 uint32_t best_offset = 0U;
776 uint32_t type_flags = 0U;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800777
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800778 // If `desired_config` is not the same as the set configuration or the caller will accept a value
779 // from any configuration, then we cannot use our filtered list of types since it only it contains
780 // types matched to the set configuration.
781 const bool use_filtered = !ignore_configuration && &desired_config == &configuration_;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800782
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700783 const size_t package_count = package_group.packages_.size();
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800784 for (size_t pi = 0; pi < package_count; pi++) {
785 const ConfiguredPackage& loaded_package_impl = package_group.packages_[pi];
786 const LoadedPackage* loaded_package = loaded_package_impl.loaded_package_;
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800787 const ApkAssetsCookie cookie = package_group.cookies_[pi];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800788
789 // If the type IDs are offset in this package, we need to take that into account when searching
790 // for a type.
791 const TypeSpec* type_spec = loaded_package->GetTypeSpecByTypeIndex(type_idx);
792 if (UNLIKELY(type_spec == nullptr)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700793 continue;
794 }
795
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800796 // Allow custom loader packages to overlay resource values with configurations equivalent to the
797 // current best configuration.
798 const bool package_is_loader = loaded_package->IsCustomLoader();
799
Ryan Mitchell80094e32020-11-16 23:08:18 +0000800 auto entry_flags = type_spec->GetFlagsForEntryIndex(entry_idx);
Bernie Innocenti58cf8e32020-12-19 15:31:52 +0900801 if (UNLIKELY(!entry_flags.has_value())) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000802 return base::unexpected(entry_flags.error());
803 }
804 type_flags |= entry_flags.value();
805
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800806 const FilteredConfigGroup& filtered_group = loaded_package_impl.filtered_configs_[type_idx];
807 const size_t type_entry_count = (use_filtered) ? filtered_group.type_entries.size()
808 : type_spec->type_entries.size();
809 for (size_t i = 0; i < type_entry_count; i++) {
810 const TypeSpec::TypeEntry* type_entry = (use_filtered) ? filtered_group.type_entries[i]
811 : &type_spec->type_entries[i];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800812
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800813 // We can skip calling ResTable_config::match() if the caller does not care for the
814 // configuration to match or if we're using the list of types that have already had their
815 // configuration matched.
816 const ResTable_config& this_config = type_entry->config;
817 if (!(use_filtered || ignore_configuration || this_config.match(desired_config))) {
818 continue;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800819 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800820
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800821 Resolution::Step::Type resolution_type;
822 if (best_config == nullptr) {
823 resolution_type = Resolution::Step::Type::INITIAL;
824 } else if (this_config.isBetterThan(*best_config, &desired_config)) {
825 resolution_type = Resolution::Step::Type::BETTER_MATCH;
826 } else if (package_is_loader && this_config.compare(*best_config) == 0) {
827 resolution_type = Resolution::Step::Type::OVERLAID;
828 } else {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000829 if (UNLIKELY(logging_enabled)) {
Jackal Guo552b45d2021-09-29 10:52:19 +0800830 last_resolution_.steps.push_back(Resolution::Step{Resolution::Step::Type::SKIPPED,
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800831 this_config.toString(),
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800832 cookie});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800833 }
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800834 continue;
835 }
836
837 // The configuration matches and is better than the previous selection.
838 // Find the entry value if it exists for this configuration.
839 const auto& type = type_entry->type;
840 const auto offset = LoadedPackage::GetEntryOffset(type, entry_idx);
841 if (UNLIKELY(IsIOError(offset))) {
842 return base::unexpected(offset.error());
843 }
844
845 if (!offset.has_value()) {
846 if (UNLIKELY(logging_enabled)) {
Jackal Guo552b45d2021-09-29 10:52:19 +0800847 last_resolution_.steps.push_back(Resolution::Step{Resolution::Step::Type::NO_ENTRY,
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800848 this_config.toString(),
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800849 cookie});
850 }
851 continue;
852 }
853
854 best_cookie = cookie;
855 best_package = loaded_package;
856 best_type = type;
857 best_config = &this_config;
858 best_offset = offset.value();
859
860 if (UNLIKELY(logging_enabled)) {
861 last_resolution_.steps.push_back(Resolution::Step{resolution_type,
862 this_config.toString(),
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800863 cookie});
864 }
865
866 // Any configuration will suffice, so break.
867 if (stop_at_first_match) {
868 break;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700869 }
870 }
871 }
872
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800873 if (UNLIKELY(best_cookie == kInvalidCookie)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000874 return base::unexpected(std::nullopt);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700875 }
876
Eric Miao368cd192022-09-09 15:46:14 -0700877 auto best_entry_verified = LoadedPackage::GetEntryFromOffset(best_type, best_offset);
878 if (!best_entry_verified.has_value()) {
879 return base::unexpected(best_entry_verified.error());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800880 }
881
Eric Miao368cd192022-09-09 15:46:14 -0700882 const auto entry = GetEntryValue(*best_entry_verified);
Ryan Mitchell80094e32020-11-16 23:08:18 +0000883 if (!entry.has_value()) {
884 return base::unexpected(entry.error());
885 }
Winson2f3669b2019-01-11 11:28:34 -0800886
Ryan Mitchell80094e32020-11-16 23:08:18 +0000887 return FindEntryResult{
888 .cookie = best_cookie,
889 .entry = *entry,
890 .config = *best_config,
891 .type_flags = type_flags,
892 .package_name = &best_package->GetPackageName(),
893 .type_string_ref = StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1),
894 .entry_string_ref = StringPoolRef(best_package->GetKeyStringPool(),
Eric Miao368cd192022-09-09 15:46:14 -0700895 (*best_entry_verified)->key()),
Ryan Mitchell80094e32020-11-16 23:08:18 +0000896 .dynamic_ref_table = package_group.dynamic_ref_table.get(),
897 };
Adam Lesinski7ad11102016-10-28 16:39:15 -0700898}
899
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700900void AssetManager2::ResetResourceResolution() const {
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700901 last_resolution_ = Resolution{};
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700902}
903
Winson2f3669b2019-01-11 11:28:34 -0800904void AssetManager2::SetResourceResolutionLoggingEnabled(bool enabled) {
905 resource_resolution_logging_enabled_ = enabled;
Winson2f3669b2019-01-11 11:28:34 -0800906 if (!enabled) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700907 ResetResourceResolution();
Winson2f3669b2019-01-11 11:28:34 -0800908 }
909}
910
911std::string AssetManager2::GetLastResourceResolution() const {
912 if (!resource_resolution_logging_enabled_) {
913 LOG(ERROR) << "Must enable resource resolution logging before getting path.";
Ryan Mitchell80094e32020-11-16 23:08:18 +0000914 return {};
Winson2f3669b2019-01-11 11:28:34 -0800915 }
916
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800917 const ApkAssetsCookie cookie = last_resolution_.cookie;
Winson2f3669b2019-01-11 11:28:34 -0800918 if (cookie == kInvalidCookie) {
919 LOG(ERROR) << "AssetManager hasn't resolved a resource to read resolution path.";
Ryan Mitchell80094e32020-11-16 23:08:18 +0000920 return {};
Winson2f3669b2019-01-11 11:28:34 -0800921 }
922
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700923 auto op = StartOperation();
924
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800925 const uint32_t resid = last_resolution_.resid;
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700926 const auto& assets = GetApkAssets(cookie);
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700927 const auto package =
928 assets ? assets->GetLoadedArsc()->GetPackageById(get_package_id(resid)) : nullptr;
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800929
Winson2f3669b2019-01-11 11:28:34 -0800930 std::string resource_name_string;
Winson2f3669b2019-01-11 11:28:34 -0800931 if (package != nullptr) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000932 auto resource_name = ToResourceName(last_resolution_.type_string_ref,
933 last_resolution_.entry_string_ref,
934 package->GetPackageName());
935 resource_name_string = resource_name.has_value() ?
936 ToFormattedResourceString(resource_name.value()) : "<unknown>";
Winson2f3669b2019-01-11 11:28:34 -0800937 }
938
939 std::stringstream log_stream;
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800940 log_stream << base::StringPrintf("Resolution for 0x%08x %s\n"
941 "\tFor config - %s", resid, resource_name_string.c_str(),
942 configuration_.toString().c_str());
Winson2f3669b2019-01-11 11:28:34 -0800943
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800944 for (const Resolution::Step& step : last_resolution_.steps) {
945 const static std::unordered_map<Resolution::Step::Type, const char*> kStepStrings = {
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800946 {Resolution::Step::Type::INITIAL, "Found initial"},
947 {Resolution::Step::Type::BETTER_MATCH, "Found better"},
948 {Resolution::Step::Type::OVERLAID, "Overlaid"},
949 {Resolution::Step::Type::OVERLAID_INLINE, "Overlaid inline"},
950 {Resolution::Step::Type::SKIPPED, "Skipped"},
951 {Resolution::Step::Type::NO_ENTRY, "No entry"}
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800952 };
953
954 const auto prefix = kStepStrings.find(step.type);
955 if (prefix == kStepStrings.end()) {
956 continue;
Winson2f3669b2019-01-11 11:28:34 -0800957 }
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -0700958 const auto& assets = GetApkAssets(step.cookie);
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -0700959 log_stream << "\n\t" << prefix->second << ": "
960 << (assets ? assets->GetDebugName() : "<null>") << " #" << step.cookie;
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800961 if (!step.config_name.isEmpty()) {
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800962 log_stream << " - " << step.config_name;
Winson2f3669b2019-01-11 11:28:34 -0800963 }
964 }
965
Jackal Guo552b45d2021-09-29 10:52:19 +0800966 log_stream << "\nBest matching is from "
967 << (last_resolution_.best_config_name.isEmpty() ? "default"
968 : last_resolution_.best_config_name)
969 << " configuration of " << last_resolution_.best_package_name;
Winson2f3669b2019-01-11 11:28:34 -0800970 return log_stream.str();
971}
972
Felka Chang00964e92021-12-10 01:19:08 +0800973base::expected<uint32_t, NullOrIOError> AssetManager2::GetParentThemeResourceId(uint32_t resid)
974const {
975 auto entry = FindEntry(resid, 0u /* density_override */,
976 false /* stop_at_first_match */,
977 false /* ignore_configuration */);
978 if (!entry.has_value()) {
979 return base::unexpected(entry.error());
980 }
981
982 auto entry_map = std::get_if<incfs::verified_map_ptr<ResTable_map_entry>>(&entry->entry);
983 if (entry_map == nullptr) {
984 // Not a bag, nothing to do.
985 return base::unexpected(std::nullopt);
986 }
987
988 auto map = *entry_map;
989 const uint32_t parent_resid = dtohl(map->parent.ident);
990
991 return parent_resid;
992}
993
Ryan Mitchell80094e32020-11-16 23:08:18 +0000994base::expected<AssetManager2::ResourceName, NullOrIOError> AssetManager2::GetResourceName(
995 uint32_t resid) const {
996 auto result = FindEntry(resid, 0u /* density_override */, true /* stop_at_first_match */,
997 true /* ignore_configuration */);
998 if (!result.has_value()) {
999 return base::unexpected(result.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001000 }
1001
Ryan Mitchell80094e32020-11-16 23:08:18 +00001002 return ToResourceName(result->type_string_ref,
1003 result->entry_string_ref,
1004 *result->package_name);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001005}
1006
Ryan Mitchell14e8ade2021-01-11 16:01:35 -08001007base::expected<uint32_t, NullOrIOError> AssetManager2::GetResourceTypeSpecFlags(
1008 uint32_t resid) const {
1009 auto result = FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */,
1010 true /* ignore_configuration */);
1011 if (!result.has_value()) {
1012 return base::unexpected(result.error());
1013 }
1014 return result->type_flags;
1015}
1016
Ryan Mitchell80094e32020-11-16 23:08:18 +00001017base::expected<AssetManager2::SelectedValue, NullOrIOError> AssetManager2::GetResource(
1018 uint32_t resid, bool may_be_bag, uint16_t density_override) const {
1019 auto result = FindEntry(resid, density_override, false /* stop_at_first_match */,
1020 false /* ignore_configuration */);
1021 if (!result.has_value()) {
1022 return base::unexpected(result.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001023 }
1024
Ryan Mitchell80094e32020-11-16 23:08:18 +00001025 auto result_map_entry = std::get_if<incfs::verified_map_ptr<ResTable_map_entry>>(&result->entry);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -07001026 if (result_map_entry != nullptr) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001027 if (!may_be_bag) {
1028 LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001029 return base::unexpected(std::nullopt);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001030 }
Adam Lesinski0c405242017-01-13 20:47:26 -08001031
1032 // Create a reference since we can't represent this complex type as a Res_value.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001033 return SelectedValue(Res_value::TYPE_REFERENCE, resid, result->cookie, result->type_flags,
1034 resid, result->config);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001035 }
1036
Adam Lesinskida431a22016-12-29 16:08:16 -05001037 // Convert the package ID to the runtime assigned package ID.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001038 Res_value value = std::get<Res_value>(result->entry);
1039 result->dynamic_ref_table->lookupResourceValue(&value);
Adam Lesinskida431a22016-12-29 16:08:16 -05001040
Ryan Mitchell80094e32020-11-16 23:08:18 +00001041 return SelectedValue(value.dataType, value.data, result->cookie, result->type_flags,
1042 resid, result->config);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001043}
1044
Ryan Mitchell80094e32020-11-16 23:08:18 +00001045base::expected<std::monostate, NullOrIOError> AssetManager2::ResolveReference(
Ryan Mitchella45506e2020-11-16 23:08:18 +00001046 AssetManager2::SelectedValue& value, bool cache_value) const {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001047 if (value.type != Res_value::TYPE_REFERENCE || value.data == 0U) {
1048 // Not a reference. Nothing to do.
1049 return {};
Adam Lesinski0c405242017-01-13 20:47:26 -08001050 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001051
Ryan Mitchella45506e2020-11-16 23:08:18 +00001052 const uint32_t original_flags = value.flags;
1053 const uint32_t original_resid = value.data;
1054 if (cache_value) {
1055 auto cached_value = cached_resolved_values_.find(value.data);
1056 if (cached_value != cached_resolved_values_.end()) {
1057 value = cached_value->second;
1058 value.flags |= original_flags;
1059 return {};
1060 }
1061 }
1062
1063 uint32_t combined_flags = 0U;
1064 uint32_t resolve_resid = original_resid;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001065 constexpr const uint32_t kMaxIterations = 20;
1066 for (uint32_t i = 0U;; i++) {
1067 auto result = GetResource(resolve_resid, true /*may_be_bag*/);
1068 if (!result.has_value()) {
Ryan Mitchelle7ab6272020-11-13 18:06:15 -08001069 value.resid = resolve_resid;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001070 return base::unexpected(result.error());
1071 }
1072
Ryan Mitchelle7ab6272020-11-13 18:06:15 -08001073 // If resource resolution fails, the value should be set to the last reference that was able to
1074 // be resolved successfully.
1075 value = *result;
1076 value.flags |= combined_flags;
1077
Ryan Mitchell80094e32020-11-16 23:08:18 +00001078 if (result->type != Res_value::TYPE_REFERENCE ||
1079 result->data == Res_value::DATA_NULL_UNDEFINED ||
1080 result->data == resolve_resid || i == kMaxIterations) {
1081 // This reference can't be resolved, so exit now and let the caller deal with it.
Ryan Mitchella45506e2020-11-16 23:08:18 +00001082 if (cache_value) {
1083 cached_resolved_values_[original_resid] = value;
1084 }
1085
1086 // Above value is cached without original_flags to ensure they don't get included in future
1087 // queries that hit the cache
1088 value.flags |= original_flags;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001089 return {};
1090 }
1091
Ryan Mitchelle7ab6272020-11-13 18:06:15 -08001092 combined_flags = result->flags;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001093 resolve_resid = result->data;
1094 }
Adam Lesinski0c405242017-01-13 20:47:26 -08001095}
1096
Ryan Mitchell80094e32020-11-16 23:08:18 +00001097const std::vector<uint32_t> AssetManager2::GetBagResIdStack(uint32_t resid) const {
Aurimas Liutikas8f004c82019-01-17 17:20:10 -08001098 auto cached_iter = cached_bag_resid_stacks_.find(resid);
1099 if (cached_iter != cached_bag_resid_stacks_.end()) {
1100 return cached_iter->second;
Aurimas Liutikas8f004c82019-01-17 17:20:10 -08001101 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001102
1103 std::vector<uint32_t> found_resids;
1104 GetBag(resid, found_resids);
1105 cached_bag_resid_stacks_.emplace(resid, found_resids);
1106 return found_resids;
Aurimas Liutikas8f004c82019-01-17 17:20:10 -08001107}
1108
Ryan Mitchell80094e32020-11-16 23:08:18 +00001109base::expected<const ResolvedBag*, NullOrIOError> AssetManager2::ResolveBag(
1110 AssetManager2::SelectedValue& value) const {
1111 if (UNLIKELY(value.type != Res_value::TYPE_REFERENCE)) {
1112 return base::unexpected(std::nullopt);
1113 }
Aurimas Liutikas8f004c82019-01-17 17:20:10 -08001114
Ryan Mitchell80094e32020-11-16 23:08:18 +00001115 auto bag = GetBag(value.data);
1116 if (bag.has_value()) {
1117 value.flags |= (*bag)->type_spec_flags;
Aurimas Liutikas8f004c82019-01-17 17:20:10 -08001118 }
1119 return bag;
y57cd1952018-04-12 14:26:23 -07001120}
1121
Ryan Mitchell80094e32020-11-16 23:08:18 +00001122base::expected<const ResolvedBag*, NullOrIOError> AssetManager2::GetBag(uint32_t resid) const {
1123 std::vector<uint32_t> found_resids;
1124 const auto bag = GetBag(resid, found_resids);
Yurii Zubrytskyiab3cb302022-10-11 12:15:52 -07001125 cached_bag_resid_stacks_.emplace(resid, std::move(found_resids));
Ryan Mitchell80094e32020-11-16 23:08:18 +00001126 return bag;
Ryan Mitchell155d5392020-02-10 13:35:24 -08001127}
1128
Ryan Mitchell80094e32020-11-16 23:08:18 +00001129base::expected<const ResolvedBag*, NullOrIOError> AssetManager2::GetBag(
1130 uint32_t resid, std::vector<uint32_t>& child_resids) const {
1131 if (auto cached_iter = cached_bags_.find(resid); cached_iter != cached_bags_.end()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001132 return cached_iter->second.get();
1133 }
1134
Ryan Mitchell80094e32020-11-16 23:08:18 +00001135 auto entry = FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */,
1136 false /* ignore_configuration */);
1137 if (!entry.has_value()) {
1138 return base::unexpected(entry.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001139 }
1140
Ryan Mitchell80094e32020-11-16 23:08:18 +00001141 auto entry_map = std::get_if<incfs::verified_map_ptr<ResTable_map_entry>>(&entry->entry);
1142 if (entry_map == nullptr) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001143 // Not a bag, nothing to do.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001144 return base::unexpected(std::nullopt);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001145 }
1146
Ryan Mitchell80094e32020-11-16 23:08:18 +00001147 auto map = *entry_map;
1148 auto map_entry = map.offset(dtohs(map->size)).convert<ResTable_map>();
1149 const auto map_entry_end = map_entry + dtohl(map->count);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001150
y57cd1952018-04-12 14:26:23 -07001151 // Keep track of ids that have already been seen to prevent infinite loops caused by circular
Ryan Mitchell80094e32020-11-16 23:08:18 +00001152 // dependencies between bags.
y57cd1952018-04-12 14:26:23 -07001153 child_resids.push_back(resid);
1154
Adam Lesinskida431a22016-12-29 16:08:16 -05001155 uint32_t parent_resid = dtohl(map->parent.ident);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001156 if (parent_resid == 0U ||
1157 std::find(child_resids.begin(), child_resids.end(), parent_resid) != child_resids.end()) {
1158 // There is no parent or a circular parental dependency exist, meaning there is nothing to
1159 // inherit and we can do a simple copy of the entries in the map.
Adam Lesinski7ad11102016-10-28 16:39:15 -07001160 const size_t entry_count = map_entry_end - map_entry;
1161 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
1162 malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))};
Ryan Mitchell155d5392020-02-10 13:35:24 -08001163
1164 bool sort_entries = false;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001165 for (auto new_entry = new_bag->entries; map_entry != map_entry_end; ++map_entry) {
1166 if (UNLIKELY(!map_entry)) {
1167 return base::unexpected(IOError::PAGES_MISSING);
1168 }
1169
Adam Lesinskida431a22016-12-29 16:08:16 -05001170 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001171 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001172 // Attributes, arrays, etc don't have a resource id as the name. They specify
1173 // other data, which would be wrong to change via a lookup.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001174 if (UNLIKELY(entry->dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001175 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
1176 resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001177 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -05001178 }
1179 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001180
1181 new_entry->cookie = entry->cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001182 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001183 new_entry->key_pool = nullptr;
1184 new_entry->type_pool = nullptr;
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001185 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -07001186 new_entry->value.copyFrom_dtoh(map_entry->value);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001187 status_t err = entry->dynamic_ref_table->lookupResourceValue(&new_entry->value);
1188 if (UNLIKELY(err != NO_ERROR)) {
Adam Lesinski30080e22017-10-16 16:18:09 -07001189 LOG(ERROR) << base::StringPrintf(
1190 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
1191 new_entry->value.data, new_key);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001192 return base::unexpected(std::nullopt);
Adam Lesinski30080e22017-10-16 16:18:09 -07001193 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001194
Ryan Mitchell155d5392020-02-10 13:35:24 -08001195 sort_entries = sort_entries ||
1196 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001197 ++new_entry;
1198 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001199
1200 if (sort_entries) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001201 std::sort(new_bag->entries, new_bag->entries + entry_count,
1202 [](auto&& lhs, auto&& rhs) { return lhs.key < rhs.key; });
Ryan Mitchell155d5392020-02-10 13:35:24 -08001203 }
1204
Ryan Mitchell80094e32020-11-16 23:08:18 +00001205 new_bag->type_spec_flags = entry->type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001206 new_bag->entry_count = static_cast<uint32_t>(entry_count);
1207 ResolvedBag* result = new_bag.get();
1208 cached_bags_[resid] = std::move(new_bag);
1209 return result;
1210 }
1211
Adam Lesinskida431a22016-12-29 16:08:16 -05001212 // In case the parent is a dynamic reference, resolve it.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001213 entry->dynamic_ref_table->lookupResourceId(&parent_resid);
Adam Lesinskida431a22016-12-29 16:08:16 -05001214
Adam Lesinski7ad11102016-10-28 16:39:15 -07001215 // Get the parent and do a merge of the keys.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001216 const auto parent_bag = GetBag(parent_resid, child_resids);
1217 if (UNLIKELY(!parent_bag.has_value())) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001218 // Failed to get the parent that should exist.
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001219 LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid,
1220 resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001221 return base::unexpected(parent_bag.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001222 }
1223
Adam Lesinski7ad11102016-10-28 16:39:15 -07001224 // Create the max possible entries we can make. Once we construct the bag,
1225 // we will realloc to fit to size.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001226 const size_t max_count = (*parent_bag)->entry_count + dtohl(map->count);
George Burgess IV09b119f2017-07-25 15:00:04 -07001227 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
1228 malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001229 ResolvedBag::Entry* new_entry = new_bag->entries;
1230
Ryan Mitchell80094e32020-11-16 23:08:18 +00001231 const ResolvedBag::Entry* parent_entry = (*parent_bag)->entries;
1232 const ResolvedBag::Entry* const parent_entry_end = parent_entry + (*parent_bag)->entry_count;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001233
1234 // The keys are expected to be in sorted order. Merge the two bags.
Ryan Mitchell155d5392020-02-10 13:35:24 -08001235 bool sort_entries = false;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001236 while (map_entry != map_entry_end && parent_entry != parent_entry_end) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001237 if (UNLIKELY(!map_entry)) {
1238 return base::unexpected(IOError::PAGES_MISSING);
1239 }
1240
Adam Lesinskida431a22016-12-29 16:08:16 -05001241 uint32_t child_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001242 if (!is_internal_resid(child_key)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001243 if (UNLIKELY(entry->dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001244 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key,
1245 resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001246 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -05001247 }
1248 }
1249
Adam Lesinski7ad11102016-10-28 16:39:15 -07001250 if (child_key <= parent_entry->key) {
1251 // Use the child key if it comes before the parent
1252 // or is equal to the parent (overrides).
Ryan Mitchell80094e32020-11-16 23:08:18 +00001253 new_entry->cookie = entry->cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001254 new_entry->key = child_key;
1255 new_entry->key_pool = nullptr;
1256 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -07001257 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001258 new_entry->style = resid;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001259 status_t err = entry->dynamic_ref_table->lookupResourceValue(&new_entry->value);
1260 if (UNLIKELY(err != NO_ERROR)) {
Adam Lesinski30080e22017-10-16 16:18:09 -07001261 LOG(ERROR) << base::StringPrintf(
1262 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
1263 new_entry->value.data, child_key);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001264 return base::unexpected(std::nullopt);
Adam Lesinski30080e22017-10-16 16:18:09 -07001265 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001266 ++map_entry;
1267 } else {
1268 // Take the parent entry as-is.
1269 memcpy(new_entry, parent_entry, sizeof(*new_entry));
1270 }
1271
Ryan Mitchell155d5392020-02-10 13:35:24 -08001272 sort_entries = sort_entries ||
1273 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001274 if (child_key >= parent_entry->key) {
1275 // Move to the next parent entry if we used it or it was overridden.
1276 ++parent_entry;
1277 }
1278 // Increment to the next entry to fill.
1279 ++new_entry;
1280 }
1281
1282 // Finish the child entries if they exist.
1283 while (map_entry != map_entry_end) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001284 if (UNLIKELY(!map_entry)) {
1285 return base::unexpected(IOError::PAGES_MISSING);
1286 }
1287
Adam Lesinskida431a22016-12-29 16:08:16 -05001288 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001289 if (!is_internal_resid(new_key)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001290 if (UNLIKELY(entry->dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001291 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
1292 resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001293 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -05001294 }
1295 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001296 new_entry->cookie = entry->cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001297 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001298 new_entry->key_pool = nullptr;
1299 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -07001300 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001301 new_entry->style = resid;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001302 status_t err = entry->dynamic_ref_table->lookupResourceValue(&new_entry->value);
1303 if (UNLIKELY(err != NO_ERROR)) {
Adam Lesinski30080e22017-10-16 16:18:09 -07001304 LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.",
1305 new_entry->value.dataType, new_entry->value.data, new_key);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001306 return base::unexpected(std::nullopt);
Adam Lesinski30080e22017-10-16 16:18:09 -07001307 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001308 sort_entries = sort_entries ||
1309 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001310 ++map_entry;
1311 ++new_entry;
1312 }
1313
1314 // Finish the parent entries if they exist.
1315 if (parent_entry != parent_entry_end) {
1316 // Take the rest of the parent entries as-is.
1317 const size_t num_entries_to_copy = parent_entry_end - parent_entry;
1318 memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry));
1319 new_entry += num_entries_to_copy;
1320 }
1321
1322 // Resize the resulting array to fit.
1323 const size_t actual_count = new_entry - new_bag->entries;
1324 if (actual_count != max_count) {
George Burgess IV09b119f2017-07-25 15:00:04 -07001325 new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc(
1326 new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry)))));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001327 }
1328
Ryan Mitchell155d5392020-02-10 13:35:24 -08001329 if (sort_entries) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001330 std::sort(new_bag->entries, new_bag->entries + actual_count,
1331 [](auto&& lhs, auto&& rhs) { return lhs.key < rhs.key; });
Ryan Mitchell155d5392020-02-10 13:35:24 -08001332 }
1333
Adam Lesinski1a1e9c22017-10-13 15:45:34 -07001334 // Combine flags from the parent and our own bag.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001335 new_bag->type_spec_flags = entry->type_flags | (*parent_bag)->type_spec_flags;
George Burgess IV09b119f2017-07-25 15:00:04 -07001336 new_bag->entry_count = static_cast<uint32_t>(actual_count);
1337 ResolvedBag* result = new_bag.get();
1338 cached_bags_[resid] = std::move(new_bag);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001339 return result;
1340}
1341
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001342static bool Utf8ToUtf16(StringPiece str, std::u16string* out) {
Adam Lesinski929d6512017-01-16 19:11:19 -08001343 ssize_t len =
1344 utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false);
1345 if (len < 0) {
1346 return false;
1347 }
1348 out->resize(static_cast<size_t>(len));
1349 utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(),
1350 static_cast<size_t>(len + 1));
1351 return true;
1352}
1353
Ryan Mitchell80094e32020-11-16 23:08:18 +00001354base::expected<uint32_t, NullOrIOError> AssetManager2::GetResourceId(
1355 const std::string& resource_name, const std::string& fallback_type,
1356 const std::string& fallback_package) const {
Adam Lesinski929d6512017-01-16 19:11:19 -08001357 StringPiece package_name, type, entry;
1358 if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001359 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -08001360 }
1361
1362 if (entry.empty()) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001363 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -08001364 }
1365
1366 if (package_name.empty()) {
1367 package_name = fallback_package;
1368 }
1369
1370 if (type.empty()) {
1371 type = fallback_type;
1372 }
1373
1374 std::u16string type16;
1375 if (!Utf8ToUtf16(type, &type16)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001376 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -08001377 }
1378
1379 std::u16string entry16;
1380 if (!Utf8ToUtf16(entry, &entry16)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001381 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -08001382 }
1383
1384 const StringPiece16 kAttr16 = u"attr";
1385 const static std::u16string kAttrPrivate16 = u"^attr-private";
1386
1387 for (const PackageGroup& package_group : package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001388 for (const ConfiguredPackage& package_impl : package_group.packages_) {
1389 const LoadedPackage* package = package_impl.loaded_package_;
Adam Lesinski929d6512017-01-16 19:11:19 -08001390 if (package_name != package->GetPackageName()) {
1391 // All packages in the same group are expected to have the same package name.
1392 break;
1393 }
1394
Ryan Mitchell80094e32020-11-16 23:08:18 +00001395 base::expected<uint32_t, NullOrIOError> resid = package->FindEntryByName(type16, entry16);
1396 if (UNLIKELY(IsIOError(resid))) {
1397 return base::unexpected(resid.error());
1398 }
1399
1400 if (!resid.has_value() && kAttr16 == type16) {
Adam Lesinski929d6512017-01-16 19:11:19 -08001401 // Private attributes in libraries (such as the framework) are sometimes encoded
1402 // under the type '^attr-private' in order to leave the ID space of public 'attr'
1403 // free for future additions. Check '^attr-private' for the same name.
1404 resid = package->FindEntryByName(kAttrPrivate16, entry16);
1405 }
1406
Ryan Mitchell80094e32020-11-16 23:08:18 +00001407 if (resid.has_value()) {
1408 return fix_package_id(*resid, package_group.dynamic_ref_table->mAssignedPackageId);
Adam Lesinski929d6512017-01-16 19:11:19 -08001409 }
1410 }
1411 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001412 return base::unexpected(std::nullopt);
Adam Lesinski0c405242017-01-13 20:47:26 -08001413}
1414
Ryan Mitchell14e8ade2021-01-11 16:01:35 -08001415void AssetManager2::RebuildFilterList() {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001416 for (PackageGroup& group : package_groups_) {
Yurii Zubrytskyidbce3562022-11-14 22:26:10 -08001417 for (ConfiguredPackage& package : group.packages_) {
1418 package.filtered_configs_.forEachItem([](auto, auto& fcg) { fcg.type_entries.clear(); });
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001419 // Create the filters here.
Yurii Zubrytskyidbce3562022-11-14 22:26:10 -08001420 package.loaded_package_->ForEachTypeSpec([&](const TypeSpec& type_spec, uint8_t type_id) {
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -07001421 FilteredConfigGroup* group = nullptr;
Ryan Mitchell14e8ade2021-01-11 16:01:35 -08001422 for (const auto& type_entry : type_spec.type_entries) {
1423 if (type_entry.config.match(configuration_)) {
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -07001424 if (!group) {
Yurii Zubrytskyidbce3562022-11-14 22:26:10 -08001425 group = &package.filtered_configs_.editItemAt(type_id - 1);
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -07001426 }
1427 group->type_entries.push_back(&type_entry);
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001428 }
1429 }
1430 });
Yurii Zubrytskyidbce3562022-11-14 22:26:10 -08001431 package.filtered_configs_.trimBuckets(
1432 [](const auto& fcg) { return fcg.type_entries.empty(); });
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001433 }
1434 }
1435}
1436
Adam Lesinski7ad11102016-10-28 16:39:15 -07001437void AssetManager2::InvalidateCaches(uint32_t diff) {
Ryan Mitchell2c4d8742019-03-04 09:41:00 -08001438 cached_bag_resid_stacks_.clear();
1439
Adam Lesinski7ad11102016-10-28 16:39:15 -07001440 if (diff == 0xffffffffu) {
1441 // Everything must go.
1442 cached_bags_.clear();
1443 return;
1444 }
1445
1446 // Be more conservative with what gets purged. Only if the bag has other possible
1447 // variations with respect to what changed (diff) should we remove it.
1448 for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) {
1449 if (diff & iter->second->type_spec_flags) {
1450 iter = cached_bags_.erase(iter);
1451 } else {
1452 ++iter;
1453 }
1454 }
Ryan Mitchella45506e2020-11-16 23:08:18 +00001455
1456 cached_resolved_values_.clear();
Adam Lesinski7ad11102016-10-28 16:39:15 -07001457}
1458
Ryan Mitchell2e394222019-08-28 12:10:51 -07001459uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) const {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001460 for (auto& package_group : package_groups_) {
1461 for (auto& package2 : package_group.packages_) {
1462 if (package2.loaded_package_ == package) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001463 return package_group.dynamic_ref_table->mAssignedPackageId;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001464 }
1465 }
1466 }
1467 return 0;
1468}
1469
Adam Lesinski30080e22017-10-16 16:18:09 -07001470std::unique_ptr<Theme> AssetManager2::NewTheme() {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001471 constexpr size_t kInitialReserveSize = 32;
1472 auto theme = std::unique_ptr<Theme>(new Theme(this));
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001473 theme->keys_.reserve(kInitialReserveSize);
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001474 theme->entries_.reserve(kInitialReserveSize);
1475 return theme;
Adam Lesinski30080e22017-10-16 16:18:09 -07001476}
1477
Yurii Zubrytskyi9eb44c92022-11-14 23:44:52 -08001478void AssetManager2::ForEachPackage(base::function_ref<bool(const std::string&, uint8_t)> func,
1479 package_property_t excluded_property_flags) const {
1480 for (const PackageGroup& package_group : package_groups_) {
1481 const auto loaded_package = package_group.packages_.front().loaded_package_;
1482 if ((loaded_package->GetPropertyFlags() & excluded_property_flags) == 0U
1483 && !func(loaded_package->GetPackageName(),
1484 package_group.dynamic_ref_table->mAssignedPackageId)) {
1485 return;
1486 }
1487 }
1488}
1489
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -07001490AssetManager2::ScopedOperation AssetManager2::StartOperation() const {
1491 ++number_of_running_scoped_operations_;
1492 return ScopedOperation(*this);
1493}
1494
1495void AssetManager2::FinishOperation() const {
1496 if (number_of_running_scoped_operations_ < 1) {
1497 ALOGW("Invalid FinishOperation() call when there's none happening");
1498 return;
1499 }
1500 if (--number_of_running_scoped_operations_ == 0) {
1501 for (auto&& [_, assets] : apk_assets_) {
1502 assets.clear();
1503 }
1504 }
1505}
1506
1507const AssetManager2::ApkAssetsPtr& AssetManager2::GetApkAssets(ApkAssetsCookie cookie) const {
1508 DCHECK(number_of_running_scoped_operations_ > 0) << "Must have an operation running";
1509
1510 if (cookie < 0 || cookie >= apk_assets_.size()) {
1511 static const ApkAssetsPtr empty{};
1512 return empty;
1513 }
1514 auto& [wptr, res] = apk_assets_[cookie];
1515 if (!res) {
1516 res = wptr.promote();
1517 }
1518 return res;
1519}
1520
Adam Lesinski30080e22017-10-16 16:18:09 -07001521Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) {
1522}
1523
1524Theme::~Theme() = default;
1525
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001526struct Theme::Entry {
Adam Lesinski30080e22017-10-16 16:18:09 -07001527 ApkAssetsCookie cookie;
1528 uint32_t type_spec_flags;
1529 Res_value value;
1530};
1531
Ryan Mitchell80094e32020-11-16 23:08:18 +00001532base::expected<std::monostate, NullOrIOError> Theme::ApplyStyle(uint32_t resid, bool force) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001533 ATRACE_NAME("Theme::ApplyStyle");
Adam Lesinski7ad11102016-10-28 16:39:15 -07001534
Ryan Mitchell80094e32020-11-16 23:08:18 +00001535 auto bag = asset_manager_->GetBag(resid);
1536 if (!bag.has_value()) {
1537 return base::unexpected(bag.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001538 }
1539
1540 // Merge the flags from this style.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001541 type_spec_flags_ |= (*bag)->type_spec_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001542
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001543 for (auto it = begin(*bag); it != end(*bag); ++it) {
1544 const uint32_t attr_res_id = it->key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001545
Adam Lesinski30080e22017-10-16 16:18:09 -07001546 // If the resource ID passed in is not a style, the key can be some other identifier that is not
1547 // a resource ID. We should fail fast instead of operating with strange resource IDs.
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001548 if (!is_valid_resid(attr_res_id)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001549 return base::unexpected(std::nullopt);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001550 }
1551
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001552 // DATA_NULL_EMPTY (@empty) is a valid resource value and DATA_NULL_UNDEFINED represents
1553 // an absence of a valid value.
1554 bool is_undefined = it->value.dataType == Res_value::TYPE_NULL &&
1555 it->value.data != Res_value::DATA_NULL_EMPTY;
1556 if (!force && is_undefined) {
1557 continue;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001558 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001559
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001560 const auto key_it = std::lower_bound(keys_.begin(), keys_.end(), attr_res_id);
1561 const auto entry_it = entries_.begin() + (key_it - keys_.begin());
1562 if (key_it != keys_.end() && *key_it == attr_res_id) {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001563 if (is_undefined) {
1564 // DATA_NULL_UNDEFINED clears the value of the attribute in the theme only when `force` is
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001565 // true.
1566 keys_.erase(key_it);
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001567 entries_.erase(entry_it);
1568 } else if (force) {
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001569 *entry_it = Entry{it->cookie, (*bag)->type_spec_flags, it->value};
Adam Lesinski30080e22017-10-16 16:18:09 -07001570 }
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001571 } else {
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001572 keys_.insert(key_it, attr_res_id);
1573 entries_.insert(entry_it, Entry{it->cookie, (*bag)->type_spec_flags, it->value});
Adam Lesinski7ad11102016-10-28 16:39:15 -07001574 }
1575 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001576 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001577}
1578
Ryan Mitchell767e34f2021-06-07 12:29:05 -07001579void Theme::Rebase(AssetManager2* am, const uint32_t* style_ids, const uint8_t* force,
1580 size_t style_count) {
1581 ATRACE_NAME("Theme::Rebase");
1582 // Reset the entries without changing the vector capacity to prevent reallocations during
1583 // ApplyStyle.
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001584 keys_.clear();
Ryan Mitchell767e34f2021-06-07 12:29:05 -07001585 entries_.clear();
1586 asset_manager_ = am;
1587 for (size_t i = 0; i < style_count; i++) {
1588 ApplyStyle(style_ids[i], force[i]);
1589 }
1590}
1591
Ryan Mitchell80094e32020-11-16 23:08:18 +00001592std::optional<AssetManager2::SelectedValue> Theme::GetAttribute(uint32_t resid) const {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001593 constexpr const uint32_t kMaxIterations = 20;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001594 uint32_t type_spec_flags = 0u;
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001595 for (uint32_t i = 0; i <= kMaxIterations; i++) {
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001596 const auto key_it = std::lower_bound(keys_.begin(), keys_.end(), resid);
1597 if (key_it == keys_.end() || *key_it != resid) {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001598 return std::nullopt;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001599 }
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001600 const auto entry_it = entries_.begin() + (key_it - keys_.begin());
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001601 type_spec_flags |= entry_it->type_spec_flags;
1602 if (entry_it->value.dataType == Res_value::TYPE_ATTRIBUTE) {
1603 resid = entry_it->value.data;
1604 continue;
1605 }
1606
1607 return AssetManager2::SelectedValue(entry_it->value.dataType, entry_it->value.data,
1608 entry_it->cookie, type_spec_flags, 0U /* resid */,
1609 {} /* config */);
1610 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001611 return std::nullopt;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001612}
1613
Ryan Mitchell80094e32020-11-16 23:08:18 +00001614base::expected<std::monostate, NullOrIOError> Theme::ResolveAttributeReference(
1615 AssetManager2::SelectedValue& value) const {
1616 if (value.type != Res_value::TYPE_ATTRIBUTE) {
1617 return asset_manager_->ResolveReference(value);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001618 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001619
1620 std::optional<AssetManager2::SelectedValue> result = GetAttribute(value.data);
1621 if (!result.has_value()) {
1622 return base::unexpected(std::nullopt);
1623 }
1624
Ryan Mitchella45506e2020-11-16 23:08:18 +00001625 auto resolve_result = asset_manager_->ResolveReference(*result, true /* cache_value */);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001626 if (resolve_result.has_value()) {
1627 result->flags |= value.flags;
1628 value = *result;
1629 }
1630 return resolve_result;
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001631}
1632
Adam Lesinski7ad11102016-10-28 16:39:15 -07001633void Theme::Clear() {
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001634 keys_.clear();
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001635 entries_.clear();
Adam Lesinski7ad11102016-10-28 16:39:15 -07001636}
1637
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001638base::expected<std::monostate, IOError> Theme::SetTo(const Theme& source) {
1639 if (this == &source) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001640 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001641 }
1642
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001643 type_spec_flags_ = source.type_spec_flags_;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001644
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001645 if (asset_manager_ == source.asset_manager_) {
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001646 keys_ = source.keys_;
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001647 entries_ = source.entries_;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001648 } else {
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001649 std::unordered_map<ApkAssetsCookie, ApkAssetsCookie> src_to_dest_asset_cookies;
1650 using SourceToDestinationRuntimePackageMap = std::unordered_map<int, int>;
1651 std::unordered_map<ApkAssetsCookie, SourceToDestinationRuntimePackageMap> src_asset_cookie_id_map;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001652
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -07001653 auto op_src = source.asset_manager_->StartOperation();
1654 auto op_dst = asset_manager_->StartOperation();
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001655
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -07001656 for (size_t i = 0; i < source.asset_manager_->GetApkAssetsCount(); i++) {
1657 const auto& src_asset = source.asset_manager_->GetApkAssets(i);
1658 if (!src_asset) {
Yurii Zubrytskyi1cf74932023-05-01 14:35:48 -07001659 continue;
1660 }
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -07001661 for (int j = 0; j < asset_manager_->GetApkAssetsCount(); j++) {
1662 const auto& dest_asset = asset_manager_->GetApkAssets(j);
Ryan Mitchellef538432021-03-01 14:52:14 -08001663 if (src_asset != dest_asset) {
1664 // ResourcesManager caches and reuses ApkAssets when the same apk must be present in
1665 // multiple AssetManagers. Two ApkAssets point to the same version of the same resources
1666 // if they are the same instance.
1667 continue;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001668 }
Ryan Mitchellef538432021-03-01 14:52:14 -08001669
1670 // Map the package ids of the asset in the source AssetManager to the package ids of the
1671 // asset in th destination AssetManager.
1672 SourceToDestinationRuntimePackageMap package_map;
1673 for (const auto& loaded_package : src_asset->GetLoadedArsc()->GetPackages()) {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001674 const int src_package_id = source.asset_manager_->GetAssignedPackageId(
1675 loaded_package.get());
Ryan Mitchellef538432021-03-01 14:52:14 -08001676 const int dest_package_id = asset_manager_->GetAssignedPackageId(loaded_package.get());
1677 package_map[src_package_id] = dest_package_id;
1678 }
1679
1680 src_to_dest_asset_cookies.insert(std::make_pair(i, j));
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001681 src_asset_cookie_id_map.insert(std::make_pair(i, std::move(package_map)));
Ryan Mitchellef538432021-03-01 14:52:14 -08001682 break;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001683 }
1684 }
1685
Ryan Mitchell93bca972019-03-08 17:26:28 -08001686 // Reset the data in the destination theme.
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001687 keys_.clear();
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001688 entries_.clear();
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001689
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001690 for (size_t i = 0, size = source.entries_.size(); i != size; ++i) {
1691 const auto& entry = source.entries_[i];
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001692 bool is_reference = (entry.value.dataType == Res_value::TYPE_ATTRIBUTE
1693 || entry.value.dataType == Res_value::TYPE_REFERENCE
1694 || entry.value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE
1695 || entry.value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE)
1696 && entry.value.data != 0x0;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001697
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001698 // If the attribute value represents an attribute or reference, the package id of the
1699 // value needs to be rewritten to the package id of the value in the destination.
1700 uint32_t attribute_data = entry.value.data;
1701 if (is_reference) {
1702 // Determine the package id of the reference in the destination AssetManager.
1703 auto value_package_map = src_asset_cookie_id_map.find(entry.cookie);
1704 if (value_package_map == src_asset_cookie_id_map.end()) {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001705 continue;
1706 }
1707
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001708 auto value_dest_package = value_package_map->second.find(
1709 get_package_id(entry.value.data));
1710 if (value_dest_package == value_package_map->second.end()) {
1711 continue;
1712 }
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001713
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001714 attribute_data = fix_package_id(entry.value.data, value_dest_package->second);
1715 }
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001716
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001717 // Find the cookie of the value in the destination. If the source apk is not loaded in the
1718 // destination, only copy resources that do not reference resources in the source.
1719 ApkAssetsCookie data_dest_cookie;
1720 auto value_dest_cookie = src_to_dest_asset_cookies.find(entry.cookie);
1721 if (value_dest_cookie != src_to_dest_asset_cookies.end()) {
1722 data_dest_cookie = value_dest_cookie->second;
1723 } else {
1724 if (is_reference || entry.value.dataType == Res_value::TYPE_STRING) {
1725 continue;
1726 } else {
1727 data_dest_cookie = 0x0;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001728 }
1729 }
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001730
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001731 const auto source_res_id = source.keys_[i];
1732
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001733 // The package id of the attribute needs to be rewritten to the package id of the
1734 // attribute in the destination.
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001735 int attribute_dest_package_id = get_package_id(source_res_id);
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001736 if (attribute_dest_package_id != 0x01) {
1737 // Find the cookie of the attribute resource id in the source AssetManager
1738 base::expected<FindEntryResult, NullOrIOError> attribute_entry_result =
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001739 source.asset_manager_->FindEntry(source_res_id, 0 /* density_override */ ,
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001740 true /* stop_at_first_match */,
1741 true /* ignore_configuration */);
1742 if (UNLIKELY(IsIOError(attribute_entry_result))) {
1743 return base::unexpected(GetIOError(attribute_entry_result.error()));
1744 }
1745 if (!attribute_entry_result.has_value()) {
1746 continue;
1747 }
1748
1749 // Determine the package id of the attribute in the destination AssetManager.
1750 auto attribute_package_map = src_asset_cookie_id_map.find(
1751 attribute_entry_result->cookie);
1752 if (attribute_package_map == src_asset_cookie_id_map.end()) {
1753 continue;
1754 }
1755 auto attribute_dest_package = attribute_package_map->second.find(
1756 attribute_dest_package_id);
1757 if (attribute_dest_package == attribute_package_map->second.end()) {
1758 continue;
1759 }
1760 attribute_dest_package_id = attribute_dest_package->second;
1761 }
1762
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001763 auto dest_attr_id = make_resid(attribute_dest_package_id, get_type_id(source_res_id),
1764 get_entry_id(source_res_id));
1765 const auto key_it = std::lower_bound(keys_.begin(), keys_.end(), dest_attr_id);
1766 const auto entry_it = entries_.begin() + (key_it - keys_.begin());
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001767 // Since the entries were cleared, the attribute resource id has yet been mapped to any value.
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001768 keys_.insert(key_it, dest_attr_id);
1769 entries_.insert(entry_it, Entry{data_dest_cookie, entry.type_spec_flags,
1770 Res_value{.dataType = entry.value.dataType,
1771 .data = attribute_data}});
Adam Lesinski7ad11102016-10-28 16:39:15 -07001772 }
1773 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001774 return {};
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001775}
1776
1777void Theme::Dump() const {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001778 LOG(INFO) << base::StringPrintf("Theme(this=%p, AssetManager2=%p)", this, asset_manager_);
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001779 for (size_t i = 0, size = keys_.size(); i != size; ++i) {
1780 auto res_id = keys_[i];
1781 const auto& entry = entries_[i];
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001782 LOG(INFO) << base::StringPrintf(" entry(0x%08x)=(0x%08x) type=(0x%02x), cookie(%d)",
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001783 res_id, entry.value.data, entry.value.dataType,
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001784 entry.cookie);
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001785 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001786}
1787
Yurii Zubrytskyi657f3c62023-05-10 13:16:23 -07001788AssetManager2::ScopedOperation::ScopedOperation(const AssetManager2& am) : am_(am) {
1789}
1790
1791AssetManager2::ScopedOperation::~ScopedOperation() {
1792 am_.FinishOperation();
1793}
1794
Adam Lesinski7ad11102016-10-28 16:39:15 -07001795} // namespace android