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