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 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 21 | #include <iterator> |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 22 | #include <set> |
| 23 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 24 | #include "android-base/logging.h" |
| 25 | #include "android-base/stringprintf.h" |
| 26 | #include "utils/ByteOrder.h" |
| 27 | #include "utils/Trace.h" |
| 28 | |
| 29 | #ifdef _WIN32 |
| 30 | #ifdef ERROR |
| 31 | #undef ERROR |
| 32 | #endif |
| 33 | #endif |
| 34 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 35 | #include "androidfw/ResourceUtils.h" |
| 36 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 37 | namespace android { |
| 38 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 39 | AssetManager2::AssetManager2() { |
| 40 | memset(&configuration_, 0, sizeof(configuration_)); |
| 41 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 42 | |
| 43 | bool AssetManager2::SetApkAssets(const std::vector<const ApkAssets*>& apk_assets, |
| 44 | bool invalidate_caches) { |
| 45 | apk_assets_ = apk_assets; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 46 | BuildDynamicRefTable(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 47 | if (invalidate_caches) { |
| 48 | InvalidateCaches(static_cast<uint32_t>(-1)); |
| 49 | } |
| 50 | return true; |
| 51 | } |
| 52 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 53 | void AssetManager2::BuildDynamicRefTable() { |
| 54 | package_groups_.clear(); |
| 55 | package_ids_.fill(0xff); |
| 56 | |
| 57 | // 0x01 is reserved for the android package. |
| 58 | int next_package_id = 0x02; |
| 59 | const size_t apk_assets_count = apk_assets_.size(); |
| 60 | for (size_t i = 0; i < apk_assets_count; i++) { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 61 | const LoadedArsc* loaded_arsc = apk_assets_[i]->GetLoadedArsc(); |
| 62 | |
| 63 | for (const std::unique_ptr<const LoadedPackage>& package : loaded_arsc->GetPackages()) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 64 | // Get the package ID or assign one if a shared library. |
| 65 | int package_id; |
| 66 | if (package->IsDynamic()) { |
| 67 | package_id = next_package_id++; |
| 68 | } else { |
| 69 | package_id = package->GetPackageId(); |
| 70 | } |
| 71 | |
| 72 | // Add the mapping for package ID to index if not present. |
| 73 | uint8_t idx = package_ids_[package_id]; |
| 74 | if (idx == 0xff) { |
| 75 | package_ids_[package_id] = idx = static_cast<uint8_t>(package_groups_.size()); |
| 76 | package_groups_.push_back({}); |
| 77 | package_groups_.back().dynamic_ref_table.mAssignedPackageId = package_id; |
| 78 | } |
| 79 | PackageGroup* package_group = &package_groups_[idx]; |
| 80 | |
| 81 | // Add the package and to the set of packages with the same ID. |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 82 | package_group->packages_.push_back(package.get()); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 83 | package_group->cookies_.push_back(static_cast<ApkAssetsCookie>(i)); |
| 84 | |
| 85 | // Add the package name -> build time ID mappings. |
| 86 | for (const DynamicPackageEntry& entry : package->GetDynamicPackageMap()) { |
| 87 | String16 package_name(entry.package_name.c_str(), entry.package_name.size()); |
| 88 | package_group->dynamic_ref_table.mEntries.replaceValueFor( |
| 89 | package_name, static_cast<uint8_t>(entry.package_id)); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Now assign the runtime IDs so that we have a build-time to runtime ID map. |
| 95 | const auto package_groups_end = package_groups_.end(); |
| 96 | for (auto iter = package_groups_.begin(); iter != package_groups_end; ++iter) { |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 97 | const std::string& package_name = iter->packages_[0]->GetPackageName(); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 98 | for (auto iter2 = package_groups_.begin(); iter2 != package_groups_end; ++iter2) { |
| 99 | iter2->dynamic_ref_table.addMapping(String16(package_name.c_str(), package_name.size()), |
| 100 | iter->dynamic_ref_table.mAssignedPackageId); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void AssetManager2::DumpToLog() const { |
| 106 | base::ScopedLogSeverity _log(base::INFO); |
| 107 | |
| 108 | std::string list; |
| 109 | for (size_t i = 0; i < package_ids_.size(); i++) { |
| 110 | if (package_ids_[i] != 0xff) { |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 111 | base::StringAppendF(&list, "%02x -> %d, ", (int) i, package_ids_[i]); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | LOG(INFO) << "Package ID map: " << list; |
| 115 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 116 | for (const auto& package_group: package_groups_) { |
| 117 | list = ""; |
| 118 | for (const auto& package : package_group.packages_) { |
| 119 | base::StringAppendF(&list, "%s(%02x), ", package->GetPackageName().c_str(), package->GetPackageId()); |
| 120 | } |
| 121 | LOG(INFO) << base::StringPrintf("PG (%02x): ", package_group.dynamic_ref_table.mAssignedPackageId) << list; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 122 | } |
| 123 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 124 | |
| 125 | const ResStringPool* AssetManager2::GetStringPoolForCookie(ApkAssetsCookie cookie) const { |
| 126 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 127 | return nullptr; |
| 128 | } |
| 129 | return apk_assets_[cookie]->GetLoadedArsc()->GetStringPool(); |
| 130 | } |
| 131 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 132 | const DynamicRefTable* AssetManager2::GetDynamicRefTableForPackage(uint32_t package_id) const { |
| 133 | if (package_id >= package_ids_.size()) { |
| 134 | return nullptr; |
| 135 | } |
| 136 | |
| 137 | const size_t idx = package_ids_[package_id]; |
| 138 | if (idx == 0xff) { |
| 139 | return nullptr; |
| 140 | } |
| 141 | return &package_groups_[idx].dynamic_ref_table; |
| 142 | } |
| 143 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 144 | const DynamicRefTable* AssetManager2::GetDynamicRefTableForCookie(ApkAssetsCookie cookie) const { |
| 145 | for (const PackageGroup& package_group : package_groups_) { |
| 146 | for (const ApkAssetsCookie& package_cookie : package_group.cookies_) { |
| 147 | if (package_cookie == cookie) { |
| 148 | return &package_group.dynamic_ref_table; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return nullptr; |
| 153 | } |
| 154 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 155 | void AssetManager2::SetConfiguration(const ResTable_config& configuration) { |
| 156 | const int diff = configuration_.diff(configuration); |
| 157 | configuration_ = configuration; |
| 158 | |
| 159 | if (diff) { |
| 160 | InvalidateCaches(static_cast<uint32_t>(diff)); |
| 161 | } |
| 162 | } |
| 163 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 164 | std::set<ResTable_config> AssetManager2::GetResourceConfigurations(bool exclude_system, |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 165 | bool exclude_mipmap) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 166 | ATRACE_CALL(); |
| 167 | std::set<ResTable_config> configurations; |
| 168 | for (const PackageGroup& package_group : package_groups_) { |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 169 | for (const LoadedPackage* package : package_group.packages_) { |
| 170 | if (exclude_system && package->IsSystem()) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 171 | continue; |
| 172 | } |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 173 | package->CollectConfigurations(exclude_mipmap, &configurations); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | return configurations; |
| 177 | } |
| 178 | |
| 179 | std::set<std::string> AssetManager2::GetResourceLocales(bool exclude_system, |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 180 | bool merge_equivalent_languages) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 181 | ATRACE_CALL(); |
| 182 | std::set<std::string> locales; |
| 183 | for (const PackageGroup& package_group : package_groups_) { |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 184 | for (const LoadedPackage* package : package_group.packages_) { |
| 185 | if (exclude_system && package->IsSystem()) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 186 | continue; |
| 187 | } |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 188 | package->CollectLocales(merge_equivalent_languages, &locales); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | return locales; |
| 192 | } |
| 193 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 194 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, Asset::AccessMode mode) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 195 | const std::string new_path = "assets/" + filename; |
| 196 | return OpenNonAsset(new_path, mode); |
| 197 | } |
| 198 | |
| 199 | std::unique_ptr<Asset> AssetManager2::Open(const std::string& filename, ApkAssetsCookie cookie, |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 200 | Asset::AccessMode mode) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 201 | const std::string new_path = "assets/" + filename; |
| 202 | return OpenNonAsset(new_path, cookie, mode); |
| 203 | } |
| 204 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 205 | std::unique_ptr<AssetDir> AssetManager2::OpenDir(const std::string& dirname) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 206 | ATRACE_CALL(); |
| 207 | |
| 208 | std::string full_path = "assets/" + dirname; |
| 209 | std::unique_ptr<SortedVector<AssetDir::FileInfo>> files = |
| 210 | util::make_unique<SortedVector<AssetDir::FileInfo>>(); |
| 211 | |
| 212 | // Start from the back. |
| 213 | for (auto iter = apk_assets_.rbegin(); iter != apk_assets_.rend(); ++iter) { |
| 214 | const ApkAssets* apk_assets = *iter; |
| 215 | |
| 216 | auto func = [&](const StringPiece& name, FileType type) { |
| 217 | AssetDir::FileInfo info; |
| 218 | info.setFileName(String8(name.data(), name.size())); |
| 219 | info.setFileType(type); |
| 220 | info.setSourceName(String8(apk_assets->GetPath().c_str())); |
| 221 | files->add(info); |
| 222 | }; |
| 223 | |
| 224 | if (!apk_assets->ForEachFile(full_path, func)) { |
| 225 | return {}; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | std::unique_ptr<AssetDir> asset_dir = util::make_unique<AssetDir>(); |
| 230 | asset_dir->setFileList(files.release()); |
| 231 | return asset_dir; |
| 232 | } |
| 233 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 234 | // Search in reverse because that's how we used to do it and we need to preserve behaviour. |
| 235 | // This is unfortunate, because ClassLoaders delegate to the parent first, so the order |
| 236 | // is inconsistent for split APKs. |
| 237 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
| 238 | Asset::AccessMode mode, |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 239 | ApkAssetsCookie* out_cookie) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 240 | ATRACE_CALL(); |
| 241 | for (int32_t i = apk_assets_.size() - 1; i >= 0; i--) { |
| 242 | std::unique_ptr<Asset> asset = apk_assets_[i]->Open(filename, mode); |
| 243 | if (asset) { |
| 244 | if (out_cookie != nullptr) { |
| 245 | *out_cookie = i; |
| 246 | } |
| 247 | return asset; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if (out_cookie != nullptr) { |
| 252 | *out_cookie = kInvalidCookie; |
| 253 | } |
| 254 | return {}; |
| 255 | } |
| 256 | |
| 257 | std::unique_ptr<Asset> AssetManager2::OpenNonAsset(const std::string& filename, |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 258 | ApkAssetsCookie cookie, Asset::AccessMode mode) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 259 | ATRACE_CALL(); |
| 260 | if (cookie < 0 || static_cast<size_t>(cookie) >= apk_assets_.size()) { |
| 261 | return {}; |
| 262 | } |
| 263 | return apk_assets_[cookie]->Open(filename, mode); |
| 264 | } |
| 265 | |
| 266 | ApkAssetsCookie AssetManager2::FindEntry(uint32_t resid, uint16_t density_override, |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 267 | bool stop_at_first_match, FindEntryResult* out_entry) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 268 | // Might use this if density_override != 0. |
| 269 | ResTable_config density_override_config; |
| 270 | |
| 271 | // Select our configuration or generate a density override configuration. |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 272 | ResTable_config* desired_config = &configuration_; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 273 | if (density_override != 0 && density_override != configuration_.density) { |
| 274 | density_override_config = configuration_; |
| 275 | density_override_config.density = density_override; |
| 276 | desired_config = &density_override_config; |
| 277 | } |
| 278 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 279 | if (!is_valid_resid(resid)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 280 | LOG(ERROR) << base::StringPrintf("Invalid ID 0x%08x.", resid); |
| 281 | return kInvalidCookie; |
| 282 | } |
| 283 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 284 | const uint32_t package_id = get_package_id(resid); |
| 285 | const uint8_t type_idx = get_type_id(resid) - 1; |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 286 | const uint16_t entry_id = get_entry_id(resid); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 287 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 288 | const uint8_t idx = package_ids_[package_id]; |
| 289 | if (idx == 0xff) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 290 | LOG(ERROR) << base::StringPrintf("No package ID %02x found for ID 0x%08x.", package_id, resid); |
| 291 | return kInvalidCookie; |
| 292 | } |
| 293 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 294 | FindEntryResult best_entry; |
Adam Lesinski | 3921327 | 2018-01-08 17:38:30 -0800 | [diff] [blame] | 295 | ApkAssetsCookie best_cookie = kInvalidCookie; |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 296 | uint32_t cumulated_flags = 0u; |
Adam Lesinski | 3921327 | 2018-01-08 17:38:30 -0800 | [diff] [blame] | 297 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 298 | const PackageGroup& package_group = package_groups_[idx]; |
| 299 | const size_t package_count = package_group.packages_.size(); |
| 300 | FindEntryResult current_entry; |
| 301 | for (size_t i = 0; i < package_count; i++) { |
| 302 | const LoadedPackage* loaded_package = package_group.packages_[i]; |
| 303 | if (!loaded_package->FindEntry(type_idx, entry_id, *desired_config, ¤t_entry)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 304 | continue; |
| 305 | } |
| 306 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 307 | cumulated_flags |= current_entry.type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 308 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 309 | const ResTable_config* current_config = current_entry.config; |
| 310 | const ResTable_config* best_config = best_entry.config; |
| 311 | if (best_cookie == kInvalidCookie || |
| 312 | current_config->isBetterThan(*best_config, desired_config) || |
| 313 | (loaded_package->IsOverlay() && current_config->compare(*best_config) == 0)) { |
| 314 | best_entry = current_entry; |
| 315 | best_cookie = package_group.cookies_[i]; |
| 316 | if (stop_at_first_match) { |
| 317 | break; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 322 | if (best_cookie == kInvalidCookie) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 323 | return kInvalidCookie; |
| 324 | } |
| 325 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 326 | *out_entry = best_entry; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 327 | out_entry->dynamic_ref_table = &package_group.dynamic_ref_table; |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 328 | out_entry->type_flags = cumulated_flags; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 329 | return best_cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 332 | bool AssetManager2::GetResourceName(uint32_t resid, ResourceName* out_name) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 333 | ATRACE_CALL(); |
| 334 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 335 | FindEntryResult entry; |
| 336 | ApkAssetsCookie cookie = |
| 337 | FindEntry(resid, 0u /* density_override */, true /* stop_at_first_match */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 338 | if (cookie == kInvalidCookie) { |
| 339 | return false; |
| 340 | } |
| 341 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 342 | const LoadedPackage* package = apk_assets_[cookie]->GetLoadedArsc()->GetPackageForId(resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 343 | if (package == nullptr) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 344 | return false; |
| 345 | } |
| 346 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 347 | out_name->package = package->GetPackageName().data(); |
| 348 | out_name->package_len = package->GetPackageName().size(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 349 | |
| 350 | out_name->type = entry.type_string_ref.string8(&out_name->type_len); |
| 351 | out_name->type16 = nullptr; |
| 352 | if (out_name->type == nullptr) { |
| 353 | out_name->type16 = entry.type_string_ref.string16(&out_name->type_len); |
| 354 | if (out_name->type16 == nullptr) { |
| 355 | return false; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | out_name->entry = entry.entry_string_ref.string8(&out_name->entry_len); |
| 360 | out_name->entry16 = nullptr; |
| 361 | if (out_name->entry == nullptr) { |
| 362 | out_name->entry16 = entry.entry_string_ref.string16(&out_name->entry_len); |
| 363 | if (out_name->entry16 == nullptr) { |
| 364 | return false; |
| 365 | } |
| 366 | } |
| 367 | return true; |
| 368 | } |
| 369 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 370 | bool AssetManager2::GetResourceFlags(uint32_t resid, uint32_t* out_flags) { |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 371 | FindEntryResult entry; |
| 372 | ApkAssetsCookie cookie = |
| 373 | FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, &entry); |
| 374 | if (cookie != kInvalidCookie) { |
| 375 | *out_flags = entry.type_flags; |
| 376 | return cookie; |
| 377 | } |
| 378 | return kInvalidCookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | ApkAssetsCookie AssetManager2::GetResource(uint32_t resid, bool may_be_bag, |
| 382 | uint16_t density_override, Res_value* out_value, |
| 383 | ResTable_config* out_selected_config, |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 384 | uint32_t* out_flags) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 385 | ATRACE_CALL(); |
| 386 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 387 | FindEntryResult entry; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 388 | ApkAssetsCookie cookie = |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 389 | FindEntry(resid, density_override, false /* stop_at_first_match */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 390 | if (cookie == kInvalidCookie) { |
| 391 | return kInvalidCookie; |
| 392 | } |
| 393 | |
Adam Lesinski | 498f605 | 2017-11-29 13:24:29 -0800 | [diff] [blame] | 394 | if (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 395 | if (!may_be_bag) { |
| 396 | LOG(ERROR) << base::StringPrintf("Resource %08x is a complex map type.", resid); |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 397 | return kInvalidCookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 398 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 399 | |
| 400 | // Create a reference since we can't represent this complex type as a Res_value. |
| 401 | out_value->dataType = Res_value::TYPE_REFERENCE; |
| 402 | out_value->data = resid; |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 403 | *out_selected_config = *entry.config; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 404 | *out_flags = entry.type_flags; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 405 | return cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | const Res_value* device_value = reinterpret_cast<const Res_value*>( |
| 409 | reinterpret_cast<const uint8_t*>(entry.entry) + dtohs(entry.entry->size)); |
| 410 | out_value->copyFrom_dtoh(*device_value); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 411 | |
| 412 | // Convert the package ID to the runtime assigned package ID. |
| 413 | entry.dynamic_ref_table->lookupResourceValue(out_value); |
| 414 | |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 415 | *out_selected_config = *entry.config; |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 416 | *out_flags = entry.type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 417 | return cookie; |
| 418 | } |
| 419 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 420 | ApkAssetsCookie AssetManager2::ResolveReference(ApkAssetsCookie cookie, Res_value* in_out_value, |
| 421 | ResTable_config* in_out_selected_config, |
| 422 | uint32_t* in_out_flags, |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 423 | uint32_t* out_last_reference) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 424 | ATRACE_CALL(); |
| 425 | constexpr const int kMaxIterations = 20; |
| 426 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 427 | for (size_t iteration = 0u; in_out_value->dataType == Res_value::TYPE_REFERENCE && |
| 428 | in_out_value->data != 0u && iteration < kMaxIterations; |
| 429 | iteration++) { |
Adam Lesinski | dcb3c65 | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 430 | *out_last_reference = in_out_value->data; |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 431 | uint32_t new_flags = 0u; |
| 432 | cookie = GetResource(in_out_value->data, true /*may_be_bag*/, 0u /*density_override*/, |
| 433 | in_out_value, in_out_selected_config, &new_flags); |
| 434 | if (cookie == kInvalidCookie) { |
| 435 | return kInvalidCookie; |
| 436 | } |
| 437 | if (in_out_flags != nullptr) { |
| 438 | *in_out_flags |= new_flags; |
| 439 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 440 | if (*out_last_reference == in_out_value->data) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 441 | // This reference can't be resolved, so exit now and let the caller deal with it. |
| 442 | return cookie; |
| 443 | } |
| 444 | } |
| 445 | return cookie; |
| 446 | } |
| 447 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 448 | const ResolvedBag* AssetManager2::GetBag(uint32_t resid) { |
| 449 | ATRACE_CALL(); |
| 450 | |
| 451 | auto cached_iter = cached_bags_.find(resid); |
| 452 | if (cached_iter != cached_bags_.end()) { |
| 453 | return cached_iter->second.get(); |
| 454 | } |
| 455 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 456 | FindEntryResult entry; |
| 457 | ApkAssetsCookie cookie = |
| 458 | FindEntry(resid, 0u /* density_override */, false /* stop_at_first_match */, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 459 | if (cookie == kInvalidCookie) { |
| 460 | return nullptr; |
| 461 | } |
| 462 | |
| 463 | // Check that the size of the entry header is at least as big as |
| 464 | // the desired ResTable_map_entry. Also verify that the entry |
| 465 | // was intended to be a map. |
| 466 | if (dtohs(entry.entry->size) < sizeof(ResTable_map_entry) || |
| 467 | (dtohs(entry.entry->flags) & ResTable_entry::FLAG_COMPLEX) == 0) { |
| 468 | // Not a bag, nothing to do. |
| 469 | return nullptr; |
| 470 | } |
| 471 | |
| 472 | const ResTable_map_entry* map = reinterpret_cast<const ResTable_map_entry*>(entry.entry); |
| 473 | const ResTable_map* map_entry = |
| 474 | reinterpret_cast<const ResTable_map*>(reinterpret_cast<const uint8_t*>(map) + map->size); |
| 475 | const ResTable_map* const map_entry_end = map_entry + dtohl(map->count); |
| 476 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 477 | uint32_t parent_resid = dtohl(map->parent.ident); |
| 478 | if (parent_resid == 0) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 479 | // There is no parent, meaning there is nothing to inherit and we can do a simple |
| 480 | // copy of the entries in the map. |
| 481 | const size_t entry_count = map_entry_end - map_entry; |
| 482 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 483 | malloc(sizeof(ResolvedBag) + (entry_count * sizeof(ResolvedBag::Entry))))}; |
| 484 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 485 | for (; map_entry != map_entry_end; ++map_entry) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 486 | uint32_t new_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 487 | if (!is_internal_resid(new_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 488 | // Attributes, arrays, etc don't have a resource id as the name. They specify |
| 489 | // other data, which would be wrong to change via a lookup. |
| 490 | if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) { |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 491 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 492 | return nullptr; |
| 493 | } |
| 494 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 495 | new_entry->cookie = cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 496 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 497 | new_entry->key_pool = nullptr; |
| 498 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 499 | new_entry->value.copyFrom_dtoh(map_entry->value); |
| 500 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 501 | if (err != NO_ERROR) { |
| 502 | LOG(ERROR) << base::StringPrintf( |
| 503 | "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType, |
| 504 | new_entry->value.data, new_key); |
| 505 | return nullptr; |
| 506 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 507 | ++new_entry; |
| 508 | } |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 509 | new_bag->type_spec_flags = entry.type_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 510 | new_bag->entry_count = static_cast<uint32_t>(entry_count); |
| 511 | ResolvedBag* result = new_bag.get(); |
| 512 | cached_bags_[resid] = std::move(new_bag); |
| 513 | return result; |
| 514 | } |
| 515 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 516 | // In case the parent is a dynamic reference, resolve it. |
| 517 | entry.dynamic_ref_table->lookupResourceId(&parent_resid); |
| 518 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 519 | // Get the parent and do a merge of the keys. |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 520 | const ResolvedBag* parent_bag = GetBag(parent_resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 521 | if (parent_bag == nullptr) { |
| 522 | // Failed to get the parent that should exist. |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 523 | LOG(ERROR) << base::StringPrintf("Failed to find parent 0x%08x of bag 0x%08x.", parent_resid, resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 524 | return nullptr; |
| 525 | } |
| 526 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 527 | // Create the max possible entries we can make. Once we construct the bag, |
| 528 | // we will realloc to fit to size. |
| 529 | 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] | 530 | util::unique_cptr<ResolvedBag> new_bag{reinterpret_cast<ResolvedBag*>( |
| 531 | malloc(sizeof(ResolvedBag) + (max_count * sizeof(ResolvedBag::Entry))))}; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 532 | ResolvedBag::Entry* new_entry = new_bag->entries; |
| 533 | |
| 534 | const ResolvedBag::Entry* parent_entry = parent_bag->entries; |
| 535 | const ResolvedBag::Entry* const parent_entry_end = parent_entry + parent_bag->entry_count; |
| 536 | |
| 537 | // The keys are expected to be in sorted order. Merge the two bags. |
| 538 | while (map_entry != map_entry_end && parent_entry != parent_entry_end) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 539 | uint32_t child_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 540 | if (!is_internal_resid(child_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 541 | if (entry.dynamic_ref_table->lookupResourceId(&child_key) != NO_ERROR) { |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 542 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", child_key, resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 543 | return nullptr; |
| 544 | } |
| 545 | } |
| 546 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 547 | if (child_key <= parent_entry->key) { |
| 548 | // Use the child key if it comes before the parent |
| 549 | // or is equal to the parent (overrides). |
| 550 | new_entry->cookie = cookie; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 551 | new_entry->key = child_key; |
| 552 | new_entry->key_pool = nullptr; |
| 553 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 554 | new_entry->value.copyFrom_dtoh(map_entry->value); |
| 555 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 556 | if (err != NO_ERROR) { |
| 557 | LOG(ERROR) << base::StringPrintf( |
| 558 | "Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", new_entry->value.dataType, |
| 559 | new_entry->value.data, child_key); |
| 560 | return nullptr; |
| 561 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 562 | ++map_entry; |
| 563 | } else { |
| 564 | // Take the parent entry as-is. |
| 565 | memcpy(new_entry, parent_entry, sizeof(*new_entry)); |
| 566 | } |
| 567 | |
| 568 | if (child_key >= parent_entry->key) { |
| 569 | // Move to the next parent entry if we used it or it was overridden. |
| 570 | ++parent_entry; |
| 571 | } |
| 572 | // Increment to the next entry to fill. |
| 573 | ++new_entry; |
| 574 | } |
| 575 | |
| 576 | // Finish the child entries if they exist. |
| 577 | while (map_entry != map_entry_end) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 578 | uint32_t new_key = dtohl(map_entry->name.ident); |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 579 | if (!is_internal_resid(new_key)) { |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 580 | if (entry.dynamic_ref_table->lookupResourceId(&new_key) != NO_ERROR) { |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 581 | LOG(ERROR) << base::StringPrintf("Failed to resolve key 0x%08x in bag 0x%08x.", new_key, resid); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 582 | return nullptr; |
| 583 | } |
| 584 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 585 | new_entry->cookie = cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 586 | new_entry->key = new_key; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 587 | new_entry->key_pool = nullptr; |
| 588 | new_entry->type_pool = nullptr; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 589 | new_entry->value.copyFrom_dtoh(map_entry->value); |
| 590 | status_t err = entry.dynamic_ref_table->lookupResourceValue(&new_entry->value); |
| 591 | if (err != NO_ERROR) { |
| 592 | LOG(ERROR) << base::StringPrintf("Failed to resolve value t=0x%02x d=0x%08x for key 0x%08x.", |
| 593 | new_entry->value.dataType, new_entry->value.data, new_key); |
| 594 | return nullptr; |
| 595 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 596 | ++map_entry; |
| 597 | ++new_entry; |
| 598 | } |
| 599 | |
| 600 | // Finish the parent entries if they exist. |
| 601 | if (parent_entry != parent_entry_end) { |
| 602 | // Take the rest of the parent entries as-is. |
| 603 | const size_t num_entries_to_copy = parent_entry_end - parent_entry; |
| 604 | memcpy(new_entry, parent_entry, num_entries_to_copy * sizeof(*new_entry)); |
| 605 | new_entry += num_entries_to_copy; |
| 606 | } |
| 607 | |
| 608 | // Resize the resulting array to fit. |
| 609 | const size_t actual_count = new_entry - new_bag->entries; |
| 610 | if (actual_count != max_count) { |
George Burgess IV | 09b119f | 2017-07-25 15:00:04 -0700 | [diff] [blame] | 611 | new_bag.reset(reinterpret_cast<ResolvedBag*>(realloc( |
| 612 | new_bag.release(), sizeof(ResolvedBag) + (actual_count * sizeof(ResolvedBag::Entry))))); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Adam Lesinski | 1a1e9c2 | 2017-10-13 15:45:34 -0700 | [diff] [blame] | 615 | // Combine flags from the parent and our own bag. |
| 616 | 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] | 617 | new_bag->entry_count = static_cast<uint32_t>(actual_count); |
| 618 | ResolvedBag* result = new_bag.get(); |
| 619 | cached_bags_[resid] = std::move(new_bag); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 620 | return result; |
| 621 | } |
| 622 | |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 623 | static bool Utf8ToUtf16(const StringPiece& str, std::u16string* out) { |
| 624 | ssize_t len = |
| 625 | utf8_to_utf16_length(reinterpret_cast<const uint8_t*>(str.data()), str.size(), false); |
| 626 | if (len < 0) { |
| 627 | return false; |
| 628 | } |
| 629 | out->resize(static_cast<size_t>(len)); |
| 630 | utf8_to_utf16(reinterpret_cast<const uint8_t*>(str.data()), str.size(), &*out->begin(), |
| 631 | static_cast<size_t>(len + 1)); |
| 632 | return true; |
| 633 | } |
| 634 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 635 | uint32_t AssetManager2::GetResourceId(const std::string& resource_name, |
| 636 | const std::string& fallback_type, |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 637 | const std::string& fallback_package) { |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 638 | StringPiece package_name, type, entry; |
| 639 | if (!ExtractResourceName(resource_name, &package_name, &type, &entry)) { |
| 640 | return 0u; |
| 641 | } |
| 642 | |
| 643 | if (entry.empty()) { |
| 644 | return 0u; |
| 645 | } |
| 646 | |
| 647 | if (package_name.empty()) { |
| 648 | package_name = fallback_package; |
| 649 | } |
| 650 | |
| 651 | if (type.empty()) { |
| 652 | type = fallback_type; |
| 653 | } |
| 654 | |
| 655 | std::u16string type16; |
| 656 | if (!Utf8ToUtf16(type, &type16)) { |
| 657 | return 0u; |
| 658 | } |
| 659 | |
| 660 | std::u16string entry16; |
| 661 | if (!Utf8ToUtf16(entry, &entry16)) { |
| 662 | return 0u; |
| 663 | } |
| 664 | |
| 665 | const StringPiece16 kAttr16 = u"attr"; |
| 666 | const static std::u16string kAttrPrivate16 = u"^attr-private"; |
| 667 | |
| 668 | for (const PackageGroup& package_group : package_groups_) { |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 669 | for (const LoadedPackage* package : package_group.packages_) { |
Adam Lesinski | 929d651 | 2017-01-16 19:11:19 -0800 | [diff] [blame] | 670 | if (package_name != package->GetPackageName()) { |
| 671 | // All packages in the same group are expected to have the same package name. |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | uint32_t resid = package->FindEntryByName(type16, entry16); |
| 676 | if (resid == 0u && kAttr16 == type16) { |
| 677 | // Private attributes in libraries (such as the framework) are sometimes encoded |
| 678 | // under the type '^attr-private' in order to leave the ID space of public 'attr' |
| 679 | // free for future additions. Check '^attr-private' for the same name. |
| 680 | resid = package->FindEntryByName(kAttrPrivate16, entry16); |
| 681 | } |
| 682 | |
| 683 | if (resid != 0u) { |
| 684 | return fix_package_id(resid, package_group.dynamic_ref_table.mAssignedPackageId); |
| 685 | } |
| 686 | } |
| 687 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 688 | return 0u; |
| 689 | } |
| 690 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 691 | void AssetManager2::InvalidateCaches(uint32_t diff) { |
| 692 | if (diff == 0xffffffffu) { |
| 693 | // Everything must go. |
| 694 | cached_bags_.clear(); |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | // Be more conservative with what gets purged. Only if the bag has other possible |
| 699 | // variations with respect to what changed (diff) should we remove it. |
| 700 | for (auto iter = cached_bags_.cbegin(); iter != cached_bags_.cend();) { |
| 701 | if (diff & iter->second->type_spec_flags) { |
| 702 | iter = cached_bags_.erase(iter); |
| 703 | } else { |
| 704 | ++iter; |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 709 | std::unique_ptr<Theme> AssetManager2::NewTheme() { |
| 710 | return std::unique_ptr<Theme>(new Theme(this)); |
| 711 | } |
| 712 | |
| 713 | Theme::Theme(AssetManager2* asset_manager) : asset_manager_(asset_manager) { |
| 714 | } |
| 715 | |
| 716 | Theme::~Theme() = default; |
| 717 | |
| 718 | namespace { |
| 719 | |
| 720 | struct ThemeEntry { |
| 721 | ApkAssetsCookie cookie; |
| 722 | uint32_t type_spec_flags; |
| 723 | Res_value value; |
| 724 | }; |
| 725 | |
| 726 | struct ThemeType { |
| 727 | int entry_count; |
| 728 | ThemeEntry entries[0]; |
| 729 | }; |
| 730 | |
| 731 | constexpr size_t kTypeCount = std::numeric_limits<uint8_t>::max() + 1; |
| 732 | |
| 733 | } // namespace |
| 734 | |
| 735 | struct Theme::Package { |
| 736 | // Each element of Type will be a dynamically sized object |
| 737 | // allocated to have the entries stored contiguously with the Type. |
| 738 | std::array<util::unique_cptr<ThemeType>, kTypeCount> types; |
| 739 | }; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 740 | |
| 741 | bool Theme::ApplyStyle(uint32_t resid, bool force) { |
| 742 | ATRACE_CALL(); |
| 743 | |
| 744 | const ResolvedBag* bag = asset_manager_->GetBag(resid); |
| 745 | if (bag == nullptr) { |
| 746 | return false; |
| 747 | } |
| 748 | |
| 749 | // Merge the flags from this style. |
| 750 | type_spec_flags_ |= bag->type_spec_flags; |
| 751 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 752 | int last_type_idx = -1; |
| 753 | int last_package_idx = -1; |
| 754 | Package* last_package = nullptr; |
| 755 | ThemeType* last_type = nullptr; |
| 756 | |
| 757 | // Iterate backwards, because each bag is sorted in ascending key ID order, meaning we will only |
| 758 | // need to perform one resize per type. |
| 759 | using reverse_bag_iterator = std::reverse_iterator<const ResolvedBag::Entry*>; |
| 760 | const auto bag_iter_end = reverse_bag_iterator(begin(bag)); |
| 761 | 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] | 762 | const uint32_t attr_resid = bag_iter->key; |
| 763 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 764 | // If the resource ID passed in is not a style, the key can be some other identifier that is not |
| 765 | // 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] | 766 | if (!is_valid_resid(attr_resid)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 767 | return false; |
| 768 | } |
| 769 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 770 | // We don't use the 0-based index for the type so that we can avoid doing ID validation |
| 771 | // upon lookup. Instead, we keep space for the type ID 0 in our data structures. Since |
| 772 | // the construction of this type is guarded with a resource ID check, it will never be |
| 773 | // populated, and querying type ID 0 will always fail. |
| 774 | const int package_idx = get_package_id(attr_resid); |
| 775 | const int type_idx = get_type_id(attr_resid); |
| 776 | const int entry_idx = get_entry_id(attr_resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 777 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 778 | if (last_package_idx != package_idx) { |
| 779 | std::unique_ptr<Package>& package = packages_[package_idx]; |
| 780 | if (package == nullptr) { |
| 781 | package.reset(new Package()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 782 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 783 | last_package_idx = package_idx; |
| 784 | last_package = package.get(); |
| 785 | last_type_idx = -1; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 786 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 787 | |
| 788 | if (last_type_idx != type_idx) { |
| 789 | util::unique_cptr<ThemeType>& type = last_package->types[type_idx]; |
| 790 | if (type == nullptr) { |
| 791 | // Allocate enough memory to contain this entry_idx. Since we're iterating in reverse over |
| 792 | // a sorted list of attributes, this shouldn't be resized again during this method call. |
| 793 | type.reset(reinterpret_cast<ThemeType*>( |
| 794 | calloc(sizeof(ThemeType) + (entry_idx + 1) * sizeof(ThemeEntry), 1))); |
| 795 | type->entry_count = entry_idx + 1; |
| 796 | } else if (entry_idx >= type->entry_count) { |
| 797 | // Reallocate the memory to contain this entry_idx. Since we're iterating in reverse over |
| 798 | // a sorted list of attributes, this shouldn't be resized again during this method call. |
| 799 | const int new_count = entry_idx + 1; |
| 800 | type.reset(reinterpret_cast<ThemeType*>( |
| 801 | realloc(type.release(), sizeof(ThemeType) + (new_count * sizeof(ThemeEntry))))); |
| 802 | |
| 803 | // Clear out the newly allocated space (which isn't zeroed). |
| 804 | memset(type->entries + type->entry_count, 0, |
| 805 | (new_count - type->entry_count) * sizeof(ThemeEntry)); |
| 806 | type->entry_count = new_count; |
| 807 | } |
| 808 | last_type_idx = type_idx; |
| 809 | last_type = type.get(); |
| 810 | } |
| 811 | |
| 812 | ThemeEntry& entry = last_type->entries[entry_idx]; |
| 813 | if (force || (entry.value.dataType == Res_value::TYPE_NULL && |
| 814 | entry.value.data != Res_value::DATA_NULL_EMPTY)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 815 | entry.cookie = bag_iter->cookie; |
| 816 | entry.type_spec_flags |= bag->type_spec_flags; |
| 817 | entry.value = bag_iter->value; |
| 818 | } |
| 819 | } |
| 820 | return true; |
| 821 | } |
| 822 | |
| 823 | ApkAssetsCookie Theme::GetAttribute(uint32_t resid, Res_value* out_value, |
| 824 | uint32_t* out_flags) const { |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 825 | int cnt = 20; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 826 | |
| 827 | uint32_t type_spec_flags = 0u; |
| 828 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 829 | do { |
| 830 | const int package_idx = get_package_id(resid); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 831 | const Package* package = packages_[package_idx].get(); |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 832 | if (package != nullptr) { |
| 833 | // The themes are constructed with a 1-based type ID, so no need to decrement here. |
| 834 | const int type_idx = get_type_id(resid); |
| 835 | const ThemeType* type = package->types[type_idx].get(); |
| 836 | if (type != nullptr) { |
| 837 | const int entry_idx = get_entry_id(resid); |
| 838 | if (entry_idx < type->entry_count) { |
| 839 | const ThemeEntry& entry = type->entries[entry_idx]; |
| 840 | type_spec_flags |= entry.type_spec_flags; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 841 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 842 | if (entry.value.dataType == Res_value::TYPE_ATTRIBUTE) { |
| 843 | if (cnt > 0) { |
| 844 | cnt--; |
| 845 | resid = entry.value.data; |
| 846 | continue; |
| 847 | } |
| 848 | return kInvalidCookie; |
| 849 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 850 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 851 | // @null is different than @empty. |
| 852 | if (entry.value.dataType == Res_value::TYPE_NULL && |
| 853 | entry.value.data != Res_value::DATA_NULL_EMPTY) { |
| 854 | return kInvalidCookie; |
| 855 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 856 | |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 857 | *out_value = entry.value; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 858 | *out_flags = type_spec_flags; |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 859 | return entry.cookie; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 860 | } |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 861 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 862 | } |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 863 | break; |
| 864 | } while (true); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 865 | return kInvalidCookie; |
| 866 | } |
| 867 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 868 | ApkAssetsCookie Theme::ResolveAttributeReference(ApkAssetsCookie cookie, Res_value* in_out_value, |
| 869 | ResTable_config* in_out_selected_config, |
| 870 | uint32_t* in_out_type_spec_flags, |
Adam Lesinski | 50706b6 | 2018-01-23 03:16:16 -0800 | [diff] [blame] | 871 | uint32_t* out_last_ref) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 872 | if (in_out_value->dataType == Res_value::TYPE_ATTRIBUTE) { |
| 873 | uint32_t new_flags; |
| 874 | cookie = GetAttribute(in_out_value->data, in_out_value, &new_flags); |
| 875 | if (cookie == kInvalidCookie) { |
| 876 | return kInvalidCookie; |
| 877 | } |
| 878 | |
| 879 | if (in_out_type_spec_flags != nullptr) { |
| 880 | *in_out_type_spec_flags |= new_flags; |
| 881 | } |
| 882 | } |
| 883 | return asset_manager_->ResolveReference(cookie, in_out_value, in_out_selected_config, |
| 884 | in_out_type_spec_flags, out_last_ref); |
| 885 | } |
| 886 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 887 | void Theme::Clear() { |
| 888 | type_spec_flags_ = 0u; |
| 889 | for (std::unique_ptr<Package>& package : packages_) { |
| 890 | package.reset(); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | bool Theme::SetTo(const Theme& o) { |
| 895 | if (this == &o) { |
| 896 | return true; |
| 897 | } |
| 898 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 899 | type_spec_flags_ = o.type_spec_flags_; |
| 900 | |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 901 | const bool copy_only_system = asset_manager_ != o.asset_manager_; |
| 902 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 903 | for (size_t p = 0; p < packages_.size(); p++) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 904 | const Package* package = o.packages_[p].get(); |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 905 | if (package == nullptr || (copy_only_system && p != 0x01)) { |
| 906 | // The other theme doesn't have this package, clear ours. |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 907 | packages_[p].reset(); |
| 908 | continue; |
| 909 | } |
| 910 | |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 911 | if (packages_[p] == nullptr) { |
| 912 | // The other theme has this package, but we don't. Make one. |
| 913 | packages_[p].reset(new Package()); |
| 914 | } |
| 915 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 916 | for (size_t t = 0; t < package->types.size(); t++) { |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 917 | const ThemeType* type = package->types[t].get(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 918 | if (type == nullptr) { |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 919 | // The other theme doesn't have this type, clear ours. |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 920 | packages_[p]->types[t].reset(); |
| 921 | continue; |
| 922 | } |
| 923 | |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 924 | // Create a new type and update it to theirs. |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 925 | const size_t type_alloc_size = sizeof(ThemeType) + (type->entry_count * sizeof(ThemeEntry)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 926 | void* copied_data = malloc(type_alloc_size); |
| 927 | memcpy(copied_data, type, type_alloc_size); |
Adam Lesinski | 30080e2 | 2017-10-16 16:18:09 -0700 | [diff] [blame] | 928 | packages_[p]->types[t].reset(reinterpret_cast<ThemeType*>(copied_data)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | return true; |
| 932 | } |
| 933 | |
| 934 | } // namespace android |