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