Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [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 | |
| 17 | #include "ConfigDescription.h" |
| 18 | #include "Resource.h" |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 19 | #include "ResourceUtils.h" |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 20 | #include "ValueVisitor.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "process/SymbolTable.h" |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 22 | #include "util/Util.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 23 | |
| 24 | #include <androidfw/AssetManager.h> |
| 25 | #include <androidfw/ResourceTypes.h> |
| 26 | |
| 27 | namespace aapt { |
| 28 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 29 | void SymbolTable::appendSource(std::unique_ptr<ISymbolSource> source) { |
| 30 | mSources.push_back(std::move(source)); |
| 31 | |
| 32 | // We do not clear the cache, because sources earlier in the list take precedent. |
| 33 | } |
| 34 | |
| 35 | void SymbolTable::prependSource(std::unique_ptr<ISymbolSource> source) { |
| 36 | mSources.insert(mSources.begin(), std::move(source)); |
| 37 | |
| 38 | // We must clear the cache in case we did a lookup before adding this resource. |
| 39 | mCache.clear(); |
| 40 | } |
| 41 | |
| 42 | const SymbolTable::Symbol* SymbolTable::findByName(const ResourceName& name) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 43 | if (const std::shared_ptr<Symbol>& s = mCache.get(name)) { |
| 44 | return s.get(); |
| 45 | } |
| 46 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 47 | // We did not find it in the cache, so look through the sources. |
| 48 | for (auto& symbolSource : mSources) { |
| 49 | std::unique_ptr<Symbol> symbol = symbolSource->findByName(name); |
| 50 | if (symbol) { |
| 51 | // Take ownership of the symbol into a shared_ptr. We do this because LruCache |
| 52 | // doesn't support unique_ptr. |
| 53 | std::shared_ptr<Symbol> sharedSymbol = std::shared_ptr<Symbol>(symbol.release()); |
| 54 | mCache.put(name, sharedSymbol); |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 55 | |
| 56 | if (sharedSymbol->id) { |
| 57 | // The symbol has an ID, so we can also cache this! |
| 58 | mIdCache.put(sharedSymbol->id.value(), sharedSymbol); |
| 59 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 60 | return sharedSymbol.get(); |
| 61 | } |
| 62 | } |
| 63 | return nullptr; |
| 64 | } |
| 65 | |
| 66 | const SymbolTable::Symbol* SymbolTable::findById(ResourceId id) { |
| 67 | if (const std::shared_ptr<Symbol>& s = mIdCache.get(id)) { |
| 68 | return s.get(); |
| 69 | } |
| 70 | |
| 71 | // We did not find it in the cache, so look through the sources. |
| 72 | for (auto& symbolSource : mSources) { |
| 73 | std::unique_ptr<Symbol> symbol = symbolSource->findById(id); |
| 74 | if (symbol) { |
| 75 | // Take ownership of the symbol into a shared_ptr. We do this because LruCache |
| 76 | // doesn't support unique_ptr. |
| 77 | std::shared_ptr<Symbol> sharedSymbol = std::shared_ptr<Symbol>(symbol.release()); |
| 78 | mIdCache.put(id, sharedSymbol); |
| 79 | return sharedSymbol.get(); |
| 80 | } |
| 81 | } |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 85 | const SymbolTable::Symbol* SymbolTable::findByReference(const Reference& ref) { |
| 86 | // First try the ID. This is because when we lookup by ID, we only fill in the ID cache. |
| 87 | // Looking up by name fills in the name and ID cache. So a cache miss will cause a failed |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 88 | // ID lookup, then a successful name lookup. Subsequent look ups will hit immediately |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 89 | // because the ID is cached too. |
| 90 | // |
| 91 | // If we looked up by name first, a cache miss would mean we failed to lookup by name, then |
| 92 | // succeeded to lookup by ID. Subsequent lookups will miss then hit. |
| 93 | const SymbolTable::Symbol* symbol = nullptr; |
| 94 | if (ref.id) { |
| 95 | symbol = findById(ref.id.value()); |
| 96 | } |
| 97 | |
| 98 | if (ref.name && !symbol) { |
| 99 | symbol = findByName(ref.name.value()); |
| 100 | } |
| 101 | return symbol; |
| 102 | } |
| 103 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 104 | std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::findByName( |
| 105 | const ResourceName& name) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 106 | Maybe<ResourceTable::SearchResult> result = mTable->findResource(name); |
| 107 | if (!result) { |
| 108 | if (name.type == ResourceType::kAttr) { |
| 109 | // Recurse and try looking up a private attribute. |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 110 | return findByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 111 | } |
| 112 | return {}; |
| 113 | } |
| 114 | |
| 115 | ResourceTable::SearchResult sr = result.value(); |
| 116 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 117 | std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 118 | symbol->isPublic = (sr.entry->symbolStatus.state == SymbolState::kPublic); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 119 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 120 | if (sr.package->id && sr.type->id && sr.entry->id) { |
| 121 | symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), sr.entry->id.value()); |
| 122 | } |
| 123 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 124 | if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 125 | const ConfigDescription kDefaultConfig; |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 126 | ResourceConfigValue* configValue = sr.entry->findValue(kDefaultConfig); |
| 127 | if (configValue) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 128 | // This resource has an Attribute. |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 129 | if (Attribute* attr = valueCast<Attribute>(configValue->value.get())) { |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 130 | symbol->attribute = std::make_shared<Attribute>(*attr); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 131 | } else { |
| 132 | return {}; |
| 133 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 136 | return symbol; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 139 | bool AssetManagerSymbolSource::addAssetPath(const StringPiece& path) { |
| 140 | int32_t cookie = 0; |
| 141 | return mAssets.addAssetPath(android::String8(path.data(), path.size()), &cookie); |
| 142 | } |
| 143 | |
| 144 | static std::unique_ptr<SymbolTable::Symbol> lookupAttributeInTable(const android::ResTable& table, |
| 145 | ResourceId id) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 146 | // Try as a bag. |
| 147 | const android::ResTable::bag_entry* entry; |
| 148 | ssize_t count = table.lockBag(id.id, &entry); |
| 149 | if (count < 0) { |
| 150 | table.unlockBag(entry); |
| 151 | return nullptr; |
| 152 | } |
| 153 | |
| 154 | // We found a resource. |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 155 | std::unique_ptr<SymbolTable::Symbol> s = util::make_unique<SymbolTable::Symbol>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 156 | s->id = id; |
| 157 | |
| 158 | // Check to see if it is an attribute. |
| 159 | for (size_t i = 0; i < (size_t) count; i++) { |
| 160 | if (entry[i].map.name.ident == android::ResTable_map::ATTR_TYPE) { |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 161 | s->attribute = std::make_shared<Attribute>(false); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 162 | s->attribute->typeMask = entry[i].map.value.data; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (s->attribute) { |
| 168 | for (size_t i = 0; i < (size_t) count; i++) { |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 169 | const android::ResTable_map& mapEntry = entry[i].map; |
| 170 | if (Res_INTERNALID(mapEntry.name.ident)) { |
| 171 | switch (mapEntry.name.ident) { |
| 172 | case android::ResTable_map::ATTR_MIN: |
| 173 | s->attribute->minInt = static_cast<int32_t>(mapEntry.value.data); |
| 174 | break; |
| 175 | case android::ResTable_map::ATTR_MAX: |
| 176 | s->attribute->maxInt = static_cast<int32_t>(mapEntry.value.data); |
| 177 | break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 178 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 179 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 180 | } |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 181 | |
| 182 | android::ResTable::resource_name entryName; |
| 183 | if (!table.getResourceName(mapEntry.name.ident, false, &entryName)) { |
| 184 | table.unlockBag(entry); |
| 185 | return nullptr; |
| 186 | } |
| 187 | |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 188 | Maybe<ResourceName> parsedName = ResourceUtils::toResourceName(entryName); |
| 189 | if (!parsedName) { |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 190 | return nullptr; |
| 191 | } |
| 192 | |
| 193 | Attribute::Symbol symbol; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 194 | symbol.symbol.name = parsedName.value(); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 195 | symbol.symbol.id = ResourceId(mapEntry.name.ident); |
| 196 | symbol.value = mapEntry.value.data; |
| 197 | s->attribute->symbols.push_back(std::move(symbol)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | table.unlockBag(entry); |
| 201 | return s; |
| 202 | } |
| 203 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 204 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findByName( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 205 | const ResourceName& name) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 206 | const android::ResTable& table = mAssets.getResources(false); |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 207 | |
| 208 | const std::u16string package16 = util::utf8ToUtf16(name.package); |
| 209 | const std::u16string type16 = util::utf8ToUtf16(toString(name.type)); |
| 210 | const std::u16string entry16 = util::utf8ToUtf16(name.entry); |
| 211 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 212 | uint32_t typeSpecFlags = 0; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 213 | ResourceId resId = table.identifierForName(entry16.data(), entry16.size(), |
| 214 | type16.data(), type16.size(), |
| 215 | package16.data(), package16.size(), |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 216 | &typeSpecFlags); |
| 217 | if (!resId.isValid()) { |
| 218 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 221 | std::unique_ptr<SymbolTable::Symbol> s; |
| 222 | if (name.type == ResourceType::kAttr) { |
| 223 | s = lookupAttributeInTable(table, resId); |
| 224 | } else { |
| 225 | s = util::make_unique<SymbolTable::Symbol>(); |
| 226 | s->id = resId; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 227 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 228 | |
| 229 | if (s) { |
| 230 | s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; |
| 231 | return s; |
| 232 | } |
| 233 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 236 | static Maybe<ResourceName> getResourceName(const android::ResTable& table, ResourceId id) { |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 237 | android::ResTable::resource_name resName = {}; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 238 | if (!table.getResourceName(id.id, true, &resName)) { |
| 239 | return {}; |
| 240 | } |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame^] | 241 | return ResourceUtils::toResourceName(resName); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 244 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findById(ResourceId id) { |
| 245 | const android::ResTable& table = mAssets.getResources(false); |
| 246 | Maybe<ResourceName> maybeName = getResourceName(table, id); |
| 247 | if (!maybeName) { |
| 248 | return {}; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 251 | uint32_t typeSpecFlags = 0; |
| 252 | table.getResourceFlags(id.id, &typeSpecFlags); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 253 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 254 | std::unique_ptr<SymbolTable::Symbol> s; |
| 255 | if (maybeName.value().type == ResourceType::kAttr) { |
| 256 | s = lookupAttributeInTable(table, id); |
| 257 | } else { |
| 258 | s = util::make_unique<SymbolTable::Symbol>(); |
| 259 | s->id = id; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 260 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 261 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 262 | if (s) { |
| 263 | s->isPublic = (typeSpecFlags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0; |
| 264 | return s; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 265 | } |
| 266 | return {}; |
| 267 | } |
| 268 | |
Adam Lesinski | 7656554f | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 269 | std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::findByReference( |
| 270 | const Reference& ref) { |
| 271 | // AssetManager always prefers IDs. |
| 272 | if (ref.id) { |
| 273 | return findById(ref.id.value()); |
| 274 | } else if (ref.name) { |
| 275 | return findByName(ref.name.value()); |
| 276 | } |
| 277 | return {}; |
| 278 | } |
| 279 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 280 | } // namespace aapt |