blob: 61a8fbbb7f52172884dbc2a2af68b94268bc802f [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinskicacb28f2016-10-19 12:18:14 -070017#include "process/SymbolTable.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080019#include <iostream>
20
21#include "android-base/logging.h"
22#include "android-base/stringprintf.h"
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080023#include "androidfw/Asset.h"
24#include "androidfw/AssetManager2.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020025#include "androidfw/ConfigDescription.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "androidfw/ResourceTypes.h"
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080027#include "androidfw/ResourceUtils.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080029#include "NameMangler.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "Resource.h"
Adam Lesinskid0f116b2016-07-08 15:00:32 -070031#include "ResourceUtils.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070032#include "ValueVisitor.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080033#include "trace/TraceBuffer.h"
Adam Lesinskie78fd612015-10-22 12:48:43 -070034#include "util/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035
Ryan Mitchella55dc2e2019-01-24 10:58:23 -080036using ::android::ApkAssets;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020037using ::android::ConfigDescription;
Adam Lesinskidc785052017-12-07 15:58:46 -080038using ::android::StringPiece;
39using ::android::StringPiece16;
Adam Lesinskid5083f62017-01-16 15:07:21 -080040
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041namespace aapt {
42
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070043SymbolTable::SymbolTable(NameMangler* mangler)
44 : mangler_(mangler),
45 delegate_(util::make_unique<DefaultSymbolTableDelegate>()),
46 cache_(200),
47 id_cache_(200) {
48}
49
50void SymbolTable::SetDelegate(std::unique_ptr<ISymbolTableDelegate> delegate) {
51 CHECK(delegate != nullptr) << "can't set a nullptr delegate";
52 delegate_ = std::move(delegate);
53
54 // Clear the cache in case this delegate changes the order of lookup.
55 cache_.clear();
56}
57
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058void SymbolTable::AppendSource(std::unique_ptr<ISymbolSource> source) {
59 sources_.push_back(std::move(source));
Adam Lesinski64587af2016-02-18 18:33:06 -080060
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 // We do not clear the cache, because sources earlier in the list take
62 // precedent.
Adam Lesinski64587af2016-02-18 18:33:06 -080063}
64
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065void SymbolTable::PrependSource(std::unique_ptr<ISymbolSource> source) {
66 sources_.insert(sources_.begin(), std::move(source));
Adam Lesinski64587af2016-02-18 18:33:06 -080067
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 // We must clear the cache in case we did a lookup before adding this
69 // resource.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 cache_.clear();
Adam Lesinski64587af2016-02-18 18:33:06 -080071}
72
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073const SymbolTable::Symbol* SymbolTable::FindByName(const ResourceName& name) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080074 const ResourceName* name_with_package = &name;
75
76 // Fill in the package name if necessary.
77 // If there is no package in `name`, we will need to copy the ResourceName
78 // and store it somewhere; we use the Maybe<> class to reserve storage.
79 Maybe<ResourceName> name_with_package_impl;
80 if (name.package.empty()) {
81 name_with_package_impl = ResourceName(mangler_->GetTargetPackageName(), name.type, name.entry);
82 name_with_package = &name_with_package_impl.value();
83 }
84
85 // We store the name unmangled in the cache, so look it up as-is.
86 if (const std::shared_ptr<Symbol>& s = cache_.get(*name_with_package)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 return s.get();
88 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -080090 // The name was not found in the cache. Mangle it (if necessary) and find it in our sources.
91 // Again, here we use a Maybe<> object to reserve storage if we need to mangle.
92 const ResourceName* mangled_name = name_with_package;
93 Maybe<ResourceName> mangled_name_impl;
94 if (mangler_->ShouldMangle(name_with_package->package)) {
95 mangled_name_impl = mangler_->MangleName(*name_with_package);
96 mangled_name = &mangled_name_impl.value();
97 }
98
Adam Lesinski1e4b0e52017-04-27 15:01:10 -070099 std::unique_ptr<Symbol> symbol = delegate_->FindByName(*mangled_name, sources_);
100 if (symbol == nullptr) {
101 return nullptr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 }
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700103
104 // Take ownership of the symbol into a shared_ptr. We do this because
105 // LruCache doesn't support unique_ptr.
106 std::shared_ptr<Symbol> shared_symbol(std::move(symbol));
107
108 // Since we look in the cache with the unmangled, but package prefixed
109 // name, we must put the same name into the cache.
110 cache_.put(*name_with_package, shared_symbol);
111
112 if (shared_symbol->id) {
113 // The symbol has an ID, so we can also cache this!
114 id_cache_.put(shared_symbol->id.value(), shared_symbol);
115 }
116
117 // Returns the raw pointer. Callers are not expected to hold on to this
118 // between calls to Find*.
119 return shared_symbol.get();
Adam Lesinski64587af2016-02-18 18:33:06 -0800120}
121
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122const SymbolTable::Symbol* SymbolTable::FindById(const ResourceId& id) {
123 if (const std::shared_ptr<Symbol>& s = id_cache_.get(id)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700124 return s.get();
125 }
Adam Lesinski64587af2016-02-18 18:33:06 -0800126
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 // We did not find it in the cache, so look through the sources.
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700128 std::unique_ptr<Symbol> symbol = delegate_->FindById(id, sources_);
129 if (symbol == nullptr) {
130 return nullptr;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 }
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700132
133 // Take ownership of the symbol into a shared_ptr. We do this because LruCache
134 // doesn't support unique_ptr.
135 std::shared_ptr<Symbol> shared_symbol(std::move(symbol));
136 id_cache_.put(id, shared_symbol);
137
138 // Returns the raw pointer. Callers are not expected to hold on to this
139 // between calls to Find*.
140 return shared_symbol.get();
Adam Lesinski64587af2016-02-18 18:33:06 -0800141}
142
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700143const SymbolTable::Symbol* SymbolTable::FindByReference(const Reference& ref) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800144 // First try the ID. This is because when we lookup by ID, we only fill in the ID cache.
145 // Looking up by name fills in the name and ID cache. So a cache miss will cause a failed
146 // ID lookup, then a successful name lookup. Subsequent look ups will hit immediately
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 // because the ID is cached too.
148 //
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800149 // If we looked up by name first, a cache miss would mean we failed to lookup by name, then
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 // succeeded to lookup by ID. Subsequent lookups will miss then hit.
151 const SymbolTable::Symbol* symbol = nullptr;
152 if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 symbol = FindById(ref.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 }
Adam Lesinski76565542016-03-10 21:55:04 -0800155
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 if (ref.name && !symbol) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 symbol = FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 }
159 return symbol;
Adam Lesinski76565542016-03-10 21:55:04 -0800160}
161
Adam Lesinski1e4b0e52017-04-27 15:01:10 -0700162std::unique_ptr<SymbolTable::Symbol> DefaultSymbolTableDelegate::FindByName(
163 const ResourceName& name, const std::vector<std::unique_ptr<ISymbolSource>>& sources) {
164 for (auto& source : sources) {
165 std::unique_ptr<SymbolTable::Symbol> symbol = source->FindByName(name);
166 if (symbol) {
167 return symbol;
168 }
169 }
170 return {};
171}
172
173std::unique_ptr<SymbolTable::Symbol> DefaultSymbolTableDelegate::FindById(
174 ResourceId id, const std::vector<std::unique_ptr<ISymbolSource>>& sources) {
175 for (auto& source : sources) {
176 std::unique_ptr<SymbolTable::Symbol> symbol = source->FindById(id);
177 if (symbol) {
178 return symbol;
179 }
180 }
181 return {};
182}
183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184std::unique_ptr<SymbolTable::Symbol> ResourceTableSymbolSource::FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700185 const ResourceName& name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700186 Maybe<ResourceTable::SearchResult> result = table_->FindResource(name);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 if (!result) {
188 if (name.type == ResourceType::kAttr) {
189 // Recurse and try looking up a private attribute.
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800190 return FindByName(ResourceName(name.package, ResourceType::kAttrPrivate, name.entry));
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 }
192 return {};
193 }
194
195 ResourceTable::SearchResult sr = result.value();
196
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800197 std::unique_ptr<SymbolTable::Symbol> symbol = util::make_unique<SymbolTable::Symbol>();
Adam Lesinski71be7052017-12-12 16:48:07 -0800198 symbol->is_public = (sr.entry->visibility.level == Visibility::Level::kPublic);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199
200 if (sr.package->id && sr.type->id && sr.entry->id) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800201 symbol->id = ResourceId(sr.package->id.value(), sr.type->id.value(), sr.entry->id.value());
Todd Kennedy196dbc32018-07-24 14:37:15 -0700202 symbol->is_dynamic = (sr.package->id.value() == 0);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700203 }
204
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800205 if (name.type == ResourceType::kAttr || name.type == ResourceType::kAttrPrivate) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206 const ConfigDescription kDefaultConfig;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 ResourceConfigValue* config_value = sr.entry->FindValue(kDefaultConfig);
208 if (config_value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209 // This resource has an Attribute.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 if (Attribute* attr = ValueCast<Attribute>(config_value->value.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211 symbol->attribute = std::make_shared<Attribute>(*attr);
212 } else {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700213 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700214 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700215 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700216 }
217 return symbol;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700218}
219
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700220bool AssetManagerSymbolSource::AddAssetPath(const StringPiece& path) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800221 TRACE_CALL();
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800222 if (std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(path.data())) {
223 apk_assets_.push_back(std::move(apk));
224
225 std::vector<const ApkAssets*> apk_assets;
226 for (const std::unique_ptr<const ApkAssets>& apk_asset : apk_assets_) {
227 apk_assets.push_back(apk_asset.get());
228 }
229
230 asset_manager_.SetApkAssets(apk_assets, true /* invalidate_caches */,
231 false /* filter_incompatible_configs */);
232 return true;
233 }
234 return false;
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800235}
236
237std::map<size_t, std::string> AssetManagerSymbolSource::GetAssignedPackageIds() const {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800238 TRACE_CALL();
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800239 std::map<size_t, std::string> package_map;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800240 asset_manager_.ForEachPackage([&package_map](const std::string& name, uint8_t id) -> bool {
241 package_map.insert(std::make_pair(id, name));
242 return true;
243 });
244
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800245 return package_map;
Adam Lesinski64587af2016-02-18 18:33:06 -0800246}
247
Todd Kennedy32512992018-04-25 16:45:59 -0700248bool AssetManagerSymbolSource::IsPackageDynamic(uint32_t packageId) const {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800249 if (packageId == 0) {
250 return true;
251 }
252
253 for (const std::unique_ptr<const ApkAssets>& assets : apk_assets_) {
254 for (const std::unique_ptr<const android::LoadedPackage>& loaded_package
255 : assets->GetLoadedArsc()->GetPackages()) {
256 if (packageId == loaded_package->GetPackageId() && loaded_package->IsDynamic()) {
257 return true;
258 }
259 }
260 }
261
262 return false;
Todd Kennedy32512992018-04-25 16:45:59 -0700263}
264
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265static std::unique_ptr<SymbolTable::Symbol> LookupAttributeInTable(
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800266 android::AssetManager2& am, ResourceId id) {
267 if (am.GetApkAssets().empty()) {
268 return {};
269 }
270
271 const android::ResolvedBag* bag = am.GetBag(id.id);
272 if (bag == nullptr) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700273 return nullptr;
274 }
275
276 // We found a resource.
Adam Lesinskic744ae82017-05-17 19:28:38 -0700277 std::unique_ptr<SymbolTable::Symbol> s = util::make_unique<SymbolTable::Symbol>(id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800279 const size_t count = bag->entry_count;
280 for (uint32_t i = 0; i < count; i++) {
281 if (bag->entries[i].key == android::ResTable_map::ATTR_TYPE) {
282 s->attribute = std::make_shared<Attribute>(bag->entries[i].value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700284 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700286
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700287 if (s->attribute) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800288 for (size_t i = 0; i < count; i++) {
289 const android::ResolvedBag::Entry& map_entry = bag->entries[i];
290 if (Res_INTERNALID(map_entry.key)) {
291 switch (map_entry.key) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 case android::ResTable_map::ATTR_MIN:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700293 s->attribute->min_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 break;
295 case android::ResTable_map::ATTR_MAX:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296 s->attribute->max_int = static_cast<int32_t>(map_entry.value.data);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700297 break;
298 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 continue;
300 }
301
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800302 android::AssetManager2::ResourceName name;
303 if (!am.GetResourceName(map_entry.key, &name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 return nullptr;
305 }
306
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800307 Maybe<ResourceName> parsed_name = ResourceUtils::ToResourceName(name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700308 if (!parsed_name) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700309 return nullptr;
310 }
311
312 Attribute::Symbol symbol;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313 symbol.symbol.name = parsed_name.value();
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800314 symbol.symbol.id = ResourceId(map_entry.key);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 symbol.value = map_entry.value.data;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700316 s->attribute->symbols.push_back(std::move(symbol));
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700317 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700318 }
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800319
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 return s;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700321}
322
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByName(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324 const ResourceName& name) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800325 const std::string mangled_entry = NameMangler::MangleEntry(name.package, name.entry);
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700326
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800327 bool found = false;
328 ResourceId res_id = 0;
Adam Lesinskidc785052017-12-07 15:58:46 -0800329 uint32_t type_spec_flags;
Adam Lesinskidc785052017-12-07 15:58:46 -0800330
331 // There can be mangled resources embedded within other packages. Here we will
332 // look into each package and look-up the mangled name until we find the resource.
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800333 asset_manager_.ForEachPackage([&](const std::string& package_name, uint8_t id) -> bool {
334 ResourceName real_name(name.package, name.type, name.entry);
335
336 if (package_name != name.package) {
337 real_name.entry = mangled_entry;
338 real_name.package = package_name;
Adam Lesinskidc785052017-12-07 15:58:46 -0800339 }
340
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800341 res_id = asset_manager_.GetResourceId(real_name.to_string());
342 if (res_id.is_valid() && asset_manager_.GetResourceFlags(res_id.id, &type_spec_flags)) {
343 found = true;
344 return false;
Adam Lesinskidc785052017-12-07 15:58:46 -0800345 }
Adam Lesinskidc785052017-12-07 15:58:46 -0800346
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800347 return true;
348 });
349
350 if (!found) {
Adam Lesinski64587af2016-02-18 18:33:06 -0800351 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700352 }
353
354 std::unique_ptr<SymbolTable::Symbol> s;
355 if (name.type == ResourceType::kAttr) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800356 s = LookupAttributeInTable(asset_manager_, res_id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700357 } else {
358 s = util::make_unique<SymbolTable::Symbol>();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700359 s->id = res_id;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800360 s->is_dynamic = IsPackageDynamic(ResourceId(res_id).package_id());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700361 }
362
363 if (s) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800364 s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700365 return s;
366 }
367 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700368}
369
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800370static Maybe<ResourceName> GetResourceName(android::AssetManager2& am,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700371 ResourceId id) {
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800372 android::AssetManager2::ResourceName name;
373 if (!am.GetResourceName(id.id, &name)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700374 return {};
375 }
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800376 return ResourceUtils::ToResourceName(name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800377}
378
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700379std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindById(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700380 ResourceId id) {
Adam Lesinskib5dc4bd2017-02-22 19:29:29 -0800381 if (!id.is_valid()) {
382 // Exit early and avoid the error logs from AssetManager.
383 return {};
384 }
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800385
386 if (apk_assets_.empty()) {
387 return {};
388 }
389
390 Maybe<ResourceName> maybe_name = GetResourceName(asset_manager_, id);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700391 if (!maybe_name) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700392 return {};
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700393 }
394
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700395
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800396 uint32_t type_spec_flags = 0;
397 if (!asset_manager_.GetResourceFlags(id.id, &type_spec_flags)) {
398 return {};
399 }
400
401 ResourceName& name = maybe_name.value();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700402 std::unique_ptr<SymbolTable::Symbol> s;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800403 if (name.type == ResourceType::kAttr) {
404 s = LookupAttributeInTable(asset_manager_, id);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700405 } else {
406 s = util::make_unique<SymbolTable::Symbol>();
407 s->id = id;
Ryan Mitchella55dc2e2019-01-24 10:58:23 -0800408 s->is_dynamic = IsPackageDynamic(ResourceId(id).package_id());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700409 }
410
411 if (s) {
Adam Lesinskiceb9b2f2017-02-16 12:05:42 -0800412 s->is_public = (type_spec_flags & android::ResTable_typeSpec::SPEC_PUBLIC) != 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700413 return s;
414 }
415 return {};
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700416}
417
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700418std::unique_ptr<SymbolTable::Symbol> AssetManagerSymbolSource::FindByReference(
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700419 const Reference& ref) {
420 // AssetManager always prefers IDs.
421 if (ref.id) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700422 return FindById(ref.id.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700423 } else if (ref.name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700424 return FindByName(ref.name.value());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700425 }
426 return {};
Adam Lesinski76565542016-03-10 21:55:04 -0800427}
428
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700429} // namespace aapt