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