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_RESOURCE_TABLE_RESOLVER_H
18#define AAPT_RESOURCE_TABLE_RESOLVER_H
19
20#include "Maybe.h"
21#include "Resolver.h"
22#include "Resource.h"
23#include "ResourceTable.h"
24#include "ResourceValues.h"
25
26#include <androidfw/AssetManager.h>
27#include <memory>
28#include <vector>
29#include <unordered_set>
30
31namespace aapt {
32
33/**
34 * Encapsulates the search of library sources as well as the local ResourceTable.
35 */
36class ResourceTableResolver : public IResolver {
37public:
38    /**
39     * Creates a resolver with a local ResourceTable and an AssetManager
40     * loaded with library packages.
41     */
42    ResourceTableResolver(
43            std::shared_ptr<const ResourceTable> table,
44            const std::vector<std::shared_ptr<const android::AssetManager>>& sources);
45
46    ResourceTableResolver(const ResourceTableResolver&) = delete; // Not copyable.
47
48    virtual Maybe<ResourceId> findId(const ResourceName& name) override;
49
50    virtual Maybe<Entry> findAttribute(const ResourceName& name) override;
51
52    virtual Maybe<ResourceName> findName(ResourceId resId) override;
53
54private:
55    struct CacheEntry {
56        ResourceId id;
57        std::unique_ptr<Attribute> attr;
58    };
59
60    const CacheEntry* buildCacheEntry(const ResourceName& name);
61
62    std::shared_ptr<const ResourceTable> mTable;
63    std::vector<std::shared_ptr<const android::AssetManager>> mSources;
64    std::map<ResourceName, CacheEntry> mCache;
65    std::unordered_set<std::u16string> mIncludedPackages;
66};
67
68} // namespace aapt
69
70#endif // AAPT_RESOURCE_TABLE_RESOLVER_H
71