blob: 7f5c2b8c0f3709fedaa5d170d8e7e1b711825677 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinski1ab598f2015-08-14 14:26:04 -070021#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include "Resource.h"
23#include "ResourceValues.h"
24#include "Source.h"
25#include "StringPool.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080026#include "io/File.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080028#include <android-base/macros.h>
29#include <map>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030#include <memory>
31#include <string>
32#include <tuple>
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080033#include <unordered_map>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034#include <vector>
35
36namespace aapt {
37
Adam Lesinski9e10ac72015-10-16 14:37:48 -070038enum class SymbolState {
39 kUndefined,
40 kPublic,
41 kPrivate
42};
43
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044/**
45 * The Public status of a resource.
46 */
Adam Lesinski9e10ac72015-10-16 14:37:48 -070047struct Symbol {
48 SymbolState state = SymbolState::kUndefined;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049 Source source;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050 std::u16string comment;
51};
52
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080053class ResourceConfigValue {
54public:
55 /**
56 * The configuration for which this value is defined.
57 */
58 const ConfigDescription config;
59
60 /**
61 * The product for which this value is defined.
62 */
63 const std::string product;
64
65 /**
66 * The actual Value.
67 */
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068 std::unique_ptr<Value> value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080069
70 ResourceConfigValue(const ConfigDescription& config, const StringPiece& product) :
71 config(config), product(product.toString()) { }
72
73private:
74 DISALLOW_COPY_AND_ASSIGN(ResourceConfigValue);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075};
76
77/**
78 * Represents a resource entry, which may have
79 * varying values for each defined configuration.
80 */
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080081class ResourceEntry {
82public:
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083 /**
84 * The name of the resource. Immutable, as
85 * this determines the order of this resource
86 * when doing lookups.
87 */
88 const std::u16string name;
89
90 /**
91 * The entry ID for this resource.
92 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -070093 Maybe<uint16_t> id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080094
95 /**
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080096 * Whether this resource is public (and must maintain the same entry ID across builds).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097 */
Adam Lesinski9e10ac72015-10-16 14:37:48 -070098 Symbol symbolStatus;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099
100 /**
101 * The resource's values for each configuration.
102 */
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800103 std::vector<std::unique_ptr<ResourceConfigValue>> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700105 ResourceEntry(const StringPiece16& name) : name(name.toString()) { }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800106
107 ResourceConfigValue* findValue(const ConfigDescription& config);
108 ResourceConfigValue* findValue(const ConfigDescription& config, const StringPiece& product);
109 ResourceConfigValue* findOrCreateValue(const ConfigDescription& config,
110 const StringPiece& product);
111 std::vector<ResourceConfigValue*> findAllValues(const ConfigDescription& config);
112
113private:
114 DISALLOW_COPY_AND_ASSIGN(ResourceEntry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115};
116
117/**
118 * Represents a resource type, which holds entries defined
119 * for this type.
120 */
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800121class ResourceTableType {
122public:
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123 /**
124 * The logical type of resource (string, drawable, layout, etc.).
125 */
126 const ResourceType type;
127
128 /**
129 * The type ID for this resource.
130 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131 Maybe<uint8_t> id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132
133 /**
134 * Whether this type is public (and must maintain the same
135 * type ID across builds).
136 */
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700137 Symbol symbolStatus;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138
139 /**
140 * List of resources for this type.
141 */
142 std::vector<std::unique_ptr<ResourceEntry>> entries;
143
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144 explicit ResourceTableType(const ResourceType type) : type(type) { }
145
146 ResourceEntry* findEntry(const StringPiece16& name);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147 ResourceEntry* findOrCreateEntry(const StringPiece16& name);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800148
149private:
150 DISALLOW_COPY_AND_ASSIGN(ResourceTableType);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151};
152
153enum class PackageType {
154 System,
155 Vendor,
156 App,
157 Dynamic
158};
159
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800160class ResourceTablePackage {
161public:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162 PackageType type = PackageType::App;
163 Maybe<uint8_t> id;
164 std::u16string name;
165
166 std::vector<std::unique_ptr<ResourceTableType>> types;
167
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800168 ResourceTablePackage() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169 ResourceTableType* findType(ResourceType type);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170 ResourceTableType* findOrCreateType(const ResourceType type);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800171
172private:
173 DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174};
175
176/**
177 * The container and index for all resources defined for an app. This gets
178 * flattened into a binary resource table (resources.arsc).
179 */
180class ResourceTable {
181public:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700182 ResourceTable() = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800183
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700184 /**
185 * When a collision of resources occurs, this method decides which value to keep.
186 * Returns -1 if the existing value should be chosen.
187 * Returns 0 if the collision can not be resolved (error).
188 * Returns 1 if the incoming value should be chosen.
189 */
190 static int resolveValueCollision(Value* existing, Value* incoming);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800191
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800192 bool addResource(const ResourceNameRef& name,
193 const ConfigDescription& config,
194 const StringPiece& product,
195 std::unique_ptr<Value> value,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700196 IDiagnostics* diag);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700197
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800198 bool addResource(const ResourceNameRef& name,
199 const ResourceId resId,
200 const ConfigDescription& config,
201 const StringPiece& product,
202 std::unique_ptr<Value> value,
203 IDiagnostics* diag);
204
205 bool addFileReference(const ResourceNameRef& name,
Adam Lesinski355f2852016-02-13 20:26:45 -0800206 const ConfigDescription& config,
207 const Source& source,
208 const StringPiece16& path,
209 IDiagnostics* diag);
Adam Lesinskifb48d292015-11-07 15:52:13 -0800210
Adam Lesinski355f2852016-02-13 20:26:45 -0800211 bool addFileReferenceAllowMangled(const ResourceNameRef& name,
212 const ConfigDescription& config,
213 const Source& source,
214 const StringPiece16& path,
215 io::IFile* file,
216 IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800217
Adam Lesinski330edcd2015-05-04 17:40:56 -0700218 /**
219 * Same as addResource, but doesn't verify the validity of the name. This is used
220 * when loading resources from an existing binary resource table that may have mangled
221 * names.
222 */
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800223 bool addResourceAllowMangled(const ResourceNameRef& name,
224 const ConfigDescription& config,
225 const StringPiece& product,
226 std::unique_ptr<Value> value,
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700227 IDiagnostics* diag);
228
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800229 bool addResourceAllowMangled(const ResourceNameRef& name,
230 const ResourceId id,
231 const ConfigDescription& config,
232 const StringPiece& product,
233 std::unique_ptr<Value> value,
234 IDiagnostics* diag);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700235
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800236 bool setSymbolState(const ResourceNameRef& name,
237 const ResourceId resId,
238 const Symbol& symbol,
239 IDiagnostics* diag);
240
241 bool setSymbolStateAllowMangled(const ResourceNameRef& name,
242 const ResourceId resId,
243 const Symbol& symbol,
244 IDiagnostics* diag);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700245
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700246 struct SearchResult {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700247 ResourceTablePackage* package;
248 ResourceTableType* type;
249 ResourceEntry* entry;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800251
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252 Maybe<SearchResult> findResource(const ResourceNameRef& name);
Adam Lesinski769de982015-04-10 19:43:55 -0700253
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800254 /**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700255 * The string pool used by this resource table. Values that reference strings must use
256 * this pool to create their strings.
257 *
258 * NOTE: `stringPool` must come before `packages` so that it is destroyed after.
259 * When `string pool` references are destroyed (as they will be when `packages`
260 * is destroyed), they decrement a refCount, which would cause invalid
261 * memory access if the pool was already destroyed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800262 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700263 StringPool stringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800264
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700265 /**
266 * The list of packages in this table, sorted alphabetically by package name.
267 */
268 std::vector<std::unique_ptr<ResourceTablePackage>> packages;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800269
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700270 /**
271 * Returns the package struct with the given name, or nullptr if such a package does not
272 * exist. The empty string is a valid package and typically is used to represent the
273 * 'current' package before it is known to the ResourceTable.
274 */
275 ResourceTablePackage* findPackage(const StringPiece16& name);
276
277 ResourceTablePackage* findPackageById(uint8_t id);
278
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700279 ResourceTablePackage* createPackage(const StringPiece16& name, Maybe<uint8_t> id = {});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280
281private:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700282 ResourceTablePackage* findOrCreatePackage(const StringPiece16& name);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800283
Adam Lesinski355f2852016-02-13 20:26:45 -0800284 bool addFileReferenceImpl(const ResourceNameRef& name,
285 const ConfigDescription& config,
286 const Source& source,
287 const StringPiece16& path,
288 io::IFile* file,
289 const char16_t* validChars,
290 IDiagnostics* diag);
291
Adam Lesinskifb48d292015-11-07 15:52:13 -0800292 bool addResourceImpl(const ResourceNameRef& name,
293 ResourceId resId,
294 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800295 const StringPiece& product,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800296 std::unique_ptr<Value> value,
297 const char16_t* validChars,
298 std::function<int(Value*,Value*)> conflictResolver,
299 IDiagnostics* diag);
300
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800301 bool setSymbolStateImpl(const ResourceNameRef& name,
302 ResourceId resId,
303 const Symbol& symbol,
304 const char16_t* validChars,
305 IDiagnostics* diag);
306
307 DISALLOW_COPY_AND_ASSIGN(ResourceTable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308};
309
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800310} // namespace aapt
311
312#endif // AAPT_RESOURCE_TABLE_H