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) { |
| 37 | std::hash<std::u16string> strHash; |
| 38 | android::hash_t hash = 0; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 39 | hash = android::JenkinsHashMix(hash, (uint32_t) strHash(name.package)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 40 | hash = android::JenkinsHashMix(hash, (uint32_t) name.type); |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 41 | hash = android::JenkinsHashMix(hash, (uint32_t) strHash(name.entry)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | return hash; |
| 43 | } |
| 44 | |
| 45 | inline android::hash_t hash_type(const ResourceId& id) { |
| 46 | return android::hash_type(id.id); |
| 47 | } |
| 48 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 49 | class ISymbolSource; |
| 50 | |
| 51 | class SymbolTable { |
| 52 | public: |
| 53 | struct Symbol { |
| 54 | Maybe<ResourceId> id; |
| 55 | std::unique_ptr<Attribute> attribute; |
| 56 | bool isPublic; |
| 57 | }; |
| 58 | |
| 59 | SymbolTable() : mCache(200), mIdCache(200) { |
| 60 | } |
| 61 | |
| 62 | void appendSource(std::unique_ptr<ISymbolSource> source); |
| 63 | void prependSource(std::unique_ptr<ISymbolSource> source); |
| 64 | |
| 65 | /** |
| 66 | * Never hold on to the result between calls to findByName or findById. The results |
| 67 | * are typically stored in a cache which may evict entries. |
| 68 | */ |
| 69 | const Symbol* findByName(const ResourceName& name); |
| 70 | const Symbol* findById(ResourceId id); |
| 71 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 72 | private: |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 73 | std::vector<std::unique_ptr<ISymbolSource>> mSources; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 74 | |
| 75 | // We use shared_ptr because unique_ptr is not supported and |
| 76 | // we need automatic deletion. |
| 77 | android::LruCache<ResourceName, std::shared_ptr<Symbol>> mCache; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 78 | android::LruCache<ResourceId, std::shared_ptr<Symbol>> mIdCache; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 79 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 80 | DISALLOW_COPY_AND_ASSIGN(SymbolTable); |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * An interface that a symbol source implements in order to surface symbol information |
| 85 | * to the symbol table. |
| 86 | */ |
| 87 | class ISymbolSource { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 88 | public: |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 89 | virtual ~ISymbolSource() = default; |
| 90 | |
| 91 | virtual std::unique_ptr<SymbolTable::Symbol> findByName(const ResourceName& name) = 0; |
| 92 | virtual std::unique_ptr<SymbolTable::Symbol> findById(ResourceId id) = 0; |
| 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * Exposes the resources in a ResourceTable as symbols for SymbolTable. |
| 97 | * Instances of this class must outlive the encompassed ResourceTable. |
| 98 | * Lookups by ID are ignored. |
| 99 | */ |
| 100 | class ResourceTableSymbolSource : public ISymbolSource { |
| 101 | public: |
| 102 | explicit ResourceTableSymbolSource(ResourceTable* table) : mTable(table) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 105 | std::unique_ptr<SymbolTable::Symbol> findByName(const ResourceName& name) override; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 106 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 107 | std::unique_ptr<SymbolTable::Symbol> findById(ResourceId id) override { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 108 | return {}; |
| 109 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 110 | |
| 111 | private: |
| 112 | ResourceTable* mTable; |
| 113 | |
| 114 | DISALLOW_COPY_AND_ASSIGN(ResourceTableSymbolSource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 117 | class AssetManagerSymbolSource : public ISymbolSource { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 118 | public: |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 119 | AssetManagerSymbolSource() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 120 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 121 | bool addAssetPath(const StringPiece& path); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 122 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 123 | std::unique_ptr<SymbolTable::Symbol> findByName(const ResourceName& name) override; |
| 124 | std::unique_ptr<SymbolTable::Symbol> findById(ResourceId id) override; |
| 125 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 126 | private: |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 127 | android::AssetManager mAssets; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 128 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 129 | DISALLOW_COPY_AND_ASSIGN(AssetManagerSymbolSource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | } // namespace aapt |
| 133 | |
| 134 | #endif /* AAPT_PROCESS_SYMBOLTABLE_H */ |