SymbolTable.h revision ce5e56e243d262a9b65459c3bd0bb9eaadd40628
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#ifndef AAPT_PROCESS_SYMBOLTABLE_H 18#define AAPT_PROCESS_SYMBOLTABLE_H 19 20#include <algorithm> 21#include <memory> 22#include <vector> 23 24#include "android-base/macros.h" 25#include "androidfw/AssetManager.h" 26#include "utils/JenkinsHash.h" 27#include "utils/LruCache.h" 28 29#include "Resource.h" 30#include "ResourceTable.h" 31#include "ResourceValues.h" 32#include "util/Util.h" 33 34namespace aapt { 35 36inline android::hash_t hash_type(const ResourceName& name) { 37 std::hash<std::string> str_hash; 38 android::hash_t hash = 0; 39 hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.package)); 40 hash = android::JenkinsHashMix(hash, (uint32_t)name.type); 41 hash = android::JenkinsHashMix(hash, (uint32_t)str_hash(name.entry)); 42 return hash; 43} 44 45inline android::hash_t hash_type(const ResourceId& id) { 46 return android::hash_type(id.id); 47} 48 49class ISymbolSource; 50 51class SymbolTable { 52 public: 53 struct Symbol { 54 Symbol() : Symbol(Maybe<ResourceId>{}) {} 55 56 explicit Symbol(const Maybe<ResourceId>& i) : Symbol(i, nullptr) {} 57 58 Symbol(const Maybe<ResourceId>& i, const std::shared_ptr<Attribute>& attr) 59 : Symbol(i, attr, false) {} 60 61 Symbol(const Maybe<ResourceId>& i, const std::shared_ptr<Attribute>& attr, 62 bool pub) 63 : id(i), attribute(attr), is_public(pub) {} 64 65 Symbol(const Symbol&) = default; 66 Symbol(Symbol&&) = default; 67 Symbol& operator=(const Symbol&) = default; 68 Symbol& operator=(Symbol&&) = default; 69 70 Maybe<ResourceId> id; 71 std::shared_ptr<Attribute> attribute; 72 bool is_public = false; 73 }; 74 75 SymbolTable() : cache_(200), id_cache_(200) {} 76 77 void AppendSource(std::unique_ptr<ISymbolSource> source); 78 void PrependSource(std::unique_ptr<ISymbolSource> source); 79 80 /** 81 * Never hold on to the result between calls to FindByName or FindById. The 82 * results stored in a cache which may evict entries. 83 */ 84 const Symbol* FindByName(const ResourceName& name); 85 const Symbol* FindById(const ResourceId& id); 86 87 /** 88 * Let's the ISymbolSource decide whether looking up by name or ID is faster, 89 * if both are available. 90 */ 91 const Symbol* FindByReference(const Reference& ref); 92 93 private: 94 std::vector<std::unique_ptr<ISymbolSource>> sources_; 95 96 // We use shared_ptr because unique_ptr is not supported and 97 // we need automatic deletion. 98 android::LruCache<ResourceName, std::shared_ptr<Symbol>> cache_; 99 android::LruCache<ResourceId, std::shared_ptr<Symbol>> id_cache_; 100 101 DISALLOW_COPY_AND_ASSIGN(SymbolTable); 102}; 103 104/** 105 * An interface that a symbol source implements in order to surface symbol 106 * information 107 * to the symbol table. 108 */ 109class ISymbolSource { 110 public: 111 virtual ~ISymbolSource() = default; 112 113 virtual std::unique_ptr<SymbolTable::Symbol> FindByName( 114 const ResourceName& name) = 0; 115 virtual std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) = 0; 116 117 /** 118 * Default implementation tries the name if it exists, else the ID. 119 */ 120 virtual std::unique_ptr<SymbolTable::Symbol> FindByReference( 121 const Reference& ref) { 122 if (ref.name) { 123 return FindByName(ref.name.value()); 124 } else if (ref.id) { 125 return FindById(ref.id.value()); 126 } 127 return {}; 128 } 129}; 130 131/** 132 * Exposes the resources in a ResourceTable as symbols for SymbolTable. 133 * Instances of this class must outlive the encompassed ResourceTable. 134 * Lookups by ID are ignored. 135 */ 136class ResourceTableSymbolSource : public ISymbolSource { 137 public: 138 explicit ResourceTableSymbolSource(ResourceTable* table) : table_(table) {} 139 140 std::unique_ptr<SymbolTable::Symbol> FindByName( 141 const ResourceName& name) override; 142 143 std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override { 144 return {}; 145 } 146 147 private: 148 ResourceTable* table_; 149 150 DISALLOW_COPY_AND_ASSIGN(ResourceTableSymbolSource); 151}; 152 153class AssetManagerSymbolSource : public ISymbolSource { 154 public: 155 AssetManagerSymbolSource() = default; 156 157 bool AddAssetPath(const StringPiece& path); 158 159 std::unique_ptr<SymbolTable::Symbol> FindByName( 160 const ResourceName& name) override; 161 std::unique_ptr<SymbolTable::Symbol> FindById(ResourceId id) override; 162 std::unique_ptr<SymbolTable::Symbol> FindByReference( 163 const Reference& ref) override; 164 165 private: 166 android::AssetManager assets_; 167 168 DISALLOW_COPY_AND_ASSIGN(AssetManagerSymbolSource); 169}; 170 171} // namespace aapt 172 173#endif /* AAPT_PROCESS_SYMBOLTABLE_H */ 174