1 /*
2  * Copyright (c) 2023 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 #include "delay_exit_task.h"
16 
17 #include "sec_comp_log.h"
18 #include "sec_comp_manager.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace SecurityComponent {
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "DelayExitTask"};
25 static const std::string DELAY_EXIT_TASK = "DelayExitTask";
26 static const int32_t DELAY_EXIT_MILLISECONDS = 120 * 1000; // 2m
27 }
28 
DelayExitTask()29 DelayExitTask::DelayExitTask()
30 {
31 }
32 
GetInstance()33 DelayExitTask& DelayExitTask::GetInstance()
34 {
35     static DelayExitTask instance;
36     return instance;
37 }
38 
Init(const std::shared_ptr<SecEventHandler> & secHandler)39 void DelayExitTask::Init(const std::shared_ptr<SecEventHandler>& secHandler)
40 {
41     secHandler_ = secHandler;
42 }
43 
Start()44 void DelayExitTask::Start()
45 {
46     if (secHandler_ == nullptr) {
47         SC_LOG_ERROR(LABEL, "fail to get EventHandler");
48         return;
49     }
50 
51     std::function<void()> delayed = ([]() {
52         SecCompManager::GetInstance().ExitSaProcess();
53     });
54 
55     SC_LOG_INFO(LABEL, "Delay exit service after %{public}d ms", DELAY_EXIT_MILLISECONDS);
56     secHandler_->ProxyPostTask(delayed, DELAY_EXIT_TASK, DELAY_EXIT_MILLISECONDS);
57 }
58 
Stop()59 void DelayExitTask::Stop()
60 {
61     if (secHandler_ == nullptr) {
62         SC_LOG_ERROR(LABEL, "fail to get EventHandler");
63         return;
64     }
65 
66     SC_LOG_INFO(LABEL, "service delay exit handler stop");
67     secHandler_->ProxyRemoveTask(DELAY_EXIT_TASK);
68 }
69 }  // namespace SecurityComponent
70 }  // namespace Security
71 }  // namespace OHOS
72