blob: 6c246d0d81d6f5b4ef44eb47927dfab1b5611537 [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,
Adam Lesinski5c3464c2016-08-24 16:03:48 -070041 kPrivate,
Adam Lesinski9e10ac72015-10-16 14:37:48 -070042 kPublic,
Adam Lesinski9e10ac72015-10-16 14:37:48 -070043};
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 Lesinskid0f116b2016-07-08 15:00:32 -070051 std::string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052};
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 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070089 const std::string name;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090
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 Hsieh9b8528f2016-08-10 14:15:30 -0700106 explicit ResourceEntry(const StringPiece& 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
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700150 ResourceEntry* findEntry(const StringPiece& name);
151 ResourceEntry* findOrCreateEntry(const StringPiece& 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;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700168 std::string name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169
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 Lesinski5c3464c2016-08-24 16:03:48 -0700188 enum class CollisionResult {
189 kKeepOriginal,
190 kConflict,
191 kTakeNew
192 };
193
194 using CollisionResolverFunc = std::function<CollisionResult(Value*, Value*)>;
195
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700196 /**
197 * When a collision of resources occurs, this method decides which value to keep.
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700198 */
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700199 static CollisionResult resolveValueCollision(Value* existing, Value* incoming);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800200
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800201 bool addResource(const ResourceNameRef& name,
202 const ConfigDescription& config,
203 const StringPiece& product,
204 std::unique_ptr<Value> value,
Adam Lesinskie78fd612015-10-22 12:48:43 -0700205 IDiagnostics* diag);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700206
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800207 bool addResource(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700208 const ResourceId& resId,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800209 const ConfigDescription& config,
210 const StringPiece& product,
211 std::unique_ptr<Value> value,
212 IDiagnostics* diag);
213
214 bool addFileReference(const ResourceNameRef& name,
Adam Lesinski355f2852016-02-13 20:26:45 -0800215 const ConfigDescription& config,
216 const Source& source,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700217 const StringPiece& path,
Adam Lesinski355f2852016-02-13 20:26:45 -0800218 IDiagnostics* diag);
Adam Lesinskifb48d292015-11-07 15:52:13 -0800219
Adam Lesinski355f2852016-02-13 20:26:45 -0800220 bool addFileReferenceAllowMangled(const ResourceNameRef& name,
221 const ConfigDescription& config,
222 const Source& source,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700223 const StringPiece& path,
Adam Lesinski355f2852016-02-13 20:26:45 -0800224 io::IFile* file,
225 IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800226
Adam Lesinski330edcd2015-05-04 17:40:56 -0700227 /**
228 * Same as addResource, but doesn't verify the validity of the name. This is used
229 * when loading resources from an existing binary resource table that may have mangled
230 * names.
231 */
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800232 bool addResourceAllowMangled(const ResourceNameRef& name,
233 const ConfigDescription& config,
234 const StringPiece& product,
235 std::unique_ptr<Value> value,
Adam Lesinski9e10ac72015-10-16 14:37:48 -0700236 IDiagnostics* diag);
237
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800238 bool addResourceAllowMangled(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700239 const ResourceId& id,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800240 const ConfigDescription& config,
241 const StringPiece& product,
242 std::unique_ptr<Value> value,
243 IDiagnostics* diag);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700244
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800245 bool setSymbolState(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700246 const ResourceId& resId,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800247 const Symbol& symbol,
248 IDiagnostics* diag);
249
250 bool setSymbolStateAllowMangled(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700251 const ResourceId& resId,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800252 const Symbol& symbol,
253 IDiagnostics* diag);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700254
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700255 struct SearchResult {
Adam Lesinski9ba47d82015-10-13 11:37:10 -0700256 ResourceTablePackage* package;
257 ResourceTableType* type;
258 ResourceEntry* entry;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700259 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800260
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700261 Maybe<SearchResult> findResource(const ResourceNameRef& name);
Adam Lesinski769de982015-04-10 19:43:55 -0700262
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800263 /**
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700264 * The string pool used by this resource table. Values that reference strings must use
265 * this pool to create their strings.
266 *
267 * NOTE: `stringPool` must come before `packages` so that it is destroyed after.
268 * When `string pool` references are destroyed (as they will be when `packages`
269 * is destroyed), they decrement a refCount, which would cause invalid
270 * memory access if the pool was already destroyed.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800271 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700272 StringPool stringPool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800273
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700274 /**
275 * The list of packages in this table, sorted alphabetically by package name.
276 */
277 std::vector<std::unique_ptr<ResourceTablePackage>> packages;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700279 /**
280 * Returns the package struct with the given name, or nullptr if such a package does not
281 * exist. The empty string is a valid package and typically is used to represent the
282 * 'current' package before it is known to the ResourceTable.
283 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700284 ResourceTablePackage* findPackage(const StringPiece& name);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700285
286 ResourceTablePackage* findPackageById(uint8_t id);
287
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700288 ResourceTablePackage* createPackage(const StringPiece& name, Maybe<uint8_t> id = {});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289
290private:
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700291 ResourceTablePackage* findOrCreatePackage(const StringPiece& name);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800292
Adam Lesinskifb48d292015-11-07 15:52:13 -0800293 bool addResourceImpl(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700294 const ResourceId& resId,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800295 const ConfigDescription& config,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800296 const StringPiece& product,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800297 std::unique_ptr<Value> value,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700298 const char* validChars,
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700299 const CollisionResolverFunc& conflictResolver,
Adam Lesinskifb48d292015-11-07 15:52:13 -0800300 IDiagnostics* diag);
301
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700302 bool addFileReferenceImpl(const ResourceNameRef& name,
303 const ConfigDescription& config,
304 const Source& source,
305 const StringPiece& path,
306 io::IFile* file,
307 const char* validChars,
308 IDiagnostics* diag);
309
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800310 bool setSymbolStateImpl(const ResourceNameRef& name,
Chih-Hung Hsieh9b8528f2016-08-10 14:15:30 -0700311 const ResourceId& resId,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800312 const Symbol& symbol,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700313 const char* validChars,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800314 IDiagnostics* diag);
315
316 DISALLOW_COPY_AND_ASSIGN(ResourceTable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800317};
318
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800319} // namespace aapt
320
321#endif // AAPT_RESOURCE_TABLE_H