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 ThrowParamsError(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 context->callback = GetCallback(env, argv[index]);
81 }
82
83 return GetInt32(env, 0);
84 }
85
UninstallAllAppCertAsyncWork(napi_env env,UninstallAllAppCertAsyncContext asyncContext)86 static napi_value UninstallAllAppCertAsyncWork(napi_env env, UninstallAllAppCertAsyncContext asyncContext)
87 {
88 napi_value promise = nullptr;
89 GenerateNapiPromise(env, asyncContext->callback, &asyncContext->deferred, &promise);
90
91 napi_value resourceName = nullptr;
92 NAPI_CALL(env, napi_create_string_latin1(env, "UninstallAllAppCertAsyncWork", NAPI_AUTO_LENGTH, &resourceName));
93
94 NAPI_CALL(env, napi_create_async_work(
95 env,
96 nullptr,
97 resourceName,
98 [](napi_env env, void *data) {
99 UninstallAllAppCertAsyncContext context = static_cast<UninstallAllAppCertAsyncContext>(data);
100 context->result = CmUninstallAllAppCert();
101 },
102 [](napi_env env, napi_status status, void *data) {
103 UninstallAllAppCertAsyncContext context = static_cast<UninstallAllAppCertAsyncContext>(data);
104 napi_value result[RESULT_NUMBER] = { nullptr };
105 if (context->result == CM_SUCCESS) {
106 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, 0, &result[0]));
107 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, true, &result[1]));
108 } else {
109 const char *errorMsg = "uninstall all app cert error";
110 result[0] = GenerateBusinessError(env, context->result, errorMsg);
111 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &result[1]));
112 }
113 if (context->deferred != nullptr) {
114 GeneratePromise(env, context->deferred, context->result, result, sizeof(result));
115 } else {
116 GenerateCallback(env, context->callback, result, sizeof(result));
117 }
118 DeleteUninstallAllAppCertAsyncContext(env, context);
119 },
120 static_cast<void *>(asyncContext),
121 &asyncContext->asyncWork));
122
123 napi_status status = napi_queue_async_work(env, asyncContext->asyncWork);
124 if (status != napi_ok) {
125 GET_AND_THROW_LAST_ERROR((env));
126 DeleteUninstallAllAppCertAsyncContext(env, asyncContext);
127 CM_LOG_E("could not queue async work");
128 return nullptr;
129 }
130
131 return promise;
132 }
133
CMNapiUninstallAllAppCert(napi_env env,napi_callback_info info)134 napi_value CMNapiUninstallAllAppCert(napi_env env, napi_callback_info info)
135 {
136 UninstallAllAppCertAsyncContext context = CreateUinstallAllAppCertAsyncContext();
137 if (context == nullptr) {
138 CM_LOG_E("could not create context");
139 return nullptr;
140 }
141
142 napi_value result = UninstallAllAppCertParseParams(env, info, context);
143 if (result == nullptr) {
144 CM_LOG_E("could not parse params");
145 DeleteUninstallAllAppCertAsyncContext(env, context);
146 return nullptr;
147 }
148 result = UninstallAllAppCertAsyncWork(env, context);
149 if (result == nullptr) {
150 CM_LOG_E("could not start async work");
151 DeleteUninstallAllAppCertAsyncContext(env, context);
152 return nullptr;
153 }
154 return result;
155 }
156 } // namespace CertManagerNapi
157