blob: 46f636e2ae7f6054e57895a0f0a35d5d8e57adea [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
Ryan Prichard41e15a02023-08-30 22:19:30 -070094struct Theme::Entry {
95 ApkAssetsCookie cookie;
96 uint32_t type_spec_flags;
97 Res_value value;
98};
99
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000100AssetManager2::AssetManager2(ApkAssetsList apk_assets, const ResTable_config& configuration) {
101 configurations_.push_back(configuration);
102
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700103 // Don't invalidate caches here as there's nothing cached yet.
104 SetApkAssets(apk_assets, false);
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700105}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700106
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000107AssetManager2::AssetManager2() {
108 configurations_.resize(1);
109}
110
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700111bool AssetManager2::SetApkAssets(ApkAssetsList apk_assets, bool invalidate_caches) {
112 BuildDynamicRefTable(apk_assets);
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800113 RebuildFilterList();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700114 if (invalidate_caches) {
115 InvalidateCaches(static_cast<uint32_t>(-1));
116 }
117 return true;
118}
119
Yurii Zubrytskyi8bd39032024-01-25 11:07:48 -0800120void AssetManager2::PresetApkAssets(ApkAssetsList apk_assets) {
121 BuildDynamicRefTable(apk_assets);
122}
123
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700124bool AssetManager2::SetApkAssets(std::initializer_list<ApkAssetsPtr> apk_assets,
125 bool invalidate_caches) {
126 return SetApkAssets(ApkAssetsList(apk_assets.begin(), apk_assets.size()), invalidate_caches);
127}
128
129void AssetManager2::BuildDynamicRefTable(ApkAssetsList apk_assets) {
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700130 auto op = StartOperation();
131
132 apk_assets_.resize(apk_assets.size());
133 for (size_t i = 0; i != apk_assets.size(); ++i) {
134 apk_assets_[i].first = apk_assets[i];
135 // Let's populate the locked assets right away as we're going to need them here later.
136 apk_assets_[i].second = apk_assets[i];
137 }
138
Adam Lesinskida431a22016-12-29 16:08:16 -0500139 package_groups_.clear();
140 package_ids_.fill(0xff);
141
Ryan Mitchellef538432021-03-01 14:52:14 -0800142 // A mapping from path of apk assets that could be target packages of overlays to the runtime
143 // package id of its first loaded package. Overlays currently can only override resources in the
144 // first package in the target resource table.
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800145 std::unordered_map<std::string_view, uint8_t> target_assets_package_ids;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700146
Ryan Mitchell824cc492020-02-12 10:48:14 -0800147 // Overlay resources are not directly referenced by an application so their resource ids
148 // can change throughout the application's lifetime. Assign overlay package ids last.
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700149 std::vector<const ApkAssets*> sorted_apk_assets;
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700150 sorted_apk_assets.reserve(apk_assets.size());
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700151 for (auto& asset : apk_assets) {
152 sorted_apk_assets.push_back(asset.get());
153 }
154 std::stable_partition(sorted_apk_assets.begin(), sorted_apk_assets.end(),
155 [](auto a) { return !a->IsOverlay(); });
Ryan Mitchell824cc492020-02-12 10:48:14 -0800156
157 // The assets cookie must map to the position of the apk assets in the unsorted apk assets list.
158 std::unordered_map<const ApkAssets*, ApkAssetsCookie> apk_assets_cookies;
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700159 apk_assets_cookies.reserve(apk_assets.size());
160 for (size_t i = 0, n = apk_assets.size(); i < n; i++) {
161 apk_assets_cookies[apk_assets[i].get()] = static_cast<ApkAssetsCookie>(i);
Ryan Mitchell824cc492020-02-12 10:48:14 -0800162 }
163
Ryan Mitchellb894c272020-02-12 10:31:44 -0800164 // 0x01 is reserved for the android package.
165 int next_package_id = 0x02;
Ryan Mitchell824cc492020-02-12 10:48:14 -0800166 for (const ApkAssets* apk_assets : sorted_apk_assets) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800167 std::shared_ptr<OverlayDynamicRefTable> overlay_ref_table;
168 if (auto loaded_idmap = apk_assets->GetLoadedIdmap(); loaded_idmap != nullptr) {
169 // The target package must precede the overlay package in the apk assets paths in order
170 // to take effect.
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800171 auto iter = target_assets_package_ids.find(loaded_idmap->TargetApkPath());
Ryan Mitchellef538432021-03-01 14:52:14 -0800172 if (iter == target_assets_package_ids.end()) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800173 LOG(INFO) << "failed to find target package for overlay "
174 << loaded_idmap->OverlayApkPath();
175 } else {
176 uint8_t target_package_id = iter->second;
177
178 // Create a special dynamic reference table for the overlay to rewrite references to
179 // overlay resources as references to the target resources they overlay.
180 overlay_ref_table = std::make_shared<OverlayDynamicRefTable>(
181 loaded_idmap->GetOverlayDynamicRefTable(target_package_id));
182
183 // Add the overlay resource map to the target package's set of overlays.
184 const uint8_t target_idx = package_ids_[target_package_id];
185 CHECK(target_idx != 0xff) << "overlay target '" << loaded_idmap->TargetApkPath()
186 << "'added to apk_assets_package_ids but does not have an"
187 << " assigned package group";
188
189 PackageGroup& target_package_group = package_groups_[target_idx];
190 target_package_group.overlays_.push_back(
191 ConfiguredOverlay{loaded_idmap->GetTargetResourcesMap(target_package_id,
192 overlay_ref_table.get()),
193 apk_assets_cookies[apk_assets]});
194 }
195 }
196
Ryan Mitchellb894c272020-02-12 10:31:44 -0800197 const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc();
Ryan Mitchellb894c272020-02-12 10:31:44 -0800198 for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) {
199 // Get the package ID or assign one if a shared library.
200 int package_id;
201 if (package->IsDynamic()) {
202 package_id = next_package_id++;
203 } else {
204 package_id = package->GetPackageId();
Adam Lesinskida431a22016-12-29 16:08:16 -0500205 }
206
Adam Lesinskida431a22016-12-29 16:08:16 -0500207 uint8_t idx = package_ids_[package_id];
208 if (idx == 0xff) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800209 // Add the mapping for package ID to index if not present.
Adam Lesinskida431a22016-12-29 16:08:16 -0500210 package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size());
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800211 PackageGroup& new_group = package_groups_.emplace_back();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700212
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800213 if (overlay_ref_table != nullptr) {
214 // If this package is from an overlay, use a dynamic reference table that can rewrite
215 // overlay resource ids to their corresponding target resource ids.
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800216 new_group.dynamic_ref_table = std::move(overlay_ref_table);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700217 }
218
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800219 DynamicRefTable* ref_table = new_group.dynamic_ref_table.get();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700220 ref_table->mAssignedPackageId = package_id;
221 ref_table->mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f;
Adam Lesinskida431a22016-12-29 16:08:16 -0500222 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500223
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800224 // Add the package to the set of packages with the same ID.
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800225 PackageGroup* package_group = &package_groups_[idx];
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800226 package_group->packages_.emplace_back().loaded_package_ = package.get();
Ryan Mitchell824cc492020-02-12 10:48:14 -0800227 package_group->cookies_.push_back(apk_assets_cookies[apk_assets]);
Adam Lesinskida431a22016-12-29 16:08:16 -0500228
229 // Add the package name -> build time ID mappings.
230 for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) {
231 String16 package_name(entry.package_name.c_str(), entry.package_name.size());
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700232 package_group->dynamic_ref_table->mEntries.replaceValueFor(
Adam Lesinskida431a22016-12-29 16:08:16 -0500233 package_name, static_cast<uint8_t>(entry.package_id));
234 }
Ryan Mitchellb894c272020-02-12 10:31:44 -0800235
Ryan Mitchellef538432021-03-01 14:52:14 -0800236 if (auto apk_assets_path = apk_assets->GetPath()) {
237 // Overlay target ApkAssets must have been created using path based load apis.
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800238 target_assets_package_ids.emplace(*apk_assets_path, package_id);
Ryan Mitchellef538432021-03-01 14:52:14 -0800239 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500240 }
241 }
242
243 // Now assign the runtime IDs so that we have a build-time to runtime ID map.
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -0700244 DynamicRefTable::AliasMap aliases;
245 for (const auto& group : package_groups_) {
246 const std::string& package_name = group.packages_[0].loaded_package_->GetPackageName();
247 const auto name_16 = String16(package_name.c_str(), package_name.size());
248 for (auto&& inner_group : package_groups_) {
249 inner_group.dynamic_ref_table->addMapping(name_16,
250 group.dynamic_ref_table->mAssignedPackageId);
Adam Lesinskida431a22016-12-29 16:08:16 -0500251 }
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -0700252
253 for (const auto& package : group.packages_) {
254 const auto& package_aliases = package.loaded_package_->GetAliasResourceIdMap();
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800255 aliases.insert(aliases.end(), package_aliases.begin(), package_aliases.end());
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -0700256 }
257 }
258
259 if (!aliases.empty()) {
Yurii Zubrytskyie3f9be62022-11-14 18:30:10 -0800260 std::sort(aliases.begin(), aliases.end(), [](auto&& l, auto&& r) { return l.first < r.first; });
261
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -0700262 // Add the alias resources to the dynamic reference table of every package group. Since
263 // staging aliases can only be defined by the framework package (which is not a shared
264 // library), the compile-time package id of the framework is the same across all packages
265 // that compile against the framework.
266 for (auto& group : std::span(package_groups_.data(), package_groups_.size() - 1)) {
267 group.dynamic_ref_table->setAliases(aliases);
268 }
269 package_groups_.back().dynamic_ref_table->setAliases(std::move(aliases));
Adam Lesinskida431a22016-12-29 16:08:16 -0500270 }
271}
272
273void AssetManager2::DumpToLog() const {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800274 LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this);
275
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700276 auto op = StartOperation();
Adam Lesinskida431a22016-12-29 16:08:16 -0500277 std::string list;
Yurii Zubrytskyi7d70bc52023-05-12 13:27:54 -0700278 for (size_t i = 0, s = apk_assets_.size(); i < s; ++i) {
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700279 const auto& assets = GetApkAssets(i);
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700280 base::StringAppendF(&list, "%s,", assets ? assets->GetDebugName().c_str() : "nullptr");
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800281 }
282 LOG(INFO) << "ApkAssets: " << list;
283
284 list = "";
Adam Lesinskida431a22016-12-29 16:08:16 -0500285 for (size_t i = 0; i < package_ids_.size(); i++) {
286 if (package_ids_[i] != 0xff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800287 base::StringAppendF(&list, "%02x -> %d, ", (int)i, package_ids_[i]);
Adam Lesinskida431a22016-12-29 16:08:16 -0500288 }
289 }
290 LOG(INFO) << "Package ID map: " << list;
291
Adam Lesinski0dd36992018-01-25 15:38:38 -0800292 for (const auto& package_group: package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800293 list = "";
294 for (const auto& package : package_group.packages_) {
295 const LoadedPackage* loaded_package = package.loaded_package_;
296 base::StringAppendF(&list, "%s(%02x%s), ", loaded_package->GetPackageName().c_str(),
297 loaded_package->GetPackageId(),
298 (loaded_package->IsDynamic() ? " dynamic" : ""));
299 }
300 LOG(INFO) << base::StringPrintf("PG (%02x): ",
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700301 package_group.dynamic_ref_table->mAssignedPackageId)
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800302 << list;
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800303
304 for (size_t i = 0; i < 256; i++) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700305 if (package_group.dynamic_ref_table->mLookupTable[i] != 0) {
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800306 LOG(INFO) << base::StringPrintf(" e[0x%02x] -> 0x%02x", (uint8_t) i,
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700307 package_group.dynamic_ref_table->mLookupTable[i]);
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800308 }
309 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500310 }
311}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700312
313const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const {
314 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
315 return nullptr;
316 }
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700317 auto op = StartOperation();
318 const auto& assets = GetApkAssets(cookie);
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700319 return assets ? assets->GetLoadedArsc()->GetStringPool() : nullptr;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700320}
321
Adam Lesinskida431a22016-12-29 16:08:16 -0500322const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const {
323 if (package_id >= package_ids_.size()) {
324 return nullptr;
325 }
326
327 const size_t idx = package_ids_[package_id];
328 if (idx == 0xff) {
329 return nullptr;
330 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700331 return package_groups_[idx].dynamic_ref_table.get();
Adam Lesinskida431a22016-12-29 16:08:16 -0500332}
333
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700334std::shared_ptr<const DynamicRefTable> AssetManager2::GetDynamicRefTableForCookie(
335 ApkAssetsCookie cookie) const {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800336 for (const PackageGroup& package_group : package_groups_) {
337 for (const ApkAssetsCookie& package_cookie : package_group.cookies_) {
338 if (package_cookie == cookie) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700339 return package_group.dynamic_ref_table;
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800340 }
341 }
342 }
343 return nullptr;
344}
345
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100346const std::unordered_map<std::string, std::string>*
347 AssetManager2::GetOverlayableMapForPackage(uint32_t package_id) const {
348
349 if (package_id >= package_ids_.size()) {
350 return nullptr;
351 }
352
353 const size_t idx = package_ids_[package_id];
354 if (idx == 0xff) {
355 return nullptr;
356 }
357
358 const PackageGroup& package_group = package_groups_[idx];
Ryan Mitchell80094e32020-11-16 23:08:18 +0000359 if (package_group.packages_.empty()) {
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100360 return nullptr;
361 }
362
363 const auto loaded_package = package_group.packages_[0].loaded_package_;
364 return &loaded_package->GetOverlayableMap();
365}
366
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700367bool AssetManager2::GetOverlayablesToString(android::StringPiece package_name,
Ryan Mitchell2e394222019-08-28 12:10:51 -0700368 std::string* out) const {
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700369 auto op = StartOperation();
Ryan Mitchell2e394222019-08-28 12:10:51 -0700370 uint8_t package_id = 0U;
Yurii Zubrytskyi7d70bc52023-05-12 13:27:54 -0700371 for (size_t i = 0, s = apk_assets_.size(); i != s; ++i) {
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700372 const auto& assets = GetApkAssets(i);
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700373 if (!assets) {
374 continue;
375 }
376 const LoadedArsc* loaded_arsc = assets->GetLoadedArsc();
Ryan Mitchell2e394222019-08-28 12:10:51 -0700377 if (loaded_arsc == nullptr) {
378 continue;
379 }
380
381 const auto& loaded_packages = loaded_arsc->GetPackages();
382 if (loaded_packages.empty()) {
383 continue;
384 }
385
386 const auto& loaded_package = loaded_packages[0];
387 if (loaded_package->GetPackageName() == package_name) {
388 package_id = GetAssignedPackageId(loaded_package.get());
389 break;
390 }
391 }
392
393 if (package_id == 0U) {
394 ANDROID_LOG(ERROR) << base::StringPrintf("No package with name '%s", package_name.data());
395 return false;
396 }
397
398 const size_t idx = package_ids_[package_id];
399 if (idx == 0xff) {
400 return false;
401 }
402
403 std::string output;
404 for (const ConfiguredPackage& package : package_groups_[idx].packages_) {
405 const LoadedPackage* loaded_package = package.loaded_package_;
406 for (auto it = loaded_package->begin(); it != loaded_package->end(); it++) {
407 const OverlayableInfo* info = loaded_package->GetOverlayableInfo(*it);
408 if (info != nullptr) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000409 auto res_name = GetResourceName(*it);
410 if (!res_name.has_value()) {
Ryan Mitchell2e394222019-08-28 12:10:51 -0700411 ANDROID_LOG(ERROR) << base::StringPrintf(
412 "Unable to retrieve name of overlayable resource 0x%08x", *it);
413 return false;
414 }
415
Ryan Mitchell80094e32020-11-16 23:08:18 +0000416 const std::string name = ToFormattedResourceString(*res_name);
Ryan Mitchell2e394222019-08-28 12:10:51 -0700417 output.append(base::StringPrintf(
418 "resource='%s' overlayable='%s' actor='%s' policy='0x%08x'\n",
Yurii Zubrytskyi9d225372022-11-29 11:12:18 -0800419 name.c_str(), info->name.data(), info->actor.data(), info->policy_flags));
Ryan Mitchell2e394222019-08-28 12:10:51 -0700420 }
421 }
422 }
423
424 *out = std::move(output);
425 return true;
426}
427
Ryan Mitchell192400c2020-04-02 09:54:23 -0700428bool AssetManager2::ContainsAllocatedTable() const {
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700429 auto op = StartOperation();
Yurii Zubrytskyi7d70bc52023-05-12 13:27:54 -0700430 for (size_t i = 0, s = apk_assets_.size(); i != s; ++i) {
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700431 const auto& assets = GetApkAssets(i);
432 if (assets && assets->IsTableAllocated()) {
433 return true;
434 }
435 }
436 return false;
Ryan Mitchell192400c2020-04-02 09:54:23 -0700437}
438
Yurii Zubrytskyi8bd39032024-01-25 11:07:48 -0800439void AssetManager2::SetConfigurations(std::vector<ResTable_config> configurations,
440 bool force_refresh) {
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000441 int diff = 0;
Yurii Zubrytskyi8bd39032024-01-25 11:07:48 -0800442 if (force_refresh) {
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000443 diff = -1;
444 } else {
Yurii Zubrytskyi8bd39032024-01-25 11:07:48 -0800445 if (configurations_.size() != configurations.size()) {
446 diff = -1;
447 } else {
448 for (int i = 0; i < configurations_.size(); i++) {
449 diff |= configurations_[i].diff(configurations[i]);
450 }
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000451 }
452 }
453 configurations_ = std::move(configurations);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700454
455 if (diff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800456 RebuildFilterList();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700457 InvalidateCaches(static_cast<uint32_t>(diff));
458 }
459}
460
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700461std::set<AssetManager2::ApkAssetsPtr> AssetManager2::GetNonSystemOverlays() const {
462 std::set<ApkAssetsPtr> non_system_overlays;
Adam Lesinski0c405242017-01-13 20:47:26 -0800463 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800464 bool found_system_package = false;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800465 for (const ConfiguredPackage& package : package_group.packages_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700466 if (package.loaded_package_->IsSystem()) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800467 found_system_package = true;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700468 break;
469 }
470 }
471
472 if (!found_system_package) {
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700473 auto op = StartOperation();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700474 for (const ConfiguredOverlay& overlay : package_group.overlays_) {
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700475 if (const auto& asset = GetApkAssets(overlay.cookie)) {
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700476 non_system_overlays.insert(std::move(asset));
477 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700478 }
479 }
480 }
481
482 return non_system_overlays;
483}
484
Ryan Mitchell80094e32020-11-16 23:08:18 +0000485base::expected<std::set<ResTable_config>, IOError> AssetManager2::GetResourceConfigurations(
486 bool exclude_system, bool exclude_mipmap) const {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700487 ATRACE_NAME("AssetManager::GetResourceConfigurations");
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700488 auto op = StartOperation();
489
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700490 const auto non_system_overlays =
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700491 exclude_system ? GetNonSystemOverlays() : std::set<ApkAssetsPtr>();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700492
493 std::set<ResTable_config> configurations;
494 for (const PackageGroup& package_group : package_groups_) {
495 for (size_t i = 0; i < package_group.packages_.size(); i++) {
496 const ConfiguredPackage& package = package_group.packages_[i];
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700497 if (exclude_system) {
498 if (package.loaded_package_->IsSystem()) {
499 continue;
500 }
501 if (!non_system_overlays.empty()) {
502 // Exclude overlays that target only system resources.
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700503 const auto& apk_assets = GetApkAssets(package_group.cookies_[i]);
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700504 if (apk_assets && apk_assets->IsOverlay() &&
505 non_system_overlays.find(apk_assets) == non_system_overlays.end()) {
506 continue;
507 }
508 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800509 }
510
Ryan Mitchell80094e32020-11-16 23:08:18 +0000511 auto result = package.loaded_package_->CollectConfigurations(exclude_mipmap, &configurations);
512 if (UNLIKELY(!result.has_value())) {
513 return base::unexpected(result.error());
514 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800515 }
516 }
517 return configurations;
518}
519
520std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800521 bool merge_equivalent_languages) const {
522 ATRACE_NAME("AssetManager::GetResourceLocales");
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700523 auto op = StartOperation();
524
Adam Lesinski0c405242017-01-13 20:47:26 -0800525 std::set<std::string> locales;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700526 const auto non_system_overlays =
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700527 exclude_system ? GetNonSystemOverlays() : std::set<ApkAssetsPtr>();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700528
Adam Lesinski0c405242017-01-13 20:47:26 -0800529 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700530 for (size_t i = 0; i < package_group.packages_.size(); i++) {
531 const ConfiguredPackage& package = package_group.packages_[i];
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700532 if (exclude_system) {
533 if (package.loaded_package_->IsSystem()) {
534 continue;
535 }
536 if (!non_system_overlays.empty()) {
537 // Exclude overlays that target only system resources.
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700538 const auto& apk_assets = GetApkAssets(package_group.cookies_[i]);
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700539 if (apk_assets && apk_assets->IsOverlay() &&
540 non_system_overlays.find(apk_assets) == non_system_overlays.end()) {
541 continue;
542 }
543 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800544 }
545
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800546 package.loaded_package_->CollectLocales(merge_equivalent_languages, &locales);
Adam Lesinski0c405242017-01-13 20:47:26 -0800547 }
548 }
549 return locales;
550}
551
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800552std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename,
553 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700554 const std::string new_path = "assets/" + filename;
555 return OpenNonAsset(new_path, mode);
556}
557
558std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800559 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700560 const std::string new_path = "assets/" + filename;
561 return OpenNonAsset(new_path, cookie, mode);
562}
563
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800564std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) const {
565 ATRACE_NAME("AssetManager::OpenDir");
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700566 auto op = StartOperation();
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800567
568 std::string full_path = "assets/" + dirname;
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700569 auto files = util::make_unique<SortedVector<AssetDir::FileInfo>>();
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800570
571 // Start from the back.
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700572 for (size_t i = apk_assets_.size(); i > 0; --i) {
573 const auto& apk_assets = GetApkAssets(i - 1);
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700574 if (!apk_assets || apk_assets->IsOverlay()) {
MÃ¥rten Kongstaddbf343b2019-02-21 07:54:18 +0100575 continue;
576 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800577
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700578 auto func = [&](StringPiece name, FileType type) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800579 AssetDir::FileInfo info;
580 info.setFileName(String8(name.data(), name.size()));
581 info.setFileType(type);
Ryan Mitchellef538432021-03-01 14:52:14 -0800582 info.setSourceName(String8(apk_assets->GetDebugName().c_str()));
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800583 files->add(info);
584 };
585
Ryan Mitchellc07aa702020-03-10 13:49:12 -0700586 if (!apk_assets->GetAssetsProvider()->ForEachFile(full_path, func)) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800587 return {};
588 }
589 }
590
591 std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>();
592 asset_dir->setFileList(files.release());
593 return asset_dir;
594}
595
Adam Lesinski7ad11102016-10-28 16:39:15 -0700596// Search in reverse because that's how we used to do it and we need to preserve behaviour.
597// This is unfortunate, because ClassLoaders delegate to the parent first, so the order
598// is inconsistent for split APKs.
599std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
600 Asset::AccessMode mode,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800601 ApkAssetsCookie* out_cookie) const {
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700602 auto op = StartOperation();
603 for (size_t i = apk_assets_.size(); i > 0; i--) {
604 const auto& assets = GetApkAssets(i - 1);
MÃ¥rten Kongstaddbf343b2019-02-21 07:54:18 +0100605 // Prevent RRO from modifying assets and other entries accessed by file
606 // path. Explicitly asking for a path in a given package (denoted by a
607 // cookie) is still OK.
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700608 if (!assets || assets->IsOverlay()) {
MÃ¥rten Kongstaddbf343b2019-02-21 07:54:18 +0100609 continue;
610 }
611
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700612 std::unique_ptr<Asset> asset = assets->GetAssetsProvider()->Open(filename, mode);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700613 if (asset) {
614 if (out_cookie != nullptr) {
Yurii Zubrytskyif48a56f2023-11-21 08:15:13 -0800615 *out_cookie = i - 1;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700616 }
617 return asset;
618 }
619 }
620
621 if (out_cookie != nullptr) {
622 *out_cookie = kInvalidCookie;
623 }
624 return {};
625}
626
627std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800628 ApkAssetsCookie cookie,
629 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700630 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
631 return {};
632 }
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700633 auto op = StartOperation();
634 const auto& assets = GetApkAssets(cookie);
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700635 return assets ? assets->GetAssetsProvider()->Open(filename, mode) : nullptr;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700636}
637
Ryan Mitchell80094e32020-11-16 23:08:18 +0000638base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntry(
639 uint32_t resid, uint16_t density_override, bool stop_at_first_match,
640 bool ignore_configuration) const {
641 const bool logging_enabled = resource_resolution_logging_enabled_;
642 if (UNLIKELY(logging_enabled)) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700643 // Clear the last logged resource resolution.
644 ResetResourceResolution();
645 last_resolution_.resid = resid;
646 }
647
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700648 auto op = StartOperation();
649
Adam Lesinski7ad11102016-10-28 16:39:15 -0700650
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700651 // Retrieve the package group from the package id of the resource id.
Ryan Mitchell80094e32020-11-16 23:08:18 +0000652 if (UNLIKELY(!is_valid_resid(resid))) {
Mark Hansenb406b0e2022-10-14 02:18:37 +0000653 LOG(ERROR) << base::StringPrintf("Invalid resource ID 0x%08x.", resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +0000654 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -0500655 }
656
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800657 const uint32_t package_id = get_package_id(resid);
658 const uint8_t type_idx = get_type_id(resid) - 1;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800659 const uint16_t entry_idx = get_entry_id(resid);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700660 uint8_t package_idx = package_ids_[package_id];
Ryan Mitchell80094e32020-11-16 23:08:18 +0000661 if (UNLIKELY(package_idx == 0xff)) {
Mark Hansenb406b0e2022-10-14 02:18:37 +0000662 ANDROID_LOG(ERROR) << base::StringPrintf("No package ID %02x found for resource ID 0x%08x.",
Ryan Mitchell2fe23472019-02-27 09:43:01 -0800663 package_id, resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +0000664 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -0500665 }
666
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800667 const PackageGroup& package_group = package_groups_[package_idx];
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000668 std::optional<FindEntryResult> final_result;
669 bool final_has_locale = false;
670 bool final_overlaid = false;
671 for (auto & config : configurations_) {
672 // Might use this if density_override != 0.
673 ResTable_config density_override_config;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800674
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000675 // Select our configuration or generate a density override configuration.
676 const ResTable_config* desired_config = &config;
677 if (density_override != 0 && density_override != config.density) {
678 density_override_config = config;
679 density_override_config.density = density_override;
680 desired_config = &density_override_config;
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700681 }
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000682
683 auto result = FindEntryInternal(package_group, type_idx, entry_idx, *desired_config,
684 stop_at_first_match, ignore_configuration);
685 if (UNLIKELY(!result.has_value())) {
686 return base::unexpected(result.error());
687 }
688 bool overlaid = false;
689 if (!stop_at_first_match && !ignore_configuration) {
690 const auto& assets = GetApkAssets(result->cookie);
691 if (!assets) {
692 ALOGE("Found expired ApkAssets #%d for resource ID 0x%08x.", result->cookie, resid);
693 return base::unexpected(std::nullopt);
694 }
695 if (!assets->IsLoader()) {
696 for (const auto& id_map : package_group.overlays_) {
697 auto overlay_entry = id_map.overlay_res_maps_.Lookup(resid);
698 if (!overlay_entry) {
699 // No id map entry exists for this target resource.
Jeremy Meyer04cf00d2023-07-20 22:17:27 +0000700 continue;
701 }
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000702 if (overlay_entry.IsInlineValue()) {
703 // The target resource is overlaid by an inline value not represented by a resource.
704 ConfigDescription best_frro_config;
705 Res_value best_frro_value;
706 bool frro_found = false;
707 for( const auto& [config, value] : overlay_entry.GetInlineValue()) {
708 if ((!frro_found || config.isBetterThan(best_frro_config, desired_config))
709 && config.match(*desired_config)) {
710 frro_found = true;
711 best_frro_config = config;
712 best_frro_value = value;
713 }
714 }
715 if (!frro_found) {
716 continue;
717 }
718 result->entry = best_frro_value;
719 result->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
720 result->cookie = id_map.cookie;
721
722 if (UNLIKELY(logging_enabled)) {
723 last_resolution_.steps.push_back(Resolution::Step{
724 Resolution::Step::Type::OVERLAID_INLINE, result->cookie, String8()});
725 if (auto path = assets->GetPath()) {
726 const std::string overlay_path = path->data();
727 if (IsFabricatedOverlay(overlay_path)) {
728 // FRRO don't have package name so we use the creating package here.
729 String8 frro_name = String8("FRRO");
730 // Get the first part of it since the expected one should be like
731 // {overlayPackageName}-{overlayName}-{4 alphanumeric chars}.frro
732 // under /data/resource-cache/.
733 const std::string name = overlay_path.substr(overlay_path.rfind('/') + 1);
734 const size_t end = name.find('-');
735 if (frro_name.size() != overlay_path.size() && end != std::string::npos) {
736 frro_name.append(base::StringPrintf(" created by %s",
737 name.substr(0 /* pos */,
738 end).c_str()).c_str());
739 }
740 last_resolution_.best_package_name = frro_name;
741 } else {
742 last_resolution_.best_package_name = result->package_name->c_str();
743 }
744 }
745 overlaid = true;
746 }
747 continue;
748 }
749
750 auto overlay_result = FindEntry(overlay_entry.GetResourceId(), density_override,
751 false /* stop_at_first_match */,
752 false /* ignore_configuration */);
753 if (UNLIKELY(IsIOError(overlay_result))) {
754 return base::unexpected(overlay_result.error());
755 }
756 if (!overlay_result.has_value()) {
757 continue;
758 }
759
760 if (!overlay_result->config.isBetterThan(result->config, desired_config)
761 && overlay_result->config.compare(result->config) != 0) {
762 // The configuration of the entry for the overlay must be equal to or better than the
763 // target configuration to be chosen as the better value.
764 continue;
765 }
766
767 result->cookie = overlay_result->cookie;
768 result->entry = overlay_result->entry;
769 result->config = overlay_result->config;
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700770 result->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700771
772 if (UNLIKELY(logging_enabled)) {
773 last_resolution_.steps.push_back(
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000774 Resolution::Step{Resolution::Step::Type::OVERLAID, overlay_result->cookie,
775 overlay_result->config.toString()});
776 last_resolution_.best_package_name =
777 overlay_result->package_name->c_str();
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700778 overlaid = true;
779 }
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800780 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700781 }
782 }
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000783
784 bool has_locale = false;
785 if (result->config.locale == 0) {
786 if (default_locale_ != 0) {
Yurii Zubrytskyi8bd39032024-01-25 11:07:48 -0800787 ResTable_config conf = {.locale = default_locale_};
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000788 // Since we know conf has a locale and only a locale, match will tell us if that locale
789 // matches
790 has_locale = conf.match(config);
791 }
792 } else {
793 has_locale = true;
794 }
795
Jeremy Meyer2ae6ed592023-09-13 12:39:53 -0700796 // if we don't have a result yet
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000797 if (!final_result ||
798 // or this config is better before the locale than the existing result
799 result->config.isBetterThanBeforeLocale(final_result->config, desired_config) ||
800 // or the existing config isn't better before locale and this one specifies a locale
801 // whereas the existing one doesn't
802 (!final_result->config.isBetterThanBeforeLocale(result->config, desired_config)
803 && has_locale && !final_has_locale)) {
804 final_result = result.value();
805 final_overlaid = overlaid;
806 final_has_locale = has_locale;
807 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700808 }
809
Ryan Mitchell80094e32020-11-16 23:08:18 +0000810 if (UNLIKELY(logging_enabled)) {
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000811 last_resolution_.cookie = final_result->cookie;
812 last_resolution_.type_string_ref = final_result->type_string_ref;
813 last_resolution_.entry_string_ref = final_result->entry_string_ref;
814 last_resolution_.best_config_name = final_result->config.toString();
815 if (!final_overlaid) {
816 last_resolution_.best_package_name = final_result->package_name->c_str();
Jackal Guo552b45d2021-09-29 10:52:19 +0800817 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700818 }
819
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000820 return *final_result;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700821}
822
Ryan Mitchell80094e32020-11-16 23:08:18 +0000823base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntryInternal(
824 const PackageGroup& package_group, uint8_t type_idx, uint16_t entry_idx,
825 const ResTable_config& desired_config, bool stop_at_first_match,
826 bool ignore_configuration) const {
827 const bool logging_enabled = resource_resolution_logging_enabled_;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800828 ApkAssetsCookie best_cookie = kInvalidCookie;
829 const LoadedPackage* best_package = nullptr;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000830 incfs::verified_map_ptr<ResTable_type> best_type;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800831 const ResTable_config* best_config = nullptr;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000832 uint32_t best_offset = 0U;
833 uint32_t type_flags = 0U;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800834
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800835 // If `desired_config` is not the same as the set configuration or the caller will accept a value
836 // from any configuration, then we cannot use our filtered list of types since it only it contains
837 // types matched to the set configuration.
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000838 const bool use_filtered = !ignore_configuration && std::find_if(
839 configurations_.begin(), configurations_.end(),
840 [&desired_config](auto& value) { return &desired_config == &value; })
841 != configurations_.end();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700842 const size_t package_count = package_group.packages_.size();
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800843 for (size_t pi = 0; pi < package_count; pi++) {
844 const ConfiguredPackage& loaded_package_impl = package_group.packages_[pi];
845 const LoadedPackage* loaded_package = loaded_package_impl.loaded_package_;
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800846 const ApkAssetsCookie cookie = package_group.cookies_[pi];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800847
848 // If the type IDs are offset in this package, we need to take that into account when searching
849 // for a type.
850 const TypeSpec* type_spec = loaded_package->GetTypeSpecByTypeIndex(type_idx);
851 if (UNLIKELY(type_spec == nullptr)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700852 continue;
853 }
854
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800855 // Allow custom loader packages to overlay resource values with configurations equivalent to the
856 // current best configuration.
857 const bool package_is_loader = loaded_package->IsCustomLoader();
858
Ryan Mitchell80094e32020-11-16 23:08:18 +0000859 auto entry_flags = type_spec->GetFlagsForEntryIndex(entry_idx);
Bernie Innocenti58cf8e32020-12-19 15:31:52 +0900860 if (UNLIKELY(!entry_flags.has_value())) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000861 return base::unexpected(entry_flags.error());
862 }
863 type_flags |= entry_flags.value();
864
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800865 const FilteredConfigGroup& filtered_group = loaded_package_impl.filtered_configs_[type_idx];
866 const size_t type_entry_count = (use_filtered) ? filtered_group.type_entries.size()
867 : type_spec->type_entries.size();
868 for (size_t i = 0; i < type_entry_count; i++) {
869 const TypeSpec::TypeEntry* type_entry = (use_filtered) ? filtered_group.type_entries[i]
870 : &type_spec->type_entries[i];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800871
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800872 // We can skip calling ResTable_config::match() if the caller does not care for the
873 // configuration to match or if we're using the list of types that have already had their
Jeremy Meyer2ae6ed592023-09-13 12:39:53 -0700874 // configuration matched. The exception to this is when the user has multiple locales set
875 // because the filtered list will then have values from multiple locales and we will need to
876 // call match() to make sure the current entry matches the config we are currently checking.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800877 const ResTable_config& this_config = type_entry->config;
Jeremy Meyer2ae6ed592023-09-13 12:39:53 -0700878 if (!((use_filtered && (configurations_.size() == 1))
879 || ignore_configuration || this_config.match(desired_config))) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800880 continue;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800881 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800882
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800883 Resolution::Step::Type resolution_type;
884 if (best_config == nullptr) {
885 resolution_type = Resolution::Step::Type::INITIAL;
886 } else if (this_config.isBetterThan(*best_config, &desired_config)) {
887 resolution_type = Resolution::Step::Type::BETTER_MATCH;
888 } else if (package_is_loader && this_config.compare(*best_config) == 0) {
889 resolution_type = Resolution::Step::Type::OVERLAID;
890 } else {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000891 if (UNLIKELY(logging_enabled)) {
Jackal Guo552b45d2021-09-29 10:52:19 +0800892 last_resolution_.steps.push_back(Resolution::Step{Resolution::Step::Type::SKIPPED,
Yurii Zubrytskyiff9cba72023-03-29 12:53:51 -0700893 cookie, this_config.toString()});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800894 }
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800895 continue;
896 }
897
898 // The configuration matches and is better than the previous selection.
899 // Find the entry value if it exists for this configuration.
900 const auto& type = type_entry->type;
901 const auto offset = LoadedPackage::GetEntryOffset(type, entry_idx);
902 if (UNLIKELY(IsIOError(offset))) {
903 return base::unexpected(offset.error());
904 }
905
906 if (!offset.has_value()) {
907 if (UNLIKELY(logging_enabled)) {
Jackal Guo552b45d2021-09-29 10:52:19 +0800908 last_resolution_.steps.push_back(Resolution::Step{Resolution::Step::Type::NO_ENTRY,
Yurii Zubrytskyiff9cba72023-03-29 12:53:51 -0700909 cookie, this_config.toString()});
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800910 }
911 continue;
912 }
913
914 best_cookie = cookie;
915 best_package = loaded_package;
916 best_type = type;
917 best_config = &this_config;
918 best_offset = offset.value();
919
920 if (UNLIKELY(logging_enabled)) {
921 last_resolution_.steps.push_back(Resolution::Step{resolution_type,
Yurii Zubrytskyiff9cba72023-03-29 12:53:51 -0700922 cookie, this_config.toString()});
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800923 }
924
925 // Any configuration will suffice, so break.
926 if (stop_at_first_match) {
927 break;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700928 }
929 }
930 }
931
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800932 if (UNLIKELY(best_cookie == kInvalidCookie)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000933 return base::unexpected(std::nullopt);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700934 }
935
Eric Miao368cd192022-09-09 15:46:14 -0700936 auto best_entry_verified = LoadedPackage::GetEntryFromOffset(best_type, best_offset);
937 if (!best_entry_verified.has_value()) {
938 return base::unexpected(best_entry_verified.error());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800939 }
940
Eric Miao368cd192022-09-09 15:46:14 -0700941 const auto entry = GetEntryValue(*best_entry_verified);
Ryan Mitchell80094e32020-11-16 23:08:18 +0000942 if (!entry.has_value()) {
943 return base::unexpected(entry.error());
944 }
Winson2f3669b2019-01-11 11:28:34 -0800945
Ryan Mitchell80094e32020-11-16 23:08:18 +0000946 return FindEntryResult{
947 .cookie = best_cookie,
948 .entry = *entry,
949 .config = *best_config,
950 .type_flags = type_flags,
Tomasz Wasilczyk9e039b92023-06-29 12:08:24 -0700951 .dynamic_ref_table = package_group.dynamic_ref_table.get(),
Ryan Mitchell80094e32020-11-16 23:08:18 +0000952 .package_name = &best_package->GetPackageName(),
953 .type_string_ref = StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1),
954 .entry_string_ref = StringPoolRef(best_package->GetKeyStringPool(),
Eric Miao368cd192022-09-09 15:46:14 -0700955 (*best_entry_verified)->key()),
Ryan Mitchell80094e32020-11-16 23:08:18 +0000956 };
Adam Lesinski7ad11102016-10-28 16:39:15 -0700957}
958
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700959void AssetManager2::ResetResourceResolution() const {
Yurii Zubrytskyiff9cba72023-03-29 12:53:51 -0700960 last_resolution_ = Resolution{};
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700961}
962
Winson2f3669b2019-01-11 11:28:34 -0800963void AssetManager2::SetResourceResolutionLoggingEnabled(bool enabled) {
964 resource_resolution_logging_enabled_ = enabled;
Winson2f3669b2019-01-11 11:28:34 -0800965 if (!enabled) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700966 ResetResourceResolution();
Winson2f3669b2019-01-11 11:28:34 -0800967 }
968}
969
970std::string AssetManager2::GetLastResourceResolution() const {
971 if (!resource_resolution_logging_enabled_) {
972 LOG(ERROR) << "Must enable resource resolution logging before getting path.";
Ryan Mitchell80094e32020-11-16 23:08:18 +0000973 return {};
Winson2f3669b2019-01-11 11:28:34 -0800974 }
975
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800976 const ApkAssetsCookie cookie = last_resolution_.cookie;
Winson2f3669b2019-01-11 11:28:34 -0800977 if (cookie == kInvalidCookie) {
978 LOG(ERROR) << "AssetManager hasn't resolved a resource to read resolution path.";
Ryan Mitchell80094e32020-11-16 23:08:18 +0000979 return {};
Winson2f3669b2019-01-11 11:28:34 -0800980 }
981
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700982 auto op = StartOperation();
983
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800984 const uint32_t resid = last_resolution_.resid;
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -0700985 const auto& assets = GetApkAssets(cookie);
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700986 const auto package =
987 assets ? assets->GetLoadedArsc()->GetPackageById(get_package_id(resid)) : nullptr;
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800988
Winson2f3669b2019-01-11 11:28:34 -0800989 std::string resource_name_string;
Winson2f3669b2019-01-11 11:28:34 -0800990 if (package != nullptr) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000991 auto resource_name = ToResourceName(last_resolution_.type_string_ref,
992 last_resolution_.entry_string_ref,
993 package->GetPackageName());
994 resource_name_string = resource_name.has_value() ?
995 ToFormattedResourceString(resource_name.value()) : "<unknown>";
Winson2f3669b2019-01-11 11:28:34 -0800996 }
997
998 std::stringstream log_stream;
Jeremy Meyerccf5cd72023-08-15 21:42:03 +0000999 if (configurations_.size() == 1) {
1000 log_stream << base::StringPrintf("Resolution for 0x%08x %s\n"
1001 "\tFor config - %s", resid, resource_name_string.c_str(),
1002 configurations_[0].toString().c_str());
1003 } else {
1004 ResTable_config conf = configurations_[0];
1005 conf.clearLocale();
1006 log_stream << base::StringPrintf("Resolution for 0x%08x %s\n\tFor config - %s and locales",
1007 resid, resource_name_string.c_str(), conf.toString().c_str());
1008 char str[40];
1009 str[0] = '\0';
1010 for(auto iter = configurations_.begin(); iter < configurations_.end(); iter++) {
1011 iter->getBcp47Locale(str);
1012 log_stream << base::StringPrintf(" %s%s", str, iter < configurations_.end() ? "," : "");
1013 }
1014 }
Ryan Mitchell14e8ade2021-01-11 16:01:35 -08001015 for (const Resolution::Step& step : last_resolution_.steps) {
Yurii Zubrytskyiff9cba72023-03-29 12:53:51 -07001016 constexpr static std::array kStepStrings = {
1017 "Found initial",
1018 "Found better",
1019 "Overlaid",
1020 "Overlaid inline",
1021 "Skipped",
1022 "No entry"
Ryan Mitchell14e8ade2021-01-11 16:01:35 -08001023 };
1024
Yurii Zubrytskyiff9cba72023-03-29 12:53:51 -07001025 if (step.type < Resolution::Step::Type::INITIAL
1026 || step.type > Resolution::Step::Type::NO_ENTRY) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -08001027 continue;
Winson2f3669b2019-01-11 11:28:34 -08001028 }
Yurii Zubrytskyiff9cba72023-03-29 12:53:51 -07001029 const auto prefix = kStepStrings[int(step.type) - int(Resolution::Step::Type::INITIAL)];
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -07001030 const auto& assets = GetApkAssets(step.cookie);
Yurii Zubrytskyib3455192023-05-01 14:35:48 -07001031 log_stream << "\n\t" << prefix << ": " << (assets ? assets->GetDebugName() : "<null>")
1032 << " #" << step.cookie;
Tomasz Wasilczyk8f74b2a2023-08-24 19:02:33 +00001033 if (!step.config_name.empty()) {
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -08001034 log_stream << " - " << step.config_name;
Winson2f3669b2019-01-11 11:28:34 -08001035 }
1036 }
1037
Jackal Guo552b45d2021-09-29 10:52:19 +08001038 log_stream << "\nBest matching is from "
Tomasz Wasilczyk8f74b2a2023-08-24 19:02:33 +00001039 << (last_resolution_.best_config_name.empty() ? "default"
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001040 : last_resolution_.best_config_name.c_str())
Jackal Guo552b45d2021-09-29 10:52:19 +08001041 << " configuration of " << last_resolution_.best_package_name;
Winson2f3669b2019-01-11 11:28:34 -08001042 return log_stream.str();
1043}
1044
Felka Chang00964e92021-12-10 01:19:08 +08001045base::expected<uint32_t, NullOrIOError> AssetManager2::GetParentThemeResourceId(uint32_t resid)
1046const {
1047 auto entry = FindEntry(resid, 0u /* density_override */,
1048 false /* stop_at_first_match */,
1049 false /* ignore_configuration */);
1050 if (!entry.has_value()) {
1051 return base::unexpected(entry.error());
1052 }
1053
1054 auto entry_map = std::get_if<incfs::verified_map_ptr<ResTable_map_entry>>(&entry->entry);
1055 if (entry_map == nullptr) {
1056 // Not a bag, nothing to do.
1057 return base::unexpected(std::nullopt);
1058 }
1059
1060 auto map = *entry_map;
1061 const uint32_t parent_resid = dtohl(map->parent.ident);
1062
1063 return parent_resid;
1064}
1065
Ryan Mitchell80094e32020-11-16 23:08:18 +00001066base::expected<AssetManager2::ResourceName, NullOrIOError> AssetManager2::GetResourceName(
1067 uint32_t resid) const {
1068 auto result = FindEntry(resid, 0u /* density_override */, true /* stop_at_first_match */,
1069 true /* ignore_configuration */);
1070 if (!result.has_value()) {
1071 return base::unexpected(result.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001072 }
1073
Ryan Mitchell80094e32020-11-16 23:08:18 +00001074 return ToResourceName(result->type_string_ref,
1075 result->entry_string_ref,
1076 *result->package_name);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001077}
1078
Ryan Mitchell14e8ade2021-01-11 16:01:35 -08001079base::expected<uint32_t, NullOrIOError> AssetManager2::GetResourceTypeSpecFlags(
1080 uint32_t resid) const {
1081 auto result = FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */,
1082 true /* ignore_configuration */);
1083 if (!result.has_value()) {
1084 return base::unexpected(result.error());
1085 }
1086 return result->type_flags;
1087}
1088
Ryan Mitchell80094e32020-11-16 23:08:18 +00001089base::expected<AssetManager2::SelectedValue, NullOrIOError> AssetManager2::GetResource(
1090 uint32_t resid, bool may_be_bag, uint16_t density_override) const {
1091 auto result = FindEntry(resid, density_override, false /* stop_at_first_match */,
1092 false /* ignore_configuration */);
1093 if (!result.has_value()) {
1094 return base::unexpected(result.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001095 }
1096
Ryan Mitchell80094e32020-11-16 23:08:18 +00001097 auto result_map_entry = std::get_if<incfs::verified_map_ptr<ResTable_map_entry>>(&result->entry);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -07001098 if (result_map_entry != nullptr) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001099 if (!may_be_bag) {
1100 LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001101 return base::unexpected(std::nullopt);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001102 }
Adam Lesinski0c405242017-01-13 20:47:26 -08001103
1104 // Create a reference since we can't represent this complex type as a Res_value.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001105 return SelectedValue(Res_value::TYPE_REFERENCE, resid, result->cookie, result->type_flags,
1106 resid, result->config);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001107 }
1108
Adam Lesinskida431a22016-12-29 16:08:16 -05001109 // Convert the package ID to the runtime assigned package ID.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001110 Res_value value = std::get<Res_value>(result->entry);
1111 result->dynamic_ref_table->lookupResourceValue(&value);
Adam Lesinskida431a22016-12-29 16:08:16 -05001112
Ryan Mitchell80094e32020-11-16 23:08:18 +00001113 return SelectedValue(value.dataType, value.data, result->cookie, result->type_flags,
1114 resid, result->config);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001115}
1116
Ryan Mitchell80094e32020-11-16 23:08:18 +00001117base::expected<std::monostate, NullOrIOError> AssetManager2::ResolveReference(
Ryan Mitchella45506e2020-11-16 23:08:18 +00001118 AssetManager2::SelectedValue& value, bool cache_value) const {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001119 if (value.type != Res_value::TYPE_REFERENCE || value.data == 0U) {
1120 // Not a reference. Nothing to do.
1121 return {};
Adam Lesinski0c405242017-01-13 20:47:26 -08001122 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001123
Ryan Mitchella45506e2020-11-16 23:08:18 +00001124 const uint32_t original_flags = value.flags;
1125 const uint32_t original_resid = value.data;
1126 if (cache_value) {
1127 auto cached_value = cached_resolved_values_.find(value.data);
1128 if (cached_value != cached_resolved_values_.end()) {
1129 value = cached_value->second;
1130 value.flags |= original_flags;
1131 return {};
1132 }
1133 }
1134
1135 uint32_t combined_flags = 0U;
1136 uint32_t resolve_resid = original_resid;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001137 constexpr const uint32_t kMaxIterations = 20;
1138 for (uint32_t i = 0U;; i++) {
1139 auto result = GetResource(resolve_resid, true /*may_be_bag*/);
1140 if (!result.has_value()) {
Ryan Mitchelle7ab6272020-11-13 18:06:15 -08001141 value.resid = resolve_resid;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001142 return base::unexpected(result.error());
1143 }
1144
Ryan Mitchelle7ab6272020-11-13 18:06:15 -08001145 // If resource resolution fails, the value should be set to the last reference that was able to
1146 // be resolved successfully.
1147 value = *result;
1148 value.flags |= combined_flags;
1149
Ryan Mitchell80094e32020-11-16 23:08:18 +00001150 if (result->type != Res_value::TYPE_REFERENCE ||
1151 result->data == Res_value::DATA_NULL_UNDEFINED ||
1152 result->data == resolve_resid || i == kMaxIterations) {
1153 // This reference can't be resolved, so exit now and let the caller deal with it.
Ryan Mitchella45506e2020-11-16 23:08:18 +00001154 if (cache_value) {
1155 cached_resolved_values_[original_resid] = value;
1156 }
1157
1158 // Above value is cached without original_flags to ensure they don't get included in future
1159 // queries that hit the cache
1160 value.flags |= original_flags;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001161 return {};
1162 }
1163
Ryan Mitchelle7ab6272020-11-13 18:06:15 -08001164 combined_flags = result->flags;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001165 resolve_resid = result->data;
1166 }
Adam Lesinski0c405242017-01-13 20:47:26 -08001167}
1168
Yurii Zubrytskyi02a555f2023-05-10 13:22:55 -07001169base::expected<const std::vector<uint32_t>*, NullOrIOError> AssetManager2::GetBagResIdStack(
1170 uint32_t resid) const {
Yurii Zubrytskyi09144882023-06-15 23:23:15 -07001171 auto it = cached_bag_resid_stacks_.find(resid);
1172 if (it != cached_bag_resid_stacks_.end()) {
1173 return &it->second;
Aurimas Liutikas8f004c82019-01-17 17:20:10 -08001174 }
Yurii Zubrytskyi09144882023-06-15 23:23:15 -07001175 std::vector<uint32_t> stacks;
1176 if (auto maybe_bag = GetBag(resid, stacks); UNLIKELY(IsIOError(maybe_bag))) {
1177 return base::unexpected(maybe_bag.error());
1178 }
1179
1180 it = cached_bag_resid_stacks_.emplace(resid, std::move(stacks)).first;
Yurii Zubrytskyi02a555f2023-05-10 13:22:55 -07001181 return &it->second;
Aurimas Liutikas8f004c82019-01-17 17:20:10 -08001182}
1183
Ryan Mitchell80094e32020-11-16 23:08:18 +00001184base::expected<const ResolvedBag*, NullOrIOError> AssetManager2::ResolveBag(
1185 AssetManager2::SelectedValue& value) const {
1186 if (UNLIKELY(value.type != Res_value::TYPE_REFERENCE)) {
1187 return base::unexpected(std::nullopt);
1188 }
Aurimas Liutikas8f004c82019-01-17 17:20:10 -08001189
Ryan Mitchell80094e32020-11-16 23:08:18 +00001190 auto bag = GetBag(value.data);
1191 if (bag.has_value()) {
1192 value.flags |= (*bag)->type_spec_flags;
Aurimas Liutikas8f004c82019-01-17 17:20:10 -08001193 }
1194 return bag;
y57cd1952018-04-12 14:26:23 -07001195}
1196
Ryan Mitchell80094e32020-11-16 23:08:18 +00001197base::expected<const ResolvedBag*, NullOrIOError> AssetManager2::GetBag(uint32_t resid) const {
Yurii Zubrytskyi09144882023-06-15 23:23:15 -07001198 auto resid_stacks_it = cached_bag_resid_stacks_.find(resid);
Yurii Zubrytskyi533e8cc2023-06-16 16:38:49 -07001199 if (resid_stacks_it == cached_bag_resid_stacks_.end()) {
Yurii Zubrytskyi09144882023-06-15 23:23:15 -07001200 resid_stacks_it = cached_bag_resid_stacks_.emplace(resid, std::vector<uint32_t>{}).first;
1201 }
Yurii Zubrytskyibd1db5d2023-05-23 18:32:47 -07001202 const auto bag = GetBag(resid, resid_stacks_it->second);
1203 if (UNLIKELY(IsIOError(bag))) {
1204 cached_bag_resid_stacks_.erase(resid_stacks_it);
1205 return base::unexpected(bag.error());
1206 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001207 return bag;
Ryan Mitchell155d5392020-02-10 13:35:24 -08001208}
1209
Ryan Mitchell80094e32020-11-16 23:08:18 +00001210base::expected<const ResolvedBag*, NullOrIOError> AssetManager2::GetBag(
1211 uint32_t resid, std::vector<uint32_t>& child_resids) const {
1212 if (auto cached_iter = cached_bags_.find(resid); cached_iter != cached_bags_.end()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001213 return cached_iter->second.get();
1214 }
1215
Ryan Mitchell80094e32020-11-16 23:08:18 +00001216 auto entry = FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */,
1217 false /* ignore_configuration */);
1218 if (!entry.has_value()) {
1219 return base::unexpected(entry.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001220 }
1221
Ryan Mitchell80094e32020-11-16 23:08:18 +00001222 auto entry_map = std::get_if<incfs::verified_map_ptr<ResTable_map_entry>>(&entry->entry);
1223 if (entry_map == nullptr) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001224 // Not a bag, nothing to do.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001225 return base::unexpected(std::nullopt);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001226 }
1227
Ryan Mitchell80094e32020-11-16 23:08:18 +00001228 auto map = *entry_map;
1229 auto map_entry = map.offset(dtohs(map->size)).convert<ResTable_map>();
1230 const auto map_entry_end = map_entry + dtohl(map->count);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001231
y57cd1952018-04-12 14:26:23 -07001232 // Keep track of ids that have already been seen to prevent infinite loops caused by circular
Ryan Mitchell80094e32020-11-16 23:08:18 +00001233 // dependencies between bags.
y57cd1952018-04-12 14:26:23 -07001234 child_resids.push_back(resid);
1235
Adam Lesinskida431a22016-12-29 16:08:16 -05001236 uint32_t parent_resid = dtohl(map->parent.ident);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001237 if (parent_resid == 0U ||
1238 std::find(child_resids.begin(), child_resids.end(), parent_resid) != child_resids.end()) {
1239 // There is no parent or a circular parental dependency exist, meaning there is nothing to
1240 // inherit and we can do a simple copy of the entries in the map.
Adam Lesinski7ad11102016-10-28 16:39:15 -07001241 const size_t entry_count = map_entry_end - map_entry;
1242 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
1243 malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))};
Ryan Mitchell155d5392020-02-10 13:35:24 -08001244
1245 bool sort_entries = false;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001246 for (auto new_entry = new_bag->entries; map_entry != map_entry_end; ++map_entry) {
1247 if (UNLIKELY(!map_entry)) {
1248 return base::unexpected(IOError::PAGES_MISSING);
1249 }
1250
Adam Lesinskida431a22016-12-29 16:08:16 -05001251 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001252 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001253 // Attributes, arrays, etc don't have a resource id as the name. They specify
1254 // other data, which would be wrong to change via a lookup.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001255 if (UNLIKELY(entry->dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001256 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
1257 resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001258 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -05001259 }
1260 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001261
1262 new_entry->cookie = entry->cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001263 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001264 new_entry->key_pool = nullptr;
1265 new_entry->type_pool = nullptr;
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001266 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -07001267 new_entry->value.copyFrom_dtoh(map_entry->value);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001268 status_t err = entry->dynamic_ref_table->lookupResourceValue(&new_entry->value);
1269 if (UNLIKELY(err != NO_ERROR)) {
Adam Lesinski30080e22017-10-16 16:18:09 -07001270 LOG(ERROR) << base::StringPrintf(
1271 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
1272 new_entry->value.data, new_key);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001273 return base::unexpected(std::nullopt);
Adam Lesinski30080e22017-10-16 16:18:09 -07001274 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001275
Ryan Mitchell155d5392020-02-10 13:35:24 -08001276 sort_entries = sort_entries ||
1277 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001278 ++new_entry;
1279 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001280
1281 if (sort_entries) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001282 std::sort(new_bag->entries, new_bag->entries + entry_count,
1283 [](auto&& lhs, auto&& rhs) { return lhs.key < rhs.key; });
Ryan Mitchell155d5392020-02-10 13:35:24 -08001284 }
1285
Ryan Mitchell80094e32020-11-16 23:08:18 +00001286 new_bag->type_spec_flags = entry->type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001287 new_bag->entry_count = static_cast<uint32_t>(entry_count);
1288 ResolvedBag* result = new_bag.get();
1289 cached_bags_[resid] = std::move(new_bag);
1290 return result;
1291 }
1292
Adam Lesinskida431a22016-12-29 16:08:16 -05001293 // In case the parent is a dynamic reference, resolve it.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001294 entry->dynamic_ref_table->lookupResourceId(&parent_resid);
Adam Lesinskida431a22016-12-29 16:08:16 -05001295
Adam Lesinski7ad11102016-10-28 16:39:15 -07001296 // Get the parent and do a merge of the keys.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001297 const auto parent_bag = GetBag(parent_resid, child_resids);
1298 if (UNLIKELY(!parent_bag.has_value())) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001299 // Failed to get the parent that should exist.
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001300 LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid,
1301 resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001302 return base::unexpected(parent_bag.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001303 }
1304
Adam Lesinski7ad11102016-10-28 16:39:15 -07001305 // Create the max possible entries we can make. Once we construct the bag,
1306 // we will realloc to fit to size.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001307 const size_t max_count = (*parent_bag)->entry_count + dtohl(map->count);
George Burgess IV09b119f2017-07-25 15:00:04 -07001308 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
1309 malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001310 ResolvedBag::Entry* new_entry = new_bag->entries;
1311
Ryan Mitchell80094e32020-11-16 23:08:18 +00001312 const ResolvedBag::Entry* parent_entry = (*parent_bag)->entries;
1313 const ResolvedBag::Entry* const parent_entry_end = parent_entry + (*parent_bag)->entry_count;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001314
1315 // The keys are expected to be in sorted order. Merge the two bags.
Ryan Mitchell155d5392020-02-10 13:35:24 -08001316 bool sort_entries = false;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001317 while (map_entry != map_entry_end && parent_entry != parent_entry_end) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001318 if (UNLIKELY(!map_entry)) {
1319 return base::unexpected(IOError::PAGES_MISSING);
1320 }
1321
Adam Lesinskida431a22016-12-29 16:08:16 -05001322 uint32_t child_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001323 if (!is_internal_resid(child_key)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001324 if (UNLIKELY(entry->dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001325 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key,
1326 resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001327 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -05001328 }
1329 }
1330
Adam Lesinski7ad11102016-10-28 16:39:15 -07001331 if (child_key <= parent_entry->key) {
1332 // Use the child key if it comes before the parent
1333 // or is equal to the parent (overrides).
Ryan Mitchell80094e32020-11-16 23:08:18 +00001334 new_entry->cookie = entry->cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001335 new_entry->key = child_key;
1336 new_entry->key_pool = nullptr;
1337 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -07001338 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001339 new_entry->style = resid;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001340 status_t err = entry->dynamic_ref_table->lookupResourceValue(&new_entry->value);
1341 if (UNLIKELY(err != NO_ERROR)) {
Adam Lesinski30080e22017-10-16 16:18:09 -07001342 LOG(ERROR) << base::StringPrintf(
1343 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
1344 new_entry->value.data, child_key);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001345 return base::unexpected(std::nullopt);
Adam Lesinski30080e22017-10-16 16:18:09 -07001346 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001347 ++map_entry;
1348 } else {
1349 // Take the parent entry as-is.
1350 memcpy(new_entry, parent_entry, sizeof(*new_entry));
1351 }
1352
Ryan Mitchell155d5392020-02-10 13:35:24 -08001353 sort_entries = sort_entries ||
1354 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001355 if (child_key >= parent_entry->key) {
1356 // Move to the next parent entry if we used it or it was overridden.
1357 ++parent_entry;
1358 }
1359 // Increment to the next entry to fill.
1360 ++new_entry;
1361 }
1362
1363 // Finish the child entries if they exist.
1364 while (map_entry != map_entry_end) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001365 if (UNLIKELY(!map_entry)) {
1366 return base::unexpected(IOError::PAGES_MISSING);
1367 }
1368
Adam Lesinskida431a22016-12-29 16:08:16 -05001369 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001370 if (!is_internal_resid(new_key)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001371 if (UNLIKELY(entry->dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001372 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
1373 resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001374 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -05001375 }
1376 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001377 new_entry->cookie = entry->cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001378 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001379 new_entry->key_pool = nullptr;
1380 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -07001381 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001382 new_entry->style = resid;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001383 status_t err = entry->dynamic_ref_table->lookupResourceValue(&new_entry->value);
1384 if (UNLIKELY(err != NO_ERROR)) {
Adam Lesinski30080e22017-10-16 16:18:09 -07001385 LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.",
1386 new_entry->value.dataType, new_entry->value.data, new_key);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001387 return base::unexpected(std::nullopt);
Adam Lesinski30080e22017-10-16 16:18:09 -07001388 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001389 sort_entries = sort_entries ||
1390 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001391 ++map_entry;
1392 ++new_entry;
1393 }
1394
1395 // Finish the parent entries if they exist.
1396 if (parent_entry != parent_entry_end) {
1397 // Take the rest of the parent entries as-is.
1398 const size_t num_entries_to_copy = parent_entry_end - parent_entry;
1399 memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry));
1400 new_entry += num_entries_to_copy;
1401 }
1402
1403 // Resize the resulting array to fit.
1404 const size_t actual_count = new_entry - new_bag->entries;
1405 if (actual_count != max_count) {
George Burgess IV09b119f2017-07-25 15:00:04 -07001406 new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc(
1407 new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry)))));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001408 }
1409
Ryan Mitchell155d5392020-02-10 13:35:24 -08001410 if (sort_entries) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001411 std::sort(new_bag->entries, new_bag->entries + actual_count,
1412 [](auto&& lhs, auto&& rhs) { return lhs.key < rhs.key; });
Ryan Mitchell155d5392020-02-10 13:35:24 -08001413 }
1414
Adam Lesinski1a1e9c22017-10-13 15:45:34 -07001415 // Combine flags from the parent and our own bag.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001416 new_bag->type_spec_flags = entry->type_flags | (*parent_bag)->type_spec_flags;
George Burgess IV09b119f2017-07-25 15:00:04 -07001417 new_bag->entry_count = static_cast<uint32_t>(actual_count);
1418 ResolvedBag* result = new_bag.get();
1419 cached_bags_[resid] = std::move(new_bag);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001420 return result;
1421}
1422
Yurii Zubrytskyia5775142022-11-02 17:49:49 -07001423static bool Utf8ToUtf16(StringPiece str, std::u16string* out) {
Adam Lesinski929d6512017-01-16 19:11:19 -08001424 ssize_t len =
1425 utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false);
1426 if (len < 0) {
1427 return false;
1428 }
1429 out->resize(static_cast<size_t>(len));
1430 utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(),
1431 static_cast<size_t>(len + 1));
1432 return true;
1433}
1434
Ryan Mitchell80094e32020-11-16 23:08:18 +00001435base::expected<uint32_t, NullOrIOError> AssetManager2::GetResourceId(
1436 const std::string& resource_name, const std::string& fallback_type,
1437 const std::string& fallback_package) const {
Adam Lesinski929d6512017-01-16 19:11:19 -08001438 StringPiece package_name, type, entry;
1439 if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001440 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -08001441 }
1442
1443 if (entry.empty()) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001444 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -08001445 }
1446
1447 if (package_name.empty()) {
1448 package_name = fallback_package;
1449 }
1450
1451 if (type.empty()) {
1452 type = fallback_type;
1453 }
1454
1455 std::u16string type16;
1456 if (!Utf8ToUtf16(type, &type16)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001457 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -08001458 }
1459
1460 std::u16string entry16;
1461 if (!Utf8ToUtf16(entry, &entry16)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001462 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -08001463 }
1464
1465 const StringPiece16 kAttr16 = u"attr";
1466 const static std::u16string kAttrPrivate16 = u"^attr-private";
1467
1468 for (const PackageGroup& package_group : package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001469 for (const ConfiguredPackage& package_impl : package_group.packages_) {
1470 const LoadedPackage* package = package_impl.loaded_package_;
Adam Lesinski929d6512017-01-16 19:11:19 -08001471 if (package_name != package->GetPackageName()) {
1472 // All packages in the same group are expected to have the same package name.
1473 break;
1474 }
1475
Ryan Mitchell80094e32020-11-16 23:08:18 +00001476 base::expected<uint32_t, NullOrIOError> resid = package->FindEntryByName(type16, entry16);
1477 if (UNLIKELY(IsIOError(resid))) {
1478 return base::unexpected(resid.error());
1479 }
1480
1481 if (!resid.has_value() && kAttr16 == type16) {
Adam Lesinski929d6512017-01-16 19:11:19 -08001482 // Private attributes in libraries (such as the framework) are sometimes encoded
1483 // under the type '^attr-private' in order to leave the ID space of public 'attr'
1484 // free for future additions. Check '^attr-private' for the same name.
1485 resid = package->FindEntryByName(kAttrPrivate16, entry16);
1486 }
1487
Ryan Mitchell80094e32020-11-16 23:08:18 +00001488 if (resid.has_value()) {
1489 return fix_package_id(*resid, package_group.dynamic_ref_table->mAssignedPackageId);
Adam Lesinski929d6512017-01-16 19:11:19 -08001490 }
1491 }
1492 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001493 return base::unexpected(std::nullopt);
Adam Lesinski0c405242017-01-13 20:47:26 -08001494}
1495
Ryan Mitchell14e8ade2021-01-11 16:01:35 -08001496void AssetManager2::RebuildFilterList() {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001497 for (PackageGroup& group : package_groups_) {
Yurii Zubrytskyidbce3562022-11-14 22:26:10 -08001498 for (ConfiguredPackage& package : group.packages_) {
1499 package.filtered_configs_.forEachItem([](auto, auto& fcg) { fcg.type_entries.clear(); });
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001500 // Create the filters here.
Yurii Zubrytskyidbce3562022-11-14 22:26:10 -08001501 package.loaded_package_->ForEachTypeSpec([&](const TypeSpec& type_spec, uint8_t type_id) {
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -07001502 FilteredConfigGroup* group = nullptr;
Ryan Mitchell14e8ade2021-01-11 16:01:35 -08001503 for (const auto& type_entry : type_spec.type_entries) {
Jeremy Meyerccf5cd72023-08-15 21:42:03 +00001504 for (auto & config : configurations_) {
1505 if (type_entry.config.match(config)) {
1506 if (!group) {
1507 group = &package.filtered_configs_.editItemAt(type_id - 1);
1508 }
1509 group->type_entries.push_back(&type_entry);
1510 break;
Yurii Zubrytskyi59dab3b2022-11-03 00:08:49 -07001511 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001512 }
1513 }
1514 });
Yurii Zubrytskyidbce3562022-11-14 22:26:10 -08001515 package.filtered_configs_.trimBuckets(
1516 [](const auto& fcg) { return fcg.type_entries.empty(); });
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001517 }
1518 }
1519}
1520
Adam Lesinski7ad11102016-10-28 16:39:15 -07001521void AssetManager2::InvalidateCaches(uint32_t diff) {
Yurii Zubrytskyi09144882023-06-15 23:23:15 -07001522 cached_resolved_values_.clear();
Ryan Mitchell2c4d8742019-03-04 09:41:00 -08001523
Adam Lesinski7ad11102016-10-28 16:39:15 -07001524 if (diff == 0xffffffffu) {
1525 // Everything must go.
1526 cached_bags_.clear();
Yurii Zubrytskyi09144882023-06-15 23:23:15 -07001527 cached_bag_resid_stacks_.clear();
Adam Lesinski7ad11102016-10-28 16:39:15 -07001528 return;
1529 }
1530
1531 // Be more conservative with what gets purged. Only if the bag has other possible
1532 // variations with respect to what changed (diff) should we remove it.
Yurii Zubrytskyi09144882023-06-15 23:23:15 -07001533 for (auto stack_it = cached_bag_resid_stacks_.begin();
1534 stack_it != cached_bag_resid_stacks_.end();) {
1535 const auto it = cached_bags_.find(stack_it->first);
1536 if (it == cached_bags_.end()) {
1537 stack_it = cached_bag_resid_stacks_.erase(stack_it);
1538 } else if ((diff & it->second->type_spec_flags) != 0) {
1539 cached_bags_.erase(it);
1540 stack_it = cached_bag_resid_stacks_.erase(stack_it);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001541 } else {
Yurii Zubrytskyi09144882023-06-15 23:23:15 -07001542 ++stack_it; // Keep the item in both caches.
Adam Lesinski7ad11102016-10-28 16:39:15 -07001543 }
1544 }
Ryan Mitchella45506e2020-11-16 23:08:18 +00001545
Yurii Zubrytskyi09144882023-06-15 23:23:15 -07001546 // Need to ensure that both bag caches are consistent, as we populate them in the same function.
1547 // Iterate over the cached bags to erase the items without the corresponding resid_stack cache
1548 // items.
1549 for (auto it = cached_bags_.begin(); it != cached_bags_.end();) {
1550 if ((diff & it->second->type_spec_flags) != 0) {
1551 it = cached_bags_.erase(it);
1552 } else {
1553 ++it;
1554 }
1555 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001556}
1557
Ryan Mitchell2e394222019-08-28 12:10:51 -07001558uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) const {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001559 for (auto& package_group : package_groups_) {
1560 for (auto& package2 : package_group.packages_) {
1561 if (package2.loaded_package_ == package) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001562 return package_group.dynamic_ref_table->mAssignedPackageId;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001563 }
1564 }
1565 }
1566 return 0;
1567}
1568
Adam Lesinski30080e22017-10-16 16:18:09 -07001569std::unique_ptr<Theme> AssetManager2::NewTheme() {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001570 constexpr size_t kInitialReserveSize = 32;
1571 auto theme = std::unique_ptr<Theme>(new Theme(this));
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001572 theme->keys_.reserve(kInitialReserveSize);
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001573 theme->entries_.reserve(kInitialReserveSize);
1574 return theme;
Adam Lesinski30080e22017-10-16 16:18:09 -07001575}
1576
Yurii Zubrytskyi9eb44c92022-11-14 23:44:52 -08001577void AssetManager2::ForEachPackage(base::function_ref<bool(const std::string&, uint8_t)> func,
1578 package_property_t excluded_property_flags) const {
1579 for (const PackageGroup& package_group : package_groups_) {
1580 const auto loaded_package = package_group.packages_.front().loaded_package_;
1581 if ((loaded_package->GetPropertyFlags() & excluded_property_flags) == 0U
1582 && !func(loaded_package->GetPackageName(),
1583 package_group.dynamic_ref_table->mAssignedPackageId)) {
1584 return;
1585 }
1586 }
1587}
1588
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -07001589AssetManager2::ScopedOperation AssetManager2::StartOperation() const {
1590 ++number_of_running_scoped_operations_;
1591 return ScopedOperation(*this);
1592}
1593
1594void AssetManager2::FinishOperation() const {
1595 if (number_of_running_scoped_operations_ < 1) {
1596 ALOGW("Invalid FinishOperation() call when there's none happening");
1597 return;
1598 }
1599 if (--number_of_running_scoped_operations_ == 0) {
1600 for (auto&& [_, assets] : apk_assets_) {
1601 assets.clear();
1602 }
1603 }
1604}
1605
1606const AssetManager2::ApkAssetsPtr& AssetManager2::GetApkAssets(ApkAssetsCookie cookie) const {
1607 DCHECK(number_of_running_scoped_operations_ > 0) << "Must have an operation running";
1608
1609 if (cookie < 0 || cookie >= apk_assets_.size()) {
1610 static const ApkAssetsPtr empty{};
1611 return empty;
1612 }
1613 auto& [wptr, res] = apk_assets_[cookie];
1614 if (!res) {
1615 res = wptr.promote();
1616 }
1617 return res;
1618}
1619
Adam Lesinski30080e22017-10-16 16:18:09 -07001620Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) {
1621}
1622
1623Theme::~Theme() = default;
1624
Ryan Mitchell80094e32020-11-16 23:08:18 +00001625base::expected<std::monostate, NullOrIOError> Theme::ApplyStyle(uint32_t resid, bool force) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001626 ATRACE_NAME("Theme::ApplyStyle");
Adam Lesinski7ad11102016-10-28 16:39:15 -07001627
Ryan Mitchell80094e32020-11-16 23:08:18 +00001628 auto bag = asset_manager_->GetBag(resid);
1629 if (!bag.has_value()) {
1630 return base::unexpected(bag.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001631 }
1632
1633 // Merge the flags from this style.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001634 type_spec_flags_ |= (*bag)->type_spec_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001635
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001636 for (auto it = begin(*bag); it != end(*bag); ++it) {
1637 const uint32_t attr_res_id = it->key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001638
Adam Lesinski30080e22017-10-16 16:18:09 -07001639 // If the resource ID passed in is not a style, the key can be some other identifier that is not
1640 // a resource ID. We should fail fast instead of operating with strange resource IDs.
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001641 if (!is_valid_resid(attr_res_id)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001642 return base::unexpected(std::nullopt);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001643 }
1644
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001645 // DATA_NULL_EMPTY (@empty) is a valid resource value and DATA_NULL_UNDEFINED represents
1646 // an absence of a valid value.
1647 bool is_undefined = it->value.dataType == Res_value::TYPE_NULL &&
1648 it->value.data != Res_value::DATA_NULL_EMPTY;
1649 if (!force && is_undefined) {
1650 continue;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001651 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001652
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001653 const auto key_it = std::lower_bound(keys_.begin(), keys_.end(), attr_res_id);
1654 const auto entry_it = entries_.begin() + (key_it - keys_.begin());
1655 if (key_it != keys_.end() && *key_it == attr_res_id) {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001656 if (is_undefined) {
1657 // DATA_NULL_UNDEFINED clears the value of the attribute in the theme only when `force` is
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001658 // true.
1659 keys_.erase(key_it);
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001660 entries_.erase(entry_it);
1661 } else if (force) {
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001662 *entry_it = Entry{it->cookie, (*bag)->type_spec_flags, it->value};
Adam Lesinski30080e22017-10-16 16:18:09 -07001663 }
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001664 } else {
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001665 keys_.insert(key_it, attr_res_id);
1666 entries_.insert(entry_it, Entry{it->cookie, (*bag)->type_spec_flags, it->value});
Adam Lesinski7ad11102016-10-28 16:39:15 -07001667 }
1668 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001669 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001670}
1671
Ryan Mitchell767e34f2021-06-07 12:29:05 -07001672void Theme::Rebase(AssetManager2* am, const uint32_t* style_ids, const uint8_t* force,
1673 size_t style_count) {
1674 ATRACE_NAME("Theme::Rebase");
1675 // Reset the entries without changing the vector capacity to prevent reallocations during
1676 // ApplyStyle.
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001677 keys_.clear();
Ryan Mitchell767e34f2021-06-07 12:29:05 -07001678 entries_.clear();
1679 asset_manager_ = am;
1680 for (size_t i = 0; i < style_count; i++) {
1681 ApplyStyle(style_ids[i], force[i]);
1682 }
1683}
1684
Ryan Mitchell80094e32020-11-16 23:08:18 +00001685std::optional<AssetManager2::SelectedValue> Theme::GetAttribute(uint32_t resid) const {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001686 constexpr const uint32_t kMaxIterations = 20;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001687 uint32_t type_spec_flags = 0u;
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001688 for (uint32_t i = 0; i <= kMaxIterations; i++) {
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001689 const auto key_it = std::lower_bound(keys_.begin(), keys_.end(), resid);
1690 if (key_it == keys_.end() || *key_it != resid) {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001691 return std::nullopt;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001692 }
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001693 const auto entry_it = entries_.begin() + (key_it - keys_.begin());
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001694 type_spec_flags |= entry_it->type_spec_flags;
1695 if (entry_it->value.dataType == Res_value::TYPE_ATTRIBUTE) {
1696 resid = entry_it->value.data;
1697 continue;
1698 }
1699
1700 return AssetManager2::SelectedValue(entry_it->value.dataType, entry_it->value.data,
1701 entry_it->cookie, type_spec_flags, 0U /* resid */,
1702 {} /* config */);
1703 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001704 return std::nullopt;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001705}
1706
Ryan Mitchell80094e32020-11-16 23:08:18 +00001707base::expected<std::monostate, NullOrIOError> Theme::ResolveAttributeReference(
1708 AssetManager2::SelectedValue& value) const {
1709 if (value.type != Res_value::TYPE_ATTRIBUTE) {
1710 return asset_manager_->ResolveReference(value);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001711 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001712
1713 std::optional<AssetManager2::SelectedValue> result = GetAttribute(value.data);
1714 if (!result.has_value()) {
1715 return base::unexpected(std::nullopt);
1716 }
1717
Ryan Mitchella45506e2020-11-16 23:08:18 +00001718 auto resolve_result = asset_manager_->ResolveReference(*result, true /* cache_value */);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001719 if (resolve_result.has_value()) {
1720 result->flags |= value.flags;
1721 value = *result;
1722 }
1723 return resolve_result;
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001724}
1725
Adam Lesinski7ad11102016-10-28 16:39:15 -07001726void Theme::Clear() {
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001727 keys_.clear();
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001728 entries_.clear();
Adam Lesinski7ad11102016-10-28 16:39:15 -07001729}
1730
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001731base::expected<std::monostate, IOError> Theme::SetTo(const Theme& source) {
1732 if (this == &source) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001733 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001734 }
1735
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001736 type_spec_flags_ = source.type_spec_flags_;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001737
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001738 if (asset_manager_ == source.asset_manager_) {
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001739 keys_ = source.keys_;
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001740 entries_ = source.entries_;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001741 } else {
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001742 std::unordered_map<ApkAssetsCookie, ApkAssetsCookie> src_to_dest_asset_cookies;
1743 using SourceToDestinationRuntimePackageMap = std::unordered_map<int, int>;
1744 std::unordered_map<ApkAssetsCookie, SourceToDestinationRuntimePackageMap> src_asset_cookie_id_map;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001745
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -07001746 auto op_src = source.asset_manager_->StartOperation();
1747 auto op_dst = asset_manager_->StartOperation();
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001748
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -07001749 for (size_t i = 0; i < source.asset_manager_->GetApkAssetsCount(); i++) {
1750 const auto& src_asset = source.asset_manager_->GetApkAssets(i);
1751 if (!src_asset) {
Yurii Zubrytskyib3455192023-05-01 14:35:48 -07001752 continue;
1753 }
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -07001754 for (int j = 0; j < asset_manager_->GetApkAssetsCount(); j++) {
1755 const auto& dest_asset = asset_manager_->GetApkAssets(j);
Ryan Mitchellef538432021-03-01 14:52:14 -08001756 if (src_asset != dest_asset) {
1757 // ResourcesManager caches and reuses ApkAssets when the same apk must be present in
1758 // multiple AssetManagers. Two ApkAssets point to the same version of the same resources
1759 // if they are the same instance.
1760 continue;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001761 }
Ryan Mitchellef538432021-03-01 14:52:14 -08001762
1763 // Map the package ids of the asset in the source AssetManager to the package ids of the
1764 // asset in th destination AssetManager.
1765 SourceToDestinationRuntimePackageMap package_map;
1766 for (const auto& loaded_package : src_asset->GetLoadedArsc()->GetPackages()) {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001767 const int src_package_id = source.asset_manager_->GetAssignedPackageId(
1768 loaded_package.get());
Ryan Mitchellef538432021-03-01 14:52:14 -08001769 const int dest_package_id = asset_manager_->GetAssignedPackageId(loaded_package.get());
1770 package_map[src_package_id] = dest_package_id;
1771 }
1772
1773 src_to_dest_asset_cookies.insert(std::make_pair(i, j));
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001774 src_asset_cookie_id_map.insert(std::make_pair(i, std::move(package_map)));
Ryan Mitchellef538432021-03-01 14:52:14 -08001775 break;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001776 }
1777 }
1778
Ryan Mitchell93bca972019-03-08 17:26:28 -08001779 // Reset the data in the destination theme.
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001780 keys_.clear();
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001781 entries_.clear();
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001782
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001783 for (size_t i = 0, size = source.entries_.size(); i != size; ++i) {
1784 const auto& entry = source.entries_[i];
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001785 bool is_reference = (entry.value.dataType == Res_value::TYPE_ATTRIBUTE
1786 || entry.value.dataType == Res_value::TYPE_REFERENCE
1787 || entry.value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE
1788 || entry.value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE)
1789 && entry.value.data != 0x0;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001790
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001791 // If the attribute value represents an attribute or reference, the package id of the
1792 // value needs to be rewritten to the package id of the value in the destination.
1793 uint32_t attribute_data = entry.value.data;
1794 if (is_reference) {
1795 // Determine the package id of the reference in the destination AssetManager.
1796 auto value_package_map = src_asset_cookie_id_map.find(entry.cookie);
1797 if (value_package_map == src_asset_cookie_id_map.end()) {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001798 continue;
1799 }
1800
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001801 auto value_dest_package = value_package_map->second.find(
1802 get_package_id(entry.value.data));
1803 if (value_dest_package == value_package_map->second.end()) {
1804 continue;
1805 }
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001806
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001807 attribute_data = fix_package_id(entry.value.data, value_dest_package->second);
1808 }
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001809
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001810 // Find the cookie of the value in the destination. If the source apk is not loaded in the
1811 // destination, only copy resources that do not reference resources in the source.
1812 ApkAssetsCookie data_dest_cookie;
1813 auto value_dest_cookie = src_to_dest_asset_cookies.find(entry.cookie);
1814 if (value_dest_cookie != src_to_dest_asset_cookies.end()) {
1815 data_dest_cookie = value_dest_cookie->second;
1816 } else {
1817 if (is_reference || entry.value.dataType == Res_value::TYPE_STRING) {
1818 continue;
1819 } else {
1820 data_dest_cookie = 0x0;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001821 }
1822 }
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001823
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001824 const auto source_res_id = source.keys_[i];
1825
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001826 // The package id of the attribute needs to be rewritten to the package id of the
1827 // attribute in the destination.
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001828 int attribute_dest_package_id = get_package_id(source_res_id);
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001829 if (attribute_dest_package_id != 0x01) {
1830 // Find the cookie of the attribute resource id in the source AssetManager
1831 base::expected<FindEntryResult, NullOrIOError> attribute_entry_result =
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001832 source.asset_manager_->FindEntry(source_res_id, 0 /* density_override */ ,
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001833 true /* stop_at_first_match */,
1834 true /* ignore_configuration */);
1835 if (UNLIKELY(IsIOError(attribute_entry_result))) {
1836 return base::unexpected(GetIOError(attribute_entry_result.error()));
1837 }
1838 if (!attribute_entry_result.has_value()) {
1839 continue;
1840 }
1841
1842 // Determine the package id of the attribute in the destination AssetManager.
1843 auto attribute_package_map = src_asset_cookie_id_map.find(
1844 attribute_entry_result->cookie);
1845 if (attribute_package_map == src_asset_cookie_id_map.end()) {
1846 continue;
1847 }
1848 auto attribute_dest_package = attribute_package_map->second.find(
1849 attribute_dest_package_id);
1850 if (attribute_dest_package == attribute_package_map->second.end()) {
1851 continue;
1852 }
1853 attribute_dest_package_id = attribute_dest_package->second;
1854 }
1855
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001856 auto dest_attr_id = make_resid(attribute_dest_package_id, get_type_id(source_res_id),
1857 get_entry_id(source_res_id));
1858 const auto key_it = std::lower_bound(keys_.begin(), keys_.end(), dest_attr_id);
1859 const auto entry_it = entries_.begin() + (key_it - keys_.begin());
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001860 // Since the entries were cleared, the attribute resource id has yet been mapped to any value.
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001861 keys_.insert(key_it, dest_attr_id);
1862 entries_.insert(entry_it, Entry{data_dest_cookie, entry.type_spec_flags,
1863 Res_value{.dataType = entry.value.dataType,
1864 .data = attribute_data}});
Adam Lesinski7ad11102016-10-28 16:39:15 -07001865 }
1866 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001867 return {};
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001868}
1869
1870void Theme::Dump() const {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001871 LOG(INFO) << base::StringPrintf("Theme(this=%p, AssetManager2=%p)", this, asset_manager_);
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001872 for (size_t i = 0, size = keys_.size(); i != size; ++i) {
1873 auto res_id = keys_[i];
1874 const auto& entry = entries_[i];
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001875 LOG(INFO) << base::StringPrintf(" entry(0x%08x)=(0x%08x) type=(0x%02x), cookie(%d)",
Yurii Zubrytskyi591895b2022-11-14 22:06:30 -08001876 res_id, entry.value.data, entry.value.dataType,
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001877 entry.cookie);
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001878 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001879}
1880
Yurii Zubrytskyibdcbf552023-05-10 13:16:23 -07001881AssetManager2::ScopedOperation::ScopedOperation(const AssetManager2& am) : am_(am) {
1882}
1883
1884AssetManager2::ScopedOperation::~ScopedOperation() {
1885 am_.FinishOperation();
1886}
1887
Adam Lesinski7ad11102016-10-28 16:39:15 -07001888} // namespace android