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 | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 21 | #include "Resource.h" |
| 22 | #include "ResourceValues.h" |
| 23 | #include "Source.h" |
| 24 | #include "StringPool.h" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 25 | #include "io/File.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 26 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 27 | #include "android-base/macros.h" |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 28 | #include "androidfw/ConfigDescription.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 29 | #include "androidfw/StringPiece.h" |
| 30 | |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 31 | #include <functional> |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 32 | #include <map> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 33 | #include <memory> |
| 34 | #include <string> |
| 35 | #include <tuple> |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 36 | #include <unordered_map> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 37 | #include <vector> |
| 38 | |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 39 | using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags; |
| 40 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 41 | namespace aapt { |
| 42 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 43 | // The Public status of a resource. |
| 44 | struct Visibility { |
| 45 | enum class Level { |
| 46 | kUndefined, |
| 47 | kPrivate, |
| 48 | kPublic, |
| 49 | }; |
| 50 | |
| 51 | Level level = Level::kUndefined; |
| 52 | Source source; |
| 53 | std::string comment; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 54 | |
| 55 | // Indicates that the resource id may change across builds and that the public R.java identifier |
| 56 | // for this resource should not be final. This is set to `true` for resources in `staging-group` |
| 57 | // tags. |
| 58 | bool staged_api = false; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 61 | // Represents <add-resource> in an overlay. |
| 62 | struct AllowNew { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | Source source; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 64 | std::string comment; |
| 65 | }; |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 66 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame^] | 67 | // Represents the staged resource id of a finalized resource. |
| 68 | struct StagedId { |
| 69 | ResourceId id; |
| 70 | Source source; |
| 71 | }; |
| 72 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 73 | struct Overlayable { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 74 | Overlayable() = default; |
| 75 | Overlayable(const android::StringPiece& name, const android::StringPiece& actor) |
| 76 | : name(name.to_string()), actor(actor.to_string()) {} |
| 77 | Overlayable(const android::StringPiece& name, const android::StringPiece& actor, |
| 78 | const Source& source) |
| 79 | : name(name.to_string()), actor(actor.to_string()), source(source ){} |
| 80 | |
| 81 | static const char* kActorScheme; |
| 82 | std::string name; |
| 83 | std::string actor; |
| 84 | Source source; |
| 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; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 94 | 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 | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 123 | Maybe<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 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 128 | Maybe<AllowNew> allow_new; |
| 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 | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 131 | Maybe<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. |
| 134 | Maybe<StagedId> staged_id; |
| 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.). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | const ResourceType 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 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 179 | explicit ResourceTableType(const ResourceType type) : type(type) {} |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 180 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 181 | ResourceEntry* CreateEntry(const android::StringPiece& name); |
| 182 | ResourceEntry* FindEntry(const android::StringPiece& name) const; |
| 183 | ResourceEntry* FindOrCreateEntry(const android::StringPiece& name); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 184 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 185 | private: |
| 186 | DISALLOW_COPY_AND_ASSIGN(ResourceTableType); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 189 | class ResourceTablePackage { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 190 | public: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 191 | std::string name; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 192 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 193 | std::vector<std::unique_ptr<ResourceTableType>> types; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 194 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 195 | explicit ResourceTablePackage(const android::StringPiece& name) : name(name.to_string()) { |
| 196 | } |
| 197 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 198 | ResourceTablePackage() = default; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 199 | ResourceTableType* FindType(ResourceType type) const; |
| 200 | ResourceTableType* FindOrCreateType(ResourceType type); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 201 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 202 | private: |
| 203 | DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 204 | }; |
| 205 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame^] | 206 | struct ResourceTableEntryView { |
| 207 | std::string name; |
| 208 | Maybe<uint16_t> id; |
| 209 | Visibility visibility; |
| 210 | Maybe<AllowNew> allow_new; |
| 211 | Maybe<OverlayableItem> overlayable_item; |
| 212 | Maybe<StagedId> staged_id; |
| 213 | std::vector<const ResourceConfigValue*> values; |
| 214 | |
| 215 | const ResourceConfigValue* FindValue(const android::ConfigDescription& config, |
| 216 | android::StringPiece product = {}) const; |
| 217 | }; |
| 218 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 219 | struct ResourceTableTypeView { |
| 220 | ResourceType type; |
| 221 | Maybe<uint8_t> id; |
| 222 | Visibility::Level visibility_level = Visibility::Level::kUndefined; |
| 223 | |
| 224 | // 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^] | 225 | // sorted lexicographically. |
| 226 | std::vector<ResourceTableEntryView> entries; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | struct ResourceTablePackageView { |
| 230 | std::string name; |
| 231 | Maybe<uint8_t> id; |
| 232 | // Types sorted in ascending type id order. If ids have not been assigned, the types are sorted by |
| 233 | // their declaration order in the ResourceType enum. |
| 234 | std::vector<ResourceTableTypeView> types; |
| 235 | }; |
| 236 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame^] | 237 | struct ResourceTableViewOptions { |
| 238 | bool create_alias_entries = false; |
| 239 | }; |
| 240 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 241 | struct ResourceTableView { |
| 242 | // Packages sorted in ascending package id order. If ids have not been assigned, the packages are |
| 243 | // sorted lexicographically. |
| 244 | std::vector<ResourceTablePackageView> packages; |
| 245 | }; |
| 246 | |
| 247 | enum class OnIdConflict { |
| 248 | // If the resource entry already exists but has a different resource id, the resource value will |
| 249 | // not be added to the table. |
| 250 | ERROR, |
| 251 | |
| 252 | // If the resource entry already exists but has a different resource id, create a new resource |
| 253 | // with this resource name and id combination. |
| 254 | CREATE_ENTRY, |
| 255 | }; |
| 256 | |
| 257 | struct NewResource { |
| 258 | ResourceName name; |
| 259 | std::unique_ptr<Value> value; |
| 260 | android::ConfigDescription config; |
| 261 | std::string product; |
| 262 | std::optional<std::pair<ResourceId, OnIdConflict>> id; |
| 263 | std::optional<Visibility> visibility; |
| 264 | std::optional<OverlayableItem> overlayable; |
| 265 | std::optional<AllowNew> allow_new; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame^] | 266 | std::optional<StagedId> staged_id; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 267 | bool allow_mangled = false; |
| 268 | }; |
| 269 | |
| 270 | struct NewResourceBuilder { |
| 271 | explicit NewResourceBuilder(const ResourceNameRef& name); |
| 272 | explicit NewResourceBuilder(const std::string& name); |
| 273 | NewResourceBuilder& SetValue(std::unique_ptr<Value> value, android::ConfigDescription config = {}, |
| 274 | std::string product = {}); |
| 275 | NewResourceBuilder& SetId(ResourceId id, OnIdConflict on_conflict = OnIdConflict::ERROR); |
| 276 | NewResourceBuilder& SetVisibility(Visibility id); |
| 277 | NewResourceBuilder& SetOverlayable(OverlayableItem overlayable); |
| 278 | NewResourceBuilder& SetAllowNew(AllowNew allow_new); |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame^] | 279 | NewResourceBuilder& SetStagedId(StagedId id); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 280 | NewResourceBuilder& SetAllowMangled(bool allow_mangled); |
| 281 | NewResource Build(); |
| 282 | |
| 283 | private: |
| 284 | NewResource res_; |
| 285 | }; |
| 286 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 287 | // The container and index for all resources defined for an app. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 288 | class ResourceTable { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 289 | public: |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 290 | enum class Validation { |
| 291 | kEnabled, |
| 292 | kDisabled, |
| 293 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 294 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 295 | enum class CollisionResult { kKeepBoth, kKeepOriginal, kConflict, kTakeNew }; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 296 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 297 | ResourceTable() = default; |
| 298 | explicit ResourceTable(Validation validation); |
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 | bool AddResource(NewResource&& res, IDiagnostics* diag); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 301 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 302 | // Retrieves a sorted a view of the packages, types, and entries sorted in ascending resource id |
| 303 | // order. |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame^] | 304 | ResourceTableView GetPartitionedView(const ResourceTableViewOptions& options = {}) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 305 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 306 | struct SearchResult { |
| 307 | ResourceTablePackage* package; |
| 308 | ResourceTableType* type; |
| 309 | ResourceEntry* entry; |
| 310 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 311 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 312 | Maybe<SearchResult> FindResource(const ResourceNameRef& name) const; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 313 | Maybe<SearchResult> FindResource(const ResourceNameRef& name, ResourceId id) const; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame^] | 314 | bool RemoveResource(const ResourceNameRef& name, ResourceId id) const; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 315 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 316 | // Returns the package struct with the given name, or nullptr if such a package does not |
| 317 | // exist. The empty string is a valid package and typically is used to represent the |
| 318 | // 'current' package before it is known to the ResourceTable. |
| 319 | ResourceTablePackage* FindPackage(const android::StringPiece& name) const; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 320 | ResourceTablePackage* FindOrCreatePackage(const android::StringPiece& name); |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 321 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 322 | std::unique_ptr<ResourceTable> Clone() const; |
| 323 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 324 | // When a collision of resources occurs, this method decides which value to keep. |
| 325 | static CollisionResult ResolveValueCollision(Value* existing, Value* incoming); |
| 326 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 327 | // The string pool used by this resource table. Values that reference strings must use |
| 328 | // this pool to create their strings. |
| 329 | // NOTE: `string_pool` must come before `packages` so that it is destroyed after. |
| 330 | // When `string_pool` references are destroyed (as they will be when `packages` is destroyed), |
| 331 | // they decrement a refCount, which would cause invalid memory access if the pool was already |
| 332 | // destroyed. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 333 | StringPool string_pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 334 | |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 335 | // The list of packages in this table, sorted alphabetically by package name and increasing |
| 336 | // package ID (missing ID being the lowest). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 337 | std::vector<std::unique_ptr<ResourceTablePackage>> packages; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 338 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 339 | // Set of dynamic packages that this table may reference. Their package names get encoded |
| 340 | // into the resources.arsc along with their compile-time assigned IDs. |
| 341 | std::map<size_t, std::string> included_packages_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 342 | |
| 343 | private: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 344 | DISALLOW_COPY_AND_ASSIGN(ResourceTable); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 345 | |
| 346 | Validation validation_ = Validation::kEnabled; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 347 | }; |
| 348 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 349 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 350 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 351 | #endif // AAPT_RESOURCE_TABLE_H |