Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 21 | #include "Resource.h" |
| 22 | #include "ResourceValues.h" |
| 23 | #include "Source.h" |
| 24 | #include "StringPool.h" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 25 | #include "io/File.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 26 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 27 | #include "android-base/macros.h" |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 28 | #include "androidfw/ConfigDescription.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 29 | #include "androidfw/StringPiece.h" |
| 30 | |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 31 | #include <functional> |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 32 | #include <map> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 33 | #include <memory> |
| 34 | #include <string> |
| 35 | #include <tuple> |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 36 | #include <unordered_map> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 37 | #include <vector> |
| 38 | |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame^] | 39 | using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags; |
| 40 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 41 | namespace aapt { |
| 42 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 43 | // The Public status of a resource. |
| 44 | struct 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 Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 56 | // Represents <add-resource> in an overlay. |
| 57 | struct AllowNew { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | Source source; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 59 | std::string comment; |
| 60 | }; |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 62 | struct Overlayable { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 63 | 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. |
| 77 | struct OverlayableItem { |
| 78 | explicit OverlayableItem(const std::shared_ptr<Overlayable>& overlayable) |
| 79 | : overlayable(overlayable) {} |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 80 | std::shared_ptr<Overlayable> overlayable; |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame^] | 81 | PolicyFlags policies = PolicyFlags::NONE; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | std::string comment; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 83 | Source source; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 86 | class ResourceConfigValue { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | public: |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 88 | // The configuration for which this value is defined. |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 89 | const android::ConfigDescription config; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 90 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 91 | // The product for which this value is defined. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | const std::string product; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 93 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 94 | // The actual Value. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | std::unique_ptr<Value> value; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 96 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 97 | ResourceConfigValue(const android::ConfigDescription& config, const android::StringPiece& product) |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 98 | : config(config), product(product.to_string()) {} |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 99 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | private: |
| 101 | DISALLOW_COPY_AND_ASSIGN(ResourceConfigValue); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 104 | // Represents a resource entry, which may have varying values for each defined configuration. |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 105 | class ResourceEntry { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | public: |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 107 | // The name of the resource. Immutable, as this determines the order of this resource |
| 108 | // when doing lookups. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | const std::string name; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 110 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 111 | // The entry ID for this resource (the EEEE in 0xPPTTEEEE). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 112 | Maybe<uint16_t> id; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 113 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 114 | // Whether this resource is public (and must maintain the same entry ID across builds). |
| 115 | Visibility visibility; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 117 | Maybe<AllowNew> allow_new; |
| 118 | |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 119 | // The declarations of this resource as overlayable for RROs |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 120 | Maybe<OverlayableItem> overlayable_item; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 121 | |
| 122 | // The resource's values for each configuration. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | std::vector<std::unique_ptr<ResourceConfigValue>> values; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 124 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 125 | explicit ResourceEntry(const android::StringPiece& name) : name(name.to_string()) {} |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 126 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 127 | ResourceConfigValue* FindValue(const android::ConfigDescription& config); |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 128 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 129 | ResourceConfigValue* FindValue(const android::ConfigDescription& config, |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 130 | const android::StringPiece& product); |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 131 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 132 | ResourceConfigValue* FindOrCreateValue(const android::ConfigDescription& config, |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 133 | const android::StringPiece& product); |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 134 | std::vector<ResourceConfigValue*> FindAllValues(const android::ConfigDescription& config); |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 135 | |
| 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 Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 148 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 149 | private: |
| 150 | DISALLOW_COPY_AND_ASSIGN(ResourceEntry); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 153 | // Represents a resource type (eg. string, drawable, layout, etc.) containing resource entries. |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 154 | class ResourceTableType { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 155 | public: |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 156 | // The logical type of resource (string, drawable, layout, etc.). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 157 | const ResourceType type; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 158 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 159 | // The type ID for this resource (the TT in 0xPPTTEEEE). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 160 | Maybe<uint8_t> id; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 161 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 162 | // Whether this type is public (and must maintain the same type ID across builds). |
| 163 | Visibility::Level visibility_level = Visibility::Level::kUndefined; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 164 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 165 | // List of resources for this type. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | std::vector<std::unique_ptr<ResourceEntry>> entries; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 167 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 168 | explicit ResourceTableType(const ResourceType type) : type(type) {} |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 169 | |
Ryan Mitchell | 8d4ee97 | 2018-08-27 11:24:04 -0700 | [diff] [blame] | 170 | ResourceEntry* FindEntry(const android::StringPiece& name, |
| 171 | Maybe<uint16_t> id = Maybe<uint16_t>()); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 172 | ResourceEntry* FindOrCreateEntry(const android::StringPiece& name, |
Ryan Mitchell | 8d4ee97 | 2018-08-27 11:24:04 -0700 | [diff] [blame] | 173 | Maybe<uint16_t> id = Maybe<uint16_t>()); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 174 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 175 | private: |
| 176 | DISALLOW_COPY_AND_ASSIGN(ResourceTableType); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 179 | class ResourceTablePackage { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 180 | public: |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | std::string name; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 182 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 183 | // The package ID (the PP in 0xPPTTEEEE). |
| 184 | Maybe<uint8_t> id; |
| 185 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | std::vector<std::unique_ptr<ResourceTableType>> types; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 187 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 188 | ResourceTablePackage() = default; |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 189 | ResourceTableType* FindType(ResourceType type, Maybe<uint8_t> id = Maybe<uint8_t>()); |
| 190 | ResourceTableType* FindOrCreateType(const ResourceType type, |
| 191 | Maybe<uint8_t> id = Maybe<uint8_t>()); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 192 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 193 | private: |
| 194 | DISALLOW_COPY_AND_ASSIGN(ResourceTablePackage); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 195 | }; |
| 196 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 197 | // The container and index for all resources defined for an app. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 198 | class ResourceTable { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | public: |
| 200 | ResourceTable() = default; |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 201 | explicit ResourceTable(bool validate_resources) : validate_resources_(validate_resources) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 202 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 203 | enum class CollisionResult { kKeepBoth, kKeepOriginal, kConflict, kTakeNew }; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 204 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | using CollisionResolverFunc = std::function<CollisionResult(Value*, Value*)>; |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 206 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 207 | // When a collision of resources occurs, this method decides which value to keep. |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 208 | static CollisionResult ResolveValueCollision(Value* existing, Value* incoming); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 209 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 210 | // When a collision of resources occurs, this method keeps both values |
| 211 | static CollisionResult IgnoreCollision(Value* existing, Value* incoming); |
| 212 | |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 213 | bool AddResource(const ResourceNameRef& name, const android::ConfigDescription& config, |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 214 | const android::StringPiece& product, std::unique_ptr<Value> value, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 215 | IDiagnostics* diag); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 216 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 217 | bool AddResourceWithId(const ResourceNameRef& name, const ResourceId& res_id, |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 218 | const android::ConfigDescription& config, |
| 219 | const android::StringPiece& product, std::unique_ptr<Value> value, |
| 220 | IDiagnostics* diag); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 221 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 222 | // Same as AddResource, but doesn't verify the validity of the name. This is used |
| 223 | // when loading resources from an existing binary resource table that may have mangled names. |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 224 | bool AddResourceMangled(const ResourceNameRef& name, const android::ConfigDescription& config, |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 225 | const android::StringPiece& product, std::unique_ptr<Value> value, |
| 226 | IDiagnostics* diag); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 227 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 228 | bool AddResourceWithIdMangled(const ResourceNameRef& name, const ResourceId& id, |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 229 | const android::ConfigDescription& config, |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 230 | const android::StringPiece& product, std::unique_ptr<Value> value, |
| 231 | IDiagnostics* diag); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 232 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 233 | bool GetValidateResources(); |
| 234 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 235 | bool SetVisibility(const ResourceNameRef& name, const Visibility& visibility, IDiagnostics* diag); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 236 | bool SetVisibilityWithId(const ResourceNameRef& name, const Visibility& visibility, |
| 237 | const ResourceId& res_id, IDiagnostics* diag); |
| 238 | bool SetVisibilityWithIdMangled(const ResourceNameRef& name, const Visibility& visibility, |
| 239 | const ResourceId& res_id, IDiagnostics* diag); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 240 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 241 | bool SetOverlayable(const ResourceNameRef& name, const OverlayableItem& overlayable, |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 242 | IDiagnostics *diag); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 243 | |
| 244 | bool SetAllowNew(const ResourceNameRef& name, const AllowNew& allow_new, IDiagnostics* diag); |
| 245 | bool SetAllowNewMangled(const ResourceNameRef& name, const AllowNew& allow_new, |
| 246 | IDiagnostics* diag); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 247 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 248 | struct SearchResult { |
| 249 | ResourceTablePackage* package; |
| 250 | ResourceTableType* type; |
| 251 | ResourceEntry* entry; |
| 252 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 253 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 254 | Maybe<SearchResult> FindResource(const ResourceNameRef& name) const; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 255 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 256 | // Returns the package struct with the given name, or nullptr if such a package does not |
| 257 | // exist. The empty string is a valid package and typically is used to represent the |
| 258 | // 'current' package before it is known to the ResourceTable. |
| 259 | ResourceTablePackage* FindPackage(const android::StringPiece& name) const; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 260 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 261 | ResourceTablePackage* FindPackageById(uint8_t id) const; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 262 | |
| 263 | ResourceTablePackage* CreatePackage(const android::StringPiece& name, Maybe<uint8_t> id = {}); |
| 264 | |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 265 | // Attempts to find a package having the specified name and ID. If not found, a new package |
| 266 | // of the specified parameters is created and returned. |
| 267 | ResourceTablePackage* CreatePackageAllowingDuplicateNames(const android::StringPiece& name, |
| 268 | const Maybe<uint8_t> id); |
| 269 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 270 | std::unique_ptr<ResourceTable> Clone() const; |
| 271 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 272 | // The string pool used by this resource table. Values that reference strings must use |
| 273 | // this pool to create their strings. |
| 274 | // NOTE: `string_pool` must come before `packages` so that it is destroyed after. |
| 275 | // When `string_pool` references are destroyed (as they will be when `packages` is destroyed), |
| 276 | // they decrement a refCount, which would cause invalid memory access if the pool was already |
| 277 | // destroyed. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 278 | StringPool string_pool; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 279 | |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 280 | // The list of packages in this table, sorted alphabetically by package name and increasing |
| 281 | // package ID (missing ID being the lowest). |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 282 | std::vector<std::unique_ptr<ResourceTablePackage>> packages; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 283 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 284 | // Set of dynamic packages that this table may reference. Their package names get encoded |
| 285 | // into the resources.arsc along with their compile-time assigned IDs. |
| 286 | std::map<size_t, std::string> included_packages_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 287 | |
| 288 | private: |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 289 | // The function type that validates a symbol name. Returns a non-empty StringPiece representing |
| 290 | // the offending character (which may be more than one byte in UTF-8). Returns an empty string |
| 291 | // if the name was valid. |
| 292 | using NameValidator = android::StringPiece(const android::StringPiece&); |
| 293 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 294 | ResourceTablePackage* FindOrCreatePackage(const android::StringPiece& name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 295 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 296 | bool ValidateName(NameValidator validator, const ResourceNameRef& name, const Source& source, |
| 297 | IDiagnostics* diag); |
| 298 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 299 | bool AddResourceImpl(const ResourceNameRef& name, const ResourceId& res_id, |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 300 | const android::ConfigDescription& config, |
| 301 | const android::StringPiece& product, std::unique_ptr<Value> value, |
| 302 | NameValidator name_validator, const CollisionResolverFunc& conflict_resolver, |
| 303 | IDiagnostics* diag); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 304 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 305 | bool SetVisibilityImpl(const ResourceNameRef& name, const Visibility& visibility, |
| 306 | const ResourceId& res_id, NameValidator name_validator, |
| 307 | IDiagnostics* diag); |
| 308 | |
| 309 | bool SetAllowNewImpl(const ResourceNameRef& name, const AllowNew& allow_new, |
| 310 | NameValidator name_validator, IDiagnostics* diag); |
| 311 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 312 | bool SetOverlayableImpl(const ResourceNameRef &name, const OverlayableItem& overlayable, |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 313 | NameValidator name_validator, IDiagnostics *diag); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 314 | |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 315 | // Controls whether the table validates resource names and prevents duplicate resource names |
| 316 | bool validate_resources_ = true; |
| 317 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 318 | DISALLOW_COPY_AND_ASSIGN(ResourceTable); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 319 | }; |
| 320 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 321 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 322 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 323 | #endif // AAPT_RESOURCE_TABLE_H |