From 491dfedbab1ff802b18933c9199445e7b96ece7e Mon Sep 17 00:00:00 2001 From: zhongjianfei Date: Sun, 6 Mar 2022 09:18:43 +0800 Subject: [PATCH] add errorcode for form api Signed-off-by: zhongjianfei Change-Id: I9f10b075c98ff0be1d4862797d2448435bb1c5de --- .../kits/ability/native/include/ability.h | 44 +- .../kits/ability/native/src/ability.cpp | 161 ++- .../kits/fmskit/native/src/form_errors.cpp | 30 +- .../aafwk/formManager/napi_form_manager.cpp | 970 +++++++++++++----- .../aafwk/formManager/napi_form_manager.h | 11 + services/formmgr/include/form_mgr_service.h | 2 +- services/formmgr/src/form_mgr_service.cpp | 66 +- 7 files changed, 891 insertions(+), 393 deletions(-) diff --git a/frameworks/kits/ability/native/include/ability.h b/frameworks/kits/ability/native/include/ability.h index 6ea00909be1..9e66ac7ca1b 100755 --- a/frameworks/kits/ability/native/include/ability.h +++ b/frameworks/kits/ability/native/include/ability.h @@ -26,6 +26,7 @@ #include "ability_lifecycle_executor.h" #include "ability_lifecycle_interface.h" #include "ability_window.h" +#include "appexecfwk_errors.h" #include "configuration.h" #include "context.h" #include "continuation_handler.h" @@ -992,7 +993,7 @@ public: *
  • The form is being restored.
  • * */ - bool ReleaseForm(const int64_t formId); + ErrCode ReleaseForm(const int64_t formId); /** * @brief Releases an obtained form by its ID. @@ -1015,7 +1016,7 @@ public: *
  • The form is being restored.
  • * */ - bool ReleaseForm(const int64_t formId, const bool isReleaseCache); + ErrCode ReleaseForm(const int64_t formId, const bool isReleaseCache); /** * @brief Deletes an obtained form by its ID. @@ -1036,7 +1037,7 @@ public: *
  • The form is being restored.
  • * */ - bool DeleteForm(const int64_t formId); + ErrCode DeleteForm(const int64_t formId); /** * @brief Keep this Service ability in the background and displays a notification bar. @@ -1120,7 +1121,7 @@ public: * @param formProviderData The data used to update the JS form displayed on the client. * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. */ - bool UpdateForm(const int64_t formId, const FormProviderData &formProviderData); + ErrCode UpdateForm(const int64_t formId, const FormProviderData &formProviderData); /** * @brief Cast temp form with formId. @@ -1129,7 +1130,7 @@ public: * * @return Returns {@code true} if the form is successfully casted; returns {@code false} otherwise. */ - bool CastTempForm(const int64_t formId); + ErrCode CastTempForm(const int64_t formId); /** * @brief Sends a notification to the form framework to make the specified forms visible. @@ -1140,7 +1141,7 @@ public: * @param formIds Indicates the IDs of the forms to be made visible. * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. */ - bool NotifyVisibleForms(const std::vector &formIds); + ErrCode NotifyVisibleForms(const std::vector &formIds); /** * @brief Sends a notification to the form framework to make the specified forms invisible. @@ -1151,7 +1152,7 @@ public: * @param formIds Indicates the IDs of the forms to be made invisible. * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. */ - bool NotifyInvisibleForms(const std::vector &formIds); + ErrCode NotifyInvisibleForms(const std::vector &formIds); /** * @brief Set form next refresh time. @@ -1163,7 +1164,7 @@ public: * @return Returns {@code true} if seting succeed; returns {@code false} otherwise. */ - bool SetFormNextRefreshTime(const int64_t formId, const int64_t nextTime); + ErrCode SetFormNextRefreshTime(const int64_t formId, const int64_t nextTime); /** * @brief Update form. @@ -1256,7 +1257,7 @@ public: * @param formId Indicates the ID of the form to update. * @return Returns true if the update request is successfully initiated, returns false otherwise. */ - bool RequestForm(const int64_t formId); + ErrCode RequestForm(const int64_t formId); /** * @brief Requests for form data update, by passing a set of parameters (using Want) to the form provider. @@ -1270,20 +1271,20 @@ public: * @param want Indicates a set of parameters to be transparently passed to the form provider. * @return Returns true if the update request is successfully initiated, returns false otherwise. */ - bool RequestForm(const int64_t formId, const Want &want); + ErrCode RequestForm(const int64_t formId, const Want &want); /** * @brief Enable form update. * * @param formIds formIds of hostclient. */ - bool EnableUpdateForm(const std::vector &formIds); + ErrCode EnableUpdateForm(const std::vector &formIds); /** * @brief Disable form update. * * @param formIds formIds of hostclient. */ - bool DisableUpdateForm(const std::vector &formIds); + ErrCode DisableUpdateForm(const std::vector &formIds); /** * @brief Check form manager service ready. @@ -1298,7 +1299,7 @@ public: * @param formInfos Returns the forms' information of all forms provided. * @return Returns true if the request is successfully initiated; returns false otherwise. */ - bool GetAllFormsInfo(std::vector &formInfos); + ErrCode GetAllFormsInfo(std::vector &formInfos); /** * @brief Get forms info by application name. @@ -1307,7 +1308,7 @@ public: * @param formInfos Returns the forms' information of the specify application name. * @return Returns true if the request is successfully initiated; returns false otherwise. */ - bool GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos); + ErrCode GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos); /** * @brief Get forms info by application name and module name. @@ -1317,7 +1318,14 @@ public: * @param formInfos Returns the forms' information of the specify application name and module name. * @return Returns true if the request is successfully initiated; returns false otherwise. */ - bool GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector &formInfos); + ErrCode GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector &formInfos); + + /** + * @brief Get the error message by error code. + * @param errorCode the error code return form fms. + * @return Returns the error message detail. + */ + std::string GetErrorMsg(const ErrCode errorCode); /** * @brief Acquire a bundle manager, if it not existed, @@ -1617,7 +1625,7 @@ private: * @param deleteType Indicates the type of delete or release. * @return Returns {@code true} if the form is successfully deleted; returns {@code false} otherwise. */ - bool DeleteForm(const int64_t formId, const int32_t deleteType); + ErrCode DeleteForm(const int64_t formId, const int32_t deleteType); /** * @brief Clean form resource with formId. @@ -1656,7 +1664,7 @@ private: * and FORM_INVISIBLE means that the form becomes invisible. * @return none. */ - bool NotifyWhetherVisibleForms(const std::vector &formIds, int32_t eventType); + ErrCode NotifyWhetherVisibleForms(const std::vector &formIds, int32_t eventType); /** * @brief Check the param of want. @@ -1675,7 +1683,7 @@ private: * @param updateType Update type. * @return Returns true if the result is ok; returns false otherwise. */ - bool LifecycleUpdate(std::vector formIds, int32_t updateType); + ErrCode LifecycleUpdate(std::vector formIds, int32_t updateType); /** * @brief Reacquire a specified form when the death callback is received. diff --git a/frameworks/kits/ability/native/src/ability.cpp b/frameworks/kits/ability/native/src/ability.cpp index 27160858b92..7be84d3a3a5 100755 --- a/frameworks/kits/ability/native/src/ability.cpp +++ b/frameworks/kits/ability/native/src/ability.cpp @@ -2005,7 +2005,7 @@ std::shared_ptr Ability::CreatePostEventTimeouter(std:: *
  • The form is being restored.
  • * */ -bool Ability::ReleaseForm(const int64_t formId) +ErrCode Ability::ReleaseForm(const int64_t formId) { APP_LOGI("%{public}s called.", __func__); // release form by formId and do not release cache @@ -2033,7 +2033,7 @@ bool Ability::ReleaseForm(const int64_t formId) *
  • The form is being restored.
  • * */ -bool Ability::ReleaseForm(const int64_t formId, const bool isReleaseCache) +ErrCode Ability::ReleaseForm(const int64_t formId, const bool isReleaseCache) { APP_LOGI("%{public}s called.", __func__); // release form with formId and specifies whether to release the cache @@ -2059,7 +2059,7 @@ bool Ability::ReleaseForm(const int64_t formId, const bool isReleaseCache) *
  • The form is being restored.
  • * */ -bool Ability::DeleteForm(const int64_t formId) +ErrCode Ability::DeleteForm(const int64_t formId) { APP_LOGI("%{public}s called.", __func__); // delete form with formId @@ -2097,26 +2097,26 @@ int Ability::StopBackgroundRunning() * * @return Returns {@code true} if the form is successfully casted; returns {@code false} otherwise. */ -bool Ability::CastTempForm(const int64_t formId) +ErrCode Ability::CastTempForm(const int64_t formId) { APP_LOGI("%{public}s start", __func__); if (formId <= 0) { APP_LOGE("%{public}s error, passing in form id can't be negative.", __func__); - return false; + return ERR_APPEXECFWK_FORM_INVALID_FORM_ID; } APP_LOGI("%{public}s, castTempForm begin of temp form %{public}" PRId64, __func__, formId); - int result = FormMgr::GetInstance().CastTempForm(formId, FormHostClient::GetInstance()); + ErrCode result = FormMgr::GetInstance().CastTempForm(formId, FormHostClient::GetInstance()); if (result != ERR_OK) { APP_LOGE("%{public}s error, some internal server occurs, error code is %{public}d.", __func__, result); - return false; + return result; } userReqParams_[formId].SetParam(Constants::PARAM_FORM_TEMPORARY_KEY, false); APP_LOGI("%{public}s end", __func__); - return true; + return result; } /** @@ -2209,35 +2209,33 @@ bool Ability::AcquireForm(const int64_t formId, const Want &want, const std::sha * @param formProviderData The data used to update the JS form displayed on the client. * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. */ -bool Ability::UpdateForm(const int64_t formId, const FormProviderData &formProviderData) +ErrCode Ability::UpdateForm(const int64_t formId, const FormProviderData &formProviderData) { APP_LOGI("%{public}s called.", __func__); // check fms recover status if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; + return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR; } // check formId if (formId <= 0) { APP_LOGE("%{public}s error, the passed in formId can't be negative or zero.", __func__); - return false; + return ERR_APPEXECFWK_FORM_INVALID_FORM_ID; } // check formProviderData if (formProviderData.GetDataString().empty()) { APP_LOGE("%{public}s error, the formProviderData is null.", __func__); - return false; + return ERR_APPEXECFWK_FORM_PROVIDER_DATA_EMPTY; } // update form request to fms - if (FormMgr::GetInstance().UpdateForm(formId, abilityInfo_->bundleName, formProviderData) != ERR_OK) { + ErrCode result = FormMgr::GetInstance().UpdateForm(formId, abilityInfo_->bundleName, formProviderData); + if (result != ERR_OK) { APP_LOGE("%{public}s error, update form for fms failed.", __func__); - return false; } - - // the update form is successfully - return true; + return result; } /** @@ -2249,7 +2247,7 @@ bool Ability::UpdateForm(const int64_t formId, const FormProviderData &formProvi * @param formIds Indicates the IDs of the forms to be made visible. * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. */ -bool Ability::NotifyVisibleForms(const std::vector &formIds) +ErrCode Ability::NotifyVisibleForms(const std::vector &formIds) { APP_LOGI("%{public}s called.", __func__); return NotifyWhetherVisibleForms(formIds, Constants::FORM_VISIBLE); @@ -2264,7 +2262,7 @@ bool Ability::NotifyVisibleForms(const std::vector &formIds) * @param formIds Indicates the IDs of the forms to be made invisible. * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. */ -bool Ability::NotifyInvisibleForms(const std::vector &formIds) +ErrCode Ability::NotifyInvisibleForms(const std::vector &formIds) { APP_LOGI("%{public}s called.", __func__); return NotifyWhetherVisibleForms(formIds, Constants::FORM_INVISIBLE); @@ -2279,26 +2277,25 @@ bool Ability::NotifyInvisibleForms(const std::vector &formIds) * @param nextTime Indicates the next time gap now in seconds, can not be litter than 300 seconds. * @return Returns {@code true} if seting succeed; returns {@code false} otherwise. */ -bool Ability::SetFormNextRefreshTime(const int64_t formId, const int64_t nextTime) +ErrCode Ability::SetFormNextRefreshTime(const int64_t formId, const int64_t nextTime) { APP_LOGI("%{public}s called.", __func__); if (nextTime < MIN_NEXT_TIME) { APP_LOGE("next time litte than 300 seconds."); - return false; + return ERR_APPEXECFWK_FORM_INVALID_REFRESH_TIME; } if (FormMgr::GetInstance().GetRecoverStatus() == Constants::IN_RECOVERING) { APP_LOGE("%{public}s, formManager is in recovering", __func__); - return false; + return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR; } - int result = FormMgr::GetInstance().SetNextRefreshTime(formId, nextTime); + ErrCode result = FormMgr::GetInstance().SetNextRefreshTime(formId, nextTime); if (result != ERR_OK) { APP_LOGE("%{public}s, internal error:[%{public}d]", __func__, result); - return false; } - return true; + return result; } /** * @brief Requests for form data update. @@ -2311,7 +2308,7 @@ bool Ability::SetFormNextRefreshTime(const int64_t formId, const int64_t nextTim * @param formId Indicates the ID of the form to update. * @return Returns true if the update request is successfully initiated, returns false otherwise. */ -bool Ability::RequestForm(const int64_t formId) +ErrCode Ability::RequestForm(const int64_t formId) { APP_LOGI("%{public}s called.", __func__); Want want; @@ -2445,18 +2442,18 @@ void Ability::OnTriggerEvent(const int64_t formId, const std::string &message) * @param deleteType Indicates the type of delete or release. * @return Returns {@code true} if the form is successfully deleted; returns {@code false} otherwise. */ -bool Ability::DeleteForm(const int64_t formId, const int32_t deleteType) +ErrCode Ability::DeleteForm(const int64_t formId, const int32_t deleteType) { APP_LOGI("%{public}s called.", __func__); // check fms recover status if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; + return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR; } // check formId if (formId <= 0) { APP_LOGE("%{public}s error, the passed in formId can't be negative or zero.", __func__); - return false; + return ERR_APPEXECFWK_FORM_INVALID_FORM_ID; } APP_LOGI("%{public}s, delete form begin, formId is %{public}" PRId64 " and deleteType is %{public}d.", @@ -2471,14 +2468,14 @@ bool Ability::DeleteForm(const int64_t formId, const int32_t deleteType) lostedByReconnectTempForms_.end()) { CleanFormResource(formId); // the delete temp form is successfully - return true; + return ERR_OK; } } // hostClient init sptr formHostClient = FormHostClient::GetInstance(); // delete or release request to fms - int result; + ErrCode result; if (deleteType == DELETE_FORM) { result = FormMgr::GetInstance().DeleteForm(formId, formHostClient); } else { @@ -2487,7 +2484,7 @@ bool Ability::DeleteForm(const int64_t formId, const int32_t deleteType) } if (result != ERR_OK) { APP_LOGE("%{public}s error, some internal server occurs, error code is %{public}d.", __func__, result); - return false; + return result; } { // form lock @@ -2496,7 +2493,7 @@ bool Ability::DeleteForm(const int64_t formId, const int32_t deleteType) CleanFormResource(formId); } // the delete form is successfully - return true; + return result; } /** @@ -2623,26 +2620,25 @@ void Ability::HandleFormMessage(const int32_t msgCode, const FormJsInfo &formJsI * and FORM_INVISIBLE means that the form becomes invisible. * @return Returns {@code true} if the request is successfully initiated; returns {@code false} otherwise. */ -bool Ability::NotifyWhetherVisibleForms(const std::vector &formIds, int32_t eventType) +ErrCode Ability::NotifyWhetherVisibleForms(const std::vector &formIds, int32_t eventType) { APP_LOGI("%{public}s called.", __func__); if (formIds.empty() || formIds.size() > Constants::MAX_VISIBLE_NOTIFY_LIST) { APP_LOGE("%{public}s, formIds is empty or exceed 32.", __func__); - return false; + return ERR_APPEXECFWK_FORM_INVALID_FORM_ID; } if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; + return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR; } - int resultCode = + ErrCode resultCode = FormMgr::GetInstance().NotifyWhetherVisibleForms(formIds, FormHostClient::GetInstance(), eventType); if (resultCode != ERR_OK) { APP_LOGE("%{public}s error, internal error occurs, error code:%{public}d.", __func__, resultCode); - return false; } - return true; + return resultCode; } /** @@ -2699,7 +2695,7 @@ bool Ability::CheckWantValid(const int64_t formId, const Want &want) * * @param formIds FormIds of hostclient. */ -bool Ability::EnableUpdateForm(const std::vector &formIds) +ErrCode Ability::EnableUpdateForm(const std::vector &formIds) { APP_LOGI("%{public}s called.", __func__); return LifecycleUpdate(formIds, ENABLE_FORM_UPDATE); @@ -2710,33 +2706,27 @@ bool Ability::EnableUpdateForm(const std::vector &formIds) * * @param formIds FormIds of hostclient. */ -bool Ability::DisableUpdateForm(const std::vector &formIds) +ErrCode Ability::DisableUpdateForm(const std::vector &formIds) { APP_LOGI("%{public}s called.", __func__); return LifecycleUpdate(formIds, DISABLE_FORM_UPDATE); } -bool Ability::LifecycleUpdate(std::vector formIds, int32_t updateType) +ErrCode Ability::LifecycleUpdate(std::vector formIds, int32_t updateType) { if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; + return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR; } // hostClient init sptr formHostClient = FormHostClient::GetInstance(); if (formHostClient == nullptr) { APP_LOGE("%{public}s error, formHostClient == nullptr.", __func__); - return false; + return ERR_APPEXECFWK_FORM_GET_HOST_FAILED; } - int result = FormMgr::GetInstance().LifecycleUpdate(formIds, formHostClient, updateType); - if (result != ERR_OK) { - APP_LOGE("%{public}s error, internal error.", __func__); - return false; - } - - return true; + return FormMgr::GetInstance().LifecycleUpdate(formIds, formHostClient, updateType); } /** @@ -2751,30 +2741,28 @@ bool Ability::LifecycleUpdate(std::vector formIds, int32_t updateType) * @param want Indicates a set of parameters to be transparently passed to the form provider. * @return Returns true if the update request is successfully initiated, returns false otherwise. */ -bool Ability::RequestForm(const int64_t formId, const Want &want) +ErrCode Ability::RequestForm(const int64_t formId, const Want &want) { APP_LOGI("%{public}s called.", __func__); if (formId <= 0) { APP_LOGE("%{public}s error, The passed formid is invalid. Its value must be larger than 0.", __func__); - return false; + return ERR_APPEXECFWK_FORM_INVALID_FORM_ID; } if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; + return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR; } // requestForm request to fms - int resultCode = FormMgr::GetInstance().RequestForm(formId, FormHostClient::GetInstance(), want); + ErrCode resultCode = FormMgr::GetInstance().RequestForm(formId, FormHostClient::GetInstance(), want); if (resultCode != ERR_OK) { APP_LOGE("%{public}s error, failed to notify the form service that the form user's lifecycle is updated, error " "code is %{public}d.", __func__, resultCode); - return false; } - - return true; + return resultCode; } /** @@ -2888,23 +2876,17 @@ bool Ability::CheckFMSReady() * @param formInfos Return the forms' information of all forms provided. * @return Return true if the request is successfully initiated; return false otherwise. */ -bool Ability::GetAllFormsInfo(std::vector &formInfos) +ErrCode Ability::GetAllFormsInfo(std::vector &formInfos) { APP_LOGI("%{public}s called.", __func__); if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; + return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR; } // GetAllFormsInfo request to fms - int resultCode = FormMgr::GetInstance().GetAllFormsInfo(formInfos); - if (resultCode != ERR_OK) { - APP_LOGE("%{public}s error, failed to GetAllFormsInfo, error code is %{public}d.", __func__, resultCode); - return false; - } - - return !formInfos.empty(); + return FormMgr::GetInstance().GetAllFormsInfo(formInfos); } /** @@ -2914,27 +2896,21 @@ bool Ability::GetAllFormsInfo(std::vector &formInfos) * @param formInfos Return the forms' information of the specify application name. * @return Return true if the request is successfully initiated; return false otherwise. */ -bool Ability::GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos) +ErrCode Ability::GetFormsInfoByApp(std::string &bundleName, std::vector &formInfos) { APP_LOGI("%{public}s called.", __func__); if (bundleName.empty()) { APP_LOGW("Failed to Get forms info, because empty bundle name"); - return false; + return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME; } if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; + return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR; } // GetFormsInfoByApp request to fms - int resultCode = FormMgr::GetInstance().GetFormsInfoByApp(bundleName, formInfos); - if (resultCode != ERR_OK) { - APP_LOGE("%{public}s error, failed to GetFormsInfoByApp, error code is %{public}d.", __func__, resultCode); - return false; - } - - return !formInfos.empty(); + return FormMgr::GetInstance().GetFormsInfoByApp(bundleName, formInfos); } /** @@ -2945,27 +2921,36 @@ bool Ability::GetFormsInfoByApp(std::string &bundleName, std::vector & * @param formInfos Return the forms' information of the specify bundle name and module name. * @return Return true if the request is successfully initiated; return false otherwise. */ -bool Ability::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector &formInfos) +ErrCode Ability::GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, std::vector &formInfos) { APP_LOGI("%{public}s called.", __func__); - if (bundleName.empty() || moduleName.empty()) { - APP_LOGW("Failed to Get forms info, because empty bundleName or moduleName"); - return false; + if (bundleName.empty()) { + APP_LOGW("Failed to Get forms info, because empty bundleName"); + return ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME; + } + + if (moduleName.empty()) { + APP_LOGW("Failed to Get forms info, because empty moduleName"); + return ERR_APPEXECFWK_FORM_INVALID_MODULENAME; } if (FormMgr::GetRecoverStatus() == Constants::IN_RECOVERING) { APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); - return false; + return ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR; } // GetFormsInfoByModule request to fms - int resultCode = FormMgr::GetInstance().GetFormsInfoByModule(bundleName, moduleName, formInfos); - if (resultCode != ERR_OK) { - APP_LOGE("%{public}s error, failed to GetFormsInfoByModule, error code is %{public}d.", __func__, resultCode); - return false; - } + return FormMgr::GetInstance().GetFormsInfoByModule(bundleName, moduleName, formInfos); +} - return !formInfos.empty(); +/** + * @brief Get the error message by error code. + * @param errorCode the error code return form fms. + * @return Returns the error message detail. + */ +std::string Ability::GetErrorMsg(const ErrCode errorCode) +{ + return FormMgr::GetInstance().GetErrorMessage(errorCode); } /** diff --git a/frameworks/kits/fmskit/native/src/form_errors.cpp b/frameworks/kits/fmskit/native/src/form_errors.cpp index 25203bad713..120942e9541 100644 --- a/frameworks/kits/fmskit/native/src/form_errors.cpp +++ b/frameworks/kits/fmskit/native/src/form_errors.cpp @@ -47,7 +47,7 @@ std::string FormErrors::GetErrorMessage(int errCode) */ void FormErrors::InitErrorMessageMap() { - errorMessageMap_ = { // error + message + errorMessageMap_ = { // error + message { ERR_APPEXECFWK_FORM_COMMON_CODE, "some internal server error occurs.", }, @@ -55,6 +55,10 @@ void FormErrors::InitErrorMessageMap() ERR_APPEXECFWK_FORM_PERMISSION_DENY, "check permission deny, need to request ohos.permission.REQUIRE_FORM.", }, + { + ERR_APPEXECFWK_FORM_PERMISSION_DENY_SYS, + "check permission deny, need system permission.", + }, { ERR_APPEXECFWK_FORM_GET_INFO_FAILED, "can't get form info by the formName.", }, @@ -64,6 +68,30 @@ void FormErrors::InitErrorMessageMap() { ERR_APPEXECFWK_FORM_INVALID_PARAM, "invalid params received on operating form.", }, + { + ERR_APPEXECFWK_FORM_INVALID_FORM_ID, "formId must be a string.", + }, + { + ERR_APPEXECFWK_FORM_FORM_ID_NUM_ERR, "formId must be a numeric string.", + }, + { + ERR_APPEXECFWK_FORM_REFRESH_TIME_NUM_ERR, "refresh time must be a number.", + }, + { + ERR_APPEXECFWK_FORM_INVALID_BUNDLENAME, "bundleName is not available.", + }, + { + ERR_APPEXECFWK_FORM_INVALID_MODULENAME, "moduleName is not available.", + }, + { + ERR_APPEXECFWK_FORM_INVALID_PROVIDER_DATA, "moduleName is not available.", + }, + { + ERR_APPEXECFWK_FORM_INVALID_REFRESH_TIME, "refresh time is not available.", + }, + { + ERR_APPEXECFWK_FORM_SERVER_STATUS_ERR, "form server error.", + }, { ERR_APPEXECFWK_FORM_CFG_NOT_MATCH_ID, "the form id and form config are not matched.", }, diff --git a/interfaces/kits/napi/aafwk/formManager/napi_form_manager.cpp b/interfaces/kits/napi/aafwk/formManager/napi_form_manager.cpp index 9981c40d04b..1109017f350 100644 --- a/interfaces/kits/napi/aafwk/formManager/napi_form_manager.cpp +++ b/interfaces/kits/napi/aafwk/formManager/napi_form_manager.cpp @@ -39,6 +39,8 @@ namespace { constexpr int DECIMAL_VALUE = 10; constexpr int BASE_NUMBER = 9; constexpr int REF_COUNT = 1; + constexpr int CALLBACK_FLG = 1; + constexpr int PROMISE_FLG = 2; OHOS::AppExecFwk::Ability* g_ability = nullptr; } @@ -174,6 +176,130 @@ static std::string GetStringFromNAPI(napi_env env, napi_value value) return result; } +/** + * @brief Create return message + * + * @param[in] env The environment that the Node-API call is invoked under + * @param[in] code result code + * @param[out] result result message + * + * @return void + */ +void InnerCreateRetMsg(napi_env env, int32_t code, napi_value* result) +{ + HILOG_DEBUG("%{public}s called. code:%{public}d", __func__, code); + if (code == ERR_OK) { + napi_create_int32(env, ERR_OK, result); + return; + } + + OHOS::AppExecFwk::Ability* ability = GetGlobalAbility(env); + std::string msg = ability->GetErrorMsg(code); + HILOG_DEBUG("message: %{public}s", msg.c_str()); + napi_value errInfo = nullptr; + napi_value errCode = nullptr; + napi_value errMsg = nullptr; + // napi_value data = nullptr; + + napi_create_object(env, &errInfo); + napi_create_int32(env, code, &errCode); + napi_create_string_utf8(env, msg.c_str(), NAPI_AUTO_LENGTH, &errMsg); + napi_set_named_property(env, errInfo, "code", errCode); + napi_set_named_property(env, errInfo, "data", errMsg); + // napi_create_object(env, &data); + result[0] = errInfo; + // result[1] = data; + HILOG_DEBUG("%{public}s, end.", __func__); +} + +/** + * @brief Send error message. + * + * @param[in] env The environment that the Node-API call is invoked under + * @param[in] value Use create callback + * @param[in] code Result code + * @param[in] type Callback or promise type + * @param[out] result result message + * + * @return void + */ +napi_value RetErrMsg(AsyncErrMsgCallbackInfo* asyncCallbackInfo) +{ + HILOG_INFO("%{public}s called.", __func__); + napi_env env = asyncCallbackInfo->env; + napi_value value = asyncCallbackInfo->callbackValue; + + if (asyncCallbackInfo->type == CALLBACK_FLG) { + HILOG_INFO("%{public}s, asyncCallback.", __func__); + + // Check the value type of the arguments + napi_valuetype valueType = napi_undefined; + NAPI_CALL(env, napi_typeof(env, value, &valueType)); + NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of deleteForm is incorrect,\ + expected type is function."); + + napi_create_reference(env, value, REF_COUNT, &asyncCallbackInfo->callback); + + napi_value resourceName; + napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) {}, + [](napi_env env, napi_status status, void *data) { + AsyncErrMsgCallbackInfo *asyncCallbackInfo = (AsyncErrMsgCallbackInfo *)data; + HILOG_INFO("%{public}s, napi_create_async_work complete", __func__); + + if (asyncCallbackInfo->callback != nullptr) { + napi_value callback; + napi_value undefined; + napi_value result; + InnerCreateRetMsg(env, asyncCallbackInfo->code, &result); + napi_get_undefined(env, &undefined); + napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); + napi_value callResult; + napi_call_function(env, undefined, callback, ARGS_SIZE_ONE, &result, &callResult); + napi_delete_reference(env, asyncCallbackInfo->callback); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + }, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + return NapiGetResut(env, 1); + } else { + HILOG_INFO("%{public}s, promise.", __func__); + napi_deferred deferred; + napi_value promise; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + asyncCallbackInfo->deferred = deferred; + + napi_value resourceName; + napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) {}, + [](napi_env env, napi_status status, void *data) { + HILOG_INFO("%{public}s, promise complete", __func__); + AsyncErrMsgCallbackInfo *asyncCallbackInfo = (AsyncErrMsgCallbackInfo *)data; + + napi_value result; + InnerCreateRetMsg(env, asyncCallbackInfo->code, &result); + napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + }, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); + napi_queue_async_work(env, asyncCallbackInfo->asyncWork); + return promise; + } +} + /** * @brief Parse form info into Node-API * @@ -343,12 +469,8 @@ static void InnerDelForm(napi_env env, AsyncDelFormCallbackInfo* const asyncCall { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - bool ret = ability->DeleteForm(asyncCallbackInfo->formId); - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } + ErrCode ret = ability->DeleteForm(asyncCallbackInfo->formId); + asyncCallbackInfo->result = ret; HILOG_DEBUG("%{public}s, end", __func__); } @@ -377,26 +499,59 @@ napi_value NAPI_DeleteForm(napi_env env, napi_callback_info info) // Check the value type of the arguments napi_valuetype valueType; NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] type of deleteForm is incorrect,\ - expected type is string."); + if (valueType != napi_string) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_INVALID_FORM_ID, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } std::string strFormId = GetStringFromNAPI(env, argv[0]); int64_t formId; - bool isConversionSucceeded = ConvertStringToInt64(strFormId, formId); - NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of deleteForm is incorrect,\ - expected type is string and the content must be numeric,\ - value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); + HILOG_ERROR("%{public}s, form id ", strFormId.c_str()); + if (!ConvertStringToInt64(strFormId, formId)) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_FORM_ID_NUM_ERR, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } AsyncDelFormCallbackInfo *asyncCallbackInfo = new - AsyncDelFormCallbackInfo { - .env = env, - .ability = GetGlobalAbility(env), - .asyncWork = nullptr, - .deferred = nullptr, - .callback = nullptr, - .formId = 0, - .result = 0, - }; + AsyncDelFormCallbackInfo { + .env = env, + .ability = GetGlobalAbility(env), + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .formId = 0, + .result = 0, + }; asyncCallbackInfo->formId = formId; if (argc == ARGS_SIZE_TWO) { @@ -406,7 +561,7 @@ napi_value NAPI_DeleteForm(napi_env env, napi_callback_info info) valueType = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of deleteForm is incorrect,\ - expected type is function."); + expected type is function."); napi_create_reference(env, argv[1], REF_COUNT, &asyncCallbackInfo->callback); @@ -466,7 +621,7 @@ napi_value NAPI_DeleteForm(napi_env env, napi_callback_info info) AsyncDelFormCallbackInfo *asyncCallbackInfo = (AsyncDelFormCallbackInfo *)data; napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; @@ -490,12 +645,8 @@ static void InnerReleaseForm(napi_env env, AsyncReleaseFormCallbackInfo* const a { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - bool ret = ability->ReleaseForm(asyncCallbackInfo->formId, asyncCallbackInfo->isReleaseCache); - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } + ErrCode ret = ability->ReleaseForm(asyncCallbackInfo->formId, asyncCallbackInfo->isReleaseCache); + asyncCallbackInfo->result = ret; HILOG_DEBUG("%{public}s end", __func__); } @@ -515,7 +666,7 @@ auto NAPI_ReleaseFormAsyncComplete = [](napi_env env, napi_status status, void * if (asyncCallbackInfo->callback != nullptr) { napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_value callback; napi_value undefined; napi_get_undefined(env, &undefined); @@ -531,11 +682,10 @@ auto NAPI_ReleaseFormAsyncComplete = [](napi_env env, napi_status status, void * // NAPI_ReleaseForm promise Complete auto NAPI_ReleaseFormPromiseComplete = [](napi_env env, napi_status status, void *data) { HILOG_INFO("%{public}s, promise complete", __func__); - AsyncReleaseFormCallbackInfo *asyncCallbackInfo = - (AsyncReleaseFormCallbackInfo *)data; + AsyncReleaseFormCallbackInfo *asyncCallbackInfo = (AsyncReleaseFormCallbackInfo *)data; napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; @@ -608,15 +758,73 @@ napi_value NAPI_ReleaseForm(napi_env env, napi_callback_info info) // Check the value type of the arguments napi_valuetype valueType; NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_ZERO], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] type of releaseForm is incorrect,\ - expected type is string."); + if (valueType != napi_string) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_INVALID_FORM_ID, + .type = 0, + .callbackValue = argv[ARGS_SIZE_TWO] + }; + + if (argc == ARGS_SIZE_ONE) { + asyncErrorInfo->type = PROMISE_FLG; + } else if (argc == ARGS_SIZE_THREE) { + asyncErrorInfo->type = CALLBACK_FLG; + asyncErrorInfo->callbackValue = argv[ARGS_SIZE_TWO]; + } else { + valueType = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); + if (valueType == napi_function) { + asyncErrorInfo->type = CALLBACK_FLG; + asyncErrorInfo->callbackValue = argv[1]; + } else if (valueType == napi_boolean) { + asyncErrorInfo->type = PROMISE_FLG; + } else { + NAPI_ASSERT(env, valueType == napi_function || valueType == napi_boolean, + "The arguments[2] type of releaseForm is incorrect,expected type is function or boolean."); + } + } + return RetErrMsg(asyncErrorInfo); + } std::string strFormId = GetStringFromNAPI(env, argv[ARGS_SIZE_ZERO]); int64_t formId; - bool isConversionSucceeded = ConvertStringToInt64(strFormId, formId); - NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of releaseForm is incorrect,\ - expected type is string and the content must be numeric,\ - value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); + if (!ConvertStringToInt64(strFormId, formId)) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_FORM_ID_NUM_ERR, + .type = 0, + .callbackValue = argv[ARGS_SIZE_TWO] + }; + + if (argc == ARGS_SIZE_ONE) { + asyncErrorInfo->type = PROMISE_FLG; + } else if (argc == ARGS_SIZE_THREE) { + asyncErrorInfo->type = CALLBACK_FLG; + asyncErrorInfo->callbackValue = argv[ARGS_SIZE_TWO]; + } else { + valueType = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); + if (valueType == napi_function) { + asyncErrorInfo->type = CALLBACK_FLG; + asyncErrorInfo->callbackValue = argv[1]; + } else if (valueType == napi_boolean) { + asyncErrorInfo->type = PROMISE_FLG; + } else { + NAPI_ASSERT(env, valueType == napi_function || valueType == napi_boolean, + "The arguments[2] type of releaseForm is incorrect,expected type is function or boolean."); + } + } + return RetErrMsg(asyncErrorInfo); + } AsyncReleaseFormCallbackInfo *asyncCallbackInfo = new AsyncReleaseFormCallbackInfo { @@ -635,24 +843,24 @@ napi_value NAPI_ReleaseForm(napi_env env, napi_callback_info info) } else if (argc == ARGS_SIZE_THREE) { // release callback, three argv HILOG_INFO("%{public}s, asyncCallback.", __func__); valueType = napi_undefined; - NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_ONE], &valueType)); + NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); NAPI_ASSERT(env, valueType == napi_boolean, "The arguments[1] type of releaseForm is incorrect,\ - expected type is boolean."); + expected type is boolean."); - napi_get_value_bool(env, argv[ARGS_SIZE_ONE], &asyncCallbackInfo->isReleaseCache); + napi_get_value_bool(env, argv[1], &asyncCallbackInfo->isReleaseCache); // Check the value type of the arguments valueType = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_TWO], &valueType)); NAPI_ASSERT(env, valueType == napi_function, "The arguments[2] type of releaseForm is incorrect,\ - expected type is function."); + expected type is function."); napi_create_reference(env, argv[ARGS_SIZE_TWO], REF_COUNT, &asyncCallbackInfo->callback); ReleaseFormCallback(env, asyncCallbackInfo); } else { valueType = napi_undefined; - NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_ONE], &valueType)); + NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); if (valueType == napi_function) { // release callback, two argv - napi_create_reference(env, argv[ARGS_SIZE_ONE], REF_COUNT, &asyncCallbackInfo->callback); + napi_create_reference(env, argv[1], REF_COUNT, &asyncCallbackInfo->callback); ReleaseFormCallback(env, asyncCallbackInfo); } else if (valueType == napi_boolean) { // release promise, two argv return ReleaseFormPromise(env, asyncCallbackInfo); @@ -676,12 +884,7 @@ static void InnerRequestForm(napi_env env, AsyncRequestFormCallbackInfo* const a { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - bool ret = ability->RequestForm(asyncCallbackInfo->formId); - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } + asyncCallbackInfo->result = ability->RequestForm(asyncCallbackInfo->formId); HILOG_DEBUG("%{public}s, end", __func__); } @@ -710,15 +913,47 @@ napi_value NAPI_RequestForm(napi_env env, napi_callback_info info) // Check the value type of the arguments napi_valuetype valueType; NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] type of requestForm is incorrect,\ - expected type is string."); + if (valueType != napi_string) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_INVALID_FORM_ID, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } std::string strFormId = GetStringFromNAPI(env, argv[0]); int64_t formId; - bool isConversionSucceeded = ConvertStringToInt64(strFormId, formId); - NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of requestForm is incorrect,\ - expected type is string and the content must be numeric,\ - value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); + if (!ConvertStringToInt64(strFormId, formId)) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_FORM_ID_NUM_ERR, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } AsyncRequestFormCallbackInfo *asyncCallbackInfo = new AsyncRequestFormCallbackInfo { @@ -759,7 +994,7 @@ napi_value NAPI_RequestForm(napi_env env, napi_callback_info info) if (asyncCallbackInfo->callback != nullptr) { napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_value callback; napi_value undefined; napi_get_undefined(env, &undefined); @@ -798,7 +1033,7 @@ napi_value NAPI_RequestForm(napi_env env, napi_callback_info info) AsyncRequestFormCallbackInfo *asyncCallbackInfo = (AsyncRequestFormCallbackInfo *)data; napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; @@ -822,12 +1057,7 @@ static void InnerSetFormNextRefreshTime(napi_env env, AsyncNextRefreshTimeFormCa { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - bool ret = ability->SetFormNextRefreshTime(asyncCallbackInfo->formId, asyncCallbackInfo->time); - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } + asyncCallbackInfo->result = ability->SetFormNextRefreshTime(asyncCallbackInfo->formId, asyncCallbackInfo->time); HILOG_DEBUG("%{public}s, end", __func__); } @@ -856,35 +1086,85 @@ napi_value NAPI_SetFormNextRefreshTime(napi_env env, napi_callback_info info) // Check the value type of the arguments napi_valuetype valueType; NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] type of setFormNextRefreshTime is incorrect,\ - expected type is string."); + if (valueType != napi_string) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_INVALID_FORM_ID, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_THREE) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } std::string strFormId = GetStringFromNAPI(env, argv[0]); int64_t formId; - bool isConversionSucceeded = ConvertStringToInt64(strFormId, formId); - NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of setFormNextRefreshTime is incorrect,\ - expected type is string and the content must be numeric,\ - value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); + if (!ConvertStringToInt64(strFormId, formId)) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_FORM_ID_NUM_ERR, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_THREE) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } valueType = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); - NAPI_ASSERT(env, valueType == napi_number, "The arguments[1] type of setFormNextRefreshTime is incorrect,\ - expected type is number."); + if (valueType != napi_number) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_REFRESH_TIME_NUM_ERR, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_THREE) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } + int32_t time; napi_get_value_int32(env, argv[1], &time); AsyncNextRefreshTimeFormCallbackInfo *asyncCallbackInfo = new - AsyncNextRefreshTimeFormCallbackInfo { - .env = env, - .ability = GetGlobalAbility(env), - .asyncWork = nullptr, - .deferred = nullptr, - .callback = nullptr, - .formId = formId, - .time = time, - .result = 0, - }; + AsyncNextRefreshTimeFormCallbackInfo { + .env = env, + .ability = GetGlobalAbility(env), + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .formId = formId, + .time = time, + .result = 0, + }; if (argc == ARGS_SIZE_THREE) { HILOG_INFO("%{public}s, asyncCallback.", __func__); @@ -919,7 +1199,7 @@ napi_value NAPI_SetFormNextRefreshTime(napi_env env, napi_callback_info info) if (asyncCallbackInfo->callback != nullptr) { napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_value callback; napi_value undefined; napi_get_undefined(env, &undefined); @@ -961,7 +1241,7 @@ napi_value NAPI_SetFormNextRefreshTime(napi_env env, napi_callback_info info) (AsyncNextRefreshTimeFormCallbackInfo *)data; napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; @@ -985,12 +1265,7 @@ static void InnerUpdateForm(napi_env env, AsyncUpdateFormCallbackInfo* const asy { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - bool ret = ability->UpdateForm(asyncCallbackInfo->formId, *asyncCallbackInfo->formProviderData); - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } + asyncCallbackInfo->result = ability->UpdateForm(asyncCallbackInfo->formId, *asyncCallbackInfo->formProviderData); HILOG_DEBUG("%{public}s, end", __func__); } @@ -1019,34 +1294,83 @@ napi_value NAPI_UpdateForm(napi_env env, napi_callback_info info) // Check the value type of the arguments napi_valuetype valueType; NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] type of updateForm is incorrect,\ - expected type is string."); + if (valueType != napi_string) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_INVALID_FORM_ID, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_THREE) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } std::string strFormId = GetStringFromNAPI(env, argv[0]); int64_t formId; - bool isConversionSucceeded = ConvertStringToInt64(strFormId, formId); - NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of updateForm is incorrect,\ - expected type is string and the content must be numeric,\ - value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); + if (!ConvertStringToInt64(strFormId, formId)) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_FORM_ID_NUM_ERR, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_THREE) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[1] type of updateForm is incorrect,\ - expected type is string."); + if (valueType != napi_string) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_INVALID_PROVIDER_DATA, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_THREE) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } OHOS::AppExecFwk::FormProviderData *formProviderData = nullptr; napi_unwrap(env, argv[1], (void**)&formProviderData); AsyncUpdateFormCallbackInfo *asyncCallbackInfo = new - AsyncUpdateFormCallbackInfo { - .env = env, - .ability = GetGlobalAbility(env), - .asyncWork = nullptr, - .deferred = nullptr, - .callback = nullptr, - .formId = formId, - .formProviderData = std::shared_ptr(formProviderData), - .result = 0, - }; + AsyncUpdateFormCallbackInfo { + .env = env, + .ability = GetGlobalAbility(env), + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .formId = formId, + .formProviderData = std::shared_ptr(formProviderData), + .result = 0, + }; if (argc == ARGS_SIZE_THREE) { HILOG_INFO("%{public}s, asyncCallback.", __func__); @@ -1076,7 +1400,7 @@ napi_value NAPI_UpdateForm(napi_env env, napi_callback_info info) if (asyncCallbackInfo->callback != nullptr) { napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_value callback; napi_value undefined; napi_get_undefined(env, &undefined); @@ -1115,7 +1439,7 @@ napi_value NAPI_UpdateForm(napi_env env, napi_callback_info info) AsyncUpdateFormCallbackInfo *asyncCallbackInfo = (AsyncUpdateFormCallbackInfo *)data; napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; @@ -1139,12 +1463,7 @@ static void InnerCastTempForm(napi_env env, AsyncCastTempFormCallbackInfo* const { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - bool ret = ability->CastTempForm(asyncCallbackInfo->formId); - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } + asyncCallbackInfo->result = ability->CastTempForm(asyncCallbackInfo->formId); HILOG_DEBUG("%{public}s, end", __func__); } @@ -1173,15 +1492,47 @@ napi_value NAPI_CastTempForm(napi_env env, napi_callback_info info) // Check the value type of the arguments napi_valuetype valueType; NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] type of castTempForm is incorrect,\ - expected type is string."); + if (valueType != napi_string) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_INVALID_FORM_ID, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } std::string strFormId = GetStringFromNAPI(env, argv[0]); int64_t formId; - bool isConversionSucceeded = ConvertStringToInt64(strFormId, formId); - NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of castTempForm is incorrect,\ - expected type is string and the content must be numeric,\ - value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); + if (!ConvertStringToInt64(strFormId, formId)) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_FORM_ID_NUM_ERR, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } AsyncCastTempFormCallbackInfo *asyncCallbackInfo = new AsyncCastTempFormCallbackInfo { @@ -1222,7 +1573,7 @@ napi_value NAPI_CastTempForm(napi_env env, napi_callback_info info) if (asyncCallbackInfo->callback != nullptr) { napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_value callback; napi_value undefined; napi_get_undefined(env, &undefined); @@ -1261,7 +1612,7 @@ napi_value NAPI_CastTempForm(napi_env env, napi_callback_info info) AsyncCastTempFormCallbackInfo *asyncCallbackInfo = (AsyncCastTempFormCallbackInfo *)data; napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; @@ -1285,12 +1636,7 @@ static void InnerNotifyVisibleForms(napi_env env, AsyncNotifyVisibleFormsCallbac { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - bool ret = ability->NotifyVisibleForms(asyncCallbackInfo->formIds); - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } + asyncCallbackInfo->result = ability->NotifyVisibleForms(asyncCallbackInfo->formIds); HILOG_DEBUG("%{public}s, end", __func__); } @@ -1319,7 +1665,7 @@ napi_value NAPI_NotifyVisibleForms(napi_env env, napi_callback_info info) uint32_t arrayLength = 0; NAPI_CALL(env, napi_get_array_length(env, argv[0], &arrayLength)); NAPI_ASSERT(env, arrayLength > 0, "The arguments[0] value of notifyVisibleForms is incorrect,\ - this array is empty."); + this array is empty."); std::vector formIds; formIds.clear(); @@ -1331,28 +1677,61 @@ napi_value NAPI_NotifyVisibleForms(napi_env env, napi_callback_info info) // Check the value type of the arguments valueType = napi_undefined; NAPI_CALL(env, napi_typeof(env, napiFormId, &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] value type of notifyVisibleForms is incorrect,\ - expected type is string."); + if (valueType != napi_string) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_INVALID_FORM_ID, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } std::string strFormId = GetStringFromNAPI(env, napiFormId); int64_t formIdValue; - bool isConversionSucceeded = ConvertStringToInt64(strFormId, formIdValue); - NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of notifyVisibleForms is incorrect,\ - expected type is string and the content must be numeric,\ - value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); + if (!ConvertStringToInt64(strFormId, formIdValue)) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_FORM_ID_NUM_ERR, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } + formIds.push_back(formIdValue); } AsyncNotifyVisibleFormsCallbackInfo *asyncCallbackInfo = new - AsyncNotifyVisibleFormsCallbackInfo { - .env = env, - .ability = GetGlobalAbility(env), - .asyncWork = nullptr, - .deferred = nullptr, - .callback = nullptr, - .formIds = formIds, - .result = 1, - }; + AsyncNotifyVisibleFormsCallbackInfo { + .env = env, + .ability = GetGlobalAbility(env), + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .formIds = formIds, + .result = 1, + }; if (argc == ARGS_SIZE_TWO) { HILOG_INFO("%{public}s, asyncCallback.", __func__); @@ -1387,7 +1766,7 @@ napi_value NAPI_NotifyVisibleForms(napi_env env, napi_callback_info info) if (asyncCallbackInfo->callback != nullptr) { napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_value callback; napi_value undefined; napi_get_undefined(env, &undefined); @@ -1431,7 +1810,7 @@ napi_value NAPI_NotifyVisibleForms(napi_env env, napi_callback_info info) (AsyncNotifyVisibleFormsCallbackInfo *)data; napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; @@ -1455,12 +1834,7 @@ static void InnerNotifyInvisibleForms(napi_env env, AsyncNotifyInvisibleFormsCal { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - bool ret = ability->NotifyInvisibleForms(asyncCallbackInfo->formIds); - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } + asyncCallbackInfo->result = ability->NotifyInvisibleForms(asyncCallbackInfo->formIds); HILOG_DEBUG("%{public}s, end", __func__); } @@ -1501,28 +1875,61 @@ napi_value NAPI_NotifyInvisibleForms(napi_env env, napi_callback_info info) // Check the value type of the arguments valueType = napi_undefined; NAPI_CALL(env, napi_typeof(env, napiFormId, &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] value type of notifyInvisibleForms \ - is incorrect, expected type is string."); + if (valueType != napi_string) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_INVALID_FORM_ID, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } std::string strFormId = GetStringFromNAPI(env, napiFormId); int64_t formIdValue; - bool isConversionSucceeded = ConvertStringToInt64(strFormId, formIdValue); - NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of notifyInvisibleForms is incorrect,\ - expected type is string and the content must be numeric,\ - value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); + if (!ConvertStringToInt64(strFormId, formIdValue)) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_FORM_ID_NUM_ERR, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } + formIds.push_back(formIdValue); } AsyncNotifyInvisibleFormsCallbackInfo *asyncCallbackInfo = new - AsyncNotifyInvisibleFormsCallbackInfo { - .env = env, - .ability = GetGlobalAbility(env), - .asyncWork = nullptr, - .deferred = nullptr, - .callback = nullptr, - .formIds = formIds, - .result = 1, - }; + AsyncNotifyInvisibleFormsCallbackInfo { + .env = env, + .ability = GetGlobalAbility(env), + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .formIds = formIds, + .result = 1, + }; if (argc == ARGS_SIZE_TWO) { HILOG_INFO("%{public}s, asyncCallback.", __func__); @@ -1557,7 +1964,7 @@ napi_value NAPI_NotifyInvisibleForms(napi_env env, napi_callback_info info) if (asyncCallbackInfo->callback != nullptr) { napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_value callback; napi_value undefined; napi_get_undefined(env, &undefined); @@ -1601,7 +2008,7 @@ napi_value NAPI_NotifyInvisibleForms(napi_env env, napi_callback_info info) (AsyncNotifyInvisibleFormsCallbackInfo *)data; napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; @@ -1625,12 +2032,7 @@ static void InnerEnableFormsUpdate(napi_env env, AsyncEnableUpdateFormCallbackIn { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - bool ret = ability->EnableUpdateForm(asyncCallbackInfo->formIds); - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } + asyncCallbackInfo->result = ability->EnableUpdateForm(asyncCallbackInfo->formIds); HILOG_DEBUG("%{public}s, end", __func__); } @@ -1659,7 +2061,7 @@ napi_value NAPI_EnableFormsUpdate(napi_env env, napi_callback_info info) uint32_t arrayLength = 0; NAPI_CALL(env, napi_get_array_length(env, argv[0], &arrayLength)); NAPI_ASSERT(env, arrayLength > 0, "The arguments[0] value of enableFormsUpdate \ - is incorrect, this array is empty."); + is incorrect, this array is empty."); std::vector formIds; formIds.clear(); @@ -1671,15 +2073,48 @@ napi_value NAPI_EnableFormsUpdate(napi_env env, napi_callback_info info) // Check the value type of the arguments valueType = napi_undefined; NAPI_CALL(env, napi_typeof(env, napiFormId, &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] value type of enableFormsUpdate \ - is incorrect, expected type is string."); + if (valueType != napi_string) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_INVALID_FORM_ID, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } std::string strFormId = GetStringFromNAPI(env, napiFormId); int64_t formIdValue; - bool isConversionSucceeded = ConvertStringToInt64(strFormId, formIdValue); - NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of enableFormsUpdate is incorrect,\ - expected type is string and the content must be numeric,\ - value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); + if (!ConvertStringToInt64(strFormId, formIdValue)) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_FORM_ID_NUM_ERR, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } + formIds.push_back(formIdValue); } @@ -1701,7 +2136,7 @@ napi_value NAPI_EnableFormsUpdate(napi_env env, napi_callback_info info) valueType = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of enableFormsUpdate \ - is incorrect, expected type is function."); + is incorrect, expected type is function."); napi_create_reference(env, argv[1], REF_COUNT, &asyncCallbackInfo->callback); @@ -1727,7 +2162,7 @@ napi_value NAPI_EnableFormsUpdate(napi_env env, napi_callback_info info) if (asyncCallbackInfo->callback != nullptr) { napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_value callback; napi_value undefined; napi_get_undefined(env, &undefined); @@ -1770,7 +2205,7 @@ napi_value NAPI_EnableFormsUpdate(napi_env env, napi_callback_info info) (AsyncEnableUpdateFormCallbackInfo *)data; napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; @@ -1794,12 +2229,7 @@ static void InnerDisableFormsUpdate(napi_env env, AsyncDisableUpdateFormCallback { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - bool ret = ability->DisableUpdateForm(asyncCallbackInfo->formIds); - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } + asyncCallbackInfo->result = ability->DisableUpdateForm(asyncCallbackInfo->formIds); HILOG_DEBUG("%{public}s, end", __func__); } @@ -1828,7 +2258,7 @@ napi_value NAPI_DisableFormsUpdate(napi_env env, napi_callback_info info) uint32_t arrayLength = 0; NAPI_CALL(env, napi_get_array_length(env, argv[0], &arrayLength)); NAPI_ASSERT(env, arrayLength > 0, "The arguments[0] value of disableFormsUpdate \ - is incorrect, this array is empty."); + is incorrect, this array is empty."); std::vector formIds; formIds.clear(); @@ -1840,28 +2270,61 @@ napi_value NAPI_DisableFormsUpdate(napi_env env, napi_callback_info info) // Check the value type of the arguments valueType = napi_undefined; NAPI_CALL(env, napi_typeof(env, napiFormId, &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] value type of disableFormsUpdate \ - is incorrect, expected type is string."); + if (valueType != napi_string) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_INVALID_FORM_ID, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } std::string strFormId = GetStringFromNAPI(env, napiFormId); int64_t formIdValue; - bool isConversionSucceeded = ConvertStringToInt64(strFormId, formIdValue); - NAPI_ASSERT(env, isConversionSucceeded, "The arguments[0] type of disableFormsUpdate is incorrect,\ - expected type is string and the content must be numeric,\ - value range is: 0x8000000000000000~0x7FFFFFFFFFFFFFFF."); + if (!ConvertStringToInt64(strFormId, formIdValue)) { + AsyncErrMsgCallbackInfo *asyncErrorInfo = new + AsyncErrMsgCallbackInfo { + .env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .code = ERR_APPEXECFWK_FORM_FORM_ID_NUM_ERR, + .type = 0, + .callbackValue = argv[1] + }; + + if (argc == ARGS_SIZE_TWO) { + asyncErrorInfo->type = CALLBACK_FLG; + } else { + asyncErrorInfo->type = PROMISE_FLG; + } + return RetErrMsg(asyncErrorInfo); + } + formIds.push_back(formIdValue); } AsyncDisableUpdateFormCallbackInfo *asyncCallbackInfo = new - AsyncDisableUpdateFormCallbackInfo { - .env = env, - .ability = GetGlobalAbility(env), - .asyncWork = nullptr, - .deferred = nullptr, - .callback = nullptr, - .formIds = formIds, - .result = 0, - }; + AsyncDisableUpdateFormCallbackInfo { + .env = env, + .ability = GetGlobalAbility(env), + .asyncWork = nullptr, + .deferred = nullptr, + .callback = nullptr, + .formIds = formIds, + .result = 0, + }; if (argc == ARGS_SIZE_TWO) { HILOG_INFO("%{public}s, asyncCallback.", __func__); @@ -1896,7 +2359,7 @@ napi_value NAPI_DisableFormsUpdate(napi_env env, napi_callback_info info) if (asyncCallbackInfo->callback != nullptr) { napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_value callback; napi_value undefined; napi_get_undefined(env, &undefined); @@ -1940,7 +2403,7 @@ napi_value NAPI_DisableFormsUpdate(napi_env env, napi_callback_info info) (AsyncDisableUpdateFormCallbackInfo *)data; napi_value result; - napi_create_int32(env, asyncCallbackInfo->result, &result); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &result); napi_resolve_deferred(asyncCallbackInfo->env, asyncCallbackInfo->deferred, result); napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; @@ -2098,33 +2561,26 @@ static void InnerGetAllFormsInfo(napi_env env, AsyncGetFormsInfoCallbackInfo* co { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - bool ret = ability->GetAllFormsInfo(asyncCallbackInfo->formInfos); - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } + asyncCallbackInfo->result = ability->GetAllFormsInfo(asyncCallbackInfo->formInfos); HILOG_DEBUG("%{public}s, end", __func__); } // NAPI_GetAllFormsInfo callback execute auto NAPI_GetAllFormsInfoAsyncExecute = [](napi_env env, void *data) { HILOG_INFO("NAPI_GetAllFormsInfo execute callback"); - AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoCallbackInfo *)data; + AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = (AsyncGetFormsInfoCallbackInfo *)data; InnerGetAllFormsInfo(env, asyncCallbackInfo); }; // NAPI_GetFormsInfo callback complete auto NAPI_GetFormsInfoAsyncComplete = [](napi_env env, napi_status status, void *data) { HILOG_INFO("NAPI_GetFormsInfo compeleted callback"); - AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoCallbackInfo *)data; + AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = (AsyncGetFormsInfoCallbackInfo *)data; napi_value arrayFormInfos; napi_create_array(env, &arrayFormInfos); - if (asyncCallbackInfo->result) { + if (asyncCallbackInfo->result == ERR_OK) { int iFormInfoCount = 0; - for (auto formInfo : asyncCallbackInfo->formInfos) { + for (auto formInfo : asyncCallbackInfo->formInfos) { napi_value formInfoObject = nullptr; napi_create_object(env, &formInfoObject); ParseFormInfoIntoNapi(env, formInfo, formInfoObject); @@ -2136,9 +2592,15 @@ auto NAPI_GetFormsInfoAsyncComplete = [](napi_env env, napi_status status, void napi_value callbackValues[2] = {0}; napi_value callback; napi_value resultCode; - napi_create_int32(env, asyncCallbackInfo->result, &resultCode); - callbackValues[0] = resultCode; - callbackValues[1] = arrayFormInfos; + ErrCode errCode = asyncCallbackInfo->result; + if (errCode == ERR_OK) { + napi_create_int32(env, errCode, &resultCode); + callbackValues[0] = resultCode; + callbackValues[1] = arrayFormInfos; + } else { + InnerCreateRetMsg(env, errCode, callbackValues); + } + napi_get_reference_value(env, asyncCallbackInfo->callback, &callback); napi_value callResult; napi_call_function(env, nullptr, callback, ARGS_SIZE_TWO, callbackValues, &callResult); @@ -2151,13 +2613,12 @@ auto NAPI_GetFormsInfoAsyncComplete = [](napi_env env, napi_status status, void // NAPI_GetFormsInfo promise Complete auto NAPI_GetFormsInfoPromiseComplete = [](napi_env env, napi_status status, void *data) { HILOG_INFO("%{public}s, promise complete", __func__); - AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoCallbackInfo *)data; - if (asyncCallbackInfo->result) { + AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = (AsyncGetFormsInfoCallbackInfo *)data; + if (asyncCallbackInfo->result == ERR_OK) { napi_value arrayFormInfos; napi_create_array(env, &arrayFormInfos); int iFormInfoCount = 0; - for (auto formInfo : asyncCallbackInfo->formInfos) { + for (auto formInfo : asyncCallbackInfo->formInfos) { napi_value formInfoObject = nullptr; napi_create_object(env, &formInfoObject); ParseFormInfoIntoNapi(env, formInfo, formInfoObject); @@ -2170,7 +2631,8 @@ auto NAPI_GetFormsInfoPromiseComplete = [](napi_env env, napi_status status, voi arrayFormInfos); } else { napi_value getFormsInfoResult; - napi_create_int32(env, asyncCallbackInfo->result, &getFormsInfoResult); + InnerCreateRetMsg(env, asyncCallbackInfo->result, &getFormsInfoResult); + napi_resolve_deferred( asyncCallbackInfo->env, asyncCallbackInfo->deferred, @@ -2223,7 +2685,7 @@ napi_value NAPI_GetAllFormsInfo(napi_env env, napi_callback_info info) napi_valuetype valueType; NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); NAPI_ASSERT(env, valueType == napi_function, "The arguments[0] type of getAllFormsInfo is incorrect,\ - expected type is function."); + expected type is function."); napi_create_reference(env, argv[0], REF_COUNT, &asyncCallbackInfo->callback); napi_value resourceName; @@ -2272,14 +2734,9 @@ static void InnerGetFormsInfoByApp(napi_env env, AsyncGetFormsInfoCallbackInfo* { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - bool ret = ability->GetFormsInfoByApp( + asyncCallbackInfo->result = ability->GetFormsInfoByApp( asyncCallbackInfo->bundleName, asyncCallbackInfo->formInfos); - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } HILOG_DEBUG("%{public}s, end", __func__); } @@ -2295,17 +2752,10 @@ static void InnerGetFormsInfoByModule(napi_env env, AsyncGetFormsInfoCallbackInf { HILOG_DEBUG("%{public}s called.", __func__); OHOS::AppExecFwk::Ability *ability = asyncCallbackInfo->ability; - - bool ret = ability->GetFormsInfoByModule( + asyncCallbackInfo->result = ability->GetFormsInfoByModule( asyncCallbackInfo->bundleName, asyncCallbackInfo->moduleName, asyncCallbackInfo->formInfos); - - if (ret) { - asyncCallbackInfo->result = 1; - } else { - asyncCallbackInfo->result = 0; - } HILOG_DEBUG("%{public}s, end", __func__); } @@ -2313,7 +2763,7 @@ static void InnerGetFormsInfoByModule(napi_env env, AsyncGetFormsInfoCallbackInf auto NAPI_GetFormsInfoByModuleAsyncExecute = [](napi_env env, void *data) { HILOG_INFO("NAPI_GetFormsInfo byModule execute callback"); AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoCallbackInfo *)data; + (AsyncGetFormsInfoCallbackInfo *)data; InnerGetFormsInfoByModule(env, asyncCallbackInfo); }; @@ -2321,7 +2771,7 @@ auto NAPI_GetFormsInfoByModuleAsyncExecute = [](napi_env env, void *data) { auto NAPI_GetFormsInfoByAppAsyncExecute = [](napi_env env, void *data) { HILOG_INFO("NAPI_GetFormsInfo byApp execute callback"); AsyncGetFormsInfoCallbackInfo *asyncCallbackInfo = - (AsyncGetFormsInfoCallbackInfo *)data; + (AsyncGetFormsInfoCallbackInfo *)data; InnerGetFormsInfoByApp(env, asyncCallbackInfo); }; @@ -2358,8 +2808,7 @@ napi_value GetFormsInfoPromise(napi_env env, AsyncGetFormsInfoCallbackInfo *asyn env, nullptr, resourceName, - isByApp ? NAPI_GetFormsInfoByAppAsyncExecute : - NAPI_GetFormsInfoByModuleAsyncExecute, + isByApp ? NAPI_GetFormsInfoByAppAsyncExecute : NAPI_GetFormsInfoByModuleAsyncExecute, NAPI_GetFormsInfoPromiseComplete, (void *)asyncCallbackInfo, &asyncCallbackInfo->asyncWork); @@ -2375,8 +2824,6 @@ napi_value GetFormsInfoThreeArgv(napi_env env, napi_value *argv, AsyncGetFormsIn // Check the value type of the arguments napi_valuetype valueType; NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_ONE], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[1] type of getFormsInfo is incorrect,\ - expected type is string."); std::string moduleNameInfo = GetStringFromNAPI(env, argv[ARGS_SIZE_ONE]); HILOG_INFO("%{public}s, moduleNameInfo=%{public}s.", __func__, moduleNameInfo.c_str()); asyncCallbackInfo->moduleName = moduleNameInfo; @@ -2385,7 +2832,7 @@ napi_value GetFormsInfoThreeArgv(napi_env env, napi_value *argv, AsyncGetFormsIn valueType = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_TWO], &valueType)); NAPI_ASSERT(env, valueType == napi_function, "The arguments[2] type of getFormsInfo is incorrect,\ - expected type is function."); + expected type is function."); napi_create_reference(env, argv[ARGS_SIZE_TWO], REF_COUNT, &asyncCallbackInfo->callback); return GetFormsInfoCallback(env, asyncCallbackInfo, false); } @@ -2401,10 +2848,10 @@ napi_value GetFormsInfoTwoArgv(napi_env env, napi_value *argv, AsyncGetFormsInfo // GetFormsInfoByModule promise if (valueType == napi_string) { - std::string moduleNameInfo = GetStringFromNAPI(env, argv[ARGS_SIZE_ONE]); - HILOG_INFO("%{public}s, moduleNameInfo=%{public}s.", __func__, moduleNameInfo.c_str()); - asyncCallbackInfo->moduleName = moduleNameInfo; - return GetFormsInfoPromise(env, asyncCallbackInfo, false); + std::string moduleNameInfo = GetStringFromNAPI(env, argv[ARGS_SIZE_ONE]); + HILOG_INFO("%{public}s, moduleNameInfo=%{public}s.", __func__, moduleNameInfo.c_str()); + asyncCallbackInfo->moduleName = moduleNameInfo; + return GetFormsInfoPromise(env, asyncCallbackInfo, false); } else if (valueType == napi_function) { // GetFormsInfoByApp callback HILOG_INFO("%{public}s, asyncCallback.", __func__); @@ -2412,12 +2859,12 @@ napi_value GetFormsInfoTwoArgv(napi_env env, napi_value *argv, AsyncGetFormsInfo valueType = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_ONE], &valueType)); NAPI_ASSERT(env, valueType == napi_function, "The arguments[1] type of getFormsInfo is incorrect,\ - expected type is function."); + expected type is function."); napi_create_reference(env, argv[ARGS_SIZE_ONE], REF_COUNT, &asyncCallbackInfo->callback); return GetFormsInfoCallback(env, asyncCallbackInfo, true); } else { NAPI_ASSERT(env, false, "The arguments[1] type of getFormsInfo is incorrect,\ - expected type is string or function."); + expected type is string or function."); return NapiGetResut(env, 1); } } @@ -2447,9 +2894,6 @@ napi_value NAPI_GetFormsInfo(napi_env env, napi_callback_info info) // Check the value type of the arguments napi_valuetype valueType; NAPI_CALL(env, napi_typeof(env, argv[ARGS_SIZE_ZERO], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "The arguments[0] type of getFormsInfo is incorrect,\ - expected type is string."); - std::string bundleNameInfo = GetStringFromNAPI(env, argv[ARGS_SIZE_ZERO]); HILOG_INFO("%{public}s, bundleName=%{public}s.", __func__, bundleNameInfo.c_str()); diff --git a/interfaces/kits/napi/aafwk/formManager/napi_form_manager.h b/interfaces/kits/napi/aafwk/formManager/napi_form_manager.h index 8f301b44b56..cc394da4600 100644 --- a/interfaces/kits/napi/aafwk/formManager/napi_form_manager.h +++ b/interfaces/kits/napi/aafwk/formManager/napi_form_manager.h @@ -17,6 +17,7 @@ #define FORM_ABILITY_H_ #include "ability.h" +#include "appexecfwk_errors.h" #include "form_info.h" #include "form_js_info.h" #include "form_provider_info.h" @@ -151,6 +152,16 @@ struct AsyncGetFormsInfoCallbackInfo { int result; }; +struct AsyncErrMsgCallbackInfo { + napi_env env; + napi_async_work asyncWork; + napi_deferred deferred; + napi_ref callback; + napi_value callbackValue; + int code; + int type; +}; + napi_value NAPI_DeleteForm(napi_env env, napi_callback_info info); napi_value NAPI_ReleaseForm(napi_env env, napi_callback_info info); napi_value NAPI_RequestForm(napi_env env, napi_callback_info info); diff --git a/services/formmgr/include/form_mgr_service.h b/services/formmgr/include/form_mgr_service.h index 400a368f640..64ffde7049a 100644 --- a/services/formmgr/include/form_mgr_service.h +++ b/services/formmgr/include/form_mgr_service.h @@ -233,7 +233,7 @@ private: */ ErrCode Init(); - bool CheckFormPermission(); + ErrCode CheckFormPermission(); private: ServiceRunningState state_; diff --git a/services/formmgr/src/form_mgr_service.cpp b/services/formmgr/src/form_mgr_service.cpp index cf1ed09d0d2..a943be78f8b 100644 --- a/services/formmgr/src/form_mgr_service.cpp +++ b/services/formmgr/src/form_mgr_service.cpp @@ -92,9 +92,10 @@ bool FormMgrService::IsReady() const int FormMgrService::AddForm(const int64_t formId, const Want &want, const sptr &callerToken, FormJsInfo &formInfo) { - if (!CheckFormPermission()) { + ErrCode ret = CheckFormPermission(); + if (ret != ERR_OK) { APP_LOGE("%{public}s fail, add form permission denied", __func__); - return ERR_APPEXECFWK_FORM_PERMISSION_DENY; + return ret; } return FormMgrAdapter::GetInstance().AddForm(formId, want, callerToken, formInfo); } @@ -107,9 +108,10 @@ int FormMgrService::AddForm(const int64_t formId, const Want &want, */ int FormMgrService::DeleteForm(const int64_t formId, const sptr &callerToken) { - if (!CheckFormPermission()) { + ErrCode ret = CheckFormPermission(); + if (ret != ERR_OK) { APP_LOGE("%{public}s fail, delete form permission denied", __func__); - return ERR_APPEXECFWK_FORM_PERMISSION_DENY; + return ret; } return FormMgrAdapter::GetInstance().DeleteForm(formId, callerToken); @@ -124,9 +126,10 @@ int FormMgrService::DeleteForm(const int64_t formId, const sptr & */ int FormMgrService::ReleaseForm(const int64_t formId, const sptr &callerToken, const bool delCache) { - if (!CheckFormPermission()) { + ErrCode ret = CheckFormPermission(); + if (ret != ERR_OK) { APP_LOGE("%{public}s fail, release form permission denied", __func__); - return ERR_APPEXECFWK_FORM_PERMISSION_DENY; + return ret; } return FormMgrAdapter::GetInstance().ReleaseForm(formId, callerToken, delCache); @@ -156,9 +159,10 @@ int FormMgrService::RequestForm(const int64_t formId, const sptr { APP_LOGI("%{public}s called.", __func__); - if (!CheckFormPermission()) { + ErrCode ret = CheckFormPermission(); + if (ret != ERR_OK) { APP_LOGE("%{public}s fail, request form permission denied", __func__); - return ERR_APPEXECFWK_FORM_PERMISSION_DENY; + return ret; } return FormMgrAdapter::GetInstance().RequestForm(formId, callerToken, want); @@ -189,9 +193,10 @@ int FormMgrService::SetNextRefreshTime(const int64_t formId, const int64_t nextT int FormMgrService::NotifyWhetherVisibleForms(const std::vector &formIds, const sptr &callerToken, const int32_t formVisibleType) { - if (!CheckFormPermission()) { + ErrCode ret = CheckFormPermission(); + if (ret != ERR_OK) { APP_LOGE("%{public}s fail, event notify visible permission denied", __func__); - return ERR_APPEXECFWK_FORM_PERMISSION_DENY; + return ret; } return FormMgrAdapter::GetInstance().NotifyWhetherVisibleForms(formIds, callerToken, formVisibleType); @@ -205,9 +210,10 @@ int FormMgrService::NotifyWhetherVisibleForms(const std::vector &formId */ int FormMgrService::CastTempForm(const int64_t formId, const sptr &callerToken) { - if (!CheckFormPermission()) { + ErrCode ret = CheckFormPermission(); + if (ret != ERR_OK) { APP_LOGE("%{public}s fail, cast temp form permission denied", __func__); - return ERR_APPEXECFWK_FORM_PERMISSION_DENY; + return ret; } return FormMgrAdapter::GetInstance().CastTempForm(formId, callerToken); @@ -225,9 +231,10 @@ int FormMgrService::LifecycleUpdate(const std::vector &formIds, { APP_LOGI("lifecycleUpdate."); - if (!CheckFormPermission()) { + ErrCode ret = CheckFormPermission(); + if (ret != ERR_OK) { APP_LOGE("%{public}s fail, delete form permission denied", __func__); - return ERR_APPEXECFWK_FORM_PERMISSION_DENY; + return ret; } if (updateType == ENABLE_FORM_UPDATE) { @@ -285,9 +292,10 @@ int FormMgrService::DumpFormTimerByFormId(const std::int64_t formId, std::string int FormMgrService::MessageEvent(const int64_t formId, const Want &want, const sptr &callerToken) { APP_LOGI("%{public}s called.", __func__); - if (!CheckFormPermission()) { + ErrCode ret = CheckFormPermission(); + if (ret != ERR_OK) { APP_LOGE("%{public}s fail, request form permission denied", __func__); - return ERR_APPEXECFWK_FORM_PERMISSION_DENY; + return ret; } return FormMgrAdapter::GetInstance().MessageEvent(formId, want, callerToken); } @@ -392,19 +400,33 @@ ErrCode FormMgrService::Init() return ERR_OK; } -bool FormMgrService::CheckFormPermission() +ErrCode FormMgrService::CheckFormPermission() { auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall(); if (isSaCall) { - return true; + return ERR_OK; } + + // get IBundleMgr + sptr iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("%{public}s error, failed to get IBundleMgr.", __func__); + return ERR_APPEXECFWK_FORM_GET_BMS_FAILED; + } + + // check if system appint + auto isSystemApp = iBundleMgr->CheckIsSystemAppByUid(IPCSkeleton::GetCallingUid()); + if (!isSystemApp) { + return ERR_APPEXECFWK_FORM_PERMISSION_DENY_SYS; + } + auto isCallingPerm = AAFwk::PermissionVerification::GetInstance()->VerifyCallingPermission( AppExecFwk::Constants::PERMISSION_REQUIRE_FORM); - if (isCallingPerm) { - return true; + if (!isCallingPerm) { + return ERR_APPEXECFWK_FORM_PERMISSION_DENY; } - APP_LOGE("Permission verification failed"); - return false; + APP_LOGI("Permission verification ok!"); + return ERR_OK; } /** -- Gitee