SymbolTable.h revision 7656554f91b40bc93bf94c89afcad4a9a8ced884
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
28#include <android-base/macros.h>
29#include <androidfw/AssetManager.h>
30#include <algorithm>
31#include <memory>
32#include <vector>
33
34namespace aapt {
35
36inline android::hash_t hash_type(const ResourceName& name) {
37    std::hash<std::u16string> 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;
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 {
52public:
53    struct Symbol {
54        Maybe<ResourceId> id;
55        std::shared_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
72    /**
73     * Let's the ISymbolSource decide whether looking up by name or ID is faster, if both
74     * are available.
75     */
76    const Symbol* findByReference(const Reference& ref);
77
78private:
79    std::vector<std::unique_ptr<ISymbolSource>> mSources;
80
81    // We use shared_ptr because unique_ptr is not supported and
82    // we need automatic deletion.
83    android::LruCache<ResourceName, std::shared_ptr<Symbol>> mCache;
84    android::LruCache<ResourceId, std::shared_ptr<Symbol>> mIdCache;
85
86    DISALLOW_COPY_AND_ASSIGN(SymbolTable);
87};
88
89/**
90 * An interface that a symbol source implements in order to surface symbol information
91 * to the symbol table.
92 */
93class ISymbolSource {
94public:
95    virtual ~ISymbolSource() = default;
96
97    virtual std::unique_ptr<SymbolTable::Symbol> findByName(const ResourceName& name) = 0;
98    virtual std::unique_ptr<SymbolTable::Symbol> findById(ResourceId id) = 0;
99
100    /**
101     * Default implementation tries the name if it exists, else the ID.
102     */
103    virtual std::unique_ptr<SymbolTable::Symbol> findByReference(const Reference& ref) {
104        if (ref.name) {
105            return findByName(ref.name.value());
106        } else if (ref.id) {
107            return findById(ref.id.value());
108        }
109        return {};
110    }
111};
112
113/**
114 * Exposes the resources in a ResourceTable as symbols for SymbolTable.
115 * Instances of this class must outlive the encompassed ResourceTable.
116 * Lookups by ID are ignored.
117 */
118class ResourceTableSymbolSource : public ISymbolSource {
119public:
120    explicit ResourceTableSymbolSource(ResourceTable* table) : mTable(table) {
121    }
122
123    std::unique_ptr<SymbolTable::Symbol> findByName(const ResourceName& name) override;
124
125    std::unique_ptr<SymbolTable::Symbol> findById(ResourceId id) override {
126        return {};
127    }
128
129private:
130    ResourceTable* mTable;
131
132    DISALLOW_COPY_AND_ASSIGN(ResourceTableSymbolSource);
133};
134
135class AssetManagerSymbolSource : public ISymbolSource {
136public:
137    AssetManagerSymbolSource() = default;
138
139    bool addAssetPath(const StringPiece& path);
140
141    std::unique_ptr<SymbolTable::Symbol> findByName(const ResourceName& name) override;
142    std::unique_ptr<SymbolTable::Symbol> findById(ResourceId id) override;
143    std::unique_ptr<SymbolTable::Symbol> findByReference(const Reference& ref) override;
144
145private:
146    android::AssetManager mAssets;
147
148    DISALLOW_COPY_AND_ASSIGN(AssetManagerSymbolSource);
149};
150
151} // namespace aapt
152
153#endif /* AAPT_PROCESS_SYMBOLTABLE_H */
154