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 OHOS_ABILITY_RUNTIME_TASK_DATA_PERSISTENCE_MGR_H
17 #define OHOS_ABILITY_RUNTIME_TASK_DATA_PERSISTENCE_MGR_H
18 
19 #include <memory>
20 #include <unordered_map>
21 #include "cpp/mutex.h"
22 
23 #include "singleton.h"
24 #include "task_handler_wrap.h"
25 #include "mission_data_storage.h"
26 
27 namespace OHOS {
28 namespace AAFwk {
29 constexpr const char* THREAD_NAME = "TaskDataStorage";
30 constexpr const char* SAVE_MISSION_INFO = "SaveMissionInfo";
31 constexpr const char* DELETE_MISSION_INFO = "DeleteMissionInfo";
32 constexpr const char* SAVE_MISSION_SNAPSHOT = "SaveMissionSnapshot";
33 constexpr const char* GET_MISSION_SNAPSHOT = "GetMissionSnapshot";
34 
35 class TaskDataPersistenceMgr : public std::enable_shared_from_this<TaskDataPersistenceMgr> {
36     DECLARE_DELAYED_SINGLETON(TaskDataPersistenceMgr)
37 public:
38     /**
39      * @brief initialization of task data persistence manager.
40      * @param user id Indicates the missionInfo object of user to operate.
41      * @return Returns true if init successfully, returns false otherwise.
42      */
43     bool Init(int userId);
44 
45     /**
46      * @brief Boot query persistent storage.
47      * @return Returns true if this function is successfully called; returns false otherwise.
48      */
49     bool LoadAllMissionInfo(std::list<InnerMissionInfo> &missionInfoList);
50 
51     /**
52      * @brief Save the mission data.
53      * @param missionInfo Indicates the missionInfo object to be save.
54      * @return Returns true if the data is successfully saved; returns false otherwise.
55      */
56     bool SaveMissionInfo(const InnerMissionInfo &missionInfo);
57 
58     /**
59      * @brief Delete the mission data corresponding to the mission Id.
60      * @param missionId Indicates this mission id.
61      * @return Returns true if the data is successfully deleted; returns false otherwise.
62      */
63     bool DeleteMissionInfo(int missionId);
64 
65     /**
66      * @brief Remove user directory.
67      * @param userId Indicates this user id.
68      * @return Returns true if the directory is successfully removed; returns false otherwise.
69      */
70     bool RemoveUserDir(int32_t userId);
71 
72     /**
73      * @brief save mission snapshot
74      * @param missionId id of mission
75      * @param snapshot result of snapshot
76      * @return return true if update mission snapshot success, else false
77      */
78     bool SaveMissionSnapshot(int missionId, const MissionSnapshot& snapshot);
79 
80 #ifdef SUPPORT_GRAPHICS
81     /**
82      * @brief Get the Snapshot object
83      * @param missionId Indicates this mission id.
84      * @return Returns PixelMap of snapshot.
85      */
86     std::shared_ptr<Media::PixelMap> GetSnapshot(int missionId) const;
87 #endif
88 
89     /**
90      * @brief Get the mission snapshot object
91      * @param missionId id of mission
92      * @param missionSnapshot output snapshot of mission.
93      * @param isLowResolution low resolution.
94      * @return return true if update mission snapshot success, else false
95      */
96     bool GetMissionSnapshot(int missionId, MissionSnapshot& missionSnapshot, bool isLowResolution);
97 
98 private:
99     std::unordered_map<int, std::shared_ptr<MissionDataStorage>> missionDataStorageMgr_;
100     std::shared_ptr<MissionDataStorage> currentMissionDataStorage_;
101     std::shared_ptr<TaskHandlerWrap> handler_;
102     int32_t currentUserId_ = -1;
103     ffrt::mutex mutex_;
104 };
105 }  // namespace AAFwk
106 }  // namespace OHOS
107 #endif  // OHOS_ABILITY_RUNTIME_TASK_DATA_PERSISTENCE_MGR_H
108