1 /*
2  * Copyright (c) 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 "ability_cache_manager.h"
17 
18 #include "hilog_tag_wrapper.h"
19 
20 namespace OHOS {
21 namespace AAFwk {
22 const std::string FRS_APP_INDEX = "ohos.extra.param.key.frs_index";
23 const std::string FRS_BUNDLE_NAME = "com.ohos.formrenderservice";
24 
AbilityCacheManager()25 AbilityCacheManager::AbilityCacheManager() {}
26 
~AbilityCacheManager()27 AbilityCacheManager::~AbilityCacheManager() {}
28 
GetInstance()29 AbilityCacheManager &AbilityCacheManager::GetInstance()
30 {
31     static AbilityCacheManager abilityRecMgr;
32     return abilityRecMgr;
33 }
34 
Init(uint32_t devCapacity, uint32_t procCapacity)35 void AbilityCacheManager::Init(uint32_t devCapacity, uint32_t procCapacity)
36 {
37     devLruCapacity_ = devCapacity;
38     procLruCapacity_ = procCapacity;
39 }
40 
RemoveAbilityRecInDevList(std::shared_ptr<AbilityRecord> abilityRecord)41 void AbilityCacheManager::RemoveAbilityRecInDevList(std::shared_ptr<AbilityRecord> abilityRecord)
42 {
43     if (abilityRecord == nullptr) {
44         return;
45     }
46     auto it = devRecLru_.begin();
47     uint32_t accessTokenId = abilityRecord->GetApplicationInfo().accessTokenId;
48     while (it != devRecLru_.end()) {
49         if ((*it)->GetRecordId() == abilityRecord->GetRecordId()) {
50             devRecLru_.erase(it);
51             devLruCnt_--;
52             return;
53         } else {
54             it++;
55         }
56     }
57 }
58 
RemoveAbilityRecInProcList(std::shared_ptr<AbilityRecord> abilityRecord)59 void AbilityCacheManager::RemoveAbilityRecInProcList(std::shared_ptr<AbilityRecord> abilityRecord)
60 {
61     if (abilityRecord == nullptr) {
62         return;
63     }
64     uint32_t accessTokenId = abilityRecord->GetApplicationInfo().accessTokenId;
65     auto findProcInfo = procLruMap_.find(accessTokenId);
66     if (findProcInfo == procLruMap_.end()) {
67         TAG_LOGE(AAFwkTag::ABILITYMGR, "can't find record");
68         return;
69     }
70     auto it = findProcInfo->second.recList.begin();
71 
72     while (it != findProcInfo->second.recList.end()) {
73         if ((*it)->GetRecordId() == abilityRecord->GetRecordId()) {
74             findProcInfo->second.recList.erase(it);
75             findProcInfo->second.cnt--;
76             if (findProcInfo->second.cnt == 0) {
77                 procLruMap_.erase(findProcInfo);
78             }
79             return;
80         } else {
81             it++;
82         }
83     }
84 }
85 
AddToProcLru(std::shared_ptr<AbilityRecord> abilityRecord)86 std::shared_ptr<AbilityRecord> AbilityCacheManager::AddToProcLru(std::shared_ptr<AbilityRecord> abilityRecord)
87 {
88     if (abilityRecord == nullptr) {
89         return nullptr;
90     }
91     auto findProcInfo = procLruMap_.find(abilityRecord->GetApplicationInfo().accessTokenId);
92     if (findProcInfo == procLruMap_.end()) {
93         std::list<std::shared_ptr<AbilityRecord>> recList;
94         ProcRecordsInfo procRecInfo = {recList, 1};
95         procRecInfo.recList.push_back(abilityRecord);
96         procLruMap_[abilityRecord->GetApplicationInfo().accessTokenId] = procRecInfo;
97         return nullptr;
98     }
99     if (findProcInfo->second.cnt == procLruCapacity_) {
100         RemoveAbilityRecInDevList(findProcInfo->second.recList.front());
101         std::shared_ptr<AbilityRecord> rec = findProcInfo->second.recList.front();
102         findProcInfo->second.recList.pop_front();
103         findProcInfo->second.recList.push_back(abilityRecord);
104         return rec;
105     }
106     findProcInfo->second.cnt++;
107     findProcInfo->second.recList.push_back(abilityRecord);
108     return nullptr;
109 }
110 
AddToDevLru(std::shared_ptr<AbilityRecord> abilityRecord, std::shared_ptr<AbilityRecord> rec)111 std::shared_ptr<AbilityRecord> AbilityCacheManager::AddToDevLru(std::shared_ptr<AbilityRecord> abilityRecord,
112     std::shared_ptr<AbilityRecord> rec)
113 {
114     if (rec != nullptr) {
115         devRecLru_.push_back(abilityRecord);
116         devLruCnt_++;
117         return rec;
118     }
119     if (devLruCnt_ == devLruCapacity_) {
120         rec = devRecLru_.front();
121         RemoveAbilityRecInProcList(rec);
122         devRecLru_.pop_front();
123         devLruCnt_--;
124     }
125     devRecLru_.push_back(abilityRecord);
126     devLruCnt_++;
127     return rec;
128 }
129 
Put(std::shared_ptr<AbilityRecord> abilityRecord)130 std::shared_ptr<AbilityRecord> AbilityCacheManager::Put(std::shared_ptr<AbilityRecord> abilityRecord)
131 {
132     if (abilityRecord == nullptr) {
133         TAG_LOGE(AAFwkTag::ABILITYMGR, "null abilityRecord");
134         return nullptr;
135     }
136     TAG_LOGD(AAFwkTag::ABILITYMGR, "Put the ability to lru, service:%{public}s, extension type %{public}d",
137         abilityRecord->GetURI().c_str(), abilityRecord->GetAbilityInfo().extensionAbilityType);
138     std::lock_guard<std::mutex> lock(mutex_);
139     std::shared_ptr<AbilityRecord> rec = AddToProcLru(abilityRecord);
140     return AddToDevLru(abilityRecord, rec);
141 }
142 
Remove(std::shared_ptr<AbilityRecord> abilityRecord)143 void AbilityCacheManager::Remove(std::shared_ptr<AbilityRecord> abilityRecord)
144 {
145     if (abilityRecord == nullptr) {
146         TAG_LOGE(AAFwkTag::ABILITYMGR, "null abilityRecord");
147         return;
148     }
149     TAG_LOGD(AAFwkTag::ABILITYMGR, "Remove the ability from lru, service:%{public}s, extension type %{public}d",
150         abilityRecord->GetURI().c_str(), abilityRecord->GetAbilityInfo().extensionAbilityType);
151     std::lock_guard<std::mutex> lock(mutex_);
152     RemoveAbilityRecInProcList(abilityRecord);
153     RemoveAbilityRecInDevList(abilityRecord);
154 }
155 
IsRecInfoSame(const AbilityRequest& abilityRequest, std::shared_ptr<AbilityRecord> abilityRecord)156 bool AbilityCacheManager::IsRecInfoSame(const AbilityRequest& abilityRequest,
157     std::shared_ptr<AbilityRecord> abilityRecord)
158 {
159     return abilityRecord != nullptr &&
160         abilityRequest.abilityInfo.moduleName == abilityRecord->GetAbilityInfo().moduleName &&
161         abilityRequest.want.GetElement().GetAbilityName() == abilityRecord->GetWant().GetElement().GetAbilityName();
162 }
163 
GetAbilityRecInProcList(const AbilityRequest &abilityRequest)164 std::shared_ptr<AbilityRecord> AbilityCacheManager::GetAbilityRecInProcList(const AbilityRequest &abilityRequest)
165 {
166     auto findProcInfo = procLruMap_.find(abilityRequest.appInfo.accessTokenId);
167     if (findProcInfo == procLruMap_.end()) {
168         TAG_LOGE(AAFwkTag::ABILITYMGR, "can't found bundleName");
169         return nullptr;
170     }
171     ProcRecordsInfo &procRecordsInfo = findProcInfo->second;
172     auto recIter = procRecordsInfo.recList.begin();
173     while (recIter != procRecordsInfo.recList.end()) {
174         if (IsRecInfoSame(abilityRequest, *recIter)) {
175             std::shared_ptr<AbilityRecord> abilityRecord = *recIter;
176             procRecordsInfo.recList.erase(recIter);
177             procRecordsInfo.cnt--;
178             return abilityRecord;
179         }
180         recIter++;
181     }
182     TAG_LOGD(AAFwkTag::ABILITYMGR, "Can't found the abilityRecord in process list for get.");
183     return nullptr;
184 }
185 
Get(const AbilityRequest& abilityRequest)186 std::shared_ptr<AbilityRecord> AbilityCacheManager::Get(const AbilityRequest& abilityRequest)
187 {
188     TAG_LOGD(AAFwkTag::ABILITYMGR, "Get the ability from lru, service:%{public}s, extension type %{public}d",
189         abilityRequest.abilityInfo.uri.c_str(), abilityRequest.abilityInfo.extensionAbilityType);
190     std::lock_guard<std::mutex> lock(mutex_);
191     std::shared_ptr<AbilityRecord> abilityRecord = GetAbilityRecInProcList(abilityRequest);
192     if (abilityRecord == nullptr) {
193         TAG_LOGD(AAFwkTag::ABILITYMGR, "Can't found the abilityRecord for get.");
194         return nullptr;
195     }
196     RemoveAbilityRecInDevList(abilityRecord);
197     return abilityRecord;
198 }
199 
FindRecordByToken(const sptr<IRemoteObject> &token)200 std::shared_ptr<AbilityRecord> AbilityCacheManager::FindRecordByToken(const sptr<IRemoteObject> &token)
201 {
202     if (token == nullptr) {
203         TAG_LOGE(AAFwkTag::ABILITYMGR, "null token");
204         return nullptr;
205     }
206     std::lock_guard<std::mutex> lock(mutex_);
207     auto it = devRecLru_.begin();
208     while (it != devRecLru_.end()) {
209         sptr<IRemoteObject> srcToken = (*it)->GetToken();
210         if (srcToken == token) {
211             std::shared_ptr<AbilityRecord> &abilityRecord = *it;
212             TAG_LOGD(AAFwkTag::ABILITYMGR,
213                 "Find the ability by token from lru, service:%{public}s, extension type %{public}d",
214                 abilityRecord->GetURI().c_str(), abilityRecord->GetAbilityInfo().extensionAbilityType);
215             return abilityRecord;
216         } else {
217             it++;
218         }
219     }
220     return nullptr;
221 }
222 
GetAbilityList()223 std::list<std::shared_ptr<AbilityRecord>> AbilityCacheManager::GetAbilityList()
224 {
225     std::lock_guard<std::mutex> lock(mutex_);
226     return devRecLru_;
227 }
228 
FindRecordBySessionId(const std::string &assertSessionId)229 std::shared_ptr<AbilityRecord> AbilityCacheManager::FindRecordBySessionId(const std::string &assertSessionId)
230 {
231     std::lock_guard<std::mutex> lock(mutex_);
232     auto it = devRecLru_.begin();
233     while (it != devRecLru_.end()) {
234         auto assertSessionStr = (*it)->GetWant().GetStringParam(Want::PARAM_ASSERT_FAULT_SESSION_ID);
235         if (assertSessionStr == assertSessionId) {
236             std::shared_ptr<AbilityRecord> &abilityRecord = *it;
237             TAG_LOGD(AAFwkTag::ABILITYMGR,
238                 "Find the ability by sessionId from lru, service:%{public}s, extension type %{public}d",
239                 abilityRecord->GetURI().c_str(), abilityRecord->GetAbilityInfo().extensionAbilityType);
240             return abilityRecord;
241         } else {
242             it++;
243         }
244     }
245     return nullptr;
246 }
247 
FindRecordByServiceKey(const std::string &serviceKey)248 std::shared_ptr<AbilityRecord> AbilityCacheManager::FindRecordByServiceKey(const std::string &serviceKey)
249 {
250     std::lock_guard<std::mutex> lock(mutex_);
251     auto it = devRecLru_.begin();
252     while (it != devRecLru_.end()) {
253         std::string curServiceKey = (*it)->GetURI();
254         if (FRS_BUNDLE_NAME == (*it)->GetAbilityInfo().bundleName) {
255             curServiceKey = curServiceKey + std::to_string((*it)->GetWant().GetIntParam(FRS_APP_INDEX, 0));
256         }
257         if (curServiceKey.compare(serviceKey) == 0) {
258             std::shared_ptr<AbilityRecord> &abilityRecord = *it;
259             TAG_LOGD(AAFwkTag::ABILITYMGR,
260                 "Find the ability by serviceKey from lru, service:%{public}s, extension type %{public}d",
261                 abilityRecord->GetURI().c_str(), abilityRecord->GetAbilityInfo().extensionAbilityType);
262             return abilityRecord;
263         } else {
264             it++;
265         }
266     }
267     return nullptr;
268 }
269 
RemoveLauncherDeathRecipient()270 void AbilityCacheManager::RemoveLauncherDeathRecipient()
271 {
272     std::lock_guard<std::mutex> lock(mutex_);
273     auto it = devRecLru_.begin();
274     while (it != devRecLru_.end()) {
275         auto targetExtension = *it;
276         if (targetExtension != nullptr && targetExtension->GetAbilityInfo().type == AbilityType::EXTENSION &&
277             ((targetExtension->GetAbilityInfo().name == AbilityConfig::LAUNCHER_ABILITY_NAME &&
278             targetExtension->GetAbilityInfo().bundleName == AbilityConfig::LAUNCHER_BUNDLE_NAME) ||
279             targetExtension->IsSceneBoard())) {
280             targetExtension->RemoveAbilityDeathRecipient();
281             return;
282         }
283         it++;
284     }
285 }
286 
SignRestartAppFlag(int32_t uid)287 void AbilityCacheManager::SignRestartAppFlag(int32_t uid)
288 {
289     std::lock_guard<std::mutex> lock(mutex_);
290     auto it = devRecLru_.begin();
291     while (it != devRecLru_.end()) {
292         auto abilityRecord = *it;
293         if (abilityRecord != nullptr && abilityRecord->GetUid() == uid) {
294             abilityRecord->SetRestartAppFlag(true);
295         }
296         it++;
297     }
298 }
299 
DeleteInvalidServiceRecord(const std::string &bundleName)300 void AbilityCacheManager::DeleteInvalidServiceRecord(const std::string &bundleName)
301 {
302     std::lock_guard<std::mutex> lock(mutex_);
303     auto it = devRecLru_.begin();
304     while (it != devRecLru_.end()) {
305         auto abilityRecord = *it;
306         if (abilityRecord != nullptr && abilityRecord->GetApplicationInfo().bundleName == bundleName) {
307             RemoveAbilityRecInProcList(abilityRecord);
308             RemoveAbilityRecInDevList(abilityRecord);
309         }
310         it++;
311     }
312 }
313 }  // namespace AAFwk
314 } // namespace OHOS