• 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_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 
39     return dialogErrorCode;
40 }
41 
CreateCmDialogPageType(napi_env env)42 static napi_value CreateCmDialogPageType(napi_env env)
43 {
44     napi_value dialogPageType = nullptr;
45     NAPI_CALL(env, napi_create_object(env, &dialogPageType));
46 
47     AddInt32Property(env, dialogPageType, "PAGE_MAIN", PAGE_MAIN);
48     AddInt32Property(env, dialogPageType, "PAGE_CA_CERTIFICATE", PAGE_CA_CERTIFICATE);
49     AddInt32Property(env, dialogPageType, "PAGE_CREDENTIAL", PAGE_CREDENTIAL);
50     AddInt32Property(env, dialogPageType, "PAGE_INSTALL_CERTIFICATE", PAGE_INSTALL_CERTIFICATE);
51 
52     return dialogPageType;
53 }
54 }  // namespace CertManagerNapi
55 
56 using namespace CMNapi;
57 
58 extern "C" {
CMDialogNapiRegister(napi_env env,napi_value exports)59 static napi_value CMDialogNapiRegister(napi_env env, napi_value exports)
60 {
61     napi_property_descriptor desc[] = {
62         DECLARE_NAPI_PROPERTY("CertificateDialogErrorCode", CreateCmErrorCode(env)),
63         DECLARE_NAPI_PROPERTY("CertificateDialogPageType", CreateCmDialogPageType(env)),
64 
65         /* dialog */
66         DECLARE_NAPI_FUNCTION("openCertificateManagerDialog", CMNapiOpenCertManagerDialog),
67     };
68     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
69     return exports;
70 }
71 
72 static napi_module g_module = {
73     .nm_version = 1,
74     .nm_flags = 0,
75     .nm_filename = nullptr,
76     .nm_register_func = CMDialogNapiRegister,
77     .nm_modname = "security.certManagerDialog",
78     .nm_priv =  nullptr,
79     .reserved = { nullptr },
80 };
81 
CMDialogNapiRegister(void)82 __attribute__((constructor)) void CMDialogNapiRegister(void)
83 {
84     napi_module_register(&g_module);
85 }
86 }
87