blob: a0c3217df6104a80b65fdee669bf30ad92407813 [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 {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 kUndefined,
41 kPrivate,
42 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 {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 SymbolState state = SymbolState::kUndefined;
50 Source source;
51 std::string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052};
53
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080054class ResourceConfigValue {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 public:
56 /**
57 * The configuration for which this value is defined.
58 */
59 const ConfigDescription config;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080060
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 /**
62 * The product for which this value is defined.
63 */
64 const std::string product;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080065
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 /**
67 * The actual Value.
68 */
69 std::unique_ptr<Value> value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 ResourceConfigValue(const ConfigDescription& config,
72 const StringPiece& product)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 : config(config), product(product.ToString()) {}
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080074
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 private:
76 DISALLOW_COPY_AND_ASSIGN(ResourceConfigValue);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077};
78
79/**
80 * Represents a resource entry, which may have
81 * varying values for each defined configuration.
82 */
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080083class ResourceEntry {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 public:
85 /**
86 * The name of the resource. Immutable, as
87 * this determines the order of this resource
88 * when doing lookups.
89 */
90 const std::string name;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 /**
93 * The entry ID for this resource.
94 */
95 Maybe<uint16_t> id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 /**
98 * Whether this resource is public (and must maintain the same entry ID across
99 * builds).
100 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 Symbol symbol_status;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 /**
104 * The resource's values for each configuration.
105 */
106 std::vector<std::unique_ptr<ResourceConfigValue>> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 explicit ResourceEntry(const StringPiece& name) : name(name.ToString()) {}
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 ResourceConfigValue* FindValue(const ConfigDescription& config);
111 ResourceConfigValue* FindValue(const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 const StringPiece& product);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 ResourceConfigValue* FindOrCreateValue(const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700114 const StringPiece& product);
115 std::vector<ResourceConfigValue*> findAllValues(
116 const ConfigDescription& config);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 std::vector<ResourceConfigValue*> FindValuesIf(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 const std::function<bool(ResourceConfigValue*)>& f);
Adam Lesinski458b8772016-04-25 14:20:21 -0700119
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 private:
121 DISALLOW_COPY_AND_ASSIGN(ResourceEntry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122};
123
124/**
125 * Represents a resource type, which holds entries defined
126 * for this type.
127 */
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800128class ResourceTableType {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 public:
130 /**
131 * The logical type of resource (string, drawable, layout, etc.).
132 */
133 const ResourceType type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 /**
136 * The type ID for this resource.
137 */
138 Maybe<uint8_t> id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 /**
141 * Whether this type is public (and must maintain the same
142 * type ID across builds).
143 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 Symbol symbol_status;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800145
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 /**
147 * List of resources for this type.
148 */
149 std::vector<std::unique_ptr<ResourceEntry>> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 explicit ResourceTableType(const ResourceType type) : type(type) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700152
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 ResourceEntry* FindEntry(const StringPiece& name);
154 ResourceEntry* FindOrCreateEntry(const StringPiece& name);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800155
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 private:
157 DISALLOW_COPY_AND_ASSIGN(ResourceTableType);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158};
159
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160enum class PackageType { System, Vendor, App, Dynamic };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700161
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800162class ResourceTablePackage {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 public:
164 PackageType type = PackageType::App;
165 Maybe<uint8_t> id;
166 std::string name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 std::vector<std::unique_ptr<ResourceTableType>> types;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 ResourceTablePackage() = default;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 ResourceTableType* FindType(ResourceType type);
172 ResourceTableType* FindOrCreateType(const ResourceType type);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800173
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 private:
175 DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800176};
177
178/**
179 * The container and index for all resources defined for an app. This gets
180 * flattened into a binary resource table (resources.arsc).
181 */
182class ResourceTable {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 public:
184 ResourceTable() = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800185
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 enum class CollisionResult { kKeepOriginal, kConflict, kTakeNew };
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700187
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 using CollisionResolverFunc = std::function<CollisionResult(Value*, Value*)>;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700189
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 /**
191 * When a collision of resources occurs, this method decides which value to
192 * keep.
193 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700194 static CollisionResult ResolveValueCollision(Value* existing,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 Value* incoming);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800196
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 bool AddResource(const ResourceNameRef& name, const ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 const StringPiece& product, std::unique_ptr<Value> value,
199 IDiagnostics* diag);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700200
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201 bool AddResource(const ResourceNameRef& name, const ResourceId& res_id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 const ConfigDescription& config, const StringPiece& product,
203 std::unique_ptr<Value> value, IDiagnostics* diag);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800204
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205 bool AddFileReference(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 const ConfigDescription& config, const Source& source,
207 const StringPiece& path, IDiagnostics* diag);
Adam Lesinskifb48d292015-11-07 15:52:13 -0800208
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700209 bool AddFileReferenceAllowMangled(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 const ConfigDescription& config,
211 const Source& source,
212 const StringPiece& path, io::IFile* file,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800213 IDiagnostics* diag);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700214
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700215 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700216 * Same as AddResource, but doesn't verify the validity of the name. This is
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700217 * used
218 * when loading resources from an existing binary resource table that may have
219 * mangled
220 * names.
221 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222 bool AddResourceAllowMangled(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 const ConfigDescription& config,
224 const StringPiece& product,
225 std::unique_ptr<Value> value,
226 IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228 bool AddResourceAllowMangled(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700229 const ResourceId& id,
230 const ConfigDescription& config,
231 const StringPiece& product,
232 std::unique_ptr<Value> value,
233 IDiagnostics* diag);
Adam Lesinski769de982015-04-10 19:43:55 -0700234
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700235 bool SetSymbolState(const ResourceNameRef& name, const ResourceId& res_id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 const Symbol& symbol, IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800237
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 bool SetSymbolStateAllowMangled(const ResourceNameRef& name,
239 const ResourceId& res_id,
240 const Symbol& symbol, IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 struct SearchResult {
243 ResourceTablePackage* package;
244 ResourceTableType* type;
245 ResourceEntry* entry;
246 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700247
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 Maybe<SearchResult> FindResource(const ResourceNameRef& name);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700249
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 /**
251 * The string pool used by this resource table. Values that reference strings
252 * must use
253 * this pool to create their strings.
254 *
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700255 * NOTE: `string_pool` must come before `packages` so that it is destroyed
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 * after.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 * When `string_pool` references are destroyed (as they will be when
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 * `packages`
259 * is destroyed), they decrement a refCount, which would cause invalid
260 * memory access if the pool was already destroyed.
261 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 StringPool string_pool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800263
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 /**
265 * The list of packages in this table, sorted alphabetically by package name.
266 */
267 std::vector<std::unique_ptr<ResourceTablePackage>> packages;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 /**
270 * Returns the package struct with the given name, or nullptr if such a
271 * package does not
272 * exist. The empty string is a valid package and typically is used to
273 * represent the
274 * 'current' package before it is known to the ResourceTable.
275 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700276 ResourceTablePackage* FindPackage(const StringPiece& name);
Adam Lesinskifb48d292015-11-07 15:52:13 -0800277
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700278 ResourceTablePackage* FindPackageById(uint8_t id);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -0700279
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 ResourceTablePackage* CreatePackage(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 Maybe<uint8_t> id = {});
282
283 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 ResourceTablePackage* FindOrCreatePackage(const StringPiece& name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700286 bool AddResourceImpl(const ResourceNameRef& name, const ResourceId& res_id,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 const ConfigDescription& config,
288 const StringPiece& product, std::unique_ptr<Value> value,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700289 const char* valid_chars,
290 const CollisionResolverFunc& conflict_resolver,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 IDiagnostics* diag);
292
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 bool AddFileReferenceImpl(const ResourceNameRef& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 const ConfigDescription& config,
295 const Source& source, const StringPiece& path,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 io::IFile* file, const char* valid_chars,
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800297 IDiagnostics* diag);
298
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 bool SetSymbolStateImpl(const ResourceNameRef& name, const ResourceId& res_id,
300 const Symbol& symbol, const char* valid_chars,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 IDiagnostics* diag);
302
303 DISALLOW_COPY_AND_ASSIGN(ResourceTable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304};
305
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800307
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308#endif // AAPT_RESOURCE_TABLE_H