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_ENTRANCE_HAP_ASSET_PROVIDER_IMPL_H
17 #define FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_HAP_ASSET_PROVIDER_IMPL_H
18 
19 #include <mutex>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 
24 #include "extractor.h"
25 
26 #include "base/resource/asset_manager.h"
27 #include "base/utils/macros.h"
28 #include "core/common/asset_manager_impl.h"
29 #include "core/common/asset_mapping.h"
30 
31 namespace OHOS::Ace {
32 class ACE_EXPORT HapAssetProviderImpl : public AssetProviderImpl {
33     DECLARE_ACE_TYPE(HapAssetProviderImpl, AssetProviderImpl);
34 public:
35     HapAssetProviderImpl() = default;
36     ~HapAssetProviderImpl() override = default;
37 
38     bool Initialize(const std::string& hapPath, const std::vector<std::string>& assetBasePaths, bool useCache = true);
39     std::unique_ptr<AssetMapping> GetAsMapping(const std::string& assetName) const override;
40     std::vector<std::unique_ptr<AssetMapping>> GetAsMappingFromI18n(const std::string& assetName) const override;
41     bool IsValid() const override;
42     std::string GetAssetPath(const std::string& assetName, bool isAddHapPath) override;
43     void GetAssetList(const std::string& path, std::vector<std::string>& assetList) override;
44     bool GetFileInfo(const std::string& fileName, MediaFileInfo& fileInfo) const override;
45     void Reload() override;
46 
47 private:
48     class HapAssetImplMapping : public AssetMapping {
49     public:
HapAssetImplMapping(const std::ostringstream & ostream)50         explicit HapAssetImplMapping(const std::ostringstream& ostream)
51         {
52             const std::string& content = ostream.str();
53             data_.assign(content.data(), content.data() + content.size());
54         }
55 
56         ~HapAssetImplMapping() override = default;
57 
GetSize()58         size_t GetSize() const override
59         {
60             return data_.size();
61         }
62 
GetAsset()63         const uint8_t* GetAsset() const override
64         {
65             return data_.data();
66         }
67 
68     private:
69         std::vector<uint8_t> data_;
70     };
71 
72     mutable std::mutex mutex_;
73     std::string hapPath_;
74     std::string loadPath_;
75     std::shared_ptr<AbilityBase::Extractor> runtimeExtractor_;
76     std::vector<std::string> assetBasePaths_;
77 };
78 } // namespace OHOS::Ace
79 #endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_HAP_ASSET_PROVIDER_IMPL_H
80