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 "ability_manager_errors.h"
20 #include "datetime_ex.h"
21 #include "ipc_skeleton.h"
22 #include "system_ability_definition.h"
23
24 #include "accesstoken_kit.h"
25 #include "app_death_recipient.h"
26 #include "app_mgr_constants.h"
27 #include "app_utils.h"
28 #include "hilog_tag_wrapper.h"
29 #include "perf_profile.h"
30 #include "permission_constants.h"
31 #include "permission_verification.h"
32
33 namespace OHOS {
34 namespace AppExecFwk {
35 namespace {
36 constexpr const char* TASK_TERMINATE_ABILITY = "TerminateAbilityTask";
37 constexpr const char* TASK_UPDATE_ABILITY_STATE = "UpdateAbilityStateTask";
38 constexpr const char* TASK_UPDATE_EXTENSION_STATE = "UpdateExtensionStateTask";
39 constexpr const char* TASK_REGISTER_APP_STATE_CALLBACK = "RegisterAppStateCallbackTask";
40 constexpr const char* TASK_STOP_ALL_PROCESS = "StopAllProcessTask";
41 constexpr const char* TASK_ABILITY_BEHAVIOR_ANALYSIS = "AbilityBehaviorAnalysisTask";
42 constexpr const char* TASK_KILL_PROCESS_BY_ABILITY_TOKEN = "KillProcessByAbilityTokenTask";
43 constexpr const char* TASK_KILL_PROCESSES_BY_USERID = "KillProcessesByUserIdTask";
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,bool subProcess,bool isKillPrecedeStart)245 int32_t AmsMgrScheduler::KillProcessesByPids(const std::vector<int32_t> &pids, const std::string &reason,
246 bool subProcess, bool isKillPrecedeStart)
247 {
248 if (!IsReady()) {
249 return ERR_INVALID_OPERATION;
250 }
251
252 pid_t callingPid = IPCSkeleton::GetCallingPid();
253 pid_t pid = getprocpid();
254 if (callingPid != pid) {
255 TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call");
256 return ERR_PERMISSION_DENIED;
257 }
258
259 return amsMgrServiceInner_->KillProcessesByPids(pids, reason, subProcess, isKillPrecedeStart);
260 }
261
AttachPidToParent(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & callerToken)262 void AmsMgrScheduler::AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken)
263 {
264 if (!IsReady()) {
265 return;
266 }
267
268 pid_t callingPid = IPCSkeleton::GetCallingPid();
269 pid_t pid = getprocpid();
270 if (callingPid != pid) {
271 TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call");
272 return;
273 }
274
275 std::function<void()> attachPidToParentFunc = [amsMgrServiceInner = amsMgrServiceInner_, token, callerToken]() {
276 amsMgrServiceInner->AttachPidToParent(token, callerToken);
277 };
278 amsHandler_->SubmitTask(attachPidToParentFunc, TASK_ATTACH_PID_TO_PARENT);
279 }
280
KillProcessWithAccount(const std::string & bundleName,const int accountId,const bool clearPageStack,int32_t appIndex)281 int32_t AmsMgrScheduler::KillProcessWithAccount(
282 const std::string &bundleName, const int accountId, const bool clearPageStack, int32_t appIndex)
283 {
284 TAG_LOGI(AAFwkTag::APPMGR, "bundle=%{public}s, appIndex=%{public}d, userId=%{public}d, clearPageStack=%{public}d",
285 bundleName.c_str(), appIndex, accountId, clearPageStack);
286 if (!IsReady()) {
287 return ERR_INVALID_OPERATION;
288 }
289 return amsMgrServiceInner_->KillApplicationByUserId(bundleName, appIndex, accountId, clearPageStack,
290 "KillProcessWithAccount");
291 }
292
KillProcessesInBatch(const std::vector<int32_t> & pids)293 int32_t AmsMgrScheduler::KillProcessesInBatch(const std::vector<int32_t> &pids)
294 {
295 TAG_LOGI(AAFwkTag::APPMGR, "pids.size=%{public}zu", pids.size());
296 if (!IsReady()) {
297 return ERR_INVALID_OPERATION;
298 }
299 return amsMgrServiceInner_->KillProcessesInBatch(pids);
300 }
301
AbilityAttachTimeOut(const sptr<IRemoteObject> & token)302 void AmsMgrScheduler::AbilityAttachTimeOut(const sptr<IRemoteObject> &token)
303 {
304 TAG_LOGI(AAFwkTag::APPMGR, "call");
305 if (!IsReady()) {
306 return;
307 }
308
309 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
310 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
311 return;
312 }
313 auto task = [amsMgrServiceInner = amsMgrServiceInner_, token]() {
314 amsMgrServiceInner->HandleAbilityAttachTimeOut(token);
315 };
316 amsHandler_->SubmitTask(task, "AbilityAttachTimeOut");
317 }
318
PrepareTerminate(const sptr<IRemoteObject> & token,bool clearMissionFlag)319 void AmsMgrScheduler::PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag)
320 {
321 TAG_LOGD(AAFwkTag::APPMGR, "Notify AppMgrService to prepare to terminate the ability.");
322 if (!IsReady()) {
323 return;
324 }
325
326 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
327 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
328 return;
329 }
330 auto task = [=]() { amsMgrServiceInner_->PrepareTerminate(token, clearMissionFlag); };
331 amsHandler_->SubmitTask(task, {
332 .taskName_ = "PrepareTerminate",
333 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
334 });
335 }
336
UpdateApplicationInfoInstalled(const std::string & bundleName,const int uid,const std::string & moduleName,bool isPlugin)337 int32_t AmsMgrScheduler::UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid,
338 const std::string &moduleName, bool isPlugin)
339 {
340 if (!IsReady()) {
341 return ERR_INVALID_OPERATION;
342 }
343
344 return amsMgrServiceInner_->UpdateApplicationInfoInstalled(bundleName, uid, moduleName, isPlugin);
345 }
346
KillApplication(const std::string & bundleName,bool clearPageStack,int32_t appIndex)347 int32_t AmsMgrScheduler::KillApplication(const std::string &bundleName, bool clearPageStack, int32_t appIndex)
348 {
349 TAG_LOGI(AAFwkTag::APPMGR, "bundleName = %{public}s, clearPageStack = %{public}d",
350 bundleName.c_str(), clearPageStack);
351 if (!IsReady()) {
352 return ERR_INVALID_OPERATION;
353 }
354
355 return amsMgrServiceInner_->KillApplication(bundleName, clearPageStack, appIndex);
356 }
357
ForceKillApplication(const std::string & bundleName,const int userId,const int appIndex)358 int32_t AmsMgrScheduler::ForceKillApplication(const std::string &bundleName,
359 const int userId, const int appIndex)
360 {
361 TAG_LOGI(AAFwkTag::APPMGR, "bundleName=%{public}s,userId=%{public}d,apIndex=%{public}d",
362 bundleName.c_str(), userId, appIndex);
363 if (!IsReady()) {
364 return ERR_INVALID_OPERATION;
365 }
366
367 return amsMgrServiceInner_->ForceKillApplication(bundleName, userId, appIndex);
368 }
369
KillProcessesByAccessTokenId(const uint32_t accessTokenId)370 int32_t AmsMgrScheduler::KillProcessesByAccessTokenId(const uint32_t accessTokenId)
371 {
372 TAG_LOGI(AAFwkTag::APPMGR, "accessTokenId=%{public}d", accessTokenId);
373 if (!IsReady()) {
374 return ERR_INVALID_OPERATION;
375 }
376
377 return amsMgrServiceInner_->KillProcessesByAccessTokenId(accessTokenId);
378 }
379
KillApplicationByUid(const std::string & bundleName,const int uid,const std::string & reason)380 int32_t AmsMgrScheduler::KillApplicationByUid(const std::string &bundleName, const int uid,
381 const std::string& reason)
382 {
383 TAG_LOGI(AAFwkTag::APPMGR, "bundleName = %{public}s, uid = %{public}d", bundleName.c_str(), uid);
384 if (!IsReady()) {
385 return ERR_INVALID_OPERATION;
386 }
387 return amsMgrServiceInner_->KillApplicationByUid(bundleName, uid, reason);
388 }
389
NotifyUninstallOrUpgradeApp(const std::string & bundleName,int32_t uid,bool isUpgrade)390 int32_t AmsMgrScheduler::NotifyUninstallOrUpgradeApp(const std::string &bundleName, int32_t uid, bool isUpgrade)
391 {
392 TAG_LOGI(AAFwkTag::APPMGR, "bundleName = %{public}s, uid = %{public}d", bundleName.c_str(), uid);
393 if (!IsReady()) {
394 return ERR_INVALID_OPERATION;
395 }
396
397 pid_t callingPid = IPCSkeleton::GetCallingPid();
398 pid_t pid = getprocpid();
399 if (callingPid != pid) {
400 TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call");
401 return ERR_PERMISSION_DENIED;
402 }
403
404 return amsMgrServiceInner_->NotifyUninstallOrUpgradeApp(bundleName, uid, isUpgrade);
405 }
406
NotifyUninstallOrUpgradeAppEnd(int32_t uid)407 void AmsMgrScheduler::NotifyUninstallOrUpgradeAppEnd(int32_t uid)
408 {
409 TAG_LOGI(AAFwkTag::APPMGR, "uid = %{public}d", uid);
410 if (!IsReady()) {
411 return;
412 }
413
414 pid_t callingPid = IPCSkeleton::GetCallingPid();
415 pid_t pid = getprocpid();
416 if (callingPid != pid) {
417 TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call");
418 return;
419 }
420
421 amsMgrServiceInner_->NotifyUninstallOrUpgradeAppEnd(uid);
422 }
423
KillApplicationSelf(const bool clearPageStack,const std::string & reason)424 int32_t AmsMgrScheduler::KillApplicationSelf(const bool clearPageStack, const std::string& reason)
425 {
426 if (!IsReady()) {
427 return ERR_INVALID_OPERATION;
428 }
429 return amsMgrServiceInner_->KillApplicationSelf(clearPageStack, reason);
430 }
431
IsReady() const432 bool AmsMgrScheduler::IsReady() const
433 {
434 if (!amsMgrServiceInner_) {
435 TAG_LOGE(AAFwkTag::APPMGR, "null amsMgrServiceInner_");
436 return false;
437 }
438 if (!amsHandler_) {
439 TAG_LOGE(AAFwkTag::APPMGR, "null amsHandler_");
440 return false;
441 }
442 return true;
443 }
444
GetRunningProcessInfoByToken(const sptr<IRemoteObject> & token,AppExecFwk::RunningProcessInfo & info)445 void AmsMgrScheduler::GetRunningProcessInfoByToken(
446 const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info)
447 {
448 if (!IsReady()) {
449 return;
450 }
451
452 amsMgrServiceInner_->GetRunningProcessInfoByToken(token, info);
453 }
454
SetAbilityForegroundingFlagToAppRecord(const pid_t pid)455 void AmsMgrScheduler::SetAbilityForegroundingFlagToAppRecord(const pid_t pid)
456 {
457 if (!IsReady()) {
458 return;
459 }
460 amsMgrServiceInner_->SetAbilityForegroundingFlagToAppRecord(pid);
461 }
462
StartSpecifiedAbility(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId)463 void AmsMgrScheduler::StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
464 int32_t requestId)
465 {
466 if (!IsReady()) {
467 return;
468 }
469
470 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
471 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
472 return;
473 }
474 auto task = [=]() { amsMgrServiceInner_->StartSpecifiedAbility(want, abilityInfo, requestId); };
475 amsHandler_->SubmitTask(task, {
476 .taskName_ = "StartSpecifiedAbility",
477 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
478 });
479 }
480
PrepareTerminateApp(const pid_t pid,const std::string & moduleName)481 void AmsMgrScheduler::PrepareTerminateApp(const pid_t pid, const std::string &moduleName)
482 {
483 TAG_LOGD(AAFwkTag::APPKIT, "called");
484 if (!IsReady()) {
485 TAG_LOGW(AAFwkTag::APPMGR, "not ready");
486 return;
487 }
488 amsMgrServiceInner_->SchedulePrepareTerminate(pid, moduleName);
489 }
490
StartSpecifiedProcess(const AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,int32_t requestId,const std::string & customProcess)491 void AmsMgrScheduler::StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
492 int32_t requestId, const std::string &customProcess)
493 {
494 if (!IsReady()) {
495 TAG_LOGW(AAFwkTag::APPMGR, "not ready");
496 return;
497 }
498
499 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
500 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
501 return;
502 }
503 auto task = [=]() { amsMgrServiceInner_->StartSpecifiedProcess(want, abilityInfo, requestId, customProcess); };
504 amsHandler_->SubmitTask(task, {
505 .taskName_ = "StartSpecifiedProcess",
506 .taskQos_ = AAFwk::TaskQoS::USER_INTERACTIVE
507 });
508 }
509
RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> & response)510 void AmsMgrScheduler::RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response)
511 {
512 if (!AAFwk::PermissionVerification::GetInstance()->IsSACall()) {
513 TAG_LOGE(AAFwkTag::APPMGR, "caller is not SA");
514 return;
515 }
516 if (!IsReady()) {
517 return;
518 }
519 auto task = [=]() { amsMgrServiceInner_->RegisterStartSpecifiedAbilityResponse(response); };
520 amsHandler_->SubmitTask(task, "RegisterStartSpecifiedAbilityResponse");
521 }
522
GetApplicationInfoByProcessID(const int pid,AppExecFwk::ApplicationInfo & application,bool & debug)523 int AmsMgrScheduler::GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug)
524 {
525 if (!IsReady()) {
526 return ERR_INVALID_OPERATION;
527 }
528 return amsMgrServiceInner_->GetApplicationInfoByProcessID(pid, application, debug);
529 }
530
NotifyAppMgrRecordExitReason(int32_t pid,int32_t reason,const std::string & exitMsg)531 int32_t AmsMgrScheduler::NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg)
532 {
533 if (!IsReady()) {
534 return ERR_INVALID_OPERATION;
535 }
536 return amsMgrServiceInner_->NotifyAppMgrRecordExitReason(pid, reason, exitMsg);
537 }
538
SetCurrentUserId(const int32_t userId)539 void AmsMgrScheduler::SetCurrentUserId(const int32_t userId)
540 {
541 if (!IsReady()) {
542 return;
543 }
544 amsMgrServiceInner_->SetCurrentUserId(userId);
545 }
546
SetEnableStartProcessFlagByUserId(int32_t userId,bool enableStartProcess)547 void AmsMgrScheduler::SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess)
548 {
549 if (!IsReady()) {
550 return;
551 }
552 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
553 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
554 return;
555 }
556 amsMgrServiceInner_->SetEnableStartProcessFlagByUserId(userId, enableStartProcess);
557 }
558
GetBundleNameByPid(const int pid,std::string & bundleName,int32_t & uid)559 int32_t AmsMgrScheduler::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid)
560 {
561 if (!IsReady()) {
562 return ERR_INVALID_OPERATION;
563 }
564 return amsMgrServiceInner_->GetBundleNameByPid(pid, bundleName, uid);
565 }
566
RegisterAppDebugListener(const sptr<IAppDebugListener> & listener)567 int32_t AmsMgrScheduler::RegisterAppDebugListener(const sptr<IAppDebugListener> &listener)
568 {
569 if (!IsReady()) {
570 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
571 return ERR_INVALID_OPERATION;
572 }
573 return amsMgrServiceInner_->RegisterAppDebugListener(listener);
574 }
575
UnregisterAppDebugListener(const sptr<IAppDebugListener> & listener)576 int32_t AmsMgrScheduler::UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener)
577 {
578 if (!IsReady()) {
579 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
580 return ERR_INVALID_OPERATION;
581 }
582 return amsMgrServiceInner_->UnregisterAppDebugListener(listener);
583 }
584
AttachAppDebug(const std::string & bundleName,bool isDebugFromLocal)585 int32_t AmsMgrScheduler::AttachAppDebug(const std::string &bundleName, bool isDebugFromLocal)
586 {
587 if (!IsReady()) {
588 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
589 return ERR_INVALID_OPERATION;
590 }
591 if (!AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_NAME)) {
592 TAG_LOGE(AAFwkTag::APPMGR, "caller is not foundation");
593 return ERR_INVALID_OPERATION;
594 }
595 return amsMgrServiceInner_->AttachAppDebug(bundleName, isDebugFromLocal);
596 }
597
DetachAppDebug(const std::string & bundleName)598 int32_t AmsMgrScheduler::DetachAppDebug(const std::string &bundleName)
599 {
600 if (!IsReady()) {
601 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
602 return ERR_INVALID_OPERATION;
603 }
604 if (!AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(FOUNDATION_NAME)) {
605 TAG_LOGE(AAFwkTag::APPMGR, "caller is not foundation");
606 return ERR_INVALID_OPERATION;
607 }
608 return amsMgrServiceInner_->DetachAppDebug(bundleName);
609 }
610
SetAppWaitingDebug(const std::string & bundleName,bool isPersist)611 int32_t AmsMgrScheduler::SetAppWaitingDebug(const std::string &bundleName, bool isPersist)
612 {
613 if (!IsReady()) {
614 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
615 return ERR_INVALID_OPERATION;
616 }
617 return amsMgrServiceInner_->SetAppWaitingDebug(bundleName, isPersist);
618 }
619
CancelAppWaitingDebug()620 int32_t AmsMgrScheduler::CancelAppWaitingDebug()
621 {
622 if (!IsReady()) {
623 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
624 return ERR_INVALID_OPERATION;
625 }
626 return amsMgrServiceInner_->CancelAppWaitingDebug();
627 }
628
GetWaitingDebugApp(std::vector<std::string> & debugInfoList)629 int32_t AmsMgrScheduler::GetWaitingDebugApp(std::vector<std::string> &debugInfoList)
630 {
631 if (!IsReady()) {
632 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
633 return ERR_INVALID_OPERATION;
634 }
635 return amsMgrServiceInner_->GetWaitingDebugApp(debugInfoList);
636 }
637
IsWaitingDebugApp(const std::string & bundleName)638 bool AmsMgrScheduler::IsWaitingDebugApp(const std::string &bundleName)
639 {
640 if (!IsReady()) {
641 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
642 return false;
643 }
644 return amsMgrServiceInner_->IsWaitingDebugApp(bundleName);
645 }
646
ClearNonPersistWaitingDebugFlag()647 void AmsMgrScheduler::ClearNonPersistWaitingDebugFlag()
648 {
649 if (!IsReady()) {
650 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
651 return;
652 }
653 amsMgrServiceInner_->ClearNonPersistWaitingDebugFlag();
654 }
655
RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> & response)656 int32_t AmsMgrScheduler::RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response)
657 {
658 if (!IsReady()) {
659 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
660 return ERR_INVALID_OPERATION;
661 }
662 return amsMgrServiceInner_->RegisterAbilityDebugResponse(response);
663 }
664
IsAttachDebug(const std::string & bundleName)665 bool AmsMgrScheduler::IsAttachDebug(const std::string &bundleName)
666 {
667 if (!IsReady()) {
668 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
669 return false;
670 }
671 return amsMgrServiceInner_->IsAttachDebug(bundleName);
672 }
673
SetKeepAliveEnableState(const std::string & bundleName,bool enable,int32_t uid)674 void AmsMgrScheduler::SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid)
675 {
676 if (!IsReady()) {
677 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
678 return;
679 }
680 amsMgrServiceInner_->SetKeepAliveEnableState(bundleName, enable, uid);
681 }
682
SetKeepAliveDkv(const std::string & bundleName,bool enable,int32_t uid)683 void AmsMgrScheduler::SetKeepAliveDkv(const std::string &bundleName, bool enable, int32_t uid)
684 {
685 if (!IsReady()) {
686 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
687 return;
688 }
689 amsMgrServiceInner_->SetKeepAliveDkv(bundleName, enable, uid);
690 }
691
SetKeepAliveAppService(const std::string & bundleName,bool enable,int32_t uid)692 void AmsMgrScheduler::SetKeepAliveAppService(const std::string &bundleName, bool enable, int32_t uid)
693 {
694 if (!IsReady()) {
695 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
696 return;
697 }
698 amsMgrServiceInner_->SetKeepAliveAppService(bundleName, enable, uid);
699 }
700
ClearProcessByToken(sptr<IRemoteObject> token)701 void AmsMgrScheduler::ClearProcessByToken(sptr<IRemoteObject> token)
702 {
703 if (!IsReady()) {
704 return;
705 }
706
707 auto callerTokenId = IPCSkeleton::GetCallingTokenID();
708 Security::AccessToken::NativeTokenInfo nativeInfo;
709 Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(callerTokenId, nativeInfo);
710 if (nativeInfo.processName != "foundation") {
711 TAG_LOGE(AAFwkTag::APPMGR, "not foundation");
712 return;
713 }
714
715 std::function<void()> clearProcessByTokenFunc = [amsMgrServiceInner = amsMgrServiceInner_, token]() {
716 amsMgrServiceInner->ClearProcessByToken(token);
717 };
718 amsHandler_->SubmitTask(clearProcessByTokenFunc, TASK_CLEAR_PROCESS_BY_ABILITY_TOKEN);
719 }
720
IsMemorySizeSufficent()721 bool AmsMgrScheduler::IsMemorySizeSufficent()
722 {
723 if (!IsReady()) {
724 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
725 return true;
726 }
727 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
728 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
729 return true;
730 }
731 return amsMgrServiceInner_->IsMemorySizeSufficient();
732 }
733
IsNoRequireBigMemory()734 bool AmsMgrScheduler::IsNoRequireBigMemory()
735 {
736 if (!IsReady()) {
737 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
738 return true;
739 }
740 if (amsMgrServiceInner_->VerifyRequestPermission() != ERR_OK) {
741 TAG_LOGE(AAFwkTag::APPMGR, "verification failed");
742 return true;
743 }
744 return amsMgrServiceInner_->IsNoRequireBigMemory();
745 }
746
AttachedToStatusBar(const sptr<IRemoteObject> & token)747 void AmsMgrScheduler::AttachedToStatusBar(const sptr<IRemoteObject> &token)
748 {
749 if (!IsReady()) {
750 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
751 return;
752 }
753 auto callerTokenId = IPCSkeleton::GetCallingTokenID();
754 Security::AccessToken::NativeTokenInfo nativeInfo;
755 Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(callerTokenId, nativeInfo);
756 if (nativeInfo.processName != "foundation") {
757 TAG_LOGE(AAFwkTag::APPMGR, "not foundation");
758 return;
759 }
760 std::function<void()> attachedToStatusBarFunc =
761 std::bind(&AppMgrServiceInner::AttachedToStatusBar, amsMgrServiceInner_, token);
762 amsHandler_->SubmitTask(attachedToStatusBarFunc, TASK_ATTACHED_TO_STATUS_BAR);
763 }
764
BlockProcessCacheByPids(const std::vector<int32_t> & pids)765 void AmsMgrScheduler::BlockProcessCacheByPids(const std::vector<int32_t> &pids)
766 {
767 if (!IsReady()) {
768 return;
769 }
770
771 pid_t callingPid = IPCSkeleton::GetCallingPid();
772 pid_t pid = getprocpid();
773 if (callingPid != pid) {
774 TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call");
775 return;
776 }
777
778 std::function<void()> blockProcCacheFunc = [amsMgrServiceInner = amsMgrServiceInner_, pids]() mutable {
779 amsMgrServiceInner->BlockProcessCacheByPids(pids);
780 };
781 amsHandler_->SubmitTask(blockProcCacheFunc, TASK_BLOCK_PROCESS_CACHE_BY_PIDS);
782 }
783
IsKilledForUpgradeWeb(const std::string & bundleName)784 bool AmsMgrScheduler::IsKilledForUpgradeWeb(const std::string &bundleName)
785 {
786 if (!IsReady()) {
787 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
788 return false;
789 }
790 return amsMgrServiceInner_->IsKilledForUpgradeWeb(bundleName);
791 }
792
CleanAbilityByUserRequest(const sptr<IRemoteObject> & token)793 bool AmsMgrScheduler::CleanAbilityByUserRequest(const sptr<IRemoteObject> &token)
794 {
795 if (!IsReady()) {
796 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
797 return false;
798 }
799
800 if (IPCSkeleton::GetCallingPid() != getprocpid()) {
801 TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call");
802 return false;
803 }
804 return amsMgrServiceInner_->CleanAbilityByUserRequest(token);
805 }
IsProcessContainsOnlyUIAbility(const pid_t pid)806 bool AmsMgrScheduler::IsProcessContainsOnlyUIAbility(const pid_t pid)
807 {
808 if (!IsReady()) {
809 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
810 return false;
811 }
812 pid_t callingPid = IPCSkeleton::GetCallingPid();
813 pid_t procPid = getprocpid();
814 if (callingPid != procPid) {
815 TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call");
816 return false;
817 }
818 return amsMgrServiceInner_->IsProcessContainsOnlyUIAbility(pid);
819 }
820
IsProcessAttached(sptr<IRemoteObject> token)821 bool AmsMgrScheduler::IsProcessAttached(sptr<IRemoteObject> token)
822 {
823 if (!IsReady()) {
824 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
825 return false;
826 }
827 return amsMgrServiceInner_->IsProcessAttached(token);
828 }
829
IsCallerKilling(const std::string & callerKey)830 bool AmsMgrScheduler::IsCallerKilling(const std::string& callerKey)
831 {
832 if (!IsReady()) {
833 TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready.");
834 return false;
835 }
836 return amsMgrServiceInner_->IsCallerKilling(callerKey);
837 }
838
PreloadApplicationByPhase(const std::string & bundleName,int32_t userId,int32_t appIndex,AppExecFwk::PreloadPhase preloadPhase)839 int32_t AmsMgrScheduler::PreloadApplicationByPhase(const std::string &bundleName, int32_t userId, int32_t appIndex,
840 AppExecFwk::PreloadPhase preloadPhase)
841 {
842 if (!IsReady()) {
843 TAG_LOGE(AAFwkTag::APPMGR, "appMgrServiceInner is not ready.");
844 return AAFwk::ERR_APP_MGR_SERVICE_NOT_READY;
845 }
846 if (!amsMgrServiceInner_->IsFoundationCall()) {
847 TAG_LOGE(AAFwkTag::APPMGR, "Not foundation call.");
848 return ERR_PERMISSION_DENIED;
849 }
850 return amsMgrServiceInner_->PreloadApplicationByPhase(bundleName, userId, appIndex, preloadPhase);
851 }
852
NotifyPreloadAbilityStateChanged(sptr<IRemoteObject> token,bool isPreForeground)853 int32_t AmsMgrScheduler::NotifyPreloadAbilityStateChanged(sptr<IRemoteObject> token, bool isPreForeground)
854 {
855 if (!IsReady()) {
856 TAG_LOGE(AAFwkTag::APPMGR, "appMgrServiceInner is not ready.");
857 return AAFwk::ERR_APP_MGR_SERVICE_NOT_READY;
858 }
859 if (!amsMgrServiceInner_->IsFoundationCall()) {
860 TAG_LOGE(AAFwkTag::APPMGR, "Not foundation call.");
861 return ERR_PERMISSION_DENIED;
862 }
863 return amsMgrServiceInner_->NotifyPreloadAbilityStateChanged(token, isPreForeground);
864 }
865
CheckPreloadAppRecordExist(const std::string & bundleName,int32_t userId,int32_t appIndex,bool & isExist)866 int32_t AmsMgrScheduler::CheckPreloadAppRecordExist(const std::string &bundleName, int32_t userId, int32_t appIndex,
867 bool &isExist)
868 {
869 if (!IsReady()) {
870 TAG_LOGE(AAFwkTag::APPMGR, "appMgrServiceInner is not ready.");
871 return AAFwk::ERR_APP_MGR_SERVICE_NOT_READY;
872 }
873 if (!amsMgrServiceInner_->IsFoundationCall()) {
874 TAG_LOGE(AAFwkTag::APPMGR, "Not foundation call.");
875 return ERR_PERMISSION_DENIED;
876 }
877 isExist = amsMgrServiceInner_->CheckPreloadAppRecordExist(bundleName, userId, appIndex);
878 return ERR_OK;
879 }
880
VerifyKillProcessPermission(const std::string & bundleName)881 int32_t AmsMgrScheduler::VerifyKillProcessPermission(const std::string &bundleName)
882 {
883 if (!IsReady()) {
884 TAG_LOGE(AAFwkTag::APPMGR, "not ready");
885 return AAFwk::ERR_APP_MGR_SERVICE_NOT_READY;
886 }
887 return static_cast<int32_t>(amsMgrServiceInner_->VerifyKillProcessPermission(bundleName));
888 }
889 } // namespace AppExecFwk
890 } // namespace OHOS
891