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