diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index d24b2599856327b0b87c5464efb30a24d4ba3ed5..e82eb67d24f824e60fd908826d05e523effa868d 100644 --- a/interfaces/kits/napi/BUILD.gn +++ b/interfaces/kits/napi/BUILD.gn @@ -87,6 +87,8 @@ ohos_shared_library("certmanagerdialog") { sources = [ "src/dialog/cm_napi_dialog.cpp", "src/dialog/cm_napi_dialog_common.cpp", + "src/dialog/cm_napi_open_certificate_operation.cpp", + "src/dialog/cm_napi_open_detail_dialog.cpp", "src/dialog/cm_napi_open_dialog.cpp", "src/dialog/cm_napi_open_install_dialog.cpp", "src/dialog/cm_napi_open_uninstall_dialog.cpp", 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 2549997c2c90ad918d60aa2c609011969033327e..f0fc15724c0d749369fe45d1222fabfdef52b707 100644 --- a/interfaces/kits/napi/include/dialog/cm_napi_dialog_common.h +++ b/interfaces/kits/napi/include/dialog/cm_napi_dialog_common.h @@ -88,6 +88,7 @@ enum ErrorCode { enum OperationType { DIALOG_OPERATION_INSTALL = 1, DIALOG_OPERATION_UNINSTALL = 2, + DIALOG_OPERATION_DETAIL = 3, }; } // namespace CertManagerNapi diff --git a/interfaces/kits/napi/include/dialog/cm_napi_open_certificate_operation.h b/interfaces/kits/napi/include/dialog/cm_napi_open_certificate_operation.h new file mode 100644 index 0000000000000000000000000000000000000000..4fa5e747385c7456e4221ad028587b4e628eb18a --- /dev/null +++ b/interfaces/kits/napi/include/dialog/cm_napi_open_certificate_operation.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 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. + */ + +#ifndef CM_NAPI_OPEN_CERTIFICATE_OPERATION_H +#define CM_NAPI_OPEN_CERTIFICATE_OPERATION_H + +#include "cm_napi_open_dialog.h" + +namespace CMNapi { +class CmOperationUIExtensionCallback : public CmUIExtensionCallback { +public: + explicit CmOperationUIExtensionCallback(std::shared_ptr& reqContext); + ~CmOperationUIExtensionCallback() override; + void OnRelease(const int32_t releaseCode) override; + void OnResult(const int32_t resultCode, const OHOS::AAFwk::Want& result) override; + void OnReceive(const OHOS::AAFwk::WantParams& request) override; + void ProcessCallback(napi_env env, const CommonAsyncContext* asyncContext) override; + void OnDestroy() override; + +private: + bool SetErrorCode(int32_t errCode); + int32_t resultCode_ = 0; + OHOS::AAFwk::Want resultWant_; + std::shared_ptr reqContext_ = nullptr; + bool alreadyCallback_ = false; +}; +} // namespace CMNapi + +#endif // CM_NAPI_OPEN_CERTIFICATE_OPERATION_H diff --git a/interfaces/kits/napi/include/dialog/cm_napi_open_detail_dialog.h b/interfaces/kits/napi/include/dialog/cm_napi_open_detail_dialog.h new file mode 100644 index 0000000000000000000000000000000000000000..5860e7f7a3b5e09f7c5d3e1c95adc2d1e72f1b48 --- /dev/null +++ b/interfaces/kits/napi/include/dialog/cm_napi_open_detail_dialog.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 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. + */ + +#ifndef CM_NAPI_OPEN_DETAIL_DIALOG_H +#define CM_NAPI_OPEN_DETAIL_DIALOG_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace CMNapi { +napi_value CMNapiOpenDetailDialog(napi_env env, napi_callback_info info); +} // namespace CMNapi + +#endif // CM_NAPI_OPEN_DETAIL_DIALOG_H 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 3d24277ee1280e49db1d1cac3afe8ea5f57ce27f..a661b5a42b516d989a278bf13ab01c0d7dae3895 100644 --- a/interfaces/kits/napi/include/dialog/cm_napi_open_dialog.h +++ b/interfaces/kits/napi/include/dialog/cm_napi_open_dialog.h @@ -35,6 +35,8 @@ 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"; +const std::string CERT_MANAGER_SHOW_INSTALL_BUTTON = "showInstallButton"; + constexpr int32_t PARAM0 = 0; constexpr int32_t PARAM1 = 1; constexpr int32_t PARAM2 = 2; @@ -44,8 +46,6 @@ constexpr int32_t PARAM_SIZE_THREE = 3; constexpr int32_t PARAM_SIZE_FOUR = 4; napi_value CMNapiOpenCertManagerDialog(napi_env env, napi_callback_info info); -napi_value CMNapiOpenInstallCertDialog(napi_env env, napi_callback_info info); -napi_value CMNapiOpenUninstallCertDialog(napi_env env, napi_callback_info info); struct CommonAsyncContext { explicit CommonAsyncContext(napi_env env); @@ -55,6 +55,7 @@ struct CommonAsyncContext { int32_t errCode = 0; napi_deferred deferred = nullptr; // promise handle std::string uri = ""; + int32_t opType = 0; }; struct CmUIExtensionRequestContext : public CommonAsyncContext { @@ -66,6 +67,7 @@ struct CmUIExtensionRequestContext : public CommonAsyncContext { std::string certStr = ""; std::string labelName = ""; CmBlob *certUri = nullptr; + bool showInstallButton = false; }; class CmUIExtensionCallback { diff --git a/interfaces/kits/napi/include/dialog/cm_napi_open_install_dialog.h b/interfaces/kits/napi/include/dialog/cm_napi_open_install_dialog.h new file mode 100644 index 0000000000000000000000000000000000000000..b68f128c23c7bc09f54ad2e39bf445c8e96c8f34 --- /dev/null +++ b/interfaces/kits/napi/include/dialog/cm_napi_open_install_dialog.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 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. + */ + +#ifndef CM_NAPI_OPEN_INSTALL_DIALOG_H +#define CM_NAPI_OPEN_INSTALL_DIALOG_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace CMNapi { +napi_value CMNapiOpenInstallCertDialog(napi_env env, napi_callback_info info); +} // namespace CMNapi + +#endif // CM_NAPI_OPEN_INSTALL_DIALOG_H diff --git a/interfaces/kits/napi/include/dialog/cm_napi_open_uninstall_dialog.h b/interfaces/kits/napi/include/dialog/cm_napi_open_uninstall_dialog.h new file mode 100644 index 0000000000000000000000000000000000000000..b59a3a6927575686fa41a6cbfedee58570201521 --- /dev/null +++ b/interfaces/kits/napi/include/dialog/cm_napi_open_uninstall_dialog.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 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. + */ + +#ifndef CM_NAPI_OPEN_UNINSTALL_DIALOG_H +#define CM_NAPI_OPEN_UNINSTALL_DIALOG_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace CMNapi { +napi_value CMNapiOpenUninstallCertDialog(napi_env env, napi_callback_info info); +} // namespace CMNapi + +#endif // CM_NAPI_OPEN_UNINSTALL_DIALOG_H diff --git a/interfaces/kits/napi/src/dialog/cm_napi_dialog.cpp b/interfaces/kits/napi/src/dialog/cm_napi_dialog.cpp index 3b33b94fb78b8faccf6168bf6e08d7992fd46373..e4dc3f598887548a1118df4ebfa6a50478f46a33 100644 --- a/interfaces/kits/napi/src/dialog/cm_napi_dialog.cpp +++ b/interfaces/kits/napi/src/dialog/cm_napi_dialog.cpp @@ -18,7 +18,10 @@ #include "cm_napi_dialog_common.h" +#include "cm_napi_open_detail_dialog.h" #include "cm_napi_open_dialog.h" +#include "cm_napi_open_install_dialog.h" +#include "cm_napi_open_uninstall_dialog.h" namespace CMNapi { inline void AddInt32Property(napi_env env, napi_value object, const char *name, int32_t value) @@ -94,6 +97,7 @@ static napi_value CMDialogNapiRegister(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("openCertificateManagerDialog", CMNapiOpenCertManagerDialog), DECLARE_NAPI_FUNCTION("openInstallCertificateDialog", CMNapiOpenInstallCertDialog), DECLARE_NAPI_FUNCTION("openUninstallCertificateDialog", CMNapiOpenUninstallCertDialog), + DECLARE_NAPI_FUNCTION("openCertificateDetailDialog", CMNapiOpenDetailDialog), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); return exports; diff --git a/interfaces/kits/napi/src/dialog/cm_napi_open_certificate_operation.cpp b/interfaces/kits/napi/src/dialog/cm_napi_open_certificate_operation.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3163d4b370398f5f32b611beba64a4ffca43e0d7 --- /dev/null +++ b/interfaces/kits/napi/src/dialog/cm_napi_open_certificate_operation.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2025 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_certificate_operation.h" + +#include "cm_napi_dialog_common.h" +#include "cm_log.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +namespace CMNapi { +CmOperationUIExtensionCallback::CmOperationUIExtensionCallback( + std::shared_ptr& reqContext) : CmUIExtensionCallback(reqContext) +{ + this->reqContext_ = reqContext; +} + +CmOperationUIExtensionCallback::~CmOperationUIExtensionCallback() +{ + CM_LOG_D("~CmOperationUIExtensionCallback"); +} + +void CmOperationUIExtensionCallback::OnRelease(const int32_t releaseCode) +{ + CM_LOG_D("OperationUIExtensionComponent OnRelease(), releaseCode = %d", releaseCode); + if (SetErrorCode(CMR_DIALOG_ERROR_OPERATION_CANCELS)) { + SendMessageBack(); + } +} + +void CmOperationUIExtensionCallback::OnResult(const int32_t resultCode, const OHOS::AAFwk::Want& result) +{ + CM_LOG_D("OperationUIExtensionComponent OnResult(), resultCode = %d", resultCode); + this->resultCode_ = resultCode; + this->resultWant_ = result; + if (SetErrorCode(this->resultCode_)) { + SendMessageBack(); + } +} + +void CmOperationUIExtensionCallback::OnReceive(const OHOS::AAFwk::WantParams& request) +{ + CM_LOG_D("OperationUIExtensionComponent OnReceive()"); + this->reqContext_->uri = request.GetStringParam("uri"); + if (SetErrorCode(0)) { + SendMessageBack(); + } +} + +void CmOperationUIExtensionCallback::ProcessCallback(napi_env env, const CommonAsyncContext* asyncContext) +{ + napi_value args = nullptr; + if (asyncContext->errCode == CM_SUCCESS) { + if (asyncContext->opType == DIALOG_OPERATION_INSTALL) { + NAPI_CALL_RETURN_VOID(env, + napi_create_string_utf8(env, asyncContext->uri.c_str(), NAPI_AUTO_LENGTH, &args)); + } else { + 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 CmOperationUIExtensionCallback::OnDestroy() +{ + CM_LOG_D("OperationUIExtensionComponent OnDestroy()"); +} + + +bool CmOperationUIExtensionCallback::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; +} +} // namespace CMNapi diff --git a/interfaces/kits/napi/src/dialog/cm_napi_open_detail_dialog.cpp b/interfaces/kits/napi/src/dialog/cm_napi_open_detail_dialog.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47499a1ed0d67f2ba7574f031e7728a8b1123558 --- /dev/null +++ b/interfaces/kits/napi/src/dialog/cm_napi_open_detail_dialog.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2025 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_uninstall_dialog.h" + +#include "cm_log.h" +#include "cm_napi_dialog_common.h" +#include "cm_napi_open_certificate_operation.h" + +#include "securec.h" +#include "syspara/parameters.h" +#include "want.h" +#include "want_params_wrapper.h" + +namespace CMNapi { +static int32_t GetInstallButtonState(napi_env env, napi_value arg, bool &showInstallButton) +{ + napi_value value = nullptr; + napi_status status = napi_get_named_property(env, arg, CERT_MANAGER_SHOW_INSTALL_BUTTON.c_str(), &value); + if (status != napi_ok) { + CM_LOG_E("Failed to get showInstallBotton"); + return CM_FAILURE; + } + + napi_value result = ParseBoolean(env, value, showInstallButton); + if (result == nullptr) { + CM_LOG_E("Failed to get showInstallBotton value"); + return CM_FAILURE; + } + + return CM_SUCCESS; +} + +static int32_t CheckDetailParamsAndInitContext(std::shared_ptr asyncContext, + napi_value argv[], size_t length) +{ + /* parse context */ + bool parseRet = ParseCmUIAbilityContextReq(asyncContext->env, argv[PARAM0], asyncContext->context); + if (!parseRet) { + CM_LOG_E("ParseUIAbilityContextReq failed"); + return CM_FAILURE; + } + + /* parse cert */ + napi_value value = GetUint8ArrayToBase64Str(asyncContext->env, argv[PARAM1], asyncContext->certStr); + if (value == nullptr) { + CM_LOG_E("cert is not a uint8Array or the length is 0 or too long."); + return CM_FAILURE; + } + + /* parse property */ + int32_t ret = GetInstallButtonState(asyncContext->env, argv[PARAM2], asyncContext->showInstallButton); + if (ret != CM_SUCCESS) { + CM_LOG_E("get property showInstallBotton failed"); + return CM_FAILURE; + } + + return CM_SUCCESS; +} + +static OHOS::AAFwk::Want CMGetCertDetailWant(std::shared_ptr asyncContext) +{ + OHOS::AAFwk::Want want; + want.SetElementName(CERT_MANAGER_BUNDLENAME, CERT_MANAGER_ABILITYNAME); + want.SetParam(PARAM_UI_EXTENSION_TYPE, SYS_COMMON_UI); + want.SetParam(CERT_MANAGER_CERTIFICATE_DATA, asyncContext->certStr); + want.SetParam(CERT_MANAGER_OPERATION_TYPE, asyncContext->opType); + want.SetParam(CERT_MANAGER_SHOW_INSTALL_BUTTON, asyncContext->showInstallButton); + want.SetParam(CERT_MANAGER_PAGE_TYPE, static_cast(CmDialogPageType::PAGE_INSTALL_CA_GUIDE)); + return want; +} + +napi_value CMNapiOpenDetailDialog(napi_env env, napi_callback_info info) +{ + CM_LOG_D("cert open detail dialog enter"); + if (OHOS::system::GetParameter("const.product.devicetype", "") != "2in1") { + CM_LOG_E("deviceType is not 2in1"); + ThrowError(env, DIALOG_ERROR_NOT_SUPPORTED, "DeviceType Error. deviceType is not 2in1"); + return nullptr; + } + + size_t argc = PARAM_SIZE_THREE; + napi_value argv[PARAM_SIZE_THREE] = { nullptr }; + napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); + if (status != napi_ok || argc != PARAM_SIZE_THREE) { + CM_LOG_E("params number mismatch"); + ThrowError(env, PARAM_ERROR, "Parameter Error. Params number mismatch."); + return nullptr; + } + + auto asyncContext = std::make_shared(env); + asyncContext->env = env; + asyncContext->opType = static_cast(DIALOG_OPERATION_DETAIL); + + int32_t ret = CheckDetailParamsAndInitContext(asyncContext, argv, argc); + if (ret != CM_SUCCESS) { + CM_LOG_E("failed to check params and init."); + ThrowError(env, PARAM_ERROR, "failed to check params and init."); + return nullptr; + } + + napi_value result = nullptr; + status = napi_create_promise(env, &asyncContext->deferred, &result); + if (status != napi_ok) { + CM_LOG_E("failed to create promise."); + ThrowError(env, DIALOG_ERROR_GENERIC, "failed to create promise."); + return nullptr; + } + + auto uiExtCallback = std::make_shared(asyncContext); + StartUIExtensionAbility(asyncContext, CMGetCertDetailWant(asyncContext), uiExtCallback); + CM_LOG_D("cert open detail dialog end"); + return result; +} +} // namespace CMNapi diff --git a/interfaces/kits/napi/src/dialog/cm_napi_open_install_dialog.cpp b/interfaces/kits/napi/src/dialog/cm_napi_open_install_dialog.cpp index 59a6d20be6baa17e934e29c260862d2971959ccb..3e424285eee4da0bf549a6156694136ce45bb917 100644 --- a/interfaces/kits/napi/src/dialog/cm_napi_open_install_dialog.cpp +++ b/interfaces/kits/napi/src/dialog/cm_napi_open_install_dialog.cpp @@ -13,108 +13,18 @@ * limitations under the License. */ -#include "cm_napi_open_dialog.h" +#include "cm_napi_open_install_dialog.h" -#include "syspara/parameters.h" -#include "securec.h" - -#include "cert_manager_api.h" #include "cm_log.h" -#include "iservice_registry.h" -#include "bundle_mgr_proxy.h" -#include "system_ability_definition.h" - #include "cm_napi_dialog_common.h" +#include "cm_napi_open_certificate_operation.h" + +#include "securec.h" +#include "syspara/parameters.h" #include "want.h" #include "want_params_wrapper.h" namespace CMNapi { - -class CmInstallUIExtensionCallback : public CmUIExtensionCallback { -public: - explicit CmInstallUIExtensionCallback( - std::shared_ptr& reqContext) : CmUIExtensionCallback(reqContext) - { - this->reqContext_ = reqContext; - } - - ~CmInstallUIExtensionCallback() override - { - CM_LOG_D("~CmInstallUIExtensionCallback"); - } - - void OnRelease(const int32_t releaseCode) override - { - CM_LOG_D("InstallUIExtensionComponent 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("InstallUIExtensionComponent 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("InstallUIExtensionComponent OnReceive()"); - this->reqContext_->uri = request.GetStringParam("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_create_string_utf8(env, asyncContext->uri.c_str(), NAPI_AUTO_LENGTH, &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("InstallUIExtensionComponent 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 IsCmCertificateScopeEnum(const uint32_t value) { return value >= NOT_SPECIFIED && value <= GLOBAL_USER; @@ -182,7 +92,7 @@ static OHOS::AAFwk::Want CMGetInstallCertWant(std::shared_ptr(asyncContext->certificateScope)); want.SetParam(CERT_MANAGER_CALLER_BUNDLENAME, asyncContext->labelName); want.SetParam(PARAM_UI_EXTENSION_TYPE, SYS_COMMON_UI); - want.SetParam(CERT_MANAGER_OPERATION_TYPE, static_cast(DIALOG_OPERATION_INSTALL)); + want.SetParam(CERT_MANAGER_OPERATION_TYPE, asyncContext->opType); return want; } @@ -211,6 +121,7 @@ napi_value CMNapiOpenInstallCertDialog(napi_env env, napi_callback_info info) auto asyncContext = std::make_shared(env); asyncContext->env = env; + asyncContext->opType = static_cast(DIALOG_OPERATION_INSTALL); if (CMCheckArgvAndInitContext(asyncContext, argv, sizeof(argv) / sizeof(argv[0])) == nullptr) { CM_LOG_E("check argv vaild and init faild"); ThrowError(env, PARAM_ERROR, "check argv vaild and init faild"); @@ -224,7 +135,7 @@ napi_value CMNapiOpenInstallCertDialog(napi_env env, napi_callback_info info) } NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result)); - auto uiExtCallback = std::make_shared(asyncContext); + auto uiExtCallback = std::make_shared(asyncContext); StartUIExtensionAbility(asyncContext, CMGetInstallCertWant(asyncContext), uiExtCallback); CM_LOG_D("cert install dialog end"); return result; 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 index 52e832d883ccec99b679969db431c9e06718d018..59f888cd0a9c1e2f54e4e16ffb7e9987c0c077bb 100755 --- a/interfaces/kits/napi/src/dialog/cm_napi_open_uninstall_dialog.cpp +++ b/interfaces/kits/napi/src/dialog/cm_napi_open_uninstall_dialog.cpp @@ -12,101 +12,19 @@ * 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_napi_open_uninstall_dialog.h" #include "cm_log.h" +#include "cm_napi_dialog_common.h" +#include "cm_napi_open_certificate_operation.h" -#include "syspara/parameters.h" #include "securec.h" +#include "syspara/parameters.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()"); - 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)) { @@ -157,7 +75,7 @@ static OHOS::AAFwk::Want CMGetUninstallCertWant(std::shared_ptr(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)); + want.SetParam(CERT_MANAGER_OPERATION_TYPE, asyncContext->opType); return want; } @@ -185,10 +103,11 @@ napi_value CMNapiOpenUninstallCertDialog(napi_env env, napi_callback_info info) ThrowError(env, PARAM_ERROR, errMsg); return result; } - + //parse and init context auto asyncContext = std::make_shared(env); asyncContext->env = env; + asyncContext->opType = static_cast(DIALOG_OPERATION_UNINSTALL); if (CMInitAsyncContext(asyncContext, argv, argc) == nullptr) { CM_LOG_E("Parse param and init asyncContext failed"); ThrowError(env, PARAM_ERROR, "Parse param and init asyncContext failed"); @@ -204,7 +123,7 @@ napi_value CMNapiOpenUninstallCertDialog(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result)); //set want params - auto uiExtCallback = std::make_shared(asyncContext); + auto uiExtCallback = std::make_shared(asyncContext); StartUIExtensionAbility(asyncContext, CMGetUninstallCertWant(asyncContext), uiExtCallback); CM_LOG_D("cert install dialog end"); return result;