1 /*
2 * Copyright (c) 2021-2023 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 "ams_mgr_scheduler.h"
17 #include <sys/types.h>
18
19 #include "datetime_ex.h"
20 #include "ipc_skeleton.h"
21 #include "system_ability_definition.h"
22
23 #include "accesstoken_kit.h"
24 #include "app_death_recipient.h"
25 #include "app_mgr_constants.h"
26 #include "hilog_wrapper.h"
27 #include "perf_profile.h"
28 #include "permission_constants.h"
29 #include "permission_verification.h"
30
31 namespace OHOS {
32 namespace AppExecFwk {
33 namespace {
34 const std::string TASK_LOAD_ABILITY = "LoadAbilityTask";
35 const std::string TASK_TERMINATE_ABILITY = "TerminateAbilityTask";
36 const std::string TASK_UPDATE_ABILITY_STATE = "UpdateAbilityStateTask";
37 const std::string TASK_UPDATE_EXTENSION_STATE = "UpdateExtensionStateTask";
38 const std::string TASK_REGISTER_APP_STATE_CALLBACK = "RegisterAppStateCallbackTask";
39 const std::string TASK_STOP_ALL_PROCESS = "StopAllProcessTask";
40 const std::string TASK_ABILITY_BEHAVIOR_ANALYSIS = "AbilityBehaviorAnalysisTask";
41 const std::string TASK_KILL_PROCESS_BY_ABILITY_TOKEN = "KillProcessByAbilityTokenTask";
42 const std::string TASK_KILL_PROCESSES_BY_USERID = "KillProcessesByUserIdTask";
43 const std::string TASK_KILL_APPLICATION = "KillApplicationTask";
44 const std::string TASK_CLEAR_PROCESS_BY_ABILITY_TOKEN = "ClearProcessByAbilityTokenTask";
45 }; // namespace
46
AmsMgrScheduler(const std::shared_ptr<AppMgrServiceInner> & mgrServiceInner_,const std::shared_ptr<AAFwk::TaskHandlerWrap> & handler_)47 AmsMgrScheduler::AmsMgrScheduler(
48 const std::shared_ptr<AppMgrServiceInner> &mgrServiceInner_,
49 const std::shared_ptr<AAFwk::TaskHandlerWrap> &handler_)
50 : amsMgrServiceInner_(mgrServiceInner_), amsHandler_(handler_)
51 {}
52
~AmsMgrScheduler()53 AmsMgrScheduler::~AmsMgrScheduler()
54 {
55 HILOG_INFO("AmsMgrScheduler instance destroyed");
56 }
57
LoadAbility(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & preToken,const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<ApplicationInfo> & appInfo,const std::shared_ptr<AAFwk::Want> & want)58 void AmsMgrScheduler::LoadAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
59 const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo,
60 const std::shared_ptr<AAFwk::Want> &want)
61 {
62 if (!abilityInfo || !appInfo) {
63 HILOG_ERROR("param error");
64 return;
65 }
66
67 if (!IsReady()) {
68 return;
69 }
70
71 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
72 HILOG_ERROR("Permission verification failed.");
73 return;
74 }
75 PerfProfile::GetInstance().SetAbilityLoadStartTime(GetTickCount());
76 HILOG_INFO("SubmitLoadTask: %{public}s-%{public}s", abilityInfo->bundleName.c_str(), abilityInfo->name.c_str());
77 std::function<void()> loadAbilityFunc =
78 std::bind(&AppMgrServiceInner::LoadAbility, amsMgrServiceInner_, token, preToken, abilityInfo, appInfo, want);
79
80 amsHandler_->SubmitTask(loadAbilityFunc, AAFwk::TaskAttribute{
81 .taskName_ = TASK_LOAD_ABILITY,
82 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
83 });
84 }
85
UpdateAbilityState(const sptr<IRemoteObject> & token,const AbilityState state)86 void AmsMgrScheduler::UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state)
87 {
88 if (!IsReady()) {
89 return;
90 }
91
92 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
93 HILOG_ERROR("Permission verification failed.");
94 return;
95 }
96 std::function<void()> updateAbilityStateFunc =
97 std::bind(&AppMgrServiceInner::UpdateAbilityState, amsMgrServiceInner_, token, state);
98 amsHandler_->SubmitTask(updateAbilityStateFunc, AAFwk::TaskAttribute{
99 .taskName_ = TASK_UPDATE_ABILITY_STATE,
100 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
101 });
102 }
103
UpdateExtensionState(const sptr<IRemoteObject> & token,const ExtensionState state)104 void AmsMgrScheduler::UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state)
105 {
106 if (!IsReady()) {
107 return;
108 }
109
110 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
111 HILOG_ERROR("Permission verification failed.");
112 return;
113 }
114 std::function<void()> updateExtensionStateFunc =
115 std::bind(&AppMgrServiceInner::UpdateExtensionState, amsMgrServiceInner_, token, state);
116 amsHandler_->SubmitTask(updateExtensionStateFunc, AAFwk::TaskAttribute{
117 .taskName_ = TASK_UPDATE_EXTENSION_STATE,
118 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
119 });
120 }
121
TerminateAbility(const sptr<IRemoteObject> & token,bool clearMissionFlag)122 void AmsMgrScheduler::TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag)
123 {
124 if (!IsReady()) {
125 return;
126 }
127
128 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
129 HILOG_ERROR("Permission verification failed.");
130 return;
131 }
132 std::function<void()> terminateAbilityFunc =
133 std::bind(&AppMgrServiceInner::TerminateAbility, amsMgrServiceInner_, token, clearMissionFlag);
134 amsHandler_->SubmitTask(terminateAbilityFunc, AAFwk::TaskAttribute{
135 .taskName_ = TASK_TERMINATE_ABILITY,
136 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
137 });
138 }
139
RegisterAppStateCallback(const sptr<IAppStateCallback> & callback)140 void AmsMgrScheduler::RegisterAppStateCallback(const sptr<IAppStateCallback> &callback)
141 {
142 if (!IsReady()) {
143 return;
144 }
145 std::function<void()> registerAppStateCallbackFunc =
146 std::bind(&AppMgrServiceInner::RegisterAppStateCallback, amsMgrServiceInner_, callback);
147 amsHandler_->SubmitTask(registerAppStateCallbackFunc, TASK_REGISTER_APP_STATE_CALLBACK);
148 }
149
AbilityBehaviorAnalysis(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & preToken,const int32_t visibility,const int32_t perceptibility,const int32_t connectionState)150 void AmsMgrScheduler::AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
151 const int32_t visibility, const int32_t perceptibility, const int32_t connectionState)
152 {
153 if (!IsReady()) {
154 return;
155 }
156
157 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
158 HILOG_ERROR("Permission verification failed.");
159 return;
160 }
161 std::function<void()> abilityBehaviorAnalysisFunc = std::bind(&AppMgrServiceInner::AbilityBehaviorAnalysis,
162 amsMgrServiceInner_, token, preToken, visibility, perceptibility, connectionState);
163 amsHandler_->SubmitTask(abilityBehaviorAnalysisFunc, TASK_ABILITY_BEHAVIOR_ANALYSIS);
164 }
165
KillProcessByAbilityToken(const sptr<IRemoteObject> & token)166 void AmsMgrScheduler::KillProcessByAbilityToken(const sptr<IRemoteObject> &token)
167 {
168 if (!IsReady()) {
169 return;
170 }
171
172 if (amsMgrServiceInner_->VerifyProcessPermission(token) != ERR_OK) {
173 HILOG_ERROR("%{public}s: Permission verification failed", __func__);
174 return;
175 }
176
177 std::function<void()> killProcessByAbilityTokenFunc =
178 std::bind(&AppMgrServiceInner::KillProcessByAbilityToken, amsMgrServiceInner_, token);
179 amsHandler_->SubmitTask(killProcessByAbilityTokenFunc, TASK_KILL_PROCESS_BY_ABILITY_TOKEN);
180 }
181
KillProcessesByUserId(int32_t userId)182 void AmsMgrScheduler::KillProcessesByUserId(int32_t userId)
183 {
184 if (!IsReady()) {
185 return;
186 }
187
188 if (!AAFwk::PermissionVerification::GetInstance()->JudgeCallerIsAllowedToUseSystemAPI()) {
189 HILOG_ERROR("The caller is not system-app, can not use system-api");
190 return;
191 }
192
193 auto permission = AAFwk::PermissionConstants::PERMISSION_CLEAN_BACKGROUND_PROCESSES;
194 if (amsMgrServiceInner_->VerifyAccountPermission(permission, userId) == ERR_PERMISSION_DENIED) {
195 HILOG_ERROR("%{public}s: Permission verification failed", __func__);
196 return;
197 }
198
199 std::function<void()> killProcessesByUserIdFunc =
200 std::bind(&AppMgrServiceInner::KillProcessesByUserId, amsMgrServiceInner_, userId);
201 amsHandler_->SubmitTask(killProcessesByUserIdFunc, TASK_KILL_PROCESSES_BY_USERID);
202 }
203
KillProcessWithAccount(const std::string & bundleName,const int accountId)204 int32_t AmsMgrScheduler::KillProcessWithAccount(const std::string &bundleName, const int accountId)
205 {
206 HILOG_INFO("bundleName = %{public}s, accountId = %{public}d", bundleName.c_str(), accountId);
207 if (!IsReady()) {
208 return ERR_INVALID_OPERATION;
209 }
210 return amsMgrServiceInner_->KillApplicationByUserId(bundleName, accountId);
211 }
212
AbilityAttachTimeOut(const sptr<IRemoteObject> & token)213 void AmsMgrScheduler::AbilityAttachTimeOut(const sptr<IRemoteObject> &token)
214 {
215 HILOG_INFO("AmsMgrScheduler AttachTimeOut begin");
216 if (!IsReady()) {
217 return;
218 }
219
220 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
221 HILOG_ERROR("Permission verification failed.");
222 return;
223 }
224 auto task = [=]() { amsMgrServiceInner_->HandleAbilityAttachTimeOut(token); };
225 amsHandler_->SubmitTask(task);
226 }
227
PrepareTerminate(const sptr<IRemoteObject> & token)228 void AmsMgrScheduler::PrepareTerminate(const sptr<IRemoteObject> &token)
229 {
230 HILOG_DEBUG("Notify AppMgrService to prepare to terminate the ability.");
231 if (!IsReady()) {
232 return;
233 }
234
235 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
236 HILOG_ERROR("Permission verification failed.");
237 return;
238 }
239 auto task = [=]() { amsMgrServiceInner_->PrepareTerminate(token); };
240 amsHandler_->SubmitTask(task, AAFwk::TaskQoS::USER_INTERACTIVE);
241 }
242
UpdateApplicationInfoInstalled(const std::string & bundleName,const int uid)243 int32_t AmsMgrScheduler::UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid)
244 {
245 if (!IsReady()) {
246 return ERR_INVALID_OPERATION;
247 }
248
249 return amsMgrServiceInner_->UpdateApplicationInfoInstalled(bundleName, uid);
250 }
251
KillApplication(const std::string & bundleName)252 int32_t AmsMgrScheduler::KillApplication(const std::string &bundleName)
253 {
254 HILOG_INFO("bundleName = %{public}s", bundleName.c_str());
255 if (!IsReady()) {
256 return ERR_INVALID_OPERATION;
257 }
258
259 return amsMgrServiceInner_->KillApplication(bundleName);
260 }
261
KillApplicationByUid(const std::string & bundleName,const int uid)262 int32_t AmsMgrScheduler::KillApplicationByUid(const std::string &bundleName, const int uid)
263 {
264 HILOG_INFO("bundleName = %{public}s, uid = %{public}d", bundleName.c_str(), uid);
265 if (!IsReady()) {
266 return ERR_INVALID_OPERATION;
267 }
268 return amsMgrServiceInner_->KillApplicationByUid(bundleName, uid);
269 }
270
KillApplicationSelf()271 int32_t AmsMgrScheduler::KillApplicationSelf()
272 {
273 if (!IsReady()) {
274 return ERR_INVALID_OPERATION;
275 }
276 return amsMgrServiceInner_->KillApplicationSelf();
277 }
278
IsReady() const279 bool AmsMgrScheduler::IsReady() const
280 {
281 if (!amsMgrServiceInner_) {
282 HILOG_ERROR("amsMgrServiceInner_ is null");
283 return false;
284 }
285 if (!amsHandler_) {
286 HILOG_ERROR("amsHandler_ is null");
287 return false;
288 }
289 return true;
290 }
291
GetRunningProcessInfoByToken(const sptr<IRemoteObject> & token,AppExecFwk::RunningProcessInfo & info)292 void AmsMgrScheduler::GetRunningProcessInfoByToken(
293 const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info)
294 {
295 if (!IsReady()) {
296 return;
297 }
298
299 amsMgrServiceInner_->GetRunningProcessInfoByToken(token, info);
300 }
301
GetRunningProcessInfoByPid(const pid_t pid,OHOS::AppExecFwk::RunningProcessInfo & info)302 void AmsMgrScheduler::GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info)
303 {
304 if (!IsReady()) {
305 return;
306 }
307
308 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
309 HILOG_ERROR("Permission verification failed.");
310 return;
311 }
312 amsMgrServiceInner_->GetRunningProcessInfoByPid(pid, info);
313 }
314
SetAbilityForegroundingFlagToAppRecord(const pid_t pid)315 void AmsMgrScheduler::SetAbilityForegroundingFlagToAppRecord(const pid_t pid)
316 {
317 if (!IsReady()) {
318 return;
319 }
320 amsMgrServiceInner_->SetAbilityForegroundingFlagToAppRecord(pid);
321 }
322
StartSpecifiedAbility(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo)323 void AmsMgrScheduler::StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo)
324 {
325 if (!IsReady()) {
326 return;
327 }
328
329 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
330 HILOG_ERROR("Permission verification failed.");
331 return;
332 }
333 auto task = [=]() { amsMgrServiceInner_->StartSpecifiedAbility(want, abilityInfo); };
334 amsHandler_->SubmitTask(task, AAFwk::TaskQoS::USER_INTERACTIVE);
335 }
336
StartSpecifiedProcess(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo)337 void AmsMgrScheduler::StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo)
338 {
339 if (!IsReady()) {
340 HILOG_WARN("not ready.");
341 return;
342 }
343
344 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
345 HILOG_ERROR("Permission verification failed.");
346 return;
347 }
348 auto task = [=]() { amsMgrServiceInner_->StartSpecifiedProcess(want, abilityInfo); };
349 amsHandler_->SubmitTask(task, AAFwk::TaskQoS::USER_INTERACTIVE);
350 }
351
RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> & response)352 void AmsMgrScheduler::RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response)
353 {
354 if (!IsReady()) {
355 return;
356 }
357 auto task = [=]() { amsMgrServiceInner_->RegisterStartSpecifiedAbilityResponse(response); };
358 amsHandler_->SubmitTask(task);
359 }
360
GetApplicationInfoByProcessID(const int pid,AppExecFwk::ApplicationInfo & application,bool & debug)361 int AmsMgrScheduler::GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug)
362 {
363 if (!IsReady()) {
364 return ERR_INVALID_OPERATION;
365 }
366 return amsMgrServiceInner_->GetApplicationInfoByProcessID(pid, application, debug);
367 }
368
SetCurrentUserId(const int32_t userId)369 void AmsMgrScheduler::SetCurrentUserId(const int32_t userId)
370 {
371 if (!IsReady()) {
372 return;
373 }
374 amsMgrServiceInner_->SetCurrentUserId(userId);
375 }
376
GetBundleNameByPid(const int pid,std::string & bundleName,int32_t & uid)377 int32_t AmsMgrScheduler::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid)
378 {
379 if (!IsReady()) {
380 return ERR_INVALID_OPERATION;
381 }
382 return amsMgrServiceInner_->GetBundleNameByPid(pid, bundleName, uid);
383 }
384
RegisterAppDebugListener(const sptr<IAppDebugListener> & listener)385 int32_t AmsMgrScheduler::RegisterAppDebugListener(const sptr<IAppDebugListener> &listener)
386 {
387 if (!IsReady()) {
388 HILOG_ERROR("AmsMgrService is not ready.");
389 return ERR_INVALID_OPERATION;
390 }
391 return amsMgrServiceInner_->RegisterAppDebugListener(listener);
392 }
393
UnregisterAppDebugListener(const sptr<IAppDebugListener> & listener)394 int32_t AmsMgrScheduler::UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener)
395 {
396 if (!IsReady()) {
397 HILOG_ERROR("AmsMgrService is not ready.");
398 return ERR_INVALID_OPERATION;
399 }
400 return amsMgrServiceInner_->UnregisterAppDebugListener(listener);
401 }
402
AttachAppDebug(const std::string & bundleName)403 int32_t AmsMgrScheduler::AttachAppDebug(const std::string &bundleName)
404 {
405 if (!IsReady()) {
406 HILOG_ERROR("AmsMgrService is not ready.");
407 return ERR_INVALID_OPERATION;
408 }
409 return amsMgrServiceInner_->AttachAppDebug(bundleName);
410 }
411
DetachAppDebug(const std::string & bundleName)412 int32_t AmsMgrScheduler::DetachAppDebug(const std::string &bundleName)
413 {
414 if (!IsReady()) {
415 HILOG_ERROR("AmsMgrService is not ready.");
416 return ERR_INVALID_OPERATION;
417 }
418 return amsMgrServiceInner_->DetachAppDebug(bundleName);
419 }
420
RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> & response)421 int32_t AmsMgrScheduler::RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response)
422 {
423 if (!IsReady()) {
424 HILOG_ERROR("AmsMgrService is not ready.");
425 return ERR_INVALID_OPERATION;
426 }
427 return amsMgrServiceInner_->RegisterAbilityDebugResponse(response);
428 }
429
IsAttachDebug(const std::string & bundleName)430 bool AmsMgrScheduler::IsAttachDebug(const std::string &bundleName)
431 {
432 if (!IsReady()) {
433 HILOG_ERROR("AmsMgrService is not ready.");
434 return false;
435 }
436 return amsMgrServiceInner_->IsAttachDebug(bundleName);
437 }
438
ClearProcessByToken(sptr<IRemoteObject> token)439 void AmsMgrScheduler::ClearProcessByToken(sptr<IRemoteObject> token)
440 {
441 if (!IsReady()) {
442 return;
443 }
444
445 auto callerTokenId = IPCSkeleton::GetCallingTokenID();
446 Security::AccessToken::NativeTokenInfo nativeInfo;
447 Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(callerTokenId, nativeInfo);
448 if (nativeInfo.processName != "foundation") {
449 HILOG_ERROR("caller is not foundation.");
450 return;
451 }
452
453 std::function<void()> clearProcessByTokenFunc =
454 std::bind(&AppMgrServiceInner::ClearProcessByToken, amsMgrServiceInner_, token);
455 amsHandler_->SubmitTask(clearProcessByTokenFunc, TASK_CLEAR_PROCESS_BY_ABILITY_TOKEN);
456 }
457 } // namespace AppExecFwk
458 } // namespace OHOS
459