blob: 20dba37fbe66e2c45b669f0381f41178572c3457 [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
Adam Lesinski30080e22017-10-16 16:18:09 -070021#include <iterator>
Adam Lesinski0c405242017-01-13 20:47:26 -080022#include <set>
23
Adam Lesinski7ad11102016-10-28 16:39:15 -070024#include "android-base/logging.h"
25#include "android-base/stringprintf.h"
26#include "utils/ByteOrder.h"
27#include "utils/Trace.h"
28
29#ifdef _WIN32
30#ifdef ERROR
31#undef ERROR
32#endif
33#endif
34
Adam Lesinski929d6512017-01-16 19:11:19 -080035#include "androidfw/ResourceUtils.h"
36
Adam Lesinski7ad11102016-10-28 16:39:15 -070037namespace android {
38
Adam Lesinski970bd8d2017-09-25 13:21:55 -070039AssetManager2::AssetManager2() {
40 memset(&configuration_, 0, sizeof(configuration_));
41}
Adam Lesinski7ad11102016-10-28 16:39:15 -070042
43bool AssetManager2::SetApkAssets(const std::vector<const ApkAssets*>& apk_assets,
44 bool invalidate_caches) {
45 apk_assets_ = apk_assets;
Adam Lesinskida431a22016-12-29 16:08:16 -050046 BuildDynamicRefTable();
Adam Lesinski7ad11102016-10-28 16:39:15 -070047 if (invalidate_caches) {
48 InvalidateCaches(static_cast<uint32_t>(-1));
49 }
50 return true;
51}
52
Adam Lesinskida431a22016-12-29 16:08:16 -050053void AssetManager2::BuildDynamicRefTable() {
54 package_groups_.clear();
55 package_ids_.fill(0xff);
56
57 // 0x01 is reserved for the android package.
58 int next_package_id = 0x02;
59 const size_t apk_assets_count = apk_assets_.size();
60 for (size_t i = 0; i < apk_assets_count; i++) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -070061 const LoadedArsc* loaded_arsc = apk_assets_[i]->GetLoadedArsc();
62
63 for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) {
Adam Lesinskida431a22016-12-29 16:08:16 -050064 // Get the package ID or assign one if a shared library.
65 int package_id;
66 if (package->IsDynamic()) {
67 package_id = next_package_id++;
68 } else {
69 package_id = package->GetPackageId();
70 }
71
72 // Add the mapping for package ID to index if not present.
73 uint8_t idx = package_ids_[package_id];
74 if (idx == 0xff) {
75 package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size());
76 package_groups_.push_back({});
Adam Lesinski9ad287c2018-01-30 17:11:48 -080077 DynamicRefTable& ref_table = package_groups_.back().dynamic_ref_table;
78 ref_table.mAssignedPackageId = package_id;
79 ref_table.mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f;
Adam Lesinskida431a22016-12-29 16:08:16 -050080 }
81 PackageGroup* package_group = &package_groups_[idx];
82
83 // Add the package and to the set of packages with the same ID.
Adam Lesinskib8b3a262018-02-09 11:01:45 -080084 package_group->packages_.push_back(package.get());
Adam Lesinskida431a22016-12-29 16:08:16 -050085 package_group->cookies_.push_back(static_cast<ApkAssetsCookie>(i));
86
87 // Add the package name -> build time ID mappings.
88 for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) {
89 String16 package_name(entry.package_name.c_str(), entry.package_name.size());
90 package_group->dynamic_ref_table.mEntries.replaceValueFor(
91 package_name, static_cast<uint8_t>(entry.package_id));
92 }
93 }
94 }
95
96 // Now assign the runtime IDs so that we have a build-time to runtime ID map.
97 const auto package_groups_end = package_groups_.end();
98 for (auto iter = package_groups_.begin(); iter != package_groups_end; ++iter) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -080099 const std::string& package_name = iter->packages_[0]->GetPackageName();
Adam Lesinskida431a22016-12-29 16:08:16 -0500100 for (auto iter2 = package_groups_.begin(); iter2 != package_groups_end; ++iter2) {
101 iter2->dynamic_ref_table.addMapping(String16(package_name.c_str(), package_name.size()),
102 iter->dynamic_ref_table.mAssignedPackageId);
103 }
104 }
105}
106
107void AssetManager2::DumpToLog() const {
108 base::ScopedLogSeverity _log(base::INFO);
109
Adam Lesinski9ad287c2018-01-30 17:11:48 -0800110 LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this);
111
Adam Lesinskida431a22016-12-29 16:08:16 -0500112 std::string list;
Adam Lesinski9ad287c2018-01-30 17:11:48 -0800113 for (const auto& apk_assets : apk_assets_) {
114 base::StringAppendF(&list, "%s,", apk_assets->GetPath().c_str());
115 }
116 LOG(INFO) << "ApkAssets: " << list;
117
118 list = "";
Adam Lesinskida431a22016-12-29 16:08:16 -0500119 for (size_t i = 0; i < package_ids_.size(); i++) {
120 if (package_ids_[i] != 0xff) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800121 base::StringAppendF(&list, "%02x -> %d, ", (int) i, package_ids_[i]);
Adam Lesinskida431a22016-12-29 16:08:16 -0500122 }
123 }
124 LOG(INFO) << "Package ID map: " << list;
125
Adam Lesinski0dd36992018-01-25 15:38:38 -0800126 for (const auto& package_group: package_groups_) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800127 list = "";
128 for (const auto& package : package_group.packages_) {
129 base::StringAppendF(&list, "%s(%02x%s), ", package->GetPackageName().c_str(),
130 package->GetPackageId(), (package->IsDynamic() ? " dynamic" : ""));
131 }
132 LOG(INFO) << base::StringPrintf("PG (%02x): ",
133 package_group.dynamic_ref_table.mAssignedPackageId)
134 << list;
Adam Lesinskida431a22016-12-29 16:08:16 -0500135 }
136}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700137
138const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const {
139 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
140 return nullptr;
141 }
142 return apk_assets_[cookie]->GetLoadedArsc()->GetStringPool();
143}
144
Adam Lesinskida431a22016-12-29 16:08:16 -0500145const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const {
146 if (package_id >= package_ids_.size()) {
147 return nullptr;
148 }
149
150 const size_t idx = package_ids_[package_id];
151 if (idx == 0xff) {
152 return nullptr;
153 }
154 return &package_groups_[idx].dynamic_ref_table;
155}
156
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800157const DynamicRefTable* AssetManager2::GetDynamicRefTableForCookie(ApkAssetsCookie cookie) const {
158 for (const PackageGroup& package_group : package_groups_) {
159 for (const ApkAssetsCookie& package_cookie : package_group.cookies_) {
160 if (package_cookie == cookie) {
161 return &package_group.dynamic_ref_table;
162 }
163 }
164 }
165 return nullptr;
166}
167
Adam Lesinski7ad11102016-10-28 16:39:15 -0700168void AssetManager2::SetConfiguration(const ResTable_config& configuration) {
169 const int diff = configuration_.diff(configuration);
170 configuration_ = configuration;
171
172 if (diff) {
173 InvalidateCaches(static_cast<uint32_t>(diff));
174 }
175}
176
Adam Lesinski0c405242017-01-13 20:47:26 -0800177std::set<ResTable_config> AssetManager2::GetResourceConfigurations(bool exclude_system,
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800178 bool exclude_mipmap) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800179 ATRACE_CALL();
180 std::set<ResTable_config> configurations;
181 for (const PackageGroup& package_group : package_groups_) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800182 for (const LoadedPackage* package : package_group.packages_) {
183 if (exclude_system && package->IsSystem()) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800184 continue;
185 }
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800186 package->CollectConfigurations(exclude_mipmap, &configurations);
Adam Lesinski0c405242017-01-13 20:47:26 -0800187 }
188 }
189 return configurations;
190}
191
192std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system,
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800193 bool merge_equivalent_languages) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800194 ATRACE_CALL();
195 std::set<std::string> locales;
196 for (const PackageGroup& package_group : package_groups_) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800197 for (const LoadedPackage* package : package_group.packages_) {
198 if (exclude_system && package->IsSystem()) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800199 continue;
200 }
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800201 package->CollectLocales(merge_equivalent_languages, &locales);
Adam Lesinski0c405242017-01-13 20:47:26 -0800202 }
203 }
204 return locales;
205}
206
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800207std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, Asset::AccessMode mode) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700208 const std::string new_path = "assets/" + filename;
209 return OpenNonAsset(new_path, mode);
210}
211
212std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie,
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800213 Asset::AccessMode mode) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700214 const std::string new_path = "assets/" + filename;
215 return OpenNonAsset(new_path, cookie, mode);
216}
217
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800218std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800219 ATRACE_CALL();
220
221 std::string full_path = "assets/" + dirname;
222 std::unique_ptr<SortedVector<AssetDir::FileInfo>> files =
223 util::make_unique<SortedVector<AssetDir::FileInfo>>();
224
225 // Start from the back.
226 for (auto iter = apk_assets_.rbegin(); iter != apk_assets_.rend(); ++iter) {
227 const ApkAssets* apk_assets = *iter;
228
229 auto func = [&](const StringPiece& name, FileType type) {
230 AssetDir::FileInfo info;
231 info.setFileName(String8(name.data(), name.size()));
232 info.setFileType(type);
233 info.setSourceName(String8(apk_assets->GetPath().c_str()));
234 files->add(info);
235 };
236
237 if (!apk_assets->ForEachFile(full_path, func)) {
238 return {};
239 }
240 }
241
242 std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>();
243 asset_dir->setFileList(files.release());
244 return asset_dir;
245}
246
Adam Lesinski7ad11102016-10-28 16:39:15 -0700247// Search in reverse because that's how we used to do it and we need to preserve behaviour.
248// This is unfortunate, because ClassLoaders delegate to the parent first, so the order
249// is inconsistent for split APKs.
250std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
251 Asset::AccessMode mode,
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800252 ApkAssetsCookie* out_cookie) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700253 ATRACE_CALL();
254 for (int32_t i = apk_assets_.size() - 1; i >= 0; i--) {
255 std::unique_ptr<Asset> asset = apk_assets_[i]->Open(filename, mode);
256 if (asset) {
257 if (out_cookie != nullptr) {
258 *out_cookie = i;
259 }
260 return asset;
261 }
262 }
263
264 if (out_cookie != nullptr) {
265 *out_cookie = kInvalidCookie;
266 }
267 return {};
268}
269
270std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename,
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800271 ApkAssetsCookie cookie, Asset::AccessMode mode) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700272 ATRACE_CALL();
273 if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) {
274 return {};
275 }
276 return apk_assets_[cookie]->Open(filename, mode);
277}
278
279ApkAssetsCookie AssetManager2::FindEntry(uint32_t resid, uint16_t density_override,
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800280 bool stop_at_first_match, FindEntryResult* out_entry) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700281 // Might use this if density_override != 0.
282 ResTable_config density_override_config;
283
284 // Select our configuration or generate a density override configuration.
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800285 ResTable_config* desired_config = &configuration_;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700286 if (density_override != 0 && density_override != configuration_.density) {
287 density_override_config = configuration_;
288 density_override_config.density = density_override;
289 desired_config = &density_override_config;
290 }
291
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800292 if (!is_valid_resid(resid)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500293 LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid);
294 return kInvalidCookie;
295 }
296
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800297 const uint32_t package_id = get_package_id(resid);
298 const uint8_t type_idx = get_type_id(resid) - 1;
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800299 const uint16_t entry_id = get_entry_id(resid);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800300
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800301 const uint8_t idx = package_ids_[package_id];
302 if (idx == 0xff) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500303 LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.", package_id, resid);
304 return kInvalidCookie;
305 }
306
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800307 FindEntryResult best_entry;
Adam Lesinski88c99592018-01-08 17:38:30 -0800308 ApkAssetsCookie best_cookie = kInvalidCookie;
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800309 uint32_t cumulated_flags = 0u;
Adam Lesinski88c99592018-01-08 17:38:30 -0800310
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800311 const PackageGroup& package_group = package_groups_[idx];
312 const size_t package_count = package_group.packages_.size();
313 FindEntryResult current_entry;
314 for (size_t i = 0; i < package_count; i++) {
315 const LoadedPackage* loaded_package = package_group.packages_[i];
316 if (!loaded_package->FindEntry(type_idx, entry_id, *desired_config, &current_entry)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700317 continue;
318 }
319
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800320 cumulated_flags |= current_entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700321
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800322 const ResTable_config* current_config = current_entry.config;
323 const ResTable_config* best_config = best_entry.config;
324 if (best_cookie == kInvalidCookie ||
325 current_config->isBetterThan(*best_config, desired_config) ||
326 (loaded_package->IsOverlay() && current_config->compare(*best_config) == 0)) {
327 best_entry = current_entry;
328 best_cookie = package_group.cookies_[i];
329 if (stop_at_first_match) {
330 break;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700331 }
332 }
333 }
334
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800335 if (best_cookie == kInvalidCookie) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700336 return kInvalidCookie;
337 }
338
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800339 *out_entry = best_entry;
Adam Lesinskida431a22016-12-29 16:08:16 -0500340 out_entry->dynamic_ref_table = &package_group.dynamic_ref_table;
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800341 out_entry->type_flags = cumulated_flags;
Adam Lesinskida431a22016-12-29 16:08:16 -0500342 return best_cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700343}
344
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800345bool AssetManager2::GetResourceName(uint32_t resid, ResourceName* out_name) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700346 ATRACE_CALL();
347
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700348 FindEntryResult entry;
349 ApkAssetsCookie cookie =
350 FindEntry(resid, 0u /* density_override */, true /* stop_at_first_match */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700351 if (cookie == kInvalidCookie) {
352 return false;
353 }
354
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800355 const LoadedPackage* package = apk_assets_[cookie]->GetLoadedArsc()->GetPackageForId(resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500356 if (package == nullptr) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700357 return false;
358 }
359
Adam Lesinskida431a22016-12-29 16:08:16 -0500360 out_name->package = package->GetPackageName().data();
361 out_name->package_len = package->GetPackageName().size();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700362
363 out_name->type = entry.type_string_ref.string8(&out_name->type_len);
364 out_name->type16 = nullptr;
365 if (out_name->type == nullptr) {
366 out_name->type16 = entry.type_string_ref.string16(&out_name->type_len);
367 if (out_name->type16 == nullptr) {
368 return false;
369 }
370 }
371
372 out_name->entry = entry.entry_string_ref.string8(&out_name->entry_len);
373 out_name->entry16 = nullptr;
374 if (out_name->entry == nullptr) {
375 out_name->entry16 = entry.entry_string_ref.string16(&out_name->entry_len);
376 if (out_name->entry16 == nullptr) {
377 return false;
378 }
379 }
380 return true;
381}
382
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800383bool AssetManager2::GetResourceFlags(uint32_t resid, uint32_t* out_flags) {
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700384 FindEntryResult entry;
385 ApkAssetsCookie cookie =
386 FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, &entry);
387 if (cookie != kInvalidCookie) {
388 *out_flags = entry.type_flags;
389 return cookie;
390 }
391 return kInvalidCookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700392}
393
394ApkAssetsCookie AssetManager2::GetResource(uint32_t resid, bool may_be_bag,
395 uint16_t density_override, Res_value* out_value,
396 ResTable_config* out_selected_config,
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800397 uint32_t* out_flags) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700398 ATRACE_CALL();
399
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700400 FindEntryResult entry;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700401 ApkAssetsCookie cookie =
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700402 FindEntry(resid, density_override, false /* stop_at_first_match */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700403 if (cookie == kInvalidCookie) {
404 return kInvalidCookie;
405 }
406
Adam Lesinski498f6052017-11-29 13:24:29 -0800407 if (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700408 if (!may_be_bag) {
409 LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid);
Adam Lesinski0c405242017-01-13 20:47:26 -0800410 return kInvalidCookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700411 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800412
413 // Create a reference since we can't represent this complex type as a Res_value.
414 out_value->dataType = Res_value::TYPE_REFERENCE;
415 out_value->data = resid;
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800416 *out_selected_config = *entry.config;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700417 *out_flags = entry.type_flags;
Adam Lesinski0c405242017-01-13 20:47:26 -0800418 return cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700419 }
420
421 const Res_value* device_value = reinterpret_cast<const Res_value*>(
422 reinterpret_cast<const uint8_t*>(entry.entry) + dtohs(entry.entry->size));
423 out_value->copyFrom_dtoh(*device_value);
Adam Lesinskida431a22016-12-29 16:08:16 -0500424
425 // Convert the package ID to the runtime assigned package ID.
426 entry.dynamic_ref_table->lookupResourceValue(out_value);
427
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800428 *out_selected_config = *entry.config;
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700429 *out_flags = entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700430 return cookie;
431}
432
Adam Lesinski0c405242017-01-13 20:47:26 -0800433ApkAssetsCookie AssetManager2::ResolveReference(ApkAssetsCookie cookie, Res_value* in_out_value,
434 ResTable_config* in_out_selected_config,
435 uint32_t* in_out_flags,
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800436 uint32_t* out_last_reference) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800437 ATRACE_CALL();
438 constexpr const int kMaxIterations = 20;
439
Adam Lesinski0c405242017-01-13 20:47:26 -0800440 for (size_t iteration = 0u; in_out_value->dataType == Res_value::TYPE_REFERENCE &&
441 in_out_value->data != 0u && iteration < kMaxIterations;
442 iteration++) {
Adam Lesinski11875902017-01-23 12:58:11 -0800443 *out_last_reference = in_out_value->data;
Adam Lesinski0c405242017-01-13 20:47:26 -0800444 uint32_t new_flags = 0u;
445 cookie = GetResource(in_out_value->data, true /*may_be_bag*/, 0u /*density_override*/,
446 in_out_value, in_out_selected_config, &new_flags);
447 if (cookie == kInvalidCookie) {
448 return kInvalidCookie;
449 }
450 if (in_out_flags != nullptr) {
451 *in_out_flags |= new_flags;
452 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800453 if (*out_last_reference == in_out_value->data) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800454 // This reference can't be resolved, so exit now and let the caller deal with it.
455 return cookie;
456 }
457 }
458 return cookie;
459}
460
Adam Lesinski7ad11102016-10-28 16:39:15 -0700461const ResolvedBag* AssetManager2::GetBag(uint32_t resid) {
462 ATRACE_CALL();
463
464 auto cached_iter = cached_bags_.find(resid);
465 if (cached_iter != cached_bags_.end()) {
466 return cached_iter->second.get();
467 }
468
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700469 FindEntryResult entry;
470 ApkAssetsCookie cookie =
471 FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700472 if (cookie == kInvalidCookie) {
473 return nullptr;
474 }
475
476 // Check that the size of the entry header is at least as big as
477 // the desired ResTable_map_entry. Also verify that the entry
478 // was intended to be a map.
479 if (dtohs(entry.entry->size) < sizeof(ResTable_map_entry) ||
480 (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) == 0) {
481 // Not a bag, nothing to do.
482 return nullptr;
483 }
484
485 const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry.entry);
486 const ResTable_map* map_entry =
487 reinterpret_cast<const ResTable_map*>(reinterpret_cast<const uint8_t*>(map) + map->size);
488 const ResTable_map* const map_entry_end = map_entry + dtohl(map->count);
489
Adam Lesinskida431a22016-12-29 16:08:16 -0500490 uint32_t parent_resid = dtohl(map->parent.ident);
491 if (parent_resid == 0) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700492 // There is no parent, meaning there is nothing to inherit and we can do a simple
493 // copy of the entries in the map.
494 const size_t entry_count = map_entry_end - map_entry;
495 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
496 malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))};
497 ResolvedBag::Entry* new_entry = new_bag->entries;
498 for (; map_entry != map_entry_end; ++map_entry) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500499 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -0800500 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500501 // Attributes, arrays, etc don't have a resource id as the name. They specify
502 // other data, which would be wrong to change via a lookup.
503 if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800504 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500505 return nullptr;
506 }
507 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700508 new_entry->cookie = cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -0500509 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700510 new_entry->key_pool = nullptr;
511 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -0700512 new_entry->value.copyFrom_dtoh(map_entry->value);
513 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
514 if (err != NO_ERROR) {
515 LOG(ERROR) << base::StringPrintf(
516 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
517 new_entry->value.data, new_key);
518 return nullptr;
519 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700520 ++new_entry;
521 }
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700522 new_bag->type_spec_flags = entry.type_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700523 new_bag->entry_count = static_cast<uint32_t>(entry_count);
524 ResolvedBag* result = new_bag.get();
525 cached_bags_[resid] = std::move(new_bag);
526 return result;
527 }
528
Adam Lesinskida431a22016-12-29 16:08:16 -0500529 // In case the parent is a dynamic reference, resolve it.
530 entry.dynamic_ref_table->lookupResourceId(&parent_resid);
531
Adam Lesinski7ad11102016-10-28 16:39:15 -0700532 // Get the parent and do a merge of the keys.
Adam Lesinskida431a22016-12-29 16:08:16 -0500533 const ResolvedBag* parent_bag = GetBag(parent_resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700534 if (parent_bag == nullptr) {
535 // Failed to get the parent that should exist.
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800536 LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid, resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700537 return nullptr;
538 }
539
Adam Lesinski7ad11102016-10-28 16:39:15 -0700540 // Create the max possible entries we can make. Once we construct the bag,
541 // we will realloc to fit to size.
542 const size_t max_count = parent_bag->entry_count + dtohl(map->count);
George Burgess IV09b119f2017-07-25 15:00:04 -0700543 util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>(
544 malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700545 ResolvedBag::Entry* new_entry = new_bag->entries;
546
547 const ResolvedBag::Entry* parent_entry = parent_bag->entries;
548 const ResolvedBag::Entry* const parent_entry_end = parent_entry + parent_bag->entry_count;
549
550 // The keys are expected to be in sorted order. Merge the two bags.
551 while (map_entry != map_entry_end && parent_entry != parent_entry_end) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500552 uint32_t child_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -0800553 if (!is_internal_resid(child_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500554 if (entry.dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800555 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key, resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500556 return nullptr;
557 }
558 }
559
Adam Lesinski7ad11102016-10-28 16:39:15 -0700560 if (child_key <= parent_entry->key) {
561 // Use the child key if it comes before the parent
562 // or is equal to the parent (overrides).
563 new_entry->cookie = cookie;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700564 new_entry->key = child_key;
565 new_entry->key_pool = nullptr;
566 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -0700567 new_entry->value.copyFrom_dtoh(map_entry->value);
568 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
569 if (err != NO_ERROR) {
570 LOG(ERROR) << base::StringPrintf(
571 "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType,
572 new_entry->value.data, child_key);
573 return nullptr;
574 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700575 ++map_entry;
576 } else {
577 // Take the parent entry as-is.
578 memcpy(new_entry, parent_entry, sizeof(*new_entry));
579 }
580
581 if (child_key >= parent_entry->key) {
582 // Move to the next parent entry if we used it or it was overridden.
583 ++parent_entry;
584 }
585 // Increment to the next entry to fill.
586 ++new_entry;
587 }
588
589 // Finish the child entries if they exist.
590 while (map_entry != map_entry_end) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500591 uint32_t new_key = dtohl(map_entry->name.ident);
Adam Lesinski929d6512017-01-16 19:11:19 -0800592 if (!is_internal_resid(new_key)) {
Adam Lesinskida431a22016-12-29 16:08:16 -0500593 if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800594 LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, resid);
Adam Lesinskida431a22016-12-29 16:08:16 -0500595 return nullptr;
596 }
597 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700598 new_entry->cookie = cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -0500599 new_entry->key = new_key;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700600 new_entry->key_pool = nullptr;
601 new_entry->type_pool = nullptr;
Adam Lesinski30080e22017-10-16 16:18:09 -0700602 new_entry->value.copyFrom_dtoh(map_entry->value);
603 status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value);
604 if (err != NO_ERROR) {
605 LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.",
606 new_entry->value.dataType, new_entry->value.data, new_key);
607 return nullptr;
608 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700609 ++map_entry;
610 ++new_entry;
611 }
612
613 // Finish the parent entries if they exist.
614 if (parent_entry != parent_entry_end) {
615 // Take the rest of the parent entries as-is.
616 const size_t num_entries_to_copy = parent_entry_end - parent_entry;
617 memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry));
618 new_entry += num_entries_to_copy;
619 }
620
621 // Resize the resulting array to fit.
622 const size_t actual_count = new_entry - new_bag->entries;
623 if (actual_count != max_count) {
George Burgess IV09b119f2017-07-25 15:00:04 -0700624 new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc(
625 new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry)))));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700626 }
627
Adam Lesinski1a1e9c22017-10-13 15:45:34 -0700628 // Combine flags from the parent and our own bag.
629 new_bag->type_spec_flags = entry.type_flags | parent_bag->type_spec_flags;
George Burgess IV09b119f2017-07-25 15:00:04 -0700630 new_bag->entry_count = static_cast<uint32_t>(actual_count);
631 ResolvedBag* result = new_bag.get();
632 cached_bags_[resid] = std::move(new_bag);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700633 return result;
634}
635
Adam Lesinski929d6512017-01-16 19:11:19 -0800636static bool Utf8ToUtf16(const StringPiece& str, std::u16string* out) {
637 ssize_t len =
638 utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false);
639 if (len < 0) {
640 return false;
641 }
642 out->resize(static_cast<size_t>(len));
643 utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(),
644 static_cast<size_t>(len + 1));
645 return true;
646}
647
Adam Lesinski0c405242017-01-13 20:47:26 -0800648uint32_t AssetManager2::GetResourceId(const std::string& resource_name,
649 const std::string& fallback_type,
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800650 const std::string& fallback_package) {
Adam Lesinski929d6512017-01-16 19:11:19 -0800651 StringPiece package_name, type, entry;
652 if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) {
653 return 0u;
654 }
655
656 if (entry.empty()) {
657 return 0u;
658 }
659
660 if (package_name.empty()) {
661 package_name = fallback_package;
662 }
663
664 if (type.empty()) {
665 type = fallback_type;
666 }
667
668 std::u16string type16;
669 if (!Utf8ToUtf16(type, &type16)) {
670 return 0u;
671 }
672
673 std::u16string entry16;
674 if (!Utf8ToUtf16(entry, &entry16)) {
675 return 0u;
676 }
677
678 const StringPiece16 kAttr16 = u"attr";
679 const static std::u16string kAttrPrivate16 = u"^attr-private";
680
681 for (const PackageGroup& package_group : package_groups_) {
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800682 for (const LoadedPackage* package : package_group.packages_) {
Adam Lesinski929d6512017-01-16 19:11:19 -0800683 if (package_name != package->GetPackageName()) {
684 // All packages in the same group are expected to have the same package name.
685 break;
686 }
687
688 uint32_t resid = package->FindEntryByName(type16, entry16);
689 if (resid == 0u && kAttr16 == type16) {
690 // Private attributes in libraries (such as the framework) are sometimes encoded
691 // under the type '^attr-private' in order to leave the ID space of public 'attr'
692 // free for future additions. Check '^attr-private' for the same name.
693 resid = package->FindEntryByName(kAttrPrivate16, entry16);
694 }
695
696 if (resid != 0u) {
697 return fix_package_id(resid, package_group.dynamic_ref_table.mAssignedPackageId);
698 }
699 }
700 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800701 return 0u;
702}
703
Adam Lesinski7ad11102016-10-28 16:39:15 -0700704void AssetManager2::InvalidateCaches(uint32_t diff) {
705 if (diff == 0xffffffffu) {
706 // Everything must go.
707 cached_bags_.clear();
708 return;
709 }
710
711 // Be more conservative with what gets purged. Only if the bag has other possible
712 // variations with respect to what changed (diff) should we remove it.
713 for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) {
714 if (diff & iter->second->type_spec_flags) {
715 iter = cached_bags_.erase(iter);
716 } else {
717 ++iter;
718 }
719 }
720}
721
Adam Lesinski30080e22017-10-16 16:18:09 -0700722std::unique_ptr<Theme> AssetManager2::NewTheme() {
723 return std::unique_ptr<Theme>(new Theme(this));
724}
725
726Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) {
727}
728
729Theme::~Theme() = default;
730
731namespace {
732
733struct ThemeEntry {
734 ApkAssetsCookie cookie;
735 uint32_t type_spec_flags;
736 Res_value value;
737};
738
739struct ThemeType {
740 int entry_count;
741 ThemeEntry entries[0];
742};
743
744constexpr size_t kTypeCount = std::numeric_limits<uint8_t>::max() + 1;
745
746} // namespace
747
748struct Theme::Package {
749 // Each element of Type will be a dynamically sized object
750 // allocated to have the entries stored contiguously with the Type.
751 std::array<util::unique_cptr<ThemeType>, kTypeCount> types;
752};
Adam Lesinski7ad11102016-10-28 16:39:15 -0700753
754bool Theme::ApplyStyle(uint32_t resid, bool force) {
755 ATRACE_CALL();
756
757 const ResolvedBag* bag = asset_manager_->GetBag(resid);
758 if (bag == nullptr) {
759 return false;
760 }
761
762 // Merge the flags from this style.
763 type_spec_flags_ |= bag->type_spec_flags;
764
Adam Lesinski30080e22017-10-16 16:18:09 -0700765 int last_type_idx = -1;
766 int last_package_idx = -1;
767 Package* last_package = nullptr;
768 ThemeType* last_type = nullptr;
769
770 // Iterate backwards, because each bag is sorted in ascending key ID order, meaning we will only
771 // need to perform one resize per type.
772 using reverse_bag_iterator = std::reverse_iterator<const ResolvedBag::Entry*>;
773 const auto bag_iter_end = reverse_bag_iterator(begin(bag));
774 for (auto bag_iter = reverse_bag_iterator(end(bag)); bag_iter != bag_iter_end; ++bag_iter) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700775 const uint32_t attr_resid = bag_iter->key;
776
Adam Lesinski30080e22017-10-16 16:18:09 -0700777 // If the resource ID passed in is not a style, the key can be some other identifier that is not
778 // a resource ID. We should fail fast instead of operating with strange resource IDs.
Adam Lesinski929d6512017-01-16 19:11:19 -0800779 if (!is_valid_resid(attr_resid)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700780 return false;
781 }
782
Adam Lesinski30080e22017-10-16 16:18:09 -0700783 // We don't use the 0-based index for the type so that we can avoid doing ID validation
784 // upon lookup. Instead, we keep space for the type ID 0 in our data structures. Since
785 // the construction of this type is guarded with a resource ID check, it will never be
786 // populated, and querying type ID 0 will always fail.
787 const int package_idx = get_package_id(attr_resid);
788 const int type_idx = get_type_id(attr_resid);
789 const int entry_idx = get_entry_id(attr_resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700790
Adam Lesinski30080e22017-10-16 16:18:09 -0700791 if (last_package_idx != package_idx) {
792 std::unique_ptr<Package>& package = packages_[package_idx];
793 if (package == nullptr) {
794 package.reset(new Package());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700795 }
Adam Lesinski30080e22017-10-16 16:18:09 -0700796 last_package_idx = package_idx;
797 last_package = package.get();
798 last_type_idx = -1;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700799 }
Adam Lesinski30080e22017-10-16 16:18:09 -0700800
801 if (last_type_idx != type_idx) {
802 util::unique_cptr<ThemeType>& type = last_package->types[type_idx];
803 if (type == nullptr) {
804 // Allocate enough memory to contain this entry_idx. Since we're iterating in reverse over
805 // a sorted list of attributes, this shouldn't be resized again during this method call.
806 type.reset(reinterpret_cast<ThemeType*>(
807 calloc(sizeof(ThemeType) + (entry_idx + 1) * sizeof(ThemeEntry), 1)));
808 type->entry_count = entry_idx + 1;
809 } else if (entry_idx >= type->entry_count) {
810 // Reallocate the memory to contain this entry_idx. Since we're iterating in reverse over
811 // a sorted list of attributes, this shouldn't be resized again during this method call.
812 const int new_count = entry_idx + 1;
813 type.reset(reinterpret_cast<ThemeType*>(
814 realloc(type.release(), sizeof(ThemeType) + (new_count * sizeof(ThemeEntry)))));
815
816 // Clear out the newly allocated space (which isn't zeroed).
817 memset(type->entries + type->entry_count, 0,
818 (new_count - type->entry_count) * sizeof(ThemeEntry));
819 type->entry_count = new_count;
820 }
821 last_type_idx = type_idx;
822 last_type = type.get();
823 }
824
825 ThemeEntry& entry = last_type->entries[entry_idx];
826 if (force || (entry.value.dataType == Res_value::TYPE_NULL &&
827 entry.value.data != Res_value::DATA_NULL_EMPTY)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700828 entry.cookie = bag_iter->cookie;
829 entry.type_spec_flags |= bag->type_spec_flags;
830 entry.value = bag_iter->value;
831 }
832 }
833 return true;
834}
835
836ApkAssetsCookie Theme::GetAttribute(uint32_t resid, Res_value* out_value,
837 uint32_t* out_flags) const {
Adam Lesinski30080e22017-10-16 16:18:09 -0700838 int cnt = 20;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700839
840 uint32_t type_spec_flags = 0u;
841
Adam Lesinski30080e22017-10-16 16:18:09 -0700842 do {
843 const int package_idx = get_package_id(resid);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700844 const Package* package = packages_[package_idx].get();
Adam Lesinski30080e22017-10-16 16:18:09 -0700845 if (package != nullptr) {
846 // The themes are constructed with a 1-based type ID, so no need to decrement here.
847 const int type_idx = get_type_id(resid);
848 const ThemeType* type = package->types[type_idx].get();
849 if (type != nullptr) {
850 const int entry_idx = get_entry_id(resid);
851 if (entry_idx < type->entry_count) {
852 const ThemeEntry& entry = type->entries[entry_idx];
853 type_spec_flags |= entry.type_spec_flags;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700854
Adam Lesinski30080e22017-10-16 16:18:09 -0700855 if (entry.value.dataType == Res_value::TYPE_ATTRIBUTE) {
856 if (cnt > 0) {
857 cnt--;
858 resid = entry.value.data;
859 continue;
860 }
861 return kInvalidCookie;
862 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700863
Adam Lesinski30080e22017-10-16 16:18:09 -0700864 // @null is different than @empty.
865 if (entry.value.dataType == Res_value::TYPE_NULL &&
866 entry.value.data != Res_value::DATA_NULL_EMPTY) {
867 return kInvalidCookie;
868 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700869
Adam Lesinski30080e22017-10-16 16:18:09 -0700870 *out_value = entry.value;
Adam Lesinskida431a22016-12-29 16:08:16 -0500871 *out_flags = type_spec_flags;
Adam Lesinski30080e22017-10-16 16:18:09 -0700872 return entry.cookie;
Adam Lesinskida431a22016-12-29 16:08:16 -0500873 }
Adam Lesinskida431a22016-12-29 16:08:16 -0500874 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700875 }
Adam Lesinski30080e22017-10-16 16:18:09 -0700876 break;
877 } while (true);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700878 return kInvalidCookie;
879}
880
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800881ApkAssetsCookie Theme::ResolveAttributeReference(ApkAssetsCookie cookie, Res_value* in_out_value,
882 ResTable_config* in_out_selected_config,
883 uint32_t* in_out_type_spec_flags,
Adam Lesinskib8b3a262018-02-09 11:01:45 -0800884 uint32_t* out_last_ref) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800885 if (in_out_value->dataType == Res_value::TYPE_ATTRIBUTE) {
886 uint32_t new_flags;
887 cookie = GetAttribute(in_out_value->data, in_out_value, &new_flags);
888 if (cookie == kInvalidCookie) {
889 return kInvalidCookie;
890 }
891
892 if (in_out_type_spec_flags != nullptr) {
893 *in_out_type_spec_flags |= new_flags;
894 }
895 }
896 return asset_manager_->ResolveReference(cookie, in_out_value, in_out_selected_config,
897 in_out_type_spec_flags, out_last_ref);
898}
899
Adam Lesinski7ad11102016-10-28 16:39:15 -0700900void Theme::Clear() {
901 type_spec_flags_ = 0u;
902 for (std::unique_ptr<Package>& package : packages_) {
903 package.reset();
904 }
905}
906
907bool Theme::SetTo(const Theme& o) {
908 if (this == &o) {
909 return true;
910 }
911
Adam Lesinski7ad11102016-10-28 16:39:15 -0700912 type_spec_flags_ = o.type_spec_flags_;
913
Adam Lesinski03ebac82017-09-25 13:10:14 -0700914 const bool copy_only_system = asset_manager_ != o.asset_manager_;
915
Adam Lesinskida431a22016-12-29 16:08:16 -0500916 for (size_t p = 0; p < packages_.size(); p++) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700917 const Package* package = o.packages_[p].get();
Adam Lesinski03ebac82017-09-25 13:10:14 -0700918 if (package == nullptr || (copy_only_system && p != 0x01)) {
919 // The other theme doesn't have this package, clear ours.
Adam Lesinski7ad11102016-10-28 16:39:15 -0700920 packages_[p].reset();
921 continue;
922 }
923
Adam Lesinski03ebac82017-09-25 13:10:14 -0700924 if (packages_[p] == nullptr) {
925 // The other theme has this package, but we don't. Make one.
926 packages_[p].reset(new Package());
927 }
928
Adam Lesinskida431a22016-12-29 16:08:16 -0500929 for (size_t t = 0; t < package->types.size(); t++) {
Adam Lesinski30080e22017-10-16 16:18:09 -0700930 const ThemeType* type = package->types[t].get();
Adam Lesinski7ad11102016-10-28 16:39:15 -0700931 if (type == nullptr) {
Adam Lesinski03ebac82017-09-25 13:10:14 -0700932 // The other theme doesn't have this type, clear ours.
Adam Lesinski7ad11102016-10-28 16:39:15 -0700933 packages_[p]->types[t].reset();
934 continue;
935 }
936
Adam Lesinski03ebac82017-09-25 13:10:14 -0700937 // Create a new type and update it to theirs.
Adam Lesinski30080e22017-10-16 16:18:09 -0700938 const size_t type_alloc_size = sizeof(ThemeType) + (type->entry_count * sizeof(ThemeEntry));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700939 void* copied_data = malloc(type_alloc_size);
940 memcpy(copied_data, type, type_alloc_size);
Adam Lesinski30080e22017-10-16 16:18:09 -0700941 packages_[p]->types[t].reset(reinterpret_cast<ThemeType*>(copied_data));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700942 }
943 }
944 return true;
945}
946
947} // namespace android