1 /*
2  * Copyright (c) 2020 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 "app_terminate_task.h"
17 
18 #include "ability_stack_manager.h"
19 #include "app_manager.h"
20 
21 namespace OHOS {
AppTerminateTask(AbilityMgrContext * context,const char * bundleName)22 AppTerminateTask::AppTerminateTask(AbilityMgrContext *context, const char *bundleName)
23     : AbilityTask(context), bundleName_(bundleName)
24 {
25 }
26 
Execute()27 AbilityMsStatus AppTerminateTask::Execute()
28 {
29     PRINTD("AppTerminateTask", "start");
30     if (abilityMgrContext_ == nullptr || bundleName_ == nullptr) {
31         return AbilityMsStatus::TaskStatus("app terminate", "invalid argument");
32     }
33 
34     // Step1: Stop process
35     AbilityMsStatus status = AppManager::GetInstance().TerminateAppProcess(bundleName_);
36     CHECK_RESULT(status);
37 
38     // Step2: Clear app MissionStack and AbilityRecord
39     AbilityStackManager &stackManager = AbilityStackManager::GetInstance();
40     stackManager.ClearAbilityStack(bundleName_, *abilityMgrContext_);
41 
42     // Step3: Remove App Record
43     AppManager::GetInstance().RemoveAppRecord(bundleName_);
44 
45     // Step4: Active top ability
46     auto topRecord = const_cast<PageAbilityRecord *>(stackManager.GetTopPageAbility(*abilityMgrContext_));
47     if (topRecord != nullptr) {
48         topRecord->ActiveAbility();
49     }
50     return AbilityMsStatus::Ok();
51 }
52 }  // namespace OHOS
53