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
19 namespace OHOS {
20 namespace Security {
21 namespace SecurityComponent {
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "DelayExitTask"};
24 static const std::string DELAY_EXIT_TASK = "DelayExitTask";
25 static const int32_t DELAY_EXIT_MILLISECONDS = 120 * 1000; // 2m
26 }
27
DelayExitTask()28 DelayExitTask::DelayExitTask()
29 {
30 }
31
GetInstance()32 DelayExitTask& DelayExitTask::GetInstance()
33 {
34 static DelayExitTask instance;
35 return instance;
36 }
37
Init(const std::shared_ptr<SecEventHandler> & secHandler,std::function<void ()> exitTask)38 void DelayExitTask::Init(const std::shared_ptr<SecEventHandler>& secHandler, std::function<void ()> exitTask)
39 {
40 secHandler_ = secHandler;
41 exitTask_ = exitTask;
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 SC_LOG_INFO(LABEL, "Delay exit service after %{public}d ms", DELAY_EXIT_MILLISECONDS);
52 secHandler_->ProxyPostTask(exitTask_, DELAY_EXIT_TASK, DELAY_EXIT_MILLISECONDS);
53 }
54
Stop()55 void DelayExitTask::Stop()
56 {
57 if (secHandler_ == nullptr) {
58 SC_LOG_ERROR(LABEL, "fail to get EventHandler");
59 return;
60 }
61
62 SC_LOG_INFO(LABEL, "service delay exit handler stop");
63 secHandler_->ProxyRemoveTask(DELAY_EXIT_TASK);
64 }
65 } // namespace SecurityComponent
66 } // namespace Security
67 } // namespace OHOS
68