blob: 77f0ef0ed94cc2e95518960bf7249e3ebee89fc4 [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>
Ryan Mitchell4382e442021-07-14 12:53:01 -070021#include <optional>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include <tuple>
23
Adam Lesinski66ea8402017-06-28 11:44:11 -070024#include "android-base/logging.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020025#include "androidfw/ConfigDescription.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070026#include "androidfw/ResourceTypes.h"
27
Adam Lesinski66ea8402017-06-28 11:44:11 -070028#include "NameMangler.h"
Ryan Mitchell9634efb2021-03-19 14:53:17 -070029#include "ResourceUtils.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070030#include "ResourceValues.h"
31#include "ValueVisitor.h"
32#include "text/Unicode.h"
Ryan Mitchell9634efb2021-03-19 14:53:17 -070033#include "trace/TraceBuffer.h"
Adam Lesinski66ea8402017-06-28 11:44:11 -070034#include "util/Util.h"
35
36using ::aapt::text::IsValidResourceEntryName;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020037using ::android::ConfigDescription;
Adam Lesinski66ea8402017-06-28 11:44:11 -070038using ::android::StringPiece;
Adam Lesinski71be7052017-12-12 16:48:07 -080039using ::android::base::StringPrintf;
Adam Lesinskid5083f62017-01-16 15:07:21 -080040
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041namespace aapt {
42
Ryan Mitchell54237ff2018-12-13 15:44:29 -080043const char* Overlayable::kActorScheme = "overlay";
44
Ryan Mitchell9634efb2021-03-19 14:53:17 -070045namespace {
46bool less_than_type(const std::unique_ptr<ResourceTableType>& lhs, ResourceType rhs) {
47 return lhs->type < rhs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048}
49
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050template <typename T>
Ryan Mitchell9634efb2021-03-19 14:53:17 -070051bool less_than_struct_with_name(const std::unique_ptr<T>& lhs, const StringPiece& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053}
54
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000055template <typename T>
Ryan Mitchell9634efb2021-03-19 14:53:17 -070056bool greater_than_struct_with_name(const StringPiece& lhs, const std::unique_ptr<T>& rhs) {
57 return rhs->name.compare(0, rhs->name.size(), lhs.data(), lhs.size()) > 0;
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000058}
59
Ryan Mitchell9634efb2021-03-19 14:53:17 -070060template <typename T>
61struct NameEqualRange {
62 bool operator()(const std::unique_ptr<T>& lhs, const StringPiece& rhs) const {
63 return less_than_struct_with_name<T>(lhs, rhs);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -070065 bool operator()(const StringPiece& lhs, const std::unique_ptr<T>& rhs) const {
66 return greater_than_struct_with_name<T>(lhs, rhs);
67 }
68};
69
70template <typename T, typename U>
71bool less_than_struct_with_name_and_id(const T& lhs,
Ryan Mitchell4382e442021-07-14 12:53:01 -070072 const std::pair<std::string_view, std::optional<U>>& rhs) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -070073 if (lhs.id != rhs.second) {
74 return lhs.id < rhs.second;
75 }
76 return lhs.name.compare(0, lhs.name.size(), rhs.first.data(), rhs.first.size()) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077}
78
Ryan Mitchell9634efb2021-03-19 14:53:17 -070079template <typename T, typename Func, typename Elements>
80T* FindElementsRunAction(const android::StringPiece& name, Elements& entries, Func action) {
81 const auto iter =
82 std::lower_bound(entries.begin(), entries.end(), name, less_than_struct_with_name<T>);
83 const bool found = iter != entries.end() && name == (*iter)->name;
84 return action(found, iter);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085}
86
Ryan Mitchell2fedba92021-04-23 07:47:38 -070087struct ConfigKey {
88 const ConfigDescription* config;
89 const StringPiece& product;
90};
91
92template <typename T>
93bool lt_config_key_ref(const T& lhs, const ConfigKey& rhs) {
94 int cmp = lhs->config.compare(*rhs.config);
95 if (cmp == 0) {
96 cmp = StringPiece(lhs->product).compare(rhs.product);
97 }
98 return cmp < 0;
99}
100
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700101} // namespace
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000102
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700103ResourceTable::ResourceTable(ResourceTable::Validation validation) : validation_(validation) {
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000104}
105
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700106ResourceTablePackage* ResourceTable::FindPackage(const android::StringPiece& name) const {
107 return FindElementsRunAction<ResourceTablePackage>(
108 name, packages, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700109}
110
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700111ResourceTablePackage* ResourceTable::FindOrCreatePackage(const android::StringPiece& name) {
112 return FindElementsRunAction<ResourceTablePackage>(name, packages, [&](bool found, auto& iter) {
113 return found ? iter->get() : packages.emplace(iter, new ResourceTablePackage(name))->get();
114 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700115}
116
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700117template <typename Func, typename Elements>
118static ResourceTableType* FindTypeRunAction(ResourceType type, Elements& entries, Func action) {
119 const auto iter = std::lower_bound(entries.begin(), entries.end(), type, less_than_type);
120 const bool found = iter != entries.end() && type == (*iter)->type;
121 return action(found, iter);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122}
123
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700124ResourceTableType* ResourceTablePackage::FindType(ResourceType type) const {
125 return FindTypeRunAction(type, types,
126 [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700127}
128
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700129ResourceTableType* ResourceTablePackage::FindOrCreateType(ResourceType type) {
130 return FindTypeRunAction(type, types, [&](bool found, auto& iter) {
131 return found ? iter->get() : types.emplace(iter, new ResourceTableType(type))->get();
132 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700133}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800134
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700135ResourceEntry* ResourceTableType::CreateEntry(const android::StringPiece& name) {
136 return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
137 return entries.emplace(iter, new ResourceEntry(name))->get();
138 });
139}
140
141ResourceEntry* ResourceTableType::FindEntry(const android::StringPiece& name) const {
142 return FindElementsRunAction<ResourceEntry>(
143 name, entries, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
144}
145
146ResourceEntry* ResourceTableType::FindOrCreateEntry(const android::StringPiece& name) {
147 return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
148 return found ? iter->get() : entries.emplace(iter, new ResourceEntry(name))->get();
149 });
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800150}
151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config,
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700153 android::StringPiece product) {
154 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700155 lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700156 if (iter != values.end()) {
157 ResourceConfigValue* value = iter->get();
158 if (value->config == config && StringPiece(value->product) == product) {
159 return value;
160 }
161 }
162 return nullptr;
163}
164
165const ResourceConfigValue* ResourceEntry::FindValue(const android::ConfigDescription& config,
166 android::StringPiece product) const {
Adam Lesinski34a16872018-02-23 16:18:10 -0800167 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700168 lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 if (iter != values.end()) {
170 ResourceConfigValue* value = iter->get();
171 if (value->config == config && StringPiece(value->product) == product) {
172 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800173 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 }
175 return nullptr;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800176}
177
Adam Lesinskib1afa072017-03-29 13:52:38 -0700178ResourceConfigValue* ResourceEntry::FindOrCreateValue(const ConfigDescription& config,
179 const StringPiece& product) {
Adam Lesinski34a16872018-02-23 16:18:10 -0800180 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700181 lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700182 if (iter != values.end()) {
183 ResourceConfigValue* value = iter->get();
184 if (value->config == config && StringPiece(value->product) == product) {
185 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800186 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 }
188 ResourceConfigValue* newValue =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700189 values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))->get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700190 return newValue;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800191}
192
Adam Lesinskib1afa072017-03-29 13:52:38 -0700193std::vector<ResourceConfigValue*> ResourceEntry::FindAllValues(const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 std::vector<ResourceConfigValue*> results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800195
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 auto iter = values.begin();
197 for (; iter != values.end(); ++iter) {
198 ResourceConfigValue* value = iter->get();
199 if (value->config == config) {
200 results.push_back(value);
201 ++iter;
202 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800203 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700204 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800205
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 for (; iter != values.end(); ++iter) {
207 ResourceConfigValue* value = iter->get();
208 if (value->config == config) {
209 results.push_back(value);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800210 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 }
212 return results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800213}
214
Adam Lesinski34a16872018-02-23 16:18:10 -0800215bool ResourceEntry::HasDefaultValue() const {
216 const ConfigDescription& default_config = ConfigDescription::DefaultConfig();
217
218 // The default config should be at the top of the list, since the list is sorted.
219 for (auto& config_value : values) {
220 if (config_value->config == default_config) {
221 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700222 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700223 }
Adam Lesinski34a16872018-02-23 16:18:10 -0800224 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -0700225}
226
Adam Lesinski71be7052017-12-12 16:48:07 -0800227// The default handler for collisions.
228//
229// Typically, a weak value will be overridden by a strong value. An existing weak
230// value will not be overridden by an incoming weak value.
231//
232// There are some exceptions:
233//
234// Attributes: There are two types of Attribute values: USE and DECL.
235//
236// USE is anywhere an Attribute is declared without a format, and in a place that would
237// be legal to declare if the Attribute already existed. This is typically in a
238// <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak.
239//
240// DECL is an absolute declaration of an Attribute and specifies an explicit format.
241//
242// A DECL will override a USE without error. Two DECLs must match in their format for there to be
243// no error.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700244ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* existing,
245 Value* incoming) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700246 Attribute* existing_attr = ValueCast<Attribute>(existing);
247 Attribute* incoming_attr = ValueCast<Attribute>(incoming);
248 if (!incoming_attr) {
249 if (incoming->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250 // We're trying to add a weak resource but a resource
251 // already exists. Keep the existing.
252 return CollisionResult::kKeepOriginal;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 } else if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 // Override the weak resource with the new strong resource.
255 return CollisionResult::kTakeNew;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800256 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 // The existing and incoming values are strong, this is an error
258 // if the values are not both attributes.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700259 return CollisionResult::kConflict;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 }
261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262 if (!existing_attr) {
263 if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 // The existing value is not an attribute and it is weak,
265 // so take the incoming attribute value.
266 return CollisionResult::kTakeNew;
267 }
268 // The existing value is not an attribute and it is strong,
269 // so the incoming attribute value is an error.
270 return CollisionResult::kConflict;
271 }
272
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700273 CHECK(incoming_attr != nullptr && existing_attr != nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274
275 //
276 // Attribute specific handling. At this point we know both
277 // values are attributes. Since we can declare and define
278 // attributes all-over, we do special handling to see
279 // which definition sticks.
280 //
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800281 if (existing_attr->IsCompatibleWith(*incoming_attr)) {
282 // The two attributes are both DECLs, but they are plain attributes with compatible formats.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 // Keep the strongest one.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700284 return existing_attr->IsWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 }
286
Adam Lesinskib1afa072017-03-29 13:52:38 -0700287 if (existing_attr->IsWeak() && existing_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 // Any incoming attribute is better than this.
289 return CollisionResult::kTakeNew;
290 }
291
Adam Lesinskib1afa072017-03-29 13:52:38 -0700292 if (incoming_attr->IsWeak() && incoming_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 // The incoming attribute may be a USE instead of a DECL.
294 // Keep the existing attribute.
295 return CollisionResult::kKeepOriginal;
296 }
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700297
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 return CollisionResult::kConflict;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800299}
300
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700301namespace {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700302template <typename T, typename Comparer>
303struct SortedVectorInserter : public Comparer {
304 std::pair<bool, typename std::vector<T>::iterator> LowerBound(std::vector<T>& el,
305 const T& value) {
306 auto it = std::lower_bound(el.begin(), el.end(), value, [&](auto& lhs, auto& rhs) {
307 return Comparer::operator()(lhs, rhs);
308 });
309 bool found =
310 it != el.end() && !Comparer::operator()(*it, value) && !Comparer::operator()(value, *it);
311 return std::make_pair(found, it);
312 }
313
314 T* Insert(std::vector<T>& el, T&& value) {
315 auto [found, it] = LowerBound(el, value);
316 if (found) {
317 return &*it;
318 }
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700319 return &*el.insert(it, std::forward<T>(value));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700320 }
321};
322
323struct PackageViewComparer {
324 bool operator()(const ResourceTablePackageView& lhs, const ResourceTablePackageView& rhs) {
325 return less_than_struct_with_name_and_id<ResourceTablePackageView, uint8_t>(
326 lhs, std::make_pair(rhs.name, rhs.id));
327 }
328};
329
330struct TypeViewComparer {
331 bool operator()(const ResourceTableTypeView& lhs, const ResourceTableTypeView& rhs) {
332 return lhs.id != rhs.id ? lhs.id < rhs.id : lhs.type < rhs.type;
333 }
334};
335
336struct EntryViewComparer {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700337 bool operator()(const ResourceTableEntryView& lhs, const ResourceTableEntryView& rhs) {
338 return less_than_struct_with_name_and_id<ResourceTableEntryView, uint16_t>(
339 lhs, std::make_pair(rhs.name, rhs.id));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700340 }
341};
342
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700343void InsertEntryIntoTableView(ResourceTableView& table, const ResourceTablePackage* package,
344 const ResourceTableType* type, const std::string& entry_name,
Ryan Mitchell4382e442021-07-14 12:53:01 -0700345 const std::optional<ResourceId>& id, const Visibility& visibility,
346 const std::optional<AllowNew>& allow_new,
347 const std::optional<OverlayableItem>& overlayable_item,
348 const std::optional<StagedId>& staged_id,
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700349 const std::vector<std::unique_ptr<ResourceConfigValue>>& values) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700350 SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter;
351 SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700352 SortedVectorInserter<ResourceTableEntryView, EntryViewComparer> entry_inserter;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700353
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700354 ResourceTablePackageView new_package{package->name,
Ryan Mitchell4382e442021-07-14 12:53:01 -0700355 id ? id.value().package_id() : std::optional<uint8_t>{}};
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700356 auto view_package = package_inserter.Insert(table.packages, std::move(new_package));
357
Ryan Mitchell4382e442021-07-14 12:53:01 -0700358 ResourceTableTypeView new_type{type->type, id ? id.value().type_id() : std::optional<uint8_t>{}};
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700359 auto view_type = type_inserter.Insert(view_package->types, std::move(new_type));
360
361 if (visibility.level == Visibility::Level::kPublic) {
362 // Only mark the type visibility level as public, it doesn't care about being private.
363 view_type->visibility_level = Visibility::Level::kPublic;
364 }
365
366 ResourceTableEntryView new_entry{.name = entry_name,
Ryan Mitchell4382e442021-07-14 12:53:01 -0700367 .id = id ? id.value().entry_id() : std::optional<uint16_t>{},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700368 .visibility = visibility,
369 .allow_new = allow_new,
370 .overlayable_item = overlayable_item,
371 .staged_id = staged_id};
372 for (auto& value : values) {
373 new_entry.values.emplace_back(value.get());
374 }
375
376 entry_inserter.Insert(view_type->entries, std::move(new_entry));
377}
378} // namespace
379
380const ResourceConfigValue* ResourceTableEntryView::FindValue(const ConfigDescription& config,
381 android::StringPiece product) const {
382 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
383 lt_config_key_ref<const ResourceConfigValue*>);
384 if (iter != values.end()) {
385 const ResourceConfigValue* value = *iter;
386 if (value->config == config && StringPiece(value->product) == product) {
387 return value;
388 }
389 }
390 return nullptr;
391}
392
393ResourceTableView ResourceTable::GetPartitionedView(const ResourceTableViewOptions& options) const {
394 ResourceTableView view;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700395 for (const auto& package : packages) {
396 for (const auto& type : package->types) {
397 for (const auto& entry : type->entries) {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700398 InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, entry->id,
399 entry->visibility, entry->allow_new, entry->overlayable_item,
400 entry->staged_id, entry->values);
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700401
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700402 if (options.create_alias_entries && entry->staged_id) {
403 auto alias_id = entry->staged_id.value().id;
404 InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, alias_id,
405 entry->visibility, entry->allow_new, entry->overlayable_item, {},
406 entry->values);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700407 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700408 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800409 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700410 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800411
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700412 // The android runtime does not support querying resources when the there are multiple type ids
413 // for the same resource type within the same package. For this reason, if there are types with
414 // multiple type ids, each type needs to exist in its own package in order to be queried by name.
415 std::vector<ResourceTablePackageView> new_packages;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700416 SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter;
417 SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700418 for (auto& package : view.packages) {
419 // If a new package was already created for a different type within this package, then
420 // we can reuse those packages for other types that need to be extracted from this package.
421 // `start_index` is the index of the first newly created package that can be reused.
422 const size_t start_index = new_packages.size();
423 std::map<ResourceType, size_t> type_new_package_index;
424 for (auto type_it = package.types.begin(); type_it != package.types.end();) {
425 auto& type = *type_it;
426 auto type_index_iter = type_new_package_index.find(type.type);
427 if (type_index_iter == type_new_package_index.end()) {
428 // First occurrence of the resource type in this package. Keep it in this package.
429 type_new_package_index.insert(type_index_iter, std::make_pair(type.type, start_index));
430 ++type_it;
431 continue;
432 }
433
434 // The resource type has already been seen for this package, so this type must be extracted to
435 // a new separate package.
436 const size_t index = type_index_iter->second;
437 if (new_packages.size() == index) {
438 new_packages.emplace_back(ResourceTablePackageView{package.name, package.id});
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700439 }
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));
Yurii Zubrytskyi76786b92021-10-07 14:33:55 -0700444 type_new_package_index[type.type] = index + 1;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700445 type_it = package.types.erase(type_it);
446 }
447 }
448
449 for (auto& new_package : new_packages) {
450 // Insert newly created packages after their original packages
451 auto [_, it] = package_inserter.LowerBound(view.packages, new_package);
452 view.packages.insert(++it, std::move(new_package));
453 }
454
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700455 return view;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800456}
457
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700458bool ResourceTable::AddResource(NewResource&& res, IDiagnostics* diag) {
459 CHECK(diag != nullptr) << "Diagnostic pointer is null";
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700460
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700461 const bool validate = validation_ == Validation::kEnabled;
462 const Source source = res.value ? res.value->GetSource() : Source{};
463 if (validate && !res.allow_mangled && !IsValidResourceEntryName(res.name.entry)) {
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700464 diag->Error(DiagMessage(source)
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700465 << "resource '" << res.name << "' has invalid entry name '" << res.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700466 return false;
467 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800468
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700469 if (res.id.has_value() && !res.id->first.is_valid()) {
470 diag->Error(DiagMessage(source) << "trying to add resource '" << res.name << "' with ID "
471 << res.id->first << " but that ID is invalid");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700472 return false;
473 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700474
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700475 auto package = FindOrCreatePackage(res.name.package);
476 auto type = package->FindOrCreateType(res.name.type);
477 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), res.name.entry,
478 NameEqualRange<ResourceEntry>{});
479 const size_t entry_count = std::distance(entry_it.first, entry_it.second);
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700480
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700481 ResourceEntry* entry;
482 if (entry_count == 0) {
483 // Adding a new resource
484 entry = type->CreateEntry(res.name.entry);
485 } else if (entry_count == 1) {
486 // Assume that the existing resource is being modified
487 entry = entry_it.first->get();
488 } else {
489 // Multiple resources with the same name exist in the resource table. The only way to
490 // distinguish between them is using resource id since each resource should have a unique id.
491 CHECK(res.id.has_value()) << "ambiguous modification of resource entry '" << res.name
492 << "' without specifying a resource id.";
493 entry = entry_it.first->get();
494 for (auto it = entry_it.first; it != entry_it.second; ++it) {
495 CHECK((bool)(*it)->id) << "ambiguous modification of resource entry '" << res.name
496 << "' with multiple entries without resource ids";
497 if ((*it)->id == res.id->first) {
498 entry = it->get();
499 break;
500 }
501 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700502 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800503
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700504 if (res.id.has_value()) {
505 if (entry->id && entry->id.value() != res.id->first) {
506 if (res.id->second != OnIdConflict::CREATE_ENTRY) {
507 diag->Error(DiagMessage(source)
508 << "trying to add resource '" << res.name << "' with ID " << res.id->first
509 << " but resource already has ID " << entry->id.value());
510 return false;
511 }
512 entry = type->CreateEntry(res.name.entry);
513 }
514 entry->id = res.id->first;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700515 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800516
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700517 if (res.visibility.has_value()) {
518 // Only mark the type visibility level as public, it doesn't care about being private.
519 if (res.visibility->level == Visibility::Level::kPublic) {
520 type->visibility_level = Visibility::Level::kPublic;
521 }
522
523 if (res.visibility->level > entry->visibility.level) {
524 // This symbol definition takes precedence, replace.
525 entry->visibility = res.visibility.value();
526 }
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700527
528 if (res.visibility->staged_api) {
529 entry->visibility.staged_api = entry->visibility.staged_api;
530 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700531 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800532
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700533 if (res.overlayable.has_value()) {
534 if (entry->overlayable_item) {
535 diag->Error(DiagMessage(res.overlayable->source)
536 << "duplicate overlayable declaration for resource '" << res.name << "'");
537 diag->Error(DiagMessage(entry->overlayable_item.value().source)
538 << "previous declaration here");
539 return false;
540 }
541 entry->overlayable_item = res.overlayable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700542 }
543
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700544 if (res.allow_new.has_value()) {
545 entry->allow_new = res.allow_new.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700546 }
547
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700548 if (res.staged_id.has_value()) {
549 entry->staged_id = res.staged_id.value();
550 }
551
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700552 if (res.value != nullptr) {
553 auto config_value = entry->FindOrCreateValue(res.config, res.product);
554 if (!config_value->value) {
555 // Resource does not exist, add it now.
556 config_value->value = std::move(res.value);
557 } else {
558 // When validation is enabled, ensure that a resource cannot have multiple values defined for
559 // the same configuration.
560 auto result = validate ? ResolveValueCollision(config_value->value.get(), res.value.get())
561 : CollisionResult::kKeepBoth;
562 switch (result) {
563 case CollisionResult::kKeepBoth:
564 // Insert the value ignoring for duplicate configurations
565 entry->values.push_back(util::make_unique<ResourceConfigValue>(res.config, res.product));
566 entry->values.back()->value = std::move(res.value);
567 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800568
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700569 case CollisionResult::kTakeNew:
570 // Take the incoming value.
571 config_value->value = std::move(res.value);
572 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800573
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700574 case CollisionResult::kConflict:
575 diag->Error(DiagMessage(source) << "duplicate value for resource '" << res.name << "' "
576 << "with config '" << res.config << "'");
577 diag->Error(DiagMessage(source) << "resource previously defined here");
578 return false;
Adam Lesinski71be7052017-12-12 16:48:07 -0800579
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700580 case CollisionResult::kKeepOriginal:
581 break;
582 }
583 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800584 }
585
Adam Lesinski71be7052017-12-12 16:48:07 -0800586 return true;
587}
588
Ryan Mitchell4382e442021-07-14 12:53:01 -0700589std::optional<ResourceTable::SearchResult> ResourceTable::FindResource(
590 const ResourceNameRef& name) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700591 ResourceTablePackage* package = FindPackage(name.package);
Adam Lesinski71be7052017-12-12 16:48:07 -0800592 if (package == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700593 return {};
594 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800595
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700596 ResourceTableType* type = package->FindType(name.type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800597 if (type == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700598 return {};
599 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800600
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700601 ResourceEntry* entry = type->FindEntry(name.entry);
Adam Lesinski71be7052017-12-12 16:48:07 -0800602 if (entry == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700603 return {};
604 }
605 return SearchResult{package, type, entry};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800606}
607
Ryan Mitchell4382e442021-07-14 12:53:01 -0700608std::optional<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name,
609 ResourceId id) const {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700610 ResourceTablePackage* package = FindPackage(name.package);
611 if (package == nullptr) {
612 return {};
613 }
614
615 ResourceTableType* type = package->FindType(name.type);
616 if (type == nullptr) {
617 return {};
618 }
619
620 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry,
621 NameEqualRange<ResourceEntry>{});
622 for (auto it = entry_it.first; it != entry_it.second; ++it) {
623 if ((*it)->id == id) {
624 return SearchResult{package, type, it->get()};
625 }
626 }
627 return {};
628}
629
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700630bool ResourceTable::RemoveResource(const ResourceNameRef& name, ResourceId id) const {
631 ResourceTablePackage* package = FindPackage(name.package);
632 if (package == nullptr) {
633 return {};
634 }
635
636 ResourceTableType* type = package->FindType(name.type);
637 if (type == nullptr) {
638 return {};
639 }
640
641 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry,
642 NameEqualRange<ResourceEntry>{});
643 for (auto it = entry_it.first; it != entry_it.second; ++it) {
644 if ((*it)->id == id) {
645 type->entries.erase(it);
646 return true;
647 }
648 }
649 return false;
650}
651
Shane Farmer0a5b2012017-06-22 12:24:12 -0700652std::unique_ptr<ResourceTable> ResourceTable::Clone() const {
653 std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>();
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700654 CloningValueTransformer cloner(&new_table->string_pool);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700655 for (const auto& pkg : packages) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700656 ResourceTablePackage* new_pkg = new_table->FindOrCreatePackage(pkg->name);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700657 for (const auto& type : pkg->types) {
658 ResourceTableType* new_type = new_pkg->FindOrCreateType(type->type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800659 new_type->visibility_level = type->visibility_level;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700660
661 for (const auto& entry : type->entries) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700662 ResourceEntry* new_entry = new_type->CreateEntry(entry->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800663 new_entry->id = entry->id;
664 new_entry->visibility = entry->visibility;
665 new_entry->allow_new = entry->allow_new;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800666 new_entry->overlayable_item = entry->overlayable_item;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700667
668 for (const auto& config_value : entry->values) {
669 ResourceConfigValue* new_value =
670 new_entry->FindOrCreateValue(config_value->config, config_value->product);
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700671 new_value->value = config_value->value->Transform(cloner);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700672 }
673 }
674 }
675 }
676 return new_table;
677}
678
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700679NewResourceBuilder::NewResourceBuilder(const ResourceNameRef& name) {
680 res_.name = name.ToResourceName();
681}
682
683NewResourceBuilder::NewResourceBuilder(const std::string& name) {
684 ResourceNameRef ref;
685 CHECK(ResourceUtils::ParseResourceName(name, &ref)) << "invalid resource name: " << name;
686 res_.name = ref.ToResourceName();
687}
688
689NewResourceBuilder& NewResourceBuilder::SetValue(std::unique_ptr<Value> value,
690 android::ConfigDescription config,
691 std::string product) {
692 res_.value = std::move(value);
693 res_.config = std::move(config);
694 res_.product = std::move(product);
695 return *this;
696}
697
698NewResourceBuilder& NewResourceBuilder::SetId(ResourceId id, OnIdConflict on_conflict) {
699 res_.id = std::make_pair(id, on_conflict);
700 return *this;
701}
702
703NewResourceBuilder& NewResourceBuilder::SetVisibility(Visibility visibility) {
704 res_.visibility = std::move(visibility);
705 return *this;
706}
707
708NewResourceBuilder& NewResourceBuilder::SetOverlayable(OverlayableItem overlayable) {
709 res_.overlayable = std::move(overlayable);
710 return *this;
711}
712NewResourceBuilder& NewResourceBuilder::SetAllowNew(AllowNew allow_new) {
713 res_.allow_new = std::move(allow_new);
714 return *this;
715}
716
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700717NewResourceBuilder& NewResourceBuilder::SetStagedId(StagedId staged_alias) {
718 res_.staged_id = std::move(staged_alias);
719 return *this;
720}
721
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700722NewResourceBuilder& NewResourceBuilder::SetAllowMangled(bool allow_mangled) {
723 res_.allow_mangled = allow_mangled;
724 return *this;
725}
726
727NewResource NewResourceBuilder::Build() {
728 return std::move(res_);
729}
730
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700731} // namespace aapt