• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 
17 #include "unload_handler.h"
18 #include "hc_log.h"
19 #include "event_runner.h"
20 #include "event_handler.h"
21 #include <system_ability_definition.h>
22 #include "iservice_registry.h"
23 
24 static std::shared_ptr<OHOS::AppExecFwk::EventHandler> g_unloadHandler;
25 static const std::string DEVAUTH_UNLOAD_TASK_ID = "devauth_unload_task";
26 static const std::string DEVAUTH_UNLOAD_SA_HANDLER = "devauth_unload_sa_handler";
27 static const int32_t DEVAUTH_LIFE_TIME = 90000; // 90 * 1000
28 static std::mutex g_unloadMutex;
29 
CreateUnloadHandler()30 bool CreateUnloadHandler()
31 {
32     std::lock_guard<std::mutex> autoLock(g_unloadMutex);
33     if (g_unloadHandler != nullptr) {
34         return true;
35     }
36     auto unloadRunner = OHOS::AppExecFwk::EventRunner::Create(DEVAUTH_UNLOAD_SA_HANDLER);
37     if (unloadRunner == nullptr) {
38         LOGE("Create unloadRunner failed.");
39         return false;
40     }
41     g_unloadHandler = std::make_shared<OHOS::AppExecFwk::EventHandler>(unloadRunner);
42     if (g_unloadHandler == nullptr) {
43         LOGE("Create unloadHandler failed.");
44         return false;
45     }
46     return true;
47 }
48 
DestroyUnloadHandler()49 void DestroyUnloadHandler()
50 {
51     std::lock_guard<std::mutex> autoLock(g_unloadMutex);
52     if (g_unloadHandler == nullptr) {
53         LOGE("unloadHandler is nullptr.");
54         return;
55     }
56     g_unloadHandler->RemoveTask(DEVAUTH_UNLOAD_TASK_ID);
57     g_unloadHandler = nullptr;
58 }
59 
DelayUnload()60 void DelayUnload()
61 {
62     if (!CreateUnloadHandler()) {
63         LOGE("UnloadHandler is nullptr.");
64         return;
65     }
66     std::lock_guard<std::mutex> autoLock(g_unloadMutex);
67     auto utask = []() {
68         LOGI("The Service starts unloading.");
69         auto saMgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
70         if (saMgr == nullptr) {
71             LOGE("Get systemabilitymanager instance failed.");
72             return;
73         }
74         int32_t ret = saMgr->UnloadSystemAbility(OHOS::DEVICE_AUTH_SERVICE_ID);
75         if (ret != OHOS::ERR_OK) {
76             LOGE("Unload system ability failed.");
77             return;
78         }
79         LOGI("Service unloaded successfully.");
80     };
81     g_unloadHandler->RemoveTask(DEVAUTH_UNLOAD_TASK_ID);
82     g_unloadHandler->PostTask(utask, DEVAUTH_UNLOAD_TASK_ID, DEVAUTH_LIFE_TIME);
83 }
84