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_IMAGE_IMAGE_LOADER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_LOADER_H 18 19 #include <regex> 20 #include <string> 21 22 #include "third_party/skia/include/core/SkImage.h" 23 24 #include "base/geometry/size.h" 25 #include "base/memory/ace_type.h" 26 #include "base/resource/internal_resource.h" 27 #include "base/resource/shared_image_manager.h" 28 #include "core/components/common/layout/constants.h" 29 #include "core/image/image_source_info.h" 30 #include "core/pipeline/pipeline_context.h" 31 32 namespace OHOS::Ace { 33 34 class ImageLoader : public virtual AceType { 35 DECLARE_ACE_TYPE(ImageLoader, AceType); 36 37 public: 38 virtual sk_sp<SkData> LoadImageData( 39 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineContext> context = nullptr) = 0; 40 41 static std::string RemovePathHead(const std::string& uri); 42 static RefPtr<ImageLoader> CreateImageLoader(const ImageSourceInfo& imageSourceInfo); 43 static sk_sp<SkData> LoadDataFromCachedFile(const std::string& uri); 44 }; 45 46 // File image provider: read image from file. 47 class FileImageLoader : public ImageLoader { 48 public: 49 FileImageLoader() = default; 50 ~FileImageLoader() override = default; 51 sk_sp<SkData> LoadImageData( 52 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineContext> context = nullptr) override; 53 }; 54 55 // data provider image loader. 56 class DataProviderImageLoader : public ImageLoader { 57 public: 58 DataProviderImageLoader() = default; 59 ~DataProviderImageLoader() override = default; 60 sk_sp<SkData> LoadImageData( 61 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineContext> context = nullptr) override; 62 }; 63 64 class AssetImageLoader final : public ImageLoader { 65 public: 66 AssetImageLoader() = default; 67 ~AssetImageLoader() override = default; 68 sk_sp<SkData> LoadImageData( 69 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineContext> context = nullptr) override; 70 std::string LoadJsonData(const std::string& src, const WeakPtr<PipelineContext> context = nullptr); 71 }; 72 73 // Network image provider: read image from network. 74 class NetworkImageLoader final : public ImageLoader { 75 public: 76 NetworkImageLoader() = default; 77 ~NetworkImageLoader() override = default; 78 sk_sp<SkData> LoadImageData( 79 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineContext> context = nullptr) override; 80 }; 81 82 class InternalImageLoader final : public ImageLoader { 83 public: 84 InternalImageLoader() = default; 85 ~InternalImageLoader() override = default; 86 sk_sp<SkData> LoadImageData( 87 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineContext> context = nullptr) override; 88 }; 89 90 class Base64ImageLoader final : public ImageLoader { 91 public: 92 Base64ImageLoader() = default; 93 ~Base64ImageLoader() override = default; 94 static std::string GetBase64ImageCode(const std::string& uri, size_t& imagSize); 95 static size_t GetBase64ImageSize(const std::string& code); 96 sk_sp<SkData> LoadImageData( 97 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineContext> context = nullptr) override; 98 }; 99 100 class ResourceImageLoader final : public ImageLoader { 101 public: 102 ResourceImageLoader() = default; 103 ~ResourceImageLoader() override = default; 104 sk_sp<SkData> LoadImageData( 105 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineContext> context = nullptr) override; 106 107 private: 108 bool GetResourceId(const std::string& uri, const RefPtr<ThemeConstants>& themeContants, uint32_t& resId) const; 109 bool GetResourceId(const std::string& uri, const RefPtr<ThemeConstants>& themeContants, std::string& path) const; 110 }; 111 112 } // namespace OHOS::Ace 113 114 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_LOADER_H