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