1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_ADAPTER_OHOS_CPP_FILE_ASSET_PROVIDER_IMPL_H
17 #define FOUNDATION_ACE_ADAPTER_OHOS_CPP_FILE_ASSET_PROVIDER_IMPL_H
18 
19 #include <map>
20 #include <sstream>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
25 #include "base/resource/asset_manager.h"
26 #include "base/utils/macros.h"
27 #include "core/common/asset_manager_impl.h"
28 #include "core/common/asset_mapping.h"
29 
30 namespace OHOS::Ace {
31 class ACE_EXPORT FileAssetProviderImpl : public AssetProviderImpl {
32     DECLARE_ACE_TYPE(FileAssetProviderImpl, AssetProviderImpl);
33 public:
34     FileAssetProviderImpl() = default;
35     ~FileAssetProviderImpl() override = default;
36 
37     bool Initialize(const std::string& packagePath, const std::vector<std::string>& assetBasePaths);
38     std::unique_ptr<AssetMapping> GetAsMapping(const std::string& assetName) const override;
GetAsMappingFromI18n(const std::string & assetName)39     std::vector<std::unique_ptr<AssetMapping>> GetAsMappingFromI18n(const std::string& assetName) const override
40     {
41         return {};
42     }
43     bool IsValid() const override;
44     std::string GetAssetPath(const std::string& assetName, bool isAddHapPath) override;
45     void GetAssetList(const std::string& path, std::vector<std::string>& assetList) override;
46 
47 private:
48     class FileAssetImplMapping : public AssetMapping {
49     public:
FileAssetImplMapping(std::unique_ptr<uint8_t[]> data,size_t size)50         FileAssetImplMapping(std::unique_ptr<uint8_t[]> data, size_t size) : data_(std::move(data)), size_(size) {}
51         ~FileAssetImplMapping() override = default;
52 
GetSize()53         size_t GetSize() const override
54         {
55             return size_;
56         }
57 
GetAsset()58         const uint8_t* GetAsset() const override
59         {
60             return data_.get();
61         }
62 
63     private:
64         std::unique_ptr<uint8_t[]> data_;
65         size_t size_ = 0;
66     };
67 
68     mutable std::mutex mutex_;
69     std::string packagePath_;
70     std::vector<std::string> assetBasePaths_;
71 };
72 } // namespace OHOS::Ace
73 #endif // FOUNDATION_ACE_ADAPTER_OHOS_CPP_FILE_ASSET_PROVIDER_IMPL_H
74