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 "ability_attach_task.h"
17 
18 #include "app_manager.h"
19 
20 namespace OHOS {
AbilityAttachTask(const AbilityThreadClient * client)21 AbilityAttachTask::AbilityAttachTask(const AbilityThreadClient *client)
22     : client_(client)
23 {
24 }
25 
~AbilityAttachTask()26 AbilityAttachTask::~AbilityAttachTask()
27 {
28     client_ = nullptr;
29 }
30 
Execute()31 AbilityMsStatus AbilityAttachTask::Execute()
32 {
33     PRINTD("AbilityAttachTask", "start");
34     if (client_ == nullptr) {
35         return AbilityMsStatus::TaskStatus("Attach", "invalid argument");
36     }
37     // step1: Get app record by token.
38     auto appRecord = const_cast<AppRecord *>(AppManager::GetInstance().GetAppRecordByToken(
39         client_->GetToken(), client_->GetPid()));
40     if (appRecord == nullptr) {
41         return AbilityMsStatus::TaskStatus("Attach", "appRecord not found");
42     }
43     // step2: Save ability thread client.
44     AbilityMsStatus status = appRecord->SetAbilityThreadClient(*client_);
45     CHECK_RESULT(status);
46 
47     // step3: Load permission
48     status = appRecord->LoadPermission();
49     if (!status.IsOk()) {
50         AppManager::GetInstance().RemoveAppRecord(*appRecord);
51         return status;
52     }
53 
54     // step4: Init app
55     status = appRecord->AppInitTransaction();
56     CHECK_RESULT(status);
57 
58     // step5: Launch pending ability.
59     return appRecord->LaunchPendingAbility();
60 }
61 }  // namespace OHOS
62