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