1 /* 2 * Copyright (C) 2021 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 #ifndef IDMAP2_INCLUDE_IDMAP2_RESOURCECONTAINER_H_ 18 #define IDMAP2_INCLUDE_IDMAP2_RESOURCECONTAINER_H_ 19 20 #include <memory> 21 #include <string> 22 #include <variant> 23 #include <vector> 24 25 #include "idmap2/Policies.h" 26 #include "idmap2/ResourceUtils.h" 27 28 namespace android::idmap2 { 29 30 struct ResourceContainer { 31 WARN_UNUSED virtual Result<uint32_t> GetCrc() const = 0; 32 WARN_UNUSED virtual const std::string& GetPath() const = 0; 33 WARN_UNUSED virtual Result<std::string> GetResourceName(ResourceId id) const = 0; 34 35 virtual ~ResourceContainer() = default; 36 }; 37 38 struct TargetResourceContainer : public ResourceContainer { 39 static Result<std::unique_ptr<TargetResourceContainer>> FromPath(std::string path); 40 41 WARN_UNUSED virtual Result<bool> DefinesOverlayable() const = 0; 42 WARN_UNUSED virtual Result<const android::OverlayableInfo*> GetOverlayableInfo( 43 ResourceId id) const = 0; 44 WARN_UNUSED virtual Result<ResourceId> GetResourceId(const std::string& name) const = 0; 45 46 ~TargetResourceContainer() override = default; 47 }; 48 49 struct OverlayManifestInfo { 50 std::string package_name; // NOLINT(misc-non-private-member-variables-in-classes) 51 std::string name; // NOLINT(misc-non-private-member-variables-in-classes) 52 std::string target_package; // NOLINT(misc-non-private-member-variables-in-classes) 53 std::string target_name; // NOLINT(misc-non-private-member-variables-in-classes) 54 ResourceId resource_mapping; // NOLINT(misc-non-private-member-variables-in-classes) 55 }; 56 57 struct OverlayData { 58 struct ResourceIdValue { 59 // The overlay resource id. 60 ResourceId overlay_id; 61 62 // Whether or not references to the overlay resource id should be rewritten to its corresponding 63 // target id during resource resolution. 64 bool rewrite_id; 65 }; 66 67 struct Value { 68 std::string resource_name; 69 std::variant<ResourceIdValue, TargetValue> value; 70 }; 71 72 struct InlineStringPoolData { 73 // The binary data of the android::ResStringPool string pool. 74 std::unique_ptr<uint8_t[]> data; 75 76 // The length of the binary data. 77 uint32_t data_length; 78 79 // The offset added to TargetValue#data_value (the index of the string in the inline string 80 // pool) in order to prevent the indices of the overlay resource table string pool from 81 // colliding with the inline string pool indices. 82 uint32_t string_pool_offset; 83 }; 84 85 // The overlay's mapping of target resource name to overlaid value. Use a vector to enforce that 86 // the overlay pairs are inserted into the ResourceMapping in the specified ordered. 87 std::vector<Value> pairs; 88 89 // If the overlay maps a target resource to a string literal (not a string resource), then the 90 // this field contains information about the string pool in which the string literal resides so it 91 // can be inlined into an idmap. 92 std::optional<InlineStringPoolData> string_pool_data; 93 }; 94 95 struct OverlayResourceContainer : public ResourceContainer { 96 static Result<std::unique_ptr<OverlayResourceContainer>> FromPath(std::string path); 97 98 WARN_UNUSED virtual Result<OverlayManifestInfo> FindOverlayInfo( 99 const std::string& name) const = 0; 100 WARN_UNUSED virtual Result<OverlayData> GetOverlayData(const OverlayManifestInfo& info) const = 0; 101 102 ~OverlayResourceContainer() override = default; 103 }; 104 105 } // namespace android::idmap2 106 107 #endif // IDMAP2_INCLUDE_IDMAP2_RESOURCECONTAINER_H_ 108