• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "app_process_manager.h"
17 
18 #include <csignal>
19 
20 #include "app_log_wrapper.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
AppProcessManager()24 AppProcessManager::AppProcessManager()
25 {}
26 
~AppProcessManager()27 AppProcessManager::~AppProcessManager()
28 {}
29 
RemoveAppFromRecentList(const std::shared_ptr<AppTaskInfo> & appTaskInfo)30 void AppProcessManager::RemoveAppFromRecentList(const std::shared_ptr<AppTaskInfo> &appTaskInfo)
31 {
32     if (appTaskInfo) {
33         recentAppList_.remove(appTaskInfo);
34     }
35 }
36 
ClearRecentAppList()37 void AppProcessManager::ClearRecentAppList()
38 {
39     recentAppList_.clear();
40 }
41 
AddAppToRecentList(const std::string & appName,const std::string & processName,const pid_t pid,const int32_t recordId)42 void AppProcessManager::AddAppToRecentList(
43     const std::string &appName, const std::string &processName, const pid_t pid, const int32_t recordId)
44 {
45     auto appTaskInfo = std::make_shared<AppTaskInfo>();
46     appTaskInfo->SetName(appName);
47     appTaskInfo->SetProcessName(processName);
48     appTaskInfo->SetPid(pid);
49     appTaskInfo->SetRecordId(recordId);
50     recentAppList_.push_front(appTaskInfo);
51 }
52 
GetRecentAppList() const53 const std::list<const std::shared_ptr<AppTaskInfo>> &AppProcessManager::GetRecentAppList() const
54 {
55     return recentAppList_;
56 }
57 
PushAppFront(const int32_t recordId)58 void AppProcessManager::PushAppFront(const int32_t recordId)
59 {
60     auto appTaskInfo = GetAppTaskInfoById(recordId);
61     if (appTaskInfo) {
62         recentAppList_.remove(appTaskInfo);
63         recentAppList_.push_front(appTaskInfo);
64     }
65 }
66 
RemoveAppFromRecentListById(const int32_t recordId)67 void AppProcessManager::RemoveAppFromRecentListById(const int32_t recordId)
68 {
69     auto appTaskInfo = GetAppTaskInfoById(recordId);
70     if (appTaskInfo) {
71         recentAppList_.remove(appTaskInfo);
72     }
73 }
74 
GetAppTaskInfoById(const int32_t recordId) const75 const std::shared_ptr<AppTaskInfo> AppProcessManager::GetAppTaskInfoById(const int32_t recordId) const
76 {
77     const auto &iter = std::find_if(recentAppList_.begin(), recentAppList_.end(), [&recordId](const auto &appTaskInfo) {
78         return appTaskInfo ? (appTaskInfo->GetRecordId() == recordId) : false;
79     });
80     return (iter == recentAppList_.end()) ? nullptr : (*iter);
81 }
82 
GetAppTaskInfoByProcessName(const std::string & appName,const std::string & processName) const83 std::shared_ptr<AppTaskInfo> AppProcessManager::GetAppTaskInfoByProcessName(
84     const std::string &appName, const std::string &processName) const
85 {
86     const auto &iter =
87         std::find_if(recentAppList_.begin(), recentAppList_.end(), [&appName, &processName](const auto &appTaskInfo) {
88             return ((appTaskInfo->GetName() == appName) && (appTaskInfo->GetProcessName() == processName));
89         });
90     return ((iter == recentAppList_.end()) ? nullptr : *iter);
91 }
92 }  // namespace AppExecFwk
93 }  // namespace OHOS
94