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 | |
| 20 | #include "ConfigDescription.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include "Resource.h" |
| 23 | #include "ResourceValues.h" |
| 24 | #include "Source.h" |
| 25 | #include "StringPool.h" |
| 26 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 27 | #include <android-base/macros.h> |
| 28 | #include <map> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | #include <memory> |
| 30 | #include <string> |
| 31 | #include <tuple> |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 32 | #include <unordered_map> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 33 | #include <vector> |
| 34 | |
| 35 | namespace aapt { |
| 36 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 37 | enum class SymbolState { |
| 38 | kUndefined, |
| 39 | kPublic, |
| 40 | kPrivate |
| 41 | }; |
| 42 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | /** |
| 44 | * The Public status of a resource. |
| 45 | */ |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 46 | struct Symbol { |
| 47 | SymbolState state = SymbolState::kUndefined; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 48 | Source source; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | std::u16string comment; |
| 50 | }; |
| 51 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 52 | class ResourceConfigValue { |
| 53 | public: |
| 54 | /** |
| 55 | * The configuration for which this value is defined. |
| 56 | */ |
| 57 | const ConfigDescription config; |
| 58 | |
| 59 | /** |
| 60 | * The product for which this value is defined. |
| 61 | */ |
| 62 | const std::string product; |
| 63 | |
| 64 | /** |
| 65 | * The actual Value. |
| 66 | */ |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 67 | std::unique_ptr<Value> value; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 68 | |
| 69 | ResourceConfigValue(const ConfigDescription& config, const StringPiece& product) : |
| 70 | config(config), product(product.toString()) { } |
| 71 | |
| 72 | private: |
| 73 | DISALLOW_COPY_AND_ASSIGN(ResourceConfigValue); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /** |
| 77 | * Represents a resource entry, which may have |
| 78 | * varying values for each defined configuration. |
| 79 | */ |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 80 | class ResourceEntry { |
| 81 | public: |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 82 | /** |
| 83 | * The name of the resource. Immutable, as |
| 84 | * this determines the order of this resource |
| 85 | * when doing lookups. |
| 86 | */ |
| 87 | const std::u16string name; |
| 88 | |
| 89 | /** |
| 90 | * The entry ID for this resource. |
| 91 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 92 | Maybe<uint16_t> id; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 93 | |
| 94 | /** |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 95 | * Whether this resource is public (and must maintain the same entry ID across builds). |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 96 | */ |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 97 | Symbol symbolStatus; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * The resource's values for each configuration. |
| 101 | */ |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 102 | std::vector<std::unique_ptr<ResourceConfigValue>> values; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 103 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 104 | ResourceEntry(const StringPiece16& name) : name(name.toString()) { } |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 105 | |
| 106 | ResourceConfigValue* findValue(const ConfigDescription& config); |
| 107 | ResourceConfigValue* findValue(const ConfigDescription& config, const StringPiece& product); |
| 108 | ResourceConfigValue* findOrCreateValue(const ConfigDescription& config, |
| 109 | const StringPiece& product); |
| 110 | std::vector<ResourceConfigValue*> findAllValues(const ConfigDescription& config); |
| 111 | |
| 112 | private: |
| 113 | DISALLOW_COPY_AND_ASSIGN(ResourceEntry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * Represents a resource type, which holds entries defined |
| 118 | * for this type. |
| 119 | */ |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 120 | class ResourceTableType { |
| 121 | public: |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 122 | /** |
| 123 | * The logical type of resource (string, drawable, layout, etc.). |
| 124 | */ |
| 125 | const ResourceType type; |
| 126 | |
| 127 | /** |
| 128 | * The type ID for this resource. |
| 129 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 130 | Maybe<uint8_t> id; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Whether this type is public (and must maintain the same |
| 134 | * type ID across builds). |
| 135 | */ |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 136 | Symbol symbolStatus; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * List of resources for this type. |
| 140 | */ |
| 141 | std::vector<std::unique_ptr<ResourceEntry>> entries; |
| 142 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 143 | explicit ResourceTableType(const ResourceType type) : type(type) { } |
| 144 | |
| 145 | ResourceEntry* findEntry(const StringPiece16& name); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 146 | ResourceEntry* findOrCreateEntry(const StringPiece16& name); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 147 | |
| 148 | private: |
| 149 | DISALLOW_COPY_AND_ASSIGN(ResourceTableType); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | enum class PackageType { |
| 153 | System, |
| 154 | Vendor, |
| 155 | App, |
| 156 | Dynamic |
| 157 | }; |
| 158 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 159 | class ResourceTablePackage { |
| 160 | public: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 161 | PackageType type = PackageType::App; |
| 162 | Maybe<uint8_t> id; |
| 163 | std::u16string name; |
| 164 | |
| 165 | std::vector<std::unique_ptr<ResourceTableType>> types; |
| 166 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 167 | ResourceTablePackage() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 168 | ResourceTableType* findType(ResourceType type); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 169 | ResourceTableType* findOrCreateType(const ResourceType type); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 170 | |
| 171 | private: |
| 172 | DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | /** |
| 176 | * The container and index for all resources defined for an app. This gets |
| 177 | * flattened into a binary resource table (resources.arsc). |
| 178 | */ |
| 179 | class ResourceTable { |
| 180 | public: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 181 | ResourceTable() = default; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 182 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 183 | /** |
| 184 | * When a collision of resources occurs, this method decides which value to keep. |
| 185 | * Returns -1 if the existing value should be chosen. |
| 186 | * Returns 0 if the collision can not be resolved (error). |
| 187 | * Returns 1 if the incoming value should be chosen. |
| 188 | */ |
| 189 | static int resolveValueCollision(Value* existing, Value* incoming); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 190 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 191 | bool addResource(const ResourceNameRef& name, |
| 192 | const ConfigDescription& config, |
| 193 | const StringPiece& product, |
| 194 | std::unique_ptr<Value> value, |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 195 | IDiagnostics* diag); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 196 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 197 | bool addResource(const ResourceNameRef& name, |
| 198 | const ResourceId resId, |
| 199 | const ConfigDescription& config, |
| 200 | const StringPiece& product, |
| 201 | std::unique_ptr<Value> value, |
| 202 | IDiagnostics* diag); |
| 203 | |
| 204 | bool addFileReference(const ResourceNameRef& name, |
| 205 | const ConfigDescription& config, |
| 206 | const Source& source, |
| 207 | const StringPiece16& path, |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 208 | IDiagnostics* diag); |
| 209 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 210 | bool addFileReference(const ResourceNameRef& name, |
| 211 | const ConfigDescription& config, |
| 212 | const Source& source, |
| 213 | const StringPiece16& path, |
| 214 | std::function<int(Value*,Value*)> conflictResolver, |
| 215 | IDiagnostics* diag); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 216 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 217 | /** |
| 218 | * Same as addResource, but doesn't verify the validity of the name. This is used |
| 219 | * when loading resources from an existing binary resource table that may have mangled |
| 220 | * names. |
| 221 | */ |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 222 | bool addResourceAllowMangled(const ResourceNameRef& name, |
| 223 | const ConfigDescription& config, |
| 224 | const StringPiece& product, |
| 225 | std::unique_ptr<Value> value, |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 226 | IDiagnostics* diag); |
| 227 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 228 | bool addResourceAllowMangled(const ResourceNameRef& name, |
| 229 | const ResourceId id, |
| 230 | const ConfigDescription& config, |
| 231 | const StringPiece& product, |
| 232 | std::unique_ptr<Value> value, |
| 233 | IDiagnostics* diag); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 234 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 235 | bool setSymbolState(const ResourceNameRef& name, |
| 236 | const ResourceId resId, |
| 237 | const Symbol& symbol, |
| 238 | IDiagnostics* diag); |
| 239 | |
| 240 | bool setSymbolStateAllowMangled(const ResourceNameRef& name, |
| 241 | const ResourceId resId, |
| 242 | const Symbol& symbol, |
| 243 | IDiagnostics* diag); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 244 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 245 | struct SearchResult { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 246 | ResourceTablePackage* package; |
| 247 | ResourceTableType* type; |
| 248 | ResourceEntry* entry; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 249 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 250 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 251 | Maybe<SearchResult> findResource(const ResourceNameRef& name); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 252 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 253 | /** |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 254 | * The string pool used by this resource table. Values that reference strings must use |
| 255 | * this pool to create their strings. |
| 256 | * |
| 257 | * NOTE: `stringPool` must come before `packages` so that it is destroyed after. |
| 258 | * When `string pool` references are destroyed (as they will be when `packages` |
| 259 | * is destroyed), they decrement a refCount, which would cause invalid |
| 260 | * memory access if the pool was already destroyed. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 261 | */ |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 262 | StringPool stringPool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 263 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 264 | /** |
| 265 | * The list of packages in this table, sorted alphabetically by package name. |
| 266 | */ |
| 267 | std::vector<std::unique_ptr<ResourceTablePackage>> packages; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 268 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 269 | /** |
| 270 | * Returns the package struct with the given name, or nullptr if such a package does not |
| 271 | * exist. The empty string is a valid package and typically is used to represent the |
| 272 | * 'current' package before it is known to the ResourceTable. |
| 273 | */ |
| 274 | ResourceTablePackage* findPackage(const StringPiece16& name); |
| 275 | |
| 276 | ResourceTablePackage* findPackageById(uint8_t id); |
| 277 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 278 | ResourceTablePackage* createPackage(const StringPiece16& name, Maybe<uint8_t> id = {}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 279 | |
| 280 | private: |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 281 | ResourceTablePackage* findOrCreatePackage(const StringPiece16& name); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 282 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 283 | bool addResourceImpl(const ResourceNameRef& name, |
| 284 | ResourceId resId, |
| 285 | const ConfigDescription& config, |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 286 | const StringPiece& product, |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 287 | std::unique_ptr<Value> value, |
| 288 | const char16_t* validChars, |
| 289 | std::function<int(Value*,Value*)> conflictResolver, |
| 290 | IDiagnostics* diag); |
| 291 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame^] | 292 | bool setSymbolStateImpl(const ResourceNameRef& name, |
| 293 | ResourceId resId, |
| 294 | const Symbol& symbol, |
| 295 | const char16_t* validChars, |
| 296 | IDiagnostics* diag); |
| 297 | |
| 298 | DISALLOW_COPY_AND_ASSIGN(ResourceTable); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 299 | }; |
| 300 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 301 | } // namespace aapt |
| 302 | |
| 303 | #endif // AAPT_RESOURCE_TABLE_H |