• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (mainProcess_ == iter->second) {
110             mainProcess_ = nullptr;
111         }
112         pidsMap_.erase(iter);
113     }
114 }
115 
GetProcessRecord(pid_t pid)116 std::shared_ptr<ProcessRecord> Application::GetProcessRecord(pid_t pid)
117 {
118     auto iter = pidsMap_.find(pid);
119     if (iter != pidsMap_.end()) {
120         return iter->second;
121     }
122     return nullptr;
123 }
124 
GetProcessRecordNonNull(pid_t pid)125 std::shared_ptr<ProcessRecord> Application::GetProcessRecordNonNull(pid_t pid)
126 {
127     auto iter = pidsMap_.find(pid);
128     if (iter != pidsMap_.end()) {
129         return iter->second;
130     }
131     auto pr = std::make_shared<ProcessRecord>(this->GetUid(), pid);
132     pidsMap_[pid] = pr;
133     return pr;
134 }
135 
FindProcessRecordByToken(uintptr_t token)136 std::shared_ptr<ProcessRecord> Application::FindProcessRecordByToken(uintptr_t token)
137 {
138     for (auto iter = pidsMap_.begin(); iter != pidsMap_.end(); iter++) {
139         auto pr = iter->second;
140         if (pr->HasAbility(token)) {
141             return pr;
142         }
143     }
144     return nullptr;
145 }
146 
SetName(const std::string & name)147 void Application::SetName(const std::string& name)
148 {
149     if (name_.empty()) {
150         name_ = name;
151     }
152 }
153 
SetMainProcess(std::shared_ptr<ProcessRecord> pr)154 void Application::SetMainProcess(std::shared_ptr<ProcessRecord> pr)
155 {
156     if (!mainProcess_) {
157         mainProcess_ = pr;
158     }
159 }
160 
FindProcessRecordByWindowId(uint32_t windowId)161 std::shared_ptr<ProcessRecord> Application::FindProcessRecordByWindowId(uint32_t windowId)
162 {
163     for (auto iter = pidsMap_.begin(); iter != pidsMap_.end(); iter++) {
164         auto pr = iter->second;
165         if (std::any_of(pr->windows_.begin(), pr->windows_.end(),
166             [ windowId ] (const auto& w) {
167                 return w->windowId_ == windowId;
168             })) {
169             return pr;
170         }
171     }
172     return nullptr;
173 }
174 
GetAppRecord(int32_t uid)175 std::shared_ptr<Application> Supervisor::GetAppRecord(int32_t uid)
176 {
177     auto iter = uidsMap_.find(uid);
178     if (iter != uidsMap_.end()) {
179         return iter->second;
180     }
181     return nullptr;
182 }
183 
GetAppRecordNonNull(int32_t uid)184 std::shared_ptr<Application> Supervisor::GetAppRecordNonNull(int32_t uid)
185 {
186     auto iter = uidsMap_.find(uid);
187     if (iter != uidsMap_.end()) {
188         return iter->second;
189     }
190     auto app = std::make_shared<Application>(uid);
191     uidsMap_[uid] = app;
192     return app;
193 }
194 
FindProcessRecord(pid_t pid)195 std::shared_ptr<ProcessRecord> Supervisor::FindProcessRecord(pid_t pid)
196 {
197     std::shared_ptr<ProcessRecord> pr = nullptr;
198     for (auto iter = uidsMap_.begin(); iter != uidsMap_.end(); iter++) {
199         auto app = iter->second;
200         pr = app->GetProcessRecord(pid);
201         if (pr) {
202             break;
203         }
204     }
205     return pr;
206 }
207 
RemoveApplication(int32_t uid)208 void Supervisor::RemoveApplication(int32_t uid)
209 {
210     auto iter = uidsMap_.find(uid);
211     if (iter != uidsMap_.end()) {
212         uidsMap_.erase(iter);
213     }
214 }
215 
SearchAbilityToken(std::shared_ptr<Application> & application,std::shared_ptr<ProcessRecord> & procRecord,uintptr_t token)216 void Supervisor::SearchAbilityToken(std::shared_ptr<Application> &application,
217     std::shared_ptr<ProcessRecord> &procRecord, uintptr_t token)
218 {
219     std::shared_ptr<ProcessRecord> pr = nullptr;
220     for (auto iter = uidsMap_.begin(); iter != uidsMap_.end(); iter++) {
221         auto app = iter->second;
222         pr = app->FindProcessRecordByToken(token);
223         if (pr) {
224             application = app;
225             procRecord = pr;
226             break;
227         }
228     }
229 }
230 
SearchWindowId(std::shared_ptr<Application> & application,std::shared_ptr<ProcessRecord> & procRecord,uint32_t windowId)231 void Supervisor::SearchWindowId(std::shared_ptr<Application> &application,
232     std::shared_ptr<ProcessRecord> &procRecord, uint32_t windowId)
233 {
234     std::shared_ptr<ProcessRecord> pr = nullptr;
235     for (auto iter = uidsMap_.begin(); iter != uidsMap_.end(); iter++) {
236         auto app = iter->second;
237         pr = app->FindProcessRecordByWindowId(windowId);
238         if (pr) {
239             application = app;
240             procRecord = pr;
241             break;
242         }
243     }
244 }
245 } // namespace ResourceSchedule
246 } // namespace OHOS
247