• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 created");
27 }
28 
~TaskDataPersistenceMgr()29 TaskDataPersistenceMgr::~TaskDataPersistenceMgr()
30 {
31     TAG_LOGI(AAFwkTag::ABILITYMGR, "TaskDataPersistenceMgr instance 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, "null currentMissionDataStorage_");
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_ null");
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_ null");
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, "removeUserDir fail");
109         return false;
110     }
111     std::string userDir = std::string(TASK_DATA_FILE_BASE_PATH) + "/" + std::to_string(userId);
112     TAG_LOGD(AAFwkTag::ABILITYMGR, "userDir: %{public}s", userDir.c_str());
113     bool ret = OHOS::ForceRemoveDirectory(userDir);
114     if (!ret) {
115         TAG_LOGE(AAFwkTag::ABILITYMGR, "remove user dir %{public}s failed", userDir.c_str());
116         return false;
117     }
118     return true;
119 }
120 
SaveMissionSnapshot(int missionId,const MissionSnapshot & snapshot)121 bool TaskDataPersistenceMgr::SaveMissionSnapshot(int missionId, const MissionSnapshot& snapshot)
122 {
123     std::lock_guard<ffrt::mutex> lock(mutex_);
124     if (!handler_ || !currentMissionDataStorage_) {
125         TAG_LOGE(AAFwkTag::ABILITYMGR, "snapshot: handler_ or currentMissionDataStorage_ null");
126         return false;
127     }
128 
129     std::weak_ptr<MissionDataStorage> weakPtr(currentMissionDataStorage_);
130     std::function<void()> SaveMissionSnapshotFunc = [weakPtr, missionId, snapshot]() {
131         auto missionDataStorage = weakPtr.lock();
132         if (missionDataStorage) {
133             missionDataStorage->SaveMissionSnapshot(missionId, snapshot);
134         }
135     };
136     handler_->SubmitTask(SaveMissionSnapshotFunc, SAVE_MISSION_SNAPSHOT);
137     return true;
138 }
139 
140 #ifdef SUPPORT_SCREEN
GetSnapshot(int missionId) const141 std::shared_ptr<Media::PixelMap> TaskDataPersistenceMgr::GetSnapshot(int missionId) const
142 {
143     if (!currentMissionDataStorage_) {
144         TAG_LOGE(AAFwkTag::ABILITYMGR, "snapshot: currentMissionDataStorage_ null");
145         return nullptr;
146     }
147     return currentMissionDataStorage_->GetSnapshot(missionId);
148 }
149 #endif
150 
GetMissionSnapshot(int missionId,MissionSnapshot & snapshot,bool isLowResolution)151 bool TaskDataPersistenceMgr::GetMissionSnapshot(int missionId, MissionSnapshot& snapshot, bool isLowResolution)
152 {
153     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
154     std::lock_guard<ffrt::mutex> lock(mutex_);
155     if (!currentMissionDataStorage_) {
156         TAG_LOGE(AAFwkTag::ABILITYMGR, "snapshot: currentMissionDataStorage_ null");
157         return false;
158     }
159     return currentMissionDataStorage_->GetMissionSnapshot(missionId, snapshot, isLowResolution);
160 }
161 }  // namespace AAFwk
162 }  // namespace OHOS
163