1 /*
2  * Copyright (c) 2022 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 "cm_napi_uninstall_all_app_cert.h"
17 
18 #include "securec.h"
19 
20 #include "cert_manager_api.h"
21 #include "cm_log.h"
22 #include "cm_mem.h"
23 #include "cm_type.h"
24 #include "cm_napi_common.h"
25 
26 namespace CMNapi {
27 namespace {
28 constexpr int CM_NAPI_UNINSTALL_ALL_APP_CERT_MIN_ARGS = 0;
29 constexpr int CM_NAPI_UNINSTALL_ALL_APP_CERT_MAX_ARGS = 1;
30 }  // namespace
31 
32 struct UninstallAllAppCertAsyncContextT {
33     napi_async_work asyncWork = nullptr;
34     napi_deferred deferred = nullptr;
35     napi_ref callback = nullptr;
36 
37     int32_t result = 0;
38 };
39 using UninstallAllAppCertAsyncContext = UninstallAllAppCertAsyncContextT *;
40 
CreateUinstallAllAppCertAsyncContext()41 static UninstallAllAppCertAsyncContext CreateUinstallAllAppCertAsyncContext()
42 {
43     UninstallAllAppCertAsyncContext context =
44         static_cast<UninstallAllAppCertAsyncContext>(CmMalloc(sizeof(UninstallAllAppCertAsyncContextT)));
45     if (context != nullptr) {
46         (void)memset_s(
47             context, sizeof(UninstallAllAppCertAsyncContextT), 0, sizeof(UninstallAllAppCertAsyncContextT));
48     }
49     return context;
50 }
51 
DeleteUninstallAllAppCertAsyncContext(napi_env env,UninstallAllAppCertAsyncContext & context)52 static void DeleteUninstallAllAppCertAsyncContext(napi_env env, UninstallAllAppCertAsyncContext &context)
53 {
54     if (context == nullptr) {
55         return;
56     }
57 
58     DeleteNapiContext(env, context->asyncWork, context->callback);
59 
60     CmFree(context);
61     context = nullptr;
62 }
63 
UninstallAllAppCertParseParams(napi_env env,napi_callback_info info,UninstallAllAppCertAsyncContext context)64 static napi_value UninstallAllAppCertParseParams(
65     napi_env env, napi_callback_info info, UninstallAllAppCertAsyncContext context)
66 {
67     size_t argc = CM_NAPI_UNINSTALL_ALL_APP_CERT_MAX_ARGS;
68     napi_value argv[CM_NAPI_UNINSTALL_ALL_APP_CERT_MAX_ARGS] = { nullptr };
69     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
70 
71     if ((argc != CM_NAPI_UNINSTALL_ALL_APP_CERT_MIN_ARGS) && (argc != CM_NAPI_UNINSTALL_ALL_APP_CERT_MAX_ARGS)) {
72         ThrowError(env, PARAM_ERROR, "Missing parameter");
73         CM_LOG_E("Missing parameter");
74         return nullptr;
75     }
76 
77     size_t index = 0;
78 
79     if (index < argc) {
80         int32_t ret = GetCallback(env, argv[index], context->callback);
81         if (ret != CM_SUCCESS) {
82             ThrowError(env, PARAM_ERROR, "Get callback type failed.");
83             CM_LOG_E("get callback function failed when uninstall all app cert function");
84             return nullptr;
85         }
86     }
87 
88     return GetInt32(env, 0);
89 }
90 
UninstallAllAppCertAsyncWork(napi_env env,UninstallAllAppCertAsyncContext asyncContext)91 static napi_value UninstallAllAppCertAsyncWork(napi_env env, UninstallAllAppCertAsyncContext asyncContext)
92 {
93     napi_value promise = nullptr;
94     GenerateNapiPromise(env, asyncContext->callback, &asyncContext->deferred, &promise);
95 
96     napi_value resourceName = nullptr;
97     NAPI_CALL(env, napi_create_string_latin1(env, "UninstallAllAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
98 
99     NAPI_CALL(env, napi_create_async_work(
100         env,
101         nullptr,
102         resourceName,
103         [](napi_env env, void *data) {
104             UninstallAllAppCertAsyncContext context = static_cast<UninstallAllAppCertAsyncContext>(data);
105             context->result = CmUninstallAllAppCert();
106         },
107         [](napi_env env, napi_status status, void *data) {
108             UninstallAllAppCertAsyncContext context = static_cast<UninstallAllAppCertAsyncContext>(data);
109             napi_value result[RESULT_NUMBER] = { nullptr };
110             if (context->result == CM_SUCCESS) {
111                 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
112                 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, true, &result[1]));
113             } else {
114                 result[0] = GenerateBusinessError(env, context->result);
115                 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
116             }
117             if (context->deferred != nullptr) {
118                 GeneratePromise(env, context->deferred, context->result, result, CM_ARRAY_SIZE(result));
119             } else {
120                 GenerateCallback(env, context->callback, result, CM_ARRAY_SIZE(result), context->result);
121             }
122             DeleteUninstallAllAppCertAsyncContext(env, context);
123         },
124         static_cast<void *>(asyncContext),
125         &asyncContext->asyncWork));
126 
127     napi_status status = napi_queue_async_work(env, asyncContext->asyncWork);
128     if (status != napi_ok) {
129         GET_AND_THROW_LAST_ERROR((env));
130         DeleteUninstallAllAppCertAsyncContext(env, asyncContext);
131         CM_LOG_E("could not queue async work");
132         return nullptr;
133     }
134 
135     return promise;
136 }
137 
CMNapiUninstallAllAppCert(napi_env env,napi_callback_info info)138 napi_value CMNapiUninstallAllAppCert(napi_env env, napi_callback_info info)
139 {
140     UninstallAllAppCertAsyncContext context = CreateUinstallAllAppCertAsyncContext();
141     if (context == nullptr) {
142         CM_LOG_E("could not create context");
143         return nullptr;
144     }
145 
146     napi_value result = UninstallAllAppCertParseParams(env, info, context);
147     if (result == nullptr) {
148         CM_LOG_E("could not parse params");
149         DeleteUninstallAllAppCertAsyncContext(env, context);
150         return nullptr;
151     }
152     result = UninstallAllAppCertAsyncWork(env, context);
153     if (result == nullptr) {
154         CM_LOG_E("could not start async work");
155         DeleteUninstallAllAppCertAsyncContext(env, context);
156         return nullptr;
157     }
158     return result;
159 }
160 }  // namespace CertManagerNapi
161