blob: f49ce8147f713ad47d1b05cd1e567a6306eef6a3 [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
Adam Lesinski458b8772016-04-25 14:20:21 -070020#include <functional>
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080021#include <map>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include <memory>
23#include <string>
24#include <tuple>
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080025#include <unordered_map>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026#include <vector>
27
Jeremy Meyer56f36e82022-05-20 20:35:42 +000028#include "Resource.h"
29#include "ResourceValues.h"
30#include "android-base/macros.h"
31#include "androidfw/ConfigDescription.h"
32#include "androidfw/IDiagnostics.h"
33#include "androidfw/Source.h"
34#include "androidfw/StringPiece.h"
35#include "androidfw/StringPool.h"
36#include "io/File.h"
37
Winson62ac8b52019-12-04 08:36:48 -080038using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
39
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040namespace aapt {
41
Adam Lesinski71be7052017-12-12 16:48:07 -080042// The Public status of a resource.
43struct Visibility {
44 enum class Level {
45 kUndefined,
46 kPrivate,
47 kPublic,
48 };
49
50 Level level = Level::kUndefined;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000051 android::Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -080052 std::string comment;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070053
54 // Indicates that the resource id may change across builds and that the public R.java identifier
55 // for this resource should not be final. This is set to `true` for resources in `staging-group`
56 // tags.
57 bool staged_api = false;
Adam Lesinski9e10ac72015-10-16 14:37:48 -070058};
59
Adam Lesinski71be7052017-12-12 16:48:07 -080060// Represents <add-resource> in an overlay.
61struct AllowNew {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000062 android::Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -080063 std::string comment;
64};
Adam Lesinski4488f1c2017-05-26 17:33:38 -070065
Ryan Mitchell2fedba92021-04-23 07:47:38 -070066// Represents the staged resource id of a finalized resource.
67struct StagedId {
68 ResourceId id;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000069 android::Source source;
Ryan Mitchell2fedba92021-04-23 07:47:38 -070070};
71
Adam Lesinski71be7052017-12-12 16:48:07 -080072struct Overlayable {
Ryan Mitchell54237ff2018-12-13 15:44:29 -080073 Overlayable() = default;
74 Overlayable(const android::StringPiece& name, const android::StringPiece& actor)
75 : name(name.to_string()), actor(actor.to_string()) {}
76 Overlayable(const android::StringPiece& name, const android::StringPiece& actor,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000077 const android::Source& source)
78 : name(name.to_string()), actor(actor.to_string()), source(source) {
79 }
Ryan Mitchell54237ff2018-12-13 15:44:29 -080080
81 static const char* kActorScheme;
82 std::string name;
83 std::string actor;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000084 android::Source source;
Ryan Mitchell54237ff2018-12-13 15:44:29 -080085};
86
87// Represents a declaration that a resource is overlayable at runtime.
88struct OverlayableItem {
89 explicit OverlayableItem(const std::shared_ptr<Overlayable>& overlayable)
90 : overlayable(overlayable) {}
Ryan Mitchell54237ff2018-12-13 15:44:29 -080091 std::shared_ptr<Overlayable> overlayable;
Winson62ac8b52019-12-04 08:36:48 -080092 PolicyFlags policies = PolicyFlags::NONE;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 std::string comment;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000094 android::Source source;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095};
96
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080097class ResourceConfigValue {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 public:
Adam Lesinski71be7052017-12-12 16:48:07 -080099 // The configuration for which this value is defined.
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200100 const android::ConfigDescription config;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800101
Adam Lesinski71be7052017-12-12 16:48:07 -0800102 // The product for which this value is defined.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 const std::string product;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800104
Adam Lesinski71be7052017-12-12 16:48:07 -0800105 // The actual Value.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 std::unique_ptr<Value> value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800107
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200108 ResourceConfigValue(const android::ConfigDescription& config, const android::StringPiece& product)
Adam Lesinskid5083f62017-01-16 15:07:21 -0800109 : config(config), product(product.to_string()) {}
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800110
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 private:
112 DISALLOW_COPY_AND_ASSIGN(ResourceConfigValue);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113};
114
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800115// Represents a resource entry, which may have varying values for each defined configuration.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800116class ResourceEntry {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117 public:
Adam Lesinski71be7052017-12-12 16:48:07 -0800118 // The name of the resource. Immutable, as this determines the order of this resource
119 // when doing lookups.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 const std::string name;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121
Adam Lesinski71be7052017-12-12 16:48:07 -0800122 // The entry ID for this resource (the EEEE in 0xPPTTEEEE).
Ryan Mitchell4382e442021-07-14 12:53:01 -0700123 std::optional<ResourceId> id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800124
Adam Lesinski71be7052017-12-12 16:48:07 -0800125 // Whether this resource is public (and must maintain the same entry ID across builds).
126 Visibility visibility;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800127
Ryan Mitchell4382e442021-07-14 12:53:01 -0700128 std::optional<AllowNew> allow_new;
Adam Lesinski71be7052017-12-12 16:48:07 -0800129
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700130 // The declarations of this resource as overlayable for RROs
Ryan Mitchell4382e442021-07-14 12:53:01 -0700131 std::optional<OverlayableItem> overlayable_item;
Adam Lesinski71be7052017-12-12 16:48:07 -0800132
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700133 // The staged resource id for a finalized resource.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700134 std::optional<StagedId> staged_id;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700135
Adam Lesinski71be7052017-12-12 16:48:07 -0800136 // The resource's values for each configuration.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 std::vector<std::unique_ptr<ResourceConfigValue>> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138
Adam Lesinskid5083f62017-01-16 15:07:21 -0800139 explicit ResourceEntry(const android::StringPiece& name) : name(name.to_string()) {}
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800140
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200141 ResourceConfigValue* FindValue(const android::ConfigDescription& config,
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700142 android::StringPiece product = {});
143 const ResourceConfigValue* FindValue(const android::ConfigDescription& config,
144 android::StringPiece product = {}) const;
Adam Lesinski34a16872018-02-23 16:18:10 -0800145
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200146 ResourceConfigValue* FindOrCreateValue(const android::ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800147 const android::StringPiece& product);
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200148 std::vector<ResourceConfigValue*> FindAllValues(const android::ConfigDescription& config);
Adam Lesinski34a16872018-02-23 16:18:10 -0800149
150 template <typename Func>
151 std::vector<ResourceConfigValue*> FindValuesIf(Func f) {
152 std::vector<ResourceConfigValue*> results;
153 for (auto& config_value : values) {
154 if (f(config_value.get())) {
155 results.push_back(config_value.get());
156 }
157 }
158 return results;
159 }
160
161 bool HasDefaultValue() const;
Adam Lesinski458b8772016-04-25 14:20:21 -0700162
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 private:
164 DISALLOW_COPY_AND_ASSIGN(ResourceEntry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800165};
166
Adam Lesinski71be7052017-12-12 16:48:07 -0800167// Represents a resource type (eg. string, drawable, layout, etc.) containing resource entries.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800168class ResourceTableType {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 public:
Adam Lesinski71be7052017-12-12 16:48:07 -0800170 // The logical type of resource (string, drawable, layout, etc.).
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000171 const ResourceNamedType named_type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800172
Adam Lesinski71be7052017-12-12 16:48:07 -0800173 // Whether this type is public (and must maintain the same type ID across builds).
174 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800175
Adam Lesinski71be7052017-12-12 16:48:07 -0800176 // List of resources for this type.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 std::vector<std::unique_ptr<ResourceEntry>> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800178
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000179 explicit ResourceTableType(const ResourceNamedTypeRef& type)
180 : named_type(type.ToResourceNamedType()) {
181 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700182
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700183 ResourceEntry* CreateEntry(const android::StringPiece& name);
184 ResourceEntry* FindEntry(const android::StringPiece& name) const;
185 ResourceEntry* FindOrCreateEntry(const android::StringPiece& name);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800186
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 private:
188 DISALLOW_COPY_AND_ASSIGN(ResourceTableType);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700189};
190
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800191class ResourceTablePackage {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 public:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 std::string name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700194
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 std::vector<std::unique_ptr<ResourceTableType>> types;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700196
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700197 explicit ResourceTablePackage(const android::StringPiece& name) : name(name.to_string()) {
198 }
199
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 ResourceTablePackage() = default;
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000201 ResourceTableType* FindTypeWithDefaultName(const ResourceType type) const;
202 ResourceTableType* FindType(const ResourceNamedTypeRef& type) const;
203 ResourceTableType* FindOrCreateType(const ResourceNamedTypeRef& type);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800204
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 private:
206 DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207};
208
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700209struct ResourceTableEntryView {
210 std::string name;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700211 std::optional<uint16_t> id;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700212 Visibility visibility;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700213 std::optional<AllowNew> allow_new;
214 std::optional<OverlayableItem> overlayable_item;
215 std::optional<StagedId> staged_id;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700216 std::vector<const ResourceConfigValue*> values;
217
218 const ResourceConfigValue* FindValue(const android::ConfigDescription& config,
219 android::StringPiece product = {}) const;
220};
221
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700222struct ResourceTableTypeView {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000223 ResourceNamedType named_type;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700224 std::optional<uint8_t> id;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700225 Visibility::Level visibility_level = Visibility::Level::kUndefined;
226
227 // Entries sorted in ascending entry id order. If ids have not been assigned, the entries are
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700228 // sorted lexicographically.
229 std::vector<ResourceTableEntryView> entries;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700230};
231
232struct ResourceTablePackageView {
233 std::string name;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700234 std::optional<uint8_t> id;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700235 // Types sorted in ascending type id order. If ids have not been assigned, the types are sorted by
236 // their declaration order in the ResourceType enum.
237 std::vector<ResourceTableTypeView> types;
238};
239
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700240struct ResourceTableViewOptions {
241 bool create_alias_entries = false;
242};
243
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700244struct ResourceTableView {
245 // Packages sorted in ascending package id order. If ids have not been assigned, the packages are
246 // sorted lexicographically.
247 std::vector<ResourceTablePackageView> packages;
248};
249
250enum class OnIdConflict {
251 // If the resource entry already exists but has a different resource id, the resource value will
252 // not be added to the table.
253 ERROR,
254
255 // If the resource entry already exists but has a different resource id, create a new resource
256 // with this resource name and id combination.
257 CREATE_ENTRY,
258};
259
260struct NewResource {
261 ResourceName name;
262 std::unique_ptr<Value> value;
263 android::ConfigDescription config;
264 std::string product;
265 std::optional<std::pair<ResourceId, OnIdConflict>> id;
266 std::optional<Visibility> visibility;
267 std::optional<OverlayableItem> overlayable;
268 std::optional<AllowNew> allow_new;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700269 std::optional<StagedId> staged_id;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700270 bool allow_mangled = false;
271};
272
273struct NewResourceBuilder {
274 explicit NewResourceBuilder(const ResourceNameRef& name);
275 explicit NewResourceBuilder(const std::string& name);
276 NewResourceBuilder& SetValue(std::unique_ptr<Value> value, android::ConfigDescription config = {},
277 std::string product = {});
278 NewResourceBuilder& SetId(ResourceId id, OnIdConflict on_conflict = OnIdConflict::ERROR);
279 NewResourceBuilder& SetVisibility(Visibility id);
280 NewResourceBuilder& SetOverlayable(OverlayableItem overlayable);
281 NewResourceBuilder& SetAllowNew(AllowNew allow_new);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700282 NewResourceBuilder& SetStagedId(StagedId id);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700283 NewResourceBuilder& SetAllowMangled(bool allow_mangled);
284 NewResource Build();
285
286 private:
287 NewResource res_;
288};
289
Adam Lesinski71be7052017-12-12 16:48:07 -0800290// The container and index for all resources defined for an app.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800291class ResourceTable {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 public:
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700293 enum class Validation {
294 kEnabled,
295 kDisabled,
296 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700298 enum class CollisionResult { kKeepBoth, kKeepOriginal, kConflict, kTakeNew };
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700299
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700300 ResourceTable() = default;
301 explicit ResourceTable(Validation validation);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700302
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000303 bool AddResource(NewResource&& res, android::IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700305 // Retrieves a sorted a view of the packages, types, and entries sorted in ascending resource id
306 // order.
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700307 ResourceTableView GetPartitionedView(const ResourceTableViewOptions& options = {}) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800308
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700309 struct SearchResult {
310 ResourceTablePackage* package;
311 ResourceTableType* type;
312 ResourceEntry* entry;
313 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700314
Ryan Mitchell4382e442021-07-14 12:53:01 -0700315 std::optional<SearchResult> FindResource(const ResourceNameRef& name) const;
316 std::optional<SearchResult> FindResource(const ResourceNameRef& name, ResourceId id) const;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700317 bool RemoveResource(const ResourceNameRef& name, ResourceId id) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700318
Adam Lesinski71be7052017-12-12 16:48:07 -0800319 // Returns the package struct with the given name, or nullptr if such a package does not
320 // exist. The empty string is a valid package and typically is used to represent the
321 // 'current' package before it is known to the ResourceTable.
322 ResourceTablePackage* FindPackage(const android::StringPiece& name) const;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700323 ResourceTablePackage* FindOrCreatePackage(const android::StringPiece& name);
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000324
Shane Farmer0a5b2012017-06-22 12:24:12 -0700325 std::unique_ptr<ResourceTable> Clone() const;
326
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700327 // When a collision of resources occurs, this method decides which value to keep.
328 static CollisionResult ResolveValueCollision(Value* existing, Value* incoming);
329
Adam Lesinski71be7052017-12-12 16:48:07 -0800330 // The string pool used by this resource table. Values that reference strings must use
331 // this pool to create their strings.
332 // NOTE: `string_pool` must come before `packages` so that it is destroyed after.
333 // When `string_pool` references are destroyed (as they will be when `packages` is destroyed),
334 // they decrement a refCount, which would cause invalid memory access if the pool was already
335 // destroyed.
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000336 android::StringPool string_pool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800337
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000338 // The list of packages in this table, sorted alphabetically by package name and increasing
339 // package ID (missing ID being the lowest).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700340 std::vector<std::unique_ptr<ResourceTablePackage>> packages;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800341
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800342 // Set of dynamic packages that this table may reference. Their package names get encoded
343 // into the resources.arsc along with their compile-time assigned IDs.
344 std::map<size_t, std::string> included_packages_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700345
346 private:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700347 DISALLOW_COPY_AND_ASSIGN(ResourceTable);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700348
349 Validation validation_ = Validation::kEnabled;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800350};
351
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800353
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700354#endif // AAPT_RESOURCE_TABLE_H