1 /*
2 * Copyright (c) 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 "supervisor.h"
17 #include "ability_info.h"
18 #include "cgroup_sched_common.h"
19
20 namespace OHOS {
21 namespace ResourceSchedule {
22 using OHOS::AppExecFwk::AbilityType;
23
GetAbilityInfoNonNull(uintptr_t token)24 std::shared_ptr<AbilityInfo> ProcessRecord::GetAbilityInfoNonNull(uintptr_t token)
25 {
26 auto a = std::find_if(abilities_.begin(), abilities_.end(), [ token ] (const auto& a) {
27 return a->token_ == token;
28 });
29 if (a != abilities_.end()) {
30 return *a;
31 }
32 std::shared_ptr<AbilityInfo> abi = std::make_shared<AbilityInfo>(token);
33 abilities_.push_back(abi);
34 return abi;
35 }
36
GetAbilityInfo(uintptr_t token)37 std::shared_ptr<AbilityInfo> ProcessRecord::GetAbilityInfo(uintptr_t token)
38 {
39 auto a = std::find_if(abilities_.begin(), abilities_.end(), [ token ] (const auto& a) {
40 return a->token_ == token;
41 });
42 if (a != abilities_.end()) {
43 return *a;
44 }
45 return nullptr;
46 }
47
GetWindowInfoNonNull(uint32_t windowId)48 std::shared_ptr<WindowInfo> ProcessRecord::GetWindowInfoNonNull(uint32_t windowId)
49 {
50 auto w = std::find_if(windows_.begin(), windows_.end(), [ windowId ] (const auto& w) {
51 return w->windowId_ == windowId;
52 });
53 if (w != windows_.end()) {
54 return *w;
55 }
56 std::shared_ptr<WindowInfo> win = std::make_shared<WindowInfo>(windowId);
57 windows_.push_back(win);
58 return win;
59 }
60
RemoveAbilityByToken(uintptr_t token)61 void ProcessRecord::RemoveAbilityByToken(uintptr_t token)
62 {
63 for (auto iter = abilities_.begin(); iter != abilities_.end(); ++iter) {
64 if ((*iter)->token_ == token) {
65 abilities_.erase(iter);
66 break;
67 }
68 }
69 }
70
HasAbility(uintptr_t token) const71 bool ProcessRecord::HasAbility(uintptr_t token) const
72 {
73 return std::any_of(abilities_.begin(), abilities_.end(), [ token ] (const auto& abi) {
74 return abi->token_ == token;
75 });
76 }
77
HasServiceExtension() const78 bool ProcessRecord::HasServiceExtension() const
79 {
80 return std::any_of(abilities_.begin(), abilities_.end(), [] (const auto& abi) {
81 return abi->type_ == (int32_t)(AbilityType::SERVICE)
82 || abi->type_ == (int32_t)(AbilityType::EXTENSION)
83 || abi->type_ == (int32_t)(AbilityType::DATA);
84 });
85 }
86
IsVisible() const87 bool ProcessRecord::IsVisible() const
88 {
89 return std::any_of(windows_.begin(), windows_.end(), [] (const auto& w) {
90 return w->isVisible_;
91 });
92 }
93
AddProcessRecord(std::shared_ptr<ProcessRecord> pr)94 std::shared_ptr<ProcessRecord> Application::AddProcessRecord(std::shared_ptr<ProcessRecord> pr)
95 {
96 if (pr) {
97 pidsMap_[pr->GetPid()] = pr;
98 }
99 return pr;
100 }
101
RemoveProcessRecord(pid_t pid)102 void Application::RemoveProcessRecord(pid_t pid)
103 {
104 auto iter = pidsMap_.find(pid);
105 if (iter != pidsMap_.end()) {
106 if (focusedProcess_ == iter->second) {
107 focusedProcess_ = nullptr;
108 }
109 pidsMap_.erase(iter);
110 }
111 }
112
GetProcessRecord(pid_t pid)113 std::shared_ptr<ProcessRecord> Application::GetProcessRecord(pid_t pid)
114 {
115 if (pidsMap_.find(pid) == pidsMap_.end()) {
116 return nullptr;
117 }
118 return pidsMap_[pid];
119 }
120
GetProcessRecordNonNull(pid_t pid)121 std::shared_ptr<ProcessRecord> Application::GetProcessRecordNonNull(pid_t pid)
122 {
123 if (pidsMap_.find(pid) == pidsMap_.end()) {
124 auto pr = std::make_shared<ProcessRecord>(this->GetUid(), pid);
125 this->AddProcessRecord(pr);
126 return pr;
127 }
128 return pidsMap_[pid];
129 }
130
FindProcessRecordByToken(uintptr_t token)131 std::shared_ptr<ProcessRecord> Application::FindProcessRecordByToken(uintptr_t token)
132 {
133 for (auto iter = pidsMap_.begin(); iter != pidsMap_.end(); iter++) {
134 auto pr = iter->second;
135 if (pr->HasAbility(token)) {
136 return pr;
137 }
138 }
139 return nullptr;
140 }
141
FindProcessRecordByWindowId(uint32_t windowId)142 std::shared_ptr<ProcessRecord> Application::FindProcessRecordByWindowId(uint32_t windowId)
143 {
144 for (auto iter = pidsMap_.begin(); iter != pidsMap_.end(); iter++) {
145 auto pr = iter->second;
146 if (std::any_of(pr->windows_.begin(), pr->windows_.end(),
147 [ windowId ] (const auto& w) {
148 return w->windowId_ == windowId;
149 })) {
150 return pr;
151 }
152 }
153 return nullptr;
154 }
155
GetAppRecord(int32_t uid)156 std::shared_ptr<Application> Supervisor::GetAppRecord(int32_t uid)
157 {
158 if (uidsMap_.find(uid) == uidsMap_.end()) {
159 return nullptr;
160 }
161 return uidsMap_[uid];
162 }
163
GetAppRecordNonNull(int32_t uid)164 std::shared_ptr<Application> Supervisor::GetAppRecordNonNull(int32_t uid)
165 {
166 if (uidsMap_.find(uid) == uidsMap_.end()) {
167 auto app = std::make_shared<Application>(uid);
168 uidsMap_[uid] = app;
169 return app;
170 }
171 return uidsMap_[uid];
172 }
173
FindProcessRecord(pid_t pid)174 std::shared_ptr<ProcessRecord> Supervisor::FindProcessRecord(pid_t pid)
175 {
176 std::shared_ptr<ProcessRecord> pr = nullptr;
177 for (auto iter = uidsMap_.begin(); iter != uidsMap_.end(); iter++) {
178 auto app = iter->second;
179 pr = app->GetProcessRecord(pid);
180 if (pr) {
181 break;
182 }
183 }
184 return pr;
185 }
186
RemoveApplication(int32_t uid)187 void Supervisor::RemoveApplication(int32_t uid)
188 {
189 auto iter = uidsMap_.find(uid);
190 if (iter != uidsMap_.end()) {
191 uidsMap_.erase(iter);
192 }
193 }
194
SearchAbilityToken(std::shared_ptr<Application> & application,std::shared_ptr<ProcessRecord> & procRecord,uintptr_t token)195 void Supervisor::SearchAbilityToken(std::shared_ptr<Application> &application,
196 std::shared_ptr<ProcessRecord> &procRecord, uintptr_t token)
197 {
198 std::shared_ptr<ProcessRecord> pr = nullptr;
199 for (auto iter = uidsMap_.begin(); iter != uidsMap_.end(); iter++) {
200 auto app = iter->second;
201 pr = app->FindProcessRecordByToken(token);
202 if (pr) {
203 application = app;
204 procRecord = pr;
205 break;
206 }
207 }
208 }
209
SearchWindowId(std::shared_ptr<Application> & application,std::shared_ptr<ProcessRecord> & procRecord,uint32_t windowId)210 void Supervisor::SearchWindowId(std::shared_ptr<Application> &application,
211 std::shared_ptr<ProcessRecord> &procRecord, uint32_t windowId)
212 {
213 std::shared_ptr<ProcessRecord> pr = nullptr;
214 for (auto iter = uidsMap_.begin(); iter != uidsMap_.end(); iter++) {
215 auto app = iter->second;
216 pr = app->FindProcessRecordByWindowId(windowId);
217 if (pr) {
218 application = app;
219 procRecord = pr;
220 break;
221 }
222 }
223 }
224 } // namespace ResourceSchedule
225 } // namespace OHOS
226