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