124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski/*
224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * Copyright (C) 2015 The Android Open Source Project
324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski *
424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * you may not use this file except in compliance with the License.
624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * You may obtain a copy of the License at
724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski *
824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski *
1024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * Unless required by applicable law or agreed to in writing, software
1124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
1224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * See the License for the specific language governing permissions and
1424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * limitations under the License.
1524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski */
1624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
1724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#ifndef AAPT_RESOURCE_TABLE_RESOLVER_H
1824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#define AAPT_RESOURCE_TABLE_RESOLVER_H
1924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
2024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include "Maybe.h"
2124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include "Resolver.h"
2224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include "Resource.h"
2324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include "ResourceTable.h"
2424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include "ResourceValues.h"
2524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
2624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include <androidfw/AssetManager.h>
2724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include <memory>
2824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include <vector>
2924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#include <unordered_set>
3024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
3124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinskinamespace aapt {
3224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
3324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski/**
3424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski * Encapsulates the search of library sources as well as the local ResourceTable.
3524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski */
3624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinskiclass ResourceTableResolver : public IResolver {
3724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinskipublic:
3824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    /**
3924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski     * Creates a resolver with a local ResourceTable and an AssetManager
4024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski     * loaded with library packages.
4124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski     */
42330edcdf1316ed599fe0eb16a64330821fd92f18Adam Lesinski    ResourceTableResolver(
43330edcdf1316ed599fe0eb16a64330821fd92f18Adam Lesinski            std::shared_ptr<const ResourceTable> table,
44330edcdf1316ed599fe0eb16a64330821fd92f18Adam Lesinski            const std::vector<std::shared_ptr<const android::AssetManager>>& sources);
4524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
4624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    ResourceTableResolver(const ResourceTableResolver&) = delete; // Not copyable.
4724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
4824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    virtual Maybe<ResourceId> findId(const ResourceName& name) override;
4924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
5024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    virtual Maybe<Entry> findAttribute(const ResourceName& name) override;
5124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
5224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    virtual Maybe<ResourceName> findName(ResourceId resId) override;
5324aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
5424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinskiprivate:
5524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    struct CacheEntry {
5624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski        ResourceId id;
5724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski        std::unique_ptr<Attribute> attr;
5824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    };
5924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
6024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    const CacheEntry* buildCacheEntry(const ResourceName& name);
6124aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
6224aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    std::shared_ptr<const ResourceTable> mTable;
63330edcdf1316ed599fe0eb16a64330821fd92f18Adam Lesinski    std::vector<std::shared_ptr<const android::AssetManager>> mSources;
6424aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    std::map<ResourceName, CacheEntry> mCache;
6524aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski    std::unordered_set<std::u16string> mIncludedPackages;
6624aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski};
6724aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
6824aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski} // namespace aapt
6924aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski
7024aad163bc88cb10d2275385e9afc3de7f342d65Adam Lesinski#endif // AAPT_RESOURCE_TABLE_RESOLVER_H
71