blob: 49392a5db830543c1f7d8ce8725c17734668ca3e [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 Lesinski1ab598f2015-08-14 14:26:04 -070020#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021#include "Resource.h"
22#include "ResourceValues.h"
23#include "Source.h"
24#include "StringPool.h"
Adam Lesinski355f2852016-02-13 20:26:45 -080025#include "io/File.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026
Adam Lesinskid5083f62017-01-16 15:07:21 -080027#include "android-base/macros.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020028#include "androidfw/ConfigDescription.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080029#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
Winson62ac8b52019-12-04 08:36:48 -080039using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
40
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041namespace aapt {
42
Adam Lesinski71be7052017-12-12 16:48:07 -080043// The Public status of a resource.
44struct Visibility {
45 enum class Level {
46 kUndefined,
47 kPrivate,
48 kPublic,
49 };
50
51 Level level = Level::kUndefined;
52 Source source;
53 std::string comment;
Adam Lesinski9e10ac72015-10-16 14:37:48 -070054};
55
Adam Lesinski71be7052017-12-12 16:48:07 -080056// Represents <add-resource> in an overlay.
57struct AllowNew {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -080059 std::string comment;
60};
Adam Lesinski4488f1c2017-05-26 17:33:38 -070061
Adam Lesinski71be7052017-12-12 16:48:07 -080062struct Overlayable {
Ryan Mitchell54237ff2018-12-13 15:44:29 -080063 Overlayable() = default;
64 Overlayable(const android::StringPiece& name, const android::StringPiece& actor)
65 : name(name.to_string()), actor(actor.to_string()) {}
66 Overlayable(const android::StringPiece& name, const android::StringPiece& actor,
67 const Source& source)
68 : name(name.to_string()), actor(actor.to_string()), source(source ){}
69
70 static const char* kActorScheme;
71 std::string name;
72 std::string actor;
73 Source source;
74};
75
76// Represents a declaration that a resource is overlayable at runtime.
77struct OverlayableItem {
78 explicit OverlayableItem(const std::shared_ptr<Overlayable>& overlayable)
79 : overlayable(overlayable) {}
Ryan Mitchell54237ff2018-12-13 15:44:29 -080080 std::shared_ptr<Overlayable> overlayable;
Winson62ac8b52019-12-04 08:36:48 -080081 PolicyFlags policies = PolicyFlags::NONE;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 std::string comment;
Ryan Mitchell54237ff2018-12-13 15:44:29 -080083 Source source;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084};
85
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080086class ResourceConfigValue {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 public:
Adam Lesinski71be7052017-12-12 16:48:07 -080088 // The configuration for which this value is defined.
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020089 const android::ConfigDescription config;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080090
Adam Lesinski71be7052017-12-12 16:48:07 -080091 // The product for which this value is defined.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 const std::string product;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080093
Adam Lesinski71be7052017-12-12 16:48:07 -080094 // The actual Value.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 std::unique_ptr<Value> value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080096
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020097 ResourceConfigValue(const android::ConfigDescription& config, const android::StringPiece& product)
Adam Lesinskid5083f62017-01-16 15:07:21 -080098 : config(config), product(product.to_string()) {}
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -080099
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 private:
101 DISALLOW_COPY_AND_ASSIGN(ResourceConfigValue);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102};
103
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800104// Represents a resource entry, which may have varying values for each defined configuration.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800105class ResourceEntry {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 public:
Adam Lesinski71be7052017-12-12 16:48:07 -0800107 // The name of the resource. Immutable, as this determines the order of this resource
108 // when doing lookups.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 const std::string name;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800110
Adam Lesinski71be7052017-12-12 16:48:07 -0800111 // The entry ID for this resource (the EEEE in 0xPPTTEEEE).
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700112 Maybe<ResourceId> id;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113
Adam Lesinski71be7052017-12-12 16:48:07 -0800114 // Whether this resource is public (and must maintain the same entry ID across builds).
115 Visibility visibility;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
Adam Lesinski71be7052017-12-12 16:48:07 -0800117 Maybe<AllowNew> allow_new;
118
Ryan Mitchelle4e989c2018-10-29 02:21:50 -0700119 // The declarations of this resource as overlayable for RROs
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800120 Maybe<OverlayableItem> overlayable_item;
Adam Lesinski71be7052017-12-12 16:48:07 -0800121
122 // The resource's values for each configuration.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700123 std::vector<std::unique_ptr<ResourceConfigValue>> values;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800124
Adam Lesinskid5083f62017-01-16 15:07:21 -0800125 explicit ResourceEntry(const android::StringPiece& name) : name(name.to_string()) {}
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800126
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200127 ResourceConfigValue* FindValue(const android::ConfigDescription& config,
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700128 android::StringPiece product = {});
129 const ResourceConfigValue* FindValue(const android::ConfigDescription& config,
130 android::StringPiece product = {}) const;
Adam Lesinski34a16872018-02-23 16:18:10 -0800131
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200132 ResourceConfigValue* FindOrCreateValue(const android::ConfigDescription& config,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800133 const android::StringPiece& product);
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200134 std::vector<ResourceConfigValue*> FindAllValues(const android::ConfigDescription& config);
Adam Lesinski34a16872018-02-23 16:18:10 -0800135
136 template <typename Func>
137 std::vector<ResourceConfigValue*> FindValuesIf(Func f) {
138 std::vector<ResourceConfigValue*> results;
139 for (auto& config_value : values) {
140 if (f(config_value.get())) {
141 results.push_back(config_value.get());
142 }
143 }
144 return results;
145 }
146
147 bool HasDefaultValue() const;
Adam Lesinski458b8772016-04-25 14:20:21 -0700148
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 private:
150 DISALLOW_COPY_AND_ASSIGN(ResourceEntry);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800151};
152
Adam Lesinski71be7052017-12-12 16:48:07 -0800153// Represents a resource type (eg. string, drawable, layout, etc.) containing resource entries.
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800154class ResourceTableType {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 public:
Adam Lesinski71be7052017-12-12 16:48:07 -0800156 // The logical type of resource (string, drawable, layout, etc.).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 const ResourceType type;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800158
Adam Lesinski71be7052017-12-12 16:48:07 -0800159 // Whether this type is public (and must maintain the same type ID across builds).
160 Visibility::Level visibility_level = Visibility::Level::kUndefined;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800161
Adam Lesinski71be7052017-12-12 16:48:07 -0800162 // List of resources for this type.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 std::vector<std::unique_ptr<ResourceEntry>> entries;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800164
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 explicit ResourceTableType(const ResourceType type) : type(type) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700167 ResourceEntry* CreateEntry(const android::StringPiece& name);
168 ResourceEntry* FindEntry(const android::StringPiece& name) const;
169 ResourceEntry* FindOrCreateEntry(const android::StringPiece& name);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800170
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 private:
172 DISALLOW_COPY_AND_ASSIGN(ResourceTableType);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173};
174
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800175class ResourceTablePackage {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 public:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 std::string name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700178
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 std::vector<std::unique_ptr<ResourceTableType>> types;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700180
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700181 explicit ResourceTablePackage(const android::StringPiece& name) : name(name.to_string()) {
182 }
183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 ResourceTablePackage() = default;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700185 ResourceTableType* FindType(ResourceType type) const;
186 ResourceTableType* FindOrCreateType(ResourceType type);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800187
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700188 private:
189 DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800190};
191
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700192struct ResourceTableTypeView {
193 ResourceType type;
194 Maybe<uint8_t> id;
195 Visibility::Level visibility_level = Visibility::Level::kUndefined;
196
197 // Entries sorted in ascending entry id order. If ids have not been assigned, the entries are
198 // // sorted lexicographically.
199 std::vector<const ResourceEntry*> entries;
200};
201
202struct ResourceTablePackageView {
203 std::string name;
204 Maybe<uint8_t> id;
205 // Types sorted in ascending type id order. If ids have not been assigned, the types are sorted by
206 // their declaration order in the ResourceType enum.
207 std::vector<ResourceTableTypeView> types;
208};
209
210struct ResourceTableView {
211 // Packages sorted in ascending package id order. If ids have not been assigned, the packages are
212 // sorted lexicographically.
213 std::vector<ResourceTablePackageView> packages;
214};
215
216enum class OnIdConflict {
217 // If the resource entry already exists but has a different resource id, the resource value will
218 // not be added to the table.
219 ERROR,
220
221 // If the resource entry already exists but has a different resource id, create a new resource
222 // with this resource name and id combination.
223 CREATE_ENTRY,
224};
225
226struct NewResource {
227 ResourceName name;
228 std::unique_ptr<Value> value;
229 android::ConfigDescription config;
230 std::string product;
231 std::optional<std::pair<ResourceId, OnIdConflict>> id;
232 std::optional<Visibility> visibility;
233 std::optional<OverlayableItem> overlayable;
234 std::optional<AllowNew> allow_new;
235 bool allow_mangled = false;
236};
237
238struct NewResourceBuilder {
239 explicit NewResourceBuilder(const ResourceNameRef& name);
240 explicit NewResourceBuilder(const std::string& name);
241 NewResourceBuilder& SetValue(std::unique_ptr<Value> value, android::ConfigDescription config = {},
242 std::string product = {});
243 NewResourceBuilder& SetId(ResourceId id, OnIdConflict on_conflict = OnIdConflict::ERROR);
244 NewResourceBuilder& SetVisibility(Visibility id);
245 NewResourceBuilder& SetOverlayable(OverlayableItem overlayable);
246 NewResourceBuilder& SetAllowNew(AllowNew allow_new);
247 NewResourceBuilder& SetAllowMangled(bool allow_mangled);
248 NewResource Build();
249
250 private:
251 NewResource res_;
252};
253
Adam Lesinski71be7052017-12-12 16:48:07 -0800254// The container and index for all resources defined for an app.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255class ResourceTable {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 public:
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700257 enum class Validation {
258 kEnabled,
259 kDisabled,
260 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800261
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700262 enum class CollisionResult { kKeepBoth, kKeepOriginal, kConflict, kTakeNew };
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700263
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700264 ResourceTable() = default;
265 explicit ResourceTable(Validation validation);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700266
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700267 bool AddResource(NewResource&& res, IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700269 // Retrieves a sorted a view of the packages, types, and entries sorted in ascending resource id
270 // order.
271 ResourceTableView GetPartitionedView() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 struct SearchResult {
274 ResourceTablePackage* package;
275 ResourceTableType* type;
276 ResourceEntry* entry;
277 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700278
Adam Lesinski71be7052017-12-12 16:48:07 -0800279 Maybe<SearchResult> FindResource(const ResourceNameRef& name) const;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700280 Maybe<SearchResult> FindResource(const ResourceNameRef& name, ResourceId id) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700281
Adam Lesinski71be7052017-12-12 16:48:07 -0800282 // Returns the package struct with the given name, or nullptr if such a package does not
283 // exist. The empty string is a valid package and typically is used to represent the
284 // 'current' package before it is known to the ResourceTable.
285 ResourceTablePackage* FindPackage(const android::StringPiece& name) const;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700286 ResourceTablePackage* FindOrCreatePackage(const android::StringPiece& name);
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000287
Shane Farmer0a5b2012017-06-22 12:24:12 -0700288 std::unique_ptr<ResourceTable> Clone() const;
289
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700290 // When a collision of resources occurs, this method decides which value to keep.
291 static CollisionResult ResolveValueCollision(Value* existing, Value* incoming);
292
Adam Lesinski71be7052017-12-12 16:48:07 -0800293 // The string pool used by this resource table. Values that reference strings must use
294 // this pool to create their strings.
295 // NOTE: `string_pool` must come before `packages` so that it is destroyed after.
296 // When `string_pool` references are destroyed (as they will be when `packages` is destroyed),
297 // they decrement a refCount, which would cause invalid memory access if the pool was already
298 // destroyed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700299 StringPool string_pool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800300
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000301 // The list of packages in this table, sorted alphabetically by package name and increasing
302 // package ID (missing ID being the lowest).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700303 std::vector<std::unique_ptr<ResourceTablePackage>> packages;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800304
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800305 // Set of dynamic packages that this table may reference. Their package names get encoded
306 // into the resources.arsc along with their compile-time assigned IDs.
307 std::map<size_t, std::string> included_packages_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308
309 private:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 DISALLOW_COPY_AND_ASSIGN(ResourceTable);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700311
312 Validation validation_ = Validation::kEnabled;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800313};
314
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800316
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317#endif // AAPT_RESOURCE_TABLE_H