From 1bbb69808f8c1571cdb1a12d9761d2bc57203ac3 Mon Sep 17 00:00:00 2001 From: wangtiantian Date: Tue, 18 Apr 2023 14:39:11 +0800 Subject: [PATCH 1/3] IssueNo:#I6WNV9 Description:additionalInfo napi Sig:SIG_ApplicaitonFramework Feature or Bugfix:Feature Binary Source:No Signed-off-by: wangtiantian --- interfaces/kits/js/installer/installer.cpp | 212 ++++++++++++++++++ interfaces/kits/js/installer/installer.h | 13 ++ .../kits/js/installer/native_module.cpp | 2 + 3 files changed, 227 insertions(+) diff --git a/interfaces/kits/js/installer/installer.cpp b/interfaces/kits/js/installer/installer.cpp index a8e40ac2a..1742af63c 100644 --- a/interfaces/kits/js/installer/installer.cpp +++ b/interfaces/kits/js/installer/installer.cpp @@ -39,6 +39,8 @@ const std::string RESOURCE_NAME_OF_GET_BUNDLE_INSTALLER = "GetBundleInstaller"; const std::string RESOURCE_NAME_OF_INSTALL = "Install"; const std::string RESOURCE_NAME_OF_UNINSTALL = "Uninstall"; const std::string RESOURCE_NAME_OF_RECOVER = "Recover"; +const std::string RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE = "GetSpecifiedDistributionType"; +const std::string RESOURCE_NAME_OF_GET_ADDITIONAL_INFO = "GetAdditionalInfo"; const std::string EMPTY_STRING = ""; // install message constexpr const char* INSTALL_PERMISSION = "ohos.permission.INSTALL_BUNDLE"; @@ -1030,5 +1032,215 @@ napi_value BundleInstallerConstructor(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr)); return jsthis; } + +ErrCode InnerGetSpecifiedDistributionType( + const std::string &bundleName, const std::string &specifiedDistributionType) +{ + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("iBundleMgr is null"); + return ERROR_BUNDLE_SERVICE_EXCEPTION; + } + ErrCode ret = iBundleMgr->GetSpecifiedDistributionType(bundleName, specifiedDistributionType); + return CommonFunc::ConvertErrCode(ret); +} + +void GetSpecifiedDistributionTypeExec(napi_env env, void *data) +{ + AsyncGetSpecifiedDistributionTypeCallbackInfo *asyncCallbackInfo = + reinterpret_cast(data); + if (asyncCallbackInfo == nullptr) { + APP_LOGE("asyncCallbackInfo is null in %{public}s", __func__); + return; + } + if (asyncCallbackInfo->err == NO_ERROR) { + asyncCallbackInfo->err = InnerGetSpecifiedDistributionType( + asyncCallbackInfo->bundleName, asyncCallbackInfo->specifiedDistributionType); + } +} + +void GetSpecifiedDistributionTypeComplete(napi_env env, napi_status status, void *data) +{ + AsyncGetSpecifiedDistributionTypeCallbackInfo *asyncCallbackInfo = + reinterpret_cast(data); + if (asyncCallbackInfo == nullptr) { + APP_LOGE("asyncCallbackInfo is null in %{public}s", __func__); + return; + } + std::unique_ptr callbackPtr {asyncCallbackInfo}; + napi_value result[CALLBACK_PARAM_SIZE] = {0}; + if (asyncCallbackInfo->err == NO_ERROR) { + NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[ARGS_POS_ZERO])); + NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, + asyncCallbackInfo->specifiedDistributionTypeInfo.c_str(), + NAPI_AUTO_LENGTH, &result[ARGS_POS_ONE])); + } else { + result[ARGS_POS_ZERO] = BusinessError::CreateCommonError(env, asyncCallbackInfo->err, + RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); + } + if (asyncCallbackInfo->deferred) { + if (asyncCallbackInfo->err == NO_ERROR) { + NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[ARGS_POS_ONE])); + } else { + NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, asyncCallbackInfo->deferred, result[ARGS_POS_ZERO])); + } + } else { + napi_value callback = nullptr; + napi_value placeHolder = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, asyncCallbackInfo->callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, nullptr, callback, + sizeof(result) / sizeof(result[ARGS_POS_ZERO]), result, &placeHolder)); + } +} + +napi_value GetSpecifiedDistributionType(napi_env env, napi_callback_info info) +{ + APP_LOGD("GetSpecifiedDistributionType napi called"); + NapiArg args(env, info); + AsyncGetSpecifiedDistributionTypeCallbackInfo *asyncCallbackInfo = + new (std::nothrow) AsyncGetSpecifiedDistributionTypeCallbackInfo(env); + if (asyncCallbackInfo == nullptr) { + APP_LOGE("asyncCallbackInfo is null."); + return nullptr; + } + std::unique_ptr callbackPtr {asyncCallbackInfo}; + if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) { + APP_LOGE("param count invalid."); + BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR); + return nullptr; + } + for (size_t i = 0; i < args.GetArgc(); ++i) { + napi_valuetype valueType = napi_undefined; + napi_typeof(env, args[i], &valueType); + if (i == ARGS_POS_ZERO) { + if (!CommonFunc::ParseString(env, args[i], asyncCallbackInfo->bundleName)) { + APP_LOGE("bundleName invalid!"); + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING); + return nullptr; + } + } else if (i == ARGS_POS_ONE) { + if (valueType == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback)); + break; + } + } else { + APP_LOGE("param check error"); + BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR); + return nullptr; + } + } + + auto promise = CommonFunc::AsyncCallNativeMethod( + env, asyncCallbackInfo, RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE, + GetSpecifiedDistributionTypeExec, GetSpecifiedDistributionTypeComplete); + callbackPtr.release(); + APP_LOGD("call GetSpecifiedDistributionType done."); + return promise; +} + +ErrCode InnerGetAdditionalInfo( + const std::string &bundleName, const std::string &additionalInfo) +{ + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("iBundleMgr is null"); + return ERROR_BUNDLE_SERVICE_EXCEPTION; + } + ErrCode ret = iBundleMgr->GetAdditionalInfo(bundleName, additionalInfo); + return CommonFunc::ConvertErrCode(ret); +} + +void GetAdditionalInfoExec(napi_env env, void *data) +{ + AsyncGetAdditionalInfoCallbackInfo *asyncCallbackInfo = + reinterpret_cast(data); + if (asyncCallbackInfo == nullptr) { + APP_LOGE("asyncCallbackInfo is null in %{public}s", __func__); + return; + } + if (asyncCallbackInfo->err == NO_ERROR) { + asyncCallbackInfo->err = InnerGetAdditionalInfo( + asyncCallbackInfo->bundleName, asyncCallbackInfo->additionalInfo); + } +} + +void GetAdditionalInfoComplete(napi_env env, napi_status status, void *data) +{ + AsyncGetAdditionalInfoCallbackInfo *asyncCallbackInfo = + reinterpret_cast(data); + if (asyncCallbackInfo == nullptr) { + APP_LOGE("asyncCallbackInfo is null in %{public}s", __func__); + return; + } + std::unique_ptr callbackPtr {asyncCallbackInfo}; + napi_value result[CALLBACK_PARAM_SIZE] = {0}; + if (asyncCallbackInfo->err == NO_ERROR) { + NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[ARGS_POS_ZERO])); + NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, + asyncCallbackInfo->additionalInfo.c_str(), + NAPI_AUTO_LENGTH, &result[ARGS_POS_ONE])); + } else { + result[ARGS_POS_ZERO] = BusinessError::CreateCommonError(env, asyncCallbackInfo->err, + RESOURCE_NAME_OF_GET_ADDITIONAL_INFO, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); + } + if (asyncCallbackInfo->deferred) { + if (asyncCallbackInfo->err == NO_ERROR) { + NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[ARGS_POS_ONE])); + } else { + NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, asyncCallbackInfo->deferred, result[ARGS_POS_ZERO])); + } + } else { + napi_value callback = nullptr; + napi_value placeHolder = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, asyncCallbackInfo->callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, nullptr, callback, + sizeof(result) / sizeof(result[ARGS_POS_ZERO]), result, &placeHolder)); + } +} + +napi_value GetAdditionalInfo(napi_env env, napi_callback_info info) +{ + APP_LOGD("GetAdditionalInfo napi called"); + NapiArg args(env, info); + AsyncGetAdditionalInfoCallbackInfo *asyncCallbackInfo = + new (std::nothrow) AsyncGetAdditionalInfoCallbackInfo(env); + if (asyncCallbackInfo == nullptr) { + APP_LOGE("asyncCallbackInfo is null."); + return nullptr; + } + std::unique_ptr callbackPtr {asyncCallbackInfo}; + if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) { + APP_LOGE("param count invalid."); + BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR); + return nullptr; + } + for (size_t i = 0; i < args.GetArgc(); ++i) { + napi_valuetype valueType = napi_undefined; + napi_typeof(env, args[i], &valueType); + if (i == ARGS_POS_ZERO) { + if (!CommonFunc::ParseString(env, args[i], asyncCallbackInfo->bundleName)) { + APP_LOGE("bundleName invalid!"); + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING); + return nullptr; + } + } else if (i == ARGS_POS_ONE) { + if (valueType == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback)); + break; + } + } else { + APP_LOGE("param check error"); + BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR); + return nullptr; + } + } + + auto promise = CommonFunc::AsyncCallNativeMethod( + env, asyncCallbackInfo, RESOURCE_NAME_OF_GET_ADDITIONAL_INFO, + GetAdditionalInfoExec, GetAdditionalInfoComplete); + callbackPtr.release(); + APP_LOGD("call GetAdditionalInfo done."); + return promise; +} } // AppExecFwk } // OHOS \ No newline at end of file diff --git a/interfaces/kits/js/installer/installer.h b/interfaces/kits/js/installer/installer.h index fb78c8356..ad2760b40 100755 --- a/interfaces/kits/js/installer/installer.h +++ b/interfaces/kits/js/installer/installer.h @@ -65,12 +65,25 @@ struct AsyncGetBundleInstallerCallbackInfo { napi_deferred deferred = nullptr; napi_ref callback = nullptr; }; +struct AsyncGetSpecifiedDistributionTypeCallbackInfo : public BaseCallbackInfo { + explicit SpecifiedDistributionTypeCallbackInfo(napi_env env) : BaseCallbackInfo(env) {} + std::string bundleName; + std::string specifiedDistributionTypeInfo; +}; + +struct AsyncGetAdditionalInfoCallbackInfo : public BaseCallbackInfo { + explicit AdditionalInfoCallbackInfo(napi_env env) : BaseCallbackInfo(env) {} + std::string bundleName; + std::string additionalInfo; +}; napi_value GetBundleInstaller(napi_env env, napi_callback_info info); napi_value Install(napi_env env, napi_callback_info info); napi_value Recover(napi_env env, napi_callback_info info); napi_value Uninstall(napi_env env, napi_callback_info info); napi_value BundleInstallerConstructor(napi_env env, napi_callback_info info); +napi_value GetSpecifiedDistributionType(napi_env env, napi_callback_info info); +napi_value GetAdditionalInfo(napi_env env, napi_callback_info info); } // AppExecFwk } // OHOS #endif // FOUNDATION_BUNDLEMGR_SERVICES_KITS_INCLUDE_INSTALLER_H \ No newline at end of file diff --git a/interfaces/kits/js/installer/native_module.cpp b/interfaces/kits/js/installer/native_module.cpp index aa6e1b6de..c3a789236 100755 --- a/interfaces/kits/js/installer/native_module.cpp +++ b/interfaces/kits/js/installer/native_module.cpp @@ -30,6 +30,8 @@ static napi_value BundleInstallerExport(napi_env env, napi_value exports) napi_property_descriptor desc[] = { DECLARE_NAPI_FUNCTION("getBundleInstaller", GetBundleInstaller), + DECLARE_NAPI_FUNCTION("getSpecifiedDistributionType", GetSpecifiedDistributionType), + DECLARE_NAPI_FUNCTION("getAdditionalInfo", GetAdditionalInfo), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); -- Gitee From 72df185764b708d50b31cbf21710b9989b59c392 Mon Sep 17 00:00:00 2001 From: wangtiantian Date: Wed, 19 Apr 2023 11:38:25 +0800 Subject: [PATCH 2/3] IssueNo:#I6WNV9 Description:additionalInfo napi sync Sig:SIG_ApplicaitonFramework Feature or Bugfix:Feature Binary Source:No Signed-off-by: wangtiantian --- interfaces/kits/js/installer/installer.cpp | 243 +++++------------- interfaces/kits/js/installer/installer.h | 15 +- .../kits/js/installer/native_module.cpp | 4 +- 3 files changed, 63 insertions(+), 199 deletions(-) diff --git a/interfaces/kits/js/installer/installer.cpp b/interfaces/kits/js/installer/installer.cpp index 1742af63c..20dba45d4 100644 --- a/interfaces/kits/js/installer/installer.cpp +++ b/interfaces/kits/js/installer/installer.cpp @@ -39,8 +39,8 @@ const std::string RESOURCE_NAME_OF_GET_BUNDLE_INSTALLER = "GetBundleInstaller"; const std::string RESOURCE_NAME_OF_INSTALL = "Install"; const std::string RESOURCE_NAME_OF_UNINSTALL = "Uninstall"; const std::string RESOURCE_NAME_OF_RECOVER = "Recover"; -const std::string RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE = "GetSpecifiedDistributionType"; -const std::string RESOURCE_NAME_OF_GET_ADDITIONAL_INFO = "GetAdditionalInfo"; +const std::string RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE = "GetSpecifiedDistributionTypeSync"; +const std::string RESOURCE_NAME_OF_GET_ADDITIONAL_INFO = "GetAdditionalInfoSync"; const std::string EMPTY_STRING = ""; // install message constexpr const char* INSTALL_PERMISSION = "ohos.permission.INSTALL_BUNDLE"; @@ -1033,214 +1033,89 @@ napi_value BundleInstallerConstructor(napi_env env, napi_callback_info info) return jsthis; } -ErrCode InnerGetSpecifiedDistributionType( - const std::string &bundleName, const std::string &specifiedDistributionType) +napi_value GetSpecifiedDistributionTypeSync(napi_env env, napi_callback_info info) { + APP_LOGD("GetSpecifiedDistributionTypeSync napi called"); + NapiArg args(env, info); + if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_ONE)) { + APP_LOGE("param count invalid."); + BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR); + return nullptr; + } + + std::string bundleName; + if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], bundleName)) { + APP_LOGE("bundleName invalid!"); + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING); + return nullptr; + } + auto iBundleMgr = CommonFunc::GetBundleMgr(); if (iBundleMgr == nullptr) { APP_LOGE("iBundleMgr is null"); - return ERROR_BUNDLE_SERVICE_EXCEPTION; + napi_value businessError = BusinessError::CreateCommonError( + env, ERROR_BUNDLE_SERVICE_EXCEPTION, RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE, NEED_PERMISSIONS); + napi_throw(env, businessError); + return nullptr; } - ErrCode ret = iBundleMgr->GetSpecifiedDistributionType(bundleName, specifiedDistributionType); - return CommonFunc::ConvertErrCode(ret); -} -void GetSpecifiedDistributionTypeExec(napi_env env, void *data) -{ - AsyncGetSpecifiedDistributionTypeCallbackInfo *asyncCallbackInfo = - reinterpret_cast(data); - if (asyncCallbackInfo == nullptr) { - APP_LOGE("asyncCallbackInfo is null in %{public}s", __func__); - return; - } - if (asyncCallbackInfo->err == NO_ERROR) { - asyncCallbackInfo->err = InnerGetSpecifiedDistributionType( - asyncCallbackInfo->bundleName, asyncCallbackInfo->specifiedDistributionType); + std::string specifiedDistributionType; + ErrCode ret = CommonFunc::ConvertErrCode( + iBundleMgr->GetSpecifiedDistributionType(bundleName, specifiedDistributionType)); + if (ret != SUCCESS) { + napi_value businessError = BusinessError::CreateCommonError( + env, ret, RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE, + Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); + napi_throw(env, businessError); + return nullptr; } -} -void GetSpecifiedDistributionTypeComplete(napi_env env, napi_status status, void *data) -{ - AsyncGetSpecifiedDistributionTypeCallbackInfo *asyncCallbackInfo = - reinterpret_cast(data); - if (asyncCallbackInfo == nullptr) { - APP_LOGE("asyncCallbackInfo is null in %{public}s", __func__); - return; - } - std::unique_ptr callbackPtr {asyncCallbackInfo}; - napi_value result[CALLBACK_PARAM_SIZE] = {0}; - if (asyncCallbackInfo->err == NO_ERROR) { - NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[ARGS_POS_ZERO])); - NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, - asyncCallbackInfo->specifiedDistributionTypeInfo.c_str(), - NAPI_AUTO_LENGTH, &result[ARGS_POS_ONE])); - } else { - result[ARGS_POS_ZERO] = BusinessError::CreateCommonError(env, asyncCallbackInfo->err, - RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); - } - if (asyncCallbackInfo->deferred) { - if (asyncCallbackInfo->err == NO_ERROR) { - NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[ARGS_POS_ONE])); - } else { - NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, asyncCallbackInfo->deferred, result[ARGS_POS_ZERO])); - } - } else { - napi_value callback = nullptr; - napi_value placeHolder = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, asyncCallbackInfo->callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, nullptr, callback, - sizeof(result) / sizeof(result[ARGS_POS_ZERO]), result, &placeHolder)); - } + napi_value nSpecifiedDistributionType; + napi_create_string_utf8(env, specifiedDistributionType.c_str(), NAPI_AUTO_LENGTH, &nSpecifiedDistributionType); + APP_LOGD("call GetSpecifiedDistributionTypeSync done."); + return nSpecifiedDistributionType; } -napi_value GetSpecifiedDistributionType(napi_env env, napi_callback_info info) +napi_value GetAdditionalInfoSync(napi_env env, napi_callback_info info) { - APP_LOGD("GetSpecifiedDistributionType napi called"); + APP_LOGD("GetAdditionalInfoSync napi called"); NapiArg args(env, info); - AsyncGetSpecifiedDistributionTypeCallbackInfo *asyncCallbackInfo = - new (std::nothrow) AsyncGetSpecifiedDistributionTypeCallbackInfo(env); - if (asyncCallbackInfo == nullptr) { - APP_LOGE("asyncCallbackInfo is null."); - return nullptr; - } - std::unique_ptr callbackPtr {asyncCallbackInfo}; - if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) { + if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_ONE)) { APP_LOGE("param count invalid."); BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR); return nullptr; } - for (size_t i = 0; i < args.GetArgc(); ++i) { - napi_valuetype valueType = napi_undefined; - napi_typeof(env, args[i], &valueType); - if (i == ARGS_POS_ZERO) { - if (!CommonFunc::ParseString(env, args[i], asyncCallbackInfo->bundleName)) { - APP_LOGE("bundleName invalid!"); - BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING); - return nullptr; - } - } else if (i == ARGS_POS_ONE) { - if (valueType == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback)); - break; - } - } else { - APP_LOGE("param check error"); - BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR); - return nullptr; - } - } - auto promise = CommonFunc::AsyncCallNativeMethod( - env, asyncCallbackInfo, RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE, - GetSpecifiedDistributionTypeExec, GetSpecifiedDistributionTypeComplete); - callbackPtr.release(); - APP_LOGD("call GetSpecifiedDistributionType done."); - return promise; -} + std::string bundleName; + if (!CommonFunc::ParseString(env, args[ARGS_POS_ZERO], bundleName)) { + APP_LOGE("bundleName invalid!"); + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING); + return nullptr; + } -ErrCode InnerGetAdditionalInfo( - const std::string &bundleName, const std::string &additionalInfo) -{ auto iBundleMgr = CommonFunc::GetBundleMgr(); if (iBundleMgr == nullptr) { APP_LOGE("iBundleMgr is null"); - return ERROR_BUNDLE_SERVICE_EXCEPTION; - } - ErrCode ret = iBundleMgr->GetAdditionalInfo(bundleName, additionalInfo); - return CommonFunc::ConvertErrCode(ret); -} - -void GetAdditionalInfoExec(napi_env env, void *data) -{ - AsyncGetAdditionalInfoCallbackInfo *asyncCallbackInfo = - reinterpret_cast(data); - if (asyncCallbackInfo == nullptr) { - APP_LOGE("asyncCallbackInfo is null in %{public}s", __func__); - return; - } - if (asyncCallbackInfo->err == NO_ERROR) { - asyncCallbackInfo->err = InnerGetAdditionalInfo( - asyncCallbackInfo->bundleName, asyncCallbackInfo->additionalInfo); - } -} - -void GetAdditionalInfoComplete(napi_env env, napi_status status, void *data) -{ - AsyncGetAdditionalInfoCallbackInfo *asyncCallbackInfo = - reinterpret_cast(data); - if (asyncCallbackInfo == nullptr) { - APP_LOGE("asyncCallbackInfo is null in %{public}s", __func__); - return; - } - std::unique_ptr callbackPtr {asyncCallbackInfo}; - napi_value result[CALLBACK_PARAM_SIZE] = {0}; - if (asyncCallbackInfo->err == NO_ERROR) { - NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[ARGS_POS_ZERO])); - NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, - asyncCallbackInfo->additionalInfo.c_str(), - NAPI_AUTO_LENGTH, &result[ARGS_POS_ONE])); - } else { - result[ARGS_POS_ZERO] = BusinessError::CreateCommonError(env, asyncCallbackInfo->err, - RESOURCE_NAME_OF_GET_ADDITIONAL_INFO, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); - } - if (asyncCallbackInfo->deferred) { - if (asyncCallbackInfo->err == NO_ERROR) { - NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[ARGS_POS_ONE])); - } else { - NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, asyncCallbackInfo->deferred, result[ARGS_POS_ZERO])); - } - } else { - napi_value callback = nullptr; - napi_value placeHolder = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, asyncCallbackInfo->callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, nullptr, callback, - sizeof(result) / sizeof(result[ARGS_POS_ZERO]), result, &placeHolder)); - } -} - -napi_value GetAdditionalInfo(napi_env env, napi_callback_info info) -{ - APP_LOGD("GetAdditionalInfo napi called"); - NapiArg args(env, info); - AsyncGetAdditionalInfoCallbackInfo *asyncCallbackInfo = - new (std::nothrow) AsyncGetAdditionalInfoCallbackInfo(env); - if (asyncCallbackInfo == nullptr) { - APP_LOGE("asyncCallbackInfo is null."); + napi_value businessError = BusinessError::CreateCommonError( + env, ERROR_BUNDLE_SERVICE_EXCEPTION, RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE, NEED_PERMISSIONS); + napi_throw(env, businessError); return nullptr; } - std::unique_ptr callbackPtr {asyncCallbackInfo}; - if (!args.Init(ARGS_SIZE_ONE, ARGS_SIZE_TWO)) { - APP_LOGE("param count invalid."); - BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR); + + std::string additionalInfo; + ErrCode ret = CommonFunc::ConvertErrCode( + iBundleMgr->GetAdditionalInfo(bundleName, additionalInfo)); + if (ret != SUCCESS) { + napi_value businessError = BusinessError::CreateCommonError( + env, ret, RESOURCE_NAME_OF_GET_ADDITIONAL_INFO, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); + napi_throw(env, businessError); return nullptr; } - for (size_t i = 0; i < args.GetArgc(); ++i) { - napi_valuetype valueType = napi_undefined; - napi_typeof(env, args[i], &valueType); - if (i == ARGS_POS_ZERO) { - if (!CommonFunc::ParseString(env, args[i], asyncCallbackInfo->bundleName)) { - APP_LOGE("bundleName invalid!"); - BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING); - return nullptr; - } - } else if (i == ARGS_POS_ONE) { - if (valueType == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[i], NAPI_RETURN_ONE, &asyncCallbackInfo->callback)); - break; - } - } else { - APP_LOGE("param check error"); - BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR); - return nullptr; - } - } - auto promise = CommonFunc::AsyncCallNativeMethod( - env, asyncCallbackInfo, RESOURCE_NAME_OF_GET_ADDITIONAL_INFO, - GetAdditionalInfoExec, GetAdditionalInfoComplete); - callbackPtr.release(); - APP_LOGD("call GetAdditionalInfo done."); - return promise; + napi_value nAdditionalInfo; + napi_create_string_utf8(env, additionalInfo.c_str(), NAPI_AUTO_LENGTH, &nAdditionalInfo); + APP_LOGD("call GetAdditionalInfoSync done."); + return nAdditionalInfo; } } // AppExecFwk } // OHOS \ No newline at end of file diff --git a/interfaces/kits/js/installer/installer.h b/interfaces/kits/js/installer/installer.h index ad2760b40..439bbbd7f 100755 --- a/interfaces/kits/js/installer/installer.h +++ b/interfaces/kits/js/installer/installer.h @@ -65,25 +65,14 @@ struct AsyncGetBundleInstallerCallbackInfo { napi_deferred deferred = nullptr; napi_ref callback = nullptr; }; -struct AsyncGetSpecifiedDistributionTypeCallbackInfo : public BaseCallbackInfo { - explicit SpecifiedDistributionTypeCallbackInfo(napi_env env) : BaseCallbackInfo(env) {} - std::string bundleName; - std::string specifiedDistributionTypeInfo; -}; - -struct AsyncGetAdditionalInfoCallbackInfo : public BaseCallbackInfo { - explicit AdditionalInfoCallbackInfo(napi_env env) : BaseCallbackInfo(env) {} - std::string bundleName; - std::string additionalInfo; -}; napi_value GetBundleInstaller(napi_env env, napi_callback_info info); napi_value Install(napi_env env, napi_callback_info info); napi_value Recover(napi_env env, napi_callback_info info); napi_value Uninstall(napi_env env, napi_callback_info info); napi_value BundleInstallerConstructor(napi_env env, napi_callback_info info); -napi_value GetSpecifiedDistributionType(napi_env env, napi_callback_info info); -napi_value GetAdditionalInfo(napi_env env, napi_callback_info info); +napi_value GetSpecifiedDistributionTypeSync(napi_env env, napi_callback_info info); +napi_value GetAdditionalInfoSync(napi_env env, napi_callback_info info); } // AppExecFwk } // OHOS #endif // FOUNDATION_BUNDLEMGR_SERVICES_KITS_INCLUDE_INSTALLER_H \ No newline at end of file diff --git a/interfaces/kits/js/installer/native_module.cpp b/interfaces/kits/js/installer/native_module.cpp index c3a789236..0dddd3ec8 100755 --- a/interfaces/kits/js/installer/native_module.cpp +++ b/interfaces/kits/js/installer/native_module.cpp @@ -30,8 +30,8 @@ static napi_value BundleInstallerExport(napi_env env, napi_value exports) napi_property_descriptor desc[] = { DECLARE_NAPI_FUNCTION("getBundleInstaller", GetBundleInstaller), - DECLARE_NAPI_FUNCTION("getSpecifiedDistributionType", GetSpecifiedDistributionType), - DECLARE_NAPI_FUNCTION("getAdditionalInfo", GetAdditionalInfo), + DECLARE_NAPI_FUNCTION("getSpecifiedDistributionTypeSync", GetSpecifiedDistributionTypeSync), + DECLARE_NAPI_FUNCTION("getAdditionalInfoSync", GetAdditionalInfoSync), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); -- Gitee From 8538f065019ef6697fd5ed359a03c6bda65e3b57 Mon Sep 17 00:00:00 2001 From: wangtiantian Date: Wed, 19 Apr 2023 15:04:08 +0800 Subject: [PATCH 3/3] IssueNo:#I6WNV9 Description:additionalInfo napi sync Sig:SIG_ApplicaitonFramework Feature or Bugfix:Feature Binary Source:No Signed-off-by: wangtiantian --- interfaces/kits/js/installer/installer.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/installer/installer.cpp b/interfaces/kits/js/installer/installer.cpp index 20dba45d4..ecb62a75e 100644 --- a/interfaces/kits/js/installer/installer.cpp +++ b/interfaces/kits/js/installer/installer.cpp @@ -1054,7 +1054,8 @@ napi_value GetSpecifiedDistributionTypeSync(napi_env env, napi_callback_info inf if (iBundleMgr == nullptr) { APP_LOGE("iBundleMgr is null"); napi_value businessError = BusinessError::CreateCommonError( - env, ERROR_BUNDLE_SERVICE_EXCEPTION, RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE, NEED_PERMISSIONS); + env, ERROR_BUNDLE_SERVICE_EXCEPTION, RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE, + Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); napi_throw(env, businessError); return nullptr; } @@ -1097,7 +1098,8 @@ napi_value GetAdditionalInfoSync(napi_env env, napi_callback_info info) if (iBundleMgr == nullptr) { APP_LOGE("iBundleMgr is null"); napi_value businessError = BusinessError::CreateCommonError( - env, ERROR_BUNDLE_SERVICE_EXCEPTION, RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE, NEED_PERMISSIONS); + env, ERROR_BUNDLE_SERVICE_EXCEPTION, RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE, + Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); napi_throw(env, businessError); return nullptr; } -- Gitee