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 bool IsValid() const override; 41 std::string GetAssetPath(const std::string& assetName, bool isAddHapPath) override; 42 void GetAssetList(const std::string& path, std::vector<std::string>& assetList) override; 43 bool GetFileInfo(const std::string& fileName, MediaFileInfo& fileInfo) const override; 44 void Reload() override; 45 46 private: 47 class HapAssetImplMapping : public AssetMapping { 48 public: HapAssetImplMapping(const std::ostringstream & ostream)49 explicit HapAssetImplMapping(const std::ostringstream& ostream) 50 { 51 const std::string& content = ostream.str(); 52 data_.assign(content.data(), content.data() + content.size()); 53 } 54 55 ~HapAssetImplMapping() override = default; 56 GetSize()57 size_t GetSize() const override 58 { 59 return data_.size(); 60 } 61 GetAsset()62 const uint8_t* GetAsset() const override 63 { 64 return data_.data(); 65 } 66 67 private: 68 std::vector<uint8_t> data_; 69 }; 70 71 mutable std::mutex mutex_; 72 std::string hapPath_; 73 std::string loadPath_; 74 std::shared_ptr<AbilityBase::Extractor> runtimeExtractor_; 75 std::vector<std::string> assetBasePaths_; 76 }; 77 } // namespace OHOS::Ace 78 #endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_HAP_ASSET_PROVIDER_IMPL_H 79