blob: eaa2d0b8af7d9d8722c94e69ea796614bedd0b9e [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
84/**
85 * Represents a resource entry, which may have
86 * varying values for each defined configuration.
87 */
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080088class ResourceEntry {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 public:
Adam Lesinski71be7052017-12-12 16:48:07 -080090 // The name of the resource. Immutable, as this determines the order of this resource
91 // when doing lookups.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 const std::string name;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
Adam Lesinski71be7052017-12-12 16:48:07 -080094 // The entry ID for this resource (the EEEE in 0xPPTTEEEE).
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 Maybe<uint16_t> id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
Adam Lesinski71be7052017-12-12 16:48:07 -080097 // Whether this resource is public (and must maintain the same entry ID across builds).
98 Visibility visibility;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099
Adam Lesinski71be7052017-12-12 16:48:07 -0800100 Maybe<AllowNew> allow_new;
101
102 Maybe<Overlayable> overlayable;
103
104 // The resource's values for each configuration.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 std::vector<std::unique_ptr<ResourceConfigValue>> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106
Adam Lesinskid5083f62017-01-16 15:07:21 -0800107 explicit ResourceEntry(const android::StringPiece& name) : name(name.to_string()) {}
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 ResourceConfigValue* FindValue(const ConfigDescription& config);
110 ResourceConfigValue* FindValue(const ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800111 const android::StringPiece& product);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 ResourceConfigValue* FindOrCreateValue(const ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800113 const android::StringPiece& product);
Adam Lesinskib1afa072017-03-29 13:52:38 -0700114 std::vector<ResourceConfigValue*> FindAllValues(const ConfigDescription& config);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700115 std::vector<ResourceConfigValue*> FindValuesIf(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 const std::function<bool(ResourceConfigValue*)>& f);
Adam Lesinski458b8772016-04-25 14:20:21 -0700117
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 private:
119 DISALLOW_COPY_AND_ASSIGN(ResourceEntry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120};
121
Adam Lesinski71be7052017-12-12 16:48:07 -0800122// Represents a resource type (eg. string, drawable, layout, etc.) containing resource entries.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800123class ResourceTableType {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 public:
Adam Lesinski71be7052017-12-12 16:48:07 -0800125 // The logical type of resource (string, drawable, layout, etc.).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 const ResourceType type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127
Adam Lesinski71be7052017-12-12 16:48:07 -0800128 // The type ID for this resource (the TT in 0xPPTTEEEE).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 Maybe<uint8_t> id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800130
Adam Lesinski71be7052017-12-12 16:48:07 -0800131 // Whether this type is public (and must maintain the same type ID across builds).
132 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133
Adam Lesinski71be7052017-12-12 16:48:07 -0800134 // List of resources for this type.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 std::vector<std::unique_ptr<ResourceEntry>> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 explicit ResourceTableType(const ResourceType type) : type(type) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700138
Adam Lesinskid5083f62017-01-16 15:07:21 -0800139 ResourceEntry* FindEntry(const android::StringPiece& name);
140 ResourceEntry* FindOrCreateEntry(const android::StringPiece& name);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800141
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 private:
143 DISALLOW_COPY_AND_ASSIGN(ResourceTableType);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144};
145
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800146class ResourceTablePackage {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 public:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 std::string name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700149
Adam Lesinski71be7052017-12-12 16:48:07 -0800150 // The package ID (the PP in 0xPPTTEEEE).
151 Maybe<uint8_t> id;
152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 std::vector<std::unique_ptr<ResourceTableType>> types;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 ResourceTablePackage() = default;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 ResourceTableType* FindType(ResourceType type);
157 ResourceTableType* FindOrCreateType(const ResourceType type);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800158
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 private:
160 DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161};
162
Adam Lesinski71be7052017-12-12 16:48:07 -0800163// The container and index for all resources defined for an app.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800164class ResourceTable {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 public:
166 ResourceTable() = default;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800167
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 enum class CollisionResult { kKeepOriginal, kConflict, kTakeNew };
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700169
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 using CollisionResolverFunc = std::function<CollisionResult(Value*, Value*)>;
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700171
Adam Lesinski71be7052017-12-12 16:48:07 -0800172 // When a collision of resources occurs, this method decides which value to keep.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700173 static CollisionResult ResolveValueCollision(Value* existing, Value* incoming);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800174
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 bool AddResource(const ResourceNameRef& name, const ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800176 const android::StringPiece& product, std::unique_ptr<Value> value,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 IDiagnostics* diag);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700178
Adam Lesinski71be7052017-12-12 16:48:07 -0800179 bool AddResourceWithId(const ResourceNameRef& name, const ResourceId& res_id,
180 const ConfigDescription& config, const android::StringPiece& product,
181 std::unique_ptr<Value> value, IDiagnostics* diag);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800182
Adam Lesinskid5083f62017-01-16 15:07:21 -0800183 bool AddFileReference(const ResourceNameRef& name, const ConfigDescription& config,
184 const Source& source, const android::StringPiece& path, IDiagnostics* diag);
Adam Lesinskifb48d292015-11-07 15:52:13 -0800185
Adam Lesinski71be7052017-12-12 16:48:07 -0800186 bool AddFileReferenceMangled(const ResourceNameRef& name, const ConfigDescription& config,
187 const Source& source, const android::StringPiece& path,
188 io::IFile* file, IDiagnostics* diag);
Adam Lesinskie78fd612015-10-22 12:48:43 -0700189
Adam Lesinski71be7052017-12-12 16:48:07 -0800190 // Same as AddResource, but doesn't verify the validity of the name. This is used
191 // when loading resources from an existing binary resource table that may have mangled names.
192 bool AddResourceMangled(const ResourceNameRef& name, const ConfigDescription& config,
193 const android::StringPiece& product, std::unique_ptr<Value> value,
194 IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800195
Adam Lesinski71be7052017-12-12 16:48:07 -0800196 bool AddResourceWithIdMangled(const ResourceNameRef& name, const ResourceId& id,
197 const ConfigDescription& config,
198 const android::StringPiece& product, std::unique_ptr<Value> value,
199 IDiagnostics* diag);
Adam Lesinski769de982015-04-10 19:43:55 -0700200
Adam Lesinski71be7052017-12-12 16:48:07 -0800201 bool SetVisibility(const ResourceNameRef& name, const Visibility& visibility, IDiagnostics* diag);
202 bool SetVisibilityMangled(const ResourceNameRef& name, const Visibility& visibility,
203 IDiagnostics* diag);
204 bool SetVisibilityWithId(const ResourceNameRef& name, const Visibility& visibility,
205 const ResourceId& res_id, IDiagnostics* diag);
206 bool SetVisibilityWithIdMangled(const ResourceNameRef& name, const Visibility& visibility,
207 const ResourceId& res_id, IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800208
Adam Lesinski71be7052017-12-12 16:48:07 -0800209 bool SetOverlayable(const ResourceNameRef& name, const Overlayable& overlayable,
210 IDiagnostics* diag);
211 bool SetOverlayableMangled(const ResourceNameRef& name, const Overlayable& overlayable,
212 IDiagnostics* diag);
213
214 bool SetAllowNew(const ResourceNameRef& name, const AllowNew& allow_new, IDiagnostics* diag);
215 bool SetAllowNewMangled(const ResourceNameRef& name, const AllowNew& allow_new,
216 IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800217
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 struct SearchResult {
219 ResourceTablePackage* package;
220 ResourceTableType* type;
221 ResourceEntry* entry;
222 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700223
Adam Lesinski71be7052017-12-12 16:48:07 -0800224 Maybe<SearchResult> FindResource(const ResourceNameRef& name) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700225
Adam Lesinski71be7052017-12-12 16:48:07 -0800226 // Returns the package struct with the given name, or nullptr if such a package does not
227 // exist. The empty string is a valid package and typically is used to represent the
228 // 'current' package before it is known to the ResourceTable.
229 ResourceTablePackage* FindPackage(const android::StringPiece& name) const;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800230
Adam Lesinski71be7052017-12-12 16:48:07 -0800231 ResourceTablePackage* FindPackageById(uint8_t id) const;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800232
233 ResourceTablePackage* CreatePackage(const android::StringPiece& name, Maybe<uint8_t> id = {});
234
Shane Farmer0a5b2012017-06-22 12:24:12 -0700235 std::unique_ptr<ResourceTable> Clone() const;
236
Adam Lesinski71be7052017-12-12 16:48:07 -0800237 // The string pool used by this resource table. Values that reference strings must use
238 // this pool to create their strings.
239 // NOTE: `string_pool` must come before `packages` so that it is destroyed after.
240 // When `string_pool` references are destroyed (as they will be when `packages` is destroyed),
241 // they decrement a refCount, which would cause invalid memory access if the pool was already
242 // destroyed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 StringPool string_pool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800244
Adam Lesinski71be7052017-12-12 16:48:07 -0800245 // The list of packages in this table, sorted alphabetically by package name.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 std::vector<std::unique_ptr<ResourceTablePackage>> packages;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800247
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800248 // Set of dynamic packages that this table may reference. Their package names get encoded
249 // into the resources.arsc along with their compile-time assigned IDs.
250 std::map<size_t, std::string> included_packages_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700251
252 private:
Adam Lesinskib1afa072017-03-29 13:52:38 -0700253 // The function type that validates a symbol name. Returns a non-empty StringPiece representing
254 // the offending character (which may be more than one byte in UTF-8). Returns an empty string
255 // if the name was valid.
256 using NameValidator = android::StringPiece(const android::StringPiece&);
257
Adam Lesinskid5083f62017-01-16 15:07:21 -0800258 ResourceTablePackage* FindOrCreatePackage(const android::StringPiece& name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259
Adam Lesinski71be7052017-12-12 16:48:07 -0800260 bool ValidateName(NameValidator validator, const ResourceNameRef& name, const Source& source,
261 IDiagnostics* diag);
262
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700263 bool AddResourceImpl(const ResourceNameRef& name, const ResourceId& res_id,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800264 const ConfigDescription& config, const android::StringPiece& product,
Adam Lesinskib1afa072017-03-29 13:52:38 -0700265 std::unique_ptr<Value> value, NameValidator name_validator,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800266 const CollisionResolverFunc& conflict_resolver, IDiagnostics* diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267
Adam Lesinskid5083f62017-01-16 15:07:21 -0800268 bool AddFileReferenceImpl(const ResourceNameRef& name, const ConfigDescription& config,
269 const Source& source, const android::StringPiece& path, io::IFile* file,
Adam Lesinskib1afa072017-03-29 13:52:38 -0700270 NameValidator name_validator, IDiagnostics* diag);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800271
Adam Lesinski71be7052017-12-12 16:48:07 -0800272 bool SetVisibilityImpl(const ResourceNameRef& name, const Visibility& visibility,
273 const ResourceId& res_id, NameValidator name_validator,
274 IDiagnostics* diag);
275
276 bool SetAllowNewImpl(const ResourceNameRef& name, const AllowNew& allow_new,
277 NameValidator name_validator, IDiagnostics* diag);
278
279 bool SetOverlayableImpl(const ResourceNameRef& name, const Overlayable& overlayable,
280 NameValidator name_validator, IDiagnostics* diag);
281
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700282 bool SetSymbolStateImpl(const ResourceNameRef& name, const ResourceId& res_id,
Adam Lesinski71be7052017-12-12 16:48:07 -0800283 const Visibility& symbol, NameValidator name_validator,
284 IDiagnostics* diag);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285
286 DISALLOW_COPY_AND_ASSIGN(ResourceTable);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800287};
288
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800290
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291#endif // AAPT_RESOURCE_TABLE_H