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 static std::mutex g_instanceMutex;
27 }
28
DelayExitTask()29 DelayExitTask::DelayExitTask()
30 {
31 }
32
GetInstance()33 DelayExitTask& DelayExitTask::GetInstance()
34 {
35 static DelayExitTask* instance = nullptr;
36 if (instance == nullptr) {
37 std::lock_guard<std::mutex> lock(g_instanceMutex);
38 if (instance == nullptr) {
39 instance = new DelayExitTask();
40 }
41 }
42 return *instance;
43 }
44
Init(const std::shared_ptr<SecEventHandler> & secHandler,std::function<void ()> exitTask)45 void DelayExitTask::Init(const std::shared_ptr<SecEventHandler>& secHandler, std::function<void ()> exitTask)
46 {
47 secHandler_ = secHandler;
48 exitTask_ = exitTask;
49 }
50
Start()51 void DelayExitTask::Start()
52 {
53 if (secHandler_ == nullptr) {
54 SC_LOG_ERROR(LABEL, "fail to get EventHandler");
55 return;
56 }
57
58 SC_LOG_INFO(LABEL, "Delay exit service after %{public}d ms", DELAY_EXIT_MILLISECONDS);
59 secHandler_->ProxyPostTask(exitTask_, DELAY_EXIT_TASK, DELAY_EXIT_MILLISECONDS);
60 }
61
Stop()62 void DelayExitTask::Stop()
63 {
64 if (secHandler_ == nullptr) {
65 SC_LOG_ERROR(LABEL, "fail to get EventHandler");
66 return;
67 }
68
69 SC_LOG_INFO(LABEL, "service delay exit handler stop");
70 secHandler_->ProxyRemoveTask(DELAY_EXIT_TASK);
71 }
72 } // namespace SecurityComponent
73 } // namespace Security
74 } // namespace OHOS
75