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_BASE_RESOURCE_SHARED_IMAGE_MANAGER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_RESOURCE_SHARED_IMAGE_MANAGER_H 18 19 #include <cstddef> 20 #include <cstdint> 21 #include <functional> 22 #include <mutex> 23 #include <new> 24 #include <set> 25 #include <string> 26 #include <unordered_map> 27 #include <vector> 28 29 #include "base/memory/ace_type.h" 30 #include "base/thread/cancelable_callback.h" 31 #include "base/thread/task_executor.h" 32 #include "base/utils/noncopyable.h" 33 #include "base/utils/system_properties.h" 34 35 namespace OHOS::Ace { 36 37 using SharedImage = std::vector<uint8_t>; 38 using SharedImageMap = std::unordered_map<std::string, SharedImage>; 39 40 class ImageProviderLoader; 41 using ProviderMapToReload = std::unordered_map<std::string, std::set<WeakPtr<ImageProviderLoader>>>; 42 43 class SharedImageManager : public AceType { 44 DECLARE_ACE_TYPE(SharedImageManager, AceType); 45 46 public: SharedImageManager(const RefPtr<TaskExecutor> & taskExecutor)47 explicit SharedImageManager(const RefPtr<TaskExecutor>& taskExecutor) : 48 sharedImageTotalSize_(0), taskExecutor_(taskExecutor) { 49 sharedImageCacheThreshold_ = SystemProperties::getFormSharedImageCacheThreshold(); 50 LOGI("getFormSharedImageCacheThreshold %{public}d", sharedImageCacheThreshold_); 51 } 52 ~SharedImageManager() override = default; 53 void AddSharedImage(const std::string& name, SharedImage&& sharedImage); 54 void AddPictureNamesToReloadMap(std::string&& name); 55 bool FindImageInSharedImageMap(const std::string& name, const WeakPtr<ImageProviderLoader>& providerWp); 56 GetSharedImageMap()57 const SharedImageMap& GetSharedImageMap() const 58 { 59 return sharedImageMap_; 60 } 61 62 bool Remove(const std::string& name); 63 64 // return true if successfully registered 65 bool RegisterLoader(const std::string& name, const WeakPtr<ImageProviderLoader>& providerWp); 66 67 private: 68 void PostDelayedTaskToClearImageData(const std::string& name, size_t dataSize); 69 std::function<void()> GenerateClearImageDataCallback(const std::string& name, size_t dataSize); 70 bool UpdateImageMap(const std::string& name, const SharedImage& sharedImage); 71 72 std::mutex sharedImageMapMutex_; 73 std::mutex providerMapMutex_; 74 std::mutex cancelableCallbackMapMutex_; 75 SharedImageMap sharedImageMap_; 76 size_t sharedImageTotalSize_; 77 int32_t sharedImageCacheThreshold_; 78 ProviderMapToReload providerMapToReload_; 79 std::function<void()> clearImageDataCallback_; 80 std::unordered_map<std::string, CancelableCallback<void()>> cancelableCallbackMap_; 81 WeakPtr<TaskExecutor> taskExecutor_; 82 }; 83 84 class ImageProviderLoader : public virtual AceType { 85 DECLARE_ACE_TYPE(ImageProviderLoader, AceType); 86 87 public: 88 ~ImageProviderLoader() override = default; 89 virtual void UpdateData(const std::string& uri, const std::vector<uint8_t>& memData) = 0; 90 91 protected: 92 ImageProviderLoader() = default; 93 94 ACE_DISALLOW_COPY_AND_MOVE(ImageProviderLoader); 95 }; 96 97 } // namespace OHOS::Ace 98 99 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_RESOURCE_SHARED_IMAGE_MANAGER_H 100