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_MISSION_DATA_STORAGE_H
17 #define OHOS_ABILITY_RUNTIME_MISSION_DATA_STORAGE_H
18 
19 #include <list>
20 #include <mutex>
21 #include <queue>
22 #include "cpp/mutex.h"
23 
24 #include "inner_mission_info.h"
25 #include "mission_snapshot.h"
26 
27 namespace OHOS {
28 namespace AAFwk {
29 constexpr const char* TASK_DATA_FILE_BASE_PATH = "/data/service/el1/public/AbilityManagerService";
30 constexpr const char* MISSION_DATA_FILE_PATH = "MissionInfo";
31 constexpr const char* MISSION_JSON_FILE_PREFIX = "mission";
32 constexpr const char* LOW_RESOLUTION_FLAG = "little";
33 constexpr const char* JSON_FILE_SUFFIX = ".json";
34 constexpr const char* JPEG_FILE_SUFFIX = ".jpg";
35 constexpr const char* FILE_SEPARATOR = "/";
36 constexpr const char* UNDERLINE_SEPARATOR = "_";
37 const int32_t SCALE = 2;
38 
39 class MissionDataStorage : public std::enable_shared_from_this<MissionDataStorage> {
40 public:
41     MissionDataStorage() = default;
42     explicit MissionDataStorage(int userId);
43     virtual ~MissionDataStorage();
44 
45     /**
46      * @brief GeT all mission info.
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      */
55     void SaveMissionInfo(const InnerMissionInfo &missionInfo);
56 
57     /**
58      * @brief Delete the bundle data corresponding to the mission Id.
59      * @param missionId Indicates this mission id.
60      */
61     void DeleteMissionInfo(int missionId);
62 
63     /**
64      * @brief Save mission snapshot
65      * @param missionId Indicates this mission id.
66      * @param missionSnapshot the mission snapshot to save
67      */
68     void SaveMissionSnapshot(int32_t missionId, const MissionSnapshot& missionSnapshot);
69 
70     /**
71      * @brief Delete mission snapshot
72      * @param missionId Indicates this mission id.
73      */
74     void DeleteMissionSnapshot(int32_t missionId);
75 
76     /**
77      * @brief Get the Mission Snapshot object
78      * @param missionId id of mission.
79      * @param missionSnapshot snapshot of target mission id.
80      * @param isLowResolution low resolution.
81      * @return Returns true if this function is successfully called; returns false otherwise.
82      */
83     bool GetMissionSnapshot(int32_t missionId, MissionSnapshot& missionSnapshot, bool isLowResolution);
84 
85 #ifdef SUPPORT_GRAPHICS
86     /**
87      * Get low resoultion pixelmap of source.
88      *
89      * @param source source pixelmap.
90      * @return return reduced pixel map.
91      */
92     static std::shared_ptr<OHOS::Media::PixelMap> GetReducedPixelMap(
93         const std::shared_ptr<OHOS::Media::PixelMap>& source);
94 
95     /**
96      * @brief Get the Snapshot object
97      * @param missionId Indicates this mission id.
98      * @return Returns PixelMap of snapshot.
99      */
100     std::shared_ptr<Media::PixelMap> GetSnapshot(int missionId, bool isLowResolution = false) const;
101 
102     std::unique_ptr<Media::PixelMap> GetPixelMap(int missionId, bool isLowResolution) const;
103     std::unique_ptr<uint8_t[]> ReadFileToBuffer(const std::string &filePath, size_t &bufferSize) const;
104 #endif
105 
106 private:
107     std::string GetMissionDataDirPath() const;
108 
109     std::string GetMissionDataFilePath(int missionId);
110 
111     std::string GetMissionSnapshotPath(int32_t missionId, bool isLowResolution) const;
112 
113     bool CheckFileNameValid(const std::string &fileName);
114 
115 #ifdef SUPPORT_GRAPHICS
116     template<typename T>
117     void WriteToJpeg(const std::string &filePath, T &snapshot) const;
118 
119     bool GetCachedSnapshot(int32_t missionId, MissionSnapshot& missionSnapshot);
120 
121     bool SaveCachedSnapshot(int32_t missionId, const MissionSnapshot& missionSnapshot);
122 
123     bool DeleteCachedSnapshot(int32_t missionId);
124     void DeleteMissionSnapshot(int32_t missionId, bool isLowResolution);
125 
126     void SaveSnapshotFile(int32_t missionId, const MissionSnapshot& missionSnapshot);
127 
128     void SaveSnapshotFile(int32_t missionId, const std::shared_ptr<OHOS::Media::PixelMap>& snapshot,
129         bool isPrivate, bool isLowResolution);
130 
131     std::map<int32_t, std::shared_ptr<Media::PixelMap>> cachedPixelMap_;
132 #endif
133 
134     int userId_ = 0;
135     ffrt::mutex cachedPixelMapMutex_;
136 };
137 }  // namespace AAFwk
138 }  // namespace OHOS
139 #endif  // OHOS_ABILITY_RUNTIME_MISSION_DATA_STORAGE_H
140