blob: 5a43a2d86cfe5bcef586d137c80e72334551b0d3 [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 Lesinskid5083f62017-01-16 15:07:21 -080028#include "android-base/macros.h"
29#include "androidfw/StringPiece.h"
30
Adam Lesinski458b8772016-04-25 14:20:21 -070031#include <functional>
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080032#include <map>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033#include <memory>
34#include <string>
35#include <tuple>
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080036#include <unordered_map>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037#include <vector>
38
39namespace aapt {
40
Adam Lesinski71be7052017-12-12 16:48:07 -080041// The Public status of a resource.
42struct Visibility {
43 enum class Level {
44 kUndefined,
45 kPrivate,
46 kPublic,
47 };
48
49 Level level = Level::kUndefined;
50 Source source;
51 std::string comment;
Adam Lesinski9e10ac72015-10-16 14:37:48 -070052};
53
Adam Lesinski71be7052017-12-12 16:48:07 -080054// Represents <add-resource> in an overlay.
55struct AllowNew {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -080057 std::string comment;
58};
Adam Lesinski4488f1c2017-05-26 17:33:38 -070059
Adam Lesinski71be7052017-12-12 16:48:07 -080060// The policy dictating whether an entry is overlayable at runtime by RROs.
61struct Overlayable {
62 Source source;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 std::string comment;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064};
65
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080066class ResourceConfigValue {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 public:
Adam Lesinski71be7052017-12-12 16:48:07 -080068 // The configuration for which this value is defined.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 const ConfigDescription config;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080070
Adam Lesinski71be7052017-12-12 16:48:07 -080071 // The product for which this value is defined.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 const std::string product;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080073
Adam Lesinski71be7052017-12-12 16:48:07 -080074 // The actual Value.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 std::unique_ptr<Value> value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080076
Adam Lesinskid5083f62017-01-16 15:07:21 -080077 ResourceConfigValue(const ConfigDescription& config, const android::StringPiece& product)
78 : config(config), product(product.to_string()) {}
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 private:
81 DISALLOW_COPY_AND_ASSIGN(ResourceConfigValue);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082};
83
Adam Lesinski73bff1e2017-12-08 16:06:10 -080084// Represents a resource entry, which may have varying values for each defined configuration.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080085class ResourceEntry {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 public:
Adam Lesinski71be7052017-12-12 16:48:07 -080087 // The name of the resource. Immutable, as this determines the order of this resource
88 // when doing lookups.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 const std::string name;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090
Adam Lesinski71be7052017-12-12 16:48:07 -080091 // The entry ID for this resource (the EEEE in 0xPPTTEEEE).
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 Maybe<uint16_t> id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
Adam Lesinski71be7052017-12-12 16:48:07 -080094 // Whether this resource is public (and must maintain the same entry ID across builds).
95 Visibility visibility;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
Adam Lesinski71be7052017-12-12 16:48:07 -080097 Maybe<AllowNew> allow_new;
98
99 Maybe<Overlayable> overlayable;
100
101 // The resource's values for each configuration.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 std::vector<std::unique_ptr<ResourceConfigValue>> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800103
Adam Lesinskid5083f62017-01-16 15:07:21 -0800104 explicit ResourceEntry(const android::StringPiece& name) : name(name.to_string()) {}
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800105
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 ResourceConfigValue* FindValue(const ConfigDescription& config);
Adam Lesinski34a16872018-02-23 16:18:10 -0800107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 ResourceConfigValue* FindValue(const ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800109 const android::StringPiece& product);
Adam Lesinski34a16872018-02-23 16:18:10 -0800110
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700111 ResourceConfigValue* FindOrCreateValue(const ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800112 const android::StringPiece& product);
Adam Lesinskib1afa072017-03-29 13:52:38 -0700113 std::vector<ResourceConfigValue*> FindAllValues(const ConfigDescription& config);
Adam Lesinski34a16872018-02-23 16:18:10 -0800114
115 template <typename Func>
116 std::vector<ResourceConfigValue*> FindValuesIf(Func f) {
117 std::vector<ResourceConfigValue*> results;
118 for (auto& config_value : values) {
119 if (f(config_value.get())) {
120 results.push_back(config_value.get());
121 }
122 }
123 return results;
124 }
125
126 bool HasDefaultValue() const;
Adam Lesinski458b8772016-04-25 14:20:21 -0700127
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700128 private:
129 DISALLOW_COPY_AND_ASSIGN(ResourceEntry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130};
131
Adam Lesinski71be7052017-12-12 16:48:07 -0800132// Represents a resource type (eg. string, drawable, layout, etc.) containing resource entries.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800133class ResourceTableType {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 public:
Adam Lesinski71be7052017-12-12 16:48:07 -0800135 // The logical type of resource (string, drawable, layout, etc.).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 const ResourceType type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800137
Adam Lesinski71be7052017-12-12 16:48:07 -0800138 // The type ID for this resource (the TT in 0xPPTTEEEE).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 Maybe<uint8_t> id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140
Adam Lesinski71be7052017-12-12 16:48:07 -0800141 // Whether this type is public (and must maintain the same type ID across builds).
142 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143
Adam Lesinski71be7052017-12-12 16:48:07 -0800144 // List of resources for this type.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 std::vector<std::unique_ptr<ResourceEntry>> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 explicit ResourceTableType(const ResourceType type) : type(type) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700148
Ryan Mitchell8d4ee972018-08-27 11:24:04 -0700149 ResourceEntry* FindEntry(const android::StringPiece& name,
150 Maybe<uint16_t> id = Maybe<uint16_t>());
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700151 ResourceEntry* FindOrCreateEntry(const android::StringPiece& name,
Ryan Mitchell8d4ee972018-08-27 11:24:04 -0700152 Maybe<uint16_t> id = Maybe<uint16_t>());
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800153
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 private:
155 DISALLOW_COPY_AND_ASSIGN(ResourceTableType);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156};
157
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800158class ResourceTablePackage {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 public:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 std::string name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700161
Adam Lesinski71be7052017-12-12 16:48:07 -0800162 // The package ID (the PP in 0xPPTTEEEE).
163 Maybe<uint8_t> id;
164
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 std::vector<std::unique_ptr<ResourceTableType>> types;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 ResourceTablePackage() = default;
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700168 ResourceTableType* FindType(ResourceType type, Maybe<uint8_t> id = Maybe<uint8_t>());
169 ResourceTableType* FindOrCreateType(const ResourceType type,
170 Maybe<uint8_t> id = Maybe<uint8_t>());
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800171
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 private:
173 DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174};
175
Adam Lesinski71be7052017-12-12 16:48:07 -0800176// The container and index for all resources defined for an app.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800177class ResourceTable {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 public:
179 ResourceTable() = default;
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700180 explicit ResourceTable(bool validate_resources) : validate_resources_(validate_resources) {}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800181
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700182 enum class CollisionResult { kKeepBoth, kKeepOriginal, kConflict, kTakeNew };
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 using CollisionResolverFunc = std::function<CollisionResult(Value*, Value*)>;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700185
Adam Lesinski71be7052017-12-12 16:48:07 -0800186 // When a collision of resources occurs, this method decides which value to keep.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700187 static CollisionResult ResolveValueCollision(Value* existing, Value* incoming);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800188
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700189 // When a collision of resources occurs, this method keeps both values
190 static CollisionResult IgnoreCollision(Value* existing, Value* incoming);
191
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700192 bool AddResource(const ResourceNameRef& name, const ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800193 const android::StringPiece& product, std::unique_ptr<Value> value,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 IDiagnostics* diag);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700195
Adam Lesinski71be7052017-12-12 16:48:07 -0800196 bool AddResourceWithId(const ResourceNameRef& name, const ResourceId& res_id,
197 const ConfigDescription& config, const android::StringPiece& product,
198 std::unique_ptr<Value> value, IDiagnostics* diag);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800199
Adam Lesinskid5083f62017-01-16 15:07:21 -0800200 bool AddFileReference(const ResourceNameRef& name, const ConfigDescription& config,
201 const Source& source, const android::StringPiece& path, IDiagnostics* diag);
Adam Lesinskifb48d292015-11-07 15:52:13 -0800202
Adam Lesinski71be7052017-12-12 16:48:07 -0800203 bool AddFileReferenceMangled(const ResourceNameRef& name, const ConfigDescription& config,
204 const Source& source, const android::StringPiece& path,
205 io::IFile* file, IDiagnostics* diag);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700206
Adam Lesinski71be7052017-12-12 16:48:07 -0800207 // Same as AddResource, but doesn't verify the validity of the name. This is used
208 // when loading resources from an existing binary resource table that may have mangled names.
209 bool AddResourceMangled(const ResourceNameRef& name, const ConfigDescription& config,
210 const android::StringPiece& product, std::unique_ptr<Value> value,
211 IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800212
Adam Lesinski71be7052017-12-12 16:48:07 -0800213 bool AddResourceWithIdMangled(const ResourceNameRef& name, const ResourceId& id,
214 const ConfigDescription& config,
215 const android::StringPiece& product, std::unique_ptr<Value> value,
216 IDiagnostics* diag);
Adam Lesinski769de982015-04-10 19:43:55 -0700217
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700218 bool GetValidateResources();
219
Adam Lesinski71be7052017-12-12 16:48:07 -0800220 bool SetVisibility(const ResourceNameRef& name, const Visibility& visibility, IDiagnostics* diag);
221 bool SetVisibilityMangled(const ResourceNameRef& name, const Visibility& visibility,
222 IDiagnostics* diag);
223 bool SetVisibilityWithId(const ResourceNameRef& name, const Visibility& visibility,
224 const ResourceId& res_id, IDiagnostics* diag);
225 bool SetVisibilityWithIdMangled(const ResourceNameRef& name, const Visibility& visibility,
226 const ResourceId& res_id, IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227
Adam Lesinski71be7052017-12-12 16:48:07 -0800228 bool SetOverlayable(const ResourceNameRef& name, const Overlayable& overlayable,
229 IDiagnostics* diag);
230 bool SetOverlayableMangled(const ResourceNameRef& name, const Overlayable& overlayable,
231 IDiagnostics* diag);
232
233 bool SetAllowNew(const ResourceNameRef& name, const AllowNew& allow_new, IDiagnostics* diag);
234 bool SetAllowNewMangled(const ResourceNameRef& name, const AllowNew& allow_new,
235 IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 struct SearchResult {
238 ResourceTablePackage* package;
239 ResourceTableType* type;
240 ResourceEntry* entry;
241 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700242
Adam Lesinski71be7052017-12-12 16:48:07 -0800243 Maybe<SearchResult> FindResource(const ResourceNameRef& name) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244
Adam Lesinski71be7052017-12-12 16:48:07 -0800245 // Returns the package struct with the given name, or nullptr if such a package does not
246 // exist. The empty string is a valid package and typically is used to represent the
247 // 'current' package before it is known to the ResourceTable.
248 ResourceTablePackage* FindPackage(const android::StringPiece& name) const;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800249
Adam Lesinski71be7052017-12-12 16:48:07 -0800250 ResourceTablePackage* FindPackageById(uint8_t id) const;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800251
252 ResourceTablePackage* CreatePackage(const android::StringPiece& name, Maybe<uint8_t> id = {});
253
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000254 // Attempts to find a package having the specified name and ID. If not found, a new package
255 // of the specified parameters is created and returned.
256 ResourceTablePackage* CreatePackageAllowingDuplicateNames(const android::StringPiece& name,
257 const Maybe<uint8_t> id);
258
Shane Farmer0a5b2012017-06-22 12:24:12 -0700259 std::unique_ptr<ResourceTable> Clone() const;
260
Adam Lesinski71be7052017-12-12 16:48:07 -0800261 // The string pool used by this resource table. Values that reference strings must use
262 // this pool to create their strings.
263 // NOTE: `string_pool` must come before `packages` so that it is destroyed after.
264 // When `string_pool` references are destroyed (as they will be when `packages` is destroyed),
265 // they decrement a refCount, which would cause invalid memory access if the pool was already
266 // destroyed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 StringPool string_pool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000269 // The list of packages in this table, sorted alphabetically by package name and increasing
270 // package ID (missing ID being the lowest).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 std::vector<std::unique_ptr<ResourceTablePackage>> packages;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800273 // Set of dynamic packages that this table may reference. Their package names get encoded
274 // into the resources.arsc along with their compile-time assigned IDs.
275 std::map<size_t, std::string> included_packages_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700276
277 private:
Adam Lesinskib1afa072017-03-29 13:52:38 -0700278 // The function type that validates a symbol name. Returns a non-empty StringPiece representing
279 // the offending character (which may be more than one byte in UTF-8). Returns an empty string
280 // if the name was valid.
281 using NameValidator = android::StringPiece(const android::StringPiece&);
282
Adam Lesinskid5083f62017-01-16 15:07:21 -0800283 ResourceTablePackage* FindOrCreatePackage(const android::StringPiece& name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284
Adam Lesinski71be7052017-12-12 16:48:07 -0800285 bool ValidateName(NameValidator validator, const ResourceNameRef& name, const Source& source,
286 IDiagnostics* diag);
287
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 bool AddResourceImpl(const ResourceNameRef& name, const ResourceId& res_id,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800289 const ConfigDescription& config, const android::StringPiece& product,
Adam Lesinskib1afa072017-03-29 13:52:38 -0700290 std::unique_ptr<Value> value, NameValidator name_validator,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800291 const CollisionResolverFunc& conflict_resolver, IDiagnostics* diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292
Adam Lesinskid5083f62017-01-16 15:07:21 -0800293 bool AddFileReferenceImpl(const ResourceNameRef& name, const ConfigDescription& config,
294 const Source& source, const android::StringPiece& path, io::IFile* file,
Adam Lesinskib1afa072017-03-29 13:52:38 -0700295 NameValidator name_validator, IDiagnostics* diag);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800296
Adam Lesinski71be7052017-12-12 16:48:07 -0800297 bool SetVisibilityImpl(const ResourceNameRef& name, const Visibility& visibility,
298 const ResourceId& res_id, NameValidator name_validator,
299 IDiagnostics* diag);
300
301 bool SetAllowNewImpl(const ResourceNameRef& name, const AllowNew& allow_new,
302 NameValidator name_validator, IDiagnostics* diag);
303
304 bool SetOverlayableImpl(const ResourceNameRef& name, const Overlayable& overlayable,
305 NameValidator name_validator, IDiagnostics* diag);
306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 bool SetSymbolStateImpl(const ResourceNameRef& name, const ResourceId& res_id,
Adam Lesinski71be7052017-12-12 16:48:07 -0800308 const Visibility& symbol, NameValidator name_validator,
309 IDiagnostics* diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700311 // Controls whether the table validates resource names and prevents duplicate resource names
312 bool validate_resources_ = true;
313
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700314 DISALLOW_COPY_AND_ASSIGN(ResourceTable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800315};
316
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700319#endif // AAPT_RESOURCE_TABLE_H