| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 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 AAPT_RESOURCE_TABLE_H | 
|  | 18 | #define AAPT_RESOURCE_TABLE_H | 
|  | 19 |  | 
| Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 20 | #include <functional> | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 21 | #include <map> | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include <memory> | 
|  | 23 | #include <string> | 
|  | 24 | #include <tuple> | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 25 | #include <unordered_map> | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 26 | #include <vector> | 
|  | 27 |  | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 28 | #include "Resource.h" | 
|  | 29 | #include "ResourceValues.h" | 
|  | 30 | #include "android-base/macros.h" | 
|  | 31 | #include "androidfw/ConfigDescription.h" | 
|  | 32 | #include "androidfw/IDiagnostics.h" | 
|  | 33 | #include "androidfw/Source.h" | 
|  | 34 | #include "androidfw/StringPiece.h" | 
|  | 35 | #include "androidfw/StringPool.h" | 
|  | 36 | #include "io/File.h" | 
|  | 37 |  | 
| Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 38 | using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags; | 
|  | 39 |  | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | namespace aapt { | 
|  | 41 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 42 | // The Public status of a resource. | 
|  | 43 | struct Visibility { | 
|  | 44 | enum class Level { | 
|  | 45 | kUndefined, | 
|  | 46 | kPrivate, | 
|  | 47 | kPublic, | 
|  | 48 | }; | 
|  | 49 |  | 
|  | 50 | Level level = Level::kUndefined; | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 51 | android::Source source; | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 52 | std::string comment; | 
| Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 53 |  | 
|  | 54 | // Indicates that the resource id may change across builds and that the public R.java identifier | 
|  | 55 | // for this resource should not be final. This is set to `true` for resources in `staging-group` | 
|  | 56 | // tags. | 
|  | 57 | bool staged_api = false; | 
| Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 58 | }; | 
|  | 59 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 60 | // Represents <add-resource> in an overlay. | 
|  | 61 | struct AllowNew { | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 62 | android::Source source; | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 63 | std::string comment; | 
|  | 64 | }; | 
| Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 65 |  | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 66 | // Represents the staged resource id of a finalized resource. | 
|  | 67 | struct StagedId { | 
|  | 68 | ResourceId id; | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 69 | android::Source source; | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 70 | }; | 
|  | 71 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 72 | struct Overlayable { | 
| Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 73 | Overlayable() = default; | 
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 74 | Overlayable(android::StringPiece name, android::StringPiece actor) : name(name), actor(actor) { | 
|  | 75 | } | 
|  | 76 | Overlayable(android::StringPiece name, android::StringPiece actor, const android::Source& source) | 
|  | 77 | : name(name), actor(actor), source(source) { | 
|  | 78 | } | 
| Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 79 |  | 
|  | 80 | static const char* kActorScheme; | 
|  | 81 | std::string name; | 
|  | 82 | std::string actor; | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 83 | android::Source source; | 
| Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 84 | }; | 
|  | 85 |  | 
|  | 86 | // Represents a declaration that a resource is overlayable at runtime. | 
|  | 87 | struct OverlayableItem { | 
|  | 88 | explicit OverlayableItem(const std::shared_ptr<Overlayable>& overlayable) | 
|  | 89 | : overlayable(overlayable) {} | 
| Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 90 | std::shared_ptr<Overlayable> overlayable; | 
| Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 91 | PolicyFlags policies = PolicyFlags::NONE; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | std::string comment; | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 93 | android::Source source; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 94 | }; | 
|  | 95 |  | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 96 | class ResourceConfigValue { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | public: | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 98 | // The configuration for which this value is defined. | 
| MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 99 | const android::ConfigDescription config; | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 100 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 101 | // The product for which this value is defined. | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | const std::string product; | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 103 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 104 | // The actual Value. | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | std::unique_ptr<Value> value; | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 106 |  | 
| Jeremy Meyer | 211bec2 | 2024-06-04 14:22:03 -0700 | [diff] [blame] | 107 | FlagStatus flag_status; | 
|  | 108 |  | 
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 109 | ResourceConfigValue(const android::ConfigDescription& config, android::StringPiece product) | 
|  | 110 | : config(config), product(product) { | 
|  | 111 | } | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 112 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | private: | 
|  | 114 | DISALLOW_COPY_AND_ASSIGN(ResourceConfigValue); | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 115 | }; | 
|  | 116 |  | 
| Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 117 | // Represents a resource entry, which may have varying values for each defined configuration. | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 118 | class ResourceEntry { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | public: | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 120 | // The name of the resource. Immutable, as this determines the order of this resource | 
|  | 121 | // when doing lookups. | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 122 | const std::string name; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 123 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 124 | // The entry ID for this resource (the EEEE in 0xPPTTEEEE). | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 125 | std::optional<ResourceId> id; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 126 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 127 | // Whether this resource is public (and must maintain the same entry ID across builds). | 
|  | 128 | Visibility visibility; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 129 |  | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 130 | std::optional<AllowNew> allow_new; | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 131 |  | 
| Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 132 | // The declarations of this resource as overlayable for RROs | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 133 | std::optional<OverlayableItem> overlayable_item; | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 134 |  | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 135 | // The staged resource id for a finalized resource. | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 136 | std::optional<StagedId> staged_id; | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 137 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 138 | // The resource's values for each configuration. | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | std::vector<std::unique_ptr<ResourceConfigValue>> values; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 140 |  | 
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 141 | explicit ResourceEntry(android::StringPiece name) : name(name) { | 
|  | 142 | } | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 143 |  | 
| MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 144 | ResourceConfigValue* FindValue(const android::ConfigDescription& config, | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 145 | android::StringPiece product = {}); | 
|  | 146 | const ResourceConfigValue* FindValue(const android::ConfigDescription& config, | 
|  | 147 | android::StringPiece product = {}) const; | 
| Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 148 |  | 
| MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 149 | ResourceConfigValue* FindOrCreateValue(const android::ConfigDescription& config, | 
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 150 | android::StringPiece product); | 
| MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 151 | std::vector<ResourceConfigValue*> FindAllValues(const android::ConfigDescription& config); | 
| Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 152 |  | 
|  | 153 | template <typename Func> | 
|  | 154 | std::vector<ResourceConfigValue*> FindValuesIf(Func f) { | 
|  | 155 | std::vector<ResourceConfigValue*> results; | 
|  | 156 | for (auto& config_value : values) { | 
|  | 157 | if (f(config_value.get())) { | 
|  | 158 | results.push_back(config_value.get()); | 
|  | 159 | } | 
|  | 160 | } | 
|  | 161 | return results; | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | bool HasDefaultValue() const; | 
| Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 165 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | private: | 
|  | 167 | DISALLOW_COPY_AND_ASSIGN(ResourceEntry); | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 168 | }; | 
|  | 169 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 170 | // Represents a resource type (eg. string, drawable, layout, etc.) containing resource entries. | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 171 | class ResourceTableType { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 172 | public: | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 173 | // The logical type of resource (string, drawable, layout, etc.). | 
| Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 174 | const ResourceNamedType named_type; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 175 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 176 | // Whether this type is public (and must maintain the same type ID across builds). | 
|  | 177 | Visibility::Level visibility_level = Visibility::Level::kUndefined; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 178 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 179 | // List of resources for this type. | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 180 | std::vector<std::unique_ptr<ResourceEntry>> entries; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 181 |  | 
| Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 182 | explicit ResourceTableType(const ResourceNamedTypeRef& type) | 
|  | 183 | : named_type(type.ToResourceNamedType()) { | 
|  | 184 | } | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 185 |  | 
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 186 | ResourceEntry* CreateEntry(android::StringPiece name); | 
|  | 187 | ResourceEntry* FindEntry(android::StringPiece name) const; | 
|  | 188 | ResourceEntry* FindOrCreateEntry(android::StringPiece name); | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 189 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 190 | private: | 
|  | 191 | DISALLOW_COPY_AND_ASSIGN(ResourceTableType); | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 192 | }; | 
|  | 193 |  | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 194 | class ResourceTablePackage { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 195 | public: | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | std::string name; | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 197 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 198 | std::vector<std::unique_ptr<ResourceTableType>> types; | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 199 |  | 
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 200 | explicit ResourceTablePackage(android::StringPiece name) : name(name) { | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 201 | } | 
|  | 202 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | ResourceTablePackage() = default; | 
| Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 204 | ResourceTableType* FindTypeWithDefaultName(const ResourceType type) const; | 
|  | 205 | ResourceTableType* FindType(const ResourceNamedTypeRef& type) const; | 
|  | 206 | ResourceTableType* FindOrCreateType(const ResourceNamedTypeRef& type); | 
| Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 207 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 208 | private: | 
|  | 209 | DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage); | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 210 | }; | 
|  | 211 |  | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 212 | struct ResourceTableEntryView { | 
|  | 213 | std::string name; | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 214 | std::optional<uint16_t> id; | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 215 | Visibility visibility; | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 216 | std::optional<AllowNew> allow_new; | 
|  | 217 | std::optional<OverlayableItem> overlayable_item; | 
|  | 218 | std::optional<StagedId> staged_id; | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 219 | std::vector<const ResourceConfigValue*> values; | 
|  | 220 |  | 
|  | 221 | const ResourceConfigValue* FindValue(const android::ConfigDescription& config, | 
|  | 222 | android::StringPiece product = {}) const; | 
|  | 223 | }; | 
|  | 224 |  | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 225 | struct ResourceTableTypeView { | 
| Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 226 | ResourceNamedType named_type; | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 227 | std::optional<uint8_t> id; | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 228 | Visibility::Level visibility_level = Visibility::Level::kUndefined; | 
|  | 229 |  | 
|  | 230 | // Entries sorted in ascending entry id order. If ids have not been assigned, the entries are | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 231 | // sorted lexicographically. | 
|  | 232 | std::vector<ResourceTableEntryView> entries; | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 233 | }; | 
|  | 234 |  | 
|  | 235 | struct ResourceTablePackageView { | 
|  | 236 | std::string name; | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 237 | std::optional<uint8_t> id; | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 238 | // Types sorted in ascending type id order. If ids have not been assigned, the types are sorted by | 
|  | 239 | // their declaration order in the ResourceType enum. | 
|  | 240 | std::vector<ResourceTableTypeView> types; | 
|  | 241 | }; | 
|  | 242 |  | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 243 | struct ResourceTableViewOptions { | 
|  | 244 | bool create_alias_entries = false; | 
|  | 245 | }; | 
|  | 246 |  | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 247 | struct ResourceTableView { | 
|  | 248 | // Packages sorted in ascending package id order. If ids have not been assigned, the packages are | 
|  | 249 | // sorted lexicographically. | 
|  | 250 | std::vector<ResourceTablePackageView> packages; | 
|  | 251 | }; | 
|  | 252 |  | 
|  | 253 | enum class OnIdConflict { | 
|  | 254 | // If the resource entry already exists but has a different resource id, the resource value will | 
|  | 255 | // not be added to the table. | 
|  | 256 | ERROR, | 
|  | 257 |  | 
|  | 258 | // If the resource entry already exists but has a different resource id, create a new resource | 
|  | 259 | // with this resource name and id combination. | 
|  | 260 | CREATE_ENTRY, | 
|  | 261 | }; | 
|  | 262 |  | 
|  | 263 | struct NewResource { | 
|  | 264 | ResourceName name; | 
|  | 265 | std::unique_ptr<Value> value; | 
|  | 266 | android::ConfigDescription config; | 
|  | 267 | std::string product; | 
|  | 268 | std::optional<std::pair<ResourceId, OnIdConflict>> id; | 
|  | 269 | std::optional<Visibility> visibility; | 
|  | 270 | std::optional<OverlayableItem> overlayable; | 
|  | 271 | std::optional<AllowNew> allow_new; | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 272 | std::optional<StagedId> staged_id; | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 273 | bool allow_mangled = false; | 
| Jeremy Meyer | 211bec2 | 2024-06-04 14:22:03 -0700 | [diff] [blame] | 274 | FlagStatus flag_status; | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 275 | }; | 
|  | 276 |  | 
|  | 277 | struct NewResourceBuilder { | 
|  | 278 | explicit NewResourceBuilder(const ResourceNameRef& name); | 
|  | 279 | explicit NewResourceBuilder(const std::string& name); | 
|  | 280 | NewResourceBuilder& SetValue(std::unique_ptr<Value> value, android::ConfigDescription config = {}, | 
|  | 281 | std::string product = {}); | 
|  | 282 | NewResourceBuilder& SetId(ResourceId id, OnIdConflict on_conflict = OnIdConflict::ERROR); | 
|  | 283 | NewResourceBuilder& SetVisibility(Visibility id); | 
|  | 284 | NewResourceBuilder& SetOverlayable(OverlayableItem overlayable); | 
|  | 285 | NewResourceBuilder& SetAllowNew(AllowNew allow_new); | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 286 | NewResourceBuilder& SetStagedId(StagedId id); | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 287 | NewResourceBuilder& SetAllowMangled(bool allow_mangled); | 
| Jeremy Meyer | 211bec2 | 2024-06-04 14:22:03 -0700 | [diff] [blame] | 288 | NewResourceBuilder& SetFlagStatus(FlagStatus flag_status); | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 289 | NewResource Build(); | 
|  | 290 |  | 
|  | 291 | private: | 
|  | 292 | NewResource res_; | 
|  | 293 | }; | 
|  | 294 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 295 | // The container and index for all resources defined for an app. | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 296 | class ResourceTable { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 297 | public: | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 298 | enum class Validation { | 
|  | 299 | kEnabled, | 
|  | 300 | kDisabled, | 
|  | 301 | }; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 302 |  | 
| Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 303 | enum class CollisionResult { kKeepBoth, kKeepOriginal, kConflict, kTakeNew }; | 
| Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 304 |  | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 305 | ResourceTable() = default; | 
|  | 306 | explicit ResourceTable(Validation validation); | 
| Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 307 |  | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 308 | bool AddResource(NewResource&& res, android::IDiagnostics* diag); | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 309 |  | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 310 | // Retrieves a sorted a view of the packages, types, and entries sorted in ascending resource id | 
|  | 311 | // order. | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 312 | ResourceTableView GetPartitionedView(const ResourceTableViewOptions& options = {}) const; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 313 |  | 
| Yurii Zubrytskyi | cf91ab8 | 2023-04-24 18:34:13 -0700 | [diff] [blame] | 314 | using ReferencedPackages = std::map<uint8_t, std::string>; | 
|  | 315 | const ReferencedPackages& GetReferencedPackages() const { | 
|  | 316 | return included_packages_; | 
|  | 317 | } | 
|  | 318 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 319 | struct SearchResult { | 
|  | 320 | ResourceTablePackage* package; | 
|  | 321 | ResourceTableType* type; | 
|  | 322 | ResourceEntry* entry; | 
|  | 323 | }; | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 324 |  | 
| Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 325 | std::optional<SearchResult> FindResource(const ResourceNameRef& name) const; | 
|  | 326 | std::optional<SearchResult> FindResource(const ResourceNameRef& name, ResourceId id) const; | 
| Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 327 | bool RemoveResource(const ResourceNameRef& name, ResourceId id) const; | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 328 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 329 | // Returns the package struct with the given name, or nullptr if such a package does not | 
|  | 330 | // exist. The empty string is a valid package and typically is used to represent the | 
|  | 331 | // 'current' package before it is known to the ResourceTable. | 
| Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 332 | ResourceTablePackage* FindPackage(android::StringPiece name) const; | 
|  | 333 | ResourceTablePackage* FindOrCreatePackage(android::StringPiece name); | 
| David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 334 |  | 
| Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 335 | std::unique_ptr<ResourceTable> Clone() const; | 
|  | 336 |  | 
| Jeremy Meyer | 211bec2 | 2024-06-04 14:22:03 -0700 | [diff] [blame] | 337 | // When a collision of resources occurs, these methods decide which value to keep. | 
|  | 338 | static CollisionResult ResolveFlagCollision(FlagStatus existing, FlagStatus incoming); | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 339 | static CollisionResult ResolveValueCollision(Value* existing, Value* incoming); | 
|  | 340 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 341 | // The string pool used by this resource table. Values that reference strings must use | 
|  | 342 | // this pool to create their strings. | 
|  | 343 | // NOTE: `string_pool` must come before `packages` so that it is destroyed after. | 
|  | 344 | // When `string_pool` references are destroyed (as they will be when `packages` is destroyed), | 
|  | 345 | // they decrement a refCount, which would cause invalid memory access if the pool was already | 
|  | 346 | // destroyed. | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 347 | android::StringPool string_pool; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 348 |  | 
| David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 349 | // The list of packages in this table, sorted alphabetically by package name and increasing | 
|  | 350 | // package ID (missing ID being the lowest). | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 351 | std::vector<std::unique_ptr<ResourceTablePackage>> packages; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 352 |  | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 353 | // Set of dynamic packages that this table may reference. Their package names get encoded | 
|  | 354 | // into the resources.arsc along with their compile-time assigned IDs. | 
| Yurii Zubrytskyi | cf91ab8 | 2023-04-24 18:34:13 -0700 | [diff] [blame] | 355 | ReferencedPackages included_packages_; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 356 |  | 
|  | 357 | private: | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 358 | DISALLOW_COPY_AND_ASSIGN(ResourceTable); | 
| Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 359 |  | 
|  | 360 | Validation validation_ = Validation::kEnabled; | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 361 | }; | 
|  | 362 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 363 | }  // namespace aapt | 
| Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 364 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 365 | #endif  // AAPT_RESOURCE_TABLE_H |