1 /*
2  * Copyright (c) 2024 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 "napi/native_api.h"
17 #include "napi/native_node_api.h"
18 
19 #include "cm_napi_dialog_common.h"
20 
21 #include "cm_napi_open_dialog.h"
22 
23 namespace CMNapi {
AddInt32Property(napi_env env,napi_value object,const char * name,int32_t value)24 inline void AddInt32Property(napi_env env, napi_value object, const char *name, int32_t value)
25 {
26     napi_value property = nullptr;
27     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, value, &property));
28     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, name, property));
29 }
30 
CreateCmErrorCode(napi_env env)31 static napi_value CreateCmErrorCode(napi_env env)
32 {
33     napi_value dialogErrorCode = nullptr;
34     NAPI_CALL(env, napi_create_object(env, &dialogErrorCode));
35 
36     AddInt32Property(env, dialogErrorCode, "ERROR_GENERIC", DIALOG_ERROR_GENERIC);
37     AddInt32Property(env, dialogErrorCode, "ERROR_OPERATION_CANCELED", DIALOG_ERROR_OPERATION_CANCELED);
38     AddInt32Property(env, dialogErrorCode, "ERROR_OPERATION_FAILED", DIALOG_ERROR_INSTALL_FAILED);
39     AddInt32Property(env, dialogErrorCode, "ERROR_DEVICE_NOT_SUPPORTED", DIALOG_ERROR_NOT_SUPPORTED);
40 
41     return dialogErrorCode;
42 }
43 
CreateCmDialogPageType(napi_env env)44 static napi_value CreateCmDialogPageType(napi_env env)
45 {
46     napi_value dialogPageType = nullptr;
47     NAPI_CALL(env, napi_create_object(env, &dialogPageType));
48 
49     AddInt32Property(env, dialogPageType, "PAGE_MAIN", PAGE_MAIN);
50     AddInt32Property(env, dialogPageType, "PAGE_CA_CERTIFICATE", PAGE_CA_CERTIFICATE);
51     AddInt32Property(env, dialogPageType, "PAGE_CREDENTIAL", PAGE_CREDENTIAL);
52     AddInt32Property(env, dialogPageType, "PAGE_INSTALL_CERTIFICATE", PAGE_INSTALL_CERTIFICATE);
53 
54     return dialogPageType;
55 }
56 
CreateCmCertificateType(napi_env env)57 static napi_value CreateCmCertificateType(napi_env env)
58 {
59     napi_value certificateType = nullptr;
60     NAPI_CALL(env, napi_create_object(env, &certificateType));
61 
62     AddInt32Property(env, certificateType, "CA_CERT", CA_CERT);
63 
64     return certificateType;
65 }
66 
CreateCmCertificateScope(napi_env env)67 static napi_value CreateCmCertificateScope(napi_env env)
68 {
69     napi_value certificateScope = nullptr;
70     NAPI_CALL(env, napi_create_object(env, &certificateScope));
71 
72     AddInt32Property(env, certificateScope, "CURRENT_USER", CURRENT_USER);
73 
74     return certificateScope;
75 }
76 }  // namespace CertManagerNapi
77 
78 using namespace CMNapi;
79 
80 extern "C" {
CMDialogNapiRegister(napi_env env,napi_value exports)81 static napi_value CMDialogNapiRegister(napi_env env, napi_value exports)
82 {
83     napi_property_descriptor desc[] = {
84         DECLARE_NAPI_PROPERTY("CertificateDialogErrorCode", CreateCmErrorCode(env)),
85         DECLARE_NAPI_PROPERTY("CertificateDialogPageType", CreateCmDialogPageType(env)),
86         DECLARE_NAPI_PROPERTY("CertificateType", CreateCmCertificateType(env)),
87         DECLARE_NAPI_PROPERTY("CertificateScope", CreateCmCertificateScope(env)),
88 
89         /* dialog */
90         DECLARE_NAPI_FUNCTION("openCertificateManagerDialog", CMNapiOpenCertManagerDialog),
91         DECLARE_NAPI_FUNCTION("openInstallCertificateDialog", CMNapiOpenInstallCertDialog),
92     };
93     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
94     return exports;
95 }
96 
97 static napi_module g_module = {
98     .nm_version = 1,
99     .nm_flags = 0,
100     .nm_filename = nullptr,
101     .nm_register_func = CMDialogNapiRegister,
102     .nm_modname = "security.certManagerDialog",
103     .nm_priv =  nullptr,
104     .reserved = { nullptr },
105 };
106 
CMDialogNapiRegister(void)107 __attribute__((constructor)) void CMDialogNapiRegister(void)
108 {
109     napi_module_register(&g_module);
110 }
111 }
112