1 /*
2 * Copyright (c) 2025-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_authorize_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 namespace CMNapi {
23
CMGetAuthCertWant(std::shared_ptr<CmUIExtensionRequestContext> asyncContext)24 static OHOS::AAFwk::Want CMGetAuthCertWant(std::shared_ptr<CmUIExtensionRequestContext> asyncContext)
25 {
26 OHOS::AAFwk::Want want;
27 want.SetElementName(CERT_MANAGER_BUNDLENAME, CERT_MANAGER_ABILITYNAME);
28 want.SetParam(CERT_MANAGER_CALLER_BUNDLENAME, asyncContext->labelName);
29 want.SetParam(CERT_MANAGER_CALLER_UID, asyncContext->appUid);
30 want.SetParam(PARAM_UI_EXTENSION_TYPE, SYS_COMMON_UI);
31 want.SetParam(CERT_MANAGER_PAGE_TYPE, static_cast<int32_t>(CmDialogPageType::PAGE_REQUEST_AUTHORIZE));
32 return want;
33 }
34
CMNapiOpenAuthorizeDialog(napi_env env,napi_callback_info info)35 napi_value CMNapiOpenAuthorizeDialog(napi_env env, napi_callback_info info)
36 {
37 CM_LOG_I("cert authorize dialog enter");
38 napi_value result = nullptr;
39 NAPI_CALL(env, napi_get_undefined(env, &result));
40
41 size_t argc = PARAM_SIZE_ONE;
42 napi_value argv[PARAM_SIZE_ONE] = { nullptr };
43 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
44 if (argc != PARAM_SIZE_ONE) {
45 CM_LOG_E("params number mismatch");
46 std::string errMsg = "Parameter Error. Params number mismatch, need " + std::to_string(PARAM_SIZE_ONE)
47 + ", given " + std::to_string(argc);
48 ThrowError(env, PARAM_ERROR, errMsg);
49 return result;
50 }
51
52 auto asyncContext = std::make_shared<CmUIExtensionRequestContext>(env);
53 asyncContext->opType = static_cast<int32_t>(DIALOG_OPERATION_AUTHORIZE);
54 if (!ParseCmUIAbilityContextReq(asyncContext->env, argv[PARAM0], asyncContext->context)) {
55 CM_LOG_E("parse abilityContext failed");
56 ThrowError(env, PARAM_ERROR, "parse abilityContext failed");
57 return nullptr;
58 }
59
60 if (GetCallerLabelName(asyncContext) != CM_SUCCESS) {
61 CM_LOG_E("get caller labelName faild");
62 ThrowError(env, DIALOG_ERROR_GENERIC, "get caller labelName faild");
63 return nullptr;
64 }
65
66 asyncContext->appUid = static_cast<int32_t>(getuid());
67
68 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
69 auto uiExtCallback = std::make_shared<CmOperationUIExtensionCallback>(asyncContext);
70 StartUIExtensionAbility(asyncContext, CMGetAuthCertWant(asyncContext), uiExtCallback);
71 CM_LOG_I("cert authorize dialog end");
72 return result;
73 }
74 } // namespace CMNapi