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