• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "account_lifecycle_plugin_proxy.h"
17 #include "device_auth_defines.h"
18 #include "hc_log.h"
19 #include "hc_types.h"
20 #include "task_manager.h"
21 #include "account_task_manager.h"
22 #include "alg_loader.h"
23 #include "identity_service.h"
24 #include "critical_handler.h"
25 
26 static AccountLifecyleExtPlug *g_accountLifeCyclePlugin = NULL;
27 static AccountLifecyleExtPlugCtx *g_accountPluginCtx = NULL;
28 
29 typedef struct {
30     HcTaskBase base;
31     ExtWorkerTask *extTask;
32 } WorkerTask;
33 
DoWorkerTask(HcTaskBase * task)34 static void DoWorkerTask(HcTaskBase *task)
35 {
36     LOGD("[ACCOUNT_LIFE_PLUGIN]: Do worker task begin.");
37     if (task == NULL) {
38         LOGE("[ACCOUNT_LIFE_PLUGIN]: The input task is NULL, cannot do task!");
39         DecreaseCriticalCnt();
40         return;
41     }
42     WorkerTask *workerTask = (WorkerTask *)task;
43     if (workerTask->extTask == NULL) {
44         LOGE("[ACCOUNT_LIFE_PLUGIN]: The inner task is NULL, cannot do task!");
45         DecreaseCriticalCnt();
46         return;
47     }
48     if (workerTask->extTask->execute == NULL) {
49         LOGE("[ACCOUNT_LIFE_PLUGIN]: The ext func is NULL, cannot do task!");
50         DecreaseCriticalCnt();
51         return;
52     }
53     workerTask->extTask->execute(workerTask->extTask);
54     DecreaseCriticalCnt();
55     LOGD("[ACCOUNT_LIFE_PLUGIN]: Do worker task end.");
56 }
57 
DestroyExtWorkerTask(ExtWorkerTask * task)58 static void DestroyExtWorkerTask(ExtWorkerTask *task)
59 {
60     if (task == NULL || task->destroy == NULL) {
61         LOGI("[ACCOUNT_LIFE_PLUGIN]: The destroy func is NULL, cannot destroy task!");
62         return;
63     }
64     task->destroy(task);
65 }
66 
DestroyWorkerTask(HcTaskBase * workerTask)67 static void DestroyWorkerTask(HcTaskBase *workerTask)
68 {
69     LOGD("[ACCOUNT_LIFE_PLUGIN]: Destroy worker task begin.");
70     if (workerTask == NULL) {
71         LOGE("[ACCOUNT_LIFE_PLUGIN]: The inner task is NULL, cannot do task!");
72         DecreaseCriticalCnt();
73         return;
74     }
75     DestroyExtWorkerTask(((WorkerTask *)workerTask)->extTask);
76     DecreaseLoadCount();
77     DecreaseCriticalCnt();
78     LOGD("[ACCOUNT_LIFE_PLUGIN]: Destroy worker task end.");
79 }
80 
ExecuteWorkerTask(struct ExtWorkerTask * extTask)81 static int32_t ExecuteWorkerTask(struct ExtWorkerTask *extTask)
82 {
83     if (extTask == NULL) {
84         LOGE("[ACCOUNT_LIFE_PLUGIN]: The input task is NULL.");
85         return HC_ERR_INVALID_PARAMS;
86     }
87     WorkerTask *baseTask = (WorkerTask *)HcMalloc(sizeof(WorkerTask), 0);
88     if (baseTask == NULL) {
89         LOGE("[ACCOUNT_LIFE_PLUGIN]: Failed to allocate task memory!");
90         DestroyExtWorkerTask(extTask);
91         return HC_ERR_ALLOC_MEMORY;
92     }
93     baseTask->extTask = extTask;
94     baseTask->base.doAction = DoWorkerTask;
95     baseTask->base.destroy = DestroyWorkerTask;
96     IncreaseLoadCount();
97     if (PushTask((HcTaskBase *)baseTask) != HC_SUCCESS) {
98         LOGE("[ACCOUNT_LIFE_PLUGIN]: Push worker task fail.");
99         DecreaseLoadCount();
100         DestroyExtWorkerTask(extTask);
101         HcFree(baseTask);
102         return HC_ERR_INIT_TASK_FAIL;
103     }
104     IncreaseCriticalCnt(ADD_TWO);
105     return HC_SUCCESS;
106 }
107 
108 
InitAccountLifecyclePluginCtx(void)109 static int32_t InitAccountLifecyclePluginCtx(void)
110 {
111     g_accountPluginCtx = (AccountLifecyleExtPlugCtx *)HcMalloc(sizeof(AccountLifecyleExtPlugCtx), 0);
112     if (g_accountPluginCtx == NULL) {
113         LOGE("[ACCOUNT_LIFE_PLUGIN]: Malloc memory failed.");
114         return HC_ERROR;
115     }
116     const DeviceGroupManager *gmInstace = GetGmInstance();
117     if (gmInstace == NULL) {
118         LOGE("[ACCOUNT_LIFE_PLUGIN]: Gm instance is null.");
119         HcFree(g_accountPluginCtx);
120         g_accountPluginCtx = NULL;
121         return HC_ERR_INVALID_PARAMS;
122     }
123 #ifdef DEV_AUTH_IS_ENABLE
124     const CredManager *cmInstace = GetCredMgrInstance();
125     if (cmInstace == NULL) {
126         LOGE("[ACCOUNT_LIFE_PLUGIN]: Cm instance is null.");
127         HcFree(g_accountPluginCtx);
128         g_accountPluginCtx = NULL;
129         return HC_ERR_INVALID_PARAMS;
130     }
131     g_accountPluginCtx->addCredential = cmInstace->addCredential;
132     g_accountPluginCtx->exportCredential = cmInstace->exportCredential;
133     g_accountPluginCtx->deleteCredential = cmInstace->deleteCredential;
134     g_accountPluginCtx->updateCredInfo = cmInstace->updateCredInfo;
135     g_accountPluginCtx->queryCredInfoByCredId = QueryCredInfoByCredIdAndUid;
136     g_accountPluginCtx->queryCredentialByParams = cmInstace->queryCredentialByParams;
137     g_accountPluginCtx->destroyInfo = cmInstace->destroyInfo;
138 #endif
139     g_accountPluginCtx->createGroup = gmInstace->createGroup;
140     g_accountPluginCtx->deleteGroup = gmInstace->deleteGroup;
141     g_accountPluginCtx->getGroupInfo = gmInstace->getGroupInfo;
142     g_accountPluginCtx->getRegisterInfo = gmInstace->getRegisterInfo;
143     g_accountPluginCtx->regCallback = gmInstace->regCallback;
144     g_accountPluginCtx->unRegCallback = gmInstace->unRegCallback;
145     g_accountPluginCtx->executeWorkerTask = ExecuteWorkerTask;
146     g_accountPluginCtx->notifyAsyncTaskStart = IncreaseLoadCount;
147     g_accountPluginCtx->notifyAsyncTaskStop = DecreaseLoadCount;
148     return HC_SUCCESS;
149 }
150 
SetAccountLifecyclePlugin(const CJson * inputParams,AccountLifecyleExtPlug * accountLifeCyclePlugin)151 int32_t SetAccountLifecyclePlugin(const CJson *inputParams, AccountLifecyleExtPlug *accountLifeCyclePlugin)
152 {
153     g_accountLifeCyclePlugin = accountLifeCyclePlugin;
154     if (g_accountLifeCyclePlugin == NULL || g_accountLifeCyclePlugin->base.init == NULL) {
155         LOGE("[ACCOUNT_LIFE_PLUGIN]: Input params are invalid.");
156         return HC_ERR_INVALID_PARAMS;
157     }
158     int32_t res = InitAccountLifecyclePluginCtx();
159     if (res != HC_SUCCESS) {
160         LOGE("[ACCOUNT_LIFE_PLUGIN]: Get account life ctx failed.");
161         return HC_ERROR;
162     }
163     return g_accountLifeCyclePlugin->base.init(&g_accountLifeCyclePlugin->base,
164         inputParams, (const ExtPluginCtx *)g_accountPluginCtx);
165 }
166 
DestoryAccountLifecyclePlugin(void)167 void DestoryAccountLifecyclePlugin(void)
168 {
169     if (g_accountLifeCyclePlugin != NULL && g_accountLifeCyclePlugin->base.destroy != NULL) {
170         g_accountLifeCyclePlugin->base.destroy(&g_accountLifeCyclePlugin->base);
171         g_accountLifeCyclePlugin = NULL;
172     }
173     if (g_accountPluginCtx != NULL) {
174         HcFree(g_accountPluginCtx);
175         g_accountPluginCtx = NULL;
176     }
177 }