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_FRAMEWORKS_CORE_COMMON_ROSEN_ROSEN_ASSET_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_ROSEN_ROSEN_ASSET_MANAGER_H
18 
19 #include <deque>
20 #include <vector>
21 
22 #include "base/log/event_report.h"
23 #include "base/memory/ace_type.h"
24 #include "base/resource/asset_manager.h"
25 #include "base/utils/macros.h"
26 
27 namespace OHOS::Ace {
28 
29 class RSAsset final : public Asset {
30 public:
RSAsset(std::unique_ptr<char[]> data,size_t size)31     RSAsset(std::unique_ptr<char[]> data, size_t size)
32         : data_(std::move(data)), size_(size)
33     {
34     }
35 
GetSize()36     size_t GetSize() const override
37     {
38         return size_;
39     }
40 
GetData()41     const uint8_t* GetData() const override
42     {
43         return reinterpret_cast<uint8_t *>(data_.get());
44     }
45 
46 private:
47     std::unique_ptr<char[]> data_ = nullptr;
48     size_t size_ = 0;
49 };
50 
51 class RSAssetProvider : public AssetProvider {
52     DECLARE_ACE_TYPE(RSAssetProvider, AssetProvider);
53 
54 public:
55     virtual RefPtr<Asset> GetAsset(const std::string &assetName) const = 0;
56 };
57 
58 class ACE_EXPORT RSAssetManager final : public AssetManager {
59     DECLARE_ACE_TYPE(RSAssetManager, AssetManager);
60 
61 public:
62     RSAssetManager() = default;
63     ~RSAssetManager() override = default;
64 
PushFront(RefPtr<AssetProvider> provider)65     void PushFront(RefPtr<AssetProvider> provider) override
66     {
67         if (!provider || !provider->IsValid()) {
68             return;
69         }
70         providers_.push_front(std::move(provider));
71     }
72 
PushBack(RefPtr<AssetProvider> provider)73     void PushBack(RefPtr<AssetProvider> provider) override
74     {
75         if (!provider || !provider->IsValid()) {
76             return;
77         }
78         providers_.push_back(std::move(provider));
79     }
80 
81     RefPtr<Asset> GetAsset(const std::string& assetName) override;
82 
GetAssetFromI18n(const std::string & assetName)83     std::vector<RefPtr<Asset>> GetAssetFromI18n(const std::string& assetName) override
84     {
85         return {};
86     }
87 
88     std::string GetAssetPath(const std::string& assetName, bool isAddHapPath) override;
89 
SetLibPath(const std::string & appLibPathKey,const std::vector<std::string> & packagePath)90     void SetLibPath(const std::string& appLibPathKey, const std::vector<std::string>& packagePath) override
91     {
92         appLibPathKey_ = appLibPathKey;
93         packagePath_ = packagePath;
94     }
95 
GetLibPath()96     std::vector<std::string> GetLibPath() const override
97     {
98         return packagePath_;
99     }
100 
GetAppLibPathKey()101     std::string GetAppLibPathKey() const override
102     {
103         return appLibPathKey_;
104     }
105 
106     void GetAssetList(const std::string& path, std::vector<std::string>& assetList) const override;
107 
108     bool GetFileInfo(const std::string& fileName, MediaFileInfo& fileInfo) const override;
109 
110 private:
111     std::deque<RefPtr<AssetProvider>> providers_;
112     std::vector<std::string> packagePath_;
113     std::string appLibPathKey_;
114 };
115 
116 } // namespace OHOS::Ace
117 
118 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_ROSEN_ROSEN_ASSET_MANAGER_H
119