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