1 /*
2 * Copyright (c) 2024-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 {
CMIsCertificateType(const uint32_t value,uint32_t & pageType)28 static bool CMIsCertificateType(const uint32_t value, uint32_t &pageType)
29 {
30 switch (static_cast<CmCertificateType>(value)) {
31 case CmCertificateType::CA_CERT:
32 pageType = CmDialogPageType::PAGE_INSTALL_CA_GUIDE;
33 return true;
34 default:
35 return false;
36 }
37 }
38
CMInitAsyncContext(std::shared_ptr<CmUIExtensionRequestContext> asyncContext,napi_value argv[],size_t length)39 static napi_value CMInitAsyncContext(std::shared_ptr<CmUIExtensionRequestContext> asyncContext,
40 napi_value argv[], size_t length)
41 {
42 //Parse the first param: context
43 if (!ParseCmUIAbilityContextReq(asyncContext->env, argv[PARAM0], asyncContext->context)) {
44 CM_LOG_E("ParseUIAbilityContextReq failed");
45 return nullptr;
46 }
47
48 //Parse the second param: certType
49 uint32_t certificateType = 0;
50 if (ParseUint32(asyncContext->env, argv[PARAM1], certificateType) == nullptr) {
51 CM_LOG_E("parse type failed");
52 return nullptr;
53 }
54 if (!CMIsCertificateType(certificateType, asyncContext->certificateType)) {
55 CM_LOG_E("certificateType invalid");
56 return nullptr;
57 }
58
59 //Parse the third param: certUri
60 if (ParseString(asyncContext->env, argv[PARAM2], asyncContext->certUri) == nullptr) {
61 CM_LOG_E("certUri is invalid");
62 return nullptr;
63 }
64 //return 0
65 return GetInt32(asyncContext->env, 0);
66 }
67
CMGetUninstallCertWant(std::shared_ptr<CmUIExtensionRequestContext> asyncContext)68 static OHOS::AAFwk::Want CMGetUninstallCertWant(std::shared_ptr<CmUIExtensionRequestContext> asyncContext)
69 {
70 OHOS::AAFwk::Want want;
71 want.SetElementName(CERT_MANAGER_BUNDLENAME, CERT_MANAGER_ABILITYNAME);
72 want.SetParam(CERT_MANAGER_PAGE_TYPE, static_cast<int32_t>(asyncContext->certificateType));
73 want.SetParam(CERT_MANAGER_CALLER_BUNDLENAME, asyncContext->labelName);
74 CmBlob *certUri = asyncContext->certUri;
75 std::string uriStr(reinterpret_cast<char *>(certUri->data), certUri->size);
76 want.SetParam(CERT_MANAGER_CERT_URI, uriStr);
77 want.SetParam(PARAM_UI_EXTENSION_TYPE, SYS_COMMON_UI);
78 want.SetParam(CERT_MANAGER_OPERATION_TYPE, asyncContext->opType);
79 return want;
80 }
81
CMNapiOpenUninstallCertDialog(napi_env env,napi_callback_info info)82 napi_value CMNapiOpenUninstallCertDialog(napi_env env, napi_callback_info info)
83 {
84 //determine the type of device
85 CM_LOG_I("enter uninstall cert dialog");
86 napi_value result = nullptr;
87 NAPI_CALL(env, napi_get_undefined(env, &result));
88 if (OHOS::system::GetParameter("const.product.devicetype", "") != "2in1") {
89 CM_LOG_E("device type is not 2in1");
90 std::string errMsg = "Device type error, device type is not 2in1";
91 ThrowError(env, DIALOG_ERROR_NOT_SUPPORTED, errMsg);
92 return result;
93 }
94
95 //determine the number of parameters
96 size_t argc = PARAM_SIZE_THREE;
97 napi_value argv[PARAM_SIZE_THREE] = { nullptr };
98 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
99 if (argc != PARAM_SIZE_THREE) {
100 CM_LOG_E("param number mismatch");
101 std::string errMsg = "Parameter Error. Params number mismatch, need " + std::to_string(PARAM_SIZE_THREE)
102 + ", given " + std::to_string(argc);
103 ThrowError(env, PARAM_ERROR, errMsg);
104 return result;
105 }
106
107 //parse and init context
108 auto asyncContext = std::make_shared<CmUIExtensionRequestContext>(env);
109 asyncContext->env = env;
110 asyncContext->opType = static_cast<int32_t>(DIALOG_OPERATION_UNINSTALL);
111 if (CMInitAsyncContext(asyncContext, argv, argc) == nullptr) {
112 CM_LOG_E("Parse param and init asyncContext failed");
113 ThrowError(env, PARAM_ERROR, "Parse param and init asyncContext failed");
114 return nullptr;
115 }
116
117 //get lable name
118 if (GetCallerLabelName(asyncContext) != CM_SUCCESS) {
119 CM_LOG_E("get caller labelName faild");
120 ThrowError(env, DIALOG_ERROR_GENERIC, "get caller labelName faild");
121 return nullptr;
122 }
123 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
124
125 //set want params
126 auto uiExtCallback = std::make_shared<CmOperationUIExtensionCallback>(asyncContext);
127 StartUIExtensionAbility(asyncContext, CMGetUninstallCertWant(asyncContext), uiExtCallback);
128 CM_LOG_I("cert uninstall dialog end");
129 return result;
130 }
131 } // namespace CMNapi