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