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