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 #include "task_data_persistence_mgr.h"
17 #include "ability_util.h"
18 #include "directory_ex.h"
19 #include "hilog_wrapper.h"
20
21 namespace OHOS {
22 namespace AAFwk {
TaskDataPersistenceMgr()23 TaskDataPersistenceMgr::TaskDataPersistenceMgr()
24 {
25 HILOG_INFO("TaskDataPersistenceMgr instance is created");
26 }
27
~TaskDataPersistenceMgr()28 TaskDataPersistenceMgr::~TaskDataPersistenceMgr()
29 {
30 eventLoop_.reset();
31 handler_.reset();
32 HILOG_INFO("TaskDataPersistenceMgr instance is destroyed");
33 }
34
Init(int userId)35 bool TaskDataPersistenceMgr::Init(int userId)
36 {
37 if (!eventLoop_) {
38 eventLoop_ = AppExecFwk::EventRunner::Create(THREAD_NAME);
39 CHECK_POINTER_RETURN_BOOL(eventLoop_);
40 }
41
42 if (!handler_) {
43 handler_ = std::make_shared<AppExecFwk::EventHandler>(eventLoop_);
44 CHECK_POINTER_RETURN_BOOL(handler_);
45 }
46
47 std::lock_guard<std::mutex> lock(mutex_);
48 if (missionDataStorageMgr_.find(userId) == missionDataStorageMgr_.end()) {
49 currentMissionDataStorage_ = std::make_shared<MissionDataStorage>(userId);
50 missionDataStorageMgr_.insert(std::make_pair(userId, currentMissionDataStorage_));
51 } else {
52 currentMissionDataStorage_ = missionDataStorageMgr_[userId];
53 }
54 currentUserId_ = userId;
55
56 CHECK_POINTER_RETURN_BOOL(currentMissionDataStorage_);
57 HILOG_INFO("Init success.");
58 return true;
59 }
60
LoadAllMissionInfo(std::list<InnerMissionInfo> & missionInfoList)61 bool TaskDataPersistenceMgr::LoadAllMissionInfo(std::list<InnerMissionInfo> &missionInfoList)
62 {
63 std::lock_guard<std::mutex> lock(mutex_);
64 if (!currentMissionDataStorage_) {
65 HILOG_ERROR("currentMissionDataStorage_ is nullptr");
66 return false;
67 }
68
69 return currentMissionDataStorage_->LoadAllMissionInfo(missionInfoList);
70 }
71
SaveMissionInfo(const InnerMissionInfo & missionInfo)72 bool TaskDataPersistenceMgr::SaveMissionInfo(const InnerMissionInfo &missionInfo)
73 {
74 std::lock_guard<std::mutex> lock(mutex_);
75 if (!handler_ || !currentMissionDataStorage_) {
76 HILOG_ERROR("handler_ or currentMissionDataStorage_ is nullptr");
77 return false;
78 }
79
80 std::weak_ptr<MissionDataStorage> weakPtr(currentMissionDataStorage_);
81 std::function<void()> SaveMissionInfoFunc = [weakPtr, missionInfo]() {
82 auto missionDataStorage = weakPtr.lock();
83 if (missionDataStorage) {
84 missionDataStorage->SaveMissionInfo(missionInfo);
85 }
86 };
87 return handler_->PostTask(SaveMissionInfoFunc, SAVE_MISSION_INFO);
88 }
89
DeleteMissionInfo(int missionId)90 bool TaskDataPersistenceMgr::DeleteMissionInfo(int missionId)
91 {
92 std::lock_guard<std::mutex> lock(mutex_);
93 if (!handler_ || !currentMissionDataStorage_) {
94 HILOG_ERROR("handler_ or currentMissionDataStorage_ is nullptr");
95 return false;
96 }
97
98 std::weak_ptr<MissionDataStorage> weakPtr(currentMissionDataStorage_);
99 std::function<void()> DeleteMissionInfoFunc = [weakPtr, missionId]() {
100 auto missionDataStorage = weakPtr.lock();
101 if (missionDataStorage) {
102 missionDataStorage->DeleteMissionInfo(missionId);
103 }
104 };
105 return handler_->PostTask(DeleteMissionInfoFunc, DELETE_MISSION_INFO);
106 }
107
RemoveUserDir(int32_t userId)108 bool TaskDataPersistenceMgr::RemoveUserDir(int32_t userId)
109 {
110 std::lock_guard<std::mutex> lock(mutex_);
111 if (currentUserId_ == userId) {
112 HILOG_ERROR("can not removed current user dir");
113 return false;
114 }
115 std::string userDir = std::string(TASK_DATA_FILE_BASE_PATH) + "/" + std::to_string(userId);
116 bool ret = OHOS::ForceRemoveDirectory(userDir);
117 if (!ret) {
118 HILOG_ERROR("remove user dir %{public}s failed.", userDir.c_str());
119 return false;
120 }
121 return true;
122 }
123
SaveMissionSnapshot(int missionId,const MissionSnapshot & snapshot)124 bool TaskDataPersistenceMgr::SaveMissionSnapshot(int missionId, const MissionSnapshot& snapshot)
125 {
126 std::lock_guard<std::mutex> lock(mutex_);
127 if (!handler_ || !currentMissionDataStorage_) {
128 HILOG_ERROR("snapshot: handler_ or currentMissionDataStorage_ is nullptr");
129 return false;
130 }
131
132 std::weak_ptr<MissionDataStorage> weakPtr(currentMissionDataStorage_);
133 std::function<void()> SaveMissionSnapshotFunc = [weakPtr, missionId, snapshot]() {
134 auto missionDataStorage = weakPtr.lock();
135 if (missionDataStorage) {
136 missionDataStorage->SaveMissionSnapshot(missionId, snapshot);
137 }
138 };
139 return handler_->PostTask(SaveMissionSnapshotFunc, SAVE_MISSION_SNAPSHOT);
140 }
141
142 #ifdef SUPPORT_GRAPHICS
GetSnapshot(int missionId) const143 std::shared_ptr<Media::PixelMap> TaskDataPersistenceMgr::GetSnapshot(int missionId) const
144 {
145 if (!currentMissionDataStorage_) {
146 HILOG_ERROR("snapshot: currentMissionDataStorage_ is nullptr");
147 return nullptr;
148 }
149 return currentMissionDataStorage_->GetSnapshot(missionId);
150 }
151 #endif
152
GetMissionSnapshot(int missionId,MissionSnapshot & snapshot,bool isLowResolution)153 bool TaskDataPersistenceMgr::GetMissionSnapshot(int missionId, MissionSnapshot& snapshot, bool isLowResolution)
154 {
155 std::lock_guard<std::mutex> lock(mutex_);
156 if (!currentMissionDataStorage_) {
157 HILOG_ERROR("snapshot: currentMissionDataStorage_ is nullptr");
158 return false;
159 }
160 return currentMissionDataStorage_->GetMissionSnapshot(missionId, snapshot, isLowResolution);
161 }
162 } // namespace AAFwk
163 } // namespace OHOS
164