blob: 4fdae32350cfcebefc109e1ff7593e293f2aa160 [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>
Adam Lesinski0c405242017-01-13 20:47:26 -080025
Adam Lesinski7ad11102016-10-28 16:39:15 -070026#include "android-base/logging.h"
27#include "android-base/stringprintf.h"
Ryan Mitchell8a891d82019-07-01 09:48:23 -070028#include "androidfw/ResourceUtils.h"
Ryan Mitchell31b11052019-06-13 13:47:26 -070029#include "androidfw/Util.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070030#include "utils/ByteOrder.h"
31#include "utils/Trace.h"
32
33#ifdef _WIN32
34#ifdef ERROR
35#undef ERROR
36#endif
37#endif
38
39namespace android {
40
Ryan Mitchell80094e32020-11-16 23:08:18 +000041namespace {
42
43using EntryValue = std::variant<Res_value, incfs::verified_map_ptr<ResTable_map_entry>>;
44
45base::expected<EntryValue, IOError> GetEntryValue(
46 incfs::verified_map_ptr<ResTable_entry> table_entry) {
47 const uint16_t entry_size = dtohs(table_entry->size);
48
49 // Check if the entry represents a bag value.
50 if (entry_size >= sizeof(ResTable_map_entry) &&
51 (dtohs(table_entry->flags) & ResTable_entry::FLAG_COMPLEX)) {
52 const auto map_entry = table_entry.convert<ResTable_map_entry>();
53 if (!map_entry) {
54 return base::unexpected(IOError::PAGES_MISSING);
55 }
56 return map_entry.verified();
57 }
58
59 // The entry represents a non-bag value.
60 const auto entry_value = table_entry.offset(entry_size).convert<Res_value>();
61 if (!entry_value) {
62 return base::unexpected(IOError::PAGES_MISSING);
63 }
64 Res_value value;
65 value.copyFrom_dtoh(entry_value.value());
66 return value;
67}
68
69} // namespace
70
Adam Lesinskibebfcc42018-02-12 14:27:46 -080071struct FindEntryResult {
Ryan Mitchell80094e32020-11-16 23:08:18 +000072 // The cookie representing the ApkAssets in which the value resides.
73 ApkAssetsCookie cookie;
74
75 // The value of the resource table entry. Either an android::Res_value for non-bag types or an
76 // incfs::verified_map_ptr<ResTable_map_entry> for bag types.
77 EntryValue entry;
Adam Lesinskibebfcc42018-02-12 14:27:46 -080078
79 // The configuration for which the resulting entry was defined. This is already swapped to host
80 // endianness.
81 ResTable_config config;
82
83 // The bitmask of configuration axis with which the resource value varies.
84 uint32_t type_flags;
85
86 // The dynamic package ID map for the package from which this resource came from.
87 const DynamicRefTable* dynamic_ref_table;
88
Ryan Mitchell8a891d82019-07-01 09:48:23 -070089 // The package name of the resource.
90 const std::string* package_name;
91
Adam Lesinskibebfcc42018-02-12 14:27:46 -080092 // The string pool reference to the type's name. This uses a different string pool than
93 // the global string pool, but this is hidden from the caller.
94 StringPoolRef type_string_ref;
95
96 // The string pool reference to the entry's name. This uses a different string pool than
97 // the global string pool, but this is hidden from the caller.
98 StringPoolRef entry_string_ref;
99};
100
Ryan Mitchellb894c272020-02-12 10:31:44 -0800101AssetManager2::AssetManager2() {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700102 memset(&configuration_, 0, sizeof(configuration_));
103}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700104
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800105bool AssetManager2::SetApkAssets(std::vector<const ApkAssets*> apk_assets, bool invalidate_caches) {
106 apk_assets_ = std::move(apk_assets);
Adam Lesinskida431a22016-12-29 16:08:16 -0500107 BuildDynamicRefTable();
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800108 RebuildFilterList();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700109 if (invalidate_caches) {
110 InvalidateCaches(static_cast<uint32_t>(-1));
111 }
112 return true;
113}
114
Adam Lesinskida431a22016-12-29 16:08:16 -0500115void AssetManager2::BuildDynamicRefTable() {
116 package_groups_.clear();
117 package_ids_.fill(0xff);
118
Ryan Mitchellef538432021-03-01 14:52:14 -0800119 // A mapping from path of apk assets that could be target packages of overlays to the runtime
120 // package id of its first loaded package. Overlays currently can only override resources in the
121 // first package in the target resource table.
122 std::unordered_map<std::string, uint8_t> target_assets_package_ids;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700123
Ryan Mitchell824cc492020-02-12 10:48:14 -0800124 // Overlay resources are not directly referenced by an application so their resource ids
125 // can change throughout the application's lifetime. Assign overlay package ids last.
126 std::vector<const ApkAssets*> sorted_apk_assets(apk_assets_);
127 std::stable_partition(sorted_apk_assets.begin(), sorted_apk_assets.end(), [](const ApkAssets* a) {
128 return !a->IsOverlay();
129 });
130
131 // The assets cookie must map to the position of the apk assets in the unsorted apk assets list.
132 std::unordered_map<const ApkAssets*, ApkAssetsCookie> apk_assets_cookies;
133 apk_assets_cookies.reserve(apk_assets_.size());
134 for (size_t i = 0, n = apk_assets_.size(); i < n; i++) {
135 apk_assets_cookies[apk_assets_[i]] = static_cast<ApkAssetsCookie>(i);
136 }
137
Ryan Mitchellb894c272020-02-12 10:31:44 -0800138 // 0x01 is reserved for the android package.
139 int next_package_id = 0x02;
Ryan Mitchell824cc492020-02-12 10:48:14 -0800140 for (const ApkAssets* apk_assets : sorted_apk_assets) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800141 std::shared_ptr<OverlayDynamicRefTable> overlay_ref_table;
142 if (auto loaded_idmap = apk_assets->GetLoadedIdmap(); loaded_idmap != nullptr) {
143 // The target package must precede the overlay package in the apk assets paths in order
144 // to take effect.
Ryan Mitchellef538432021-03-01 14:52:14 -0800145 auto iter = target_assets_package_ids.find(std::string(loaded_idmap->TargetApkPath()));
146 if (iter == target_assets_package_ids.end()) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800147 LOG(INFO) << "failed to find target package for overlay "
148 << loaded_idmap->OverlayApkPath();
149 } else {
150 uint8_t target_package_id = iter->second;
151
152 // Create a special dynamic reference table for the overlay to rewrite references to
153 // overlay resources as references to the target resources they overlay.
154 overlay_ref_table = std::make_shared<OverlayDynamicRefTable>(
155 loaded_idmap->GetOverlayDynamicRefTable(target_package_id));
156
157 // Add the overlay resource map to the target package's set of overlays.
158 const uint8_t target_idx = package_ids_[target_package_id];
159 CHECK(target_idx != 0xff) << "overlay target '" << loaded_idmap->TargetApkPath()
160 << "'added to apk_assets_package_ids but does not have an"
161 << " assigned package group";
162
163 PackageGroup& target_package_group = package_groups_[target_idx];
164 target_package_group.overlays_.push_back(
165 ConfiguredOverlay{loaded_idmap->GetTargetResourcesMap(target_package_id,
166 overlay_ref_table.get()),
167 apk_assets_cookies[apk_assets]});
168 }
169 }
170
Ryan Mitchellb894c272020-02-12 10:31:44 -0800171 const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc();
Ryan Mitchellb894c272020-02-12 10:31:44 -0800172 for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) {
173 // Get the package ID or assign one if a shared library.
174 int package_id;
175 if (package->IsDynamic()) {
176 package_id = next_package_id++;
177 } else {
178 package_id = package->GetPackageId();
Adam Lesinskida431a22016-12-29 16:08:16 -0500179 }
180
Adam Lesinskida431a22016-12-29 16:08:16 -0500181 uint8_t idx = package_ids_[package_id];
182 if (idx == 0xff) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800183 // Add the mapping for package ID to index if not present.
Adam Lesinskida431a22016-12-29 16:08:16 -0500184 package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size());
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800185 PackageGroup& new_group = package_groups_.emplace_back();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700186
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800187 if (overlay_ref_table != nullptr) {
188 // If this package is from an overlay, use a dynamic reference table that can rewrite
189 // overlay resource ids to their corresponding target resource ids.
190 new_group.dynamic_ref_table = overlay_ref_table;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700191 }
192
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800193 DynamicRefTable* ref_table = new_group.dynamic_ref_table.get();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700194 ref_table->mAssignedPackageId = package_id;
195 ref_table->mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f;
Adam Lesinskida431a22016-12-29 16:08:16 -0500196 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500197
198 // Add the package and to the set of packages with the same ID.
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800199 PackageGroup* package_group = &package_groups_[idx];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800200 package_group->packages_.push_back(ConfiguredPackage{package.get(), {}});
Ryan Mitchell824cc492020-02-12 10:48:14 -0800201 package_group->cookies_.push_back(apk_assets_cookies[apk_assets]);
Adam Lesinskida431a22016-12-29 16:08:16 -0500202
203 // Add the package name -> build time ID mappings.
204 for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) {
205 String16 package_name(entry.package_name.c_str(), entry.package_name.size());
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700206 package_group->dynamic_ref_table->mEntries.replaceValueFor(
Adam Lesinskida431a22016-12-29 16:08:16 -0500207 package_name, static_cast<uint8_t>(entry.package_id));
208 }
Ryan Mitchellb894c272020-02-12 10:31:44 -0800209
Ryan Mitchellef538432021-03-01 14:52:14 -0800210 if (auto apk_assets_path = apk_assets->GetPath()) {
211 // Overlay target ApkAssets must have been created using path based load apis.
212 target_assets_package_ids.insert(std::make_pair(std::string(*apk_assets_path), package_id));
213 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500214 }
215 }
216
217 // Now assign the runtime IDs so that we have a build-time to runtime ID map.
218 const auto package_groups_end = package_groups_.end();
219 for (auto iter = package_groups_.begin(); iter != package_groups_end; ++iter) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800220 const std::string& package_name = iter->packages_[0].loaded_package_->GetPackageName();
Adam Lesinskida431a22016-12-29 16:08:16 -0500221 for (auto iter2 = package_groups_.begin(); iter2 != package_groups_end; ++iter2) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700222 iter2->dynamic_ref_table->addMapping(String16(package_name.c_str(), package_name.size()),
223 iter->dynamic_ref_table->mAssignedPackageId);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700224
225 // Add the alias resources to the dynamic reference table of every package group. Since
226 // staging aliases can only be defined by the framework package (which is not a shared
227 // library), the compile-time package id of the framework is the same across all packages
228 // that compile against the framework.
Ryan Mitchell2ec8e1b2021-05-11 08:28:00 -0700229 for (const auto& package : iter->packages_) {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700230 for (const auto& entry : package.loaded_package_->GetAliasResourceIdMap()) {
Ryan Mitchell2ec8e1b2021-05-11 08:28:00 -0700231 iter2->dynamic_ref_table->addAlias(entry.first, entry.second);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700232 }
233 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500234 }
235 }
236}
237
238void AssetManager2::DumpToLog() const {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800239 LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this);
240
Adam Lesinskida431a22016-12-29 16:08:16 -0500241 std::string list;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800242 for (const auto& apk_assets : apk_assets_) {
Ryan Mitchellef538432021-03-01 14:52:14 -0800243 base::StringAppendF(&list, "%s,", apk_assets->GetDebugName().c_str());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800244 }
245 LOG(INFO) << "ApkAssets: " << list;
246
247 list = "";
Adam Lesinskida431a22016-12-29 16:08:16 -0500248 for (size_t i = 0; i < package_ids_.size(); i++) {
249 if (package_ids_[i] != 0xff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800250 base::StringAppendF(&list, "%02x -> %d, ", (int)i, package_ids_[i]);
Adam Lesinskida431a22016-12-29 16:08:16 -0500251 }
252 }
253 LOG(INFO) << "Package ID map: " << list;
254
Adam Lesinski0dd36992018-01-25 15:38:38 -0800255 for (const auto& package_group: package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800256 list = "";
257 for (const auto& package : package_group.packages_) {
258 const LoadedPackage* loaded_package = package.loaded_package_;
259 base::StringAppendF(&list, "%s(%02x%s), ", loaded_package->GetPackageName().c_str(),
260 loaded_package->GetPackageId(),
261 (loaded_package->IsDynamic() ? " dynamic" : ""));
262 }
263 LOG(INFO) << base::StringPrintf("PG (%02x): ",
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700264 package_group.dynamic_ref_table->mAssignedPackageId)
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800265 << list;
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800266
267 for (size_t i = 0; i < 256; i++) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700268 if (package_group.dynamic_ref_table->mLookupTable[i] != 0) {
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800269 LOG(INFO) << base::StringPrintf(" e[0x%02x] -> 0x%02x", (uint8_t) i,
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700270 package_group.dynamic_ref_table->mLookupTable[i]);
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800271 }
272 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500273 }
274}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700275
276const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const {
277 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
278 return nullptr;
279 }
280 return apk_assets_[cookie]->GetLoadedArsc()->GetStringPool();
281}
282
Adam Lesinskida431a22016-12-29 16:08:16 -0500283const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const {
284 if (package_id >= package_ids_.size()) {
285 return nullptr;
286 }
287
288 const size_t idx = package_ids_[package_id];
289 if (idx == 0xff) {
290 return nullptr;
291 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700292 return package_groups_[idx].dynamic_ref_table.get();
Adam Lesinskida431a22016-12-29 16:08:16 -0500293}
294
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700295std::shared_ptr<const DynamicRefTable> AssetManager2::GetDynamicRefTableForCookie(
296 ApkAssetsCookie cookie) const {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800297 for (const PackageGroup& package_group : package_groups_) {
298 for (const ApkAssetsCookie& package_cookie : package_group.cookies_) {
299 if (package_cookie == cookie) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700300 return package_group.dynamic_ref_table;
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800301 }
302 }
303 }
304 return nullptr;
305}
306
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100307const std::unordered_map<std::string, std::string>*
308 AssetManager2::GetOverlayableMapForPackage(uint32_t package_id) const {
309
310 if (package_id >= package_ids_.size()) {
311 return nullptr;
312 }
313
314 const size_t idx = package_ids_[package_id];
315 if (idx == 0xff) {
316 return nullptr;
317 }
318
319 const PackageGroup& package_group = package_groups_[idx];
Ryan Mitchell80094e32020-11-16 23:08:18 +0000320 if (package_group.packages_.empty()) {
MÃ¥rten Kongstadc92c4dd2019-02-05 01:29:59 +0100321 return nullptr;
322 }
323
324 const auto loaded_package = package_group.packages_[0].loaded_package_;
325 return &loaded_package->GetOverlayableMap();
326}
327
Ryan Mitchell2e394222019-08-28 12:10:51 -0700328bool AssetManager2::GetOverlayablesToString(const android::StringPiece& package_name,
329 std::string* out) const {
330 uint8_t package_id = 0U;
331 for (const auto& apk_assets : apk_assets_) {
332 const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc();
333 if (loaded_arsc == nullptr) {
334 continue;
335 }
336
337 const auto& loaded_packages = loaded_arsc->GetPackages();
338 if (loaded_packages.empty()) {
339 continue;
340 }
341
342 const auto& loaded_package = loaded_packages[0];
343 if (loaded_package->GetPackageName() == package_name) {
344 package_id = GetAssignedPackageId(loaded_package.get());
345 break;
346 }
347 }
348
349 if (package_id == 0U) {
350 ANDROID_LOG(ERROR) << base::StringPrintf("No package with name '%s", package_name.data());
351 return false;
352 }
353
354 const size_t idx = package_ids_[package_id];
355 if (idx == 0xff) {
356 return false;
357 }
358
359 std::string output;
360 for (const ConfiguredPackage& package : package_groups_[idx].packages_) {
361 const LoadedPackage* loaded_package = package.loaded_package_;
362 for (auto it = loaded_package->begin(); it != loaded_package->end(); it++) {
363 const OverlayableInfo* info = loaded_package->GetOverlayableInfo(*it);
364 if (info != nullptr) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000365 auto res_name = GetResourceName(*it);
366 if (!res_name.has_value()) {
Ryan Mitchell2e394222019-08-28 12:10:51 -0700367 ANDROID_LOG(ERROR) << base::StringPrintf(
368 "Unable to retrieve name of overlayable resource 0x%08x", *it);
369 return false;
370 }
371
Ryan Mitchell80094e32020-11-16 23:08:18 +0000372 const std::string name = ToFormattedResourceString(*res_name);
Ryan Mitchell2e394222019-08-28 12:10:51 -0700373 output.append(base::StringPrintf(
374 "resource='%s' overlayable='%s' actor='%s' policy='0x%08x'\n",
375 name.c_str(), info->name.c_str(), info->actor.c_str(), info->policy_flags));
376 }
377 }
378 }
379
380 *out = std::move(output);
381 return true;
382}
383
Ryan Mitchell192400c2020-04-02 09:54:23 -0700384bool AssetManager2::ContainsAllocatedTable() const {
385 return std::find_if(apk_assets_.begin(), apk_assets_.end(),
386 std::mem_fn(&ApkAssets::IsTableAllocated)) != apk_assets_.end();
387}
388
Adam Lesinski7ad11102016-10-28 16:39:15 -0700389void AssetManager2::SetConfiguration(const ResTable_config& configuration) {
390 const int diff = configuration_.diff(configuration);
391 configuration_ = configuration;
392
393 if (diff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800394 RebuildFilterList();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700395 InvalidateCaches(static_cast<uint32_t>(diff));
396 }
397}
398
Ryan Mitchellef538432021-03-01 14:52:14 -0800399std::set<const ApkAssets*> AssetManager2::GetNonSystemOverlays() const {
400 std::set<const ApkAssets*> non_system_overlays;
Adam Lesinski0c405242017-01-13 20:47:26 -0800401 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800402 bool found_system_package = false;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800403 for (const ConfiguredPackage& package : package_group.packages_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700404 if (package.loaded_package_->IsSystem()) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800405 found_system_package = true;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700406 break;
407 }
408 }
409
410 if (!found_system_package) {
411 for (const ConfiguredOverlay& overlay : package_group.overlays_) {
Ryan Mitchellef538432021-03-01 14:52:14 -0800412 non_system_overlays.insert(apk_assets_[overlay.cookie]);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700413 }
414 }
415 }
416
417 return non_system_overlays;
418}
419
Ryan Mitchell80094e32020-11-16 23:08:18 +0000420base::expected<std::set<ResTable_config>, IOError> AssetManager2::GetResourceConfigurations(
421 bool exclude_system, bool exclude_mipmap) const {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700422 ATRACE_NAME("AssetManager::GetResourceConfigurations");
423 const auto non_system_overlays =
Ryan Mitchellef538432021-03-01 14:52:14 -0800424 (exclude_system) ? GetNonSystemOverlays() : std::set<const ApkAssets*>();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700425
426 std::set<ResTable_config> configurations;
427 for (const PackageGroup& package_group : package_groups_) {
428 for (size_t i = 0; i < package_group.packages_.size(); i++) {
429 const ConfiguredPackage& package = package_group.packages_[i];
430 if (exclude_system && package.loaded_package_->IsSystem()) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800431 continue;
432 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800433
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700434 auto apk_assets = apk_assets_[package_group.cookies_[i]];
Ryan Mitchellef538432021-03-01 14:52:14 -0800435 if (exclude_system && apk_assets->IsOverlay() &&
436 non_system_overlays.find(apk_assets) == non_system_overlays.end()) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700437 // Exclude overlays that target system resources.
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800438 continue;
439 }
440
Ryan Mitchell80094e32020-11-16 23:08:18 +0000441 auto result = package.loaded_package_->CollectConfigurations(exclude_mipmap, &configurations);
442 if (UNLIKELY(!result.has_value())) {
443 return base::unexpected(result.error());
444 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800445 }
446 }
447 return configurations;
448}
449
450std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800451 bool merge_equivalent_languages) const {
452 ATRACE_NAME("AssetManager::GetResourceLocales");
Adam Lesinski0c405242017-01-13 20:47:26 -0800453 std::set<std::string> locales;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700454 const auto non_system_overlays =
Ryan Mitchellef538432021-03-01 14:52:14 -0800455 (exclude_system) ? GetNonSystemOverlays() : std::set<const ApkAssets*>();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700456
Adam Lesinski0c405242017-01-13 20:47:26 -0800457 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700458 for (size_t i = 0; i < package_group.packages_.size(); i++) {
459 const ConfiguredPackage& package = package_group.packages_[i];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800460 if (exclude_system && package.loaded_package_->IsSystem()) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800461 continue;
462 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800463
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700464 auto apk_assets = apk_assets_[package_group.cookies_[i]];
Ryan Mitchellef538432021-03-01 14:52:14 -0800465 if (exclude_system && apk_assets->IsOverlay() &&
466 non_system_overlays.find(apk_assets) == non_system_overlays.end()) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700467 // Exclude overlays that target system resources.
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800468 continue;
469 }
470
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800471 package.loaded_package_->CollectLocales(merge_equivalent_languages, &locales);
Adam Lesinski0c405242017-01-13 20:47:26 -0800472 }
473 }
474 return locales;
475}
476
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800477std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename,
478 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700479 const std::string new_path = "assets/" + filename;
480 return OpenNonAsset(new_path, mode);
481}
482
483std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800484 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700485 const std::string new_path = "assets/" + filename;
486 return OpenNonAsset(new_path, cookie, mode);
487}
488
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800489std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) const {
490 ATRACE_NAME("AssetManager::OpenDir");
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800491
492 std::string full_path = "assets/" + dirname;
493 std::unique_ptr<SortedVector<AssetDir::FileInfo>> files =
494 util::make_unique<SortedVector<AssetDir::FileInfo>>();
495
496 // Start from the back.
497 for (auto iter = apk_assets_.rbegin(); iter != apk_assets_.rend(); ++iter) {
498 const ApkAssets* apk_assets = *iter;
MÃ¥rten Kongstaddbf343b2019-02-21 07:54:18 +0100499 if (apk_assets->IsOverlay()) {
500 continue;
501 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800502
503 auto func = [&](const StringPiece& name, FileType type) {
504 AssetDir::FileInfo info;
505 info.setFileName(String8(name.data(), name.size()));
506 info.setFileType(type);
Ryan Mitchellef538432021-03-01 14:52:14 -0800507 info.setSourceName(String8(apk_assets->GetDebugName().c_str()));
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800508 files->add(info);
509 };
510
Ryan Mitchellc07aa702020-03-10 13:49:12 -0700511 if (!apk_assets->GetAssetsProvider()->ForEachFile(full_path, func)) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800512 return {};
513 }
514 }
515
516 std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>();
517 asset_dir->setFileList(files.release());
518 return asset_dir;
519}
520
Adam Lesinski7ad11102016-10-28 16:39:15 -0700521// Search in reverse because that's how we used to do it and we need to preserve behaviour.
522// This is unfortunate, because ClassLoaders delegate to the parent first, so the order
523// is inconsistent for split APKs.
524std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
525 Asset::AccessMode mode,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800526 ApkAssetsCookie* out_cookie) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700527 for (int32_t i = apk_assets_.size() - 1; i >= 0; i--) {
MÃ¥rten Kongstaddbf343b2019-02-21 07:54:18 +0100528 // Prevent RRO from modifying assets and other entries accessed by file
529 // path. Explicitly asking for a path in a given package (denoted by a
530 // cookie) is still OK.
531 if (apk_assets_[i]->IsOverlay()) {
532 continue;
533 }
534
Ryan Mitchellc07aa702020-03-10 13:49:12 -0700535 std::unique_ptr<Asset> asset = apk_assets_[i]->GetAssetsProvider()->Open(filename, mode);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700536 if (asset) {
537 if (out_cookie != nullptr) {
538 *out_cookie = i;
539 }
540 return asset;
541 }
542 }
543
544 if (out_cookie != nullptr) {
545 *out_cookie = kInvalidCookie;
546 }
547 return {};
548}
549
550std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800551 ApkAssetsCookie cookie,
552 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700553 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
554 return {};
555 }
Ryan Mitchellc07aa702020-03-10 13:49:12 -0700556 return apk_assets_[cookie]->GetAssetsProvider()->Open(filename, mode);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700557}
558
Ryan Mitchell80094e32020-11-16 23:08:18 +0000559base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntry(
560 uint32_t resid, uint16_t density_override, bool stop_at_first_match,
561 bool ignore_configuration) const {
562 const bool logging_enabled = resource_resolution_logging_enabled_;
563 if (UNLIKELY(logging_enabled)) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700564 // Clear the last logged resource resolution.
565 ResetResourceResolution();
566 last_resolution_.resid = resid;
567 }
568
Adam Lesinski7ad11102016-10-28 16:39:15 -0700569 // Might use this if density_override != 0.
570 ResTable_config density_override_config;
571
572 // Select our configuration or generate a density override configuration.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800573 const ResTable_config* desired_config = &configuration_;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700574 if (density_override != 0 && density_override != configuration_.density) {
575 density_override_config = configuration_;
576 density_override_config.density = density_override;
577 desired_config = &density_override_config;
578 }
579
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700580 // Retrieve the package group from the package id of the resource id.
Ryan Mitchell80094e32020-11-16 23:08:18 +0000581 if (UNLIKELY(!is_valid_resid(resid))) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500582 LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +0000583 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -0500584 }
585
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800586 const uint32_t package_id = get_package_id(resid);
587 const uint8_t type_idx = get_type_id(resid) - 1;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800588 const uint16_t entry_idx = get_entry_id(resid);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700589 uint8_t package_idx = package_ids_[package_id];
Ryan Mitchell80094e32020-11-16 23:08:18 +0000590 if (UNLIKELY(package_idx == 0xff)) {
Ryan Mitchell2fe23472019-02-27 09:43:01 -0800591 ANDROID_LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.",
592 package_id, resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +0000593 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -0500594 }
595
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800596 const PackageGroup& package_group = package_groups_[package_idx];
Ryan Mitchell80094e32020-11-16 23:08:18 +0000597 auto result = FindEntryInternal(package_group, type_idx, entry_idx, *desired_config,
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800598 stop_at_first_match, ignore_configuration);
Ryan Mitchell80094e32020-11-16 23:08:18 +0000599 if (UNLIKELY(!result.has_value())) {
600 return base::unexpected(result.error());
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700601 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800602
Ryan Mitchell80094e32020-11-16 23:08:18 +0000603 if (!stop_at_first_match && !ignore_configuration && !apk_assets_[result->cookie]->IsLoader()) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700604 for (const auto& id_map : package_group.overlays_) {
605 auto overlay_entry = id_map.overlay_res_maps_.Lookup(resid);
606 if (!overlay_entry) {
607 // No id map entry exists for this target resource.
608 continue;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000609 }
610 if (overlay_entry.IsInlineValue()) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700611 // The target resource is overlaid by an inline value not represented by a resource.
Ryan Mitchell80094e32020-11-16 23:08:18 +0000612 result->entry = overlay_entry.GetInlineValue();
613 result->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
614 result->cookie = id_map.cookie;
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800615
616 if (UNLIKELY(logging_enabled)) {
617 last_resolution_.steps.push_back(
618 Resolution::Step{Resolution::Step::Type::OVERLAID_INLINE, String8(), result->cookie});
619 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700620 continue;
621 }
622
Ryan Mitchell80094e32020-11-16 23:08:18 +0000623 auto overlay_result = FindEntry(overlay_entry.GetResourceId(), density_override,
624 false /* stop_at_first_match */,
625 false /* ignore_configuration */);
626 if (UNLIKELY(IsIOError(overlay_result))) {
627 return base::unexpected(overlay_result.error());
628 }
629 if (!overlay_result.has_value()) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700630 continue;
631 }
632
Ryan Mitchell80094e32020-11-16 23:08:18 +0000633 if (!overlay_result->config.isBetterThan(result->config, desired_config)
634 && overlay_result->config.compare(result->config) != 0) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700635 // The configuration of the entry for the overlay must be equal to or better than the target
636 // configuration to be chosen as the better value.
637 continue;
638 }
639
Ryan Mitchell80094e32020-11-16 23:08:18 +0000640 result->cookie = overlay_result->cookie;
641 result->entry = overlay_result->entry;
642 result->config = overlay_result->config;
643 result->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
644
645 if (UNLIKELY(logging_enabled)) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700646 last_resolution_.steps.push_back(
Ryan Mitchell80094e32020-11-16 23:08:18 +0000647 Resolution::Step{Resolution::Step::Type::OVERLAID, overlay_result->config.toString(),
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800648 overlay_result->cookie});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700649 }
650 }
651 }
652
Ryan Mitchell80094e32020-11-16 23:08:18 +0000653 if (UNLIKELY(logging_enabled)) {
654 last_resolution_.cookie = result->cookie;
655 last_resolution_.type_string_ref = result->type_string_ref;
656 last_resolution_.entry_string_ref = result->entry_string_ref;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700657 }
658
Ryan Mitchell80094e32020-11-16 23:08:18 +0000659 return result;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700660}
661
Ryan Mitchell80094e32020-11-16 23:08:18 +0000662base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntryInternal(
663 const PackageGroup& package_group, uint8_t type_idx, uint16_t entry_idx,
664 const ResTable_config& desired_config, bool stop_at_first_match,
665 bool ignore_configuration) const {
666 const bool logging_enabled = resource_resolution_logging_enabled_;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800667 ApkAssetsCookie best_cookie = kInvalidCookie;
668 const LoadedPackage* best_package = nullptr;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000669 incfs::verified_map_ptr<ResTable_type> best_type;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800670 const ResTable_config* best_config = nullptr;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000671 uint32_t best_offset = 0U;
672 uint32_t type_flags = 0U;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800673
Winson2f3669b2019-01-11 11:28:34 -0800674 std::vector<Resolution::Step> resolution_steps;
675
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800676 // If `desired_config` is not the same as the set configuration or the caller will accept a value
677 // from any configuration, then we cannot use our filtered list of types since it only it contains
678 // types matched to the set configuration.
679 const bool use_filtered = !ignore_configuration && &desired_config == &configuration_;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800680
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700681 const size_t package_count = package_group.packages_.size();
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800682 for (size_t pi = 0; pi < package_count; pi++) {
683 const ConfiguredPackage& loaded_package_impl = package_group.packages_[pi];
684 const LoadedPackage* loaded_package = loaded_package_impl.loaded_package_;
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800685 const ApkAssetsCookie cookie = package_group.cookies_[pi];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800686
687 // If the type IDs are offset in this package, we need to take that into account when searching
688 // for a type.
689 const TypeSpec* type_spec = loaded_package->GetTypeSpecByTypeIndex(type_idx);
690 if (UNLIKELY(type_spec == nullptr)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700691 continue;
692 }
693
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800694 // Allow custom loader packages to overlay resource values with configurations equivalent to the
695 // current best configuration.
696 const bool package_is_loader = loaded_package->IsCustomLoader();
697
Ryan Mitchell80094e32020-11-16 23:08:18 +0000698 auto entry_flags = type_spec->GetFlagsForEntryIndex(entry_idx);
Bernie Innocenti58cf8e32020-12-19 15:31:52 +0900699 if (UNLIKELY(!entry_flags.has_value())) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000700 return base::unexpected(entry_flags.error());
701 }
702 type_flags |= entry_flags.value();
703
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800704 const FilteredConfigGroup& filtered_group = loaded_package_impl.filtered_configs_[type_idx];
705 const size_t type_entry_count = (use_filtered) ? filtered_group.type_entries.size()
706 : type_spec->type_entries.size();
707 for (size_t i = 0; i < type_entry_count; i++) {
708 const TypeSpec::TypeEntry* type_entry = (use_filtered) ? filtered_group.type_entries[i]
709 : &type_spec->type_entries[i];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800710
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800711 // We can skip calling ResTable_config::match() if the caller does not care for the
712 // configuration to match or if we're using the list of types that have already had their
713 // configuration matched.
714 const ResTable_config& this_config = type_entry->config;
715 if (!(use_filtered || ignore_configuration || this_config.match(desired_config))) {
716 continue;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800717 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800718
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800719 Resolution::Step::Type resolution_type;
720 if (best_config == nullptr) {
721 resolution_type = Resolution::Step::Type::INITIAL;
722 } else if (this_config.isBetterThan(*best_config, &desired_config)) {
723 resolution_type = Resolution::Step::Type::BETTER_MATCH;
724 } else if (package_is_loader && this_config.compare(*best_config) == 0) {
725 resolution_type = Resolution::Step::Type::OVERLAID;
726 } else {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000727 if (UNLIKELY(logging_enabled)) {
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800728 resolution_steps.push_back(Resolution::Step{Resolution::Step::Type::SKIPPED,
729 this_config.toString(),
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800730 cookie});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800731 }
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800732 continue;
733 }
734
735 // The configuration matches and is better than the previous selection.
736 // Find the entry value if it exists for this configuration.
737 const auto& type = type_entry->type;
738 const auto offset = LoadedPackage::GetEntryOffset(type, entry_idx);
739 if (UNLIKELY(IsIOError(offset))) {
740 return base::unexpected(offset.error());
741 }
742
743 if (!offset.has_value()) {
744 if (UNLIKELY(logging_enabled)) {
745 resolution_steps.push_back(Resolution::Step{Resolution::Step::Type::NO_ENTRY,
746 this_config.toString(),
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800747 cookie});
748 }
749 continue;
750 }
751
752 best_cookie = cookie;
753 best_package = loaded_package;
754 best_type = type;
755 best_config = &this_config;
756 best_offset = offset.value();
757
758 if (UNLIKELY(logging_enabled)) {
759 last_resolution_.steps.push_back(Resolution::Step{resolution_type,
760 this_config.toString(),
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800761 cookie});
762 }
763
764 // Any configuration will suffice, so break.
765 if (stop_at_first_match) {
766 break;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700767 }
768 }
769 }
770
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800771 if (UNLIKELY(best_cookie == kInvalidCookie)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000772 return base::unexpected(std::nullopt);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700773 }
774
Ryan Mitchell80094e32020-11-16 23:08:18 +0000775 auto best_entry_result = LoadedPackage::GetEntryFromOffset(best_type, best_offset);
776 if (!best_entry_result.has_value()) {
777 return base::unexpected(best_entry_result.error());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800778 }
779
Ryan Mitchell80094e32020-11-16 23:08:18 +0000780 const incfs::map_ptr<ResTable_entry> best_entry = *best_entry_result;
781 if (!best_entry) {
782 return base::unexpected(IOError::PAGES_MISSING);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700783 }
784
Ryan Mitchell80094e32020-11-16 23:08:18 +0000785 const auto entry = GetEntryValue(best_entry.verified());
786 if (!entry.has_value()) {
787 return base::unexpected(entry.error());
788 }
Winson2f3669b2019-01-11 11:28:34 -0800789
Ryan Mitchell80094e32020-11-16 23:08:18 +0000790 return FindEntryResult{
791 .cookie = best_cookie,
792 .entry = *entry,
793 .config = *best_config,
794 .type_flags = type_flags,
795 .package_name = &best_package->GetPackageName(),
796 .type_string_ref = StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1),
797 .entry_string_ref = StringPoolRef(best_package->GetKeyStringPool(),
798 best_entry->key.index),
799 .dynamic_ref_table = package_group.dynamic_ref_table.get(),
800 };
Adam Lesinski7ad11102016-10-28 16:39:15 -0700801}
802
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700803void AssetManager2::ResetResourceResolution() const {
804 last_resolution_.cookie = kInvalidCookie;
805 last_resolution_.resid = 0;
806 last_resolution_.steps.clear();
807 last_resolution_.type_string_ref = StringPoolRef();
808 last_resolution_.entry_string_ref = StringPoolRef();
809}
810
Winson2f3669b2019-01-11 11:28:34 -0800811void AssetManager2::SetResourceResolutionLoggingEnabled(bool enabled) {
812 resource_resolution_logging_enabled_ = enabled;
Winson2f3669b2019-01-11 11:28:34 -0800813 if (!enabled) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700814 ResetResourceResolution();
Winson2f3669b2019-01-11 11:28:34 -0800815 }
816}
817
818std::string AssetManager2::GetLastResourceResolution() const {
819 if (!resource_resolution_logging_enabled_) {
820 LOG(ERROR) << "Must enable resource resolution logging before getting path.";
Ryan Mitchell80094e32020-11-16 23:08:18 +0000821 return {};
Winson2f3669b2019-01-11 11:28:34 -0800822 }
823
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800824 const ApkAssetsCookie cookie = last_resolution_.cookie;
Winson2f3669b2019-01-11 11:28:34 -0800825 if (cookie == kInvalidCookie) {
826 LOG(ERROR) << "AssetManager hasn't resolved a resource to read resolution path.";
Ryan Mitchell80094e32020-11-16 23:08:18 +0000827 return {};
Winson2f3669b2019-01-11 11:28:34 -0800828 }
829
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800830 const uint32_t resid = last_resolution_.resid;
831 const auto package = apk_assets_[cookie]->GetLoadedArsc()->GetPackageById(get_package_id(resid));
832
Winson2f3669b2019-01-11 11:28:34 -0800833 std::string resource_name_string;
Winson2f3669b2019-01-11 11:28:34 -0800834 if (package != nullptr) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000835 auto resource_name = ToResourceName(last_resolution_.type_string_ref,
836 last_resolution_.entry_string_ref,
837 package->GetPackageName());
838 resource_name_string = resource_name.has_value() ?
839 ToFormattedResourceString(resource_name.value()) : "<unknown>";
Winson2f3669b2019-01-11 11:28:34 -0800840 }
841
842 std::stringstream log_stream;
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800843 log_stream << base::StringPrintf("Resolution for 0x%08x %s\n"
844 "\tFor config - %s", resid, resource_name_string.c_str(),
845 configuration_.toString().c_str());
Winson2f3669b2019-01-11 11:28:34 -0800846
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800847 for (const Resolution::Step& step : last_resolution_.steps) {
848 const static std::unordered_map<Resolution::Step::Type, const char*> kStepStrings = {
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800849 {Resolution::Step::Type::INITIAL, "Found initial"},
850 {Resolution::Step::Type::BETTER_MATCH, "Found better"},
851 {Resolution::Step::Type::OVERLAID, "Overlaid"},
852 {Resolution::Step::Type::OVERLAID_INLINE, "Overlaid inline"},
853 {Resolution::Step::Type::SKIPPED, "Skipped"},
854 {Resolution::Step::Type::NO_ENTRY, "No entry"}
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800855 };
856
857 const auto prefix = kStepStrings.find(step.type);
858 if (prefix == kStepStrings.end()) {
859 continue;
Winson2f3669b2019-01-11 11:28:34 -0800860 }
861
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800862 log_stream << "\n\t" << prefix->second << ": " << apk_assets_[step.cookie]->GetDebugName();
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800863 if (!step.config_name.isEmpty()) {
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800864 log_stream << " - " << step.config_name;
Winson2f3669b2019-01-11 11:28:34 -0800865 }
866 }
867
868 return log_stream.str();
869}
870
Ryan Mitchell80094e32020-11-16 23:08:18 +0000871base::expected<AssetManager2::ResourceName, NullOrIOError> AssetManager2::GetResourceName(
872 uint32_t resid) const {
873 auto result = FindEntry(resid, 0u /* density_override */, true /* stop_at_first_match */,
874 true /* ignore_configuration */);
875 if (!result.has_value()) {
876 return base::unexpected(result.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700877 }
878
Ryan Mitchell80094e32020-11-16 23:08:18 +0000879 return ToResourceName(result->type_string_ref,
880 result->entry_string_ref,
881 *result->package_name);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700882}
883
Ryan Mitchell14e8ade2021-01-11 16:01:35 -0800884base::expected<uint32_t, NullOrIOError> AssetManager2::GetResourceTypeSpecFlags(
885 uint32_t resid) const {
886 auto result = FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */,
887 true /* ignore_configuration */);
888 if (!result.has_value()) {
889 return base::unexpected(result.error());
890 }
891 return result->type_flags;
892}
893
Ryan Mitchell80094e32020-11-16 23:08:18 +0000894base::expected<AssetManager2::SelectedValue, NullOrIOError> AssetManager2::GetResource(
895 uint32_t resid, bool may_be_bag, uint16_t density_override) const {
896 auto result = FindEntry(resid, density_override, false /* stop_at_first_match */,
897 false /* ignore_configuration */);
898 if (!result.has_value()) {
899 return base::unexpected(result.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700900 }
901
Ryan Mitchell80094e32020-11-16 23:08:18 +0000902 auto result_map_entry = std::get_if<incfs::verified_map_ptr<ResTable_map_entry>>(&result->entry);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700903 if (result_map_entry != nullptr) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700904 if (!may_be_bag) {
905 LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +0000906 return base::unexpected(std::nullopt);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700907 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800908
909 // Create a reference since we can't represent this complex type as a Res_value.
Ryan Mitchell80094e32020-11-16 23:08:18 +0000910 return SelectedValue(Res_value::TYPE_REFERENCE, resid, result->cookie, result->type_flags,
911 resid, result->config);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700912 }
913
Adam Lesinskida431a22016-12-29 16:08:16 -0500914 // Convert the package ID to the runtime assigned package ID.
Ryan Mitchell80094e32020-11-16 23:08:18 +0000915 Res_value value = std::get<Res_value>(result->entry);
916 result->dynamic_ref_table->lookupResourceValue(&value);
Adam Lesinskida431a22016-12-29 16:08:16 -0500917
Ryan Mitchell80094e32020-11-16 23:08:18 +0000918 return SelectedValue(value.dataType, value.data, result->cookie, result->type_flags,
919 resid, result->config);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700920}
921
Ryan Mitchell80094e32020-11-16 23:08:18 +0000922base::expected<std::monostate, NullOrIOError> AssetManager2::ResolveReference(
Ryan Mitchella45506e2020-11-16 23:08:18 +0000923 AssetManager2::SelectedValue& value, bool cache_value) const {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000924 if (value.type != Res_value::TYPE_REFERENCE || value.data == 0U) {
925 // Not a reference. Nothing to do.
926 return {};
Adam Lesinski0c405242017-01-13 20:47:26 -0800927 }
Ryan Mitchell80094e32020-11-16 23:08:18 +0000928
Ryan Mitchella45506e2020-11-16 23:08:18 +0000929 const uint32_t original_flags = value.flags;
930 const uint32_t original_resid = value.data;
931 if (cache_value) {
932 auto cached_value = cached_resolved_values_.find(value.data);
933 if (cached_value != cached_resolved_values_.end()) {
934 value = cached_value->second;
935 value.flags |= original_flags;
936 return {};
937 }
938 }
939
940 uint32_t combined_flags = 0U;
941 uint32_t resolve_resid = original_resid;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000942 constexpr const uint32_t kMaxIterations = 20;
943 for (uint32_t i = 0U;; i++) {
944 auto result = GetResource(resolve_resid, true /*may_be_bag*/);
945 if (!result.has_value()) {
Ryan Mitchelle7ab6272020-11-13 18:06:15 -0800946 value.resid = resolve_resid;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000947 return base::unexpected(result.error());
948 }
949
Ryan Mitchelle7ab6272020-11-13 18:06:15 -0800950 // If resource resolution fails, the value should be set to the last reference that was able to
951 // be resolved successfully.
952 value = *result;
953 value.flags |= combined_flags;
954
Ryan Mitchell80094e32020-11-16 23:08:18 +0000955 if (result->type != Res_value::TYPE_REFERENCE ||
956 result->data == Res_value::DATA_NULL_UNDEFINED ||
957 result->data == resolve_resid || i == kMaxIterations) {
958 // This reference can't be resolved, so exit now and let the caller deal with it.
Ryan Mitchella45506e2020-11-16 23:08:18 +0000959 if (cache_value) {
960 cached_resolved_values_[original_resid] = value;
961 }
962
963 // Above value is cached without original_flags to ensure they don't get included in future
964 // queries that hit the cache
965 value.flags |= original_flags;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000966 return {};
967 }
968
Ryan Mitchelle7ab6272020-11-13 18:06:15 -0800969 combined_flags = result->flags;
Ryan Mitchell80094e32020-11-16 23:08:18 +0000970 resolve_resid = result->data;
971 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800972}
973
Ryan Mitchell80094e32020-11-16 23:08:18 +0000974const std::vector<uint32_t> AssetManager2::GetBagResIdStack(uint32_t resid) const {
Aurimas Liutikas8f004c82019-01-17 17:20:10 -0800975 auto cached_iter = cached_bag_resid_stacks_.find(resid);
976 if (cached_iter != cached_bag_resid_stacks_.end()) {
977 return cached_iter->second;
Aurimas Liutikas8f004c82019-01-17 17:20:10 -0800978 }
Ryan Mitchell80094e32020-11-16 23:08:18 +0000979
980 std::vector<uint32_t> found_resids;
981 GetBag(resid, found_resids);
982 cached_bag_resid_stacks_.emplace(resid, found_resids);
983 return found_resids;
Aurimas Liutikas8f004c82019-01-17 17:20:10 -0800984}
985
Ryan Mitchell80094e32020-11-16 23:08:18 +0000986base::expected<const ResolvedBag*, NullOrIOError> AssetManager2::ResolveBag(
987 AssetManager2::SelectedValue& value) const {
988 if (UNLIKELY(value.type != Res_value::TYPE_REFERENCE)) {
989 return base::unexpected(std::nullopt);
990 }
Aurimas Liutikas8f004c82019-01-17 17:20:10 -0800991
Ryan Mitchell80094e32020-11-16 23:08:18 +0000992 auto bag = GetBag(value.data);
993 if (bag.has_value()) {
994 value.flags |= (*bag)->type_spec_flags;
Aurimas Liutikas8f004c82019-01-17 17:20:10 -0800995 }
996 return bag;
y57cd1952018-04-12 14:26:23 -0700997}
998
Ryan Mitchell80094e32020-11-16 23:08:18 +0000999base::expected<const ResolvedBag*, NullOrIOError> AssetManager2::GetBag(uint32_t resid) const {
1000 std::vector<uint32_t> found_resids;
1001 const auto bag = GetBag(resid, found_resids);
1002 cached_bag_resid_stacks_.emplace(resid, found_resids);
1003 return bag;
Ryan Mitchell155d5392020-02-10 13:35:24 -08001004}
1005
Ryan Mitchell80094e32020-11-16 23:08:18 +00001006base::expected<const ResolvedBag*, NullOrIOError> AssetManager2::GetBag(
1007 uint32_t resid, std::vector<uint32_t>& child_resids) const {
1008 if (auto cached_iter = cached_bags_.find(resid); cached_iter != cached_bags_.end()) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001009 return cached_iter->second.get();
1010 }
1011
Ryan Mitchell80094e32020-11-16 23:08:18 +00001012 auto entry = FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */,
1013 false /* ignore_configuration */);
1014 if (!entry.has_value()) {
1015 return base::unexpected(entry.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001016 }
1017
Ryan Mitchell80094e32020-11-16 23:08:18 +00001018 auto entry_map = std::get_if<incfs::verified_map_ptr<ResTable_map_entry>>(&entry->entry);
1019 if (entry_map == nullptr) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001020 // Not a bag, nothing to do.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001021 return base::unexpected(std::nullopt);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001022 }
1023
Ryan Mitchell80094e32020-11-16 23:08:18 +00001024 auto map = *entry_map;
1025 auto map_entry = map.offset(dtohs(map->size)).convert<ResTable_map>();
1026 const auto map_entry_end = map_entry + dtohl(map->count);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001027
y57cd1952018-04-12 14:26:23 -07001028 // Keep track of ids that have already been seen to prevent infinite loops caused by circular
Ryan Mitchell80094e32020-11-16 23:08:18 +00001029 // dependencies between bags.
y57cd1952018-04-12 14:26:23 -07001030 child_resids.push_back(resid);
1031
Adam Lesinskida431a22016-12-29 16:08:16 -05001032 uint32_t parent_resid = dtohl(map->parent.ident);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001033 if (parent_resid == 0U ||
1034 std::find(child_resids.begin(), child_resids.end(), parent_resid) != child_resids.end()) {
1035 // There is no parent or a circular parental dependency exist, meaning there is nothing to
1036 // inherit and we can do a simple copy of the entries in the map.
Adam Lesinski7ad11102016-10-28 16:39:15 -07001037 const size_t entry_count = map_entry_end - map_entry;
1038 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
1039 malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))};
Ryan Mitchell155d5392020-02-10 13:35:24 -08001040
1041 bool sort_entries = false;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001042 for (auto new_entry = new_bag->entries; map_entry != map_entry_end; ++map_entry) {
1043 if (UNLIKELY(!map_entry)) {
1044 return base::unexpected(IOError::PAGES_MISSING);
1045 }
1046
Adam Lesinskida431a22016-12-29 16:08:16 -05001047 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001048 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001049 // Attributes, arrays, etc don't have a resource id as the name. They specify
1050 // other data, which would be wrong to change via a lookup.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001051 if (UNLIKELY(entry->dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001052 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
1053 resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001054 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -05001055 }
1056 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001057
1058 new_entry->cookie = entry->cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001059 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001060 new_entry->key_pool = nullptr;
1061 new_entry->type_pool = nullptr;
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001062 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -07001063 new_entry->value.copyFrom_dtoh(map_entry->value);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001064 status_t err = entry->dynamic_ref_table->lookupResourceValue(&new_entry->value);
1065 if (UNLIKELY(err != NO_ERROR)) {
Adam Lesinski30080e22017-10-16 16:18:09 -07001066 LOG(ERROR) << base::StringPrintf(
1067 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
1068 new_entry->value.data, new_key);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001069 return base::unexpected(std::nullopt);
Adam Lesinski30080e22017-10-16 16:18:09 -07001070 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001071
Ryan Mitchell155d5392020-02-10 13:35:24 -08001072 sort_entries = sort_entries ||
1073 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001074 ++new_entry;
1075 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001076
1077 if (sort_entries) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001078 std::sort(new_bag->entries, new_bag->entries + entry_count,
1079 [](auto&& lhs, auto&& rhs) { return lhs.key < rhs.key; });
Ryan Mitchell155d5392020-02-10 13:35:24 -08001080 }
1081
Ryan Mitchell80094e32020-11-16 23:08:18 +00001082 new_bag->type_spec_flags = entry->type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001083 new_bag->entry_count = static_cast<uint32_t>(entry_count);
1084 ResolvedBag* result = new_bag.get();
1085 cached_bags_[resid] = std::move(new_bag);
1086 return result;
1087 }
1088
Adam Lesinskida431a22016-12-29 16:08:16 -05001089 // In case the parent is a dynamic reference, resolve it.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001090 entry->dynamic_ref_table->lookupResourceId(&parent_resid);
Adam Lesinskida431a22016-12-29 16:08:16 -05001091
Adam Lesinski7ad11102016-10-28 16:39:15 -07001092 // Get the parent and do a merge of the keys.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001093 const auto parent_bag = GetBag(parent_resid, child_resids);
1094 if (UNLIKELY(!parent_bag.has_value())) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001095 // Failed to get the parent that should exist.
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001096 LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid,
1097 resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001098 return base::unexpected(parent_bag.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001099 }
1100
Adam Lesinski7ad11102016-10-28 16:39:15 -07001101 // Create the max possible entries we can make. Once we construct the bag,
1102 // we will realloc to fit to size.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001103 const size_t max_count = (*parent_bag)->entry_count + dtohl(map->count);
George Burgess IV09b119f2017-07-25 15:00:04 -07001104 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
1105 malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001106 ResolvedBag::Entry* new_entry = new_bag->entries;
1107
Ryan Mitchell80094e32020-11-16 23:08:18 +00001108 const ResolvedBag::Entry* parent_entry = (*parent_bag)->entries;
1109 const ResolvedBag::Entry* const parent_entry_end = parent_entry + (*parent_bag)->entry_count;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001110
1111 // The keys are expected to be in sorted order. Merge the two bags.
Ryan Mitchell155d5392020-02-10 13:35:24 -08001112 bool sort_entries = false;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001113 while (map_entry != map_entry_end && parent_entry != parent_entry_end) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001114 if (UNLIKELY(!map_entry)) {
1115 return base::unexpected(IOError::PAGES_MISSING);
1116 }
1117
Adam Lesinskida431a22016-12-29 16:08:16 -05001118 uint32_t child_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001119 if (!is_internal_resid(child_key)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001120 if (UNLIKELY(entry->dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001121 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key,
1122 resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001123 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -05001124 }
1125 }
1126
Adam Lesinski7ad11102016-10-28 16:39:15 -07001127 if (child_key <= parent_entry->key) {
1128 // Use the child key if it comes before the parent
1129 // or is equal to the parent (overrides).
Ryan Mitchell80094e32020-11-16 23:08:18 +00001130 new_entry->cookie = entry->cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001131 new_entry->key = child_key;
1132 new_entry->key_pool = nullptr;
1133 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -07001134 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001135 new_entry->style = resid;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001136 status_t err = entry->dynamic_ref_table->lookupResourceValue(&new_entry->value);
1137 if (UNLIKELY(err != NO_ERROR)) {
Adam Lesinski30080e22017-10-16 16:18:09 -07001138 LOG(ERROR) << base::StringPrintf(
1139 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
1140 new_entry->value.data, child_key);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001141 return base::unexpected(std::nullopt);
Adam Lesinski30080e22017-10-16 16:18:09 -07001142 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001143 ++map_entry;
1144 } else {
1145 // Take the parent entry as-is.
1146 memcpy(new_entry, parent_entry, sizeof(*new_entry));
1147 }
1148
Ryan Mitchell155d5392020-02-10 13:35:24 -08001149 sort_entries = sort_entries ||
1150 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001151 if (child_key >= parent_entry->key) {
1152 // Move to the next parent entry if we used it or it was overridden.
1153 ++parent_entry;
1154 }
1155 // Increment to the next entry to fill.
1156 ++new_entry;
1157 }
1158
1159 // Finish the child entries if they exist.
1160 while (map_entry != map_entry_end) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001161 if (UNLIKELY(!map_entry)) {
1162 return base::unexpected(IOError::PAGES_MISSING);
1163 }
1164
Adam Lesinskida431a22016-12-29 16:08:16 -05001165 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001166 if (!is_internal_resid(new_key)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001167 if (UNLIKELY(entry->dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001168 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
1169 resid);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001170 return base::unexpected(std::nullopt);
Adam Lesinskida431a22016-12-29 16:08:16 -05001171 }
1172 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001173 new_entry->cookie = entry->cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001174 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001175 new_entry->key_pool = nullptr;
1176 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -07001177 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001178 new_entry->style = resid;
Ryan Mitchell80094e32020-11-16 23:08:18 +00001179 status_t err = entry->dynamic_ref_table->lookupResourceValue(&new_entry->value);
1180 if (UNLIKELY(err != NO_ERROR)) {
Adam Lesinski30080e22017-10-16 16:18:09 -07001181 LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.",
1182 new_entry->value.dataType, new_entry->value.data, new_key);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001183 return base::unexpected(std::nullopt);
Adam Lesinski30080e22017-10-16 16:18:09 -07001184 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001185 sort_entries = sort_entries ||
1186 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001187 ++map_entry;
1188 ++new_entry;
1189 }
1190
1191 // Finish the parent entries if they exist.
1192 if (parent_entry != parent_entry_end) {
1193 // Take the rest of the parent entries as-is.
1194 const size_t num_entries_to_copy = parent_entry_end - parent_entry;
1195 memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry));
1196 new_entry += num_entries_to_copy;
1197 }
1198
1199 // Resize the resulting array to fit.
1200 const size_t actual_count = new_entry - new_bag->entries;
1201 if (actual_count != max_count) {
George Burgess IV09b119f2017-07-25 15:00:04 -07001202 new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc(
1203 new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry)))));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001204 }
1205
Ryan Mitchell155d5392020-02-10 13:35:24 -08001206 if (sort_entries) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001207 std::sort(new_bag->entries, new_bag->entries + actual_count,
1208 [](auto&& lhs, auto&& rhs) { return lhs.key < rhs.key; });
Ryan Mitchell155d5392020-02-10 13:35:24 -08001209 }
1210
Adam Lesinski1a1e9c22017-10-13 15:45:34 -07001211 // Combine flags from the parent and our own bag.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001212 new_bag->type_spec_flags = entry->type_flags | (*parent_bag)->type_spec_flags;
George Burgess IV09b119f2017-07-25 15:00:04 -07001213 new_bag->entry_count = static_cast<uint32_t>(actual_count);
1214 ResolvedBag* result = new_bag.get();
1215 cached_bags_[resid] = std::move(new_bag);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001216 return result;
1217}
1218
Adam Lesinski929d6512017-01-16 19:11:19 -08001219static bool Utf8ToUtf16(const StringPiece& str, std::u16string* out) {
1220 ssize_t len =
1221 utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false);
1222 if (len < 0) {
1223 return false;
1224 }
1225 out->resize(static_cast<size_t>(len));
1226 utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(),
1227 static_cast<size_t>(len + 1));
1228 return true;
1229}
1230
Ryan Mitchell80094e32020-11-16 23:08:18 +00001231base::expected<uint32_t, NullOrIOError> AssetManager2::GetResourceId(
1232 const std::string& resource_name, const std::string& fallback_type,
1233 const std::string& fallback_package) const {
Adam Lesinski929d6512017-01-16 19:11:19 -08001234 StringPiece package_name, type, entry;
1235 if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001236 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -08001237 }
1238
1239 if (entry.empty()) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001240 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -08001241 }
1242
1243 if (package_name.empty()) {
1244 package_name = fallback_package;
1245 }
1246
1247 if (type.empty()) {
1248 type = fallback_type;
1249 }
1250
1251 std::u16string type16;
1252 if (!Utf8ToUtf16(type, &type16)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001253 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -08001254 }
1255
1256 std::u16string entry16;
1257 if (!Utf8ToUtf16(entry, &entry16)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001258 return base::unexpected(std::nullopt);
Adam Lesinski929d6512017-01-16 19:11:19 -08001259 }
1260
1261 const StringPiece16 kAttr16 = u"attr";
1262 const static std::u16string kAttrPrivate16 = u"^attr-private";
1263
1264 for (const PackageGroup& package_group : package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001265 for (const ConfiguredPackage& package_impl : package_group.packages_) {
1266 const LoadedPackage* package = package_impl.loaded_package_;
Adam Lesinski929d6512017-01-16 19:11:19 -08001267 if (package_name != package->GetPackageName()) {
1268 // All packages in the same group are expected to have the same package name.
1269 break;
1270 }
1271
Ryan Mitchell80094e32020-11-16 23:08:18 +00001272 base::expected<uint32_t, NullOrIOError> resid = package->FindEntryByName(type16, entry16);
1273 if (UNLIKELY(IsIOError(resid))) {
1274 return base::unexpected(resid.error());
1275 }
1276
1277 if (!resid.has_value() && kAttr16 == type16) {
Adam Lesinski929d6512017-01-16 19:11:19 -08001278 // Private attributes in libraries (such as the framework) are sometimes encoded
1279 // under the type '^attr-private' in order to leave the ID space of public 'attr'
1280 // free for future additions. Check '^attr-private' for the same name.
1281 resid = package->FindEntryByName(kAttrPrivate16, entry16);
1282 }
1283
Ryan Mitchell80094e32020-11-16 23:08:18 +00001284 if (resid.has_value()) {
1285 return fix_package_id(*resid, package_group.dynamic_ref_table->mAssignedPackageId);
Adam Lesinski929d6512017-01-16 19:11:19 -08001286 }
1287 }
1288 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001289 return base::unexpected(std::nullopt);
Adam Lesinski0c405242017-01-13 20:47:26 -08001290}
1291
Ryan Mitchell14e8ade2021-01-11 16:01:35 -08001292void AssetManager2::RebuildFilterList() {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001293 for (PackageGroup& group : package_groups_) {
1294 for (ConfiguredPackage& impl : group.packages_) {
1295 // Destroy it.
1296 impl.filtered_configs_.~ByteBucketArray();
1297
1298 // Re-create it.
1299 new (&impl.filtered_configs_) ByteBucketArray<FilteredConfigGroup>();
1300
1301 // Create the filters here.
Ryan Mitchell14e8ade2021-01-11 16:01:35 -08001302 impl.loaded_package_->ForEachTypeSpec([&](const TypeSpec& type_spec, uint8_t type_id) {
1303 FilteredConfigGroup& group = impl.filtered_configs_.editItemAt(type_id - 1);
1304 for (const auto& type_entry : type_spec.type_entries) {
1305 if (type_entry.config.match(configuration_)) {
1306 group.type_entries.push_back(&type_entry);
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001307 }
1308 }
1309 });
1310 }
1311 }
1312}
1313
Adam Lesinski7ad11102016-10-28 16:39:15 -07001314void AssetManager2::InvalidateCaches(uint32_t diff) {
Ryan Mitchell2c4d8742019-03-04 09:41:00 -08001315 cached_bag_resid_stacks_.clear();
1316
Adam Lesinski7ad11102016-10-28 16:39:15 -07001317 if (diff == 0xffffffffu) {
1318 // Everything must go.
1319 cached_bags_.clear();
1320 return;
1321 }
1322
1323 // Be more conservative with what gets purged. Only if the bag has other possible
1324 // variations with respect to what changed (diff) should we remove it.
1325 for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) {
1326 if (diff & iter->second->type_spec_flags) {
1327 iter = cached_bags_.erase(iter);
1328 } else {
1329 ++iter;
1330 }
1331 }
Ryan Mitchella45506e2020-11-16 23:08:18 +00001332
1333 cached_resolved_values_.clear();
Adam Lesinski7ad11102016-10-28 16:39:15 -07001334}
1335
Ryan Mitchell2e394222019-08-28 12:10:51 -07001336uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) const {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001337 for (auto& package_group : package_groups_) {
1338 for (auto& package2 : package_group.packages_) {
1339 if (package2.loaded_package_ == package) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001340 return package_group.dynamic_ref_table->mAssignedPackageId;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001341 }
1342 }
1343 }
1344 return 0;
1345}
1346
Adam Lesinski30080e22017-10-16 16:18:09 -07001347std::unique_ptr<Theme> AssetManager2::NewTheme() {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001348 constexpr size_t kInitialReserveSize = 32;
1349 auto theme = std::unique_ptr<Theme>(new Theme(this));
1350 theme->entries_.reserve(kInitialReserveSize);
1351 return theme;
Adam Lesinski30080e22017-10-16 16:18:09 -07001352}
1353
1354Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) {
1355}
1356
1357Theme::~Theme() = default;
1358
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001359struct Theme::Entry {
1360 uint32_t attr_res_id;
Adam Lesinski30080e22017-10-16 16:18:09 -07001361 ApkAssetsCookie cookie;
1362 uint32_t type_spec_flags;
1363 Res_value value;
1364};
1365
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001366namespace {
1367struct ThemeEntryKeyComparer {
1368 bool operator() (const Theme::Entry& entry, uint32_t attr_res_id) const noexcept {
1369 return entry.attr_res_id < attr_res_id;
1370 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001371};
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001372} // namespace
Adam Lesinski7ad11102016-10-28 16:39:15 -07001373
Ryan Mitchell80094e32020-11-16 23:08:18 +00001374base::expected<std::monostate, NullOrIOError> Theme::ApplyStyle(uint32_t resid, bool force) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001375 ATRACE_NAME("Theme::ApplyStyle");
Adam Lesinski7ad11102016-10-28 16:39:15 -07001376
Ryan Mitchell80094e32020-11-16 23:08:18 +00001377 auto bag = asset_manager_->GetBag(resid);
1378 if (!bag.has_value()) {
1379 return base::unexpected(bag.error());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001380 }
1381
1382 // Merge the flags from this style.
Ryan Mitchell80094e32020-11-16 23:08:18 +00001383 type_spec_flags_ |= (*bag)->type_spec_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001384
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001385 for (auto it = begin(*bag); it != end(*bag); ++it) {
1386 const uint32_t attr_res_id = it->key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001387
Adam Lesinski30080e22017-10-16 16:18:09 -07001388 // If the resource ID passed in is not a style, the key can be some other identifier that is not
1389 // a resource ID. We should fail fast instead of operating with strange resource IDs.
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001390 if (!is_valid_resid(attr_res_id)) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001391 return base::unexpected(std::nullopt);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001392 }
1393
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001394 // DATA_NULL_EMPTY (@empty) is a valid resource value and DATA_NULL_UNDEFINED represents
1395 // an absence of a valid value.
1396 bool is_undefined = it->value.dataType == Res_value::TYPE_NULL &&
1397 it->value.data != Res_value::DATA_NULL_EMPTY;
1398 if (!force && is_undefined) {
1399 continue;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001400 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001401
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001402 Theme::Entry new_entry{attr_res_id, it->cookie, (*bag)->type_spec_flags, it->value};
1403 auto entry_it = std::lower_bound(entries_.begin(), entries_.end(), attr_res_id,
1404 ThemeEntryKeyComparer{});
1405 if (entry_it != entries_.end() && entry_it->attr_res_id == attr_res_id) {
1406 if (is_undefined) {
1407 // DATA_NULL_UNDEFINED clears the value of the attribute in the theme only when `force` is
1408 /// true.
1409 entries_.erase(entry_it);
1410 } else if (force) {
1411 *entry_it = new_entry;
Adam Lesinski30080e22017-10-16 16:18:09 -07001412 }
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001413 } else {
1414 entries_.insert(entry_it, new_entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001415 }
1416 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001417 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001418}
1419
Ryan Mitchell80094e32020-11-16 23:08:18 +00001420std::optional<AssetManager2::SelectedValue> Theme::GetAttribute(uint32_t resid) const {
1421
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001422 constexpr const uint32_t kMaxIterations = 20;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001423 uint32_t type_spec_flags = 0u;
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001424 for (uint32_t i = 0; i <= kMaxIterations; i++) {
1425 auto entry_it = std::lower_bound(entries_.begin(), entries_.end(), resid,
1426 ThemeEntryKeyComparer{});
1427 if (entry_it == entries_.end() || entry_it->attr_res_id != resid) {
1428 return std::nullopt;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001429 }
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001430
1431 type_spec_flags |= entry_it->type_spec_flags;
1432 if (entry_it->value.dataType == Res_value::TYPE_ATTRIBUTE) {
1433 resid = entry_it->value.data;
1434 continue;
1435 }
1436
1437 return AssetManager2::SelectedValue(entry_it->value.dataType, entry_it->value.data,
1438 entry_it->cookie, type_spec_flags, 0U /* resid */,
1439 {} /* config */);
1440 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001441 return std::nullopt;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001442}
1443
Ryan Mitchell80094e32020-11-16 23:08:18 +00001444base::expected<std::monostate, NullOrIOError> Theme::ResolveAttributeReference(
1445 AssetManager2::SelectedValue& value) const {
1446 if (value.type != Res_value::TYPE_ATTRIBUTE) {
1447 return asset_manager_->ResolveReference(value);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001448 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001449
1450 std::optional<AssetManager2::SelectedValue> result = GetAttribute(value.data);
1451 if (!result.has_value()) {
1452 return base::unexpected(std::nullopt);
1453 }
1454
Ryan Mitchella45506e2020-11-16 23:08:18 +00001455 auto resolve_result = asset_manager_->ResolveReference(*result, true /* cache_value */);
Ryan Mitchell80094e32020-11-16 23:08:18 +00001456 if (resolve_result.has_value()) {
1457 result->flags |= value.flags;
1458 value = *result;
1459 }
1460 return resolve_result;
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001461}
1462
Adam Lesinski7ad11102016-10-28 16:39:15 -07001463void Theme::Clear() {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001464 entries_.clear();
Adam Lesinski7ad11102016-10-28 16:39:15 -07001465}
1466
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001467base::expected<std::monostate, IOError> Theme::SetTo(const Theme& source) {
1468 if (this == &source) {
Ryan Mitchell80094e32020-11-16 23:08:18 +00001469 return {};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001470 }
1471
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001472 type_spec_flags_ = source.type_spec_flags_;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001473
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001474 if (asset_manager_ == source.asset_manager_) {
1475 entries_ = source.entries_;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001476 } else {
1477 std::map<ApkAssetsCookie, ApkAssetsCookie> src_to_dest_asset_cookies;
1478 typedef std::map<int, int> SourceToDestinationRuntimePackageMap;
1479 std::map<ApkAssetsCookie, SourceToDestinationRuntimePackageMap> src_asset_cookie_id_map;
1480
Ryan Mitchell93bca972019-03-08 17:26:28 -08001481 // Determine which ApkAssets are loaded in both theme AssetManagers.
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001482 const auto src_assets = source.asset_manager_->GetApkAssets();
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001483 for (size_t i = 0; i < src_assets.size(); i++) {
1484 const ApkAssets* src_asset = src_assets[i];
1485
Ryan Mitchellef538432021-03-01 14:52:14 -08001486 const auto dest_assets = asset_manager_->GetApkAssets();
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001487 for (size_t j = 0; j < dest_assets.size(); j++) {
1488 const ApkAssets* dest_asset = dest_assets[j];
Ryan Mitchellef538432021-03-01 14:52:14 -08001489 if (src_asset != dest_asset) {
1490 // ResourcesManager caches and reuses ApkAssets when the same apk must be present in
1491 // multiple AssetManagers. Two ApkAssets point to the same version of the same resources
1492 // if they are the same instance.
1493 continue;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001494 }
Ryan Mitchellef538432021-03-01 14:52:14 -08001495
1496 // Map the package ids of the asset in the source AssetManager to the package ids of the
1497 // asset in th destination AssetManager.
1498 SourceToDestinationRuntimePackageMap package_map;
1499 for (const auto& loaded_package : src_asset->GetLoadedArsc()->GetPackages()) {
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001500 const int src_package_id = source.asset_manager_->GetAssignedPackageId(
1501 loaded_package.get());
Ryan Mitchellef538432021-03-01 14:52:14 -08001502 const int dest_package_id = asset_manager_->GetAssignedPackageId(loaded_package.get());
1503 package_map[src_package_id] = dest_package_id;
1504 }
1505
1506 src_to_dest_asset_cookies.insert(std::make_pair(i, j));
1507 src_asset_cookie_id_map.insert(std::make_pair(i, package_map));
1508 break;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001509 }
1510 }
1511
Ryan Mitchell93bca972019-03-08 17:26:28 -08001512 // Reset the data in the destination theme.
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001513 entries_.clear();
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001514
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001515 for (const auto& entry : source.entries_) {
1516 bool is_reference = (entry.value.dataType == Res_value::TYPE_ATTRIBUTE
1517 || entry.value.dataType == Res_value::TYPE_REFERENCE
1518 || entry.value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE
1519 || entry.value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE)
1520 && entry.value.data != 0x0;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001521
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001522 // If the attribute value represents an attribute or reference, the package id of the
1523 // value needs to be rewritten to the package id of the value in the destination.
1524 uint32_t attribute_data = entry.value.data;
1525 if (is_reference) {
1526 // Determine the package id of the reference in the destination AssetManager.
1527 auto value_package_map = src_asset_cookie_id_map.find(entry.cookie);
1528 if (value_package_map == src_asset_cookie_id_map.end()) {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001529 continue;
1530 }
1531
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001532 auto value_dest_package = value_package_map->second.find(
1533 get_package_id(entry.value.data));
1534 if (value_dest_package == value_package_map->second.end()) {
1535 continue;
1536 }
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001537
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001538 attribute_data = fix_package_id(entry.value.data, value_dest_package->second);
1539 }
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001540
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001541 // Find the cookie of the value in the destination. If the source apk is not loaded in the
1542 // destination, only copy resources that do not reference resources in the source.
1543 ApkAssetsCookie data_dest_cookie;
1544 auto value_dest_cookie = src_to_dest_asset_cookies.find(entry.cookie);
1545 if (value_dest_cookie != src_to_dest_asset_cookies.end()) {
1546 data_dest_cookie = value_dest_cookie->second;
1547 } else {
1548 if (is_reference || entry.value.dataType == Res_value::TYPE_STRING) {
1549 continue;
1550 } else {
1551 data_dest_cookie = 0x0;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001552 }
1553 }
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001554
1555 // The package id of the attribute needs to be rewritten to the package id of the
1556 // attribute in the destination.
1557 int attribute_dest_package_id = get_package_id(entry.attr_res_id);
1558 if (attribute_dest_package_id != 0x01) {
1559 // Find the cookie of the attribute resource id in the source AssetManager
1560 base::expected<FindEntryResult, NullOrIOError> attribute_entry_result =
1561 source.asset_manager_->FindEntry(entry.attr_res_id, 0 /* density_override */ ,
1562 true /* stop_at_first_match */,
1563 true /* ignore_configuration */);
1564 if (UNLIKELY(IsIOError(attribute_entry_result))) {
1565 return base::unexpected(GetIOError(attribute_entry_result.error()));
1566 }
1567 if (!attribute_entry_result.has_value()) {
1568 continue;
1569 }
1570
1571 // Determine the package id of the attribute in the destination AssetManager.
1572 auto attribute_package_map = src_asset_cookie_id_map.find(
1573 attribute_entry_result->cookie);
1574 if (attribute_package_map == src_asset_cookie_id_map.end()) {
1575 continue;
1576 }
1577 auto attribute_dest_package = attribute_package_map->second.find(
1578 attribute_dest_package_id);
1579 if (attribute_dest_package == attribute_package_map->second.end()) {
1580 continue;
1581 }
1582 attribute_dest_package_id = attribute_dest_package->second;
1583 }
1584
1585 auto dest_attr_id = make_resid(attribute_dest_package_id, get_type_id(entry.attr_res_id),
1586 get_entry_id(entry.attr_res_id));
1587 Theme::Entry new_entry{dest_attr_id, data_dest_cookie, entry.type_spec_flags,
1588 Res_value{.dataType = entry.value.dataType,
1589 .data = attribute_data}};
1590
1591 // Since the entries were cleared, the attribute resource id has yet been mapped to any value.
1592 auto entry_it = std::lower_bound(entries_.begin(), entries_.end(), dest_attr_id,
1593 ThemeEntryKeyComparer{});
1594 entries_.insert(entry_it, new_entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001595 }
1596 }
Ryan Mitchell80094e32020-11-16 23:08:18 +00001597 return {};
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001598}
1599
1600void Theme::Dump() const {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001601 LOG(INFO) << base::StringPrintf("Theme(this=%p, AssetManager2=%p)", this, asset_manager_);
Ryan Mitchell3c6480c2021-06-07 11:22:30 -07001602 for (auto& entry : entries_) {
1603 LOG(INFO) << base::StringPrintf(" entry(0x%08x)=(0x%08x) type=(0x%02x), cookie(%d)",
1604 entry.attr_res_id, entry.value.data, entry.value.dataType,
1605 entry.cookie);
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001606 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001607}
1608
1609} // namespace android