diff --git a/interfaces/kits/napi/include/dialog/cm_napi_dialog_common.h b/interfaces/kits/napi/include/dialog/cm_napi_dialog_common.h index 2c8705b4211665f3877f519ea411ed49bb40245f..2549997c2c90ad918d60aa2c609011969033327e 100644 --- a/interfaces/kits/napi/include/dialog/cm_napi_dialog_common.h +++ b/interfaces/kits/napi/include/dialog/cm_napi_dialog_common.h @@ -53,6 +53,8 @@ inline napi_value GetInt32(napi_env env, int32_t value) return result; } +int32_t GetCallerLabelName(std::shared_ptr asyncContext); + enum CmDialogPageType { PAGE_MAIN = 1, PAGE_CA_CERTIFICATE = 2, diff --git a/interfaces/kits/napi/include/dialog/cm_napi_open_dialog.h b/interfaces/kits/napi/include/dialog/cm_napi_open_dialog.h index c34925d582c1972a34072ca25cd6876c5dfeed93..7cf163dbbf4442c9040e35bf08796dcd8496a35e 100644 --- a/interfaces/kits/napi/include/dialog/cm_napi_open_dialog.h +++ b/interfaces/kits/napi/include/dialog/cm_napi_open_dialog.h @@ -32,6 +32,7 @@ const std::string CERT_MANAGER_PAGE_TYPE = "pageType"; const std::string CERT_MANAGER_CERTSCOPE_TYPE = "certScope"; const std::string CERT_MANAGER_CERTIFICATE_DATA = "cert"; const std::string CERT_MANAGER_CALLER_BUNDLENAME = "bundleName"; +const std::string CERT_MANAGER_CERT_URI = "certUri"; const std::string CERT_MANAGER_OPERATION_TYPE = "operationType"; constexpr int32_t PARAM0 = 0; constexpr int32_t PARAM1 = 1; @@ -61,6 +62,11 @@ struct CmUIExtensionRequestContext : public CommonAsyncContext { uint32_t certificateScope = 0; std::string certStr = ""; std::string labelName = ""; +<<<<<<< HEAD +======= + std::string operateType = ""; + CmBlob *certUri = nullptr; +>>>>>>> 3502ab1... publicAPI拉起删除CA证书界面 }; class CmUIExtensionCallback { diff --git a/interfaces/kits/napi/src/dialog/cm_napi_dialog.cpp b/interfaces/kits/napi/src/dialog/cm_napi_dialog.cpp index f0a9d7141c98b39e20dc7d9a2e7021b7c01ca87d..83b0a15ca9ed846d19f3b697fe6cb3f016785f04 100644 --- a/interfaces/kits/napi/src/dialog/cm_napi_dialog.cpp +++ b/interfaces/kits/napi/src/dialog/cm_napi_dialog.cpp @@ -38,7 +38,7 @@ static napi_value CreateCmErrorCode(napi_env env) AddInt32Property(env, dialogErrorCode, "ERROR_OPERATION_FAILED", DIALOG_ERROR_INSTALL_FAILED); AddInt32Property(env, dialogErrorCode, "ERROR_DEVICE_NOT_SUPPORTED", DIALOG_ERROR_NOT_SUPPORTED); AddInt32Property(env, dialogErrorCode, "ERROR_NOT_COMPLY_SECURITY_POLICY", - DIALOG_ERROR_NOT_COMPLY_SECURITY_POLICY); + DIALOG_ERROR_NOT_COMPLY_SECURITY_POLICY); return dialogErrorCode; } diff --git a/interfaces/kits/napi/src/dialog/cm_napi_dialog_common.cpp b/interfaces/kits/napi/src/dialog/cm_napi_dialog_common.cpp index 5821f26c81cc3ee57232f684b7b70ef841af0d1d..2ce51ca393c8dd7e40456721da268ba1eb15af83 100644 --- a/interfaces/kits/napi/src/dialog/cm_napi_dialog_common.cpp +++ b/interfaces/kits/napi/src/dialog/cm_napi_dialog_common.cpp @@ -343,7 +343,6 @@ napi_value GenerateBusinessError(napi_env env, int32_t errorCode) napi_value code = nullptr; int32_t outputCode = TranformErrorCode(errorCode); NAPI_CALL(env, napi_create_int32(env, outputCode, &code)); - napi_value message = nullptr; NAPI_CALL(env, napi_create_string_utf8(env, errorMessage, NAPI_AUTO_LENGTH, &message)); diff --git a/interfaces/kits/napi/src/dialog/cm_napi_open_uninstall_dialog.cpp b/interfaces/kits/napi/src/dialog/cm_napi_open_uninstall_dialog.cpp new file mode 100755 index 0000000000000000000000000000000000000000..bc3c5df369adb1e37d5263f4b3c24a76ac597adb --- /dev/null +++ b/interfaces/kits/napi/src/dialog/cm_napi_open_uninstall_dialog.cpp @@ -0,0 +1,214 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "cm_napi_open_dialog.h" +#include "cm_napi_dialog_common.h" + +#include "cm_log.h" + +#include "syspara/parameters.h" +#include "securec.h" +#include "want.h" +#include "want_params_wrapper.h" + +namespace CMNapi { + +class CmUninstallUIExtensionCallback : public CmUIExtensionCallback { +public: + explicit CmUninstallUIExtensionCallback( + std::shared_ptr& reqContext) : CmUIExtensionCallback(reqContext) + { + this->reqContext_ = reqContext; + } + + ~CmUninstallUIExtensionCallback() override + { + CM_LOG_D("~CmUninstallUIExtensionCallback"); + } + + void OnRelease(const int32_t releaseCode) override + { + CM_LOG_D("UninstallUIExtensionComponent OnRelease(), releaseCode = %d", releaseCode); + if (SetErrorCode(CMR_DIALOG_ERROR_OPERATION_CANCELS)) { + SendMessageBack(); + } + } + + void OnResult(const int32_t resultCode, const OHOS::AAFwk::Want& result) override + { + CM_LOG_D("UninstallUIExtensionComponent OnResult(), resultCode = %d", resultCode); + this->resultCode_ = resultCode; + this->resultWant_ = result; + if (SetErrorCode(this->resultCode_)) { + SendMessageBack(); + } + } + + void OnReceive(const OHOS::AAFwk::WantParams& request) override + { + CM_LOG_D("UninstallUIExtensionComponent OnReceive()"); + this->reqContext_->uri = request.GetStringParam("uri"); /* only install get uri */ + if (SetErrorCode(0)) { + SendMessageBack(); + } + } + + void ProcessCallback(napi_env env, const CommonAsyncContext* asyncContext) override + { + napi_value args = nullptr; + if (asyncContext->errCode == CM_SUCCESS) { + NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &args)); + } else { + args = GenerateBusinessError(env, asyncContext->errCode); + } + + if (asyncContext->deferred != nullptr) { + if (asyncContext->errCode == CM_SUCCESS) { + NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncContext->deferred, args)); + } else { + NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, asyncContext->deferred, args)); + } + } + } + + void OnDestroy() override + { + CM_LOG_D("UninstallUIExtensionComponent OnDestroy()"); + } + +private: + bool SetErrorCode(int32_t errCode) + { + if (this->reqContext_ == nullptr) { + CM_LOG_E("OnError reqContext is nullptr"); + return false; + } + if (this->alreadyCallback_) { + CM_LOG_D("alreadyCallback"); + return false; + } + this->alreadyCallback_ = true; + this->reqContext_->errCode = errCode; + return true; + }; + int32_t resultCode_ = 0; + OHOS::AAFwk::Want resultWant_; + std::shared_ptr reqContext_ = nullptr; + bool alreadyCallback_ = false; +}; + +static bool CMIsCertificateType(const uint32_t value, uint32_t &pageType) +{ + switch (static_cast(value)) { + case CmCertificateType::CA_CERT: + pageType = CmDialogPageType::PAGE_INSTALL_CA_GUIDE; + return true; + default: + return false; + } +} + +static napi_value CMInitAsyncContext(std::shared_ptr asyncContext, + napi_value argv[], size_t length) +{ + //Parse the first param: context + if (!ParseCmUIAbilityContextReq(asyncContext->env, argv[PARAM0], asyncContext->context)) { + CM_LOG_E("ParseUIAbilityContextReq failed"); + return nullptr; + } + + //Parse the second param: certType + uint32_t certificateType = 0; + if (ParseUint32(asyncContext->env, argv[PARAM1], certificateType) == nullptr) { + CM_LOG_E("parse type failed"); + return nullptr; + } + if (!CMIsCertificateType(certificateType, asyncContext->certificateType)) { + CM_LOG_E("certificateType invalid"); + return nullptr; + } + + //Parse the third param: certUri + if (ParseString(asyncContext->env, argv[PARAM2], asyncContext->certUri) == nullptr) { + CM_LOG_E("certUri is invalid"); + return nullptr; + } + //return 0 + return GetInt32(asyncContext->env, 0); +} + +static OHOS::AAFwk::Want CMGetUninstallCertWant(std::shared_ptr asyncContext) +{ + OHOS::AAFwk::Want want; + want.SetElementName(CERT_MANAGER_BUNDLENAME, CERT_MANAGER_ABILITYNAME); + want.SetParam(CERT_MANAGER_PAGE_TYPE, static_cast(asyncContext->certificateType)); + want.SetParam(CERT_MANAGER_CALLER_BUNDLENAME, asyncContext->labelName); + CmBlob *certUri = asyncContext->certUri; + std::string uriStr(reinterpret_cast(certUri->data), certUri->size); + want.SetParam(CERT_MANAGER_CERT_URI, uriStr); + want.SetParam(PARAM_UI_EXTENSION_TYPE, SYS_COMMON_UI); + want.SetParam(CERT_MANAGER_OPERATION_TYPE, static_cast(DIALOG_OPERATION_UNINSTALL)); + return want; +} + +napi_value CMNapiOpenUninstallCertDialog(napi_env env, napi_callback_info info) +{ + //determine the type of device + CM_LOG_D("enter uninstall cert dialog"); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_undefined(env, &result)); + if (OHOS::system::GetParameter("const.product.devicetype", "") != "2in1") { + CM_LOG_E("device type is not 2in1"); + std::string errMsg = "Device type error, device type is not 2in1"; + ThrowError(env, DIALOG_ERROR_NOT_SUPPORTED, errMsg); + return result; + } + + //determine the number of parameters + size_t argc = PARAM_SIZE_THREE; + napi_value argv[PARAM_SIZE_THREE] = { nullptr }; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + if (argc != PARAM_SIZE_THREE) { + CM_LOG_E("param number mismatch"); + std::string errMsg = "Parameter Error. Params number mismatch, need " + std::to_string(PARAM_SIZE_THREE) + + ", given " + std::to_string(argc); + ThrowError(env, PARAM_ERROR, errMsg); + return result; + } + + //parse and init context + auto asyncContext = std::make_shared(env); + asyncContext->env = env; + if (CMInitAsyncContext(asyncContext, argv, sizeof(argv) / sizeof(argv[0])) == nullptr) { + CM_LOG_E("Parse param and init asyncContext failed"); + ThrowError(env, PARAM_ERROR, "Parse param and init asyncContext failed"); + return nullptr; + } + + //get lable name + if (GetCallerLabelName(asyncContext) != CM_SUCCESS) { + CM_LOG_E("get caller labelName faild"); + ThrowError(env, DIALOG_ERROR_GENERIC, "get caller labelName faild"); + return nullptr; + } + NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result)); + + //set want params + auto uiExtCallback = std::make_shared(asyncContext); + StartUIExtensionAbility(asyncContext, CMGetUninstallCertWant(asyncContext), uiExtCallback); + CM_LOG_D("cert install dialog end"); + return result; + +} +} // namespace CMNapi \ No newline at end of file