1 /* 2 * Copyright (c) 2021 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_COMPONENTS_PLUGIN_FILE_ASSET_PROVIDER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PLUGIN_FILE_ASSET_PROVIDER_H 18 19 #include <map> 20 #include <string> 21 #include <unordered_map> 22 #include <vector> 23 24 #include "flutter/assets/asset_resolver.h" 25 #include "flutter/fml/mapping.h" 26 27 #include "base/resource/asset_manager.h" 28 #include "base/utils/macros.h" 29 #include "core/common/flutter/flutter_asset_manager.h" 30 31 namespace OHOS::Ace::Plugin { 32 class ACE_EXPORT FileAssetProvider : public FlutterAssetProvider { 33 DECLARE_ACE_TYPE(FileAssetProvider, FlutterAssetProvider); 34 35 public: 36 FileAssetProvider() = default; 37 ~FileAssetProvider() override = default; 38 39 bool Initialize(const std::string& packagePath, const std::vector<std::string>& assetBasePaths); 40 41 std::unique_ptr<fml::Mapping> GetAsMapping(const std::string& assetName) const override; 42 43 bool IsValid() const override; 44 45 std::string GetAssetPath(const std::string& assetName, bool isAddHapPath) override; 46 47 void GetAssetList(const std::string& path, std::vector<std::string>& assetList) override; 48 49 private: 50 class FileAssetMapping : public fml::Mapping { 51 public: FileAssetMapping(std::unique_ptr<uint8_t[]> data,size_t size)52 FileAssetMapping(std::unique_ptr<uint8_t[]> data, size_t size) : data_(std::move(data)), size_(size) {} 53 ~FileAssetMapping()54 ~FileAssetMapping() override {} 55 GetSize()56 size_t GetSize() const override 57 { 58 return size_; 59 } 60 GetMapping()61 const uint8_t* GetMapping() const override 62 { 63 return data_.get(); 64 } 65 66 private: 67 std::unique_ptr<uint8_t[]> data_ = nullptr; 68 size_t size_ = 0; 69 }; 70 71 mutable std::mutex mutex_; 72 std::string packagePath_; 73 std::vector<std::string> assetBasePaths_; 74 }; 75 } // namespace OHOS::Ace::Plugin 76 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PLUGIN_FILE_ASSET_PROVIDER_H 77