• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <mutex>
20 #include <set>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include "base/memory/ace_type.h"
25 #include "base/thread/cancelable_callback.h"
26 #include "base/thread/task_executor.h"
27 
28 namespace OHOS::Ace {
29 
30 using SharedImage = std::vector<uint8_t>;
31 using SharedImageMap = std::unordered_map<std::string, SharedImage>;
32 
33 class ImageProviderLoader;
34 using ProviderMapToReload = std::unordered_map<std::string, std::set<WeakPtr<ImageProviderLoader>>>;
35 
36 class SharedImageManager : public AceType {
37     DECLARE_ACE_TYPE(SharedImageManager, AceType);
38 
39 public:
SharedImageManager(const RefPtr<TaskExecutor> & taskExecutor)40     explicit SharedImageManager(const RefPtr<TaskExecutor>& taskExecutor) : taskExecutor_(taskExecutor) {}
41     ~SharedImageManager() override = default;
42     void AddSharedImage(const std::string& name, SharedImage&& sharedImage);
43     void AddPictureNamesToReloadMap(std::string&& name);
44     bool AddProviderToReloadMap(const std::string& name, const WeakPtr<ImageProviderLoader>& providerWp);
45     bool FindImageInSharedImageMap(const std::string& name, const WeakPtr<ImageProviderLoader>& providerWp);
46 
GetSharedImageMap()47     const SharedImageMap& GetSharedImageMap() const
48     {
49         return sharedImageMap_;
50     }
51 
Remove(const std::string & name)52     bool Remove(const std::string& name)
53     {
54         int res = static_cast<int>(sharedImageMap_.erase(name));
55         return (res != 0);
56     }
57 
IsResourceToReload(const std::string & name)58     bool IsResourceToReload(const std::string& name)
59     {
60         std::lock_guard<std::mutex> lockProviderMap(providerMapMutex_);
61         return providerMapToReload_.find(name) != providerMapToReload_.end();
62     }
63 
64 private:
65     void PostDelayedTaskToClearImageData(const std::string& name, size_t dataSize);
66     std::function<void()> GenerateClearImageDataCallback(const std::string& name, size_t dataSize);
67 
68     std::mutex sharedImageMapMutex_;
69     std::mutex providerMapMutex_;
70     std::mutex cancelableCallbackMapMutex_;
71     SharedImageMap sharedImageMap_;
72     ProviderMapToReload providerMapToReload_;
73     std::function<void()> clearImageDataCallback_;
74     std::unordered_map<std::string, CancelableCallback<void()>> cancelableCallbackMap_;
75     RefPtr<TaskExecutor> taskExecutor_;
76 };
77 
78 class ImageProviderLoader : public virtual AceType {
79     DECLARE_ACE_TYPE(ImageProviderLoader, AceType);
80 
81 public:
82     ~ImageProviderLoader() override = default;
83     virtual void UpdateData(const std::string& uri, const std::vector<uint8_t>& memData) = 0;
84 
85 protected:
86     ImageProviderLoader() = default;
87 
88     ACE_DISALLOW_COPY_AND_MOVE(ImageProviderLoader);
89 };
90 
91 } // namespace OHOS::Ace
92 
93 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_RESOURCE_SHARED_IMAGE_MANAGER_H
94