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