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