Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 22 | #include <iterator> |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 23 | #include <map> |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 24 | #include <set> |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 25 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 26 | #include "android-base/logging.h" |
| 27 | #include "android-base/stringprintf.h" |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 28 | #include "androidfw/ResourceUtils.h" |
Ryan Mitchell | 31b1105 | 2019-06-13 13:47:26 -0700 | [diff] [blame] | 29 | #include "androidfw/Util.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 30 | #include "utils/ByteOrder.h" |
| 31 | #include "utils/Trace.h" |
| 32 | |
| 33 | #ifdef _WIN32 |
| 34 | #ifdef ERROR |
| 35 | #undef ERROR |
| 36 | #endif |
| 37 | #endif |
| 38 | |
| 39 | namespace android { |
| 40 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 41 | struct FindEntryResult { |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame^] | 42 | // A pointer to the value of the resource table entry. |
| 43 | std::variant<Res_value, const ResTable_map_entry*> entry; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 44 | |
| 45 | // The configuration for which the resulting entry was defined. This is already swapped to host |
| 46 | // endianness. |
| 47 | ResTable_config config; |
| 48 | |
| 49 | // The bitmask of configuration axis with which the resource value varies. |
| 50 | uint32_t type_flags; |
| 51 | |
| 52 | // The dynamic package ID map for the package from which this resource came from. |
| 53 | const DynamicRefTable* dynamic_ref_table; |
| 54 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 55 | // The package name of the resource. |
| 56 | const std::string* package_name; |
| 57 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 58 | // The string pool reference to the type's name. This uses a different string pool than |
| 59 | // the global string pool, but this is hidden from the caller. |
| 60 | StringPoolRef type_string_ref; |
| 61 | |
| 62 | // The string pool reference to the entry's name. This uses a different string pool than |
| 63 | // the global string pool, but this is hidden from the caller. |
| 64 | StringPoolRef entry_string_ref; |
| 65 | }; |
| 66 | |
Ryan Mitchell | b894c27 | 2020-02-12 10:31:44 -0800 | [diff] [blame] | 67 | AssetManager2::AssetManager2() { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 68 | memset(&configuration_, 0, sizeof(configuration_)); |
| 69 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 70 | |
| 71 | bool AssetManager2::SetApkAssets(const std::vector<const ApkAssets*>& apk_assets, |
Mårten Kongstad | 668ec5b | 2018-06-11 14:11:33 +0200 | [diff] [blame] | 72 | bool invalidate_caches, bool filter_incompatible_configs) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 73 | apk_assets_ = apk_assets; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 74 | BuildDynamicRefTable(); |
Mårten Kongstad | 668ec5b | 2018-06-11 14:11:33 +0200 | [diff] [blame] | 75 | RebuildFilterList(filter_incompatible_configs); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 76 | if (invalidate_caches) { |
| 77 | InvalidateCaches(static_cast<uint32_t>(-1)); |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 82 | void AssetManager2::BuildDynamicRefTable() { |
| 83 | package_groups_.clear(); |
| 84 | package_ids_.fill(0xff); |
| 85 | |
Ryan Mitchell | b894c27 | 2020-02-12 10:31:44 -0800 | [diff] [blame] | 86 | // A mapping from apk assets path to the runtime package id of its first loaded package. |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 87 | std::unordered_map<std::string, uint8_t> apk_assets_package_ids; |
| 88 | |
Ryan Mitchell | 824cc49 | 2020-02-12 10:48:14 -0800 | [diff] [blame] | 89 | // Overlay resources are not directly referenced by an application so their resource ids |
| 90 | // can change throughout the application's lifetime. Assign overlay package ids last. |
| 91 | std::vector<const ApkAssets*> sorted_apk_assets(apk_assets_); |
| 92 | std::stable_partition(sorted_apk_assets.begin(), sorted_apk_assets.end(), [](const ApkAssets* a) { |
| 93 | return !a->IsOverlay(); |
| 94 | }); |
| 95 | |
| 96 | // The assets cookie must map to the position of the apk assets in the unsorted apk assets list. |
| 97 | std::unordered_map<const ApkAssets*, ApkAssetsCookie> apk_assets_cookies; |
| 98 | apk_assets_cookies.reserve(apk_assets_.size()); |
| 99 | for (size_t i = 0, n = apk_assets_.size(); i < n; i++) { |
| 100 | apk_assets_cookies[apk_assets_[i]] = static_cast<ApkAssetsCookie>(i); |
| 101 | } |
| 102 | |
Ryan Mitchell | b894c27 | 2020-02-12 10:31:44 -0800 | [diff] [blame] | 103 | // 0x01 is reserved for the android package. |
| 104 | int next_package_id = 0x02; |
Ryan Mitchell | 824cc49 | 2020-02-12 10:48:14 -0800 | [diff] [blame] | 105 | for (const ApkAssets* apk_assets : sorted_apk_assets) { |
Ryan Mitchell | b894c27 | 2020-02-12 10:31:44 -0800 | [diff] [blame] | 106 | const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc(); |
Ryan Mitchell | b894c27 | 2020-02-12 10:31:44 -0800 | [diff] [blame] | 107 | for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) { |
| 108 | // Get the package ID or assign one if a shared library. |
| 109 | int package_id; |
| 110 | if (package->IsDynamic()) { |
| 111 | package_id = next_package_id++; |
| 112 | } else { |
| 113 | package_id = package->GetPackageId(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Add the mapping for package ID to index if not present. |
| 117 | uint8_t idx = package_ids_[package_id]; |
| 118 | if (idx == 0xff) { |
| 119 | package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size()); |
| 120 | package_groups_.push_back({}); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 121 | |
| 122 | if (apk_assets->IsOverlay()) { |
| 123 | // The target package must precede the overlay package in the apk assets paths in order |
| 124 | // to take effect. |
| 125 | const auto& loaded_idmap = apk_assets->GetLoadedIdmap(); |
| 126 | auto target_package_iter = apk_assets_package_ids.find(loaded_idmap->TargetApkPath()); |
Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 127 | if (target_package_iter == apk_assets_package_ids.end()) { |
| 128 | LOG(INFO) << "failed to find target package for overlay " |
| 129 | << loaded_idmap->OverlayApkPath(); |
| 130 | } else { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 131 | const uint8_t target_package_id = target_package_iter->second; |
| 132 | const uint8_t target_idx = package_ids_[target_package_id]; |
| 133 | CHECK(target_idx != 0xff) << "overlay added to apk_assets_package_ids but does not" |
| 134 | << " have an assigned package group"; |
| 135 | |
| 136 | PackageGroup& target_package_group = package_groups_[target_idx]; |
| 137 | |
Ryan Mitchell | 824cc49 | 2020-02-12 10:48:14 -0800 | [diff] [blame] | 138 | // Create a special dynamic reference table for the overlay to rewrite references to |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 139 | // overlay resources as references to the target resources they overlay. |
| 140 | auto overlay_table = std::make_shared<OverlayDynamicRefTable>( |
| 141 | loaded_idmap->GetOverlayDynamicRefTable(target_package_id)); |
| 142 | package_groups_.back().dynamic_ref_table = overlay_table; |
| 143 | |
| 144 | // Add the overlay resource map to the target package's set of overlays. |
| 145 | target_package_group.overlays_.push_back( |
| 146 | ConfiguredOverlay{loaded_idmap->GetTargetResourcesMap(target_package_id, |
| 147 | overlay_table.get()), |
Ryan Mitchell | 824cc49 | 2020-02-12 10:48:14 -0800 | [diff] [blame] | 148 | apk_assets_cookies[apk_assets]}); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | DynamicRefTable* ref_table = package_groups_.back().dynamic_ref_table.get(); |
| 153 | ref_table->mAssignedPackageId = package_id; |
| 154 | ref_table->mAppAsLib = package->IsDynamic() && package->GetPackageId() == 0x7f; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 155 | } |
| 156 | PackageGroup* package_group = &package_groups_[idx]; |
| 157 | |
| 158 | // Add the package and to the set of packages with the same ID. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 159 | package_group->packages_.push_back(ConfiguredPackage{package.get(), {}}); |
Ryan Mitchell | 824cc49 | 2020-02-12 10:48:14 -0800 | [diff] [blame] | 160 | package_group->cookies_.push_back(apk_assets_cookies[apk_assets]); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 161 | |
| 162 | // Add the package name -> build time ID mappings. |
| 163 | for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) { |
| 164 | String16 package_name(entry.package_name.c_str(), entry.package_name.size()); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 165 | package_group->dynamic_ref_table->mEntries.replaceValueFor( |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 166 | package_name, static_cast<uint8_t>(entry.package_id)); |
| 167 | } |
Ryan Mitchell | b894c27 | 2020-02-12 10:31:44 -0800 | [diff] [blame] | 168 | |
| 169 | apk_assets_package_ids.insert(std::make_pair(apk_assets->GetPath(), package_id)); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | // Now assign the runtime IDs so that we have a build-time to runtime ID map. |
| 174 | const auto package_groups_end = package_groups_.end(); |
| 175 | for (auto iter = package_groups_.begin(); iter != package_groups_end; ++iter) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 176 | const std::string& package_name = iter->packages_[0].loaded_package_->GetPackageName(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 177 | for (auto iter2 = package_groups_.begin(); iter2 != package_groups_end; ++iter2) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 178 | iter2->dynamic_ref_table->addMapping(String16(package_name.c_str(), package_name.size()), |
| 179 | iter->dynamic_ref_table->mAssignedPackageId); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void AssetManager2::DumpToLog() const { |
| 185 | base::ScopedLogSeverity _log(base::INFO); |
| 186 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 187 | LOG(INFO) << base::StringPrintf("AssetManager2(this=%p)", this); |
| 188 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 189 | std::string list; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 190 | for (const auto& apk_assets : apk_assets_) { |
| 191 | base::StringAppendF(&list, "%s,", apk_assets->GetPath().c_str()); |
| 192 | } |
| 193 | LOG(INFO) << "ApkAssets: " << list; |
| 194 | |
| 195 | list = ""; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 196 | for (size_t i = 0; i < package_ids_.size(); i++) { |
| 197 | if (package_ids_[i] != 0xff) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 198 | base::StringAppendF(&list, "%02x -> %d, ", (int)i, package_ids_[i]); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | LOG(INFO) << "Package ID map: " << list; |
| 202 | |
Adam Lesinski | 0dd3699 | 2018-01-25 15:38:38 -0800 | [diff] [blame] | 203 | for (const auto& package_group: package_groups_) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 204 | list = ""; |
| 205 | for (const auto& package : package_group.packages_) { |
| 206 | const LoadedPackage* loaded_package = package.loaded_package_; |
| 207 | base::StringAppendF(&list, "%s(%02x%s), ", loaded_package->GetPackageName().c_str(), |
| 208 | loaded_package->GetPackageId(), |
| 209 | (loaded_package->IsDynamic() ? " dynamic" : "")); |
| 210 | } |
| 211 | LOG(INFO) << base::StringPrintf("PG (%02x): ", |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 212 | package_group.dynamic_ref_table->mAssignedPackageId) |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 213 | << list; |
Ryan Mitchell | 5db396d | 2018-11-05 15:56:15 -0800 | [diff] [blame] | 214 | |
| 215 | for (size_t i = 0; i < 256; i++) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 216 | if (package_group.dynamic_ref_table->mLookupTable[i] != 0) { |
Ryan Mitchell | 5db396d | 2018-11-05 15:56:15 -0800 | [diff] [blame] | 217 | LOG(INFO) << base::StringPrintf(" e[0x%02x] -> 0x%02x", (uint8_t) i, |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 218 | package_group.dynamic_ref_table->mLookupTable[i]); |
Ryan Mitchell | 5db396d | 2018-11-05 15:56:15 -0800 | [diff] [blame] | 219 | } |
| 220 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 221 | } |
| 222 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 223 | |
| 224 | const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const { |
| 225 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 226 | return nullptr; |
| 227 | } |
| 228 | return apk_assets_[cookie]->GetLoadedArsc()->GetStringPool(); |
| 229 | } |
| 230 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 231 | const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const { |
| 232 | if (package_id >= package_ids_.size()) { |
| 233 | return nullptr; |
| 234 | } |
| 235 | |
| 236 | const size_t idx = package_ids_[package_id]; |
| 237 | if (idx == 0xff) { |
| 238 | return nullptr; |
| 239 | } |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 240 | return package_groups_[idx].dynamic_ref_table.get(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 241 | } |
| 242 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 243 | std::shared_ptr<const DynamicRefTable> AssetManager2::GetDynamicRefTableForCookie( |
| 244 | ApkAssetsCookie cookie) const { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 245 | for (const PackageGroup& package_group : package_groups_) { |
| 246 | for (const ApkAssetsCookie& package_cookie : package_group.cookies_) { |
| 247 | if (package_cookie == cookie) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 248 | return package_group.dynamic_ref_table; |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | return nullptr; |
| 253 | } |
| 254 | |
Mårten Kongstad | c92c4dd | 2019-02-05 01:29:59 +0100 | [diff] [blame] | 255 | const std::unordered_map<std::string, std::string>* |
| 256 | AssetManager2::GetOverlayableMapForPackage(uint32_t package_id) const { |
| 257 | |
| 258 | if (package_id >= package_ids_.size()) { |
| 259 | return nullptr; |
| 260 | } |
| 261 | |
| 262 | const size_t idx = package_ids_[package_id]; |
| 263 | if (idx == 0xff) { |
| 264 | return nullptr; |
| 265 | } |
| 266 | |
| 267 | const PackageGroup& package_group = package_groups_[idx]; |
| 268 | if (package_group.packages_.size() == 0) { |
| 269 | return nullptr; |
| 270 | } |
| 271 | |
| 272 | const auto loaded_package = package_group.packages_[0].loaded_package_; |
| 273 | return &loaded_package->GetOverlayableMap(); |
| 274 | } |
| 275 | |
Ryan Mitchell | 2e39422 | 2019-08-28 12:10:51 -0700 | [diff] [blame] | 276 | bool AssetManager2::GetOverlayablesToString(const android::StringPiece& package_name, |
| 277 | std::string* out) const { |
| 278 | uint8_t package_id = 0U; |
| 279 | for (const auto& apk_assets : apk_assets_) { |
| 280 | const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc(); |
| 281 | if (loaded_arsc == nullptr) { |
| 282 | continue; |
| 283 | } |
| 284 | |
| 285 | const auto& loaded_packages = loaded_arsc->GetPackages(); |
| 286 | if (loaded_packages.empty()) { |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | const auto& loaded_package = loaded_packages[0]; |
| 291 | if (loaded_package->GetPackageName() == package_name) { |
| 292 | package_id = GetAssignedPackageId(loaded_package.get()); |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (package_id == 0U) { |
| 298 | ANDROID_LOG(ERROR) << base::StringPrintf("No package with name '%s", package_name.data()); |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | const size_t idx = package_ids_[package_id]; |
| 303 | if (idx == 0xff) { |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | std::string output; |
| 308 | for (const ConfiguredPackage& package : package_groups_[idx].packages_) { |
| 309 | const LoadedPackage* loaded_package = package.loaded_package_; |
| 310 | for (auto it = loaded_package->begin(); it != loaded_package->end(); it++) { |
| 311 | const OverlayableInfo* info = loaded_package->GetOverlayableInfo(*it); |
| 312 | if (info != nullptr) { |
| 313 | ResourceName res_name; |
| 314 | if (!GetResourceName(*it, &res_name)) { |
| 315 | ANDROID_LOG(ERROR) << base::StringPrintf( |
| 316 | "Unable to retrieve name of overlayable resource 0x%08x", *it); |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | const std::string name = ToFormattedResourceString(&res_name); |
| 321 | output.append(base::StringPrintf( |
| 322 | "resource='%s' overlayable='%s' actor='%s' policy='0x%08x'\n", |
| 323 | name.c_str(), info->name.c_str(), info->actor.c_str(), info->policy_flags)); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | *out = std::move(output); |
| 329 | return true; |
| 330 | } |
| 331 | |
Ryan Mitchell | 192400c | 2020-04-02 09:54:23 -0700 | [diff] [blame] | 332 | bool AssetManager2::ContainsAllocatedTable() const { |
| 333 | return std::find_if(apk_assets_.begin(), apk_assets_.end(), |
| 334 | std::mem_fn(&ApkAssets::IsTableAllocated)) != apk_assets_.end(); |
| 335 | } |
| 336 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 337 | void AssetManager2::SetConfiguration(const ResTable_config& configuration) { |
| 338 | const int diff = configuration_.diff(configuration); |
| 339 | configuration_ = configuration; |
| 340 | |
| 341 | if (diff) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 342 | RebuildFilterList(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 343 | InvalidateCaches(static_cast<uint32_t>(diff)); |
| 344 | } |
| 345 | } |
| 346 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 347 | std::set<std::string> AssetManager2::GetNonSystemOverlayPaths() const { |
| 348 | std::set<std::string> non_system_overlays; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 349 | for (const PackageGroup& package_group : package_groups_) { |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 350 | bool found_system_package = false; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 351 | for (const ConfiguredPackage& package : package_group.packages_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 352 | if (package.loaded_package_->IsSystem()) { |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 353 | found_system_package = true; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 354 | break; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (!found_system_package) { |
| 359 | for (const ConfiguredOverlay& overlay : package_group.overlays_) { |
| 360 | non_system_overlays.insert(apk_assets_[overlay.cookie]->GetPath()); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | return non_system_overlays; |
| 366 | } |
| 367 | |
| 368 | std::set<ResTable_config> AssetManager2::GetResourceConfigurations(bool exclude_system, |
| 369 | bool exclude_mipmap) const { |
| 370 | ATRACE_NAME("AssetManager::GetResourceConfigurations"); |
| 371 | const auto non_system_overlays = |
| 372 | (exclude_system) ? GetNonSystemOverlayPaths() : std::set<std::string>(); |
| 373 | |
| 374 | std::set<ResTable_config> configurations; |
| 375 | for (const PackageGroup& package_group : package_groups_) { |
| 376 | for (size_t i = 0; i < package_group.packages_.size(); i++) { |
| 377 | const ConfiguredPackage& package = package_group.packages_[i]; |
| 378 | if (exclude_system && package.loaded_package_->IsSystem()) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 379 | continue; |
| 380 | } |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 381 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 382 | auto apk_assets = apk_assets_[package_group.cookies_[i]]; |
| 383 | if (exclude_system && apk_assets->IsOverlay() |
| 384 | && non_system_overlays.find(apk_assets->GetPath()) == non_system_overlays.end()) { |
| 385 | // Exclude overlays that target system resources. |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 386 | continue; |
| 387 | } |
| 388 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 389 | package.loaded_package_->CollectConfigurations(exclude_mipmap, &configurations); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | return configurations; |
| 393 | } |
| 394 | |
| 395 | std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 396 | bool merge_equivalent_languages) const { |
| 397 | ATRACE_NAME("AssetManager::GetResourceLocales"); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 398 | std::set<std::string> locales; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 399 | const auto non_system_overlays = |
| 400 | (exclude_system) ? GetNonSystemOverlayPaths() : std::set<std::string>(); |
| 401 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 402 | for (const PackageGroup& package_group : package_groups_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 403 | for (size_t i = 0; i < package_group.packages_.size(); i++) { |
| 404 | const ConfiguredPackage& package = package_group.packages_[i]; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 405 | if (exclude_system && package.loaded_package_->IsSystem()) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 406 | continue; |
| 407 | } |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 408 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 409 | auto apk_assets = apk_assets_[package_group.cookies_[i]]; |
| 410 | if (exclude_system && apk_assets->IsOverlay() |
| 411 | && non_system_overlays.find(apk_assets->GetPath()) == non_system_overlays.end()) { |
| 412 | // Exclude overlays that target system resources. |
Ryan Mitchell | 449a54f | 2018-11-30 15:22:31 -0800 | [diff] [blame] | 413 | continue; |
| 414 | } |
| 415 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 416 | package.loaded_package_->CollectLocales(merge_equivalent_languages, &locales); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | return locales; |
| 420 | } |
| 421 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 422 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, |
| 423 | Asset::AccessMode mode) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 424 | const std::string new_path = "assets/" + filename; |
| 425 | return OpenNonAsset(new_path, mode); |
| 426 | } |
| 427 | |
| 428 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 429 | Asset::AccessMode mode) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 430 | const std::string new_path = "assets/" + filename; |
| 431 | return OpenNonAsset(new_path, cookie, mode); |
| 432 | } |
| 433 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 434 | std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) const { |
| 435 | ATRACE_NAME("AssetManager::OpenDir"); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 436 | |
| 437 | std::string full_path = "assets/" + dirname; |
| 438 | std::unique_ptr<SortedVector<AssetDir::FileInfo>> files = |
| 439 | util::make_unique<SortedVector<AssetDir::FileInfo>>(); |
| 440 | |
| 441 | // Start from the back. |
| 442 | for (auto iter = apk_assets_.rbegin(); iter != apk_assets_.rend(); ++iter) { |
| 443 | const ApkAssets* apk_assets = *iter; |
Mårten Kongstad | dbf343b | 2019-02-21 07:54:18 +0100 | [diff] [blame] | 444 | if (apk_assets->IsOverlay()) { |
| 445 | continue; |
| 446 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 447 | |
| 448 | auto func = [&](const StringPiece& name, FileType type) { |
| 449 | AssetDir::FileInfo info; |
| 450 | info.setFileName(String8(name.data(), name.size())); |
| 451 | info.setFileType(type); |
| 452 | info.setSourceName(String8(apk_assets->GetPath().c_str())); |
| 453 | files->add(info); |
| 454 | }; |
| 455 | |
Ryan Mitchell | c07aa70 | 2020-03-10 13:49:12 -0700 | [diff] [blame] | 456 | if (!apk_assets->GetAssetsProvider()->ForEachFile(full_path, func)) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 457 | return {}; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>(); |
| 462 | asset_dir->setFileList(files.release()); |
| 463 | return asset_dir; |
| 464 | } |
| 465 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 466 | // Search in reverse because that's how we used to do it and we need to preserve behaviour. |
| 467 | // This is unfortunate, because ClassLoaders delegate to the parent first, so the order |
| 468 | // is inconsistent for split APKs. |
| 469 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
| 470 | Asset::AccessMode mode, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 471 | ApkAssetsCookie* out_cookie) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 472 | for (int32_t i = apk_assets_.size() - 1; i >= 0; i--) { |
Mårten Kongstad | dbf343b | 2019-02-21 07:54:18 +0100 | [diff] [blame] | 473 | // Prevent RRO from modifying assets and other entries accessed by file |
| 474 | // path. Explicitly asking for a path in a given package (denoted by a |
| 475 | // cookie) is still OK. |
| 476 | if (apk_assets_[i]->IsOverlay()) { |
| 477 | continue; |
| 478 | } |
| 479 | |
Ryan Mitchell | c07aa70 | 2020-03-10 13:49:12 -0700 | [diff] [blame] | 480 | std::unique_ptr<Asset> asset = apk_assets_[i]->GetAssetsProvider()->Open(filename, mode); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 481 | if (asset) { |
| 482 | if (out_cookie != nullptr) { |
| 483 | *out_cookie = i; |
| 484 | } |
| 485 | return asset; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if (out_cookie != nullptr) { |
| 490 | *out_cookie = kInvalidCookie; |
| 491 | } |
| 492 | return {}; |
| 493 | } |
| 494 | |
| 495 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 496 | ApkAssetsCookie cookie, |
| 497 | Asset::AccessMode mode) const { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 498 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 499 | return {}; |
| 500 | } |
Ryan Mitchell | c07aa70 | 2020-03-10 13:49:12 -0700 | [diff] [blame] | 501 | return apk_assets_[cookie]->GetAssetsProvider()->Open(filename, mode); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | ApkAssetsCookie AssetManager2::FindEntry(uint32_t resid, uint16_t density_override, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 505 | bool /*stop_at_first_match*/, |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 506 | bool ignore_configuration, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 507 | FindEntryResult* out_entry) const { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 508 | if (resource_resolution_logging_enabled_) { |
| 509 | // Clear the last logged resource resolution. |
| 510 | ResetResourceResolution(); |
| 511 | last_resolution_.resid = resid; |
| 512 | } |
| 513 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 514 | // Might use this if density_override != 0. |
| 515 | ResTable_config density_override_config; |
| 516 | |
| 517 | // Select our configuration or generate a density override configuration. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 518 | const ResTable_config* desired_config = &configuration_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 519 | if (density_override != 0 && density_override != configuration_.density) { |
| 520 | density_override_config = configuration_; |
| 521 | density_override_config.density = density_override; |
| 522 | desired_config = &density_override_config; |
| 523 | } |
| 524 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 525 | // Retrieve the package group from the package id of the resource id. |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 526 | if (!is_valid_resid(resid)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 527 | LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid); |
| 528 | return kInvalidCookie; |
| 529 | } |
| 530 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 531 | const uint32_t package_id = get_package_id(resid); |
| 532 | const uint8_t type_idx = get_type_id(resid) - 1; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 533 | const uint16_t entry_idx = get_entry_id(resid); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 534 | uint8_t package_idx = package_ids_[package_id]; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 535 | if (package_idx == 0xff) { |
Ryan Mitchell | 2fe2347 | 2019-02-27 09:43:01 -0800 | [diff] [blame] | 536 | ANDROID_LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.", |
| 537 | package_id, resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 538 | return kInvalidCookie; |
| 539 | } |
| 540 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 541 | const PackageGroup& package_group = package_groups_[package_idx]; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 542 | ApkAssetsCookie cookie = FindEntryInternal(package_group, type_idx, entry_idx, *desired_config, |
| 543 | false /* stop_at_first_match */, |
| 544 | ignore_configuration, out_entry); |
| 545 | if (UNLIKELY(cookie == kInvalidCookie)) { |
| 546 | return kInvalidCookie; |
| 547 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 548 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 549 | if (!apk_assets_[cookie]->IsLoader()) { |
| 550 | for (const auto& id_map : package_group.overlays_) { |
| 551 | auto overlay_entry = id_map.overlay_res_maps_.Lookup(resid); |
| 552 | if (!overlay_entry) { |
| 553 | // No id map entry exists for this target resource. |
| 554 | continue; |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame^] | 555 | } else if (overlay_entry.IsInlineValue()) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 556 | // The target resource is overlaid by an inline value not represented by a resource. |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame^] | 557 | out_entry->entry = overlay_entry.GetInlineValue(); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 558 | out_entry->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable(); |
| 559 | cookie = id_map.cookie; |
| 560 | continue; |
| 561 | } |
| 562 | |
| 563 | FindEntryResult overlay_result; |
| 564 | ApkAssetsCookie overlay_cookie = FindEntry(overlay_entry.GetResourceId(), density_override, |
| 565 | false /* stop_at_first_match */, |
| 566 | ignore_configuration, &overlay_result); |
| 567 | if (UNLIKELY(overlay_cookie == kInvalidCookie)) { |
| 568 | continue; |
| 569 | } |
| 570 | |
| 571 | if (!overlay_result.config.isBetterThan(out_entry->config, desired_config) |
| 572 | && overlay_result.config.compare(out_entry->config) != 0) { |
| 573 | // The configuration of the entry for the overlay must be equal to or better than the target |
| 574 | // configuration to be chosen as the better value. |
| 575 | continue; |
| 576 | } |
| 577 | |
| 578 | cookie = overlay_cookie; |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame^] | 579 | out_entry->entry = overlay_result.entry; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 580 | out_entry->config = overlay_result.config; |
| 581 | out_entry->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable(); |
| 582 | if (resource_resolution_logging_enabled_) { |
| 583 | last_resolution_.steps.push_back( |
| 584 | Resolution::Step{Resolution::Step::Type::OVERLAID, overlay_result.config.toString(), |
Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 585 | overlay_result.package_name}); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | if (resource_resolution_logging_enabled_) { |
| 591 | last_resolution_.cookie = cookie; |
| 592 | last_resolution_.type_string_ref = out_entry->type_string_ref; |
| 593 | last_resolution_.entry_string_ref = out_entry->entry_string_ref; |
| 594 | } |
| 595 | |
| 596 | return cookie; |
| 597 | } |
| 598 | |
| 599 | ApkAssetsCookie AssetManager2::FindEntryInternal(const PackageGroup& package_group, |
| 600 | uint8_t type_idx, uint16_t entry_idx, |
| 601 | const ResTable_config& desired_config, |
| 602 | bool /*stop_at_first_match*/, |
| 603 | bool ignore_configuration, |
| 604 | FindEntryResult* out_entry) const { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 605 | ApkAssetsCookie best_cookie = kInvalidCookie; |
| 606 | const LoadedPackage* best_package = nullptr; |
| 607 | const ResTable_type* best_type = nullptr; |
| 608 | const ResTable_config* best_config = nullptr; |
| 609 | ResTable_config best_config_copy; |
| 610 | uint32_t best_offset = 0u; |
| 611 | uint32_t type_flags = 0u; |
| 612 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 613 | Resolution::Step::Type resolution_type = Resolution::Step::Type::NO_ENTRY; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 614 | std::vector<Resolution::Step> resolution_steps; |
| 615 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 616 | // If desired_config is the same as the set configuration, then we can use our filtered list |
| 617 | // and we don't need to match the configurations, since they already matched. |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 618 | const bool use_fast_path = !ignore_configuration && &desired_config == &configuration_; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 619 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 620 | const size_t package_count = package_group.packages_.size(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 621 | for (size_t pi = 0; pi < package_count; pi++) { |
| 622 | const ConfiguredPackage& loaded_package_impl = package_group.packages_[pi]; |
| 623 | const LoadedPackage* loaded_package = loaded_package_impl.loaded_package_; |
| 624 | ApkAssetsCookie cookie = package_group.cookies_[pi]; |
| 625 | |
| 626 | // If the type IDs are offset in this package, we need to take that into account when searching |
| 627 | // for a type. |
| 628 | const TypeSpec* type_spec = loaded_package->GetTypeSpecByTypeIndex(type_idx); |
| 629 | if (UNLIKELY(type_spec == nullptr)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 630 | continue; |
| 631 | } |
| 632 | |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 633 | // If the package is an overlay or custom loader, |
| 634 | // then even configurations that are the same MUST be chosen. |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 635 | const bool package_is_loader = loaded_package->IsCustomLoader(); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 636 | type_flags |= type_spec->GetFlagsForEntryIndex(entry_idx); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 637 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 638 | if (use_fast_path) { |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 639 | const FilteredConfigGroup& filtered_group = loaded_package_impl.filtered_configs_[type_idx]; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 640 | const std::vector<ResTable_config>& candidate_configs = filtered_group.configurations; |
| 641 | const size_t type_count = candidate_configs.size(); |
| 642 | for (uint32_t i = 0; i < type_count; i++) { |
| 643 | const ResTable_config& this_config = candidate_configs[i]; |
| 644 | |
| 645 | // We can skip calling ResTable_config::match() because we know that all candidate |
| 646 | // configurations that do NOT match have been filtered-out. |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 647 | if (best_config == nullptr) { |
| 648 | resolution_type = Resolution::Step::Type::INITIAL; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 649 | } else if (this_config.isBetterThan(*best_config, &desired_config)) { |
| 650 | resolution_type = (package_is_loader) ? Resolution::Step::Type::BETTER_MATCH_LOADER |
| 651 | : Resolution::Step::Type::BETTER_MATCH; |
| 652 | } else if (package_is_loader && this_config.compare(*best_config) == 0) { |
| 653 | resolution_type = Resolution::Step::Type::OVERLAID_LOADER; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 654 | } else { |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 655 | if (resource_resolution_logging_enabled_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 656 | resolution_type = (package_is_loader) ? Resolution::Step::Type::SKIPPED_LOADER |
| 657 | : Resolution::Step::Type::SKIPPED; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 658 | resolution_steps.push_back(Resolution::Step{resolution_type, |
| 659 | this_config.toString(), |
| 660 | &loaded_package->GetPackageName()}); |
| 661 | } |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 662 | continue; |
| 663 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 664 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 665 | // The configuration matches and is better than the previous selection. |
| 666 | // Find the entry value if it exists for this configuration. |
| 667 | const ResTable_type* type = filtered_group.types[i]; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 668 | const uint32_t offset = LoadedPackage::GetEntryOffset(type, entry_idx); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 669 | if (offset == ResTable_type::NO_ENTRY) { |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 670 | if (resource_resolution_logging_enabled_) { |
| 671 | if (package_is_loader) { |
| 672 | resolution_type = Resolution::Step::Type::NO_ENTRY_LOADER; |
| 673 | } else { |
| 674 | resolution_type = Resolution::Step::Type::NO_ENTRY; |
| 675 | } |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 676 | resolution_steps.push_back(Resolution::Step{resolution_type, |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 677 | this_config.toString(), |
| 678 | &loaded_package->GetPackageName()}); |
| 679 | } |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 680 | continue; |
| 681 | } |
| 682 | |
| 683 | best_cookie = cookie; |
| 684 | best_package = loaded_package; |
| 685 | best_type = type; |
| 686 | best_config = &this_config; |
| 687 | best_offset = offset; |
| 688 | |
| 689 | if (resource_resolution_logging_enabled_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 690 | last_resolution_.steps.push_back(Resolution::Step{resolution_type, |
| 691 | this_config.toString(), |
| 692 | &loaded_package->GetPackageName()}); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | } else { |
| 696 | // This is the slower path, which doesn't use the filtered list of configurations. |
| 697 | // Here we must read the ResTable_config from the mmapped APK, convert it to host endianness |
| 698 | // and fill in any new fields that did not exist when the APK was compiled. |
| 699 | // Furthermore when selecting configurations we can't just record the pointer to the |
| 700 | // ResTable_config, we must copy it. |
| 701 | const auto iter_end = type_spec->types + type_spec->type_count; |
| 702 | for (auto iter = type_spec->types; iter != iter_end; ++iter) { |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 703 | ResTable_config this_config{}; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 704 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 705 | if (!ignore_configuration) { |
| 706 | this_config.copyFromDtoH((*iter)->config); |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 707 | if (!this_config.match(desired_config)) { |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 708 | continue; |
| 709 | } |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 710 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 711 | if (best_config == nullptr) { |
| 712 | resolution_type = Resolution::Step::Type::INITIAL; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 713 | } else if (this_config.isBetterThan(*best_config, &desired_config)) { |
| 714 | resolution_type = (package_is_loader) ? Resolution::Step::Type::BETTER_MATCH_LOADER |
| 715 | : Resolution::Step::Type::BETTER_MATCH; |
| 716 | } else if (package_is_loader && this_config.compare(*best_config) == 0) { |
| 717 | resolution_type = Resolution::Step::Type::OVERLAID_LOADER; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 718 | } else { |
| 719 | continue; |
| 720 | } |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | // The configuration matches and is better than the previous selection. |
| 724 | // Find the entry value if it exists for this configuration. |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 725 | const uint32_t offset = LoadedPackage::GetEntryOffset(*iter, entry_idx); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 726 | if (offset == ResTable_type::NO_ENTRY) { |
| 727 | continue; |
| 728 | } |
| 729 | |
| 730 | best_cookie = cookie; |
| 731 | best_package = loaded_package; |
| 732 | best_type = *iter; |
| 733 | best_config_copy = this_config; |
| 734 | best_config = &best_config_copy; |
| 735 | best_offset = offset; |
| 736 | |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 737 | if (ignore_configuration) { |
| 738 | // Any configuration will suffice, so break. |
| 739 | break; |
| 740 | } |
| 741 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 742 | if (resource_resolution_logging_enabled_) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 743 | last_resolution_.steps.push_back(Resolution::Step{resolution_type, |
| 744 | this_config.toString(), |
| 745 | &loaded_package->GetPackageName()}); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 746 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 751 | if (UNLIKELY(best_cookie == kInvalidCookie)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 752 | return kInvalidCookie; |
| 753 | } |
| 754 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 755 | const ResTable_entry* best_entry = LoadedPackage::GetEntryFromOffset(best_type, best_offset); |
| 756 | if (UNLIKELY(best_entry == nullptr)) { |
| 757 | return kInvalidCookie; |
| 758 | } |
| 759 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame^] | 760 | const uint16_t entry_size = dtohs(best_entry->size); |
| 761 | if (entry_size >= sizeof(ResTable_map_entry) && |
| 762 | (dtohs(best_entry->flags) & ResTable_entry::FLAG_COMPLEX)) { |
| 763 | // The entry represents a bag/map. |
| 764 | out_entry->entry = reinterpret_cast<const ResTable_map_entry*>(best_entry); |
| 765 | } else { |
| 766 | // The entry represents a value. |
| 767 | Res_value value; |
| 768 | value.copyFrom_dtoh(*reinterpret_cast<const Res_value*>( |
| 769 | reinterpret_cast<const uint8_t*>(best_entry) + entry_size)); |
| 770 | out_entry->entry = value; |
| 771 | } |
| 772 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 773 | out_entry->config = *best_config; |
| 774 | out_entry->type_flags = type_flags; |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 775 | out_entry->package_name = &best_package->GetPackageName(); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 776 | out_entry->type_string_ref = StringPoolRef(best_package->GetTypeStringPool(), best_type->id - 1); |
| 777 | out_entry->entry_string_ref = |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 778 | StringPoolRef(best_package->GetKeyStringPool(), best_entry->key.index); |
| 779 | out_entry->dynamic_ref_table = package_group.dynamic_ref_table.get(); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 780 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 781 | return best_cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 784 | void 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 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 792 | void AssetManager2::SetResourceResolutionLoggingEnabled(bool enabled) { |
| 793 | resource_resolution_logging_enabled_ = enabled; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 794 | if (!enabled) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 795 | ResetResourceResolution(); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
| 799 | std::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 Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 805 | auto cookie = last_resolution_.cookie; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 806 | if (cookie == kInvalidCookie) { |
| 807 | LOG(ERROR) << "AssetManager hasn't resolved a resource to read resolution path."; |
| 808 | return std::string(); |
| 809 | } |
| 810 | |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 811 | uint32_t resid = last_resolution_.resid; |
| 812 | std::vector<Resolution::Step>& steps = last_resolution_.steps; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 813 | |
| 814 | ResourceName resource_name; |
| 815 | std::string resource_name_string; |
| 816 | |
| 817 | const LoadedPackage* package = |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 818 | apk_assets_[cookie]->GetLoadedArsc()->GetPackageById(get_package_id(resid)); |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 819 | |
| 820 | if (package != nullptr) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 821 | ToResourceName(last_resolution_.type_string_ref, |
| 822 | last_resolution_.entry_string_ref, |
Ryan Mitchell | 741e96f | 2019-01-23 16:56:51 -0800 | [diff] [blame] | 823 | package->GetPackageName(), |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 824 | &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; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 843 | case Resolution::Step::Type::BETTER_MATCH_LOADER: |
| 844 | prefix = "Found better in loader"; |
| 845 | break; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 846 | case Resolution::Step::Type::OVERLAID: |
| 847 | prefix = "Overlaid"; |
| 848 | break; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 849 | 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; |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 864 | } |
| 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 Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 878 | bool AssetManager2::GetResourceName(uint32_t resid, ResourceName* out_name) const { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 879 | FindEntryResult entry; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 880 | ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */, |
| 881 | true /* stop_at_first_match */, |
| 882 | true /* ignore_configuration */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 883 | if (cookie == kInvalidCookie) { |
| 884 | return false; |
| 885 | } |
| 886 | |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 887 | return ToResourceName(entry.type_string_ref, |
| 888 | entry.entry_string_ref, |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 889 | *entry.package_name, |
Winson | 2f3669b | 2019-01-11 11:28:34 -0800 | [diff] [blame] | 890 | out_name); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 893 | bool AssetManager2::GetResourceFlags(uint32_t resid, uint32_t* out_flags) const { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 894 | FindEntryResult entry; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 895 | ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */, |
| 896 | false /* stop_at_first_match */, |
| 897 | true /* ignore_configuration */, &entry); |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 898 | if (cookie != kInvalidCookie) { |
| 899 | *out_flags = entry.type_flags; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 900 | return true; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 901 | } |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 902 | return false; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | ApkAssetsCookie 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 Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 908 | uint32_t* out_flags) const { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 909 | FindEntryResult entry; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 910 | ApkAssetsCookie cookie = FindEntry(resid, density_override, false /* stop_at_first_match */, |
| 911 | false /* ignore_configuration */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 912 | if (cookie == kInvalidCookie) { |
| 913 | return kInvalidCookie; |
| 914 | } |
| 915 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame^] | 916 | auto result_map_entry = std::get_if<const ResTable_map_entry*>(&entry.entry); |
| 917 | if (result_map_entry != nullptr) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 918 | if (!may_be_bag) { |
| 919 | LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 920 | return kInvalidCookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 921 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 922 | |
| 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 Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 926 | *out_selected_config = entry.config; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 927 | *out_flags = entry.type_flags; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 928 | return cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 931 | // Convert the package ID to the runtime assigned package ID. |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame^] | 932 | *out_value = std::get<Res_value>(entry.entry); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 933 | entry.dynamic_ref_table->lookupResourceValue(out_value); |
| 934 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 935 | *out_selected_config = entry.config; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 936 | *out_flags = entry.type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 937 | return cookie; |
| 938 | } |
| 939 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 940 | ApkAssetsCookie AssetManager2::ResolveReference(ApkAssetsCookie cookie, Res_value* in_out_value, |
| 941 | ResTable_config* in_out_selected_config, |
| 942 | uint32_t* in_out_flags, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 943 | uint32_t* out_last_reference) const { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 944 | constexpr const int kMaxIterations = 20; |
| 945 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 946 | for (size_t iteration = 0u; in_out_value->dataType == Res_value::TYPE_REFERENCE && |
| 947 | in_out_value->data != 0u && iteration < kMaxIterations; |
| 948 | iteration++) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 949 | *out_last_reference = in_out_value->data; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 950 | uint32_t new_flags = 0u; |
| 951 | cookie = GetResource(in_out_value->data, true /*may_be_bag*/, 0u /*density_override*/, |
| 952 | in_out_value, in_out_selected_config, &new_flags); |
| 953 | if (cookie == kInvalidCookie) { |
| 954 | return kInvalidCookie; |
| 955 | } |
| 956 | if (in_out_flags != nullptr) { |
| 957 | *in_out_flags |= new_flags; |
| 958 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 959 | if (*out_last_reference == in_out_value->data) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 960 | // This reference can't be resolved, so exit now and let the caller deal with it. |
| 961 | return cookie; |
| 962 | } |
| 963 | } |
| 964 | return cookie; |
| 965 | } |
| 966 | |
Aurimas Liutikas | 8f004c8 | 2019-01-17 17:20:10 -0800 | [diff] [blame] | 967 | const std::vector<uint32_t> AssetManager2::GetBagResIdStack(uint32_t resid) { |
| 968 | auto cached_iter = cached_bag_resid_stacks_.find(resid); |
| 969 | if (cached_iter != cached_bag_resid_stacks_.end()) { |
| 970 | return cached_iter->second; |
| 971 | } else { |
| 972 | auto found_resids = std::vector<uint32_t>(); |
| 973 | GetBag(resid, found_resids); |
| 974 | // Cache style stacks if they are not already cached. |
| 975 | cached_bag_resid_stacks_[resid] = found_resids; |
| 976 | return found_resids; |
| 977 | } |
| 978 | } |
| 979 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 980 | const ResolvedBag* AssetManager2::GetBag(uint32_t resid) { |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 981 | auto found_resids = std::vector<uint32_t>(); |
Aurimas Liutikas | 8f004c8 | 2019-01-17 17:20:10 -0800 | [diff] [blame] | 982 | auto bag = GetBag(resid, found_resids); |
| 983 | |
| 984 | // Cache style stacks if they are not already cached. |
| 985 | auto cached_iter = cached_bag_resid_stacks_.find(resid); |
| 986 | if (cached_iter == cached_bag_resid_stacks_.end()) { |
| 987 | cached_bag_resid_stacks_[resid] = found_resids; |
| 988 | } |
| 989 | return bag; |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 992 | static bool compare_bag_entries(const ResolvedBag::Entry& entry1, |
| 993 | const ResolvedBag::Entry& entry2) { |
| 994 | return entry1.key < entry2.key; |
| 995 | } |
| 996 | |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 997 | const ResolvedBag* AssetManager2::GetBag(uint32_t resid, std::vector<uint32_t>& child_resids) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 998 | auto cached_iter = cached_bags_.find(resid); |
| 999 | if (cached_iter != cached_bags_.end()) { |
| 1000 | return cached_iter->second.get(); |
| 1001 | } |
| 1002 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 1003 | FindEntryResult entry; |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 1004 | ApkAssetsCookie cookie = FindEntry(resid, 0u /* density_override */, |
| 1005 | false /* stop_at_first_match */, |
| 1006 | false /* ignore_configuration */, |
| 1007 | &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1008 | if (cookie == kInvalidCookie) { |
| 1009 | return nullptr; |
| 1010 | } |
| 1011 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame^] | 1012 | auto result_map_entry = std::get_if<const ResTable_map_entry*>(&entry.entry); |
| 1013 | if (result_map_entry == nullptr) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1014 | // Not a bag, nothing to do. |
| 1015 | return nullptr; |
| 1016 | } |
| 1017 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame^] | 1018 | auto map = reinterpret_cast<const ResTable_map_entry*>(*result_map_entry); |
| 1019 | auto map_entry = reinterpret_cast<const ResTable_map*>( |
| 1020 | reinterpret_cast<const uint8_t*>(map) + map->size); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1021 | const ResTable_map* const map_entry_end = map_entry + dtohl(map->count); |
| 1022 | |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 1023 | // Keep track of ids that have already been seen to prevent infinite loops caused by circular |
| 1024 | // dependencies between bags |
| 1025 | child_resids.push_back(resid); |
| 1026 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1027 | uint32_t parent_resid = dtohl(map->parent.ident); |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1028 | if (parent_resid == 0U || std::find(child_resids.begin(), child_resids.end(), parent_resid) |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 1029 | != child_resids.end()) { |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1030 | // There is no parent or a circular dependency exist, meaning there is nothing to inherit and |
| 1031 | // we can do a simple copy of the entries in the map. |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1032 | const size_t entry_count = map_entry_end - map_entry; |
| 1033 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 1034 | malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))}; |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1035 | |
| 1036 | bool sort_entries = false; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1037 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 1038 | for (; map_entry != map_entry_end; ++map_entry) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1039 | uint32_t new_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1040 | if (!is_internal_resid(new_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1041 | // Attributes, arrays, etc don't have a resource id as the name. They specify |
| 1042 | // other data, which would be wrong to change via a lookup. |
| 1043 | if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1044 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, |
| 1045 | resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1046 | return nullptr; |
| 1047 | } |
| 1048 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1049 | new_entry->cookie = cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1050 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1051 | new_entry->key_pool = nullptr; |
| 1052 | new_entry->type_pool = nullptr; |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 1053 | new_entry->style = resid; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1054 | new_entry->value.copyFrom_dtoh(map_entry->value); |
| 1055 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 1056 | if (err != NO_ERROR) { |
| 1057 | LOG(ERROR) << base::StringPrintf( |
| 1058 | "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType, |
| 1059 | new_entry->value.data, new_key); |
| 1060 | return nullptr; |
| 1061 | } |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1062 | sort_entries = sort_entries || |
| 1063 | (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1064 | ++new_entry; |
| 1065 | } |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1066 | |
| 1067 | if (sort_entries) { |
| 1068 | std::sort(new_bag->entries, new_bag->entries + entry_count, compare_bag_entries); |
| 1069 | } |
| 1070 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 1071 | new_bag->type_spec_flags = entry.type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1072 | new_bag->entry_count = static_cast<uint32_t>(entry_count); |
| 1073 | ResolvedBag* result = new_bag.get(); |
| 1074 | cached_bags_[resid] = std::move(new_bag); |
| 1075 | return result; |
| 1076 | } |
| 1077 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1078 | // In case the parent is a dynamic reference, resolve it. |
| 1079 | entry.dynamic_ref_table->lookupResourceId(&parent_resid); |
| 1080 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1081 | // Get the parent and do a merge of the keys. |
y | 57cd195 | 2018-04-12 14:26:23 -0700 | [diff] [blame] | 1082 | const ResolvedBag* parent_bag = GetBag(parent_resid, child_resids); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1083 | if (parent_bag == nullptr) { |
| 1084 | // Failed to get the parent that should exist. |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1085 | LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid, |
| 1086 | resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1087 | return nullptr; |
| 1088 | } |
| 1089 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1090 | // Create the max possible entries we can make. Once we construct the bag, |
| 1091 | // we will realloc to fit to size. |
| 1092 | const size_t max_count = parent_bag->entry_count + dtohl(map->count); |
George Burgess IV | 09b119f | 2017-07-25 15:00:04 -0700 | [diff] [blame] | 1093 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 1094 | malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1095 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 1096 | |
| 1097 | const ResolvedBag::Entry* parent_entry = parent_bag->entries; |
| 1098 | const ResolvedBag::Entry* const parent_entry_end = parent_entry + parent_bag->entry_count; |
| 1099 | |
| 1100 | // The keys are expected to be in sorted order. Merge the two bags. |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1101 | bool sort_entries = false; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1102 | while (map_entry != map_entry_end && parent_entry != parent_entry_end) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1103 | uint32_t child_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1104 | if (!is_internal_resid(child_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1105 | if (entry.dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1106 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key, |
| 1107 | resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1108 | return nullptr; |
| 1109 | } |
| 1110 | } |
| 1111 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1112 | if (child_key <= parent_entry->key) { |
| 1113 | // Use the child key if it comes before the parent |
| 1114 | // or is equal to the parent (overrides). |
| 1115 | new_entry->cookie = cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1116 | new_entry->key = child_key; |
| 1117 | new_entry->key_pool = nullptr; |
| 1118 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1119 | new_entry->value.copyFrom_dtoh(map_entry->value); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 1120 | new_entry->style = resid; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1121 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 1122 | if (err != NO_ERROR) { |
| 1123 | LOG(ERROR) << base::StringPrintf( |
| 1124 | "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType, |
| 1125 | new_entry->value.data, child_key); |
| 1126 | return nullptr; |
| 1127 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1128 | ++map_entry; |
| 1129 | } else { |
| 1130 | // Take the parent entry as-is. |
| 1131 | memcpy(new_entry, parent_entry, sizeof(*new_entry)); |
| 1132 | } |
| 1133 | |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1134 | sort_entries = sort_entries || |
| 1135 | (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1136 | if (child_key >= parent_entry->key) { |
| 1137 | // Move to the next parent entry if we used it or it was overridden. |
| 1138 | ++parent_entry; |
| 1139 | } |
| 1140 | // Increment to the next entry to fill. |
| 1141 | ++new_entry; |
| 1142 | } |
| 1143 | |
| 1144 | // Finish the child entries if they exist. |
| 1145 | while (map_entry != map_entry_end) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1146 | uint32_t new_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1147 | if (!is_internal_resid(new_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1148 | if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1149 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, |
| 1150 | resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1151 | return nullptr; |
| 1152 | } |
| 1153 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1154 | new_entry->cookie = cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1155 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1156 | new_entry->key_pool = nullptr; |
| 1157 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1158 | new_entry->value.copyFrom_dtoh(map_entry->value); |
Aurimas Liutikas | d42a670 | 2018-11-15 15:48:28 -0800 | [diff] [blame] | 1159 | new_entry->style = resid; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1160 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 1161 | if (err != NO_ERROR) { |
| 1162 | LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", |
| 1163 | new_entry->value.dataType, new_entry->value.data, new_key); |
| 1164 | return nullptr; |
| 1165 | } |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1166 | sort_entries = sort_entries || |
| 1167 | (new_entry != new_bag->entries && (new_entry->key < (new_entry - 1U)->key)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1168 | ++map_entry; |
| 1169 | ++new_entry; |
| 1170 | } |
| 1171 | |
| 1172 | // Finish the parent entries if they exist. |
| 1173 | if (parent_entry != parent_entry_end) { |
| 1174 | // Take the rest of the parent entries as-is. |
| 1175 | const size_t num_entries_to_copy = parent_entry_end - parent_entry; |
| 1176 | memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry)); |
| 1177 | new_entry += num_entries_to_copy; |
| 1178 | } |
| 1179 | |
| 1180 | // Resize the resulting array to fit. |
| 1181 | const size_t actual_count = new_entry - new_bag->entries; |
| 1182 | if (actual_count != max_count) { |
George Burgess IV | 09b119f | 2017-07-25 15:00:04 -0700 | [diff] [blame] | 1183 | new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc( |
| 1184 | new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry))))); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Ryan Mitchell | 155d539 | 2020-02-10 13:35:24 -0800 | [diff] [blame] | 1187 | if (sort_entries) { |
| 1188 | std::sort(new_bag->entries, new_bag->entries + actual_count, compare_bag_entries); |
| 1189 | } |
| 1190 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 1191 | // Combine flags from the parent and our own bag. |
| 1192 | new_bag->type_spec_flags = entry.type_flags | parent_bag->type_spec_flags; |
George Burgess IV | 09b119f | 2017-07-25 15:00:04 -0700 | [diff] [blame] | 1193 | new_bag->entry_count = static_cast<uint32_t>(actual_count); |
| 1194 | ResolvedBag* result = new_bag.get(); |
| 1195 | cached_bags_[resid] = std::move(new_bag); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1196 | return result; |
| 1197 | } |
| 1198 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1199 | static bool Utf8ToUtf16(const StringPiece& str, std::u16string* out) { |
| 1200 | ssize_t len = |
| 1201 | utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false); |
| 1202 | if (len < 0) { |
| 1203 | return false; |
| 1204 | } |
| 1205 | out->resize(static_cast<size_t>(len)); |
| 1206 | utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(), |
| 1207 | static_cast<size_t>(len + 1)); |
| 1208 | return true; |
| 1209 | } |
| 1210 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 1211 | uint32_t AssetManager2::GetResourceId(const std::string& resource_name, |
| 1212 | const std::string& fallback_type, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1213 | const std::string& fallback_package) const { |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1214 | StringPiece package_name, type, entry; |
| 1215 | if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) { |
| 1216 | return 0u; |
| 1217 | } |
| 1218 | |
| 1219 | if (entry.empty()) { |
| 1220 | return 0u; |
| 1221 | } |
| 1222 | |
| 1223 | if (package_name.empty()) { |
| 1224 | package_name = fallback_package; |
| 1225 | } |
| 1226 | |
| 1227 | if (type.empty()) { |
| 1228 | type = fallback_type; |
| 1229 | } |
| 1230 | |
| 1231 | std::u16string type16; |
| 1232 | if (!Utf8ToUtf16(type, &type16)) { |
| 1233 | return 0u; |
| 1234 | } |
| 1235 | |
| 1236 | std::u16string entry16; |
| 1237 | if (!Utf8ToUtf16(entry, &entry16)) { |
| 1238 | return 0u; |
| 1239 | } |
| 1240 | |
| 1241 | const StringPiece16 kAttr16 = u"attr"; |
| 1242 | const static std::u16string kAttrPrivate16 = u"^attr-private"; |
| 1243 | |
| 1244 | for (const PackageGroup& package_group : package_groups_) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1245 | for (const ConfiguredPackage& package_impl : package_group.packages_) { |
| 1246 | const LoadedPackage* package = package_impl.loaded_package_; |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1247 | if (package_name != package->GetPackageName()) { |
| 1248 | // All packages in the same group are expected to have the same package name. |
| 1249 | break; |
| 1250 | } |
| 1251 | |
| 1252 | uint32_t resid = package->FindEntryByName(type16, entry16); |
| 1253 | if (resid == 0u && kAttr16 == type16) { |
| 1254 | // Private attributes in libraries (such as the framework) are sometimes encoded |
| 1255 | // under the type '^attr-private' in order to leave the ID space of public 'attr' |
| 1256 | // free for future additions. Check '^attr-private' for the same name. |
| 1257 | resid = package->FindEntryByName(kAttrPrivate16, entry16); |
| 1258 | } |
| 1259 | |
| 1260 | if (resid != 0u) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 1261 | return fix_package_id(resid, package_group.dynamic_ref_table->mAssignedPackageId); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1262 | } |
| 1263 | } |
| 1264 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 1265 | return 0u; |
| 1266 | } |
| 1267 | |
Mårten Kongstad | 668ec5b | 2018-06-11 14:11:33 +0200 | [diff] [blame] | 1268 | void AssetManager2::RebuildFilterList(bool filter_incompatible_configs) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1269 | for (PackageGroup& group : package_groups_) { |
| 1270 | for (ConfiguredPackage& impl : group.packages_) { |
| 1271 | // Destroy it. |
| 1272 | impl.filtered_configs_.~ByteBucketArray(); |
| 1273 | |
| 1274 | // Re-create it. |
| 1275 | new (&impl.filtered_configs_) ByteBucketArray<FilteredConfigGroup>(); |
| 1276 | |
| 1277 | // Create the filters here. |
| 1278 | impl.loaded_package_->ForEachTypeSpec([&](const TypeSpec* spec, uint8_t type_index) { |
| 1279 | FilteredConfigGroup& group = impl.filtered_configs_.editItemAt(type_index); |
| 1280 | const auto iter_end = spec->types + spec->type_count; |
| 1281 | for (auto iter = spec->types; iter != iter_end; ++iter) { |
| 1282 | ResTable_config this_config; |
| 1283 | this_config.copyFromDtoH((*iter)->config); |
Mårten Kongstad | 668ec5b | 2018-06-11 14:11:33 +0200 | [diff] [blame] | 1284 | if (!filter_incompatible_configs || this_config.match(configuration_)) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1285 | group.configurations.push_back(this_config); |
| 1286 | group.types.push_back(*iter); |
| 1287 | } |
| 1288 | } |
| 1289 | }); |
| 1290 | } |
| 1291 | } |
| 1292 | } |
| 1293 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1294 | void AssetManager2::InvalidateCaches(uint32_t diff) { |
Ryan Mitchell | 2c4d874 | 2019-03-04 09:41:00 -0800 | [diff] [blame] | 1295 | cached_bag_resid_stacks_.clear(); |
| 1296 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1297 | if (diff == 0xffffffffu) { |
| 1298 | // Everything must go. |
| 1299 | cached_bags_.clear(); |
| 1300 | return; |
| 1301 | } |
| 1302 | |
| 1303 | // Be more conservative with what gets purged. Only if the bag has other possible |
| 1304 | // variations with respect to what changed (diff) should we remove it. |
| 1305 | for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) { |
| 1306 | if (diff & iter->second->type_spec_flags) { |
| 1307 | iter = cached_bags_.erase(iter); |
| 1308 | } else { |
| 1309 | ++iter; |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | |
Ryan Mitchell | 2e39422 | 2019-08-28 12:10:51 -0700 | [diff] [blame] | 1314 | uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) const { |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1315 | for (auto& package_group : package_groups_) { |
| 1316 | for (auto& package2 : package_group.packages_) { |
| 1317 | if (package2.loaded_package_ == package) { |
Ryan Mitchell | 8a891d8 | 2019-07-01 09:48:23 -0700 | [diff] [blame] | 1318 | return package_group.dynamic_ref_table->mAssignedPackageId; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1325 | std::unique_ptr<Theme> AssetManager2::NewTheme() { |
| 1326 | return std::unique_ptr<Theme>(new Theme(this)); |
| 1327 | } |
| 1328 | |
| 1329 | Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) { |
| 1330 | } |
| 1331 | |
| 1332 | Theme::~Theme() = default; |
| 1333 | |
| 1334 | namespace { |
| 1335 | |
| 1336 | struct ThemeEntry { |
| 1337 | ApkAssetsCookie cookie; |
| 1338 | uint32_t type_spec_flags; |
| 1339 | Res_value value; |
| 1340 | }; |
| 1341 | |
| 1342 | struct ThemeType { |
| 1343 | int entry_count; |
| 1344 | ThemeEntry entries[0]; |
| 1345 | }; |
| 1346 | |
| 1347 | constexpr size_t kTypeCount = std::numeric_limits<uint8_t>::max() + 1; |
| 1348 | |
| 1349 | } // namespace |
| 1350 | |
| 1351 | struct Theme::Package { |
| 1352 | // Each element of Type will be a dynamically sized object |
| 1353 | // allocated to have the entries stored contiguously with the Type. |
| 1354 | std::array<util::unique_cptr<ThemeType>, kTypeCount> types; |
| 1355 | }; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1356 | |
| 1357 | bool Theme::ApplyStyle(uint32_t resid, bool force) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1358 | ATRACE_NAME("Theme::ApplyStyle"); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1359 | |
| 1360 | const ResolvedBag* bag = asset_manager_->GetBag(resid); |
| 1361 | if (bag == nullptr) { |
| 1362 | return false; |
| 1363 | } |
| 1364 | |
| 1365 | // Merge the flags from this style. |
| 1366 | type_spec_flags_ |= bag->type_spec_flags; |
| 1367 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1368 | int last_type_idx = -1; |
| 1369 | int last_package_idx = -1; |
| 1370 | Package* last_package = nullptr; |
| 1371 | ThemeType* last_type = nullptr; |
| 1372 | |
| 1373 | // Iterate backwards, because each bag is sorted in ascending key ID order, meaning we will only |
| 1374 | // need to perform one resize per type. |
| 1375 | using reverse_bag_iterator = std::reverse_iterator<const ResolvedBag::Entry*>; |
| 1376 | const auto bag_iter_end = reverse_bag_iterator(begin(bag)); |
| 1377 | for (auto bag_iter = reverse_bag_iterator(end(bag)); bag_iter != bag_iter_end; ++bag_iter) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1378 | const uint32_t attr_resid = bag_iter->key; |
| 1379 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1380 | // If the resource ID passed in is not a style, the key can be some other identifier that is not |
| 1381 | // a resource ID. We should fail fast instead of operating with strange resource IDs. |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 1382 | if (!is_valid_resid(attr_resid)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1383 | return false; |
| 1384 | } |
| 1385 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1386 | // We don't use the 0-based index for the type so that we can avoid doing ID validation |
| 1387 | // upon lookup. Instead, we keep space for the type ID 0 in our data structures. Since |
| 1388 | // the construction of this type is guarded with a resource ID check, it will never be |
| 1389 | // populated, and querying type ID 0 will always fail. |
| 1390 | const int package_idx = get_package_id(attr_resid); |
| 1391 | const int type_idx = get_type_id(attr_resid); |
| 1392 | const int entry_idx = get_entry_id(attr_resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1393 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1394 | if (last_package_idx != package_idx) { |
| 1395 | std::unique_ptr<Package>& package = packages_[package_idx]; |
| 1396 | if (package == nullptr) { |
| 1397 | package.reset(new Package()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1398 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1399 | last_package_idx = package_idx; |
| 1400 | last_package = package.get(); |
| 1401 | last_type_idx = -1; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1402 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1403 | |
| 1404 | if (last_type_idx != type_idx) { |
| 1405 | util::unique_cptr<ThemeType>& type = last_package->types[type_idx]; |
| 1406 | if (type == nullptr) { |
| 1407 | // Allocate enough memory to contain this entry_idx. Since we're iterating in reverse over |
| 1408 | // a sorted list of attributes, this shouldn't be resized again during this method call. |
| 1409 | type.reset(reinterpret_cast<ThemeType*>( |
| 1410 | calloc(sizeof(ThemeType) + (entry_idx + 1) * sizeof(ThemeEntry), 1))); |
| 1411 | type->entry_count = entry_idx + 1; |
| 1412 | } else if (entry_idx >= type->entry_count) { |
| 1413 | // Reallocate the memory to contain this entry_idx. Since we're iterating in reverse over |
| 1414 | // a sorted list of attributes, this shouldn't be resized again during this method call. |
| 1415 | const int new_count = entry_idx + 1; |
| 1416 | type.reset(reinterpret_cast<ThemeType*>( |
| 1417 | realloc(type.release(), sizeof(ThemeType) + (new_count * sizeof(ThemeEntry))))); |
| 1418 | |
| 1419 | // Clear out the newly allocated space (which isn't zeroed). |
| 1420 | memset(type->entries + type->entry_count, 0, |
| 1421 | (new_count - type->entry_count) * sizeof(ThemeEntry)); |
| 1422 | type->entry_count = new_count; |
| 1423 | } |
| 1424 | last_type_idx = type_idx; |
| 1425 | last_type = type.get(); |
| 1426 | } |
| 1427 | |
| 1428 | ThemeEntry& entry = last_type->entries[entry_idx]; |
| 1429 | if (force || (entry.value.dataType == Res_value::TYPE_NULL && |
| 1430 | entry.value.data != Res_value::DATA_NULL_EMPTY)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1431 | entry.cookie = bag_iter->cookie; |
| 1432 | entry.type_spec_flags |= bag->type_spec_flags; |
| 1433 | entry.value = bag_iter->value; |
| 1434 | } |
| 1435 | } |
| 1436 | return true; |
| 1437 | } |
| 1438 | |
| 1439 | ApkAssetsCookie Theme::GetAttribute(uint32_t resid, Res_value* out_value, |
| 1440 | uint32_t* out_flags) const { |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1441 | int cnt = 20; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1442 | |
| 1443 | uint32_t type_spec_flags = 0u; |
| 1444 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1445 | do { |
| 1446 | const int package_idx = get_package_id(resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1447 | const Package* package = packages_[package_idx].get(); |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1448 | if (package != nullptr) { |
| 1449 | // The themes are constructed with a 1-based type ID, so no need to decrement here. |
| 1450 | const int type_idx = get_type_id(resid); |
| 1451 | const ThemeType* type = package->types[type_idx].get(); |
| 1452 | if (type != nullptr) { |
| 1453 | const int entry_idx = get_entry_id(resid); |
| 1454 | if (entry_idx < type->entry_count) { |
| 1455 | const ThemeEntry& entry = type->entries[entry_idx]; |
| 1456 | type_spec_flags |= entry.type_spec_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1457 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1458 | if (entry.value.dataType == Res_value::TYPE_ATTRIBUTE) { |
| 1459 | if (cnt > 0) { |
| 1460 | cnt--; |
| 1461 | resid = entry.value.data; |
| 1462 | continue; |
| 1463 | } |
| 1464 | return kInvalidCookie; |
| 1465 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1466 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1467 | // @null is different than @empty. |
| 1468 | if (entry.value.dataType == Res_value::TYPE_NULL && |
| 1469 | entry.value.data != Res_value::DATA_NULL_EMPTY) { |
| 1470 | return kInvalidCookie; |
| 1471 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1472 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1473 | *out_value = entry.value; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1474 | *out_flags = type_spec_flags; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1475 | return entry.cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1476 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 1477 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1478 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 1479 | break; |
| 1480 | } while (true); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1481 | return kInvalidCookie; |
| 1482 | } |
| 1483 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 1484 | ApkAssetsCookie Theme::ResolveAttributeReference(ApkAssetsCookie cookie, Res_value* in_out_value, |
| 1485 | ResTable_config* in_out_selected_config, |
| 1486 | uint32_t* in_out_type_spec_flags, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 1487 | uint32_t* out_last_ref) const { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 1488 | if (in_out_value->dataType == Res_value::TYPE_ATTRIBUTE) { |
| 1489 | uint32_t new_flags; |
| 1490 | cookie = GetAttribute(in_out_value->data, in_out_value, &new_flags); |
| 1491 | if (cookie == kInvalidCookie) { |
| 1492 | return kInvalidCookie; |
| 1493 | } |
| 1494 | |
| 1495 | if (in_out_type_spec_flags != nullptr) { |
| 1496 | *in_out_type_spec_flags |= new_flags; |
| 1497 | } |
| 1498 | } |
| 1499 | return asset_manager_->ResolveReference(cookie, in_out_value, in_out_selected_config, |
| 1500 | in_out_type_spec_flags, out_last_ref); |
| 1501 | } |
| 1502 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1503 | void Theme::Clear() { |
| 1504 | type_spec_flags_ = 0u; |
| 1505 | for (std::unique_ptr<Package>& package : packages_) { |
| 1506 | package.reset(); |
| 1507 | } |
| 1508 | } |
| 1509 | |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1510 | void Theme::SetTo(const Theme& o) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1511 | if (this == &o) { |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1512 | return; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1515 | type_spec_flags_ = o.type_spec_flags_; |
| 1516 | |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1517 | if (asset_manager_ == o.asset_manager_) { |
| 1518 | // The theme comes from the same asset manager so all theme data can be copied exactly |
| 1519 | for (size_t p = 0; p < packages_.size(); p++) { |
| 1520 | const Package *package = o.packages_[p].get(); |
| 1521 | if (package == nullptr) { |
| 1522 | // The other theme doesn't have this package, clear ours. |
| 1523 | packages_[p].reset(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1524 | continue; |
| 1525 | } |
| 1526 | |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1527 | if (packages_[p] == nullptr) { |
| 1528 | // The other theme has this package, but we don't. Make one. |
| 1529 | packages_[p].reset(new Package()); |
| 1530 | } |
| 1531 | |
| 1532 | for (size_t t = 0; t < package->types.size(); t++) { |
| 1533 | const ThemeType *type = package->types[t].get(); |
| 1534 | if (type == nullptr) { |
| 1535 | // The other theme doesn't have this type, clear ours. |
| 1536 | packages_[p]->types[t].reset(); |
| 1537 | continue; |
| 1538 | } |
| 1539 | |
| 1540 | // Create a new type and update it to theirs. |
| 1541 | const size_t type_alloc_size = sizeof(ThemeType) + (type->entry_count * sizeof(ThemeEntry)); |
| 1542 | void *copied_data = malloc(type_alloc_size); |
| 1543 | memcpy(copied_data, type, type_alloc_size); |
| 1544 | packages_[p]->types[t].reset(reinterpret_cast<ThemeType *>(copied_data)); |
| 1545 | } |
| 1546 | } |
| 1547 | } else { |
| 1548 | std::map<ApkAssetsCookie, ApkAssetsCookie> src_to_dest_asset_cookies; |
| 1549 | typedef std::map<int, int> SourceToDestinationRuntimePackageMap; |
| 1550 | std::map<ApkAssetsCookie, SourceToDestinationRuntimePackageMap> src_asset_cookie_id_map; |
| 1551 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1552 | // Determine which ApkAssets are loaded in both theme AssetManagers. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1553 | std::vector<const ApkAssets*> src_assets = o.asset_manager_->GetApkAssets(); |
| 1554 | for (size_t i = 0; i < src_assets.size(); i++) { |
| 1555 | const ApkAssets* src_asset = src_assets[i]; |
| 1556 | |
| 1557 | std::vector<const ApkAssets*> dest_assets = asset_manager_->GetApkAssets(); |
| 1558 | for (size_t j = 0; j < dest_assets.size(); j++) { |
| 1559 | const ApkAssets* dest_asset = dest_assets[j]; |
| 1560 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1561 | // Map the runtime package of the source apk asset to the destination apk asset. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1562 | if (src_asset->GetPath() == dest_asset->GetPath()) { |
| 1563 | const std::vector<std::unique_ptr<const LoadedPackage>>& src_packages = |
| 1564 | src_asset->GetLoadedArsc()->GetPackages(); |
| 1565 | const std::vector<std::unique_ptr<const LoadedPackage>>& dest_packages = |
| 1566 | dest_asset->GetLoadedArsc()->GetPackages(); |
| 1567 | |
| 1568 | SourceToDestinationRuntimePackageMap package_map; |
| 1569 | |
| 1570 | // The source and destination package should have the same number of packages loaded in |
| 1571 | // the same order. |
| 1572 | const size_t N = src_packages.size(); |
| 1573 | CHECK(N == dest_packages.size()) |
| 1574 | << " LoadedArsc " << src_asset->GetPath() << " differs number of packages."; |
| 1575 | for (size_t p = 0; p < N; p++) { |
| 1576 | auto& src_package = src_packages[p]; |
| 1577 | auto& dest_package = dest_packages[p]; |
| 1578 | CHECK(src_package->GetPackageName() == dest_package->GetPackageName()) |
| 1579 | << " Package " << src_package->GetPackageName() << " differs in load order."; |
| 1580 | |
| 1581 | int src_package_id = o.asset_manager_->GetAssignedPackageId(src_package.get()); |
| 1582 | int dest_package_id = asset_manager_->GetAssignedPackageId(dest_package.get()); |
| 1583 | package_map[src_package_id] = dest_package_id; |
| 1584 | } |
| 1585 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1586 | src_to_dest_asset_cookies.insert(std::make_pair(i, j)); |
| 1587 | src_asset_cookie_id_map.insert(std::make_pair(i, package_map)); |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1588 | break; |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1593 | // Reset the data in the destination theme. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1594 | for (size_t p = 0; p < packages_.size(); p++) { |
| 1595 | if (packages_[p] != nullptr) { |
| 1596 | packages_[p].reset(); |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | for (size_t p = 0; p < packages_.size(); p++) { |
| 1601 | const Package *package = o.packages_[p].get(); |
| 1602 | if (package == nullptr) { |
| 1603 | continue; |
| 1604 | } |
| 1605 | |
| 1606 | for (size_t t = 0; t < package->types.size(); t++) { |
| 1607 | const ThemeType *type = package->types[t].get(); |
| 1608 | if (type == nullptr) { |
| 1609 | continue; |
| 1610 | } |
| 1611 | |
| 1612 | for (size_t e = 0; e < type->entry_count; e++) { |
| 1613 | const ThemeEntry &entry = type->entries[e]; |
| 1614 | if (entry.value.dataType == Res_value::TYPE_NULL && |
| 1615 | entry.value.data != Res_value::DATA_NULL_EMPTY) { |
| 1616 | continue; |
| 1617 | } |
| 1618 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1619 | bool is_reference = (entry.value.dataType == Res_value::TYPE_ATTRIBUTE |
| 1620 | || entry.value.dataType == Res_value::TYPE_REFERENCE |
| 1621 | || entry.value.dataType == Res_value::TYPE_DYNAMIC_ATTRIBUTE |
| 1622 | || entry.value.dataType == Res_value::TYPE_DYNAMIC_REFERENCE) |
| 1623 | && entry.value.data != 0x0; |
Ryan Mitchell | b85d9b2 | 2018-11-19 12:11:38 -0800 | [diff] [blame] | 1624 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1625 | // If the attribute value represents an attribute or reference, the package id of the |
| 1626 | // value needs to be rewritten to the package id of the value in the destination. |
| 1627 | uint32_t attribute_data = entry.value.data; |
| 1628 | if (is_reference) { |
| 1629 | // Determine the package id of the reference in the destination AssetManager. |
Ryan Mitchell | b85d9b2 | 2018-11-19 12:11:38 -0800 | [diff] [blame] | 1630 | auto value_package_map = src_asset_cookie_id_map.find(entry.cookie); |
| 1631 | if (value_package_map == src_asset_cookie_id_map.end()) { |
| 1632 | continue; |
| 1633 | } |
| 1634 | |
| 1635 | auto value_dest_package = value_package_map->second.find( |
| 1636 | get_package_id(entry.value.data)); |
| 1637 | if (value_dest_package == value_package_map->second.end()) { |
| 1638 | continue; |
| 1639 | } |
| 1640 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1641 | attribute_data = fix_package_id(entry.value.data, value_dest_package->second); |
| 1642 | } |
| 1643 | |
| 1644 | // Find the cookie of the value in the destination. If the source apk is not loaded in the |
| 1645 | // destination, only copy resources that do not reference resources in the source. |
| 1646 | ApkAssetsCookie data_dest_cookie; |
| 1647 | auto value_dest_cookie = src_to_dest_asset_cookies.find(entry.cookie); |
| 1648 | if (value_dest_cookie != src_to_dest_asset_cookies.end()) { |
| 1649 | data_dest_cookie = value_dest_cookie->second; |
| 1650 | } else { |
| 1651 | if (is_reference || entry.value.dataType == Res_value::TYPE_STRING) { |
| 1652 | continue; |
| 1653 | } else { |
| 1654 | data_dest_cookie = 0x0; |
| 1655 | } |
Ryan Mitchell | b85d9b2 | 2018-11-19 12:11:38 -0800 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | // The package id of the attribute needs to be rewritten to the package id of the |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1659 | // attribute in the destination. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1660 | int attribute_dest_package_id = p; |
| 1661 | if (attribute_dest_package_id != 0x01) { |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1662 | // Find the cookie of the attribute resource id in the source AssetManager |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1663 | FindEntryResult attribute_entry_result; |
| 1664 | ApkAssetsCookie attribute_cookie = |
Ryan Mitchell | a55dc2e | 2019-01-24 10:58:23 -0800 | [diff] [blame] | 1665 | o.asset_manager_->FindEntry(make_resid(p, t, e), 0 /* density_override */ , |
| 1666 | true /* stop_at_first_match */, |
| 1667 | true /* ignore_configuration */, |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1668 | &attribute_entry_result); |
| 1669 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1670 | // Determine the package id of the attribute in the destination AssetManager. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1671 | auto attribute_package_map = src_asset_cookie_id_map.find(attribute_cookie); |
| 1672 | if (attribute_package_map == src_asset_cookie_id_map.end()) { |
| 1673 | continue; |
| 1674 | } |
| 1675 | auto attribute_dest_package = attribute_package_map->second.find( |
| 1676 | attribute_dest_package_id); |
| 1677 | if (attribute_dest_package == attribute_package_map->second.end()) { |
| 1678 | continue; |
| 1679 | } |
| 1680 | attribute_dest_package_id = attribute_dest_package->second; |
| 1681 | } |
| 1682 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1683 | // Lazily instantiate the destination package. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1684 | std::unique_ptr<Package>& dest_package = packages_[attribute_dest_package_id]; |
| 1685 | if (dest_package == nullptr) { |
| 1686 | dest_package.reset(new Package()); |
| 1687 | } |
| 1688 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1689 | // Lazily instantiate and resize the destination type. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1690 | util::unique_cptr<ThemeType>& dest_type = dest_package->types[t]; |
| 1691 | if (dest_type == nullptr || dest_type->entry_count < type->entry_count) { |
| 1692 | const size_t type_alloc_size = sizeof(ThemeType) |
| 1693 | + (type->entry_count * sizeof(ThemeEntry)); |
| 1694 | void* dest_data = malloc(type_alloc_size); |
| 1695 | memset(dest_data, 0, type->entry_count * sizeof(ThemeEntry)); |
| 1696 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1697 | // Copy the existing destination type values if the type is resized. |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1698 | if (dest_type != nullptr) { |
| 1699 | memcpy(dest_data, type, sizeof(ThemeType) |
| 1700 | + (dest_type->entry_count * sizeof(ThemeEntry))); |
| 1701 | } |
| 1702 | |
| 1703 | dest_type.reset(reinterpret_cast<ThemeType *>(dest_data)); |
| 1704 | dest_type->entry_count = type->entry_count; |
| 1705 | } |
| 1706 | |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1707 | dest_type->entries[e].cookie = data_dest_cookie; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1708 | dest_type->entries[e].value.dataType = entry.value.dataType; |
Ryan Mitchell | 93bca97 | 2019-03-08 17:26:28 -0800 | [diff] [blame] | 1709 | dest_type->entries[e].value.data = attribute_data; |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1710 | dest_type->entries[e].type_spec_flags = entry.type_spec_flags; |
| 1711 | } |
| 1712 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1713 | } |
| 1714 | } |
Ryan Mitchell | b3ae42e | 2018-10-16 12:48:38 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | void Theme::Dump() const { |
| 1718 | base::ScopedLogSeverity _log(base::INFO); |
| 1719 | LOG(INFO) << base::StringPrintf("Theme(this=%p, AssetManager2=%p)", this, asset_manager_); |
| 1720 | |
| 1721 | for (int p = 0; p < packages_.size(); p++) { |
| 1722 | auto& package = packages_[p]; |
| 1723 | if (package == nullptr) { |
| 1724 | continue; |
| 1725 | } |
| 1726 | |
| 1727 | for (int t = 0; t < package->types.size(); t++) { |
| 1728 | auto& type = package->types[t]; |
| 1729 | if (type == nullptr) { |
| 1730 | continue; |
| 1731 | } |
| 1732 | |
| 1733 | for (int e = 0; e < type->entry_count; e++) { |
| 1734 | auto& entry = type->entries[e]; |
| 1735 | if (entry.value.dataType == Res_value::TYPE_NULL && |
| 1736 | entry.value.data != Res_value::DATA_NULL_EMPTY) { |
| 1737 | continue; |
| 1738 | } |
| 1739 | |
| 1740 | LOG(INFO) << base::StringPrintf(" entry(0x%08x)=(0x%08x) type=(0x%02x), cookie(%d)", |
| 1741 | make_resid(p, t, e), entry.value.data, |
| 1742 | entry.value.dataType, entry.cookie); |
| 1743 | } |
| 1744 | } |
| 1745 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | } // namespace android |