blob: 8ab1493c6ab3170dc1f7a3ae575a188011fdd072 [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
Adam Lesinskicacb28f2016-10-19 12:18:14 -070017#include "ResourceTable.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080018
Adam Lesinskicacb28f2016-10-19 12:18:14 -070019#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <memory>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021#include <tuple>
22
Adam Lesinski66ea8402017-06-28 11:44:11 -070023#include "android-base/logging.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020024#include "androidfw/ConfigDescription.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070025#include "androidfw/ResourceTypes.h"
26
Adam Lesinski66ea8402017-06-28 11:44:11 -070027#include "NameMangler.h"
Ryan Mitchell9634efb2021-03-19 14:53:17 -070028#include "ResourceUtils.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070029#include "ResourceValues.h"
30#include "ValueVisitor.h"
31#include "text/Unicode.h"
Ryan Mitchell9634efb2021-03-19 14:53:17 -070032#include "trace/TraceBuffer.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070033#include "util/Util.h"
34
35using ::aapt::text::IsValidResourceEntryName;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020036using ::android::ConfigDescription;
Adam Lesinski66ea8402017-06-28 11:44:11 -070037using ::android::StringPiece;
Adam Lesinski71be7052017-12-12 16:48:07 -080038using ::android::base::StringPrintf;
Adam Lesinskid5083f62017-01-16 15:07:21 -080039
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040namespace aapt {
41
Ryan Mitchell54237ff2018-12-13 15:44:29 -080042const char* Overlayable::kActorScheme = "overlay";
43
Ryan Mitchell9634efb2021-03-19 14:53:17 -070044namespace {
45bool less_than_type(const std::unique_ptr<ResourceTableType>& lhs, ResourceType rhs) {
46 return lhs->type < rhs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047}
48
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049template <typename T>
Ryan Mitchell9634efb2021-03-19 14:53:17 -070050bool less_than_struct_with_name(const std::unique_ptr<T>& lhs, const StringPiece& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052}
53
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000054template <typename T>
Ryan Mitchell9634efb2021-03-19 14:53:17 -070055bool greater_than_struct_with_name(const StringPiece& lhs, const std::unique_ptr<T>& rhs) {
56 return rhs->name.compare(0, rhs->name.size(), lhs.data(), lhs.size()) > 0;
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000057}
58
Ryan Mitchell9634efb2021-03-19 14:53:17 -070059template <typename T>
60struct NameEqualRange {
61 bool operator()(const std::unique_ptr<T>& lhs, const StringPiece& rhs) const {
62 return less_than_struct_with_name<T>(lhs, rhs);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -070064 bool operator()(const StringPiece& lhs, const std::unique_ptr<T>& rhs) const {
65 return greater_than_struct_with_name<T>(lhs, rhs);
66 }
67};
68
69template <typename T, typename U>
70bool less_than_struct_with_name_and_id(const T& lhs,
71 const std::pair<std::string_view, Maybe<U>>& rhs) {
72 if (lhs.id != rhs.second) {
73 return lhs.id < rhs.second;
74 }
75 return lhs.name.compare(0, lhs.name.size(), rhs.first.data(), rhs.first.size()) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076}
77
Ryan Mitchell9634efb2021-03-19 14:53:17 -070078template <typename T, typename Func, typename Elements>
79T* FindElementsRunAction(const android::StringPiece& name, Elements& entries, Func action) {
80 const auto iter =
81 std::lower_bound(entries.begin(), entries.end(), name, less_than_struct_with_name<T>);
82 const bool found = iter != entries.end() && name == (*iter)->name;
83 return action(found, iter);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084}
85
Ryan Mitchell2fedba92021-04-23 07:47:38 -070086struct ConfigKey {
87 const ConfigDescription* config;
88 const StringPiece& product;
89};
90
91template <typename T>
92bool lt_config_key_ref(const T& lhs, const ConfigKey& rhs) {
93 int cmp = lhs->config.compare(*rhs.config);
94 if (cmp == 0) {
95 cmp = StringPiece(lhs->product).compare(rhs.product);
96 }
97 return cmp < 0;
98}
99
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700100} // namespace
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000101
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700102ResourceTable::ResourceTable(ResourceTable::Validation validation) : validation_(validation) {
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000103}
104
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700105ResourceTablePackage* ResourceTable::FindPackage(const android::StringPiece& name) const {
106 return FindElementsRunAction<ResourceTablePackage>(
107 name, packages, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700108}
109
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700110ResourceTablePackage* ResourceTable::FindOrCreatePackage(const android::StringPiece& name) {
111 return FindElementsRunAction<ResourceTablePackage>(name, packages, [&](bool found, auto& iter) {
112 return found ? iter->get() : packages.emplace(iter, new ResourceTablePackage(name))->get();
113 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114}
115
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700116template <typename Func, typename Elements>
117static ResourceTableType* FindTypeRunAction(ResourceType type, Elements& entries, Func action) {
118 const auto iter = std::lower_bound(entries.begin(), entries.end(), type, less_than_type);
119 const bool found = iter != entries.end() && type == (*iter)->type;
120 return action(found, iter);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700121}
122
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700123ResourceTableType* ResourceTablePackage::FindType(ResourceType type) const {
124 return FindTypeRunAction(type, types,
125 [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700126}
127
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700128ResourceTableType* ResourceTablePackage::FindOrCreateType(ResourceType type) {
129 return FindTypeRunAction(type, types, [&](bool found, auto& iter) {
130 return found ? iter->get() : types.emplace(iter, new ResourceTableType(type))->get();
131 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700132}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700134ResourceEntry* ResourceTableType::CreateEntry(const android::StringPiece& name) {
135 return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
136 return entries.emplace(iter, new ResourceEntry(name))->get();
137 });
138}
139
140ResourceEntry* ResourceTableType::FindEntry(const android::StringPiece& name) const {
141 return FindElementsRunAction<ResourceEntry>(
142 name, entries, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
143}
144
145ResourceEntry* ResourceTableType::FindOrCreateEntry(const android::StringPiece& name) {
146 return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
147 return found ? iter->get() : entries.emplace(iter, new ResourceEntry(name))->get();
148 });
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800149}
150
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config,
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700152 android::StringPiece product) {
153 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700154 lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700155 if (iter != values.end()) {
156 ResourceConfigValue* value = iter->get();
157 if (value->config == config && StringPiece(value->product) == product) {
158 return value;
159 }
160 }
161 return nullptr;
162}
163
164const ResourceConfigValue* ResourceEntry::FindValue(const android::ConfigDescription& config,
165 android::StringPiece product) const {
Adam Lesinski34a16872018-02-23 16:18:10 -0800166 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700167 lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 if (iter != values.end()) {
169 ResourceConfigValue* value = iter->get();
170 if (value->config == config && StringPiece(value->product) == product) {
171 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800172 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 }
174 return nullptr;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800175}
176
Adam Lesinskib1afa072017-03-29 13:52:38 -0700177ResourceConfigValue* ResourceEntry::FindOrCreateValue(const ConfigDescription& config,
178 const StringPiece& product) {
Adam Lesinski34a16872018-02-23 16:18:10 -0800179 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700180 lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 if (iter != values.end()) {
182 ResourceConfigValue* value = iter->get();
183 if (value->config == config && StringPiece(value->product) == product) {
184 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800185 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 }
187 ResourceConfigValue* newValue =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700188 values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))->get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 return newValue;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800190}
191
Adam Lesinskib1afa072017-03-29 13:52:38 -0700192std::vector<ResourceConfigValue*> ResourceEntry::FindAllValues(const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700193 std::vector<ResourceConfigValue*> results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800194
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 auto iter = values.begin();
196 for (; iter != values.end(); ++iter) {
197 ResourceConfigValue* value = iter->get();
198 if (value->config == config) {
199 results.push_back(value);
200 ++iter;
201 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800202 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800204
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700205 for (; iter != values.end(); ++iter) {
206 ResourceConfigValue* value = iter->get();
207 if (value->config == config) {
208 results.push_back(value);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800209 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700210 }
211 return results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800212}
213
Adam Lesinski34a16872018-02-23 16:18:10 -0800214bool ResourceEntry::HasDefaultValue() const {
215 const ConfigDescription& default_config = ConfigDescription::DefaultConfig();
216
217 // The default config should be at the top of the list, since the list is sorted.
218 for (auto& config_value : values) {
219 if (config_value->config == default_config) {
220 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700221 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700222 }
Adam Lesinski34a16872018-02-23 16:18:10 -0800223 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -0700224}
225
Adam Lesinski71be7052017-12-12 16:48:07 -0800226// The default handler for collisions.
227//
228// Typically, a weak value will be overridden by a strong value. An existing weak
229// value will not be overridden by an incoming weak value.
230//
231// There are some exceptions:
232//
233// Attributes: There are two types of Attribute values: USE and DECL.
234//
235// USE is anywhere an Attribute is declared without a format, and in a place that would
236// be legal to declare if the Attribute already existed. This is typically in a
237// <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak.
238//
239// DECL is an absolute declaration of an Attribute and specifies an explicit format.
240//
241// A DECL will override a USE without error. Two DECLs must match in their format for there to be
242// no error.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700243ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* existing,
244 Value* incoming) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700245 Attribute* existing_attr = ValueCast<Attribute>(existing);
246 Attribute* incoming_attr = ValueCast<Attribute>(incoming);
247 if (!incoming_attr) {
248 if (incoming->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 // We're trying to add a weak resource but a resource
250 // already exists. Keep the existing.
251 return CollisionResult::kKeepOriginal;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700252 } else if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 // Override the weak resource with the new strong resource.
254 return CollisionResult::kTakeNew;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800255 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 // The existing and incoming values are strong, this is an error
257 // if the values are not both attributes.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700258 return CollisionResult::kConflict;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 }
260
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 if (!existing_attr) {
262 if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 // The existing value is not an attribute and it is weak,
264 // so take the incoming attribute value.
265 return CollisionResult::kTakeNew;
266 }
267 // The existing value is not an attribute and it is strong,
268 // so the incoming attribute value is an error.
269 return CollisionResult::kConflict;
270 }
271
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700272 CHECK(incoming_attr != nullptr && existing_attr != nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273
274 //
275 // Attribute specific handling. At this point we know both
276 // values are attributes. Since we can declare and define
277 // attributes all-over, we do special handling to see
278 // which definition sticks.
279 //
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800280 if (existing_attr->IsCompatibleWith(*incoming_attr)) {
281 // The two attributes are both DECLs, but they are plain attributes with compatible formats.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700282 // Keep the strongest one.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700283 return existing_attr->IsWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 }
285
Adam Lesinskib1afa072017-03-29 13:52:38 -0700286 if (existing_attr->IsWeak() && existing_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 // Any incoming attribute is better than this.
288 return CollisionResult::kTakeNew;
289 }
290
Adam Lesinskib1afa072017-03-29 13:52:38 -0700291 if (incoming_attr->IsWeak() && incoming_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 // The incoming attribute may be a USE instead of a DECL.
293 // Keep the existing attribute.
294 return CollisionResult::kKeepOriginal;
295 }
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700296
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 return CollisionResult::kConflict;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800298}
299
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700300namespace {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700301template <typename T, typename Comparer>
302struct SortedVectorInserter : public Comparer {
303 std::pair<bool, typename std::vector<T>::iterator> LowerBound(std::vector<T>& el,
304 const T& value) {
305 auto it = std::lower_bound(el.begin(), el.end(), value, [&](auto& lhs, auto& rhs) {
306 return Comparer::operator()(lhs, rhs);
307 });
308 bool found =
309 it != el.end() && !Comparer::operator()(*it, value) && !Comparer::operator()(value, *it);
310 return std::make_pair(found, it);
311 }
312
313 T* Insert(std::vector<T>& el, T&& value) {
314 auto [found, it] = LowerBound(el, value);
315 if (found) {
316 return &*it;
317 }
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700318 return &*el.insert(it, std::forward<T>(value));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700319 }
320};
321
322struct PackageViewComparer {
323 bool operator()(const ResourceTablePackageView& lhs, const ResourceTablePackageView& rhs) {
324 return less_than_struct_with_name_and_id<ResourceTablePackageView, uint8_t>(
325 lhs, std::make_pair(rhs.name, rhs.id));
326 }
327};
328
329struct TypeViewComparer {
330 bool operator()(const ResourceTableTypeView& lhs, const ResourceTableTypeView& rhs) {
331 return lhs.id != rhs.id ? lhs.id < rhs.id : lhs.type < rhs.type;
332 }
333};
334
335struct EntryViewComparer {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700336 bool operator()(const ResourceTableEntryView& lhs, const ResourceTableEntryView& rhs) {
337 return less_than_struct_with_name_and_id<ResourceTableEntryView, uint16_t>(
338 lhs, std::make_pair(rhs.name, rhs.id));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700339 }
340};
341
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700342void InsertEntryIntoTableView(ResourceTableView& table, const ResourceTablePackage* package,
343 const ResourceTableType* type, const std::string& entry_name,
344 const Maybe<ResourceId>& id, const Visibility& visibility,
345 const Maybe<AllowNew>& allow_new,
346 const Maybe<OverlayableItem>& overlayable_item,
347 const Maybe<StagedId>& staged_id,
348 const std::vector<std::unique_ptr<ResourceConfigValue>>& values) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700349 SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter;
350 SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700351 SortedVectorInserter<ResourceTableEntryView, EntryViewComparer> entry_inserter;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700352
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700353 ResourceTablePackageView new_package{package->name,
354 id ? id.value().package_id() : Maybe<uint8_t>{}};
355 auto view_package = package_inserter.Insert(table.packages, std::move(new_package));
356
357 ResourceTableTypeView new_type{type->type, id ? id.value().type_id() : Maybe<uint8_t>{}};
358 auto view_type = type_inserter.Insert(view_package->types, std::move(new_type));
359
360 if (visibility.level == Visibility::Level::kPublic) {
361 // Only mark the type visibility level as public, it doesn't care about being private.
362 view_type->visibility_level = Visibility::Level::kPublic;
363 }
364
365 ResourceTableEntryView new_entry{.name = entry_name,
366 .id = id ? id.value().entry_id() : Maybe<uint16_t>{},
367 .visibility = visibility,
368 .allow_new = allow_new,
369 .overlayable_item = overlayable_item,
370 .staged_id = staged_id};
371 for (auto& value : values) {
372 new_entry.values.emplace_back(value.get());
373 }
374
375 entry_inserter.Insert(view_type->entries, std::move(new_entry));
376}
377} // namespace
378
379const ResourceConfigValue* ResourceTableEntryView::FindValue(const ConfigDescription& config,
380 android::StringPiece product) const {
381 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
382 lt_config_key_ref<const ResourceConfigValue*>);
383 if (iter != values.end()) {
384 const ResourceConfigValue* value = *iter;
385 if (value->config == config && StringPiece(value->product) == product) {
386 return value;
387 }
388 }
389 return nullptr;
390}
391
392ResourceTableView ResourceTable::GetPartitionedView(const ResourceTableViewOptions& options) const {
393 ResourceTableView view;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700394 for (const auto& package : packages) {
395 for (const auto& type : package->types) {
396 for (const auto& entry : type->entries) {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700397 InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, entry->id,
398 entry->visibility, entry->allow_new, entry->overlayable_item,
399 entry->staged_id, entry->values);
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700400
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700401 if (options.create_alias_entries && entry->staged_id) {
402 auto alias_id = entry->staged_id.value().id;
403 InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, alias_id,
404 entry->visibility, entry->allow_new, entry->overlayable_item, {},
405 entry->values);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700406 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700407 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800408 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800410
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700411 // The android runtime does not support querying resources when the there are multiple type ids
412 // for the same resource type within the same package. For this reason, if there are types with
413 // multiple type ids, each type needs to exist in its own package in order to be queried by name.
414 std::vector<ResourceTablePackageView> new_packages;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700415 SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter;
416 SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700417 for (auto& package : view.packages) {
418 // If a new package was already created for a different type within this package, then
419 // we can reuse those packages for other types that need to be extracted from this package.
420 // `start_index` is the index of the first newly created package that can be reused.
421 const size_t start_index = new_packages.size();
422 std::map<ResourceType, size_t> type_new_package_index;
423 for (auto type_it = package.types.begin(); type_it != package.types.end();) {
424 auto& type = *type_it;
425 auto type_index_iter = type_new_package_index.find(type.type);
426 if (type_index_iter == type_new_package_index.end()) {
427 // First occurrence of the resource type in this package. Keep it in this package.
428 type_new_package_index.insert(type_index_iter, std::make_pair(type.type, start_index));
429 ++type_it;
430 continue;
431 }
432
433 // The resource type has already been seen for this package, so this type must be extracted to
434 // a new separate package.
435 const size_t index = type_index_iter->second;
436 if (new_packages.size() == index) {
437 new_packages.emplace_back(ResourceTablePackageView{package.name, package.id});
438 type_new_package_index[type.type] = index + 1;
439 }
440
441 // Move the type into a new package
442 auto& other_package = new_packages[index];
443 type_inserter.Insert(other_package.types, std::move(type));
444 type_it = package.types.erase(type_it);
445 }
446 }
447
448 for (auto& new_package : new_packages) {
449 // Insert newly created packages after their original packages
450 auto [_, it] = package_inserter.LowerBound(view.packages, new_package);
451 view.packages.insert(++it, std::move(new_package));
452 }
453
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700454 return view;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800455}
456
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700457bool ResourceTable::AddResource(NewResource&& res, IDiagnostics* diag) {
458 CHECK(diag != nullptr) << "Diagnostic pointer is null";
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700459
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700460 const bool validate = validation_ == Validation::kEnabled;
461 const Source source = res.value ? res.value->GetSource() : Source{};
462 if (validate && !res.allow_mangled && !IsValidResourceEntryName(res.name.entry)) {
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700463 diag->Error(DiagMessage(source)
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700464 << "resource '" << res.name << "' has invalid entry name '" << res.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700465 return false;
466 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800467
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700468 if (res.id.has_value() && !res.id->first.is_valid()) {
469 diag->Error(DiagMessage(source) << "trying to add resource '" << res.name << "' with ID "
470 << res.id->first << " but that ID is invalid");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700471 return false;
472 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700473
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700474 auto package = FindOrCreatePackage(res.name.package);
475 auto type = package->FindOrCreateType(res.name.type);
476 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), res.name.entry,
477 NameEqualRange<ResourceEntry>{});
478 const size_t entry_count = std::distance(entry_it.first, entry_it.second);
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700479
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700480 ResourceEntry* entry;
481 if (entry_count == 0) {
482 // Adding a new resource
483 entry = type->CreateEntry(res.name.entry);
484 } else if (entry_count == 1) {
485 // Assume that the existing resource is being modified
486 entry = entry_it.first->get();
487 } else {
488 // Multiple resources with the same name exist in the resource table. The only way to
489 // distinguish between them is using resource id since each resource should have a unique id.
490 CHECK(res.id.has_value()) << "ambiguous modification of resource entry '" << res.name
491 << "' without specifying a resource id.";
492 entry = entry_it.first->get();
493 for (auto it = entry_it.first; it != entry_it.second; ++it) {
494 CHECK((bool)(*it)->id) << "ambiguous modification of resource entry '" << res.name
495 << "' with multiple entries without resource ids";
496 if ((*it)->id == res.id->first) {
497 entry = it->get();
498 break;
499 }
500 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700501 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800502
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700503 if (res.id.has_value()) {
504 if (entry->id && entry->id.value() != res.id->first) {
505 if (res.id->second != OnIdConflict::CREATE_ENTRY) {
506 diag->Error(DiagMessage(source)
507 << "trying to add resource '" << res.name << "' with ID " << res.id->first
508 << " but resource already has ID " << entry->id.value());
509 return false;
510 }
511 entry = type->CreateEntry(res.name.entry);
512 }
513 entry->id = res.id->first;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700514 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800515
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700516 if (res.visibility.has_value()) {
517 // Only mark the type visibility level as public, it doesn't care about being private.
518 if (res.visibility->level == Visibility::Level::kPublic) {
519 type->visibility_level = Visibility::Level::kPublic;
520 }
521
522 if (res.visibility->level > entry->visibility.level) {
523 // This symbol definition takes precedence, replace.
524 entry->visibility = res.visibility.value();
525 }
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700526
527 if (res.visibility->staged_api) {
528 entry->visibility.staged_api = entry->visibility.staged_api;
529 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700530 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800531
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700532 if (res.overlayable.has_value()) {
533 if (entry->overlayable_item) {
534 diag->Error(DiagMessage(res.overlayable->source)
535 << "duplicate overlayable declaration for resource '" << res.name << "'");
536 diag->Error(DiagMessage(entry->overlayable_item.value().source)
537 << "previous declaration here");
538 return false;
539 }
540 entry->overlayable_item = res.overlayable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700541 }
542
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700543 if (res.allow_new.has_value()) {
544 entry->allow_new = res.allow_new.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700545 }
546
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700547 if (res.staged_id.has_value()) {
548 entry->staged_id = res.staged_id.value();
549 }
550
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700551 if (res.value != nullptr) {
552 auto config_value = entry->FindOrCreateValue(res.config, res.product);
553 if (!config_value->value) {
554 // Resource does not exist, add it now.
555 config_value->value = std::move(res.value);
556 } else {
557 // When validation is enabled, ensure that a resource cannot have multiple values defined for
558 // the same configuration.
559 auto result = validate ? ResolveValueCollision(config_value->value.get(), res.value.get())
560 : CollisionResult::kKeepBoth;
561 switch (result) {
562 case CollisionResult::kKeepBoth:
563 // Insert the value ignoring for duplicate configurations
564 entry->values.push_back(util::make_unique<ResourceConfigValue>(res.config, res.product));
565 entry->values.back()->value = std::move(res.value);
566 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800567
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700568 case CollisionResult::kTakeNew:
569 // Take the incoming value.
570 config_value->value = std::move(res.value);
571 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800572
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700573 case CollisionResult::kConflict:
574 diag->Error(DiagMessage(source) << "duplicate value for resource '" << res.name << "' "
575 << "with config '" << res.config << "'");
576 diag->Error(DiagMessage(source) << "resource previously defined here");
577 return false;
Adam Lesinski71be7052017-12-12 16:48:07 -0800578
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700579 case CollisionResult::kKeepOriginal:
580 break;
581 }
582 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800583 }
584
Adam Lesinski71be7052017-12-12 16:48:07 -0800585 return true;
586}
587
588Maybe<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700589 ResourceTablePackage* package = FindPackage(name.package);
Adam Lesinski71be7052017-12-12 16:48:07 -0800590 if (package == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700591 return {};
592 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800593
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700594 ResourceTableType* type = package->FindType(name.type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800595 if (type == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700596 return {};
597 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800598
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700599 ResourceEntry* entry = type->FindEntry(name.entry);
Adam Lesinski71be7052017-12-12 16:48:07 -0800600 if (entry == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700601 return {};
602 }
603 return SearchResult{package, type, entry};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800604}
605
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700606Maybe<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name,
607 ResourceId id) const {
608 ResourceTablePackage* package = FindPackage(name.package);
609 if (package == nullptr) {
610 return {};
611 }
612
613 ResourceTableType* type = package->FindType(name.type);
614 if (type == nullptr) {
615 return {};
616 }
617
618 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry,
619 NameEqualRange<ResourceEntry>{});
620 for (auto it = entry_it.first; it != entry_it.second; ++it) {
621 if ((*it)->id == id) {
622 return SearchResult{package, type, it->get()};
623 }
624 }
625 return {};
626}
627
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700628bool ResourceTable::RemoveResource(const ResourceNameRef& name, ResourceId id) const {
629 ResourceTablePackage* package = FindPackage(name.package);
630 if (package == nullptr) {
631 return {};
632 }
633
634 ResourceTableType* type = package->FindType(name.type);
635 if (type == nullptr) {
636 return {};
637 }
638
639 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry,
640 NameEqualRange<ResourceEntry>{});
641 for (auto it = entry_it.first; it != entry_it.second; ++it) {
642 if ((*it)->id == id) {
643 type->entries.erase(it);
644 return true;
645 }
646 }
647 return false;
648}
649
Shane Farmer0a5b2012017-06-22 12:24:12 -0700650std::unique_ptr<ResourceTable> ResourceTable::Clone() const {
651 std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>();
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700652 CloningValueTransformer cloner(&new_table->string_pool);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700653 for (const auto& pkg : packages) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700654 ResourceTablePackage* new_pkg = new_table->FindOrCreatePackage(pkg->name);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700655 for (const auto& type : pkg->types) {
656 ResourceTableType* new_type = new_pkg->FindOrCreateType(type->type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800657 new_type->visibility_level = type->visibility_level;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700658
659 for (const auto& entry : type->entries) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700660 ResourceEntry* new_entry = new_type->CreateEntry(entry->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800661 new_entry->id = entry->id;
662 new_entry->visibility = entry->visibility;
663 new_entry->allow_new = entry->allow_new;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800664 new_entry->overlayable_item = entry->overlayable_item;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700665
666 for (const auto& config_value : entry->values) {
667 ResourceConfigValue* new_value =
668 new_entry->FindOrCreateValue(config_value->config, config_value->product);
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700669 new_value->value = config_value->value->Transform(cloner);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700670 }
671 }
672 }
673 }
674 return new_table;
675}
676
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700677NewResourceBuilder::NewResourceBuilder(const ResourceNameRef& name) {
678 res_.name = name.ToResourceName();
679}
680
681NewResourceBuilder::NewResourceBuilder(const std::string& name) {
682 ResourceNameRef ref;
683 CHECK(ResourceUtils::ParseResourceName(name, &ref)) << "invalid resource name: " << name;
684 res_.name = ref.ToResourceName();
685}
686
687NewResourceBuilder& NewResourceBuilder::SetValue(std::unique_ptr<Value> value,
688 android::ConfigDescription config,
689 std::string product) {
690 res_.value = std::move(value);
691 res_.config = std::move(config);
692 res_.product = std::move(product);
693 return *this;
694}
695
696NewResourceBuilder& NewResourceBuilder::SetId(ResourceId id, OnIdConflict on_conflict) {
697 res_.id = std::make_pair(id, on_conflict);
698 return *this;
699}
700
701NewResourceBuilder& NewResourceBuilder::SetVisibility(Visibility visibility) {
702 res_.visibility = std::move(visibility);
703 return *this;
704}
705
706NewResourceBuilder& NewResourceBuilder::SetOverlayable(OverlayableItem overlayable) {
707 res_.overlayable = std::move(overlayable);
708 return *this;
709}
710NewResourceBuilder& NewResourceBuilder::SetAllowNew(AllowNew allow_new) {
711 res_.allow_new = std::move(allow_new);
712 return *this;
713}
714
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700715NewResourceBuilder& NewResourceBuilder::SetStagedId(StagedId staged_alias) {
716 res_.staged_id = std::move(staged_alias);
717 return *this;
718}
719
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700720NewResourceBuilder& NewResourceBuilder::SetAllowMangled(bool allow_mangled) {
721 res_.allow_mangled = allow_mangled;
722 return *this;
723}
724
725NewResource NewResourceBuilder::Build() {
726 return std::move(res_);
727}
728
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700729} // namespace aapt