• 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 <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 
34 namespace OHOS::Ace {
35 
36 using SharedImage = std::vector<uint8_t>;
37 using SharedImageMap = std::unordered_map<std::string, SharedImage>;
38 
39 class ImageProviderLoader;
40 using ProviderMapToReload = std::unordered_map<std::string, std::set<WeakPtr<ImageProviderLoader>>>;
41 
42 class SharedImageManager : public AceType {
43     DECLARE_ACE_TYPE(SharedImageManager, AceType);
44 
45 public:
SharedImageManager(const RefPtr<TaskExecutor> & taskExecutor)46     explicit SharedImageManager(const RefPtr<TaskExecutor>& taskExecutor) : taskExecutor_(taskExecutor) {}
47     ~SharedImageManager() override = default;
48     void AddSharedImage(const std::string& name, SharedImage&& sharedImage);
49     void AddPictureNamesToReloadMap(std::string&& name);
50     bool FindImageInSharedImageMap(const std::string& name, const WeakPtr<ImageProviderLoader>& providerWp);
51 
GetSharedImageMap()52     const SharedImageMap& GetSharedImageMap() const
53     {
54         return sharedImageMap_;
55     }
56 
57     bool Remove(const std::string& name);
58 
59     // return true if successfully registered
60     bool RegisterLoader(const std::string& name, const WeakPtr<ImageProviderLoader>& providerWp);
61 
62 private:
63     void PostDelayedTaskToClearImageData(const std::string& name, size_t dataSize);
64     std::function<void()> GenerateClearImageDataCallback(const std::string& name, size_t dataSize);
65 
66     std::mutex sharedImageMapMutex_;
67     std::mutex providerMapMutex_;
68     std::mutex cancelableCallbackMapMutex_;
69     SharedImageMap sharedImageMap_;
70     ProviderMapToReload providerMapToReload_;
71     std::function<void()> clearImageDataCallback_;
72     std::unordered_map<std::string, CancelableCallback<void()>> cancelableCallbackMap_;
73     WeakPtr<TaskExecutor> taskExecutor_;
74 };
75 
76 class ImageProviderLoader : public virtual AceType {
77     DECLARE_ACE_TYPE(ImageProviderLoader, AceType);
78 
79 public:
80     ~ImageProviderLoader() override = default;
81     virtual void UpdateData(const std::string& uri, const std::vector<uint8_t>& memData) = 0;
82 
83 protected:
84     ImageProviderLoader() = default;
85 
86     ACE_DISALLOW_COPY_AND_MOVE(ImageProviderLoader);
87 };
88 
89 } // namespace OHOS::Ace
90 
91 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_RESOURCE_SHARED_IMAGE_MANAGER_H
92