1 /*
2 * Copyright (c) 2021-2025 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 "app_utils.h"
27 #include "hilog_tag_wrapper.h"
28 #include "perf_profile.h"
29 #include "permission_constants.h"
30 #include "permission_verification.h"
31
32 namespace OHOS {
33 namespace AppExecFwk {
34 namespace {
35 constexpr const char* TASK_TERMINATE_ABILITY = "TerminateAbilityTask";
36 constexpr const char* TASK_UPDATE_ABILITY_STATE = "UpdateAbilityStateTask";
37 constexpr const char* TASK_UPDATE_EXTENSION_STATE = "UpdateExtensionStateTask";
38 constexpr const char* TASK_REGISTER_APP_STATE_CALLBACK = "RegisterAppStateCallbackTask";
39 constexpr const char* TASK_STOP_ALL_PROCESS = "StopAllProcessTask";
40 constexpr const char* TASK_ABILITY_BEHAVIOR_ANALYSIS = "AbilityBehaviorAnalysisTask";
41 constexpr const char* TASK_KILL_PROCESS_BY_ABILITY_TOKEN = "KillProcessByAbilityTokenTask";
42 constexpr const char* TASK_KILL_PROCESSES_BY_USERID = "KillProcessesByUserIdTask";
43 constexpr const char* TASK_KILL_PROCESSES_BY_PIDS = "KillProcessesByPids";
44 constexpr const char* TASK_ATTACH_PID_TO_PARENT = "AttachPidToParent";
45 constexpr const char* TASK_KILL_APPLICATION = "KillApplicationTask";
46 constexpr const char* TASK_CLEAR_PROCESS_BY_ABILITY_TOKEN = "ClearProcessByAbilityTokenTask";
47 constexpr const char* FOUNDATION_NAME = "foundation";
48 constexpr const char* SCENE_BOARD_BUNDLE_NAME = "com.ohos.sceneboard";
49 constexpr const char* SCENEBOARD_ABILITY_NAME = "com.ohos.sceneboard.MainAbility";
50 constexpr const char* TASK_SCENE_BOARD_ATTACH_TIMEOUT = "sceneBoardAttachTimeoutTask";
51 constexpr const char* TASK_ATTACHED_TO_STATUS_BAR = "AttachedToStatusBar";
52 constexpr const char* TASK_BLOCK_PROCESS_CACHE_BY_PIDS = "BlockProcessCacheByPids";
53 constexpr const char* POWER_OFF_ABILITY = "PoweroffAbility";
54 constexpr int32_t SCENE_BOARD_ATTACH_TIMEOUT_TASK_TIME = 1000;
55 constexpr int32_t LOAD_TASK_TIMEOUT = 60000; // ms
56 }; // namespace
57
AmsMgrScheduler(const std::shared_ptr<AppMgrServiceInner> & mgrServiceInner_,const std::shared_ptr<AAFwk::TaskHandlerWrap> & handler_)58 AmsMgrScheduler::AmsMgrScheduler(
59 const std::shared_ptr<AppMgrServiceInner> &mgrServiceInner_,
60 const std::shared_ptr<AAFwk::TaskHandlerWrap> &handler_)
61 : amsMgrServiceInner_(mgrServiceInner_), amsHandler_(handler_)
62 {}
63
~AmsMgrScheduler()64 AmsMgrScheduler::~AmsMgrScheduler()
65 {
66 TAG_LOGI(AAFwkTag::APPMGR, "call");
67 }
68
LoadAbility(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<ApplicationInfo> & appInfo,const std::shared_ptr<AAFwk::Want> & want,std::shared_ptr<AbilityRuntime::LoadParam> loadParam)69 void AmsMgrScheduler::LoadAbility(const std::shared_ptr<AbilityInfo> &abilityInfo,
70 const std::shared_ptr<ApplicationInfo> &appInfo,
71 const std::shared_ptr<AAFwk::Want> &want, std::shared_ptr<AbilityRuntime::LoadParam> loadParam)
72 {
73 if (!abilityInfo || !appInfo) {
74 TAG_LOGE(AAFwkTag::APPMGR, "param error");
75 return;
76 }
77
78 if (!IsReady()) {
79 return;
80 }
81
82 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
83 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
84 return;
85 }
86 PerfProfile::GetInstance().SetAbilityLoadStartTime(GetTickCount());
87 TAG_LOGI(AAFwkTag::APPMGR, "SubmitLoadTask: %{public}s-%{public}s", abilityInfo->bundleName.c_str(),
88 abilityInfo->name.c_str());
89 std::function<void()> loadAbilityFunc = [amsMgrServiceInner = amsMgrServiceInner_,
90 abilityInfo, appInfo, want, loadParam]() {
91 amsMgrServiceInner->LoadAbility(abilityInfo, appInfo, want, loadParam);
92 };
93
94 // cache other application load ability task before scene board attach
95 if (!amsMgrServiceInner_->GetSceneBoardAttachFlag() && abilityInfo->bundleName != SCENE_BOARD_BUNDLE_NAME) {
96 amsMgrServiceInner_->CacheLoadAbilityTask(std::move(loadAbilityFunc));
97 return;
98 }
99 if (abilityInfo->bundleName == SCENE_BOARD_BUNDLE_NAME && abilityInfo->name == SCENEBOARD_ABILITY_NAME) {
100 amsMgrServiceInner_->SetSceneBoardAttachFlag(false);
101 // set scene board attach timeout task
102 std::weak_ptr<AppMgrServiceInner> amsMgrServiceInner = amsMgrServiceInner_;
103 auto timeoutTask = [amsMgrServiceInner]() {
104 auto inner = amsMgrServiceInner.lock();
105 if (inner != nullptr) {
106 inner->SetSceneBoardAttachFlag(true);
107 }
108 };
109 amsHandler_->SubmitTask(timeoutTask, TASK_SCENE_BOARD_ATTACH_TIMEOUT, SCENE_BOARD_ATTACH_TIMEOUT_TASK_TIME);
110 }
111
112 AAFwk::TaskAttribute taskAttr{
113 .taskName_ = "LoadAbilityTask",
114 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE,
115 .timeoutMillis_ = LOAD_TASK_TIMEOUT
116 };
117
118 if (abilityInfo->bundleName == AAFwk::AppUtils::GetInstance().GetMigrateClientBundleName()) {
119 taskAttr.taskPriority_ = AAFwk::TaskQueuePriority::IMMEDIATE;
120 }
121 if (abilityInfo->bundleName == SCENE_BOARD_BUNDLE_NAME && abilityInfo->name == POWER_OFF_ABILITY) {
122 taskAttr.insertHead_ = true;
123 }
124
125 amsHandler_->SubmitTask(loadAbilityFunc, taskAttr);
126 }
127
UpdateAbilityState(const sptr<IRemoteObject> & token,const AbilityState state)128 void AmsMgrScheduler::UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state)
129 {
130 if (!IsReady()) {
131 return;
132 }
133
134 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
135 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
136 return;
137 }
138 std::function<void()> updateAbilityStateFunc = [amsMgrServiceInner = amsMgrServiceInner_, token, state] () {
139 amsMgrServiceInner->UpdateAbilityState(token, state);
140 };
141 amsHandler_->SubmitTask(updateAbilityStateFunc, AAFwk::TaskAttribute{
142 .taskName_ = TASK_UPDATE_ABILITY_STATE,
143 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
144 });
145 }
146
UpdateExtensionState(const sptr<IRemoteObject> & token,const ExtensionState state)147 void AmsMgrScheduler::UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state)
148 {
149 if (!IsReady()) {
150 return;
151 }
152
153 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
154 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
155 return;
156 }
157 std::function<void()> updateExtensionStateFunc = [amsMgrServiceInner = amsMgrServiceInner_, token, state]() {
158 amsMgrServiceInner->UpdateExtensionState(token, state);
159 };
160 amsHandler_->SubmitTask(updateExtensionStateFunc, AAFwk::TaskAttribute{
161 .taskName_ = TASK_UPDATE_EXTENSION_STATE,
162 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
163 });
164 }
165
TerminateAbility(const sptr<IRemoteObject> & token,bool clearMissionFlag)166 void AmsMgrScheduler::TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag)
167 {
168 if (!IsReady()) {
169 return;
170 }
171
172 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
173 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
174 return;
175 }
176 std::function<void()> terminateAbilityFunc = [amsMgrServiceInner = amsMgrServiceInner_, token, clearMissionFlag]() {
177 amsMgrServiceInner->TerminateAbility(token, clearMissionFlag);
178 };
179 amsHandler_->SubmitTask(terminateAbilityFunc, AAFwk::TaskAttribute{
180 .taskName_ = TASK_TERMINATE_ABILITY,
181 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
182 });
183 }
184
RegisterAppStateCallback(const sptr<IAppStateCallback> & callback)185 void AmsMgrScheduler::RegisterAppStateCallback(const sptr<IAppStateCallback> &callback)
186 {
187 if (!AAFwk::PermissionVerification::GetInstance()->IsSACall()) {
188 TAG_LOGE(AAFwkTag::APPMGR, "caller is not SA");
189 return;
190 }
191 if (!IsReady()) {
192 return;
193 }
194 std::function<void()> registerAppStateCallbackFunc = [amsMgrServiceInner = amsMgrServiceInner_, callback]() {
195 amsMgrServiceInner->RegisterAppStateCallback(callback);
196 };
197 amsHandler_->SubmitTask(registerAppStateCallbackFunc, TASK_REGISTER_APP_STATE_CALLBACK);
198 }
199
KillProcessByAbilityToken(const sptr<IRemoteObject> & token)200 void AmsMgrScheduler::KillProcessByAbilityToken(const sptr<IRemoteObject> &token)
201 {
202 if (!IsReady()) {
203 return;
204 }
205
206 if (amsMgrServiceInner_->VerifyKillProcessPermission(token) != ERR_OK) {
207 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
208 return;
209 }
210
211 std::function<void()> killProcessByAbilityTokenFunc = [amsMgrServiceInner = amsMgrServiceInner_, token]() {
212 amsMgrServiceInner->KillProcessByAbilityToken(token);
213 };
214 amsHandler_->SubmitTask(killProcessByAbilityTokenFunc, TASK_KILL_PROCESS_BY_ABILITY_TOKEN);
215 }
216
KillProcessesByUserId(int32_t userId,bool isNeedSendAppSpawnMsg,sptr<AAFwk::IUserCallback> callback)217 void AmsMgrScheduler::KillProcessesByUserId(int32_t userId, bool isNeedSendAppSpawnMsg,
218 sptr<AAFwk::IUserCallback> callback)
219 {
220 if (!IsReady()) {
221 return;
222 }
223
224 if (!AAFwk::PermissionVerification::GetInstance()->JudgeCallerIsAllowedToUseSystemAPI()) {
225 TAG_LOGE(AAFwkTag::APPMGR, "caller is not SA");
226 return;
227 }
228
229 bool isCallingFromFoundation =
230 AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_NAME);
231 auto permission = AAFwk::PermissionConstants::PERMISSION_CLEAN_BACKGROUND_PROCESSES;
232 if (!isCallingFromFoundation &&
233 amsMgrServiceInner_->VerifyAccountPermission(permission, userId) == ERR_PERMISSION_DENIED) {
234 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
235 return;
236 }
237
238 std::function<void()> killProcessesByUserIdFunc = [amsMgrServiceInner = amsMgrServiceInner_, userId,
239 isNeedSendAppSpawnMsg, callback]() {
240 amsMgrServiceInner->KillProcessesByUserId(userId, isNeedSendAppSpawnMsg, callback);
241 };
242 amsHandler_->SubmitTask(killProcessesByUserIdFunc, TASK_KILL_PROCESSES_BY_USERID);
243 }
244
KillProcessesByPids(const std::vector<int32_t> & pids,const std::string & reason)245 void AmsMgrScheduler::KillProcessesByPids(const std::vector<int32_t> &pids, const std::string &reason)
246 {
247 if (!IsReady()) {
248 return;
249 }
250
251 pid_t callingPid = IPCSkeleton::GetCallingPid();
252 pid_t pid = getprocpid();
253 if (callingPid != pid) {
254 TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call");
255 return;
256 }
257
258 std::function<void()> killProcessesByPidsFunc = [amsMgrServiceInner = amsMgrServiceInner_,
259 pidsInner = pids, reasonInner = reason]() {
260 amsMgrServiceInner->KillProcessesByPids(pidsInner, reasonInner);
261 };
262 amsHandler_->SubmitTask(killProcessesByPidsFunc, TASK_KILL_PROCESSES_BY_PIDS);
263 }
264
AttachPidToParent(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & callerToken)265 void AmsMgrScheduler::AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken)
266 {
267 if (!IsReady()) {
268 return;
269 }
270
271 pid_t callingPid = IPCSkeleton::GetCallingPid();
272 pid_t pid = getprocpid();
273 if (callingPid != pid) {
274 TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call");
275 return;
276 }
277
278 std::function<void()> attachPidToParentFunc = [amsMgrServiceInner = amsMgrServiceInner_, token, callerToken]() {
279 amsMgrServiceInner->AttachPidToParent(token, callerToken);
280 };
281 amsHandler_->SubmitTask(attachPidToParentFunc, TASK_ATTACH_PID_TO_PARENT);
282 }
283
KillProcessWithAccount(const std::string & bundleName,const int accountId,const bool clearPageStack,int32_t appIndex)284 int32_t AmsMgrScheduler::KillProcessWithAccount(
285 const std::string &bundleName, const int accountId, const bool clearPageStack, int32_t appIndex)
286 {
287 TAG_LOGI(AAFwkTag::APPMGR, "bundle=%{public}s, appIndex=%{public}d, userId=%{public}d, clearPageStack=%{public}d",
288 bundleName.c_str(), appIndex, accountId, clearPageStack);
289 if (!IsReady()) {
290 return ERR_INVALID_OPERATION;
291 }
292 return amsMgrServiceInner_->KillApplicationByUserId(bundleName, appIndex, accountId, clearPageStack,
293 "KillProcessWithAccount");
294 }
295
KillProcessesInBatch(const std::vector<int32_t> & pids)296 int32_t AmsMgrScheduler::KillProcessesInBatch(const std::vector<int32_t> &pids)
297 {
298 TAG_LOGI(AAFwkTag::APPMGR, "pids.size=%{public}zu", pids.size());
299 if (!IsReady()) {
300 return ERR_INVALID_OPERATION;
301 }
302 return amsMgrServiceInner_->KillProcessesInBatch(pids);
303 }
304
AbilityAttachTimeOut(const sptr<IRemoteObject> & token)305 void AmsMgrScheduler::AbilityAttachTimeOut(const sptr<IRemoteObject> &token)
306 {
307 TAG_LOGI(AAFwkTag::APPMGR, "call");
308 if (!IsReady()) {
309 return;
310 }
311
312 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
313 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
314 return;
315 }
316 auto task = [amsMgrServiceInner = amsMgrServiceInner_, token]() {
317 amsMgrServiceInner->HandleAbilityAttachTimeOut(token);
318 };
319 amsHandler_->SubmitTask(task, "AbilityAttachTimeOut");
320 }
321
PrepareTerminate(const sptr<IRemoteObject> & token,bool clearMissionFlag)322 void AmsMgrScheduler::PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag)
323 {
324 TAG_LOGD(AAFwkTag::APPMGR, "Notify AppMgrService to prepare to terminate the ability.");
325 if (!IsReady()) {
326 return;
327 }
328
329 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
330 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
331 return;
332 }
333 auto task = [=]() { amsMgrServiceInner_->PrepareTerminate(token, clearMissionFlag); };
334 amsHandler_->SubmitTask(task, {
335 .taskName_ = "PrepareTerminate",
336 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
337 });
338 }
339
UpdateApplicationInfoInstalled(const std::string & bundleName,const int uid,const std::string & moduleName)340 int32_t AmsMgrScheduler::UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid,
341 const std::string &moduleName)
342 {
343 if (!IsReady()) {
344 return ERR_INVALID_OPERATION;
345 }
346
347 return amsMgrServiceInner_->UpdateApplicationInfoInstalled(bundleName, uid, moduleName);
348 }
349
KillApplication(const std::string & bundleName,bool clearPageStack,int32_t appIndex)350 int32_t AmsMgrScheduler::KillApplication(const std::string &bundleName, bool clearPageStack, int32_t appIndex)
351 {
352 TAG_LOGI(AAFwkTag::APPMGR, "bundleName = %{public}s, clearPageStack = %{public}d",
353 bundleName.c_str(), clearPageStack);
354 if (!IsReady()) {
355 return ERR_INVALID_OPERATION;
356 }
357
358 return amsMgrServiceInner_->KillApplication(bundleName, clearPageStack, appIndex);
359 }
360
ForceKillApplication(const std::string & bundleName,const int userId,const int appIndex)361 int32_t AmsMgrScheduler::ForceKillApplication(const std::string &bundleName,
362 const int userId, const int appIndex)
363 {
364 TAG_LOGI(AAFwkTag::APPMGR, "bundleName=%{public}s,userId=%{public}d,apIndex=%{public}d",
365 bundleName.c_str(), userId, appIndex);
366 if (!IsReady()) {
367 return ERR_INVALID_OPERATION;
368 }
369
370 return amsMgrServiceInner_->ForceKillApplication(bundleName, userId, appIndex);
371 }
372
KillProcessesByAccessTokenId(const uint32_t accessTokenId)373 int32_t AmsMgrScheduler::KillProcessesByAccessTokenId(const uint32_t accessTokenId)
374 {
375 TAG_LOGI(AAFwkTag::APPMGR, "accessTokenId=%{public}d", accessTokenId);
376 if (!IsReady()) {
377 return ERR_INVALID_OPERATION;
378 }
379
380 return amsMgrServiceInner_->KillProcessesByAccessTokenId(accessTokenId);
381 }
382
KillApplicationByUid(const std::string & bundleName,const int uid,const std::string & reason)383 int32_t AmsMgrScheduler::KillApplicationByUid(const std::string &bundleName, const int uid,
384 const std::string& reason)
385 {
386 TAG_LOGI(AAFwkTag::APPMGR, "bundleName = %{public}s, uid = %{public}d", bundleName.c_str(), uid);
387 if (!IsReady()) {
388 return ERR_INVALID_OPERATION;
389 }
390 return amsMgrServiceInner_->KillApplicationByUid(bundleName, uid, reason);
391 }
392
KillApplicationSelf(const bool clearPageStack,const std::string & reason)393 int32_t AmsMgrScheduler::KillApplicationSelf(const bool clearPageStack, const std::string& reason)
394 {
395 if (!IsReady()) {
396 return ERR_INVALID_OPERATION;
397 }
398 return amsMgrServiceInner_->KillApplicationSelf(clearPageStack, reason);
399 }
400
IsReady() const401 bool AmsMgrScheduler::IsReady() const
402 {
403 if (!amsMgrServiceInner_) {
404 TAG_LOGE(AAFwkTag::APPMGR, "null amsMgrServiceInner_");
405 return false;
406 }
407 if (!amsHandler_) {
408 TAG_LOGE(AAFwkTag::APPMGR, "null amsHandler_");
409 return false;
410 }
411 return true;
412 }
413
GetRunningProcessInfoByToken(const sptr<IRemoteObject> & token,AppExecFwk::RunningProcessInfo & info)414 void AmsMgrScheduler::GetRunningProcessInfoByToken(
415 const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info)
416 {
417 if (!IsReady()) {
418 return;
419 }
420
421 amsMgrServiceInner_->GetRunningProcessInfoByToken(token, info);
422 }
423
SetAbilityForegroundingFlagToAppRecord(const pid_t pid)424 void AmsMgrScheduler::SetAbilityForegroundingFlagToAppRecord(const pid_t pid)
425 {
426 if (!IsReady()) {
427 return;
428 }
429 amsMgrServiceInner_->SetAbilityForegroundingFlagToAppRecord(pid);
430 }
431
StartSpecifiedAbility(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId)432 void AmsMgrScheduler::StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
433 int32_t requestId)
434 {
435 if (!IsReady()) {
436 return;
437 }
438
439 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
440 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
441 return;
442 }
443 auto task = [=]() { amsMgrServiceInner_->StartSpecifiedAbility(want, abilityInfo, requestId); };
444 amsHandler_->SubmitTask(task, {
445 .taskName_ = "StartSpecifiedAbility",
446 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
447 });
448 }
449
PrepareTerminateApp(const pid_t pid,const std::string & moduleName)450 void AmsMgrScheduler::PrepareTerminateApp(const pid_t pid, const std::string &moduleName)
451 {
452 TAG_LOGD(AAFwkTag::APPKIT, "called");
453 if (!IsReady()) {
454 TAG_LOGW(AAFwkTag::APPMGR, "not ready");
455 return;
456 }
457 amsMgrServiceInner_->SchedulePrepareTerminate(pid, moduleName);
458 }
459
StartSpecifiedProcess(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId)460 void AmsMgrScheduler::StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
461 int32_t requestId)
462 {
463 if (!IsReady()) {
464 TAG_LOGW(AAFwkTag::APPMGR, "not ready");
465 return;
466 }
467
468 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
469 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
470 return;
471 }
472 auto task = [=]() { amsMgrServiceInner_->StartSpecifiedProcess(want, abilityInfo, requestId); };
473 amsHandler_->SubmitTask(task, {
474 .taskName_ = "StartSpecifiedProcess",
475 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
476 });
477 }
478
RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> & response)479 void AmsMgrScheduler::RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response)
480 {
481 if (!AAFwk::PermissionVerification::GetInstance()->IsSACall()) {
482 TAG_LOGE(AAFwkTag::APPMGR, "caller is not SA");
483 return;
484 }
485 if (!IsReady()) {
486 return;
487 }
488 auto task = [=]() { amsMgrServiceInner_->RegisterStartSpecifiedAbilityResponse(response); };
489 amsHandler_->SubmitTask(task, "RegisterStartSpecifiedAbilityResponse");
490 }
491
GetApplicationInfoByProcessID(const int pid,AppExecFwk::ApplicationInfo & application,bool & debug)492 int AmsMgrScheduler::GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug)
493 {
494 if (!IsReady()) {
495 return ERR_INVALID_OPERATION;
496 }
497 return amsMgrServiceInner_->GetApplicationInfoByProcessID(pid, application, debug);
498 }
499
NotifyAppMgrRecordExitReason(int32_t pid,int32_t reason,const std::string & exitMsg)500 int32_t AmsMgrScheduler::NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg)
501 {
502 if (!IsReady()) {
503 return ERR_INVALID_OPERATION;
504 }
505 return amsMgrServiceInner_->NotifyAppMgrRecordExitReason(pid, reason, exitMsg);
506 }
507
SetCurrentUserId(const int32_t userId)508 void AmsMgrScheduler::SetCurrentUserId(const int32_t userId)
509 {
510 if (!IsReady()) {
511 return;
512 }
513 amsMgrServiceInner_->SetCurrentUserId(userId);
514 }
515
SetEnableStartProcessFlagByUserId(int32_t userId,bool enableStartProcess)516 void AmsMgrScheduler::SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess)
517 {
518 if (!IsReady()) {
519 return;
520 }
521 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
522 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
523 return;
524 }
525 amsMgrServiceInner_->SetEnableStartProcessFlagByUserId(userId, enableStartProcess);
526 }
527
GetBundleNameByPid(const int pid,std::string & bundleName,int32_t & uid)528 int32_t AmsMgrScheduler::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid)
529 {
530 if (!IsReady()) {
531 return ERR_INVALID_OPERATION;
532 }
533 return amsMgrServiceInner_->GetBundleNameByPid(pid, bundleName, uid);
534 }
535
RegisterAppDebugListener(const sptr<IAppDebugListener> & listener)536 int32_t AmsMgrScheduler::RegisterAppDebugListener(const sptr<IAppDebugListener> &listener)
537 {
538 if (!IsReady()) {
539 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
540 return ERR_INVALID_OPERATION;
541 }
542 return amsMgrServiceInner_->RegisterAppDebugListener(listener);
543 }
544
UnregisterAppDebugListener(const sptr<IAppDebugListener> & listener)545 int32_t AmsMgrScheduler::UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener)
546 {
547 if (!IsReady()) {
548 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
549 return ERR_INVALID_OPERATION;
550 }
551 return amsMgrServiceInner_->UnregisterAppDebugListener(listener);
552 }
553
AttachAppDebug(const std::string & bundleName,bool isDebugFromLocal)554 int32_t AmsMgrScheduler::AttachAppDebug(const std::string &bundleName, bool isDebugFromLocal)
555 {
556 if (!IsReady()) {
557 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
558 return ERR_INVALID_OPERATION;
559 }
560 if (!AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_NAME)) {
561 TAG_LOGE(AAFwkTag::APPMGR, "caller is not foundation");
562 return ERR_INVALID_OPERATION;
563 }
564 return amsMgrServiceInner_->AttachAppDebug(bundleName, isDebugFromLocal);
565 }
566
DetachAppDebug(const std::string & bundleName)567 int32_t AmsMgrScheduler::DetachAppDebug(const std::string &bundleName)
568 {
569 if (!IsReady()) {
570 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
571 return ERR_INVALID_OPERATION;
572 }
573 if (!AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_NAME)) {
574 TAG_LOGE(AAFwkTag::APPMGR, "caller is not foundation");
575 return ERR_INVALID_OPERATION;
576 }
577 return amsMgrServiceInner_->DetachAppDebug(bundleName);
578 }
579
SetAppWaitingDebug(const std::string & bundleName,bool isPersist)580 int32_t AmsMgrScheduler::SetAppWaitingDebug(const std::string &bundleName, bool isPersist)
581 {
582 if (!IsReady()) {
583 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
584 return ERR_INVALID_OPERATION;
585 }
586 return amsMgrServiceInner_->SetAppWaitingDebug(bundleName, isPersist);
587 }
588
CancelAppWaitingDebug()589 int32_t AmsMgrScheduler::CancelAppWaitingDebug()
590 {
591 if (!IsReady()) {
592 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
593 return ERR_INVALID_OPERATION;
594 }
595 return amsMgrServiceInner_->CancelAppWaitingDebug();
596 }
597
GetWaitingDebugApp(std::vector<std::string> & debugInfoList)598 int32_t AmsMgrScheduler::GetWaitingDebugApp(std::vector<std::string> &debugInfoList)
599 {
600 if (!IsReady()) {
601 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
602 return ERR_INVALID_OPERATION;
603 }
604 return amsMgrServiceInner_->GetWaitingDebugApp(debugInfoList);
605 }
606
IsWaitingDebugApp(const std::string & bundleName)607 bool AmsMgrScheduler::IsWaitingDebugApp(const std::string &bundleName)
608 {
609 if (!IsReady()) {
610 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
611 return false;
612 }
613 return amsMgrServiceInner_->IsWaitingDebugApp(bundleName);
614 }
615
ClearNonPersistWaitingDebugFlag()616 void AmsMgrScheduler::ClearNonPersistWaitingDebugFlag()
617 {
618 if (!IsReady()) {
619 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
620 return;
621 }
622 amsMgrServiceInner_->ClearNonPersistWaitingDebugFlag();
623 }
624
RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> & response)625 int32_t AmsMgrScheduler::RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response)
626 {
627 if (!IsReady()) {
628 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
629 return ERR_INVALID_OPERATION;
630 }
631 return amsMgrServiceInner_->RegisterAbilityDebugResponse(response);
632 }
633
IsAttachDebug(const std::string & bundleName)634 bool AmsMgrScheduler::IsAttachDebug(const std::string &bundleName)
635 {
636 if (!IsReady()) {
637 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
638 return false;
639 }
640 return amsMgrServiceInner_->IsAttachDebug(bundleName);
641 }
642
SetKeepAliveEnableState(const std::string & bundleName,bool enable,int32_t uid)643 void AmsMgrScheduler::SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid)
644 {
645 if (!IsReady()) {
646 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
647 return;
648 }
649 amsMgrServiceInner_->SetKeepAliveEnableState(bundleName, enable, uid);
650 }
651
SetKeepAliveDkv(const std::string & bundleName,bool enable,int32_t uid)652 void AmsMgrScheduler::SetKeepAliveDkv(const std::string &bundleName, bool enable, int32_t uid)
653 {
654 if (!IsReady()) {
655 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
656 return;
657 }
658 amsMgrServiceInner_->SetKeepAliveDkv(bundleName, enable, uid);
659 }
660
ClearProcessByToken(sptr<IRemoteObject> token)661 void AmsMgrScheduler::ClearProcessByToken(sptr<IRemoteObject> token)
662 {
663 if (!IsReady()) {
664 return;
665 }
666
667 auto callerTokenId = IPCSkeleton::GetCallingTokenID();
668 Security::AccessToken::NativeTokenInfo nativeInfo;
669 Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(callerTokenId, nativeInfo);
670 if (nativeInfo.processName != "foundation") {
671 TAG_LOGE(AAFwkTag::APPMGR, "not foundation");
672 return;
673 }
674
675 std::function<void()> clearProcessByTokenFunc = [amsMgrServiceInner = amsMgrServiceInner_, token]() {
676 amsMgrServiceInner->ClearProcessByToken(token);
677 };
678 amsHandler_->SubmitTask(clearProcessByTokenFunc, TASK_CLEAR_PROCESS_BY_ABILITY_TOKEN);
679 }
680
IsMemorySizeSufficent()681 bool AmsMgrScheduler::IsMemorySizeSufficent()
682 {
683 if (!IsReady()) {
684 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
685 return true;
686 }
687 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
688 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
689 return true;
690 }
691 return amsMgrServiceInner_->IsMemorySizeSufficient();
692 }
693
AttachedToStatusBar(const sptr<IRemoteObject> & token)694 void AmsMgrScheduler::AttachedToStatusBar(const sptr<IRemoteObject> &token)
695 {
696 if (!IsReady()) {
697 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
698 return;
699 }
700 auto callerTokenId = IPCSkeleton::GetCallingTokenID();
701 Security::AccessToken::NativeTokenInfo nativeInfo;
702 Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(callerTokenId, nativeInfo);
703 if (nativeInfo.processName != "foundation") {
704 TAG_LOGE(AAFwkTag::APPMGR, "not foundation");
705 return;
706 }
707 std::function<void()> attachedToStatusBarFunc =
708 std::bind(&AppMgrServiceInner::AttachedToStatusBar, amsMgrServiceInner_, token);
709 amsHandler_->SubmitTask(attachedToStatusBarFunc, TASK_ATTACHED_TO_STATUS_BAR);
710 }
711
BlockProcessCacheByPids(const std::vector<int32_t> & pids)712 void AmsMgrScheduler::BlockProcessCacheByPids(const std::vector<int32_t> &pids)
713 {
714 if (!IsReady()) {
715 return;
716 }
717
718 pid_t callingPid = IPCSkeleton::GetCallingPid();
719 pid_t pid = getprocpid();
720 if (callingPid != pid) {
721 TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call");
722 return;
723 }
724
725 std::function<void()> blockProcCacheFunc = [amsMgrServiceInner = amsMgrServiceInner_, pids]() mutable {
726 amsMgrServiceInner->BlockProcessCacheByPids(pids);
727 };
728 amsHandler_->SubmitTask(blockProcCacheFunc, TASK_BLOCK_PROCESS_CACHE_BY_PIDS);
729 }
730
IsKilledForUpgradeWeb(const std::string & bundleName)731 bool AmsMgrScheduler::IsKilledForUpgradeWeb(const std::string &bundleName)
732 {
733 if (!IsReady()) {
734 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
735 return false;
736 }
737 return amsMgrServiceInner_->IsKilledForUpgradeWeb(bundleName);
738 }
739
CleanAbilityByUserRequest(const sptr<IRemoteObject> & token)740 bool AmsMgrScheduler::CleanAbilityByUserRequest(const sptr<IRemoteObject> &token)
741 {
742 if (!IsReady()) {
743 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
744 return false;
745 }
746
747 if (IPCSkeleton::GetCallingPid() != getprocpid()) {
748 TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call");
749 return false;
750 }
751 return amsMgrServiceInner_->CleanAbilityByUserRequest(token);
752 }
IsProcessContainsOnlyUIAbility(const pid_t pid)753 bool AmsMgrScheduler::IsProcessContainsOnlyUIAbility(const pid_t pid)
754 {
755 if (!IsReady()) {
756 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
757 return false;
758 }
759 pid_t callingPid = IPCSkeleton::GetCallingPid();
760 pid_t procPid = getprocpid();
761 if (callingPid != procPid) {
762 TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call");
763 return false;
764 }
765 return amsMgrServiceInner_->IsProcessContainsOnlyUIAbility(pid);
766 }
767
IsProcessAttached(sptr<IRemoteObject> token)768 bool AmsMgrScheduler::IsProcessAttached(sptr<IRemoteObject> token)
769 {
770 if (!IsReady()) {
771 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
772 return false;
773 }
774 return amsMgrServiceInner_->IsProcessAttached(token);
775 }
776
IsCallerKilling(const std::string & callerKey)777 bool AmsMgrScheduler::IsCallerKilling(const std::string& callerKey)
778 {
779 if (!IsReady()) {
780 TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
781 return false;
782 }
783 return amsMgrServiceInner_->IsCallerKilling(callerKey);
784 }
785 } // namespace AppExecFwk
786 } // namespace OHOS
787