• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "exit_info_data_manager.h"
17 
18 #include "hilog_tag_wrapper.h"
19 
20 namespace OHOS {
21 namespace AbilityRuntime {
22 
AddExitInfo(uint32_t accessTokenId,ExitCacheInfo & cacheInfo)23 bool ExitInfoDataManager::AddExitInfo(uint32_t accessTokenId, ExitCacheInfo &cacheInfo)
24 {
25     if (IsExitInfoExist(accessTokenId)) {
26         TAG_LOGE(AAFwkTag::ABILITYMGR, "ExitInfo exist!");
27         return false;
28     }
29     std::lock_guard<std::mutex> lock(mutex_);
30     exitCacheInfos_.insert(std::make_pair(accessTokenId, cacheInfo));
31     return true;
32 }
33 
DeleteExitInfo(uint32_t accessTokenId)34 bool ExitInfoDataManager::DeleteExitInfo(uint32_t accessTokenId)
35 {
36     if (!IsExitInfoExist(accessTokenId)) {
37         TAG_LOGE(AAFwkTag::ABILITYMGR, "ExitInfo not exist!");
38         return false;
39     }
40     std::lock_guard<std::mutex> lock(mutex_);
41     exitCacheInfos_.erase(accessTokenId);
42     return true;
43 }
44 
GetExitInfo(uint32_t accessTokenId,ExitCacheInfo & cacheInfo)45 bool ExitInfoDataManager::GetExitInfo(uint32_t accessTokenId, ExitCacheInfo &cacheInfo)
46 {
47     std::lock_guard<std::mutex> lock(mutex_);
48     auto exitInfoIter = exitCacheInfos_.find(accessTokenId);
49     if (exitInfoIter == exitCacheInfos_.end()) {
50         TAG_LOGW(AAFwkTag::ABILITYMGR, "ExitInfo not exist!");
51         return false;
52     }
53     cacheInfo = exitInfoIter->second;
54     exitCacheInfos_.erase(exitInfoIter);
55     return true;
56 }
57 
GetExitInfo(int32_t pid,int32_t uid,uint32_t & accessTokenId,ExitCacheInfo & cacheInfo)58 bool ExitInfoDataManager::GetExitInfo(int32_t pid, int32_t uid, uint32_t &accessTokenId, ExitCacheInfo &cacheInfo)
59 {
60     std::map<uint32_t, ExitCacheInfo>::iterator it;
61     std::lock_guard<std::mutex> lock(mutex_);
62     for (it = exitCacheInfos_.begin(); it != exitCacheInfos_.end(); ++it) {
63         if (it->second.exitInfo.pid == pid && it->second.exitInfo.uid == uid) {
64             accessTokenId = it->first;
65             cacheInfo = it->second;
66             exitCacheInfos_.erase(it);
67             return true;
68         }
69     }
70     return false;
71 }
72 
IsExitInfoExist(uint32_t accessTokenId)73 bool ExitInfoDataManager::IsExitInfoExist(uint32_t accessTokenId)
74 {
75     std::lock_guard<std::mutex> lock(mutex_);
76     auto exitInfoIter = exitCacheInfos_.find(accessTokenId);
77     return exitInfoIter != exitCacheInfos_.end();
78 }
79 }  // namespace AbilityRuntime
80 }  // namespace OHOS
81