blob: 0f5118da9408ce04ba7c01db7bfa76538be43554 [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 {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +000046bool less_than_type(const std::unique_ptr<ResourceTableType>& lhs,
47 const ResourceNamedTypeRef& rhs) {
48 return lhs->named_type < rhs;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049}
50
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051template <typename T>
Ryan Mitchell9634efb2021-03-19 14:53:17 -070052bool less_than_struct_with_name(const std::unique_ptr<T>& lhs, const StringPiece& rhs) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 return lhs->name.compare(0, lhs->name.size(), rhs.data(), rhs.size()) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054}
55
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000056template <typename T>
Ryan Mitchell9634efb2021-03-19 14:53:17 -070057bool greater_than_struct_with_name(const StringPiece& lhs, const std::unique_ptr<T>& rhs) {
58 return rhs->name.compare(0, rhs->name.size(), lhs.data(), lhs.size()) > 0;
David Chaloupkae3c1a4a2018-01-18 13:44:36 +000059}
60
Ryan Mitchell9634efb2021-03-19 14:53:17 -070061template <typename T>
62struct NameEqualRange {
63 bool operator()(const std::unique_ptr<T>& lhs, const StringPiece& rhs) const {
64 return less_than_struct_with_name<T>(lhs, rhs);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -070066 bool operator()(const StringPiece& lhs, const std::unique_ptr<T>& rhs) const {
67 return greater_than_struct_with_name<T>(lhs, rhs);
68 }
69};
70
71template <typename T, typename U>
72bool less_than_struct_with_name_and_id(const T& lhs,
Ryan Mitchell4382e442021-07-14 12:53:01 -070073 const std::pair<std::string_view, std::optional<U>>& rhs) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -070074 if (lhs.id != rhs.second) {
75 return lhs.id < rhs.second;
76 }
77 return lhs.name.compare(0, lhs.name.size(), rhs.first.data(), rhs.first.size()) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078}
79
Ryan Mitchell9634efb2021-03-19 14:53:17 -070080template <typename T, typename Func, typename Elements>
81T* FindElementsRunAction(const android::StringPiece& name, Elements& entries, Func action) {
82 const auto iter =
83 std::lower_bound(entries.begin(), entries.end(), name, less_than_struct_with_name<T>);
84 const bool found = iter != entries.end() && name == (*iter)->name;
85 return action(found, iter);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086}
87
Ryan Mitchell2fedba92021-04-23 07:47:38 -070088struct ConfigKey {
89 const ConfigDescription* config;
90 const StringPiece& product;
91};
92
93template <typename T>
94bool lt_config_key_ref(const T& lhs, const ConfigKey& rhs) {
95 int cmp = lhs->config.compare(*rhs.config);
96 if (cmp == 0) {
97 cmp = StringPiece(lhs->product).compare(rhs.product);
98 }
99 return cmp < 0;
100}
101
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700102} // namespace
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000103
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700104ResourceTable::ResourceTable(ResourceTable::Validation validation) : validation_(validation) {
David Chaloupkae3c1a4a2018-01-18 13:44:36 +0000105}
106
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700107ResourceTablePackage* ResourceTable::FindPackage(const android::StringPiece& name) const {
108 return FindElementsRunAction<ResourceTablePackage>(
109 name, packages, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700110}
111
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700112ResourceTablePackage* ResourceTable::FindOrCreatePackage(const android::StringPiece& name) {
113 return FindElementsRunAction<ResourceTablePackage>(name, packages, [&](bool found, auto& iter) {
114 return found ? iter->get() : packages.emplace(iter, new ResourceTablePackage(name))->get();
115 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116}
117
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700118template <typename Func, typename Elements>
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000119static ResourceTableType* FindTypeRunAction(const ResourceNamedTypeRef& type, Elements& entries,
120 Func action) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700121 const auto iter = std::lower_bound(entries.begin(), entries.end(), type, less_than_type);
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000122 const bool found = iter != entries.end() && type == (*iter)->named_type;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700123 return action(found, iter);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124}
125
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000126ResourceTableType* ResourceTablePackage::FindTypeWithDefaultName(const ResourceType type) const {
127 auto named_type = ResourceNamedTypeWithDefaultName(type);
128 return FindType(named_type);
129}
130
131ResourceTableType* ResourceTablePackage::FindType(const ResourceNamedTypeRef& type) const {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700132 return FindTypeRunAction(type, types,
133 [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134}
135
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000136ResourceTableType* ResourceTablePackage::FindOrCreateType(const ResourceNamedTypeRef& type) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700137 return FindTypeRunAction(type, types, [&](bool found, auto& iter) {
138 return found ? iter->get() : types.emplace(iter, new ResourceTableType(type))->get();
139 });
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700140}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700142ResourceEntry* ResourceTableType::CreateEntry(const android::StringPiece& name) {
143 return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
144 return entries.emplace(iter, new ResourceEntry(name))->get();
145 });
146}
147
148ResourceEntry* ResourceTableType::FindEntry(const android::StringPiece& name) const {
149 return FindElementsRunAction<ResourceEntry>(
150 name, entries, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; });
151}
152
153ResourceEntry* ResourceTableType::FindOrCreateEntry(const android::StringPiece& name) {
154 return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) {
155 return found ? iter->get() : entries.emplace(iter, new ResourceEntry(name))->get();
156 });
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800157}
158
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config,
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700160 android::StringPiece product) {
161 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700162 lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700163 if (iter != values.end()) {
164 ResourceConfigValue* value = iter->get();
165 if (value->config == config && StringPiece(value->product) == product) {
166 return value;
167 }
168 }
169 return nullptr;
170}
171
172const ResourceConfigValue* ResourceEntry::FindValue(const android::ConfigDescription& config,
173 android::StringPiece product) const {
Adam Lesinski34a16872018-02-23 16:18:10 -0800174 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700175 lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700176 if (iter != values.end()) {
177 ResourceConfigValue* value = iter->get();
178 if (value->config == config && StringPiece(value->product) == product) {
179 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800180 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 }
182 return nullptr;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800183}
184
Adam Lesinskib1afa072017-03-29 13:52:38 -0700185ResourceConfigValue* ResourceEntry::FindOrCreateValue(const ConfigDescription& config,
186 const StringPiece& product) {
Adam Lesinski34a16872018-02-23 16:18:10 -0800187 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700188 lt_config_key_ref<std::unique_ptr<ResourceConfigValue>>);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189 if (iter != values.end()) {
190 ResourceConfigValue* value = iter->get();
191 if (value->config == config && StringPiece(value->product) == product) {
192 return value;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800193 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 }
195 ResourceConfigValue* newValue =
Adam Lesinskib1afa072017-03-29 13:52:38 -0700196 values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))->get();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 return newValue;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800198}
199
Adam Lesinskib1afa072017-03-29 13:52:38 -0700200std::vector<ResourceConfigValue*> ResourceEntry::FindAllValues(const ConfigDescription& config) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700201 std::vector<ResourceConfigValue*> results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800202
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 auto iter = values.begin();
204 for (; iter != values.end(); ++iter) {
205 ResourceConfigValue* value = iter->get();
206 if (value->config == config) {
207 results.push_back(value);
208 ++iter;
209 break;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800210 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 }
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800212
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700213 for (; iter != values.end(); ++iter) {
214 ResourceConfigValue* value = iter->get();
215 if (value->config == config) {
216 results.push_back(value);
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800217 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700218 }
219 return results;
Adam Lesinskie4bb9eb2016-02-12 22:18:51 -0800220}
221
Adam Lesinski34a16872018-02-23 16:18:10 -0800222bool ResourceEntry::HasDefaultValue() const {
223 const ConfigDescription& default_config = ConfigDescription::DefaultConfig();
224
225 // The default config should be at the top of the list, since the list is sorted.
226 for (auto& config_value : values) {
227 if (config_value->config == default_config) {
228 return true;
Adam Lesinski458b8772016-04-25 14:20:21 -0700229 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700230 }
Adam Lesinski34a16872018-02-23 16:18:10 -0800231 return false;
Adam Lesinski458b8772016-04-25 14:20:21 -0700232}
233
Adam Lesinski71be7052017-12-12 16:48:07 -0800234// The default handler for collisions.
235//
236// Typically, a weak value will be overridden by a strong value. An existing weak
237// value will not be overridden by an incoming weak value.
238//
239// There are some exceptions:
240//
241// Attributes: There are two types of Attribute values: USE and DECL.
242//
243// USE is anywhere an Attribute is declared without a format, and in a place that would
244// be legal to declare if the Attribute already existed. This is typically in a
245// <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak.
246//
247// DECL is an absolute declaration of an Attribute and specifies an explicit format.
248//
249// A DECL will override a USE without error. Two DECLs must match in their format for there to be
250// no error.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700251ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* existing,
252 Value* incoming) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 Attribute* existing_attr = ValueCast<Attribute>(existing);
254 Attribute* incoming_attr = ValueCast<Attribute>(incoming);
255 if (!incoming_attr) {
256 if (incoming->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 // We're trying to add a weak resource but a resource
258 // already exists. Keep the existing.
259 return CollisionResult::kKeepOriginal;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700260 } else if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700261 // Override the weak resource with the new strong resource.
262 return CollisionResult::kTakeNew;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800263 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 // The existing and incoming values are strong, this is an error
265 // if the values are not both attributes.
Adam Lesinski5c3464c2016-08-24 16:03:48 -0700266 return CollisionResult::kConflict;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 }
268
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700269 if (!existing_attr) {
270 if (existing->IsWeak()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 // The existing value is not an attribute and it is weak,
272 // so take the incoming attribute value.
273 return CollisionResult::kTakeNew;
274 }
275 // The existing value is not an attribute and it is strong,
276 // so the incoming attribute value is an error.
277 return CollisionResult::kConflict;
278 }
279
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700280 CHECK(incoming_attr != nullptr && existing_attr != nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281
282 //
283 // Attribute specific handling. At this point we know both
284 // values are attributes. Since we can declare and define
285 // attributes all-over, we do special handling to see
286 // which definition sticks.
287 //
Adam Lesinski73bff1e2017-12-08 16:06:10 -0800288 if (existing_attr->IsCompatibleWith(*incoming_attr)) {
289 // The two attributes are both DECLs, but they are plain attributes with compatible formats.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 // Keep the strongest one.
Adam Lesinskib1afa072017-03-29 13:52:38 -0700291 return existing_attr->IsWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 }
293
Adam Lesinskib1afa072017-03-29 13:52:38 -0700294 if (existing_attr->IsWeak() && existing_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295 // Any incoming attribute is better than this.
296 return CollisionResult::kTakeNew;
297 }
298
Adam Lesinskib1afa072017-03-29 13:52:38 -0700299 if (incoming_attr->IsWeak() && incoming_attr->type_mask == android::ResTable_map::TYPE_ANY) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 // The incoming attribute may be a USE instead of a DECL.
301 // Keep the existing attribute.
302 return CollisionResult::kKeepOriginal;
303 }
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700304
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700305 return CollisionResult::kConflict;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800306}
307
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700308namespace {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700309template <typename T, typename Comparer>
310struct SortedVectorInserter : public Comparer {
311 std::pair<bool, typename std::vector<T>::iterator> LowerBound(std::vector<T>& el,
312 const T& value) {
313 auto it = std::lower_bound(el.begin(), el.end(), value, [&](auto& lhs, auto& rhs) {
314 return Comparer::operator()(lhs, rhs);
315 });
316 bool found =
317 it != el.end() && !Comparer::operator()(*it, value) && !Comparer::operator()(value, *it);
318 return std::make_pair(found, it);
319 }
320
321 T* Insert(std::vector<T>& el, T&& value) {
322 auto [found, it] = LowerBound(el, value);
323 if (found) {
324 return &*it;
325 }
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700326 return &*el.insert(it, std::forward<T>(value));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700327 }
328};
329
330struct PackageViewComparer {
331 bool operator()(const ResourceTablePackageView& lhs, const ResourceTablePackageView& rhs) {
332 return less_than_struct_with_name_and_id<ResourceTablePackageView, uint8_t>(
333 lhs, std::make_pair(rhs.name, rhs.id));
334 }
335};
336
337struct TypeViewComparer {
338 bool operator()(const ResourceTableTypeView& lhs, const ResourceTableTypeView& rhs) {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000339 return lhs.id != rhs.id ? lhs.id < rhs.id : lhs.named_type < rhs.named_type;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700340 }
341};
342
343struct EntryViewComparer {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700344 bool operator()(const ResourceTableEntryView& lhs, const ResourceTableEntryView& rhs) {
345 return less_than_struct_with_name_and_id<ResourceTableEntryView, uint16_t>(
346 lhs, std::make_pair(rhs.name, rhs.id));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700347 }
348};
349
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700350void InsertEntryIntoTableView(ResourceTableView& table, const ResourceTablePackage* package,
351 const ResourceTableType* type, const std::string& entry_name,
Ryan Mitchell4382e442021-07-14 12:53:01 -0700352 const std::optional<ResourceId>& id, const Visibility& visibility,
353 const std::optional<AllowNew>& allow_new,
354 const std::optional<OverlayableItem>& overlayable_item,
355 const std::optional<StagedId>& staged_id,
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700356 const std::vector<std::unique_ptr<ResourceConfigValue>>& values) {
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700357 SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter;
358 SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700359 SortedVectorInserter<ResourceTableEntryView, EntryViewComparer> entry_inserter;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700360
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700361 ResourceTablePackageView new_package{package->name,
Ryan Mitchell4382e442021-07-14 12:53:01 -0700362 id ? id.value().package_id() : std::optional<uint8_t>{}};
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700363 auto view_package = package_inserter.Insert(table.packages, std::move(new_package));
364
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000365 ResourceTableTypeView new_type{type->named_type,
366 id ? id.value().type_id() : std::optional<uint8_t>{}};
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700367 auto view_type = type_inserter.Insert(view_package->types, std::move(new_type));
368
369 if (visibility.level == Visibility::Level::kPublic) {
370 // Only mark the type visibility level as public, it doesn't care about being private.
371 view_type->visibility_level = Visibility::Level::kPublic;
372 }
373
374 ResourceTableEntryView new_entry{.name = entry_name,
Ryan Mitchell4382e442021-07-14 12:53:01 -0700375 .id = id ? id.value().entry_id() : std::optional<uint16_t>{},
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700376 .visibility = visibility,
377 .allow_new = allow_new,
378 .overlayable_item = overlayable_item,
379 .staged_id = staged_id};
380 for (auto& value : values) {
381 new_entry.values.emplace_back(value.get());
382 }
383
384 entry_inserter.Insert(view_type->entries, std::move(new_entry));
385}
386} // namespace
387
388const ResourceConfigValue* ResourceTableEntryView::FindValue(const ConfigDescription& config,
389 android::StringPiece product) const {
390 auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product},
391 lt_config_key_ref<const ResourceConfigValue*>);
392 if (iter != values.end()) {
393 const ResourceConfigValue* value = *iter;
394 if (value->config == config && StringPiece(value->product) == product) {
395 return value;
396 }
397 }
398 return nullptr;
399}
400
401ResourceTableView ResourceTable::GetPartitionedView(const ResourceTableViewOptions& options) const {
402 ResourceTableView view;
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700403 for (const auto& package : packages) {
404 for (const auto& type : package->types) {
405 for (const auto& entry : type->entries) {
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700406 InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, entry->id,
407 entry->visibility, entry->allow_new, entry->overlayable_item,
408 entry->staged_id, entry->values);
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700409
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700410 if (options.create_alias_entries && entry->staged_id) {
411 auto alias_id = entry->staged_id.value().id;
412 InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, alias_id,
413 entry->visibility, entry->allow_new, entry->overlayable_item, {},
414 entry->values);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700415 }
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700416 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800417 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700418 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800419
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700420 // The android runtime does not support querying resources when the there are multiple type ids
421 // for the same resource type within the same package. For this reason, if there are types with
422 // multiple type ids, each type needs to exist in its own package in order to be queried by name.
423 std::vector<ResourceTablePackageView> new_packages;
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700424 SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter;
425 SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700426 for (auto& package : view.packages) {
427 // If a new package was already created for a different type within this package, then
428 // we can reuse those packages for other types that need to be extracted from this package.
429 // `start_index` is the index of the first newly created package that can be reused.
430 const size_t start_index = new_packages.size();
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000431 std::map<ResourceNamedType, size_t> type_new_package_index;
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700432 for (auto type_it = package.types.begin(); type_it != package.types.end();) {
433 auto& type = *type_it;
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000434 auto type_index_iter = type_new_package_index.find(type.named_type);
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700435 if (type_index_iter == type_new_package_index.end()) {
436 // First occurrence of the resource type in this package. Keep it in this package.
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000437 type_new_package_index.insert(type_index_iter,
438 std::make_pair(type.named_type, start_index));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700439 ++type_it;
440 continue;
441 }
442
443 // The resource type has already been seen for this package, so this type must be extracted to
444 // a new separate package.
445 const size_t index = type_index_iter->second;
446 if (new_packages.size() == index) {
447 new_packages.emplace_back(ResourceTablePackageView{package.name, package.id});
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700448 }
449
450 // Move the type into a new package
451 auto& other_package = new_packages[index];
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000452 type_new_package_index[type.named_type] = index + 1;
Greg Kaiserb40f3ab2021-10-18 06:37:26 -0700453 type_inserter.Insert(other_package.types, std::move(type));
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700454 type_it = package.types.erase(type_it);
455 }
456 }
457
458 for (auto& new_package : new_packages) {
459 // Insert newly created packages after their original packages
460 auto [_, it] = package_inserter.LowerBound(view.packages, new_package);
461 view.packages.insert(++it, std::move(new_package));
462 }
463
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700464 return view;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800465}
466
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700467bool ResourceTable::AddResource(NewResource&& res, IDiagnostics* diag) {
468 CHECK(diag != nullptr) << "Diagnostic pointer is null";
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700469
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700470 const bool validate = validation_ == Validation::kEnabled;
471 const Source source = res.value ? res.value->GetSource() : Source{};
472 if (validate && !res.allow_mangled && !IsValidResourceEntryName(res.name.entry)) {
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700473 diag->Error(DiagMessage(source)
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700474 << "resource '" << res.name << "' has invalid entry name '" << res.name.entry);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700475 return false;
476 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800477
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700478 if (res.id.has_value() && !res.id->first.is_valid()) {
479 diag->Error(DiagMessage(source) << "trying to add resource '" << res.name << "' with ID "
480 << res.id->first << " but that ID is invalid");
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700481 return false;
482 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700483
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700484 auto package = FindOrCreatePackage(res.name.package);
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000485 auto type = package->FindOrCreateType(res.name.type);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700486 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), res.name.entry,
487 NameEqualRange<ResourceEntry>{});
488 const size_t entry_count = std::distance(entry_it.first, entry_it.second);
Ryan Mitchell83a37ad2018-08-06 16:32:24 -0700489
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700490 ResourceEntry* entry;
491 if (entry_count == 0) {
492 // Adding a new resource
493 entry = type->CreateEntry(res.name.entry);
494 } else if (entry_count == 1) {
495 // Assume that the existing resource is being modified
496 entry = entry_it.first->get();
497 } else {
498 // Multiple resources with the same name exist in the resource table. The only way to
499 // distinguish between them is using resource id since each resource should have a unique id.
500 CHECK(res.id.has_value()) << "ambiguous modification of resource entry '" << res.name
501 << "' without specifying a resource id.";
502 entry = entry_it.first->get();
503 for (auto it = entry_it.first; it != entry_it.second; ++it) {
504 CHECK((bool)(*it)->id) << "ambiguous modification of resource entry '" << res.name
505 << "' with multiple entries without resource ids";
506 if ((*it)->id == res.id->first) {
507 entry = it->get();
508 break;
509 }
510 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700511 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800512
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700513 if (res.id.has_value()) {
514 if (entry->id && entry->id.value() != res.id->first) {
515 if (res.id->second != OnIdConflict::CREATE_ENTRY) {
516 diag->Error(DiagMessage(source)
517 << "trying to add resource '" << res.name << "' with ID " << res.id->first
518 << " but resource already has ID " << entry->id.value());
519 return false;
520 }
521 entry = type->CreateEntry(res.name.entry);
522 }
523 entry->id = res.id->first;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700524 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800525
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700526 if (res.visibility.has_value()) {
527 // Only mark the type visibility level as public, it doesn't care about being private.
528 if (res.visibility->level == Visibility::Level::kPublic) {
529 type->visibility_level = Visibility::Level::kPublic;
530 }
531
532 if (res.visibility->level > entry->visibility.level) {
533 // This symbol definition takes precedence, replace.
534 entry->visibility = res.visibility.value();
535 }
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700536
537 if (res.visibility->staged_api) {
538 entry->visibility.staged_api = entry->visibility.staged_api;
539 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700540 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800541
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700542 if (res.overlayable.has_value()) {
543 if (entry->overlayable_item) {
544 diag->Error(DiagMessage(res.overlayable->source)
545 << "duplicate overlayable declaration for resource '" << res.name << "'");
546 diag->Error(DiagMessage(entry->overlayable_item.value().source)
547 << "previous declaration here");
548 return false;
549 }
550 entry->overlayable_item = res.overlayable.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700551 }
552
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700553 if (res.allow_new.has_value()) {
554 entry->allow_new = res.allow_new.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700555 }
556
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700557 if (res.staged_id.has_value()) {
558 entry->staged_id = res.staged_id.value();
559 }
560
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700561 if (res.value != nullptr) {
562 auto config_value = entry->FindOrCreateValue(res.config, res.product);
563 if (!config_value->value) {
564 // Resource does not exist, add it now.
565 config_value->value = std::move(res.value);
566 } else {
567 // When validation is enabled, ensure that a resource cannot have multiple values defined for
568 // the same configuration.
569 auto result = validate ? ResolveValueCollision(config_value->value.get(), res.value.get())
570 : CollisionResult::kKeepBoth;
571 switch (result) {
572 case CollisionResult::kKeepBoth:
573 // Insert the value ignoring for duplicate configurations
574 entry->values.push_back(util::make_unique<ResourceConfigValue>(res.config, res.product));
575 entry->values.back()->value = std::move(res.value);
576 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800577
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700578 case CollisionResult::kTakeNew:
579 // Take the incoming value.
580 config_value->value = std::move(res.value);
581 break;
Adam Lesinski71be7052017-12-12 16:48:07 -0800582
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700583 case CollisionResult::kConflict:
584 diag->Error(DiagMessage(source) << "duplicate value for resource '" << res.name << "' "
585 << "with config '" << res.config << "'");
586 diag->Error(DiagMessage(source) << "resource previously defined here");
587 return false;
Adam Lesinski71be7052017-12-12 16:48:07 -0800588
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700589 case CollisionResult::kKeepOriginal:
590 break;
591 }
592 }
Adam Lesinski71be7052017-12-12 16:48:07 -0800593 }
594
Adam Lesinski71be7052017-12-12 16:48:07 -0800595 return true;
596}
597
Ryan Mitchell4382e442021-07-14 12:53:01 -0700598std::optional<ResourceTable::SearchResult> ResourceTable::FindResource(
599 const ResourceNameRef& name) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700600 ResourceTablePackage* package = FindPackage(name.package);
Adam Lesinski71be7052017-12-12 16:48:07 -0800601 if (package == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700602 return {};
603 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800604
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000605 ResourceTableType* type = package->FindType(name.type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800606 if (type == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700607 return {};
608 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800609
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700610 ResourceEntry* entry = type->FindEntry(name.entry);
Adam Lesinski71be7052017-12-12 16:48:07 -0800611 if (entry == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700612 return {};
613 }
614 return SearchResult{package, type, entry};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800615}
616
Ryan Mitchell4382e442021-07-14 12:53:01 -0700617std::optional<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name,
618 ResourceId id) const {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700619 ResourceTablePackage* package = FindPackage(name.package);
620 if (package == nullptr) {
621 return {};
622 }
623
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000624 ResourceTableType* type = package->FindType(name.type);
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700625 if (type == nullptr) {
626 return {};
627 }
628
629 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry,
630 NameEqualRange<ResourceEntry>{});
631 for (auto it = entry_it.first; it != entry_it.second; ++it) {
632 if ((*it)->id == id) {
633 return SearchResult{package, type, it->get()};
634 }
635 }
636 return {};
637}
638
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700639bool ResourceTable::RemoveResource(const ResourceNameRef& name, ResourceId id) const {
640 ResourceTablePackage* package = FindPackage(name.package);
641 if (package == nullptr) {
642 return {};
643 }
644
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000645 ResourceTableType* type = package->FindType(name.type);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700646 if (type == nullptr) {
647 return {};
648 }
649
650 auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry,
651 NameEqualRange<ResourceEntry>{});
652 for (auto it = entry_it.first; it != entry_it.second; ++it) {
653 if ((*it)->id == id) {
654 type->entries.erase(it);
655 return true;
656 }
657 }
658 return false;
659}
660
Shane Farmer0a5b2012017-06-22 12:24:12 -0700661std::unique_ptr<ResourceTable> ResourceTable::Clone() const {
662 std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>();
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700663 CloningValueTransformer cloner(&new_table->string_pool);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700664 for (const auto& pkg : packages) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700665 ResourceTablePackage* new_pkg = new_table->FindOrCreatePackage(pkg->name);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700666 for (const auto& type : pkg->types) {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +0000667 ResourceTableType* new_type = new_pkg->FindOrCreateType(type->named_type);
Adam Lesinski71be7052017-12-12 16:48:07 -0800668 new_type->visibility_level = type->visibility_level;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700669
670 for (const auto& entry : type->entries) {
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700671 ResourceEntry* new_entry = new_type->CreateEntry(entry->name);
Adam Lesinski71be7052017-12-12 16:48:07 -0800672 new_entry->id = entry->id;
673 new_entry->visibility = entry->visibility;
674 new_entry->allow_new = entry->allow_new;
Ryan Mitchell54237ff2018-12-13 15:44:29 -0800675 new_entry->overlayable_item = entry->overlayable_item;
Shane Farmer0a5b2012017-06-22 12:24:12 -0700676
677 for (const auto& config_value : entry->values) {
678 ResourceConfigValue* new_value =
679 new_entry->FindOrCreateValue(config_value->config, config_value->product);
Ryan Mitchellefcdb952021-04-14 17:31:37 -0700680 new_value->value = config_value->value->Transform(cloner);
Shane Farmer0a5b2012017-06-22 12:24:12 -0700681 }
682 }
683 }
684 }
685 return new_table;
686}
687
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700688NewResourceBuilder::NewResourceBuilder(const ResourceNameRef& name) {
689 res_.name = name.ToResourceName();
690}
691
692NewResourceBuilder::NewResourceBuilder(const std::string& name) {
693 ResourceNameRef ref;
694 CHECK(ResourceUtils::ParseResourceName(name, &ref)) << "invalid resource name: " << name;
695 res_.name = ref.ToResourceName();
696}
697
698NewResourceBuilder& NewResourceBuilder::SetValue(std::unique_ptr<Value> value,
699 android::ConfigDescription config,
700 std::string product) {
701 res_.value = std::move(value);
702 res_.config = std::move(config);
703 res_.product = std::move(product);
704 return *this;
705}
706
707NewResourceBuilder& NewResourceBuilder::SetId(ResourceId id, OnIdConflict on_conflict) {
708 res_.id = std::make_pair(id, on_conflict);
709 return *this;
710}
711
712NewResourceBuilder& NewResourceBuilder::SetVisibility(Visibility visibility) {
713 res_.visibility = std::move(visibility);
714 return *this;
715}
716
717NewResourceBuilder& NewResourceBuilder::SetOverlayable(OverlayableItem overlayable) {
718 res_.overlayable = std::move(overlayable);
719 return *this;
720}
721NewResourceBuilder& NewResourceBuilder::SetAllowNew(AllowNew allow_new) {
722 res_.allow_new = std::move(allow_new);
723 return *this;
724}
725
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700726NewResourceBuilder& NewResourceBuilder::SetStagedId(StagedId staged_alias) {
727 res_.staged_id = std::move(staged_alias);
728 return *this;
729}
730
Ryan Mitchell9634efb2021-03-19 14:53:17 -0700731NewResourceBuilder& NewResourceBuilder::SetAllowMangled(bool allow_mangled) {
732 res_.allow_mangled = allow_mangled;
733 return *this;
734}
735
736NewResource NewResourceBuilder::Build() {
737 return std::move(res_);
738}
739
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700740} // namespace aapt