1 /*
2 * Copyright (c) 2025 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_open_uninstall_dialog.h"
17
18 #include "cm_log.h"
19 #include "cm_napi_dialog_common.h"
20 #include "cm_napi_open_certificate_operation.h"
21
22 #include "securec.h"
23 #include "syspara/parameters.h"
24 #include "want.h"
25 #include "want_params_wrapper.h"
26
27 namespace CMNapi {
GetInstallButtonState(napi_env env,napi_value arg,bool & showInstallButton)28 static int32_t GetInstallButtonState(napi_env env, napi_value arg, bool &showInstallButton)
29 {
30 napi_value value = nullptr;
31 napi_status status = napi_get_named_property(env, arg, CERT_MANAGER_SHOW_INSTALL_BUTTON.c_str(), &value);
32 if (status != napi_ok) {
33 CM_LOG_E("Failed to get showInstallBotton");
34 return CM_FAILURE;
35 }
36
37 napi_value result = ParseBoolean(env, value, showInstallButton);
38 if (result == nullptr) {
39 CM_LOG_E("Failed to get showInstallBotton value");
40 return CM_FAILURE;
41 }
42
43 return CM_SUCCESS;
44 }
45
CheckDetailParamsAndInitContext(std::shared_ptr<CmUIExtensionRequestContext> asyncContext,napi_value argv[],size_t length)46 static int32_t CheckDetailParamsAndInitContext(std::shared_ptr<CmUIExtensionRequestContext> asyncContext,
47 napi_value argv[], size_t length)
48 {
49 /* parse context */
50 bool parseRet = ParseCmUIAbilityContextReq(asyncContext->env, argv[PARAM0], asyncContext->context);
51 if (!parseRet) {
52 CM_LOG_E("ParseUIAbilityContextReq failed");
53 return CM_FAILURE;
54 }
55
56 /* parse cert */
57 napi_value value = GetUint8ArrayToBase64Str(asyncContext->env, argv[PARAM1], asyncContext->certStr);
58 if (value == nullptr) {
59 CM_LOG_E("cert is not a uint8Array or the length is 0 or too long.");
60 return CM_FAILURE;
61 }
62
63 /* parse property */
64 int32_t ret = GetInstallButtonState(asyncContext->env, argv[PARAM2], asyncContext->showInstallButton);
65 if (ret != CM_SUCCESS) {
66 CM_LOG_E("get property showInstallBotton failed");
67 return CM_FAILURE;
68 }
69
70 return CM_SUCCESS;
71 }
72
CMGetCertDetailWant(std::shared_ptr<CmUIExtensionRequestContext> asyncContext)73 static OHOS::AAFwk::Want CMGetCertDetailWant(std::shared_ptr<CmUIExtensionRequestContext> asyncContext)
74 {
75 OHOS::AAFwk::Want want;
76 want.SetElementName(CERT_MANAGER_BUNDLENAME, CERT_MANAGER_ABILITYNAME);
77 want.SetParam(PARAM_UI_EXTENSION_TYPE, SYS_COMMON_UI);
78 want.SetParam(CERT_MANAGER_CERTIFICATE_DATA, asyncContext->certStr);
79 want.SetParam(CERT_MANAGER_OPERATION_TYPE, asyncContext->opType);
80 want.SetParam(CERT_MANAGER_SHOW_INSTALL_BUTTON, asyncContext->showInstallButton);
81 want.SetParam(CERT_MANAGER_PAGE_TYPE, static_cast<int32_t>(CmDialogPageType::PAGE_INSTALL_CA_GUIDE));
82 return want;
83 }
84
CMNapiOpenDetailDialog(napi_env env,napi_callback_info info)85 napi_value CMNapiOpenDetailDialog(napi_env env, napi_callback_info info)
86 {
87 CM_LOG_I("cert open detail dialog enter");
88 if (OHOS::system::GetParameter("const.product.devicetype", "") != "2in1") {
89 CM_LOG_E("deviceType is not 2in1");
90 ThrowError(env, DIALOG_ERROR_NOT_SUPPORTED, "DeviceType Error. deviceType is not 2in1");
91 return nullptr;
92 }
93
94 size_t argc = PARAM_SIZE_THREE;
95 napi_value argv[PARAM_SIZE_THREE] = { nullptr };
96 napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
97 if (status != napi_ok || argc != PARAM_SIZE_THREE) {
98 CM_LOG_E("params number mismatch");
99 ThrowError(env, PARAM_ERROR, "Parameter Error. Params number mismatch.");
100 return nullptr;
101 }
102
103 auto asyncContext = std::make_shared<CmUIExtensionRequestContext>(env);
104 asyncContext->env = env;
105 asyncContext->opType = static_cast<int32_t>(DIALOG_OPERATION_DETAIL);
106
107 int32_t ret = CheckDetailParamsAndInitContext(asyncContext, argv, argc);
108 if (ret != CM_SUCCESS) {
109 CM_LOG_E("failed to check params and init.");
110 ThrowError(env, PARAM_ERROR, "failed to check params and init.");
111 return nullptr;
112 }
113
114 napi_value result = nullptr;
115 status = napi_create_promise(env, &asyncContext->deferred, &result);
116 if (status != napi_ok) {
117 CM_LOG_E("failed to create promise.");
118 ThrowError(env, DIALOG_ERROR_GENERIC, "failed to create promise.");
119 return nullptr;
120 }
121
122 auto uiExtCallback = std::make_shared<CmOperationUIExtensionCallback>(asyncContext);
123 StartUIExtensionAbility(asyncContext, CMGetCertDetailWant(asyncContext), uiExtCallback);
124 CM_LOG_I("cert open detail dialog end");
125 return result;
126 }
127 } // namespace CMNapi
128