Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include "idmap2/ResourceMapping.h" |
| 18 | |
| 19 | #include <map> |
| 20 | #include <memory> |
| 21 | #include <set> |
| 22 | #include <string> |
| 23 | #include <utility> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include "android-base/stringprintf.h" |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 27 | #include "androidfw/ResourceTypes.h" |
| 28 | #include "idmap2/PolicyUtils.h" |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 29 | #include "idmap2/ResourceUtils.h" |
| 30 | |
| 31 | using android::base::StringPrintf; |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 32 | using android::idmap2::utils::BitmaskToPolicies; |
Ryan Mitchell | 5035d66 | 2020-01-22 13:19:41 -0800 | [diff] [blame] | 33 | using android::idmap2::utils::IsReference; |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 34 | using android::idmap2::utils::ResToTypeEntryName; |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 35 | using PolicyBitmask = android::ResTable_overlayable_policy_header::PolicyBitmask; |
| 36 | using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags; |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 37 | |
| 38 | namespace android::idmap2 { |
| 39 | |
| 40 | namespace { |
| 41 | |
Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 42 | #define REWRITE_PACKAGE(resid, package_id) \ |
| 43 | (((resid)&0x00ffffffU) | (((uint32_t)(package_id)) << 24U)) |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 44 | #define EXTRACT_PACKAGE(resid) ((0xff000000 & (resid)) >> 24) |
| 45 | |
| 46 | std::string ConcatPolicies(const std::vector<std::string>& policies) { |
| 47 | std::string message; |
| 48 | for (const std::string& policy : policies) { |
| 49 | if (!message.empty()) { |
| 50 | message.append("|"); |
| 51 | } |
| 52 | message.append(policy); |
| 53 | } |
| 54 | |
| 55 | return message; |
| 56 | } |
| 57 | |
| 58 | Result<Unit> CheckOverlayable(const LoadedPackage& target_package, |
| 59 | const OverlayManifestInfo& overlay_info, |
| 60 | const PolicyBitmask& fulfilled_policies, |
| 61 | const ResourceId& target_resource) { |
| 62 | static constexpr const PolicyBitmask sDefaultPolicies = |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 63 | PolicyFlags::ODM_PARTITION | PolicyFlags::OEM_PARTITION | PolicyFlags::SYSTEM_PARTITION | |
Zoran Jovanovic | 0f942f9 | 2020-06-09 18:51:57 +0200 | [diff] [blame] | 64 | PolicyFlags::VENDOR_PARTITION | PolicyFlags::PRODUCT_PARTITION | PolicyFlags::SIGNATURE | |
| 65 | PolicyFlags::CONFIG_SIGNATURE; |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 66 | |
| 67 | // If the resource does not have an overlayable definition, allow the resource to be overlaid if |
Zoran Jovanovic | 0f942f9 | 2020-06-09 18:51:57 +0200 | [diff] [blame] | 68 | // the overlay is preinstalled, signed with the same signature as the target or signed with the |
| 69 | // same signature as reference package defined in SystemConfig under 'overlay-config-signature' |
| 70 | // tag. |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 71 | if (!target_package.DefinesOverlayable()) { |
| 72 | return (sDefaultPolicies & fulfilled_policies) != 0 |
| 73 | ? Result<Unit>({}) |
Ryan Mitchell | 70e02d4 | 2020-10-14 14:19:51 -0700 | [diff] [blame] | 74 | : Error("overlay must be preinstalled, signed with the same signature as the target," |
| 75 | " or signed with the same signature as the package referenced through" |
| 76 | " <overlay-config-signature>."); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | const OverlayableInfo* overlayable_info = target_package.GetOverlayableInfo(target_resource); |
| 80 | if (overlayable_info == nullptr) { |
| 81 | // Do not allow non-overlayable resources to be overlaid. |
| 82 | return Error("target resource has no overlayable declaration"); |
| 83 | } |
| 84 | |
| 85 | if (overlay_info.target_name != overlayable_info->name) { |
| 86 | // If the overlay supplies a target overlayable name, the resource must belong to the |
| 87 | // overlayable defined with the specified name to be overlaid. |
| 88 | return Error(R"(<overlay> android:targetName "%s" does not match overlayable name "%s")", |
| 89 | overlay_info.target_name.c_str(), overlayable_info->name.c_str()); |
| 90 | } |
| 91 | |
| 92 | // Enforce policy restrictions if the resource is declared as overlayable. |
| 93 | if ((overlayable_info->policy_flags & fulfilled_policies) == 0) { |
| 94 | return Error(R"(overlay with policies "%s" does not fulfill any overlayable policies "%s")", |
| 95 | ConcatPolicies(BitmaskToPolicies(fulfilled_policies)).c_str(), |
| 96 | ConcatPolicies(BitmaskToPolicies(overlayable_info->policy_flags)).c_str()); |
| 97 | } |
| 98 | |
| 99 | return Result<Unit>({}); |
| 100 | } |
| 101 | |
| 102 | // TODO(martenkongstad): scan for package name instead of assuming package at index 0 |
| 103 | // |
| 104 | // idmap version 0x01 naively assumes that the package to use is always the first ResTable_package |
| 105 | // in the resources.arsc blob. In most cases, there is only a single ResTable_package anyway, so |
| 106 | // this assumption tends to work out. That said, the correct thing to do is to scan |
| 107 | // resources.arsc for a package with a given name as read from the package manifest instead of |
| 108 | // relying on a hard-coded index. This however requires storing the package name in the idmap |
| 109 | // header, which in turn requires incrementing the idmap version. Because the initial version of |
| 110 | // idmap2 is compatible with idmap, this will have to wait for now. |
| 111 | const LoadedPackage* GetPackageAtIndex0(const LoadedArsc& loaded_arsc) { |
| 112 | const std::vector<std::unique_ptr<const LoadedPackage>>& packages = loaded_arsc.GetPackages(); |
| 113 | if (packages.empty()) { |
| 114 | return nullptr; |
| 115 | } |
| 116 | int id = packages[0]->GetPackageId(); |
| 117 | return loaded_arsc.GetPackageById(id); |
| 118 | } |
| 119 | |
| 120 | Result<std::unique_ptr<Asset>> OpenNonAssetFromResource(const ResourceId& resource_id, |
| 121 | const AssetManager2& asset_manager) { |
| 122 | Res_value value{}; |
| 123 | ResTable_config selected_config{}; |
| 124 | uint32_t flags; |
| 125 | auto cookie = |
| 126 | asset_manager.GetResource(resource_id, /* may_be_bag */ false, |
| 127 | /* density_override */ 0U, &value, &selected_config, &flags); |
| 128 | if (cookie == kInvalidCookie) { |
| 129 | return Error("failed to find resource for id 0x%08x", resource_id); |
| 130 | } |
| 131 | |
| 132 | if (value.dataType != Res_value::TYPE_STRING) { |
| 133 | return Error("resource for is 0x%08x is not a file", resource_id); |
| 134 | } |
| 135 | |
| 136 | auto string_pool = asset_manager.GetStringPoolForCookie(cookie); |
| 137 | size_t len; |
| 138 | auto file_path16 = string_pool->stringAt(value.data, &len); |
| 139 | if (file_path16 == nullptr) { |
| 140 | return Error("failed to find string for index %d", value.data); |
| 141 | } |
| 142 | |
| 143 | // Load the overlay resource mappings from the file specified using android:resourcesMap. |
| 144 | auto file_path = String8(String16(file_path16)); |
| 145 | auto asset = asset_manager.OpenNonAsset(file_path.c_str(), Asset::AccessMode::ACCESS_BUFFER); |
| 146 | if (asset == nullptr) { |
| 147 | return Error("file \"%s\" not found", file_path.c_str()); |
| 148 | } |
| 149 | |
| 150 | return asset; |
| 151 | } |
| 152 | |
| 153 | } // namespace |
| 154 | |
| 155 | Result<ResourceMapping> ResourceMapping::CreateResourceMapping(const AssetManager2* target_am, |
| 156 | const LoadedPackage* target_package, |
| 157 | const LoadedPackage* overlay_package, |
| 158 | size_t string_pool_offset, |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 159 | const XmlParser& overlay_parser, |
| 160 | LogInfo& log_info) { |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 161 | ResourceMapping resource_mapping; |
| 162 | auto root_it = overlay_parser.tree_iterator(); |
| 163 | if (root_it->event() != XmlParser::Event::START_TAG || root_it->name() != "overlay") { |
| 164 | return Error("root element is not <overlay> tag"); |
| 165 | } |
| 166 | |
Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 167 | const uint8_t target_package_id = target_package->GetPackageId(); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 168 | const uint8_t overlay_package_id = overlay_package->GetPackageId(); |
| 169 | auto overlay_it_end = root_it.end(); |
| 170 | for (auto overlay_it = root_it.begin(); overlay_it != overlay_it_end; ++overlay_it) { |
| 171 | if (overlay_it->event() == XmlParser::Event::BAD_DOCUMENT) { |
| 172 | return Error("failed to parse overlay xml document"); |
| 173 | } |
| 174 | |
| 175 | if (overlay_it->event() != XmlParser::Event::START_TAG) { |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | if (overlay_it->name() != "item") { |
| 180 | return Error("unexpected tag <%s> in <overlay>", overlay_it->name().c_str()); |
| 181 | } |
| 182 | |
| 183 | Result<std::string> target_resource = overlay_it->GetAttributeStringValue("target"); |
| 184 | if (!target_resource) { |
| 185 | return Error(R"(<item> tag missing expected attribute "target")"); |
| 186 | } |
| 187 | |
| 188 | Result<android::Res_value> overlay_resource = overlay_it->GetAttributeValue("value"); |
| 189 | if (!overlay_resource) { |
| 190 | return Error(R"(<item> tag missing expected attribute "value")"); |
| 191 | } |
| 192 | |
| 193 | ResourceId target_id = |
| 194 | target_am->GetResourceId(*target_resource, "", target_package->GetPackageName()); |
| 195 | if (target_id == 0U) { |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 196 | log_info.Warning(LogMessage() << "failed to find resource \"" << *target_resource |
| 197 | << "\" in target resources"); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 198 | continue; |
| 199 | } |
| 200 | |
Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 201 | // Retrieve the compile-time resource id of the target resource. |
| 202 | target_id = REWRITE_PACKAGE(target_id, target_package_id); |
| 203 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 204 | if (overlay_resource->dataType == Res_value::TYPE_STRING) { |
| 205 | overlay_resource->data += string_pool_offset; |
| 206 | } |
| 207 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 208 | if (IsReference(overlay_resource->dataType)) { |
| 209 | // Only rewrite resources defined within the overlay package to their corresponding target |
| 210 | // resource ids at runtime. |
| 211 | bool rewrite_reference = overlay_package_id == EXTRACT_PACKAGE(overlay_resource->data); |
| 212 | resource_mapping.AddMapping(target_id, overlay_resource->data, rewrite_reference); |
| 213 | } else { |
| 214 | resource_mapping.AddMapping(target_id, overlay_resource->dataType, overlay_resource->data); |
Ryan Mitchell | e753ffe | 2019-09-23 09:47:02 -0700 | [diff] [blame] | 215 | } |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | return resource_mapping; |
| 219 | } |
| 220 | |
| 221 | Result<ResourceMapping> ResourceMapping::CreateResourceMappingLegacy( |
| 222 | const AssetManager2* target_am, const AssetManager2* overlay_am, |
| 223 | const LoadedPackage* target_package, const LoadedPackage* overlay_package) { |
| 224 | ResourceMapping resource_mapping; |
Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 225 | const uint8_t target_package_id = target_package->GetPackageId(); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 226 | const auto end = overlay_package->end(); |
| 227 | for (auto iter = overlay_package->begin(); iter != end; ++iter) { |
| 228 | const ResourceId overlay_resid = *iter; |
| 229 | Result<std::string> name = utils::ResToTypeEntryName(*overlay_am, overlay_resid); |
| 230 | if (!name) { |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | // Find the resource with the same type and entry name within the target package. |
| 235 | const std::string full_name = |
| 236 | base::StringPrintf("%s:%s", target_package->GetPackageName().c_str(), name->c_str()); |
Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 237 | ResourceId target_resource = target_am->GetResourceId(full_name); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 238 | if (target_resource == 0U) { |
| 239 | continue; |
| 240 | } |
| 241 | |
Ryan Mitchell | ee4a564 | 2019-10-16 08:32:55 -0700 | [diff] [blame] | 242 | // Retrieve the compile-time resource id of the target resource. |
| 243 | target_resource = REWRITE_PACKAGE(target_resource, target_package_id); |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 244 | resource_mapping.AddMapping(target_resource, overlay_resid, |
| 245 | false /* rewrite_overlay_reference */); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | return resource_mapping; |
| 249 | } |
| 250 | |
| 251 | void ResourceMapping::FilterOverlayableResources(const AssetManager2* target_am, |
| 252 | const LoadedPackage* target_package, |
| 253 | const LoadedPackage* overlay_package, |
| 254 | const OverlayManifestInfo& overlay_info, |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 255 | const PolicyBitmask& fulfilled_policies, |
| 256 | LogInfo& log_info) { |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 257 | std::set<ResourceId> remove_ids; |
| 258 | for (const auto& target_map : target_map_) { |
| 259 | const ResourceId target_resid = target_map.first; |
| 260 | Result<Unit> success = |
| 261 | CheckOverlayable(*target_package, overlay_info, fulfilled_policies, target_resid); |
| 262 | if (success) { |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | // Attempting to overlay a resource that is not allowed to be overlaid is treated as a |
| 267 | // warning. |
| 268 | Result<std::string> name = utils::ResToTypeEntryName(*target_am, target_resid); |
| 269 | if (!name) { |
| 270 | name = StringPrintf("0x%08x", target_resid); |
| 271 | } |
| 272 | |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 273 | log_info.Warning(LogMessage() << "overlay \"" << overlay_package->GetPackageName() |
| 274 | << "\" is not allowed to overlay resource \"" << *name |
| 275 | << "\" in target: " << success.GetErrorMessage()); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 276 | |
| 277 | remove_ids.insert(target_resid); |
| 278 | } |
| 279 | |
| 280 | for (const ResourceId target_resid : remove_ids) { |
| 281 | RemoveMapping(target_resid); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | Result<ResourceMapping> ResourceMapping::FromApkAssets(const ApkAssets& target_apk_assets, |
| 286 | const ApkAssets& overlay_apk_assets, |
| 287 | const OverlayManifestInfo& overlay_info, |
| 288 | const PolicyBitmask& fulfilled_policies, |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 289 | bool enforce_overlayable, |
| 290 | LogInfo& log_info) { |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 291 | AssetManager2 target_asset_manager; |
| 292 | if (!target_asset_manager.SetApkAssets({&target_apk_assets}, true /* invalidate_caches */, |
| 293 | false /* filter_incompatible_configs*/)) { |
| 294 | return Error("failed to create target asset manager"); |
| 295 | } |
| 296 | |
| 297 | AssetManager2 overlay_asset_manager; |
| 298 | if (!overlay_asset_manager.SetApkAssets({&overlay_apk_assets}, true /* invalidate_caches */, |
| 299 | false /* filter_incompatible_configs */)) { |
| 300 | return Error("failed to create overlay asset manager"); |
| 301 | } |
| 302 | |
| 303 | const LoadedArsc* target_arsc = target_apk_assets.GetLoadedArsc(); |
| 304 | if (target_arsc == nullptr) { |
| 305 | return Error("failed to load target resources.arsc"); |
| 306 | } |
| 307 | |
| 308 | const LoadedArsc* overlay_arsc = overlay_apk_assets.GetLoadedArsc(); |
| 309 | if (overlay_arsc == nullptr) { |
| 310 | return Error("failed to load overlay resources.arsc"); |
| 311 | } |
| 312 | |
| 313 | const LoadedPackage* target_pkg = GetPackageAtIndex0(*target_arsc); |
| 314 | if (target_pkg == nullptr) { |
| 315 | return Error("failed to load target package from resources.arsc"); |
| 316 | } |
| 317 | |
| 318 | const LoadedPackage* overlay_pkg = GetPackageAtIndex0(*overlay_arsc); |
| 319 | if (overlay_pkg == nullptr) { |
| 320 | return Error("failed to load overlay package from resources.arsc"); |
| 321 | } |
| 322 | |
| 323 | size_t string_pool_data_length = 0U; |
| 324 | size_t string_pool_offset = 0U; |
| 325 | std::unique_ptr<uint8_t[]> string_pool_data; |
| 326 | Result<ResourceMapping> resource_mapping = {{}}; |
| 327 | if (overlay_info.resource_mapping != 0U) { |
Ryan Mitchell | 5035d66 | 2020-01-22 13:19:41 -0800 | [diff] [blame] | 328 | // Use the dynamic reference table to find the assigned resource id of the map xml. |
| 329 | const auto& ref_table = overlay_asset_manager.GetDynamicRefTableForCookie(0); |
| 330 | uint32_t resource_mapping_id = overlay_info.resource_mapping; |
| 331 | ref_table->lookupResourceId(&resource_mapping_id); |
| 332 | |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 333 | // Load the overlay resource mappings from the file specified using android:resourcesMap. |
Ryan Mitchell | 5035d66 | 2020-01-22 13:19:41 -0800 | [diff] [blame] | 334 | auto asset = OpenNonAssetFromResource(resource_mapping_id, overlay_asset_manager); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 335 | if (!asset) { |
| 336 | return Error("failed opening xml for android:resourcesMap: %s", |
| 337 | asset.GetErrorMessage().c_str()); |
| 338 | } |
| 339 | |
| 340 | auto parser = |
| 341 | XmlParser::Create((*asset)->getBuffer(true /* wordAligned*/), (*asset)->getLength()); |
| 342 | if (!parser) { |
| 343 | return Error("failed opening ResXMLTree"); |
| 344 | } |
| 345 | |
| 346 | // Copy the xml string pool data before the parse goes out of scope. |
| 347 | auto& string_pool = (*parser)->get_strings(); |
| 348 | string_pool_data_length = string_pool.bytes(); |
| 349 | string_pool_data.reset(new uint8_t[string_pool_data_length]); |
| 350 | memcpy(string_pool_data.get(), string_pool.data(), string_pool_data_length); |
| 351 | |
| 352 | // Offset string indices by the size of the overlay resource table string pool. |
| 353 | string_pool_offset = overlay_arsc->GetStringPool()->size(); |
| 354 | |
| 355 | resource_mapping = CreateResourceMapping(&target_asset_manager, target_pkg, overlay_pkg, |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 356 | string_pool_offset, *(*parser), log_info); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 357 | } else { |
| 358 | // If no file is specified using android:resourcesMap, it is assumed that the overlay only |
| 359 | // defines resources intended to override target resources of the same type and name. |
| 360 | resource_mapping = CreateResourceMappingLegacy(&target_asset_manager, &overlay_asset_manager, |
| 361 | target_pkg, overlay_pkg); |
| 362 | } |
| 363 | |
| 364 | if (!resource_mapping) { |
| 365 | return resource_mapping.GetError(); |
| 366 | } |
| 367 | |
| 368 | if (enforce_overlayable) { |
| 369 | // Filter out resources the overlay is not allowed to override. |
| 370 | (*resource_mapping) |
| 371 | .FilterOverlayableResources(&target_asset_manager, target_pkg, overlay_pkg, overlay_info, |
Mårten Kongstad | d7e8a53 | 2019-10-11 08:32:04 +0200 | [diff] [blame] | 372 | fulfilled_policies, log_info); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | resource_mapping->target_package_id_ = target_pkg->GetPackageId(); |
| 376 | resource_mapping->overlay_package_id_ = overlay_pkg->GetPackageId(); |
| 377 | resource_mapping->string_pool_offset_ = string_pool_offset; |
| 378 | resource_mapping->string_pool_data_ = std::move(string_pool_data); |
| 379 | resource_mapping->string_pool_data_length_ = string_pool_data_length; |
| 380 | return std::move(*resource_mapping); |
| 381 | } |
| 382 | |
| 383 | OverlayResourceMap ResourceMapping::GetOverlayToTargetMap() const { |
| 384 | // An overlay resource can override multiple target resources at once. Rewrite the overlay |
| 385 | // resource as the first target resource it overrides. |
| 386 | OverlayResourceMap map; |
| 387 | for (const auto& mappings : overlay_map_) { |
| 388 | map.insert(std::make_pair(mappings.first, mappings.second)); |
| 389 | } |
| 390 | return map; |
| 391 | } |
| 392 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 393 | Result<Unit> ResourceMapping::AddMapping(ResourceId target_resource, ResourceId overlay_resource, |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 394 | bool rewrite_overlay_reference) { |
| 395 | if (target_map_.find(target_resource) != target_map_.end()) { |
| 396 | return Error(R"(target resource id "0x%08x" mapped to multiple values)", target_resource); |
| 397 | } |
| 398 | |
| 399 | // TODO(141485591): Ensure that the overlay type is compatible with the target type. If the |
| 400 | // runtime types are not compatible, it could cause runtime crashes when the resource is resolved. |
| 401 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 402 | target_map_.insert(std::make_pair(target_resource, overlay_resource)); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 403 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 404 | if (rewrite_overlay_reference) { |
| 405 | overlay_map_.insert(std::make_pair(overlay_resource, target_resource)); |
| 406 | } |
| 407 | return Unit{}; |
| 408 | } |
| 409 | |
| 410 | Result<Unit> ResourceMapping::AddMapping(ResourceId target_resource, |
| 411 | TargetValue::DataType data_type, |
| 412 | TargetValue::DataValue data_value) { |
| 413 | if (target_map_.find(target_resource) != target_map_.end()) { |
| 414 | return Error(R"(target resource id "0x%08x" mapped to multiple values)", target_resource); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 417 | // TODO(141485591): Ensure that the overlay type is compatible with the target type. If the |
| 418 | // runtime types are not compatible, it could cause runtime crashes when the resource is resolved. |
| 419 | |
| 420 | target_map_.insert(std::make_pair(target_resource, TargetValue{data_type, data_value})); |
| 421 | return Unit{}; |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | void ResourceMapping::RemoveMapping(ResourceId target_resource) { |
| 425 | auto target_iter = target_map_.find(target_resource); |
| 426 | if (target_iter == target_map_.end()) { |
| 427 | return; |
| 428 | } |
| 429 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 430 | const auto value = target_iter->second; |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 431 | target_map_.erase(target_iter); |
| 432 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 433 | const ResourceId* overlay_resource = std::get_if<ResourceId>(&value); |
| 434 | if (overlay_resource == nullptr) { |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 435 | return; |
| 436 | } |
| 437 | |
Ryan Mitchell | bf1f45b | 2020-09-29 17:22:52 -0700 | [diff] [blame] | 438 | auto overlay_iter = overlay_map_.equal_range(*overlay_resource); |
Ryan Mitchell | 9e4f52b | 2019-09-19 12:15:52 -0700 | [diff] [blame] | 439 | for (auto i = overlay_iter.first; i != overlay_iter.second; ++i) { |
| 440 | if (i->second == target_resource) { |
| 441 | overlay_map_.erase(i); |
| 442 | return; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | } // namespace android::idmap2 |