Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 17 | #include "ResourceTable.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 18 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include <memory> |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 21 | #include <optional> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include <tuple> |
| 23 | |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 24 | #include "android-base/logging.h" |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 25 | #include "androidfw/ConfigDescription.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 26 | #include "androidfw/ResourceTypes.h" |
| 27 | |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 28 | #include "NameMangler.h" |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 29 | #include "ResourceUtils.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 30 | #include "ResourceValues.h" |
| 31 | #include "ValueVisitor.h" |
| 32 | #include "text/Unicode.h" |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 33 | #include "trace/TraceBuffer.h" |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 34 | #include "util/Util.h" |
| 35 | |
| 36 | using ::aapt::text::IsValidResourceEntryName; |
Mårten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 37 | using ::android::ConfigDescription; |
Adam Lesinski | 66ea840 | 2017-06-28 11:44:11 -0700 | [diff] [blame] | 38 | using ::android::StringPiece; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 39 | using ::android::base::StringPrintf; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 41 | namespace aapt { |
| 42 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 43 | const char* Overlayable::kActorScheme = "overlay"; |
| 44 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 45 | namespace { |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 46 | bool less_than_type(const std::unique_ptr<ResourceTableType>& lhs, |
| 47 | const ResourceNamedTypeRef& rhs) { |
| 48 | return lhs->named_type < rhs; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | template <typename T> |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 52 | bool less_than_struct_with_name(const std::unique_ptr<T>& lhs, StringPiece rhs) { |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 53 | return lhs->name < rhs; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | } |
| 55 | |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 56 | template <typename T> |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 57 | bool greater_than_struct_with_name(StringPiece lhs, const std::unique_ptr<T>& rhs) { |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 58 | return rhs->name > lhs; |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 61 | template <typename T> |
| 62 | struct NameEqualRange { |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 63 | bool operator()(const std::unique_ptr<T>& lhs, StringPiece rhs) const { |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 64 | return less_than_struct_with_name(lhs, rhs); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | } |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 66 | bool operator()(StringPiece lhs, const std::unique_ptr<T>& rhs) const { |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 67 | return greater_than_struct_with_name(lhs, rhs); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | |
| 71 | template <typename T, typename U> |
| 72 | bool less_than_struct_with_name_and_id(const T& lhs, |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 73 | const std::pair<std::string_view, std::optional<U>>& rhs) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 74 | if (lhs.id != rhs.second) { |
| 75 | return lhs.id < rhs.second; |
| 76 | } |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 77 | return lhs.name < rhs.first; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 80 | template <typename T, typename Func, typename Elements> |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 81 | T* FindElementsRunAction(android::StringPiece name, Elements& entries, Func action) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 88 | struct ConfigKey { |
| 89 | const ConfigDescription* config; |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 90 | StringPiece product; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 93 | struct lt_config_key_ref { |
| 94 | template <typename T> |
| 95 | bool operator()(const T& lhs, const ConfigKey& rhs) const noexcept { |
| 96 | int cmp = lhs->config.compare(*rhs.config); |
| 97 | if (cmp == 0) { |
| 98 | cmp = lhs->product.compare(rhs.product); |
| 99 | } |
| 100 | return cmp < 0; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 101 | } |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 102 | }; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 103 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 104 | } // namespace |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 105 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 106 | ResourceTable::ResourceTable(ResourceTable::Validation validation) : validation_(validation) { |
David Chaloupka | e3c1a4a | 2018-01-18 13:44:36 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 109 | ResourceTablePackage* ResourceTable::FindPackage(android::StringPiece name) const { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 110 | return FindElementsRunAction<ResourceTablePackage>( |
| 111 | name, packages, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 114 | ResourceTablePackage* ResourceTable::FindOrCreatePackage(android::StringPiece name) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 115 | return FindElementsRunAction<ResourceTablePackage>(name, packages, [&](bool found, auto& iter) { |
| 116 | return found ? iter->get() : packages.emplace(iter, new ResourceTablePackage(name))->get(); |
| 117 | }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 120 | template <typename Func, typename Elements> |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 121 | static ResourceTableType* FindTypeRunAction(const ResourceNamedTypeRef& type, Elements& entries, |
| 122 | Func action) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 123 | const auto iter = std::lower_bound(entries.begin(), entries.end(), type, less_than_type); |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 124 | const bool found = iter != entries.end() && type == (*iter)->named_type; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 125 | return action(found, iter); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 128 | ResourceTableType* ResourceTablePackage::FindTypeWithDefaultName(const ResourceType type) const { |
| 129 | auto named_type = ResourceNamedTypeWithDefaultName(type); |
| 130 | return FindType(named_type); |
| 131 | } |
| 132 | |
| 133 | ResourceTableType* ResourceTablePackage::FindType(const ResourceNamedTypeRef& type) const { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 134 | return FindTypeRunAction(type, types, |
| 135 | [&](bool found, auto& iter) { return found ? iter->get() : nullptr; }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 138 | ResourceTableType* ResourceTablePackage::FindOrCreateType(const ResourceNamedTypeRef& type) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 139 | return FindTypeRunAction(type, types, [&](bool found, auto& iter) { |
| 140 | return found ? iter->get() : types.emplace(iter, new ResourceTableType(type))->get(); |
| 141 | }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 142 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 143 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 144 | ResourceEntry* ResourceTableType::CreateEntry(android::StringPiece name) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 145 | return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) { |
| 146 | return entries.emplace(iter, new ResourceEntry(name))->get(); |
| 147 | }); |
| 148 | } |
| 149 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 150 | ResourceEntry* ResourceTableType::FindEntry(android::StringPiece name) const { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 151 | return FindElementsRunAction<ResourceEntry>( |
| 152 | name, entries, [&](bool found, auto& iter) { return found ? iter->get() : nullptr; }); |
| 153 | } |
| 154 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 155 | ResourceEntry* ResourceTableType::FindOrCreateEntry(android::StringPiece name) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 156 | return FindElementsRunAction<ResourceEntry>(name, entries, [&](bool found, auto& iter) { |
| 157 | return found ? iter->get() : entries.emplace(iter, new ResourceEntry(name))->get(); |
| 158 | }); |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 161 | ResourceConfigValue* ResourceEntry::FindValue(const ConfigDescription& config, |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 162 | android::StringPiece product) { |
| 163 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 164 | lt_config_key_ref()); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 165 | if (iter != values.end()) { |
| 166 | ResourceConfigValue* value = iter->get(); |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 167 | if (value->config == config && value->product == product) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 168 | return value; |
| 169 | } |
| 170 | } |
| 171 | return nullptr; |
| 172 | } |
| 173 | |
| 174 | const ResourceConfigValue* ResourceEntry::FindValue(const android::ConfigDescription& config, |
| 175 | android::StringPiece product) const { |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 176 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 177 | lt_config_key_ref()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | if (iter != values.end()) { |
| 179 | ResourceConfigValue* value = iter->get(); |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 180 | if (value->config == config && value->product == product) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | return value; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 182 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 183 | } |
| 184 | return nullptr; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 187 | ResourceConfigValue* ResourceEntry::FindOrCreateValue(const ConfigDescription& config, |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 188 | StringPiece product) { |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 189 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 190 | lt_config_key_ref()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 191 | if (iter != values.end()) { |
| 192 | ResourceConfigValue* value = iter->get(); |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 193 | if (value->config == config && value->product == product) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 194 | return value; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 195 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | } |
| 197 | ResourceConfigValue* newValue = |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 198 | values.insert(iter, util::make_unique<ResourceConfigValue>(config, product))->get(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | return newValue; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 202 | std::vector<ResourceConfigValue*> ResourceEntry::FindAllValues(const ConfigDescription& config) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 203 | std::vector<ResourceConfigValue*> results; |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 204 | auto iter = |
| 205 | std::lower_bound(values.begin(), values.end(), ConfigKey{&config, ""}, lt_config_key_ref()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 206 | for (; iter != values.end(); ++iter) { |
| 207 | ResourceConfigValue* value = iter->get(); |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 208 | if (value->config != config) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | break; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 210 | } |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 211 | results.push_back(value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 212 | } |
| 213 | return results; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 216 | bool ResourceEntry::HasDefaultValue() const { |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 217 | // The default config should be at the top of the list, since the list is sorted. |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 218 | return !values.empty() && values.front()->config == ConfigDescription::DefaultConfig(); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Jeremy Meyer | 211bec2 | 2024-06-04 14:22:03 -0700 | [diff] [blame] | 221 | ResourceTable::CollisionResult ResourceTable::ResolveFlagCollision(FlagStatus existing, |
| 222 | FlagStatus incoming) { |
| 223 | switch (existing) { |
| 224 | case FlagStatus::NoFlag: |
| 225 | switch (incoming) { |
| 226 | case FlagStatus::NoFlag: |
| 227 | return CollisionResult::kConflict; |
| 228 | case FlagStatus::Disabled: |
| 229 | return CollisionResult::kKeepOriginal; |
| 230 | case FlagStatus::Enabled: |
| 231 | return CollisionResult::kTakeNew; |
| 232 | default: |
| 233 | return CollisionResult::kConflict; |
| 234 | } |
| 235 | case FlagStatus::Disabled: |
| 236 | switch (incoming) { |
| 237 | case FlagStatus::NoFlag: |
| 238 | return CollisionResult::kTakeNew; |
| 239 | case FlagStatus::Disabled: |
| 240 | return CollisionResult::kKeepOriginal; |
| 241 | case FlagStatus::Enabled: |
| 242 | return CollisionResult::kTakeNew; |
| 243 | default: |
| 244 | return CollisionResult::kConflict; |
| 245 | } |
| 246 | case FlagStatus::Enabled: |
| 247 | switch (incoming) { |
| 248 | case FlagStatus::NoFlag: |
| 249 | return CollisionResult::kKeepOriginal; |
| 250 | case FlagStatus::Disabled: |
| 251 | return CollisionResult::kKeepOriginal; |
| 252 | case FlagStatus::Enabled: |
| 253 | return CollisionResult::kConflict; |
| 254 | default: |
| 255 | return CollisionResult::kConflict; |
| 256 | } |
| 257 | default: |
| 258 | return CollisionResult::kConflict; |
| 259 | } |
| 260 | } |
| 261 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 262 | // The default handler for collisions. |
| 263 | // |
| 264 | // Typically, a weak value will be overridden by a strong value. An existing weak |
| 265 | // value will not be overridden by an incoming weak value. |
| 266 | // |
| 267 | // There are some exceptions: |
| 268 | // |
| 269 | // Attributes: There are two types of Attribute values: USE and DECL. |
| 270 | // |
| 271 | // USE is anywhere an Attribute is declared without a format, and in a place that would |
| 272 | // be legal to declare if the Attribute already existed. This is typically in a |
| 273 | // <declare-styleable> tag. Attributes defined in a <declare-styleable> are also weak. |
| 274 | // |
| 275 | // DECL is an absolute declaration of an Attribute and specifies an explicit format. |
| 276 | // |
| 277 | // A DECL will override a USE without error. Two DECLs must match in their format for there to be |
| 278 | // no error. |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 279 | ResourceTable::CollisionResult ResourceTable::ResolveValueCollision(Value* existing, |
| 280 | Value* incoming) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 281 | Attribute* existing_attr = ValueCast<Attribute>(existing); |
| 282 | Attribute* incoming_attr = ValueCast<Attribute>(incoming); |
| 283 | if (!incoming_attr) { |
| 284 | if (incoming->IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 285 | // We're trying to add a weak resource but a resource |
| 286 | // already exists. Keep the existing. |
| 287 | return CollisionResult::kKeepOriginal; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 288 | } else if (existing->IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 289 | // Override the weak resource with the new strong resource. |
| 290 | return CollisionResult::kTakeNew; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 291 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 292 | // The existing and incoming values are strong, this is an error |
| 293 | // if the values are not both attributes. |
Adam Lesinski | 5c3464c | 2016-08-24 16:03:48 -0700 | [diff] [blame] | 294 | return CollisionResult::kConflict; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 297 | if (!existing_attr) { |
| 298 | if (existing->IsWeak()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 299 | // The existing value is not an attribute and it is weak, |
| 300 | // so take the incoming attribute value. |
| 301 | return CollisionResult::kTakeNew; |
| 302 | } |
| 303 | // The existing value is not an attribute and it is strong, |
| 304 | // so the incoming attribute value is an error. |
| 305 | return CollisionResult::kConflict; |
| 306 | } |
| 307 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 308 | CHECK(incoming_attr != nullptr && existing_attr != nullptr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 309 | |
| 310 | // |
| 311 | // Attribute specific handling. At this point we know both |
| 312 | // values are attributes. Since we can declare and define |
| 313 | // attributes all-over, we do special handling to see |
| 314 | // which definition sticks. |
| 315 | // |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 316 | if (existing_attr->IsCompatibleWith(*incoming_attr)) { |
| 317 | // The two attributes are both DECLs, but they are plain attributes with compatible formats. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 318 | // Keep the strongest one. |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 319 | return existing_attr->IsWeak() ? CollisionResult::kTakeNew : CollisionResult::kKeepOriginal; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 322 | if (existing_attr->IsWeak() && existing_attr->type_mask == android::ResTable_map::TYPE_ANY) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 323 | // Any incoming attribute is better than this. |
| 324 | return CollisionResult::kTakeNew; |
| 325 | } |
| 326 | |
Adam Lesinski | b1afa07 | 2017-03-29 13:52:38 -0700 | [diff] [blame] | 327 | if (incoming_attr->IsWeak() && incoming_attr->type_mask == android::ResTable_map::TYPE_ANY) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 328 | // The incoming attribute may be a USE instead of a DECL. |
| 329 | // Keep the existing attribute. |
| 330 | return CollisionResult::kKeepOriginal; |
| 331 | } |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 332 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 333 | return CollisionResult::kConflict; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 336 | namespace { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 337 | template <typename T, typename Comparer> |
| 338 | struct SortedVectorInserter : public Comparer { |
| 339 | std::pair<bool, typename std::vector<T>::iterator> LowerBound(std::vector<T>& el, |
| 340 | const T& value) { |
| 341 | auto it = std::lower_bound(el.begin(), el.end(), value, [&](auto& lhs, auto& rhs) { |
| 342 | return Comparer::operator()(lhs, rhs); |
| 343 | }); |
| 344 | bool found = |
| 345 | it != el.end() && !Comparer::operator()(*it, value) && !Comparer::operator()(value, *it); |
| 346 | return std::make_pair(found, it); |
| 347 | } |
| 348 | |
| 349 | T* Insert(std::vector<T>& el, T&& value) { |
| 350 | auto [found, it] = LowerBound(el, value); |
| 351 | if (found) { |
| 352 | return &*it; |
| 353 | } |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 354 | return &*el.insert(it, std::move(value)); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 355 | } |
| 356 | }; |
| 357 | |
| 358 | struct PackageViewComparer { |
| 359 | bool operator()(const ResourceTablePackageView& lhs, const ResourceTablePackageView& rhs) { |
| 360 | return less_than_struct_with_name_and_id<ResourceTablePackageView, uint8_t>( |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 361 | lhs, std::tie(rhs.name, rhs.id)); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 362 | } |
| 363 | }; |
| 364 | |
| 365 | struct TypeViewComparer { |
| 366 | bool operator()(const ResourceTableTypeView& lhs, const ResourceTableTypeView& rhs) { |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 367 | return lhs.id != rhs.id ? lhs.id < rhs.id : lhs.named_type < rhs.named_type; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 368 | } |
| 369 | }; |
| 370 | |
| 371 | struct EntryViewComparer { |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 372 | bool operator()(const ResourceTableEntryView& lhs, const ResourceTableEntryView& rhs) { |
| 373 | return less_than_struct_with_name_and_id<ResourceTableEntryView, uint16_t>( |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 374 | lhs, std::tie(rhs.name, rhs.id)); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 375 | } |
| 376 | }; |
| 377 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 378 | void InsertEntryIntoTableView(ResourceTableView& table, const ResourceTablePackage* package, |
| 379 | const ResourceTableType* type, const std::string& entry_name, |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 380 | const std::optional<ResourceId>& id, const Visibility& visibility, |
| 381 | const std::optional<AllowNew>& allow_new, |
| 382 | const std::optional<OverlayableItem>& overlayable_item, |
| 383 | const std::optional<StagedId>& staged_id, |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 384 | const std::vector<std::unique_ptr<ResourceConfigValue>>& values) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 385 | SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter; |
| 386 | SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 387 | SortedVectorInserter<ResourceTableEntryView, EntryViewComparer> entry_inserter; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 388 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 389 | ResourceTablePackageView new_package{package->name, |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 390 | id ? id.value().package_id() : std::optional<uint8_t>{}}; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 391 | auto view_package = package_inserter.Insert(table.packages, std::move(new_package)); |
| 392 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 393 | ResourceTableTypeView new_type{type->named_type, |
| 394 | id ? id.value().type_id() : std::optional<uint8_t>{}}; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 395 | auto view_type = type_inserter.Insert(view_package->types, std::move(new_type)); |
| 396 | |
| 397 | if (visibility.level == Visibility::Level::kPublic) { |
| 398 | // Only mark the type visibility level as public, it doesn't care about being private. |
| 399 | view_type->visibility_level = Visibility::Level::kPublic; |
| 400 | } |
| 401 | |
| 402 | ResourceTableEntryView new_entry{.name = entry_name, |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 403 | .id = id ? id.value().entry_id() : std::optional<uint16_t>{}, |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 404 | .visibility = visibility, |
| 405 | .allow_new = allow_new, |
| 406 | .overlayable_item = overlayable_item, |
| 407 | .staged_id = staged_id}; |
| 408 | for (auto& value : values) { |
| 409 | new_entry.values.emplace_back(value.get()); |
| 410 | } |
| 411 | |
| 412 | entry_inserter.Insert(view_type->entries, std::move(new_entry)); |
| 413 | } |
| 414 | } // namespace |
| 415 | |
| 416 | const ResourceConfigValue* ResourceTableEntryView::FindValue(const ConfigDescription& config, |
| 417 | android::StringPiece product) const { |
| 418 | auto iter = std::lower_bound(values.begin(), values.end(), ConfigKey{&config, product}, |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 419 | lt_config_key_ref()); |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 420 | if (iter != values.end()) { |
| 421 | const ResourceConfigValue* value = *iter; |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 422 | if (value->config == config && value->product == product) { |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 423 | return value; |
| 424 | } |
| 425 | } |
| 426 | return nullptr; |
| 427 | } |
| 428 | |
| 429 | ResourceTableView ResourceTable::GetPartitionedView(const ResourceTableViewOptions& options) const { |
| 430 | ResourceTableView view; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 431 | for (const auto& package : packages) { |
| 432 | for (const auto& type : package->types) { |
| 433 | for (const auto& entry : type->entries) { |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 434 | InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, entry->id, |
| 435 | entry->visibility, entry->allow_new, entry->overlayable_item, |
| 436 | entry->staged_id, entry->values); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 437 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 438 | if (options.create_alias_entries && entry->staged_id) { |
| 439 | auto alias_id = entry->staged_id.value().id; |
| 440 | InsertEntryIntoTableView(view, package.get(), type.get(), entry->name, alias_id, |
| 441 | entry->visibility, entry->allow_new, entry->overlayable_item, {}, |
| 442 | entry->values); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 443 | } |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 444 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 445 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 446 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 447 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 448 | // The android runtime does not support querying resources when the there are multiple type ids |
| 449 | // for the same resource type within the same package. For this reason, if there are types with |
| 450 | // multiple type ids, each type needs to exist in its own package in order to be queried by name. |
| 451 | std::vector<ResourceTablePackageView> new_packages; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 452 | SortedVectorInserter<ResourceTablePackageView, PackageViewComparer> package_inserter; |
| 453 | SortedVectorInserter<ResourceTableTypeView, TypeViewComparer> type_inserter; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 454 | for (auto& package : view.packages) { |
| 455 | // If a new package was already created for a different type within this package, then |
| 456 | // we can reuse those packages for other types that need to be extracted from this package. |
| 457 | // `start_index` is the index of the first newly created package that can be reused. |
| 458 | const size_t start_index = new_packages.size(); |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 459 | std::map<ResourceNamedType, size_t> type_new_package_index; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 460 | for (auto type_it = package.types.begin(); type_it != package.types.end();) { |
| 461 | auto& type = *type_it; |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 462 | auto type_index_iter = type_new_package_index.find(type.named_type); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 463 | if (type_index_iter == type_new_package_index.end()) { |
| 464 | // First occurrence of the resource type in this package. Keep it in this package. |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 465 | type_new_package_index.insert(type_index_iter, |
| 466 | std::make_pair(type.named_type, start_index)); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 467 | ++type_it; |
| 468 | continue; |
| 469 | } |
| 470 | |
| 471 | // The resource type has already been seen for this package, so this type must be extracted to |
| 472 | // a new separate package. |
| 473 | const size_t index = type_index_iter->second; |
| 474 | if (new_packages.size() == index) { |
| 475 | new_packages.emplace_back(ResourceTablePackageView{package.name, package.id}); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // Move the type into a new package |
| 479 | auto& other_package = new_packages[index]; |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 480 | type_new_package_index[type.named_type] = index + 1; |
Greg Kaiser | b40f3ab | 2021-10-18 06:37:26 -0700 | [diff] [blame] | 481 | type_inserter.Insert(other_package.types, std::move(type)); |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 482 | type_it = package.types.erase(type_it); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | for (auto& new_package : new_packages) { |
| 487 | // Insert newly created packages after their original packages |
| 488 | auto [_, it] = package_inserter.LowerBound(view.packages, new_package); |
| 489 | view.packages.insert(++it, std::move(new_package)); |
| 490 | } |
| 491 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 492 | return view; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 495 | bool ResourceTable::AddResource(NewResource&& res, android::IDiagnostics* diag) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 496 | CHECK(diag != nullptr) << "Diagnostic pointer is null"; |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 497 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 498 | const bool validate = validation_ == Validation::kEnabled; |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 499 | const android::Source source = res.value ? res.value->GetSource() : android::Source{}; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 500 | if (validate && !res.allow_mangled && !IsValidResourceEntryName(res.name.entry)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 501 | diag->Error(android::DiagMessage(source) |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 502 | << "resource '" << res.name << "' has invalid entry name '" << res.name.entry); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 503 | return false; |
| 504 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 505 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 506 | if (res.id.has_value() && !res.id->first.is_valid()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 507 | diag->Error(android::DiagMessage(source) |
| 508 | << "trying to add resource '" << res.name << "' with ID " << res.id->first |
| 509 | << " but that ID is invalid"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 510 | return false; |
| 511 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 512 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 513 | auto package = FindOrCreatePackage(res.name.package); |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 514 | auto type = package->FindOrCreateType(res.name.type); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 515 | auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), res.name.entry, |
| 516 | NameEqualRange<ResourceEntry>{}); |
| 517 | const size_t entry_count = std::distance(entry_it.first, entry_it.second); |
Ryan Mitchell | 83a37ad | 2018-08-06 16:32:24 -0700 | [diff] [blame] | 518 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 519 | ResourceEntry* entry; |
| 520 | if (entry_count == 0) { |
| 521 | // Adding a new resource |
| 522 | entry = type->CreateEntry(res.name.entry); |
| 523 | } else if (entry_count == 1) { |
| 524 | // Assume that the existing resource is being modified |
| 525 | entry = entry_it.first->get(); |
| 526 | } else { |
| 527 | // Multiple resources with the same name exist in the resource table. The only way to |
| 528 | // distinguish between them is using resource id since each resource should have a unique id. |
| 529 | CHECK(res.id.has_value()) << "ambiguous modification of resource entry '" << res.name |
| 530 | << "' without specifying a resource id."; |
| 531 | entry = entry_it.first->get(); |
| 532 | for (auto it = entry_it.first; it != entry_it.second; ++it) { |
| 533 | CHECK((bool)(*it)->id) << "ambiguous modification of resource entry '" << res.name |
| 534 | << "' with multiple entries without resource ids"; |
| 535 | if ((*it)->id == res.id->first) { |
| 536 | entry = it->get(); |
| 537 | break; |
| 538 | } |
| 539 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 540 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 541 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 542 | if (res.id.has_value()) { |
| 543 | if (entry->id && entry->id.value() != res.id->first) { |
| 544 | if (res.id->second != OnIdConflict::CREATE_ENTRY) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 545 | diag->Error(android::DiagMessage(source) |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 546 | << "trying to add resource '" << res.name << "' with ID " << res.id->first |
| 547 | << " but resource already has ID " << entry->id.value()); |
| 548 | return false; |
| 549 | } |
| 550 | entry = type->CreateEntry(res.name.entry); |
| 551 | } |
| 552 | entry->id = res.id->first; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 553 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 554 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 555 | if (res.visibility.has_value()) { |
| 556 | // Only mark the type visibility level as public, it doesn't care about being private. |
| 557 | if (res.visibility->level == Visibility::Level::kPublic) { |
| 558 | type->visibility_level = Visibility::Level::kPublic; |
| 559 | } |
| 560 | |
| 561 | if (res.visibility->level > entry->visibility.level) { |
| 562 | // This symbol definition takes precedence, replace. |
| 563 | entry->visibility = res.visibility.value(); |
| 564 | } |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 565 | |
| 566 | if (res.visibility->staged_api) { |
| 567 | entry->visibility.staged_api = entry->visibility.staged_api; |
| 568 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 569 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 570 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 571 | if (res.overlayable.has_value()) { |
| 572 | if (entry->overlayable_item) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 573 | diag->Error(android::DiagMessage(res.overlayable->source) |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 574 | << "duplicate overlayable declaration for resource '" << res.name << "'"); |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 575 | diag->Error(android::DiagMessage(entry->overlayable_item.value().source) |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 576 | << "previous declaration here"); |
| 577 | return false; |
| 578 | } |
| 579 | entry->overlayable_item = res.overlayable.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 582 | if (res.allow_new.has_value()) { |
| 583 | entry->allow_new = res.allow_new.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 586 | if (res.staged_id.has_value()) { |
| 587 | entry->staged_id = res.staged_id.value(); |
| 588 | } |
| 589 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 590 | if (res.value != nullptr) { |
| 591 | auto config_value = entry->FindOrCreateValue(res.config, res.product); |
| 592 | if (!config_value->value) { |
| 593 | // Resource does not exist, add it now. |
| 594 | config_value->value = std::move(res.value); |
| 595 | } else { |
| 596 | // When validation is enabled, ensure that a resource cannot have multiple values defined for |
Jeremy Meyer | 211bec2 | 2024-06-04 14:22:03 -0700 | [diff] [blame] | 597 | // the same configuration unless protected by flags. |
Jeremy Meyer | d52bd68 | 2024-08-14 11:16:58 -0700 | [diff] [blame] | 598 | auto result = |
| 599 | validate ? ResolveFlagCollision(config_value->value->GetFlagStatus(), res.flag_status) |
| 600 | : CollisionResult::kKeepBoth; |
Jeremy Meyer | 211bec2 | 2024-06-04 14:22:03 -0700 | [diff] [blame] | 601 | if (result == CollisionResult::kConflict) { |
| 602 | result = ResolveValueCollision(config_value->value.get(), res.value.get()); |
| 603 | } |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 604 | switch (result) { |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 605 | case CollisionResult::kKeepBoth: { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 606 | // Insert the value ignoring for duplicate configurations |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 607 | auto it = entry->values.insert( |
| 608 | std::lower_bound(entry->values.begin(), entry->values.end(), |
| 609 | ConfigKey{&res.config, res.product}, lt_config_key_ref()), |
| 610 | util::make_unique<ResourceConfigValue>(res.config, res.product)); |
| 611 | (*it)->value = std::move(res.value); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 612 | break; |
Yurii Zubrytskyi | a62e6be | 2024-08-27 14:20:51 -0700 | [diff] [blame] | 613 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 614 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 615 | case CollisionResult::kTakeNew: |
| 616 | // Take the incoming value. |
| 617 | config_value->value = std::move(res.value); |
| 618 | break; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 619 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 620 | case CollisionResult::kConflict: |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 621 | diag->Error(android::DiagMessage(source) |
| 622 | << "duplicate value for resource '" << res.name << "' " |
| 623 | << "with config '" << res.config << "'"); |
| 624 | diag->Error(android::DiagMessage(source) << "resource previously defined here"); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 625 | return false; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 626 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 627 | case CollisionResult::kKeepOriginal: |
| 628 | break; |
| 629 | } |
| 630 | } |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 633 | return true; |
| 634 | } |
| 635 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 636 | std::optional<ResourceTable::SearchResult> ResourceTable::FindResource( |
| 637 | const ResourceNameRef& name) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 638 | ResourceTablePackage* package = FindPackage(name.package); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 639 | if (package == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 640 | return {}; |
| 641 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 642 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 643 | ResourceTableType* type = package->FindType(name.type); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 644 | if (type == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 645 | return {}; |
| 646 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 647 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 648 | ResourceEntry* entry = type->FindEntry(name.entry); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 649 | if (entry == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 650 | return {}; |
| 651 | } |
| 652 | return SearchResult{package, type, entry}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 655 | std::optional<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNameRef& name, |
| 656 | ResourceId id) const { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 657 | ResourceTablePackage* package = FindPackage(name.package); |
| 658 | if (package == nullptr) { |
| 659 | return {}; |
| 660 | } |
| 661 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 662 | ResourceTableType* type = package->FindType(name.type); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 663 | if (type == nullptr) { |
| 664 | return {}; |
| 665 | } |
| 666 | |
| 667 | auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry, |
| 668 | NameEqualRange<ResourceEntry>{}); |
| 669 | for (auto it = entry_it.first; it != entry_it.second; ++it) { |
| 670 | if ((*it)->id == id) { |
| 671 | return SearchResult{package, type, it->get()}; |
| 672 | } |
| 673 | } |
| 674 | return {}; |
| 675 | } |
| 676 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 677 | bool ResourceTable::RemoveResource(const ResourceNameRef& name, ResourceId id) const { |
| 678 | ResourceTablePackage* package = FindPackage(name.package); |
| 679 | if (package == nullptr) { |
| 680 | return {}; |
| 681 | } |
| 682 | |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 683 | ResourceTableType* type = package->FindType(name.type); |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 684 | if (type == nullptr) { |
| 685 | return {}; |
| 686 | } |
| 687 | |
| 688 | auto entry_it = std::equal_range(type->entries.begin(), type->entries.end(), name.entry, |
| 689 | NameEqualRange<ResourceEntry>{}); |
| 690 | for (auto it = entry_it.first; it != entry_it.second; ++it) { |
| 691 | if ((*it)->id == id) { |
| 692 | type->entries.erase(it); |
| 693 | return true; |
| 694 | } |
| 695 | } |
| 696 | return false; |
| 697 | } |
| 698 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 699 | std::unique_ptr<ResourceTable> ResourceTable::Clone() const { |
| 700 | std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>(); |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 701 | CloningValueTransformer cloner(&new_table->string_pool); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 702 | for (const auto& pkg : packages) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 703 | ResourceTablePackage* new_pkg = new_table->FindOrCreatePackage(pkg->name); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 704 | for (const auto& type : pkg->types) { |
Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 705 | ResourceTableType* new_type = new_pkg->FindOrCreateType(type->named_type); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 706 | new_type->visibility_level = type->visibility_level; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 707 | |
| 708 | for (const auto& entry : type->entries) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 709 | ResourceEntry* new_entry = new_type->CreateEntry(entry->name); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 710 | new_entry->id = entry->id; |
| 711 | new_entry->visibility = entry->visibility; |
| 712 | new_entry->allow_new = entry->allow_new; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 713 | new_entry->overlayable_item = entry->overlayable_item; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 714 | |
| 715 | for (const auto& config_value : entry->values) { |
| 716 | ResourceConfigValue* new_value = |
| 717 | new_entry->FindOrCreateValue(config_value->config, config_value->product); |
Ryan Mitchell | efcdb95 | 2021-04-14 17:31:37 -0700 | [diff] [blame] | 718 | new_value->value = config_value->value->Transform(cloner); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | return new_table; |
| 724 | } |
| 725 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 726 | NewResourceBuilder::NewResourceBuilder(const ResourceNameRef& name) { |
| 727 | res_.name = name.ToResourceName(); |
| 728 | } |
| 729 | |
| 730 | NewResourceBuilder::NewResourceBuilder(const std::string& name) { |
| 731 | ResourceNameRef ref; |
| 732 | CHECK(ResourceUtils::ParseResourceName(name, &ref)) << "invalid resource name: " << name; |
| 733 | res_.name = ref.ToResourceName(); |
| 734 | } |
| 735 | |
| 736 | NewResourceBuilder& NewResourceBuilder::SetValue(std::unique_ptr<Value> value, |
| 737 | android::ConfigDescription config, |
| 738 | std::string product) { |
| 739 | res_.value = std::move(value); |
| 740 | res_.config = std::move(config); |
| 741 | res_.product = std::move(product); |
| 742 | return *this; |
| 743 | } |
| 744 | |
| 745 | NewResourceBuilder& NewResourceBuilder::SetId(ResourceId id, OnIdConflict on_conflict) { |
| 746 | res_.id = std::make_pair(id, on_conflict); |
| 747 | return *this; |
| 748 | } |
| 749 | |
| 750 | NewResourceBuilder& NewResourceBuilder::SetVisibility(Visibility visibility) { |
| 751 | res_.visibility = std::move(visibility); |
| 752 | return *this; |
| 753 | } |
| 754 | |
| 755 | NewResourceBuilder& NewResourceBuilder::SetOverlayable(OverlayableItem overlayable) { |
| 756 | res_.overlayable = std::move(overlayable); |
| 757 | return *this; |
| 758 | } |
| 759 | NewResourceBuilder& NewResourceBuilder::SetAllowNew(AllowNew allow_new) { |
| 760 | res_.allow_new = std::move(allow_new); |
| 761 | return *this; |
| 762 | } |
| 763 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 764 | NewResourceBuilder& NewResourceBuilder::SetStagedId(StagedId staged_alias) { |
| 765 | res_.staged_id = std::move(staged_alias); |
| 766 | return *this; |
| 767 | } |
| 768 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 769 | NewResourceBuilder& NewResourceBuilder::SetAllowMangled(bool allow_mangled) { |
| 770 | res_.allow_mangled = allow_mangled; |
| 771 | return *this; |
| 772 | } |
| 773 | |
Jeremy Meyer | 211bec2 | 2024-06-04 14:22:03 -0700 | [diff] [blame] | 774 | NewResourceBuilder& NewResourceBuilder::SetFlagStatus(FlagStatus flag_status) { |
| 775 | res_.flag_status = flag_status; |
| 776 | return *this; |
| 777 | } |
| 778 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 779 | NewResource NewResourceBuilder::Build() { |
| 780 | return std::move(res_); |
| 781 | } |
| 782 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 783 | } // namespace aapt |