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