blob: 32086625a7267d39c0858e51c7265efe10c50b46 [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 Mitchellfe50d732019-12-19 16:12:35 -080028#include "androidfw/DynamicLibManager.h"
Ryan Mitchell8a891d82019-07-01 09:48:23 -070029#include "androidfw/ResourceUtils.h"
Ryan Mitchell31b11052019-06-13 13:47:26 -070030#include "androidfw/Util.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070031#include "utils/ByteOrder.h"
32#include "utils/Trace.h"
33
34#ifdef _WIN32
35#ifdef ERROR
36#undef ERROR
37#endif
38#endif
39
40namespace android {
41
Adam Lesinskibebfcc42018-02-12 14:27:46 -080042struct FindEntryResult {
43 // A pointer to the resource table entry for this resource.
44 // If the size of the entry is > sizeof(ResTable_entry), it can be cast to
45 // a ResTable_map_entry and processed as a bag/map.
Ryan Mitchell8a891d82019-07-01 09:48:23 -070046 ResTable_entry_handle entry;
Adam Lesinskibebfcc42018-02-12 14:27:46 -080047
48 // The configuration for which the resulting entry was defined. This is already swapped to host
49 // endianness.
50 ResTable_config config;
51
52 // The bitmask of configuration axis with which the resource value varies.
53 uint32_t type_flags;
54
55 // The dynamic package ID map for the package from which this resource came from.
56 const DynamicRefTable* dynamic_ref_table;
57
Ryan Mitchell8a891d82019-07-01 09:48:23 -070058 // The package name of the resource.
59 const std::string* package_name;
60
Adam Lesinskibebfcc42018-02-12 14:27:46 -080061 // The string pool reference to the type's name. This uses a different string pool than
62 // the global string pool, but this is hidden from the caller.
63 StringPoolRef type_string_ref;
64
65 // The string pool reference to the entry's name. This uses a different string pool than
66 // the global string pool, but this is hidden from the caller.
67 StringPoolRef entry_string_ref;
68};
69
Ryan Mitchellfe50d732019-12-19 16:12:35 -080070AssetManager2::AssetManager2() : dynamic_lib_manager_(std::make_unique<DynamicLibManager>()) {
71 memset(&configuration_, 0, sizeof(configuration_));
72}
73
74AssetManager2::AssetManager2(DynamicLibManager* dynamic_lib_manager)
75 : dynamic_lib_manager_(dynamic_lib_manager) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -070076 memset(&configuration_, 0, sizeof(configuration_));
77}
Adam Lesinski7ad11102016-10-28 16:39:15 -070078
79bool AssetManager2::SetApkAssets(const std::vector<const ApkAssets*>& apk_assets,
Mårten Kongstad668ec5b2018-06-11 14:11:33 +020080 bool invalidate_caches, bool filter_incompatible_configs) {
Adam Lesinski7ad11102016-10-28 16:39:15 -070081 apk_assets_ = apk_assets;
Adam Lesinskida431a22016-12-29 16:08:16 -050082 BuildDynamicRefTable();
Mårten Kongstad668ec5b2018-06-11 14:11:33 +020083 RebuildFilterList(filter_incompatible_configs);
Adam Lesinski7ad11102016-10-28 16:39:15 -070084 if (invalidate_caches) {
85 InvalidateCaches(static_cast<uint32_t>(-1));
86 }
87 return true;
88}
89
Adam Lesinskida431a22016-12-29 16:08:16 -050090void AssetManager2::BuildDynamicRefTable() {
91 package_groups_.clear();
92 package_ids_.fill(0xff);
93
Ryan Mitchellfe50d732019-12-19 16:12:35 -080094 // Overlay resources are not directly referenced by an application so their resource ids
95 // can change throughout the application's lifetime. Assign overlay package ids last.
96 std::vector<const ApkAssets*> sorted_apk_assets(apk_assets_);
97 std::stable_partition(sorted_apk_assets.begin(), sorted_apk_assets.end(), [](const ApkAssets* a) {
98 return !a->IsOverlay();
99 });
100
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700101 std::unordered_map<std::string, uint8_t> apk_assets_package_ids;
Ryan Mitchellfe50d732019-12-19 16:12:35 -0800102 std::unordered_map<std::string, uint8_t> package_name_package_ids;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700103
Ryan Mitchellfe50d732019-12-19 16:12:35 -0800104 // Assign stable package ids to application packages.
105 uint8_t next_available_package_id = 0U;
106 for (const auto& apk_assets : sorted_apk_assets) {
107 for (const auto& package : apk_assets->GetLoadedArsc()->GetPackages()) {
108 uint8_t package_id = package->GetPackageId();
109 if (package->IsOverlay()) {
110 package_id = GetDynamicLibManager()->FindUnassignedId(next_available_package_id);
111 next_available_package_id = package_id + 1;
112 } else if (package->IsDynamic()) {
113 package_id = GetDynamicLibManager()->GetAssignedId(package->GetPackageName());
Adam Lesinskida431a22016-12-29 16:08:16 -0500114 }
115
Ryan Mitchellfe50d732019-12-19 16:12:35 -0800116 // Map the path of the apk assets to the package id of its first loaded package.
117 apk_assets_package_ids[apk_assets->GetPath()] = package_id;
118
119 // Map the package name of the package to the first loaded package with that package id.
120 package_name_package_ids[package->GetPackageName()] = package_id;
121 }
122 }
123
124 const int apk_assets_count = apk_assets_.size();
125 for (int i = 0; i < apk_assets_count; i++) {
126 const auto& apk_assets = apk_assets_[i];
127 for (const auto& package : apk_assets->GetLoadedArsc()->GetPackages()) {
128 const auto package_id_entry = package_name_package_ids.find(package->GetPackageName());
129 CHECK(package_id_entry != package_name_package_ids.end())
130 << "no package id assgined to package " << package->GetPackageName();
131 const uint8_t package_id = package_id_entry->second;
132
Adam Lesinskida431a22016-12-29 16:08:16 -0500133 // Add the mapping for package ID to index if not present.
134 uint8_t idx = package_ids_[package_id];
135 if (idx == 0xff) {
136 package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size());
137 package_groups_.push_back({});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700138
139 if (apk_assets->IsOverlay()) {
140 // The target package must precede the overlay package in the apk assets paths in order
141 // to take effect.
142 const auto& loaded_idmap = apk_assets->GetLoadedIdmap();
143 auto target_package_iter = apk_assets_package_ids.find(loaded_idmap->TargetApkPath());
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700144 if (target_package_iter == apk_assets_package_ids.end()) {
145 LOG(INFO) << "failed to find target package for overlay "
146 << loaded_idmap->OverlayApkPath();
147 } else {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700148 const uint8_t target_package_id = target_package_iter->second;
149 const uint8_t target_idx = package_ids_[target_package_id];
150 CHECK(target_idx != 0xff) << "overlay added to apk_assets_package_ids but does not"
151 << " have an assigned package group";
152
153 PackageGroup& target_package_group = package_groups_[target_idx];
154
Ryan Mitchellfe50d732019-12-19 16:12:35 -0800155 // Create a special dynamic reference table for the overlay to rewrite references to
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700156 // overlay resources as references to the target resources they overlay.
157 auto overlay_table = std::make_shared<OverlayDynamicRefTable>(
158 loaded_idmap->GetOverlayDynamicRefTable(target_package_id));
159 package_groups_.back().dynamic_ref_table = overlay_table;
160
161 // Add the overlay resource map to the target package's set of overlays.
162 target_package_group.overlays_.push_back(
163 ConfiguredOverlay{loaded_idmap->GetTargetResourcesMap(target_package_id,
164 overlay_table.get()),
165 static_cast<ApkAssetsCookie>(i)});
166 }
167 }
168
169 DynamicRefTable* ref_table = package_groups_.back().dynamic_ref_table.get();
170 ref_table->mAssignedPackageId = package_id;
171 ref_table->mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f;
Adam Lesinskida431a22016-12-29 16:08:16 -0500172 }
173 PackageGroup* package_group = &package_groups_[idx];
174
175 // Add the package and to the set of packages with the same ID.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800176 package_group->packages_.push_back(ConfiguredPackage{package.get(), {}});
Adam Lesinskida431a22016-12-29 16:08:16 -0500177 package_group->cookies_.push_back(static_cast<ApkAssetsCookie>(i));
178
179 // Add the package name -> build time ID mappings.
180 for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) {
181 String16 package_name(entry.package_name.c_str(), entry.package_name.size());
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700182 package_group->dynamic_ref_table->mEntries.replaceValueFor(
Adam Lesinskida431a22016-12-29 16:08:16 -0500183 package_name, static_cast<uint8_t>(entry.package_id));
184 }
185 }
186 }
187
188 // Now assign the runtime IDs so that we have a build-time to runtime ID map.
189 const auto package_groups_end = package_groups_.end();
190 for (auto iter = package_groups_.begin(); iter != package_groups_end; ++iter) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800191 const std::string& package_name = iter->packages_[0].loaded_package_->GetPackageName();
Adam Lesinskida431a22016-12-29 16:08:16 -0500192 for (auto iter2 = package_groups_.begin(); iter2 != package_groups_end; ++iter2) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700193 iter2->dynamic_ref_table->addMapping(String16(package_name.c_str(), package_name.size()),
194 iter->dynamic_ref_table->mAssignedPackageId);
Adam Lesinskida431a22016-12-29 16:08:16 -0500195 }
196 }
197}
198
199void AssetManager2::DumpToLog() const {
200 base::ScopedLogSeverity _log(base::INFO);
201
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800202 LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this);
203
Adam Lesinskida431a22016-12-29 16:08:16 -0500204 std::string list;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800205 for (const auto& apk_assets : apk_assets_) {
206 base::StringAppendF(&list, "%s,", apk_assets->GetPath().c_str());
207 }
208 LOG(INFO) << "ApkAssets: " << list;
209
210 list = "";
Adam Lesinskida431a22016-12-29 16:08:16 -0500211 for (size_t i = 0; i < package_ids_.size(); i++) {
212 if (package_ids_[i] != 0xff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800213 base::StringAppendF(&list, "%02x -> %d, ", (int)i, package_ids_[i]);
Adam Lesinskida431a22016-12-29 16:08:16 -0500214 }
215 }
216 LOG(INFO) << "Package ID map: " << list;
217
Adam Lesinski0dd36992018-01-25 15:38:38 -0800218 for (const auto& package_group: package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800219 list = "";
220 for (const auto& package : package_group.packages_) {
221 const LoadedPackage* loaded_package = package.loaded_package_;
222 base::StringAppendF(&list, "%s(%02x%s), ", loaded_package->GetPackageName().c_str(),
223 loaded_package->GetPackageId(),
224 (loaded_package->IsDynamic() ? " dynamic" : ""));
225 }
226 LOG(INFO) << base::StringPrintf("PG (%02x): ",
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700227 package_group.dynamic_ref_table->mAssignedPackageId)
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800228 << list;
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800229
230 for (size_t i = 0; i < 256; i++) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700231 if (package_group.dynamic_ref_table->mLookupTable[i] != 0) {
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800232 LOG(INFO) << base::StringPrintf(" e[0x%02x] -> 0x%02x", (uint8_t) i,
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700233 package_group.dynamic_ref_table->mLookupTable[i]);
Ryan Mitchell5db396d2018-11-05 15:56:15 -0800234 }
235 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500236 }
237}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700238
239const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const {
240 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
241 return nullptr;
242 }
243 return apk_assets_[cookie]->GetLoadedArsc()->GetStringPool();
244}
245
Adam Lesinskida431a22016-12-29 16:08:16 -0500246const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const {
247 if (package_id >= package_ids_.size()) {
248 return nullptr;
249 }
250
251 const size_t idx = package_ids_[package_id];
252 if (idx == 0xff) {
253 return nullptr;
254 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700255 return package_groups_[idx].dynamic_ref_table.get();
Adam Lesinskida431a22016-12-29 16:08:16 -0500256}
257
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700258std::shared_ptr<const DynamicRefTable> AssetManager2::GetDynamicRefTableForCookie(
259 ApkAssetsCookie cookie) const {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800260 for (const PackageGroup& package_group : package_groups_) {
261 for (const ApkAssetsCookie& package_cookie : package_group.cookies_) {
262 if (package_cookie == cookie) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700263 return package_group.dynamic_ref_table;
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800264 }
265 }
266 }
267 return nullptr;
268}
269
Mårten Kongstadc92c4dd2019-02-05 01:29:59 +0100270const std::unordered_map<std::string, std::string>*
271 AssetManager2::GetOverlayableMapForPackage(uint32_t package_id) const {
272
273 if (package_id >= package_ids_.size()) {
274 return nullptr;
275 }
276
277 const size_t idx = package_ids_[package_id];
278 if (idx == 0xff) {
279 return nullptr;
280 }
281
282 const PackageGroup& package_group = package_groups_[idx];
283 if (package_group.packages_.size() == 0) {
284 return nullptr;
285 }
286
287 const auto loaded_package = package_group.packages_[0].loaded_package_;
288 return &loaded_package->GetOverlayableMap();
289}
290
Ryan Mitchell2e394222019-08-28 12:10:51 -0700291bool AssetManager2::GetOverlayablesToString(const android::StringPiece& package_name,
292 std::string* out) const {
293 uint8_t package_id = 0U;
294 for (const auto& apk_assets : apk_assets_) {
295 const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc();
296 if (loaded_arsc == nullptr) {
297 continue;
298 }
299
300 const auto& loaded_packages = loaded_arsc->GetPackages();
301 if (loaded_packages.empty()) {
302 continue;
303 }
304
305 const auto& loaded_package = loaded_packages[0];
306 if (loaded_package->GetPackageName() == package_name) {
307 package_id = GetAssignedPackageId(loaded_package.get());
308 break;
309 }
310 }
311
312 if (package_id == 0U) {
313 ANDROID_LOG(ERROR) << base::StringPrintf("No package with name '%s", package_name.data());
314 return false;
315 }
316
317 const size_t idx = package_ids_[package_id];
318 if (idx == 0xff) {
319 return false;
320 }
321
322 std::string output;
323 for (const ConfiguredPackage& package : package_groups_[idx].packages_) {
324 const LoadedPackage* loaded_package = package.loaded_package_;
325 for (auto it = loaded_package->begin(); it != loaded_package->end(); it++) {
326 const OverlayableInfo* info = loaded_package->GetOverlayableInfo(*it);
327 if (info != nullptr) {
328 ResourceName res_name;
329 if (!GetResourceName(*it, &res_name)) {
330 ANDROID_LOG(ERROR) << base::StringPrintf(
331 "Unable to retrieve name of overlayable resource 0x%08x", *it);
332 return false;
333 }
334
335 const std::string name = ToFormattedResourceString(&res_name);
336 output.append(base::StringPrintf(
337 "resource='%s' overlayable='%s' actor='%s' policy='0x%08x'\n",
338 name.c_str(), info->name.c_str(), info->actor.c_str(), info->policy_flags));
339 }
340 }
341 }
342
343 *out = std::move(output);
344 return true;
345}
346
Adam Lesinski7ad11102016-10-28 16:39:15 -0700347void AssetManager2::SetConfiguration(const ResTable_config& configuration) {
348 const int diff = configuration_.diff(configuration);
349 configuration_ = configuration;
350
351 if (diff) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800352 RebuildFilterList();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700353 InvalidateCaches(static_cast<uint32_t>(diff));
354 }
355}
356
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700357std::set<std::string> AssetManager2::GetNonSystemOverlayPaths() const {
358 std::set<std::string> non_system_overlays;
Adam Lesinski0c405242017-01-13 20:47:26 -0800359 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800360 bool found_system_package = false;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800361 for (const ConfiguredPackage& package : package_group.packages_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700362 if (package.loaded_package_->IsSystem()) {
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800363 found_system_package = true;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700364 break;
365 }
366 }
367
368 if (!found_system_package) {
369 for (const ConfiguredOverlay& overlay : package_group.overlays_) {
370 non_system_overlays.insert(apk_assets_[overlay.cookie]->GetPath());
371 }
372 }
373 }
374
375 return non_system_overlays;
376}
377
378std::set<ResTable_config> AssetManager2::GetResourceConfigurations(bool exclude_system,
379 bool exclude_mipmap) const {
380 ATRACE_NAME("AssetManager::GetResourceConfigurations");
381 const auto non_system_overlays =
382 (exclude_system) ? GetNonSystemOverlayPaths() : std::set<std::string>();
383
384 std::set<ResTable_config> configurations;
385 for (const PackageGroup& package_group : package_groups_) {
386 for (size_t i = 0; i < package_group.packages_.size(); i++) {
387 const ConfiguredPackage& package = package_group.packages_[i];
388 if (exclude_system && package.loaded_package_->IsSystem()) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800389 continue;
390 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800391
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700392 auto apk_assets = apk_assets_[package_group.cookies_[i]];
393 if (exclude_system && apk_assets->IsOverlay()
394 && non_system_overlays.find(apk_assets->GetPath()) == non_system_overlays.end()) {
395 // Exclude overlays that target system resources.
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800396 continue;
397 }
398
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800399 package.loaded_package_->CollectConfigurations(exclude_mipmap, &configurations);
Adam Lesinski0c405242017-01-13 20:47:26 -0800400 }
401 }
402 return configurations;
403}
404
405std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800406 bool merge_equivalent_languages) const {
407 ATRACE_NAME("AssetManager::GetResourceLocales");
Adam Lesinski0c405242017-01-13 20:47:26 -0800408 std::set<std::string> locales;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700409 const auto non_system_overlays =
410 (exclude_system) ? GetNonSystemOverlayPaths() : std::set<std::string>();
411
Adam Lesinski0c405242017-01-13 20:47:26 -0800412 for (const PackageGroup& package_group : package_groups_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700413 for (size_t i = 0; i < package_group.packages_.size(); i++) {
414 const ConfiguredPackage& package = package_group.packages_[i];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800415 if (exclude_system && package.loaded_package_->IsSystem()) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800416 continue;
417 }
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800418
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700419 auto apk_assets = apk_assets_[package_group.cookies_[i]];
420 if (exclude_system && apk_assets->IsOverlay()
421 && non_system_overlays.find(apk_assets->GetPath()) == non_system_overlays.end()) {
422 // Exclude overlays that target system resources.
Ryan Mitchell449a54f2018-11-30 15:22:31 -0800423 continue;
424 }
425
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800426 package.loaded_package_->CollectLocales(merge_equivalent_languages, &locales);
Adam Lesinski0c405242017-01-13 20:47:26 -0800427 }
428 }
429 return locales;
430}
431
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800432std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename,
433 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700434 const std::string new_path = "assets/" + filename;
435 return OpenNonAsset(new_path, mode);
436}
437
438std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800439 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700440 const std::string new_path = "assets/" + filename;
441 return OpenNonAsset(new_path, cookie, mode);
442}
443
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800444std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) const {
445 ATRACE_NAME("AssetManager::OpenDir");
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800446
447 std::string full_path = "assets/" + dirname;
448 std::unique_ptr<SortedVector<AssetDir::FileInfo>> files =
449 util::make_unique<SortedVector<AssetDir::FileInfo>>();
450
451 // Start from the back.
452 for (auto iter = apk_assets_.rbegin(); iter != apk_assets_.rend(); ++iter) {
453 const ApkAssets* apk_assets = *iter;
Mårten Kongstaddbf343b2019-02-21 07:54:18 +0100454 if (apk_assets->IsOverlay()) {
455 continue;
456 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800457
458 auto func = [&](const StringPiece& name, FileType type) {
459 AssetDir::FileInfo info;
460 info.setFileName(String8(name.data(), name.size()));
461 info.setFileType(type);
462 info.setSourceName(String8(apk_assets->GetPath().c_str()));
463 files->add(info);
464 };
465
466 if (!apk_assets->ForEachFile(full_path, func)) {
467 return {};
468 }
469 }
470
471 std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>();
472 asset_dir->setFileList(files.release());
473 return asset_dir;
474}
475
Adam Lesinski7ad11102016-10-28 16:39:15 -0700476// Search in reverse because that's how we used to do it and we need to preserve behaviour.
477// This is unfortunate, because ClassLoaders delegate to the parent first, so the order
478// is inconsistent for split APKs.
479std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
480 Asset::AccessMode mode,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800481 ApkAssetsCookie* out_cookie) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700482 for (int32_t i = apk_assets_.size() - 1; i >= 0; i--) {
Mårten Kongstaddbf343b2019-02-21 07:54:18 +0100483 // Prevent RRO from modifying assets and other entries accessed by file
484 // path. Explicitly asking for a path in a given package (denoted by a
485 // cookie) is still OK.
486 if (apk_assets_[i]->IsOverlay()) {
487 continue;
488 }
489
Adam Lesinski7ad11102016-10-28 16:39:15 -0700490 std::unique_ptr<Asset> asset = apk_assets_[i]->Open(filename, mode);
491 if (asset) {
492 if (out_cookie != nullptr) {
493 *out_cookie = i;
494 }
495 return asset;
496 }
497 }
498
499 if (out_cookie != nullptr) {
500 *out_cookie = kInvalidCookie;
501 }
502 return {};
503}
504
505std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800506 ApkAssetsCookie cookie,
507 Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700508 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
509 return {};
510 }
511 return apk_assets_[cookie]->Open(filename, mode);
512}
513
514ApkAssetsCookie AssetManager2::FindEntry(uint32_t resid, uint16_t density_override,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800515 bool /*stop_at_first_match*/,
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800516 bool ignore_configuration,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800517 FindEntryResult* out_entry) const {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700518 if (resource_resolution_logging_enabled_) {
519 // Clear the last logged resource resolution.
520 ResetResourceResolution();
521 last_resolution_.resid = resid;
522 }
523
Adam Lesinski7ad11102016-10-28 16:39:15 -0700524 // Might use this if density_override != 0.
525 ResTable_config density_override_config;
526
527 // Select our configuration or generate a density override configuration.
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800528 const ResTable_config* desired_config = &configuration_;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700529 if (density_override != 0 && density_override != configuration_.density) {
530 density_override_config = configuration_;
531 density_override_config.density = density_override;
532 desired_config = &density_override_config;
533 }
534
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700535 // Retrieve the package group from the package id of the resource id.
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800536 if (!is_valid_resid(resid)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500537 LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid);
538 return kInvalidCookie;
539 }
540
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800541 const uint32_t package_id = get_package_id(resid);
542 const uint8_t type_idx = get_type_id(resid) - 1;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800543 const uint16_t entry_idx = get_entry_id(resid);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700544 uint8_t package_idx = package_ids_[package_id];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800545 if (package_idx == 0xff) {
Ryan Mitchell2fe23472019-02-27 09:43:01 -0800546 ANDROID_LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.",
547 package_id, resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500548 return kInvalidCookie;
549 }
550
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800551 const PackageGroup& package_group = package_groups_[package_idx];
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700552 ApkAssetsCookie cookie = FindEntryInternal(package_group, type_idx, entry_idx, *desired_config,
553 false /* stop_at_first_match */,
554 ignore_configuration, out_entry);
555 if (UNLIKELY(cookie == kInvalidCookie)) {
556 return kInvalidCookie;
557 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800558
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700559 if (!apk_assets_[cookie]->IsLoader()) {
560 for (const auto& id_map : package_group.overlays_) {
561 auto overlay_entry = id_map.overlay_res_maps_.Lookup(resid);
562 if (!overlay_entry) {
563 // No id map entry exists for this target resource.
564 continue;
565 }
566
567 if (overlay_entry.IsTableEntry()) {
568 // The target resource is overlaid by an inline value not represented by a resource.
569 out_entry->entry = overlay_entry.GetTableEntry();
570 out_entry->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
571 cookie = id_map.cookie;
572 continue;
573 }
574
575 FindEntryResult overlay_result;
576 ApkAssetsCookie overlay_cookie = FindEntry(overlay_entry.GetResourceId(), density_override,
577 false /* stop_at_first_match */,
578 ignore_configuration, &overlay_result);
579 if (UNLIKELY(overlay_cookie == kInvalidCookie)) {
580 continue;
581 }
582
583 if (!overlay_result.config.isBetterThan(out_entry->config, desired_config)
584 && overlay_result.config.compare(out_entry->config) != 0) {
585 // The configuration of the entry for the overlay must be equal to or better than the target
586 // configuration to be chosen as the better value.
587 continue;
588 }
589
590 cookie = overlay_cookie;
591 out_entry->entry = std::move(overlay_result.entry);
592 out_entry->config = overlay_result.config;
593 out_entry->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
594 if (resource_resolution_logging_enabled_) {
595 last_resolution_.steps.push_back(
596 Resolution::Step{Resolution::Step::Type::OVERLAID, overlay_result.config.toString(),
Ryan Mitchellee4a5642019-10-16 08:32:55 -0700597 overlay_result.package_name});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700598 }
599 }
600 }
601
602 if (resource_resolution_logging_enabled_) {
603 last_resolution_.cookie = cookie;
604 last_resolution_.type_string_ref = out_entry->type_string_ref;
605 last_resolution_.entry_string_ref = out_entry->entry_string_ref;
606 }
607
608 return cookie;
609}
610
611ApkAssetsCookie AssetManager2::FindEntryInternal(const PackageGroup& package_group,
612 uint8_t type_idx, uint16_t entry_idx,
613 const ResTable_config& desired_config,
614 bool /*stop_at_first_match*/,
615 bool ignore_configuration,
616 FindEntryResult* out_entry) const {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800617 ApkAssetsCookie best_cookie = kInvalidCookie;
618 const LoadedPackage* best_package = nullptr;
619 const ResTable_type* best_type = nullptr;
620 const ResTable_config* best_config = nullptr;
621 ResTable_config best_config_copy;
622 uint32_t best_offset = 0u;
623 uint32_t type_flags = 0u;
624
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700625 Resolution::Step::Type resolution_type = Resolution::Step::Type::NO_ENTRY;
Winson2f3669b2019-01-11 11:28:34 -0800626 std::vector<Resolution::Step> resolution_steps;
627
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800628 // If desired_config is the same as the set configuration, then we can use our filtered list
629 // and we don't need to match the configurations, since they already matched.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700630 const bool use_fast_path = !ignore_configuration && &desired_config == &configuration_;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800631
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700632 const size_t package_count = package_group.packages_.size();
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800633 for (size_t pi = 0; pi < package_count; pi++) {
634 const ConfiguredPackage& loaded_package_impl = package_group.packages_[pi];
635 const LoadedPackage* loaded_package = loaded_package_impl.loaded_package_;
636 ApkAssetsCookie cookie = package_group.cookies_[pi];
637
638 // If the type IDs are offset in this package, we need to take that into account when searching
639 // for a type.
640 const TypeSpec* type_spec = loaded_package->GetTypeSpecByTypeIndex(type_idx);
641 if (UNLIKELY(type_spec == nullptr)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700642 continue;
643 }
644
Winson9947f1e2019-08-16 10:20:39 -0700645 // If the package is an overlay or custom loader,
646 // then even configurations that are the same MUST be chosen.
Winson9947f1e2019-08-16 10:20:39 -0700647 const bool package_is_loader = loaded_package->IsCustomLoader();
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700648 type_flags |= type_spec->GetFlagsForEntryIndex(entry_idx);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800649
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800650 if (use_fast_path) {
Winson2f3669b2019-01-11 11:28:34 -0800651 const FilteredConfigGroup& filtered_group = loaded_package_impl.filtered_configs_[type_idx];
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800652 const std::vector<ResTable_config>& candidate_configs = filtered_group.configurations;
653 const size_t type_count = candidate_configs.size();
654 for (uint32_t i = 0; i < type_count; i++) {
655 const ResTable_config& this_config = candidate_configs[i];
656
657 // We can skip calling ResTable_config::match() because we know that all candidate
658 // configurations that do NOT match have been filtered-out.
Winson2f3669b2019-01-11 11:28:34 -0800659 if (best_config == nullptr) {
660 resolution_type = Resolution::Step::Type::INITIAL;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700661 } else if (this_config.isBetterThan(*best_config, &desired_config)) {
662 resolution_type = (package_is_loader) ? Resolution::Step::Type::BETTER_MATCH_LOADER
663 : Resolution::Step::Type::BETTER_MATCH;
664 } else if (package_is_loader && this_config.compare(*best_config) == 0) {
665 resolution_type = Resolution::Step::Type::OVERLAID_LOADER;
Winson2f3669b2019-01-11 11:28:34 -0800666 } else {
Winson9947f1e2019-08-16 10:20:39 -0700667 if (resource_resolution_logging_enabled_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700668 resolution_type = (package_is_loader) ? Resolution::Step::Type::SKIPPED_LOADER
669 : Resolution::Step::Type::SKIPPED;
Winson9947f1e2019-08-16 10:20:39 -0700670 resolution_steps.push_back(Resolution::Step{resolution_type,
671 this_config.toString(),
672 &loaded_package->GetPackageName()});
673 }
Winson2f3669b2019-01-11 11:28:34 -0800674 continue;
675 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800676
Winson2f3669b2019-01-11 11:28:34 -0800677 // The configuration matches and is better than the previous selection.
678 // Find the entry value if it exists for this configuration.
679 const ResTable_type* type = filtered_group.types[i];
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700680 const uint32_t offset = LoadedPackage::GetEntryOffset(type, entry_idx);
Winson2f3669b2019-01-11 11:28:34 -0800681 if (offset == ResTable_type::NO_ENTRY) {
Winson9947f1e2019-08-16 10:20:39 -0700682 if (resource_resolution_logging_enabled_) {
683 if (package_is_loader) {
684 resolution_type = Resolution::Step::Type::NO_ENTRY_LOADER;
685 } else {
686 resolution_type = Resolution::Step::Type::NO_ENTRY;
687 }
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700688 resolution_steps.push_back(Resolution::Step{resolution_type,
Winson9947f1e2019-08-16 10:20:39 -0700689 this_config.toString(),
690 &loaded_package->GetPackageName()});
691 }
Winson2f3669b2019-01-11 11:28:34 -0800692 continue;
693 }
694
695 best_cookie = cookie;
696 best_package = loaded_package;
697 best_type = type;
698 best_config = &this_config;
699 best_offset = offset;
700
701 if (resource_resolution_logging_enabled_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700702 last_resolution_.steps.push_back(Resolution::Step{resolution_type,
703 this_config.toString(),
704 &loaded_package->GetPackageName()});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800705 }
706 }
707 } else {
708 // This is the slower path, which doesn't use the filtered list of configurations.
709 // Here we must read the ResTable_config from the mmapped APK, convert it to host endianness
710 // and fill in any new fields that did not exist when the APK was compiled.
711 // Furthermore when selecting configurations we can't just record the pointer to the
712 // ResTable_config, we must copy it.
713 const auto iter_end = type_spec->types + type_spec->type_count;
714 for (auto iter = type_spec->types; iter != iter_end; ++iter) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800715 ResTable_config this_config{};
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800716
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800717 if (!ignore_configuration) {
718 this_config.copyFromDtoH((*iter)->config);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700719 if (!this_config.match(desired_config)) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800720 continue;
721 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800722
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800723 if (best_config == nullptr) {
724 resolution_type = Resolution::Step::Type::INITIAL;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700725 } else if (this_config.isBetterThan(*best_config, &desired_config)) {
726 resolution_type = (package_is_loader) ? Resolution::Step::Type::BETTER_MATCH_LOADER
727 : Resolution::Step::Type::BETTER_MATCH;
728 } else if (package_is_loader && this_config.compare(*best_config) == 0) {
729 resolution_type = Resolution::Step::Type::OVERLAID_LOADER;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800730 } else {
731 continue;
732 }
Winson2f3669b2019-01-11 11:28:34 -0800733 }
734
735 // The configuration matches and is better than the previous selection.
736 // Find the entry value if it exists for this configuration.
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700737 const uint32_t offset = LoadedPackage::GetEntryOffset(*iter, entry_idx);
Winson2f3669b2019-01-11 11:28:34 -0800738 if (offset == ResTable_type::NO_ENTRY) {
739 continue;
740 }
741
742 best_cookie = cookie;
743 best_package = loaded_package;
744 best_type = *iter;
745 best_config_copy = this_config;
746 best_config = &best_config_copy;
747 best_offset = offset;
748
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800749 if (ignore_configuration) {
750 // Any configuration will suffice, so break.
751 break;
752 }
753
Winson2f3669b2019-01-11 11:28:34 -0800754 if (resource_resolution_logging_enabled_) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700755 last_resolution_.steps.push_back(Resolution::Step{resolution_type,
756 this_config.toString(),
757 &loaded_package->GetPackageName()});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800758 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700759 }
760 }
761 }
762
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800763 if (UNLIKELY(best_cookie == kInvalidCookie)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700764 return kInvalidCookie;
765 }
766
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800767 const ResTable_entry* best_entry = LoadedPackage::GetEntryFromOffset(best_type, best_offset);
768 if (UNLIKELY(best_entry == nullptr)) {
769 return kInvalidCookie;
770 }
771
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700772 out_entry->entry = ResTable_entry_handle::unmanaged(best_entry);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800773 out_entry->config = *best_config;
774 out_entry->type_flags = type_flags;
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700775 out_entry->package_name = &best_package->GetPackageName();
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800776 out_entry->type_string_ref = StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1);
777 out_entry->entry_string_ref =
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700778 StringPoolRef(best_package->GetKeyStringPool(), best_entry->key.index);
779 out_entry->dynamic_ref_table = package_group.dynamic_ref_table.get();
Winson2f3669b2019-01-11 11:28:34 -0800780
Adam Lesinskida431a22016-12-29 16:08:16 -0500781 return best_cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700782}
783
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700784void AssetManager2::ResetResourceResolution() const {
785 last_resolution_.cookie = kInvalidCookie;
786 last_resolution_.resid = 0;
787 last_resolution_.steps.clear();
788 last_resolution_.type_string_ref = StringPoolRef();
789 last_resolution_.entry_string_ref = StringPoolRef();
790}
791
Winson2f3669b2019-01-11 11:28:34 -0800792void AssetManager2::SetResourceResolutionLoggingEnabled(bool enabled) {
793 resource_resolution_logging_enabled_ = enabled;
Winson2f3669b2019-01-11 11:28:34 -0800794 if (!enabled) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700795 ResetResourceResolution();
Winson2f3669b2019-01-11 11:28:34 -0800796 }
797}
798
799std::string AssetManager2::GetLastResourceResolution() const {
800 if (!resource_resolution_logging_enabled_) {
801 LOG(ERROR) << "Must enable resource resolution logging before getting path.";
802 return std::string();
803 }
804
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700805 auto cookie = last_resolution_.cookie;
Winson2f3669b2019-01-11 11:28:34 -0800806 if (cookie == kInvalidCookie) {
807 LOG(ERROR) << "AssetManager hasn't resolved a resource to read resolution path.";
808 return std::string();
809 }
810
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700811 uint32_t resid = last_resolution_.resid;
812 std::vector<Resolution::Step>& steps = last_resolution_.steps;
Winson2f3669b2019-01-11 11:28:34 -0800813
814 ResourceName resource_name;
815 std::string resource_name_string;
816
817 const LoadedPackage* package =
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700818 apk_assets_[cookie]->GetLoadedArsc()->GetPackageById(get_package_id(resid));
Winson2f3669b2019-01-11 11:28:34 -0800819
820 if (package != nullptr) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700821 ToResourceName(last_resolution_.type_string_ref,
822 last_resolution_.entry_string_ref,
Ryan Mitchell741e96f2019-01-23 16:56:51 -0800823 package->GetPackageName(),
Winson2f3669b2019-01-11 11:28:34 -0800824 &resource_name);
825 resource_name_string = ToFormattedResourceString(&resource_name);
826 }
827
828 std::stringstream log_stream;
829 log_stream << base::StringPrintf("Resolution for 0x%08x ", resid)
830 << resource_name_string
831 << "\n\tFor config -"
832 << configuration_.toString();
833
834 std::string prefix;
835 for (Resolution::Step step : steps) {
836 switch (step.type) {
837 case Resolution::Step::Type::INITIAL:
838 prefix = "Found initial";
839 break;
840 case Resolution::Step::Type::BETTER_MATCH:
841 prefix = "Found better";
842 break;
Winson9947f1e2019-08-16 10:20:39 -0700843 case Resolution::Step::Type::BETTER_MATCH_LOADER:
844 prefix = "Found better in loader";
845 break;
Winson2f3669b2019-01-11 11:28:34 -0800846 case Resolution::Step::Type::OVERLAID:
847 prefix = "Overlaid";
848 break;
Winson9947f1e2019-08-16 10:20:39 -0700849 case Resolution::Step::Type::OVERLAID_LOADER:
850 prefix = "Overlaid by loader";
851 break;
852 case Resolution::Step::Type::SKIPPED:
853 prefix = "Skipped";
854 break;
855 case Resolution::Step::Type::SKIPPED_LOADER:
856 prefix = "Skipped loader";
857 break;
858 case Resolution::Step::Type::NO_ENTRY:
859 prefix = "No entry";
860 break;
861 case Resolution::Step::Type::NO_ENTRY_LOADER:
862 prefix = "No entry for loader";
863 break;
Winson2f3669b2019-01-11 11:28:34 -0800864 }
865
866 if (!prefix.empty()) {
867 log_stream << "\n\t" << prefix << ": " << *step.package_name;
868
869 if (!step.config_name.isEmpty()) {
870 log_stream << " -" << step.config_name;
871 }
872 }
873 }
874
875 return log_stream.str();
876}
877
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800878bool AssetManager2::GetResourceName(uint32_t resid, ResourceName* out_name) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700879 FindEntryResult entry;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800880 ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */,
881 true /* stop_at_first_match */,
882 true /* ignore_configuration */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700883 if (cookie == kInvalidCookie) {
884 return false;
885 }
886
Winson2f3669b2019-01-11 11:28:34 -0800887 return ToResourceName(entry.type_string_ref,
888 entry.entry_string_ref,
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700889 *entry.package_name,
Winson2f3669b2019-01-11 11:28:34 -0800890 out_name);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700891}
892
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800893bool AssetManager2::GetResourceFlags(uint32_t resid, uint32_t* out_flags) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700894 FindEntryResult entry;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800895 ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */,
896 false /* stop_at_first_match */,
897 true /* ignore_configuration */, &entry);
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700898 if (cookie != kInvalidCookie) {
899 *out_flags = entry.type_flags;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800900 return true;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700901 }
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800902 return false;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700903}
904
905ApkAssetsCookie AssetManager2::GetResource(uint32_t resid, bool may_be_bag,
906 uint16_t density_override, Res_value* out_value,
907 ResTable_config* out_selected_config,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800908 uint32_t* out_flags) const {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700909 FindEntryResult entry;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800910 ApkAssetsCookie cookie = FindEntry(resid, density_override, false /* stop_at_first_match */,
911 false /* ignore_configuration */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700912 if (cookie == kInvalidCookie) {
913 return kInvalidCookie;
914 }
915
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700916 const ResTable_entry* table_entry = *entry.entry;
917 if (dtohs(table_entry->flags) & ResTable_entry::FLAG_COMPLEX) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700918 if (!may_be_bag) {
919 LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid);
Adam Lesinski0c405242017-01-13 20:47:26 -0800920 return kInvalidCookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700921 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800922
923 // Create a reference since we can't represent this complex type as a Res_value.
924 out_value->dataType = Res_value::TYPE_REFERENCE;
925 out_value->data = resid;
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800926 *out_selected_config = entry.config;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700927 *out_flags = entry.type_flags;
Adam Lesinski0c405242017-01-13 20:47:26 -0800928 return cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700929 }
930
931 const Res_value* device_value = reinterpret_cast<const Res_value*>(
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700932 reinterpret_cast<const uint8_t*>(table_entry) + dtohs(table_entry->size));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700933 out_value->copyFrom_dtoh(*device_value);
Adam Lesinskida431a22016-12-29 16:08:16 -0500934
935 // Convert the package ID to the runtime assigned package ID.
936 entry.dynamic_ref_table->lookupResourceValue(out_value);
937
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800938 *out_selected_config = entry.config;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700939 *out_flags = entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700940 return cookie;
941}
942
Adam Lesinski0c405242017-01-13 20:47:26 -0800943ApkAssetsCookie AssetManager2::ResolveReference(ApkAssetsCookie cookie, Res_value* in_out_value,
944 ResTable_config* in_out_selected_config,
945 uint32_t* in_out_flags,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800946 uint32_t* out_last_reference) const {
Adam Lesinski0c405242017-01-13 20:47:26 -0800947 constexpr const int kMaxIterations = 20;
948
Adam Lesinski0c405242017-01-13 20:47:26 -0800949 for (size_t iteration = 0u; in_out_value->dataType == Res_value::TYPE_REFERENCE &&
950 in_out_value->data != 0u && iteration < kMaxIterations;
951 iteration++) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800952 *out_last_reference = in_out_value->data;
Adam Lesinski0c405242017-01-13 20:47:26 -0800953 uint32_t new_flags = 0u;
954 cookie = GetResource(in_out_value->data, true /*may_be_bag*/, 0u /*density_override*/,
955 in_out_value, in_out_selected_config, &new_flags);
956 if (cookie == kInvalidCookie) {
957 return kInvalidCookie;
958 }
959 if (in_out_flags != nullptr) {
960 *in_out_flags |= new_flags;
961 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800962 if (*out_last_reference == in_out_value->data) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800963 // This reference can't be resolved, so exit now and let the caller deal with it.
964 return cookie;
965 }
966 }
967 return cookie;
968}
969
Aurimas Liutikas8f004c82019-01-17 17:20:10 -0800970const std::vector<uint32_t> AssetManager2::GetBagResIdStack(uint32_t resid) {
971 auto cached_iter = cached_bag_resid_stacks_.find(resid);
972 if (cached_iter != cached_bag_resid_stacks_.end()) {
973 return cached_iter->second;
974 } else {
975 auto found_resids = std::vector<uint32_t>();
976 GetBag(resid, found_resids);
977 // Cache style stacks if they are not already cached.
978 cached_bag_resid_stacks_[resid] = found_resids;
979 return found_resids;
980 }
981}
982
Adam Lesinski7ad11102016-10-28 16:39:15 -0700983const ResolvedBag* AssetManager2::GetBag(uint32_t resid) {
y57cd1952018-04-12 14:26:23 -0700984 auto found_resids = std::vector<uint32_t>();
Aurimas Liutikas8f004c82019-01-17 17:20:10 -0800985 auto bag = GetBag(resid, found_resids);
986
987 // Cache style stacks if they are not already cached.
988 auto cached_iter = cached_bag_resid_stacks_.find(resid);
989 if (cached_iter == cached_bag_resid_stacks_.end()) {
990 cached_bag_resid_stacks_[resid] = found_resids;
991 }
992 return bag;
y57cd1952018-04-12 14:26:23 -0700993}
994
Ryan Mitchell155d5392020-02-10 13:35:24 -0800995static bool compare_bag_entries(const ResolvedBag::Entry& entry1,
996 const ResolvedBag::Entry& entry2) {
997 return entry1.key < entry2.key;
998}
999
y57cd1952018-04-12 14:26:23 -07001000const ResolvedBag* AssetManager2::GetBag(uint32_t resid, std::vector<uint32_t>& child_resids) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001001 auto cached_iter = cached_bags_.find(resid);
1002 if (cached_iter != cached_bags_.end()) {
1003 return cached_iter->second.get();
1004 }
1005
Adam Lesinski1a1e9c22017-10-13 15:45:34 -07001006 FindEntryResult entry;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -08001007 ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */,
1008 false /* stop_at_first_match */,
1009 false /* ignore_configuration */,
1010 &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001011 if (cookie == kInvalidCookie) {
1012 return nullptr;
1013 }
1014
1015 // Check that the size of the entry header is at least as big as
1016 // the desired ResTable_map_entry. Also verify that the entry
1017 // was intended to be a map.
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001018 const ResTable_entry* table_entry = *entry.entry;
1019 if (dtohs(table_entry->size) < sizeof(ResTable_map_entry) ||
1020 (dtohs(table_entry->flags) & ResTable_entry::FLAG_COMPLEX) == 0) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001021 // Not a bag, nothing to do.
1022 return nullptr;
1023 }
1024
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001025 const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(table_entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001026 const ResTable_map* map_entry =
1027 reinterpret_cast<const ResTable_map*>(reinterpret_cast<const uint8_t*>(map) + map->size);
1028 const ResTable_map* const map_entry_end = map_entry + dtohl(map->count);
1029
y57cd1952018-04-12 14:26:23 -07001030 // Keep track of ids that have already been seen to prevent infinite loops caused by circular
1031 // dependencies between bags
1032 child_resids.push_back(resid);
1033
Adam Lesinskida431a22016-12-29 16:08:16 -05001034 uint32_t parent_resid = dtohl(map->parent.ident);
Ryan Mitchell155d5392020-02-10 13:35:24 -08001035 if (parent_resid == 0U || std::find(child_resids.begin(), child_resids.end(), parent_resid)
y57cd1952018-04-12 14:26:23 -07001036 != child_resids.end()) {
Ryan Mitchell155d5392020-02-10 13:35:24 -08001037 // There is no parent or a circular dependency exist, meaning there is nothing to inherit and
1038 // we can do a simple copy of the entries in the map.
Adam Lesinski7ad11102016-10-28 16:39:15 -07001039 const size_t entry_count = map_entry_end - map_entry;
1040 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
1041 malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))};
Ryan Mitchell155d5392020-02-10 13:35:24 -08001042
1043 bool sort_entries = false;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001044 ResolvedBag::Entry* new_entry = new_bag->entries;
1045 for (; map_entry != map_entry_end; ++map_entry) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001046 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001047 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001048 // Attributes, arrays, etc don't have a resource id as the name. They specify
1049 // other data, which would be wrong to change via a lookup.
1050 if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001051 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
1052 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -05001053 return nullptr;
1054 }
1055 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001056 new_entry->cookie = cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001057 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001058 new_entry->key_pool = nullptr;
1059 new_entry->type_pool = nullptr;
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001060 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -07001061 new_entry->value.copyFrom_dtoh(map_entry->value);
1062 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
1063 if (err != NO_ERROR) {
1064 LOG(ERROR) << base::StringPrintf(
1065 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
1066 new_entry->value.data, new_key);
1067 return nullptr;
1068 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001069 sort_entries = sort_entries ||
1070 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001071 ++new_entry;
1072 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001073
1074 if (sort_entries) {
1075 std::sort(new_bag->entries, new_bag->entries + entry_count, compare_bag_entries);
1076 }
1077
Adam Lesinski1a1e9c22017-10-13 15:45:34 -07001078 new_bag->type_spec_flags = entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001079 new_bag->entry_count = static_cast<uint32_t>(entry_count);
1080 ResolvedBag* result = new_bag.get();
1081 cached_bags_[resid] = std::move(new_bag);
1082 return result;
1083 }
1084
Adam Lesinskida431a22016-12-29 16:08:16 -05001085 // In case the parent is a dynamic reference, resolve it.
1086 entry.dynamic_ref_table->lookupResourceId(&parent_resid);
1087
Adam Lesinski7ad11102016-10-28 16:39:15 -07001088 // Get the parent and do a merge of the keys.
y57cd1952018-04-12 14:26:23 -07001089 const ResolvedBag* parent_bag = GetBag(parent_resid, child_resids);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001090 if (parent_bag == nullptr) {
1091 // Failed to get the parent that should exist.
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001092 LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid,
1093 resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001094 return nullptr;
1095 }
1096
Adam Lesinski7ad11102016-10-28 16:39:15 -07001097 // Create the max possible entries we can make. Once we construct the bag,
1098 // we will realloc to fit to size.
1099 const size_t max_count = parent_bag->entry_count + dtohl(map->count);
George Burgess IV09b119f2017-07-25 15:00:04 -07001100 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
1101 malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001102 ResolvedBag::Entry* new_entry = new_bag->entries;
1103
1104 const ResolvedBag::Entry* parent_entry = parent_bag->entries;
1105 const ResolvedBag::Entry* const parent_entry_end = parent_entry + parent_bag->entry_count;
1106
1107 // The keys are expected to be in sorted order. Merge the two bags.
Ryan Mitchell155d5392020-02-10 13:35:24 -08001108 bool sort_entries = false;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001109 while (map_entry != map_entry_end && parent_entry != parent_entry_end) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001110 uint32_t child_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001111 if (!is_internal_resid(child_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001112 if (entry.dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001113 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key,
1114 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -05001115 return nullptr;
1116 }
1117 }
1118
Adam Lesinski7ad11102016-10-28 16:39:15 -07001119 if (child_key <= parent_entry->key) {
1120 // Use the child key if it comes before the parent
1121 // or is equal to the parent (overrides).
1122 new_entry->cookie = cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001123 new_entry->key = child_key;
1124 new_entry->key_pool = nullptr;
1125 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -07001126 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001127 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -07001128 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
1129 if (err != NO_ERROR) {
1130 LOG(ERROR) << base::StringPrintf(
1131 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
1132 new_entry->value.data, child_key);
1133 return nullptr;
1134 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001135 ++map_entry;
1136 } else {
1137 // Take the parent entry as-is.
1138 memcpy(new_entry, parent_entry, sizeof(*new_entry));
1139 }
1140
Ryan Mitchell155d5392020-02-10 13:35:24 -08001141 sort_entries = sort_entries ||
1142 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001143 if (child_key >= parent_entry->key) {
1144 // Move to the next parent entry if we used it or it was overridden.
1145 ++parent_entry;
1146 }
1147 // Increment to the next entry to fill.
1148 ++new_entry;
1149 }
1150
1151 // Finish the child entries if they exist.
1152 while (map_entry != map_entry_end) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001153 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -08001154 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -05001155 if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001156 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key,
1157 resid);
Adam Lesinskida431a22016-12-29 16:08:16 -05001158 return nullptr;
1159 }
1160 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001161 new_entry->cookie = cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001162 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001163 new_entry->key_pool = nullptr;
1164 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -07001165 new_entry->value.copyFrom_dtoh(map_entry->value);
Aurimas Liutikasd42a6702018-11-15 15:48:28 -08001166 new_entry->style = resid;
Adam Lesinski30080e22017-10-16 16:18:09 -07001167 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
1168 if (err != NO_ERROR) {
1169 LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.",
1170 new_entry->value.dataType, new_entry->value.data, new_key);
1171 return nullptr;
1172 }
Ryan Mitchell155d5392020-02-10 13:35:24 -08001173 sort_entries = sort_entries ||
1174 (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001175 ++map_entry;
1176 ++new_entry;
1177 }
1178
1179 // Finish the parent entries if they exist.
1180 if (parent_entry != parent_entry_end) {
1181 // Take the rest of the parent entries as-is.
1182 const size_t num_entries_to_copy = parent_entry_end - parent_entry;
1183 memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry));
1184 new_entry += num_entries_to_copy;
1185 }
1186
1187 // Resize the resulting array to fit.
1188 const size_t actual_count = new_entry - new_bag->entries;
1189 if (actual_count != max_count) {
George Burgess IV09b119f2017-07-25 15:00:04 -07001190 new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc(
1191 new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry)))));
Adam Lesinski7ad11102016-10-28 16:39:15 -07001192 }
1193
Ryan Mitchell155d5392020-02-10 13:35:24 -08001194 if (sort_entries) {
1195 std::sort(new_bag->entries, new_bag->entries + actual_count, compare_bag_entries);
1196 }
1197
Adam Lesinski1a1e9c22017-10-13 15:45:34 -07001198 // Combine flags from the parent and our own bag.
1199 new_bag->type_spec_flags = entry.type_flags | parent_bag->type_spec_flags;
George Burgess IV09b119f2017-07-25 15:00:04 -07001200 new_bag->entry_count = static_cast<uint32_t>(actual_count);
1201 ResolvedBag* result = new_bag.get();
1202 cached_bags_[resid] = std::move(new_bag);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001203 return result;
1204}
1205
Adam Lesinski929d6512017-01-16 19:11:19 -08001206static bool Utf8ToUtf16(const StringPiece& str, std::u16string* out) {
1207 ssize_t len =
1208 utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false);
1209 if (len < 0) {
1210 return false;
1211 }
1212 out->resize(static_cast<size_t>(len));
1213 utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(),
1214 static_cast<size_t>(len + 1));
1215 return true;
1216}
1217
Adam Lesinski0c405242017-01-13 20:47:26 -08001218uint32_t AssetManager2::GetResourceId(const std::string& resource_name,
1219 const std::string& fallback_type,
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001220 const std::string& fallback_package) const {
Adam Lesinski929d6512017-01-16 19:11:19 -08001221 StringPiece package_name, type, entry;
1222 if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) {
1223 return 0u;
1224 }
1225
1226 if (entry.empty()) {
1227 return 0u;
1228 }
1229
1230 if (package_name.empty()) {
1231 package_name = fallback_package;
1232 }
1233
1234 if (type.empty()) {
1235 type = fallback_type;
1236 }
1237
1238 std::u16string type16;
1239 if (!Utf8ToUtf16(type, &type16)) {
1240 return 0u;
1241 }
1242
1243 std::u16string entry16;
1244 if (!Utf8ToUtf16(entry, &entry16)) {
1245 return 0u;
1246 }
1247
1248 const StringPiece16 kAttr16 = u"attr";
1249 const static std::u16string kAttrPrivate16 = u"^attr-private";
1250
1251 for (const PackageGroup& package_group : package_groups_) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001252 for (const ConfiguredPackage& package_impl : package_group.packages_) {
1253 const LoadedPackage* package = package_impl.loaded_package_;
Adam Lesinski929d6512017-01-16 19:11:19 -08001254 if (package_name != package->GetPackageName()) {
1255 // All packages in the same group are expected to have the same package name.
1256 break;
1257 }
1258
1259 uint32_t resid = package->FindEntryByName(type16, entry16);
1260 if (resid == 0u && kAttr16 == type16) {
1261 // Private attributes in libraries (such as the framework) are sometimes encoded
1262 // under the type '^attr-private' in order to leave the ID space of public 'attr'
1263 // free for future additions. Check '^attr-private' for the same name.
1264 resid = package->FindEntryByName(kAttrPrivate16, entry16);
1265 }
1266
1267 if (resid != 0u) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001268 return fix_package_id(resid, package_group.dynamic_ref_table->mAssignedPackageId);
Adam Lesinski929d6512017-01-16 19:11:19 -08001269 }
1270 }
1271 }
Adam Lesinski0c405242017-01-13 20:47:26 -08001272 return 0u;
1273}
1274
Mårten Kongstad668ec5b2018-06-11 14:11:33 +02001275void AssetManager2::RebuildFilterList(bool filter_incompatible_configs) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001276 for (PackageGroup& group : package_groups_) {
1277 for (ConfiguredPackage& impl : group.packages_) {
1278 // Destroy it.
1279 impl.filtered_configs_.~ByteBucketArray();
1280
1281 // Re-create it.
1282 new (&impl.filtered_configs_) ByteBucketArray<FilteredConfigGroup>();
1283
1284 // Create the filters here.
1285 impl.loaded_package_->ForEachTypeSpec([&](const TypeSpec* spec, uint8_t type_index) {
1286 FilteredConfigGroup& group = impl.filtered_configs_.editItemAt(type_index);
1287 const auto iter_end = spec->types + spec->type_count;
1288 for (auto iter = spec->types; iter != iter_end; ++iter) {
1289 ResTable_config this_config;
1290 this_config.copyFromDtoH((*iter)->config);
Mårten Kongstad668ec5b2018-06-11 14:11:33 +02001291 if (!filter_incompatible_configs || this_config.match(configuration_)) {
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001292 group.configurations.push_back(this_config);
1293 group.types.push_back(*iter);
1294 }
1295 }
1296 });
1297 }
1298 }
1299}
1300
Adam Lesinski7ad11102016-10-28 16:39:15 -07001301void AssetManager2::InvalidateCaches(uint32_t diff) {
Ryan Mitchell2c4d8742019-03-04 09:41:00 -08001302 cached_bag_resid_stacks_.clear();
1303
Adam Lesinski7ad11102016-10-28 16:39:15 -07001304 if (diff == 0xffffffffu) {
1305 // Everything must go.
1306 cached_bags_.clear();
1307 return;
1308 }
1309
1310 // Be more conservative with what gets purged. Only if the bag has other possible
1311 // variations with respect to what changed (diff) should we remove it.
1312 for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) {
1313 if (diff & iter->second->type_spec_flags) {
1314 iter = cached_bags_.erase(iter);
1315 } else {
1316 ++iter;
1317 }
1318 }
1319}
1320
Ryan Mitchell2e394222019-08-28 12:10:51 -07001321uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) const {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001322 for (auto& package_group : package_groups_) {
1323 for (auto& package2 : package_group.packages_) {
1324 if (package2.loaded_package_ == package) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -07001325 return package_group.dynamic_ref_table->mAssignedPackageId;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001326 }
1327 }
1328 }
1329 return 0;
1330}
1331
Ryan Mitchellfe50d732019-12-19 16:12:35 -08001332DynamicLibManager* AssetManager2::GetDynamicLibManager() const {
1333 auto dynamic_lib_manager =
1334 std::get_if<std::unique_ptr<DynamicLibManager>>(&dynamic_lib_manager_);
1335 if (dynamic_lib_manager) {
1336 return (*dynamic_lib_manager).get();
1337 } else {
1338 return *std::get_if<DynamicLibManager*>(&dynamic_lib_manager_);
1339 }
1340}
1341
Adam Lesinski30080e22017-10-16 16:18:09 -07001342std::unique_ptr<Theme> AssetManager2::NewTheme() {
1343 return std::unique_ptr<Theme>(new Theme(this));
1344}
1345
1346Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) {
1347}
1348
1349Theme::~Theme() = default;
1350
1351namespace {
1352
1353struct ThemeEntry {
1354 ApkAssetsCookie cookie;
1355 uint32_t type_spec_flags;
1356 Res_value value;
1357};
1358
1359struct ThemeType {
1360 int entry_count;
1361 ThemeEntry entries[0];
1362};
1363
1364constexpr size_t kTypeCount = std::numeric_limits<uint8_t>::max() + 1;
1365
1366} // namespace
1367
1368struct Theme::Package {
1369 // Each element of Type will be a dynamically sized object
1370 // allocated to have the entries stored contiguously with the Type.
1371 std::array<util::unique_cptr<ThemeType>, kTypeCount> types;
1372};
Adam Lesinski7ad11102016-10-28 16:39:15 -07001373
1374bool 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
1377 const ResolvedBag* bag = asset_manager_->GetBag(resid);
1378 if (bag == nullptr) {
1379 return false;
1380 }
1381
1382 // Merge the flags from this style.
1383 type_spec_flags_ |= bag->type_spec_flags;
1384
Adam Lesinski30080e22017-10-16 16:18:09 -07001385 int last_type_idx = -1;
1386 int last_package_idx = -1;
1387 Package* last_package = nullptr;
1388 ThemeType* last_type = nullptr;
1389
1390 // Iterate backwards, because each bag is sorted in ascending key ID order, meaning we will only
1391 // need to perform one resize per type.
1392 using reverse_bag_iterator = std::reverse_iterator<const ResolvedBag::Entry*>;
1393 const auto bag_iter_end = reverse_bag_iterator(begin(bag));
1394 for (auto bag_iter = reverse_bag_iterator(end(bag)); bag_iter != bag_iter_end; ++bag_iter) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001395 const uint32_t attr_resid = bag_iter->key;
1396
Adam Lesinski30080e22017-10-16 16:18:09 -07001397 // If the resource ID passed in is not a style, the key can be some other identifier that is not
1398 // a resource ID. We should fail fast instead of operating with strange resource IDs.
Adam Lesinski929d6512017-01-16 19:11:19 -08001399 if (!is_valid_resid(attr_resid)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001400 return false;
1401 }
1402
Adam Lesinski30080e22017-10-16 16:18:09 -07001403 // We don't use the 0-based index for the type so that we can avoid doing ID validation
1404 // upon lookup. Instead, we keep space for the type ID 0 in our data structures. Since
1405 // the construction of this type is guarded with a resource ID check, it will never be
1406 // populated, and querying type ID 0 will always fail.
1407 const int package_idx = get_package_id(attr_resid);
1408 const int type_idx = get_type_id(attr_resid);
1409 const int entry_idx = get_entry_id(attr_resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001410
Adam Lesinski30080e22017-10-16 16:18:09 -07001411 if (last_package_idx != package_idx) {
1412 std::unique_ptr<Package>& package = packages_[package_idx];
1413 if (package == nullptr) {
1414 package.reset(new Package());
Adam Lesinski7ad11102016-10-28 16:39:15 -07001415 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001416 last_package_idx = package_idx;
1417 last_package = package.get();
1418 last_type_idx = -1;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001419 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001420
1421 if (last_type_idx != type_idx) {
1422 util::unique_cptr<ThemeType>& type = last_package->types[type_idx];
1423 if (type == nullptr) {
1424 // Allocate enough memory to contain this entry_idx. Since we're iterating in reverse over
1425 // a sorted list of attributes, this shouldn't be resized again during this method call.
1426 type.reset(reinterpret_cast<ThemeType*>(
1427 calloc(sizeof(ThemeType) + (entry_idx + 1) * sizeof(ThemeEntry), 1)));
1428 type->entry_count = entry_idx + 1;
1429 } else if (entry_idx >= type->entry_count) {
1430 // Reallocate the memory to contain this entry_idx. Since we're iterating in reverse over
1431 // a sorted list of attributes, this shouldn't be resized again during this method call.
1432 const int new_count = entry_idx + 1;
1433 type.reset(reinterpret_cast<ThemeType*>(
1434 realloc(type.release(), sizeof(ThemeType) + (new_count * sizeof(ThemeEntry)))));
1435
1436 // Clear out the newly allocated space (which isn't zeroed).
1437 memset(type->entries + type->entry_count, 0,
1438 (new_count - type->entry_count) * sizeof(ThemeEntry));
1439 type->entry_count = new_count;
1440 }
1441 last_type_idx = type_idx;
1442 last_type = type.get();
1443 }
1444
1445 ThemeEntry& entry = last_type->entries[entry_idx];
1446 if (force || (entry.value.dataType == Res_value::TYPE_NULL &&
1447 entry.value.data != Res_value::DATA_NULL_EMPTY)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001448 entry.cookie = bag_iter->cookie;
1449 entry.type_spec_flags |= bag->type_spec_flags;
1450 entry.value = bag_iter->value;
1451 }
1452 }
1453 return true;
1454}
1455
1456ApkAssetsCookie Theme::GetAttribute(uint32_t resid, Res_value* out_value,
1457 uint32_t* out_flags) const {
Adam Lesinski30080e22017-10-16 16:18:09 -07001458 int cnt = 20;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001459
1460 uint32_t type_spec_flags = 0u;
1461
Adam Lesinski30080e22017-10-16 16:18:09 -07001462 do {
1463 const int package_idx = get_package_id(resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001464 const Package* package = packages_[package_idx].get();
Adam Lesinski30080e22017-10-16 16:18:09 -07001465 if (package != nullptr) {
1466 // The themes are constructed with a 1-based type ID, so no need to decrement here.
1467 const int type_idx = get_type_id(resid);
1468 const ThemeType* type = package->types[type_idx].get();
1469 if (type != nullptr) {
1470 const int entry_idx = get_entry_id(resid);
1471 if (entry_idx < type->entry_count) {
1472 const ThemeEntry& entry = type->entries[entry_idx];
1473 type_spec_flags |= entry.type_spec_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001474
Adam Lesinski30080e22017-10-16 16:18:09 -07001475 if (entry.value.dataType == Res_value::TYPE_ATTRIBUTE) {
1476 if (cnt > 0) {
1477 cnt--;
1478 resid = entry.value.data;
1479 continue;
1480 }
1481 return kInvalidCookie;
1482 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001483
Adam Lesinski30080e22017-10-16 16:18:09 -07001484 // @null is different than @empty.
1485 if (entry.value.dataType == Res_value::TYPE_NULL &&
1486 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1487 return kInvalidCookie;
1488 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001489
Adam Lesinski30080e22017-10-16 16:18:09 -07001490 *out_value = entry.value;
Adam Lesinskida431a22016-12-29 16:08:16 -05001491 *out_flags = type_spec_flags;
Adam Lesinski30080e22017-10-16 16:18:09 -07001492 return entry.cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -05001493 }
Adam Lesinskida431a22016-12-29 16:08:16 -05001494 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001495 }
Adam Lesinski30080e22017-10-16 16:18:09 -07001496 break;
1497 } while (true);
Adam Lesinski7ad11102016-10-28 16:39:15 -07001498 return kInvalidCookie;
1499}
1500
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001501ApkAssetsCookie Theme::ResolveAttributeReference(ApkAssetsCookie cookie, Res_value* in_out_value,
1502 ResTable_config* in_out_selected_config,
1503 uint32_t* in_out_type_spec_flags,
Adam Lesinskibebfcc42018-02-12 14:27:46 -08001504 uint32_t* out_last_ref) const {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -08001505 if (in_out_value->dataType == Res_value::TYPE_ATTRIBUTE) {
1506 uint32_t new_flags;
1507 cookie = GetAttribute(in_out_value->data, in_out_value, &new_flags);
1508 if (cookie == kInvalidCookie) {
1509 return kInvalidCookie;
1510 }
1511
1512 if (in_out_type_spec_flags != nullptr) {
1513 *in_out_type_spec_flags |= new_flags;
1514 }
1515 }
1516 return asset_manager_->ResolveReference(cookie, in_out_value, in_out_selected_config,
1517 in_out_type_spec_flags, out_last_ref);
1518}
1519
Adam Lesinski7ad11102016-10-28 16:39:15 -07001520void Theme::Clear() {
1521 type_spec_flags_ = 0u;
1522 for (std::unique_ptr<Package>& package : packages_) {
1523 package.reset();
1524 }
1525}
1526
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001527void Theme::SetTo(const Theme& o) {
Adam Lesinski7ad11102016-10-28 16:39:15 -07001528 if (this == &o) {
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001529 return;
Adam Lesinski7ad11102016-10-28 16:39:15 -07001530 }
1531
Adam Lesinski7ad11102016-10-28 16:39:15 -07001532 type_spec_flags_ = o.type_spec_flags_;
1533
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001534 if (asset_manager_ == o.asset_manager_) {
1535 // The theme comes from the same asset manager so all theme data can be copied exactly
1536 for (size_t p = 0; p < packages_.size(); p++) {
1537 const Package *package = o.packages_[p].get();
1538 if (package == nullptr) {
1539 // The other theme doesn't have this package, clear ours.
1540 packages_[p].reset();
Adam Lesinski7ad11102016-10-28 16:39:15 -07001541 continue;
1542 }
1543
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001544 if (packages_[p] == nullptr) {
1545 // The other theme has this package, but we don't. Make one.
1546 packages_[p].reset(new Package());
1547 }
1548
1549 for (size_t t = 0; t < package->types.size(); t++) {
1550 const ThemeType *type = package->types[t].get();
1551 if (type == nullptr) {
1552 // The other theme doesn't have this type, clear ours.
1553 packages_[p]->types[t].reset();
1554 continue;
1555 }
1556
1557 // Create a new type and update it to theirs.
1558 const size_t type_alloc_size = sizeof(ThemeType) + (type->entry_count * sizeof(ThemeEntry));
1559 void *copied_data = malloc(type_alloc_size);
1560 memcpy(copied_data, type, type_alloc_size);
1561 packages_[p]->types[t].reset(reinterpret_cast<ThemeType *>(copied_data));
1562 }
1563 }
1564 } else {
1565 std::map<ApkAssetsCookie, ApkAssetsCookie> src_to_dest_asset_cookies;
1566 typedef std::map<int, int> SourceToDestinationRuntimePackageMap;
1567 std::map<ApkAssetsCookie, SourceToDestinationRuntimePackageMap> src_asset_cookie_id_map;
1568
Ryan Mitchell93bca972019-03-08 17:26:28 -08001569 // Determine which ApkAssets are loaded in both theme AssetManagers.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001570 std::vector<const ApkAssets*> src_assets = o.asset_manager_->GetApkAssets();
1571 for (size_t i = 0; i < src_assets.size(); i++) {
1572 const ApkAssets* src_asset = src_assets[i];
1573
1574 std::vector<const ApkAssets*> dest_assets = asset_manager_->GetApkAssets();
1575 for (size_t j = 0; j < dest_assets.size(); j++) {
1576 const ApkAssets* dest_asset = dest_assets[j];
1577
Ryan Mitchell93bca972019-03-08 17:26:28 -08001578 // Map the runtime package of the source apk asset to the destination apk asset.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001579 if (src_asset->GetPath() == dest_asset->GetPath()) {
1580 const std::vector<std::unique_ptr<const LoadedPackage>>& src_packages =
1581 src_asset->GetLoadedArsc()->GetPackages();
1582 const std::vector<std::unique_ptr<const LoadedPackage>>& dest_packages =
1583 dest_asset->GetLoadedArsc()->GetPackages();
1584
1585 SourceToDestinationRuntimePackageMap package_map;
1586
1587 // The source and destination package should have the same number of packages loaded in
1588 // the same order.
1589 const size_t N = src_packages.size();
1590 CHECK(N == dest_packages.size())
1591 << " LoadedArsc " << src_asset->GetPath() << " differs number of packages.";
1592 for (size_t p = 0; p < N; p++) {
1593 auto& src_package = src_packages[p];
1594 auto& dest_package = dest_packages[p];
1595 CHECK(src_package->GetPackageName() == dest_package->GetPackageName())
1596 << " Package " << src_package->GetPackageName() << " differs in load order.";
1597
1598 int src_package_id = o.asset_manager_->GetAssignedPackageId(src_package.get());
1599 int dest_package_id = asset_manager_->GetAssignedPackageId(dest_package.get());
1600 package_map[src_package_id] = dest_package_id;
1601 }
1602
Ryan Mitchell93bca972019-03-08 17:26:28 -08001603 src_to_dest_asset_cookies.insert(std::make_pair(i, j));
1604 src_asset_cookie_id_map.insert(std::make_pair(i, package_map));
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001605 break;
1606 }
1607 }
1608 }
1609
Ryan Mitchell93bca972019-03-08 17:26:28 -08001610 // Reset the data in the destination theme.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001611 for (size_t p = 0; p < packages_.size(); p++) {
1612 if (packages_[p] != nullptr) {
1613 packages_[p].reset();
1614 }
1615 }
1616
1617 for (size_t p = 0; p < packages_.size(); p++) {
1618 const Package *package = o.packages_[p].get();
1619 if (package == nullptr) {
1620 continue;
1621 }
1622
1623 for (size_t t = 0; t < package->types.size(); t++) {
1624 const ThemeType *type = package->types[t].get();
1625 if (type == nullptr) {
1626 continue;
1627 }
1628
1629 for (size_t e = 0; e < type->entry_count; e++) {
1630 const ThemeEntry &entry = type->entries[e];
1631 if (entry.value.dataType == Res_value::TYPE_NULL &&
1632 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1633 continue;
1634 }
1635
Ryan Mitchell93bca972019-03-08 17:26:28 -08001636 bool is_reference = (entry.value.dataType == Res_value::TYPE_ATTRIBUTE
1637 || entry.value.dataType == Res_value::TYPE_REFERENCE
1638 || entry.value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE
1639 || entry.value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE)
1640 && entry.value.data != 0x0;
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001641
Ryan Mitchell93bca972019-03-08 17:26:28 -08001642 // If the attribute value represents an attribute or reference, the package id of the
1643 // value needs to be rewritten to the package id of the value in the destination.
1644 uint32_t attribute_data = entry.value.data;
1645 if (is_reference) {
1646 // Determine the package id of the reference in the destination AssetManager.
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001647 auto value_package_map = src_asset_cookie_id_map.find(entry.cookie);
1648 if (value_package_map == src_asset_cookie_id_map.end()) {
1649 continue;
1650 }
1651
1652 auto value_dest_package = value_package_map->second.find(
1653 get_package_id(entry.value.data));
1654 if (value_dest_package == value_package_map->second.end()) {
1655 continue;
1656 }
1657
Ryan Mitchell93bca972019-03-08 17:26:28 -08001658 attribute_data = fix_package_id(entry.value.data, value_dest_package->second);
1659 }
1660
1661 // Find the cookie of the value in the destination. If the source apk is not loaded in the
1662 // destination, only copy resources that do not reference resources in the source.
1663 ApkAssetsCookie data_dest_cookie;
1664 auto value_dest_cookie = src_to_dest_asset_cookies.find(entry.cookie);
1665 if (value_dest_cookie != src_to_dest_asset_cookies.end()) {
1666 data_dest_cookie = value_dest_cookie->second;
1667 } else {
1668 if (is_reference || entry.value.dataType == Res_value::TYPE_STRING) {
1669 continue;
1670 } else {
1671 data_dest_cookie = 0x0;
1672 }
Ryan Mitchellb85d9b22018-11-19 12:11:38 -08001673 }
1674
1675 // The package id of the attribute needs to be rewritten to the package id of the
Ryan Mitchell93bca972019-03-08 17:26:28 -08001676 // attribute in the destination.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001677 int attribute_dest_package_id = p;
1678 if (attribute_dest_package_id != 0x01) {
Ryan Mitchell93bca972019-03-08 17:26:28 -08001679 // Find the cookie of the attribute resource id in the source AssetManager
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001680 FindEntryResult attribute_entry_result;
1681 ApkAssetsCookie attribute_cookie =
Ryan Mitchella55dc2e2019-01-24 10:58:23 -08001682 o.asset_manager_->FindEntry(make_resid(p, t, e), 0 /* density_override */ ,
1683 true /* stop_at_first_match */,
1684 true /* ignore_configuration */,
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001685 &attribute_entry_result);
1686
Ryan Mitchell93bca972019-03-08 17:26:28 -08001687 // Determine the package id of the attribute in the destination AssetManager.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001688 auto attribute_package_map = src_asset_cookie_id_map.find(attribute_cookie);
1689 if (attribute_package_map == src_asset_cookie_id_map.end()) {
1690 continue;
1691 }
1692 auto attribute_dest_package = attribute_package_map->second.find(
1693 attribute_dest_package_id);
1694 if (attribute_dest_package == attribute_package_map->second.end()) {
1695 continue;
1696 }
1697 attribute_dest_package_id = attribute_dest_package->second;
1698 }
1699
Ryan Mitchell93bca972019-03-08 17:26:28 -08001700 // Lazily instantiate the destination package.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001701 std::unique_ptr<Package>& dest_package = packages_[attribute_dest_package_id];
1702 if (dest_package == nullptr) {
1703 dest_package.reset(new Package());
1704 }
1705
Ryan Mitchell93bca972019-03-08 17:26:28 -08001706 // Lazily instantiate and resize the destination type.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001707 util::unique_cptr<ThemeType>& dest_type = dest_package->types[t];
1708 if (dest_type == nullptr || dest_type->entry_count < type->entry_count) {
1709 const size_t type_alloc_size = sizeof(ThemeType)
1710 + (type->entry_count * sizeof(ThemeEntry));
1711 void* dest_data = malloc(type_alloc_size);
1712 memset(dest_data, 0, type->entry_count * sizeof(ThemeEntry));
1713
Ryan Mitchell93bca972019-03-08 17:26:28 -08001714 // Copy the existing destination type values if the type is resized.
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001715 if (dest_type != nullptr) {
1716 memcpy(dest_data, type, sizeof(ThemeType)
1717 + (dest_type->entry_count * sizeof(ThemeEntry)));
1718 }
1719
1720 dest_type.reset(reinterpret_cast<ThemeType *>(dest_data));
1721 dest_type->entry_count = type->entry_count;
1722 }
1723
Ryan Mitchell93bca972019-03-08 17:26:28 -08001724 dest_type->entries[e].cookie = data_dest_cookie;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001725 dest_type->entries[e].value.dataType = entry.value.dataType;
Ryan Mitchell93bca972019-03-08 17:26:28 -08001726 dest_type->entries[e].value.data = attribute_data;
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001727 dest_type->entries[e].type_spec_flags = entry.type_spec_flags;
1728 }
1729 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001730 }
1731 }
Ryan Mitchellb3ae42e2018-10-16 12:48:38 -07001732}
1733
1734void Theme::Dump() const {
1735 base::ScopedLogSeverity _log(base::INFO);
1736 LOG(INFO) << base::StringPrintf("Theme(this=%p, AssetManager2=%p)", this, asset_manager_);
1737
1738 for (int p = 0; p < packages_.size(); p++) {
1739 auto& package = packages_[p];
1740 if (package == nullptr) {
1741 continue;
1742 }
1743
1744 for (int t = 0; t < package->types.size(); t++) {
1745 auto& type = package->types[t];
1746 if (type == nullptr) {
1747 continue;
1748 }
1749
1750 for (int e = 0; e < type->entry_count; e++) {
1751 auto& entry = type->entries[e];
1752 if (entry.value.dataType == Res_value::TYPE_NULL &&
1753 entry.value.data != Res_value::DATA_NULL_EMPTY) {
1754 continue;
1755 }
1756
1757 LOG(INFO) << base::StringPrintf(" entry(0x%08x)=(0x%08x) type=(0x%02x), cookie(%d)",
1758 make_resid(p, t, e), entry.value.data,
1759 entry.value.dataType, entry.cookie);
1760 }
1761 }
1762 }
Adam Lesinski7ad11102016-10-28 16:39:15 -07001763}
1764
1765} // namespace android