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