blob: 2e17659b06790695b475aefda764d59b38a96674 [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;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -070054
55 // Indicates that the resource id may change across builds and that the public R.java identifier
56 // for this resource should not be final. This is set to `true` for resources in `staging-group`
57 // tags.
58 bool staged_api = false;
Adam Lesinski9e10ac72015-10-16 14:37:48 -070059};
60
Adam Lesinski71be7052017-12-12 16:48:07 -080061// Represents <add-resource> in an overlay.
62struct AllowNew {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 Source source;
Adam Lesinski71be7052017-12-12 16:48:07 -080064 std::string comment;
65};
Adam Lesinski4488f1c2017-05-26 17:33:38 -070066
Ryan Mitchell2fedba92021-04-23 07:47:38 -070067// Represents the staged resource id of a finalized resource.
68struct StagedId {
69 ResourceId id;
70 Source source;
71};
72
Adam Lesinski71be7052017-12-12 16:48:07 -080073struct Overlayable {
Ryan Mitchell54237ff2018-12-13 15:44:29 -080074 Overlayable() = default;
75 Overlayable(const android::StringPiece& name, const android::StringPiece& actor)
76 : name(name.to_string()), actor(actor.to_string()) {}
77 Overlayable(const android::StringPiece& name, const android::StringPiece& actor,
78 const Source& source)
79 : name(name.to_string()), actor(actor.to_string()), source(source ){}
80
81 static const char* kActorScheme;
82 std::string name;
83 std::string actor;
84 Source source;
85};
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;
Ryan Mitchell54237ff2018-12-13 15:44:29 -080094 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.).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 const ResourceType 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
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 explicit ResourceTableType(const ResourceType type) : type(type) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700180
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700181 ResourceEntry* CreateEntry(const android::StringPiece& name);
182 ResourceEntry* FindEntry(const android::StringPiece& name) const;
183 ResourceEntry* FindOrCreateEntry(const android::StringPiece& name);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800184
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 private:
186 DISALLOW_COPY_AND_ASSIGN(ResourceTableType);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700187};
188
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800189class ResourceTablePackage {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 public:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 std::string name;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700192
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 std::vector<std::unique_ptr<ResourceTableType>> types;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700194
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700195 explicit ResourceTablePackage(const android::StringPiece& name) : name(name.to_string()) {
196 }
197
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 ResourceTablePackage() = default;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700199 ResourceTableType* FindType(ResourceType type) const;
200 ResourceTableType* FindOrCreateType(ResourceType type);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800201
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 private:
203 DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204};
205
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700206struct ResourceTableEntryView {
207 std::string name;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700208 std::optional<uint16_t> id;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700209 Visibility visibility;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700210 std::optional<AllowNew> allow_new;
211 std::optional<OverlayableItem> overlayable_item;
212 std::optional<StagedId> staged_id;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700213 std::vector<const ResourceConfigValue*> values;
214
215 const ResourceConfigValue* FindValue(const android::ConfigDescription& config,
216 android::StringPiece product = {}) const;
217};
218
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700219struct ResourceTableTypeView {
220 ResourceType type;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700221 std::optional<uint8_t> id;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700222 Visibility::Level visibility_level = Visibility::Level::kUndefined;
223
224 // Entries sorted in ascending entry id order. If ids have not been assigned, the entries are
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700225 // sorted lexicographically.
226 std::vector<ResourceTableEntryView> entries;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700227};
228
229struct ResourceTablePackageView {
230 std::string name;
Ryan Mitchell4382e442021-07-14 12:53:01 -0700231 std::optional<uint8_t> id;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700232 // Types sorted in ascending type id order. If ids have not been assigned, the types are sorted by
233 // their declaration order in the ResourceType enum.
234 std::vector<ResourceTableTypeView> types;
235};
236
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700237struct ResourceTableViewOptions {
238 bool create_alias_entries = false;
239};
240
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700241struct ResourceTableView {
242 // Packages sorted in ascending package id order. If ids have not been assigned, the packages are
243 // sorted lexicographically.
244 std::vector<ResourceTablePackageView> packages;
245};
246
247enum class OnIdConflict {
248 // If the resource entry already exists but has a different resource id, the resource value will
249 // not be added to the table.
250 ERROR,
251
252 // If the resource entry already exists but has a different resource id, create a new resource
253 // with this resource name and id combination.
254 CREATE_ENTRY,
255};
256
257struct NewResource {
258 ResourceName name;
259 std::unique_ptr<Value> value;
260 android::ConfigDescription config;
261 std::string product;
262 std::optional<std::pair<ResourceId, OnIdConflict>> id;
263 std::optional<Visibility> visibility;
264 std::optional<OverlayableItem> overlayable;
265 std::optional<AllowNew> allow_new;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700266 std::optional<StagedId> staged_id;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700267 bool allow_mangled = false;
268};
269
270struct NewResourceBuilder {
271 explicit NewResourceBuilder(const ResourceNameRef& name);
272 explicit NewResourceBuilder(const std::string& name);
273 NewResourceBuilder& SetValue(std::unique_ptr<Value> value, android::ConfigDescription config = {},
274 std::string product = {});
275 NewResourceBuilder& SetId(ResourceId id, OnIdConflict on_conflict = OnIdConflict::ERROR);
276 NewResourceBuilder& SetVisibility(Visibility id);
277 NewResourceBuilder& SetOverlayable(OverlayableItem overlayable);
278 NewResourceBuilder& SetAllowNew(AllowNew allow_new);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700279 NewResourceBuilder& SetStagedId(StagedId id);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700280 NewResourceBuilder& SetAllowMangled(bool allow_mangled);
281 NewResource Build();
282
283 private:
284 NewResource res_;
285};
286
Adam Lesinski71be7052017-12-12 16:48:07 -0800287// The container and index for all resources defined for an app.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800288class ResourceTable {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 public:
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700290 enum class Validation {
291 kEnabled,
292 kDisabled,
293 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800294
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700295 enum class CollisionResult { kKeepBoth, kKeepOriginal, kConflict, kTakeNew };
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700296
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700297 ResourceTable() = default;
298 explicit ResourceTable(Validation validation);
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700299
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700300 bool AddResource(NewResource&& res, IDiagnostics* diag);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800301
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700302 // Retrieves a sorted a view of the packages, types, and entries sorted in ascending resource id
303 // order.
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700304 ResourceTableView GetPartitionedView(const ResourceTableViewOptions& options = {}) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800305
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 struct SearchResult {
307 ResourceTablePackage* package;
308 ResourceTableType* type;
309 ResourceEntry* entry;
310 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700311
Ryan Mitchell4382e442021-07-14 12:53:01 -0700312 std::optional<SearchResult> FindResource(const ResourceNameRef& name) const;
313 std::optional<SearchResult> FindResource(const ResourceNameRef& name, ResourceId id) const;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700314 bool RemoveResource(const ResourceNameRef& name, ResourceId id) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700315
Adam Lesinski71be7052017-12-12 16:48:07 -0800316 // Returns the package struct with the given name, or nullptr if such a package does not
317 // exist. The empty string is a valid package and typically is used to represent the
318 // 'current' package before it is known to the ResourceTable.
319 ResourceTablePackage* FindPackage(const android::StringPiece& name) const;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700320 ResourceTablePackage* FindOrCreatePackage(const android::StringPiece& name);
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000321
Shane Farmer0a5b2012017-06-22 12:24:12 -0700322 std::unique_ptr<ResourceTable> Clone() const;
323
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700324 // When a collision of resources occurs, this method decides which value to keep.
325 static CollisionResult ResolveValueCollision(Value* existing, Value* incoming);
326
Adam Lesinski71be7052017-12-12 16:48:07 -0800327 // The string pool used by this resource table. Values that reference strings must use
328 // this pool to create their strings.
329 // NOTE: `string_pool` must come before `packages` so that it is destroyed after.
330 // When `string_pool` references are destroyed (as they will be when `packages` is destroyed),
331 // they decrement a refCount, which would cause invalid memory access if the pool was already
332 // destroyed.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700333 StringPool string_pool;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800334
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000335 // The list of packages in this table, sorted alphabetically by package name and increasing
336 // package ID (missing ID being the lowest).
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700337 std::vector<std::unique_ptr<ResourceTablePackage>> packages;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800338
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800339 // Set of dynamic packages that this table may reference. Their package names get encoded
340 // into the resources.arsc along with their compile-time assigned IDs.
341 std::map<size_t, std::string> included_packages_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700342
343 private:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700344 DISALLOW_COPY_AND_ASSIGN(ResourceTable);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700345
346 Validation validation_ = Validation::kEnabled;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800347};
348
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700349} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800350
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700351#endif // AAPT_RESOURCE_TABLE_H