• 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 "ext_plugin_manager.h"
17 
18 #include "account_related_cred_plugin.h"
19 #include "device_auth_defines.h"
20 #include "hc_log.h"
21 #include "hc_vector.h"
22 #include "hitrace_adapter.h"
23 
24 DECLARE_HC_VECTOR(CredPluginVec, CredPlugin *);
25 IMPLEMENT_HC_VECTOR(CredPluginVec, CredPlugin *, 2)
26 
27 static CredPluginVec g_credPluginVec;
28 
ProcCred(int32_t pluginName,int32_t osAccountId,int32_t cmdId,CJson * in,CJson * out)29 int32_t ProcCred(int32_t pluginName, int32_t osAccountId, int32_t cmdId, CJson *in, CJson *out)
30 {
31     uint32_t index;
32     CredPlugin **pluginPtr;
33     FOR_EACH_HC_VECTOR(g_credPluginVec, index, pluginPtr) {
34         if ((*pluginPtr)->pluginName == pluginName) {
35             return (*pluginPtr)->procCred(osAccountId, cmdId, in, out);
36         }
37     }
38     LOGE("[CredMgr]: There is no matched cred plugin. [Name]: %" LOG_PUB "d", pluginName);
39     return HC_ERR_NOT_SUPPORT;
40 }
41 
InitCredMgr(void)42 int32_t InitCredMgr(void)
43 {
44     g_credPluginVec = CREATE_HC_VECTOR(CredPluginVec);
45     CredPlugin *plugin = GetAccountRelatedCredPlugin();
46     if (plugin != NULL) {
47         int32_t res = plugin->init();
48         if (res == HC_SUCCESS) {
49             if (g_credPluginVec.pushBackT(&g_credPluginVec, plugin) == NULL) {
50                 LOGE("[CredMgr]: Failed to push plugin!");
51                 plugin->destroy();
52                 return HC_ERR_ALLOC_MEMORY;
53             }
54         } else {
55             LOGW("[CredMgr]: Init account related cred plugin fail. [Res]: %" LOG_PUB "d", res);
56         }
57     }
58     LOGI("[CredMgr]: Init success!");
59     return HC_SUCCESS;
60 }
61 
DestroyCredMgr(void)62 void DestroyCredMgr(void)
63 {
64     uint32_t index;
65     CredPlugin **pluginPtr;
66     FOR_EACH_HC_VECTOR(g_credPluginVec, index, pluginPtr) {
67         (*pluginPtr)->destroy();
68     }
69     DESTROY_HC_VECTOR(CredPluginVec, &g_credPluginVec);
70 }
71 
AddCredPlugin(const CredPlugin * plugin)72 int32_t AddCredPlugin(const CredPlugin *plugin)
73 {
74     if (plugin == NULL || plugin->init == NULL ||
75         plugin->destroy == NULL || plugin->procCred == NULL) {
76         LOGE("The plugin is invalid.");
77         return HC_ERR_INVALID_PARAMS;
78     }
79     int32_t res = plugin->init();
80     if (res != HC_SUCCESS) {
81         LOGE("[CredMgr]: Init cred plugin fail. [Res]: %" LOG_PUB "d", res);
82         return HC_ERR_INIT_FAILED;
83     }
84     bool isNeedReplace = false;
85     uint32_t index;
86     CredPlugin **pluginPtr;
87     FOR_EACH_HC_VECTOR(g_credPluginVec, index, pluginPtr) {
88         if ((*pluginPtr)->pluginName == plugin->pluginName) {
89             isNeedReplace = true;
90             break;
91         }
92     }
93     if (g_credPluginVec.pushBack(&g_credPluginVec, &plugin) == NULL) {
94         LOGE("[CredMgr]: Push cred plugin to vector fail.");
95         plugin->destroy();
96         return HC_ERR_ALLOC_MEMORY;
97     }
98     if (isNeedReplace) {
99         LOGI("[CredMgr]: Replace cred plugin. [Name]: %" LOG_PUB "d", plugin->pluginName);
100         HC_VECTOR_POPELEMENT(&g_credPluginVec, pluginPtr, index);
101     } else {
102         LOGI("[CredMgr]: Add new cred plugin. [Name]: %" LOG_PUB "d", plugin->pluginName);
103     }
104     return HC_SUCCESS;
105 }
106 
DelCredPlugin(int32_t pluginName)107 void DelCredPlugin(int32_t pluginName)
108 {
109     uint32_t index;
110     CredPlugin **pluginPtr;
111     FOR_EACH_HC_VECTOR(g_credPluginVec, index, pluginPtr) {
112         if ((*pluginPtr)->pluginName == pluginName) {
113             LOGI("[CredMgr]: Delete cred plugin success. [Name]: %" LOG_PUB "d", pluginName);
114             (*pluginPtr)->destroy();
115             HC_VECTOR_POPELEMENT(&g_credPluginVec, pluginPtr, index);
116             break;
117         }
118     }
119 }
120