blob: cf9e7b8bfc00dc33d0fea6d3a8e046869bfed610 [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>
Adam Lesinski458b8772016-04-25 14:20:21 -070029#include <functional>
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080030#include <map>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080031#include <memory>
32#include <string>
33#include <tuple>
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080034#include <unordered_map>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035#include <vector>
36
37namespace aapt {
38
Adam Lesinski9e10ac72015-10-16 14:37:48 -070039enum class SymbolState {
40 kUndefined,
41 kPublic,
42 kPrivate
43};
44
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080045/**
46 * The Public status of a resource.
47 */
Adam Lesinski9e10ac72015-10-16 14:37:48 -070048struct Symbol {
49 SymbolState state = SymbolState::kUndefined;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050 Source source;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051 std::u16string comment;
52};
53
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080054class ResourceConfigValue {
55public:
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 Lesinski6f6ceb72014-11-14 14:48:12 -080069 std::unique_ptr<Value> value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080070
71 ResourceConfigValue(const ConfigDescription& config, const StringPiece& product) :
72 config(config), product(product.toString()) { }
73
74private:
75 DISALLOW_COPY_AND_ASSIGN(ResourceConfigValue);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076};
77
78/**
79 * Represents a resource entry, which may have
80 * varying values for each defined configuration.
81 */
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080082class ResourceEntry {
83public:
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084 /**
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 Lesinski1ab598f2015-08-14 14:26:04 -070094 Maybe<uint16_t> id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095
96 /**
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080097 * Whether this resource is public (and must maintain the same entry ID across builds).
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098 */
Adam Lesinski9e10ac72015-10-16 14:37:48 -070099 Symbol symbolStatus;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100
101 /**
102 * The resource's values for each configuration.
103 */
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800104 std::vector<std::unique_ptr<ResourceConfigValue>> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700106 explicit ResourceEntry(const StringPiece16& name) : name(name.toString()) { }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800107
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 Lesinski458b8772016-04-25 14:20:21 -0700113 std::vector<ResourceConfigValue*> findValuesIf(
114 const std::function<bool(ResourceConfigValue*)>& f);
115
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800116
117private:
118 DISALLOW_COPY_AND_ASSIGN(ResourceEntry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119};
120
121/**
122 * Represents a resource type, which holds entries defined
123 * for this type.
124 */
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800125class ResourceTableType {
126public:
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127 /**
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 Lesinski1ab598f2015-08-14 14:26:04 -0700135 Maybe<uint8_t> id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800136
137 /**
138 * Whether this type is public (and must maintain the same
139 * type ID across builds).
140 */
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700141 Symbol symbolStatus;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142
143 /**
144 * List of resources for this type.
145 */
146 std::vector<std::unique_ptr<ResourceEntry>> entries;
147
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700148 explicit ResourceTableType(const ResourceType type) : type(type) { }
149
150 ResourceEntry* findEntry(const StringPiece16& name);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151 ResourceEntry* findOrCreateEntry(const StringPiece16& name);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800152
153private:
154 DISALLOW_COPY_AND_ASSIGN(ResourceTableType);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155};
156
157enum class PackageType {
158 System,
159 Vendor,
160 App,
161 Dynamic
162};
163
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800164class ResourceTablePackage {
165public:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166 PackageType type = PackageType::App;
167 Maybe<uint8_t> id;
168 std::u16string name;
169
170 std::vector<std::unique_ptr<ResourceTableType>> types;
171
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800172 ResourceTablePackage() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173 ResourceTableType* findType(ResourceType type);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700174 ResourceTableType* findOrCreateType(const ResourceType type);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800175
176private:
177 DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800178};
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 */
184class ResourceTable {
185public:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700186 ResourceTable() = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800187
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700188 /**
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 Lesinski6f6ceb72014-11-14 14:48:12 -0800195
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800196 bool addResource(const ResourceNameRef& name,
197 const ConfigDescription& config,
198 const StringPiece& product,
199 std::unique_ptr<Value> value,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700200 IDiagnostics* diag);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700201
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800202 bool addResource(const ResourceNameRef& name,
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700203 const ResourceId& resId,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800204 const ConfigDescription& config,
205 const StringPiece& product,
206 std::unique_ptr<Value> value,
207 IDiagnostics* diag);
208
209 bool addFileReference(const ResourceNameRef& name,
Adam Lesinski355f2852016-02-13 20:26:45 -0800210 const ConfigDescription& config,
211 const Source& source,
212 const StringPiece16& path,
213 IDiagnostics* diag);
Adam Lesinskifb48d292015-11-07 15:52:13 -0800214
Adam Lesinski355f2852016-02-13 20:26:45 -0800215 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 Lesinski6f6ceb72014-11-14 14:48:12 -0800221
Adam Lesinski330edcd2015-05-04 17:40:56 -0700222 /**
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 Lesinskie4bb9eb2016-02-12 22:18:51 -0800227 bool addResourceAllowMangled(const ResourceNameRef& name,
228 const ConfigDescription& config,
229 const StringPiece& product,
230 std::unique_ptr<Value> value,
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700231 IDiagnostics* diag);
232
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800233 bool addResourceAllowMangled(const ResourceNameRef& name,
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700234 const ResourceId& id,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800235 const ConfigDescription& config,
236 const StringPiece& product,
237 std::unique_ptr<Value> value,
238 IDiagnostics* diag);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700239
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800240 bool setSymbolState(const ResourceNameRef& name,
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700241 const ResourceId& resId,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800242 const Symbol& symbol,
243 IDiagnostics* diag);
244
245 bool setSymbolStateAllowMangled(const ResourceNameRef& name,
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700246 const ResourceId& resId,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800247 const Symbol& symbol,
248 IDiagnostics* diag);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700249
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250 struct SearchResult {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700251 ResourceTablePackage* package;
252 ResourceTableType* type;
253 ResourceEntry* entry;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700254 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256 Maybe<SearchResult> findResource(const ResourceNameRef& name);
Adam Lesinski769de982015-04-10 19:43:55 -0700257
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800258 /**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700259 * 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 Lesinski6f6ceb72014-11-14 14:48:12 -0800266 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700267 StringPool stringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700269 /**
270 * The list of packages in this table, sorted alphabetically by package name.
271 */
272 std::vector<std::unique_ptr<ResourceTablePackage>> packages;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800273
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700274 /**
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 Lesinski9ba47d82015-10-13 11:37:10 -0700283 ResourceTablePackage* createPackage(const StringPiece16& name, Maybe<uint8_t> id = {});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800284
285private:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700286 ResourceTablePackage* findOrCreatePackage(const StringPiece16& name);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800287
Adam Lesinski355f2852016-02-13 20:26:45 -0800288 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 Lesinskifb48d292015-11-07 15:52:13 -0800296 bool addResourceImpl(const ResourceNameRef& name,
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700297 const ResourceId& resId,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800298 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800299 const StringPiece& product,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800300 std::unique_ptr<Value> value,
301 const char16_t* validChars,
302 std::function<int(Value*,Value*)> conflictResolver,
303 IDiagnostics* diag);
304
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800305 bool setSymbolStateImpl(const ResourceNameRef& name,
Chih-Hung Hsiehb3d46b42016-08-12 11:35:17 -0700306 const ResourceId& resId,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800307 const Symbol& symbol,
308 const char16_t* validChars,
309 IDiagnostics* diag);
310
311 DISALLOW_COPY_AND_ASSIGN(ResourceTable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312};
313
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800314} // namespace aapt
315
316#endif // AAPT_RESOURCE_TABLE_H