• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_install_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 {
IsCmCertificateScopeEnum(const uint32_t value)28 static bool IsCmCertificateScopeEnum(const uint32_t value)
29 {
30     return value >= NOT_SPECIFIED && value <= GLOBAL_USER;
31 }
32 
IsCmCertificateTypeAndConvert(const uint32_t value,uint32_t & pageType)33 static bool IsCmCertificateTypeAndConvert(const uint32_t value, uint32_t &pageType)
34 {
35     switch (static_cast<CmCertificateType>(value)) {
36         case CmCertificateType::CA_CERT:
37             pageType = CmDialogPageType::PAGE_INSTALL_CA_GUIDE;
38             return true;
39         default:
40             return false;
41     }
42 }
43 
CMCheckArgvAndInitContext(std::shared_ptr<CmUIExtensionRequestContext> asyncContext,napi_value argv[],size_t length)44 static napi_value CMCheckArgvAndInitContext(std::shared_ptr<CmUIExtensionRequestContext> asyncContext,
45     napi_value argv[], size_t length)
46 {
47     if (length != PARAM_SIZE_FOUR) {
48         CM_LOG_E("params number vaild failed");
49         return nullptr;
50     }
51     // Parse first argument for context.
52     if (!ParseCmUIAbilityContextReq(asyncContext->env, argv[PARAM0], asyncContext->context)) {
53         CM_LOG_E("ParseUIAbilityContextReq failed");
54         return nullptr;
55     }
56 
57     // Parse second argument for certificate type.
58     uint32_t certificateType = 0;
59     if (ParseUint32(asyncContext->env, argv[PARAM1], certificateType) == nullptr) {
60         CM_LOG_E("parse type failed");
61         return nullptr;
62     }
63     if (!IsCmCertificateTypeAndConvert(certificateType, asyncContext->certificateType)) {
64         CM_LOG_E("certificateType invalid");
65         return nullptr;
66     }
67 
68     // Parse third argument for certificateScope.
69     if (ParseUint32(asyncContext->env, argv[PARAM2], asyncContext->certificateScope) == nullptr) {
70         CM_LOG_E("parse type failed");
71         return nullptr;
72     }
73     if (!IsCmCertificateScopeEnum(asyncContext->certificateScope)) {
74         CM_LOG_E("certificateScope invalid");
75         return nullptr;
76     }
77 
78     // Parse fourth argument for cert.
79     if (GetUint8ArrayToBase64Str(asyncContext->env, argv[PARAM3], asyncContext->certStr) == nullptr) {
80         CM_LOG_E("cert is not a uint8Array or the length is 0 or too long.");
81         return nullptr;
82     }
83     return GetInt32(asyncContext->env, 0);
84 }
85 
CMGetInstallCertWant(std::shared_ptr<CmUIExtensionRequestContext> asyncContext)86 static OHOS::AAFwk::Want CMGetInstallCertWant(std::shared_ptr<CmUIExtensionRequestContext> asyncContext)
87 {
88     OHOS::AAFwk::Want want;
89     want.SetElementName(CERT_MANAGER_BUNDLENAME, CERT_MANAGER_ABILITYNAME);
90     want.SetParam(CERT_MANAGER_PAGE_TYPE, static_cast<int32_t>(asyncContext->certificateType));
91     want.SetParam(CERT_MANAGER_CERTIFICATE_DATA, asyncContext->certStr);
92     want.SetParam(CERT_MANAGER_CERTSCOPE_TYPE, static_cast<int32_t>(asyncContext->certificateScope));
93     want.SetParam(CERT_MANAGER_CALLER_BUNDLENAME, asyncContext->labelName);
94     want.SetParam(PARAM_UI_EXTENSION_TYPE, SYS_COMMON_UI);
95     want.SetParam(CERT_MANAGER_OPERATION_TYPE, asyncContext->opType);
96     return want;
97 }
98 
CMNapiOpenInstallCertDialog(napi_env env,napi_callback_info info)99 napi_value CMNapiOpenInstallCertDialog(napi_env env, napi_callback_info info)
100 {
101     CM_LOG_I("cert install dialog enter");
102     napi_value result = nullptr;
103     NAPI_CALL(env, napi_get_undefined(env, &result));
104     if (OHOS::system::GetParameter("const.product.devicetype", "") != "2in1") {
105         CM_LOG_E("deviceType is not 2in1");
106         std::string errMsg = "DeviceType Error. deviceType is not 2in1";
107         ThrowError(env, DIALOG_ERROR_NOT_SUPPORTED, errMsg);
108         return result;
109     }
110 
111     size_t argc = PARAM_SIZE_FOUR;
112     napi_value argv[PARAM_SIZE_FOUR] = { nullptr };
113     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
114     if (argc != PARAM_SIZE_FOUR) {
115         CM_LOG_E("params number mismatch");
116         std::string errMsg = "Parameter Error. Params number mismatch, need " + std::to_string(PARAM_SIZE_FOUR)
117             + ", given " + std::to_string(argc);
118         ThrowError(env, PARAM_ERROR, errMsg);
119         return result;
120     }
121 
122     auto asyncContext = std::make_shared<CmUIExtensionRequestContext>(env);
123     asyncContext->env = env;
124     asyncContext->opType = static_cast<int32_t>(DIALOG_OPERATION_INSTALL);
125     if (CMCheckArgvAndInitContext(asyncContext, argv, sizeof(argv) / sizeof(argv[0])) == nullptr) {
126         CM_LOG_E("check argv vaild and init faild");
127         ThrowError(env, PARAM_ERROR, "check argv vaild and init faild");
128         return nullptr;
129     }
130 
131     if (GetCallerLabelName(asyncContext) != CM_SUCCESS) {
132         CM_LOG_E("get caller labelName faild");
133         ThrowError(env, DIALOG_ERROR_GENERIC, "get caller labelName faild");
134         return nullptr;
135     }
136     NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
137 
138     auto uiExtCallback = std::make_shared<CmOperationUIExtensionCallback>(asyncContext);
139     StartUIExtensionAbility(asyncContext, CMGetInstallCertWant(asyncContext), uiExtCallback);
140     CM_LOG_I("cert install dialog end");
141     return result;
142 }
143 }  // namespace CMNapi
144 
145