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