From c090bbac32e72d69554b1fea9c5b68eeafdf3e3a Mon Sep 17 00:00:00 2001 From: zhaoyuan Date: Wed, 15 Sep 2021 19:14:08 +0000 Subject: [PATCH] add service ability code Signed-off-by: zhaoyuan --- .../kits/ability/native/include/ability.h | 11 +- .../ability/native/include/ability_impl.h | 2 +- .../ability/native/include/ability_loader.h | 4 +- .../ability/native/include/ipc_singleton.h | 8 +- .../kits/ability/native/src/ability.cpp | 191 +- .../ability/native/src/ability_context.cpp | 13 +- .../ability/native/src/ability_process.cpp | 8 +- .../ability/native/src/ability_thread.cpp | 30 + .../ability/native/src/page_ability_impl.cpp | 1 + ...mock_ability_manager_client_interface1.cpp | 2 +- .../mock_resourceManager_interface1.cpp | 8 +- .../ability_context_interface1_test.cpp | 12 - .../test/unittest/ability_impl_test.cpp | 8 +- .../unittest/data_ability_operation_test.cpp | 6 +- .../unittest/data_ability_result_test.cpp | 90 +- .../cpp/test/unittest/common/base_test.cpp | 53 +- .../test/unittest/common/operation_test.cpp | 1 - .../cpp/test/unittest/common/skills_test.cpp | 15 +- .../data_ability_operation_module_test.cpp | 188 +- .../kits/js/@ohos.ability.featureAbility.d.ts | 89 +- .../js/@ohos.ability.particleAbility.d.ts | 22 +- .../kits/js/@ohos.ability.wantConstant.d.ts | 2 +- .../kits/js/@ohos.app.abilityManager.d.ts | 10 +- interfaces/kits/js/@ohos.bundle.d.ts | 101 +- .../kits/js/ability/dataAbilityHelper.d.ts | 2 +- .../js/ability/startAbilityParameter.d.ts | 6 +- interfaces/kits/js/ability/want.d.ts | 2 +- .../kits/js/app/abilityMissionInfo.d.ts | 54 +- interfaces/kits/js/app/activeProcessInfo.d.ts | 3 +- .../kits/js/app/activeServiceAbilityInfo.d.ts | 55 + interfaces/kits/js/app/context.d.ts | 97 +- interfaces/kits/js/app/memoryMapInfo.d.ts | 63 + .../app/missionSnapshot.d.ts} | 24 +- interfaces/kits/js/app/processErrorInfo.d.ts | 56 + interfaces/kits/js/app/processInfo.d.ts | 44 - interfaces/kits/js/app/splitMissionInfo.d.ts | 47 + interfaces/kits/js/app/systemMemoryAttr.d.ts | 44 + interfaces/kits/js/bundle/hapModuleInfo.d.ts | 116 + interfaces/kits/napi/aafwk/context/BUILD.gn | 51 - .../kits/napi/aafwk/context/napi_context.cpp | 876 ----- .../kits/napi/aafwk/context/napi_context.h | 205 -- .../kits/napi/aafwk/featureAbility/BUILD.gn | 1 + .../aafwk/featureAbility/feature_ability.cpp | 264 +- .../aafwk/featureAbility/feature_ability.h | 79 +- .../feature_ability_constant.cpp | 73 + .../feature_ability_constant.h} | 48 +- .../aafwk/featureAbility/napi_context.cpp | 1897 +++++++++- .../napi/aafwk/featureAbility/napi_context.h | 51 +- .../napi_data_ability_helper.cpp | 256 +- .../featureAbility/napi_data_ability_helper.h | 31 +- .../aafwk/featureAbility/native_module.cpp | 2 + .../napi_common/feature_ability_common.h | 5 + .../inner/napi_common/napi_common_ability.cpp | 3179 +++++------------ .../inner/napi_common/napi_common_ability.h | 79 +- .../particleAbility/particle_ability.cpp | 81 +- ohos.build | 3 +- services/abilitymgr/BUILD.gn | 15 +- services/abilitymgr/abilitymgr.gni | 1 + .../include/ability_manager_service.h | 24 +- .../include/ams_configuration_parameter.h | 85 + .../resource/ams_service_config.json | 8 + .../src/ability_manager_service.cpp | 151 +- .../abilitymgr/src/ability_stack_manager.cpp | 6 +- .../src/ams_configuration_parameter.cpp | 97 + .../ability_manager_service_test.cpp | 46 + .../app_scheduler_test/app_scheduler_test.cpp | 51 +- .../ability_mgr_module_test.cpp | 47 + tools/zip/kits/napi/napi_zlib.cpp | 40 +- tools/zip/src/file_path.cpp | 24 +- tools/zip/src/zip.cpp | 4 + 70 files changed, 4631 insertions(+), 4637 deletions(-) mode change 100755 => 100644 interfaces/kits/js/@ohos.ability.featureAbility.d.ts mode change 100644 => 100755 interfaces/kits/js/app/abilityMissionInfo.d.ts mode change 100644 => 100755 interfaces/kits/js/app/activeProcessInfo.d.ts create mode 100755 interfaces/kits/js/app/activeServiceAbilityInfo.d.ts create mode 100755 interfaces/kits/js/app/memoryMapInfo.d.ts rename interfaces/kits/{napi/aafwk/context/napi_error.h => js/app/missionSnapshot.d.ts} (52%) mode change 100644 => 100755 create mode 100755 interfaces/kits/js/app/processErrorInfo.d.ts delete mode 100644 interfaces/kits/js/app/processInfo.d.ts create mode 100755 interfaces/kits/js/app/splitMissionInfo.d.ts create mode 100755 interfaces/kits/js/app/systemMemoryAttr.d.ts create mode 100644 interfaces/kits/js/bundle/hapModuleInfo.d.ts delete mode 100644 interfaces/kits/napi/aafwk/context/BUILD.gn delete mode 100644 interfaces/kits/napi/aafwk/context/napi_context.cpp delete mode 100644 interfaces/kits/napi/aafwk/context/napi_context.h create mode 100644 interfaces/kits/napi/aafwk/featureAbility/feature_ability_constant.cpp rename interfaces/kits/napi/aafwk/{context/native_module.cpp => featureAbility/feature_ability_constant.h} (49%) mode change 100644 => 100755 ohos.build create mode 100644 services/abilitymgr/include/ams_configuration_parameter.h create mode 100644 services/abilitymgr/resource/ams_service_config.json create mode 100644 services/abilitymgr/src/ams_configuration_parameter.cpp diff --git a/frameworks/kits/ability/native/include/ability.h b/frameworks/kits/ability/native/include/ability.h index 42140a34c30..a02f029c2d5 100755 --- a/frameworks/kits/ability/native/include/ability.h +++ b/frameworks/kits/ability/native/include/ability.h @@ -74,6 +74,8 @@ class Ability : public IAbilityEvent, public IAbilityContinuation, public std::enable_shared_from_this { public: + friend class PageAbilityImpl; + Ability() = default; virtual ~Ability() = default; @@ -724,7 +726,6 @@ public: */ virtual ContinuationState GetContinuationState() final; - /** * @brief Obtains the singleton AbilityPackage object to which this ability belongs. * @@ -1280,6 +1281,13 @@ private: bool VerifySupportForContinuation(); void HandleCreateAsContinuation(const Want &want); bool IsFlagExists(int flag, int flagSet); + /** + * @brief Set the start ability setting. + * @param setting the start ability setting. + */ + void SetStartAbilitySetting(std::shared_ptr setting); + +private: std::shared_ptr continuationHandler_ = nullptr; std::shared_ptr continuationManager_ = nullptr; std::shared_ptr continuationRegisterManager_ = nullptr; @@ -1293,6 +1301,7 @@ private: std::shared_ptr abilityWindow_ = nullptr; std::shared_ptr setWant_ = nullptr; sptr reverseContinuationSchedulerReplica_ = nullptr; + std::shared_ptr setting_ = nullptr; bool bWindowFocus_ = false; static const std::string SYSTEM_UI; diff --git a/frameworks/kits/ability/native/include/ability_impl.h b/frameworks/kits/ability/native/include/ability_impl.h index 357929720e1..cc00a6e6d0e 100644 --- a/frameworks/kits/ability/native/include/ability_impl.h +++ b/frameworks/kits/ability/native/include/ability_impl.h @@ -154,7 +154,7 @@ public: * @param resultData Indicates the data returned after the ability is destroyed. You can define the data * returned. This parameter can be null. */ - void SendResult(int requestCode, int resultCode, /*const AAFwk::Want& resultData*/ const Want &resultData); + void SendResult(int requestCode, int resultCode, const Want &resultData); /** * @brief Called when the launch mode of an ability is set to singleInstance. This happens when you re-launch diff --git a/frameworks/kits/ability/native/include/ability_loader.h b/frameworks/kits/ability/native/include/ability_loader.h index 6a8460601e2..58d9f49f582 100644 --- a/frameworks/kits/ability/native/include/ability_loader.h +++ b/frameworks/kits/ability/native/include/ability_loader.h @@ -88,7 +88,7 @@ private: __attribute__((constructor)) void RegisterAA##className() \ { \ AbilityLoader::GetInstance().RegisterAbility( \ - #className, []() -> Ability * { return new (std::nothrow) className; }); \ + #className, []()->Ability * { return new (std::nothrow) className; }); \ } /** @@ -104,7 +104,7 @@ private: __attribute((constructor)) void RegisterAS##className() \ { \ AbilityLoader::GetInstance().RegisterAbilitySlice( \ - #className, []() -> AbilitySlice * { return new (std::nothrow) className; }); + #className, []()->AbilitySlice * { return new (std::nothrow) className; }); } #endif } // namespace OHOS diff --git a/frameworks/kits/ability/native/include/ipc_singleton.h b/frameworks/kits/ability/native/include/ipc_singleton.h index 758b13a4d6b..fe36d161a84 100644 --- a/frameworks/kits/ability/native/include/ipc_singleton.h +++ b/frameworks/kits/ability/native/include/ipc_singleton.h @@ -29,7 +29,7 @@ private: \ friend DelayedIPCSingleton; \ MyClass(); -template +template class DelayedIPCSingleton : public NoCopyable { public: static sptr GetInstance() @@ -51,13 +51,13 @@ private: static std::mutex mutex_; }; -template +template sptr DelayedIPCSingleton::instance_ = nullptr; -template +template std::mutex DelayedIPCSingleton::mutex_; -template +template void DelayedIPCSingleton::DestroyInstance() { std::lock_guard lock(mutex_); diff --git a/frameworks/kits/ability/native/src/ability.cpp b/frameworks/kits/ability/native/src/ability.cpp index 78460c2a762..d0bd8099c98 100755 --- a/frameworks/kits/ability/native/src/ability.cpp +++ b/frameworks/kits/ability/native/src/ability.cpp @@ -151,8 +151,7 @@ void Ability::OnStart(const Want &want) winType = OHOS::WindowType::WINDOW_TYPE_ALARM_SCREEN; } - if (abilityInfo_->bundleName == DEVICE_MANAGER_BUNDLE_NAME && - abilityInfo_->name == DEVICE_MANAGER_NAME) { + if (abilityInfo_->bundleName == DEVICE_MANAGER_BUNDLE_NAME && abilityInfo_->name == DEVICE_MANAGER_NAME) { winType = OHOS::WindowType::WINDOW_TYPE_ALARM_SCREEN; } @@ -162,6 +161,18 @@ void Ability::OnStart(const Want &want) abilityInfo_->name.c_str(), winType); + if (setting_ != nullptr) { + auto windowMode = static_cast( + std::atoi(setting_->GetProperty(AbilityStartSetting::WINDOW_MODE_KEY).c_str())); + APP_LOGI("%{public}s windowMode : %{public}d", __func__, windowMode); + if (windowMode == AbilityWindowConfiguration::MULTI_WINDOW_DISPLAY_FLOATING) { + APP_LOGI("%{public}s begin SetWindowMode : WINDOW_MODE_FREE.", __func__); + config->SetWindowType(WINDOW_TYPE_FLOAT); + APP_LOGI("%{public}s end SetWindowMode : WINDOW_MODE_FREE.", __func__); + } + } else { + APP_LOGI("Ability::OnStart setting_ == nullptr."); + } SetUIContent(config); if (abilityWindow_ != nullptr) { @@ -170,7 +181,6 @@ void Ability::OnStart(const Want &want) APP_LOGI("%{public}s end abilityWindow_->OnPostAbilityStart.", __func__); } } - // should called in ace ability onStart methord. SetWant(want); if (abilityLifecycleExecutor_ == nullptr) { @@ -1455,7 +1465,7 @@ bool Ability::OnSaveData(WantParams &saveData) } /** - * @brief After creating the Ability on the remote device, + * @brief After creating the Ability on the remote device, * immediately restore the user data saved during the migration of the Ability on the remote device. * @param restoreData Indicates the user data to be restored. * @return If the data is restored successfully, it returns true; otherwise, it returns false . @@ -1638,7 +1648,7 @@ bool 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 - return DeleteForm(formId, isReleaseCache ? RELEASE_CACHED_FORM : RELEASE_FORM); + return DeleteForm(formId, isReleaseCache ? RELEASE_CACHED_FORM : RELEASE_FORM); } /** @@ -1672,12 +1682,12 @@ bool Ability::DeleteForm(const int64_t formId) * @brief Cast temp form with formId. * * @param formId Indicates the form's ID. - * + * * @return Returns {@code true} if the form is successfully casted; returns {@code false} otherwise. */ bool Ability::CastTempForm(const int64_t formId) { - APP_LOGI("%{public}s start",__func__); + 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; @@ -1694,7 +1704,7 @@ bool Ability::CastTempForm(const int64_t formId) userReqParams_[formId].SetParam(Constants::PARAM_FORM_TEMPORARY_KEY, false); - APP_LOGI("%{public}s end",__func__); + APP_LOGI("%{public}s end", __func__); return true; } @@ -1741,7 +1751,12 @@ bool Ability::AcquireForm(const int64_t formId, const Want &want, const std::sha ElementName elementName = want.GetElement(); std::string bundleName = elementName.GetBundleName(); std::string abilityName = elementName.GetAbilityName(); - APP_LOGI("%{public}s, begin to acquire form, bundleName is %{public}s, abilityName is %{public}s, formId is %{public}" PRId64 ".",__func__, bundleName.c_str(), abilityName.c_str(), formId); + APP_LOGI("%{public}s, begin to acquire form, bundleName is %{public}s, abilityName is %{public}s, formId is " + "%{public}" PRId64 ".", + __func__, + bundleName.c_str(), + abilityName.c_str(), + formId); // hostClient init sptr formHostClient = FormHostClient::GetInstance(); @@ -1757,7 +1772,8 @@ bool Ability::AcquireForm(const int64_t formId, const Want &want, const std::sha return false; } APP_LOGI("%{public}s, end to acquire form, the formId returned from the fms is %{public}" PRId64 ".", - __func__, formJsInfo.formId); + __func__, + formJsInfo.formId); // check for form presence in hostForms if (formHostClient->ContainsForm(formJsInfo.formId)) { @@ -1770,7 +1786,7 @@ bool Ability::AcquireForm(const int64_t formId, const Want &want, const std::sha formHostClient->AddForm(thisAbility, formJsInfo.formId); // post the async task of handleAcquireResult - PostTask([this, want, formJsInfo, callback]() {HandleAcquireResult(want, formJsInfo, callback);}, 0L); + PostTask([this, want, formJsInfo, callback]() { HandleAcquireResult(want, formJsInfo, callback); }, 0L); // the acquire form is successfully return true; @@ -1850,14 +1866,14 @@ bool Ability::NotifyInvisibleForms(const std::vector &formIds) } /** -* @brief Set form next refresh time. -* -*

This method is called by a form provider to set refresh time. -* -* @param formId Indicates the ID of the form to set refresh time. -* @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. -*/ + * @brief Set form next refresh time. + * + *

This method is called by a form provider to set refresh time. + * + * @param formId Indicates the ID of the form to set refresh time. + * @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) { APP_LOGI("%{public}s called.", __func__); @@ -1909,22 +1925,21 @@ void Ability::ProcessFormUpdate(const FormJsInfo &formJsInfo) // post the async task of handleFormMessage int32_t msgCode = OHOS_FORM_UPDATE_FORM; - PostTask([this, msgCode, formJsInfo]() {HandleFormMessage(msgCode, formJsInfo);}, 0L); + PostTask([this, msgCode, formJsInfo]() { HandleFormMessage(msgCode, formJsInfo); }, 0L); } /** * @brief Uninstall form. * * @param formId Indicates the ID of the form to uninstall. */ -void Ability::ProcessFormUninstall(const int64_t formId) { +void Ability::ProcessFormUninstall(const int64_t formId) +{ APP_LOGI("%{public}s start.", __func__); std::shared_ptr formCallback = nullptr; { std::lock_guard lock(formLock); // get callback iterator by formId - std::map>::iterator appCallbackIterator = - appCallbacks_.find(formId); - + std::map>::iterator appCallbackIterator = appCallbacks_.find(formId); // call the callback function when you need to be notified if (appCallbackIterator == appCallbacks_.end()) { @@ -1934,7 +1949,7 @@ void Ability::ProcessFormUninstall(const int64_t formId) { formCallback = appCallbackIterator->second; CleanFormResource(formId); } - if ( formCallback == nullptr) { + if (formCallback == nullptr) { APP_LOGE("%{public}s failed, callback is nullptr.", __func__); return; } @@ -1956,7 +1971,7 @@ void Ability::ProcessFormUninstall(const int64_t formId) { * Ability#PARAM_FORM_NAME_KEY, and Ability#PARAM_FORM_DIMENSION_KEY, * respectively. Such form information must be managed as persistent data for further form * acquisition, update, and deletion. - * + * * @return Returns the created FormProviderInfo object. */ FormProviderInfo Ability::OnCreate(const Want &want) @@ -1983,8 +1998,7 @@ void Ability::OnDelete(const int64_t formId) * @return none. */ void Ability::OnUpdate(const int64_t formId) -{ -} +{} /** * @brief Called when the form provider is notified that a temporary form is successfully converted to a normal form. @@ -2005,8 +2019,7 @@ void Ability::OnCastTemptoNormal(const int64_t formId) * @return none. */ void Ability::OnVisibilityChanged(const std::map &formEventsMap) -{ -} +{} /** * @brief Called to notify the form provider to update a specified form. * @@ -2014,8 +2027,7 @@ void Ability::OnVisibilityChanged(const std::map &formEventsMa * @param message Form event message. */ void Ability::OnTriggerEvent(const int64_t formId, const std::string &message) -{ -} +{} /** * @brief Delete or release form with formId. * @@ -2031,22 +2043,22 @@ bool Ability::DeleteForm(const int64_t formId, const int32_t deleteType) APP_LOGE("%{public}s error, form is in recover status, can't do action on form.", __func__); return false; } - // check formId + // check formId if (formId <= 0) { APP_LOGE("%{public}s error, the passed in formId can't be negative or zero.", __func__); return false; } APP_LOGI("%{public}s, delete form begin, formId is %{public}" PRId64 " and deleteType is %{public}d.", - __func__, formId, deleteType); + __func__, + formId, + deleteType); { // form lock std::lock_guard lock(formLock); // clean form resource when form is temp form - if (std::find( - lostedByReconnectTempForms_.begin(), - lostedByReconnectTempForms_.end(), - formId) != lostedByReconnectTempForms_.end()) { + if (std::find(lostedByReconnectTempForms_.begin(), lostedByReconnectTempForms_.end(), formId) != + lostedByReconnectTempForms_.end()) { CleanFormResource(formId); // the delete temp form is successfully return true; @@ -2061,9 +2073,7 @@ bool Ability::DeleteForm(const int64_t formId, const int32_t deleteType) result = FormMgr::GetInstance().DeleteForm(formId, formHostClient); } else { result = FormMgr::GetInstance().ReleaseForm( - formId, - formHostClient, - (deleteType == RELEASE_CACHED_FORM) ? true : false); + formId, formHostClient, (deleteType == RELEASE_CACHED_FORM) ? true : false); } if (result != ERR_OK) { APP_LOGE("%{public}s error, some internal server occurs, error code is %{public}d.", __func__, result); @@ -2084,11 +2094,11 @@ bool Ability::DeleteForm(const int64_t formId, const int32_t deleteType) * * @param formId Indicates the form's ID. */ -void Ability::CleanFormResource(const int64_t formId) +void Ability::CleanFormResource(const int64_t formId) { APP_LOGI("%{public}s called.", __func__); // compatible with int form id - int64_t cleanId {-1L}; + int64_t cleanId{-1L}; for (auto param : userReqParams_) { if ((param.first & 0x00000000ffffffffL) == (formId & 0x00000000ffffffffL)) { cleanId = param.first; @@ -2112,7 +2122,6 @@ void Ability::CleanFormResource(const int64_t formId) std::shared_ptr thisAbility = this->shared_from_this(); FormHostClient::GetInstance()->RemoveForm(thisAbility, cleanId); - // unregister death callback when appCallbacks is empty if (appCallbacks_.empty()) { std::shared_ptr thisAbility = this->shared_from_this(); @@ -2130,9 +2139,7 @@ void Ability::CleanFormResource(const int64_t formId) * @param callback Indicates the callback to be invoked whenever the {@link FormJsInfo} instance is obtained. */ void Ability::HandleAcquireResult( - const Want &want, - const FormJsInfo &formJsInfo, - const std::shared_ptr callback) + const Want &want, const FormJsInfo &formJsInfo, const std::shared_ptr callback) { APP_LOGI("%{public}s called.", __func__); { @@ -2172,10 +2179,9 @@ void Ability::HandleFormMessage(const int32_t msgCode, const FormJsInfo &formJsI { std::lock_guard lock(formLock); // get callback iterator by formId - std::map>::iterator appCallbackIterator = + std::map>::iterator appCallbackIterator = appCallbacks_.find(formJsInfo.formId); - // call the callback function when you need to be notified if (appCallbackIterator == appCallbacks_.end()) { APP_LOGE("%{public}s failed, callback not find, formId: %{public}" PRId64 ".", __func__, formJsInfo.formId); @@ -2183,7 +2189,7 @@ void Ability::HandleFormMessage(const int32_t msgCode, const FormJsInfo &formJsI } formCallback = appCallbackIterator->second; } - if ( formCallback == nullptr) { + if (formCallback == nullptr) { APP_LOGE("%{public}s failed, callback is nullptr.", __func__); return; } @@ -2199,7 +2205,7 @@ void Ability::HandleFormMessage(const int32_t msgCode, const FormJsInfo &formJsI /** * @brief Notify the forms visibility change event. - * + * * @param formIds Indicates the IDs of the forms to be made visible or invisible. * @param eventType Indicates the form events occurred. FORM_VISIBLE means that the form becomes visible, * and FORM_INVISIBLE means that the form becomes invisible. @@ -2219,9 +2225,10 @@ bool Ability::NotifyWhetherVisibleForms(const std::vector &formIds, int return false; } - int resultCode = FormMgr::GetInstance().NotifyWhetherVisibleForms(formIds, FormHostClient::GetInstance(), eventType); + int 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); + APP_LOGE("%{public}s error, internal error occurs, error code:%{public}d.", __func__, resultCode); return false; } return true; @@ -2229,7 +2236,7 @@ bool Ability::NotifyWhetherVisibleForms(const std::vector &formIds, int /** * @brief Check the param of want. - * + * * @param formId Indicates the form's ID. * @param want Indicates the detailed information about the form to be obtained, including the bundle name, * module name, ability name, form name, form id, tempForm flag, form dimension, and form customize data. @@ -2299,7 +2306,7 @@ bool Ability::DisableUpdateForm(const std::vector &formIds) return LifecycleUpdate(formIds, DISABLE_FORM_UPDATE); } -bool Ability::LifecycleUpdate(std::vector formIds, int32_t updateType) +bool 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__); @@ -2351,7 +2358,10 @@ bool Ability::RequestForm(const int64_t formId, const Want &want) // requestForm request to fms int 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); + 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; } @@ -2375,9 +2385,8 @@ void Ability::OnDeathReceived() { std::lock_guard lock(formLock); want = userReqRaram.second; - if (want.GetBoolParam(Constants::PARAM_FORM_TEMPORARY_KEY, false) - && std::find(lostedTempForms.begin(), lostedTempForms.end(), - formId) == lostedTempForms.end()) { + if (want.GetBoolParam(Constants::PARAM_FORM_TEMPORARY_KEY, false) && + std::find(lostedTempForms.begin(), lostedTempForms.end(), formId) == lostedTempForms.end()) { lostedTempForms.emplace_back(formId); continue; } @@ -2390,18 +2399,21 @@ void Ability::OnDeathReceived() { std::lock_guard lock(formLock); // get callback iterator by formId - std::map>::iterator appCallbackIterator = appCallbacks_.find(formId); - + std::map>::iterator appCallbackIterator = + appCallbacks_.find(formId); + if (appCallbackIterator == appCallbacks_.end()) { - APP_LOGW("%{public}s error, lack of form callback for form, formId:%{public}" PRId64 ".", __func__, formId); + APP_LOGW("%{public}s error, lack of form callback for form, formId:%{public}" PRId64 ".", + __func__, + formId); continue; } formCallback = appCallbackIterator->second; } - if(formCallback == nullptr) { + if (formCallback == nullptr) { APP_LOGW("%{public}s failed, callback is nullptr.", __func__); continue; - } + } FormJsInfo formJsInfo; formJsInfo.formId = formId; @@ -2412,7 +2424,7 @@ void Ability::OnDeathReceived() /** * @brief Reacquire a specified form when the death callback is received. - * + * * @param formId Indicates the form ID. * @param want Indicates the detailed information about the form to be obtained. * @return Returns true if the request is successfully initiated; returns false otherwise. @@ -2430,8 +2442,8 @@ bool Ability::ReAcquireForm(const int64_t formId, const Want &want) // reacquire form FormJsInfo formJsInfo; - if (FormMgr::GetInstance().AddForm(formId, want, formHostClient, formJsInfo) != ERR_OK - || formJsInfo.formId <= 0 || formJsInfo.formId != formId) { + if (FormMgr::GetInstance().AddForm(formId, want, formHostClient, formJsInfo) != ERR_OK || formJsInfo.formId <= 0 || + formJsInfo.formId != formId) { APP_LOGE("%{public}s error, fms reacquire form failed, formId:%{public}" PRId64 ".", __func__, formId); return false; } @@ -2452,13 +2464,13 @@ bool Ability::CheckFMSReady() APP_LOGI("%{public}s called.", __func__); sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); auto remoteObject = systemAbilityManager->GetSystemAbility(FORM_MGR_SERVICE_ID); if (remoteObject == nullptr) { APP_LOGI("%{public}s, form manager service is not ready.", __func__); return false; } - + return true; } @@ -2477,7 +2489,7 @@ bool Ability::GetAllFormsInfo(std::vector &formInfos) APP_LOGE("%{public}s error, failed to get IBundleMgr.", __func__); return false; } - + if (!CheckPermission()) { return false; } @@ -2506,21 +2518,20 @@ bool Ability::GetFormsInfoByApp(std::string &bundleName, std::vector & APP_LOGE("%{public}s error, failed to get IBundleMgr.", __func__); return IsGetFormsInfoByApp; } - + if (!CheckPermission()) { return IsGetFormsInfoByApp; } - + IsGetFormsInfoByApp = iBundleMgr->GetFormsInfoByApp(bundleName, formInfos); - if (formInfos.size() == 0){ + if (formInfos.size() == 0) { return IsGetFormsInfoByApp; } - + return IsGetFormsInfoByApp; } - - /** +/** * @brief Get forms info by application name and module name. * * @param bundleName Application name. @@ -2547,9 +2558,9 @@ bool Ability::GetFormsInfoByModule(std::string &bundleName, std::string &moduleN if (!CheckPermission()) { return IsGetFormsInfoByModule; } - + IsGetFormsInfoByModule = iBundleMgr->GetFormsInfoByModule(bundleName, moduleName, formInfos); - + return IsGetFormsInfoByModule; } @@ -2563,7 +2574,7 @@ sptr Ability::GetBundleMgr() if (iBundleMgr_ == nullptr) { sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); if (remoteObject == nullptr) { APP_LOGE("%{public}s error, failed to get bundle manager service.", __func__); @@ -2571,12 +2582,12 @@ sptr Ability::GetBundleMgr() } iBundleMgr_ = iface_cast(remoteObject); - if (iBundleMgr_ == nullptr) { + if (iBundleMgr_ == nullptr) { APP_LOGE("%{public}s error, failed to get bundle manager service", __func__); - return nullptr; + return nullptr; } } - + return iBundleMgr_; } @@ -2584,7 +2595,8 @@ sptr Ability::GetBundleMgr() * @brief check permission of bundle, if it not existed. * @return returns the permission is vaild, or false for failed. */ -bool Ability::CheckPermission() { +bool Ability::CheckPermission() +{ APP_LOGI("%{public}s called.", __func__); int32_t uid = IPCSkeleton::GetCallingUid(); @@ -2621,10 +2633,10 @@ void Ability::SetBundleManager(const sptr &bundleManager) sptr Ability::GetFormRemoteObject() { APP_LOGI("%{public}s start", __func__); - if(providerRemoteObject_ == nullptr) { + if (providerRemoteObject_ == nullptr) { sptr providerClient = new (std::nothrow) FormProviderClient(); std::shared_ptr thisAbility = this->shared_from_this(); - if(thisAbility == nullptr) { + if (thisAbility == nullptr) { APP_LOGE("%{public}s failed, thisAbility is nullptr", __func__); } providerClient->SetOwner(thisAbility); @@ -2634,5 +2646,14 @@ sptr Ability::GetFormRemoteObject() return providerRemoteObject_; } +/** + * @brief Set the start ability setting. + * @param setting the start ability setting. + */ +void Ability::SetStartAbilitySetting(std::shared_ptr setting) +{ + APP_LOGI("%{public}s called.", __func__); + setting_ = setting; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/kits/ability/native/src/ability_context.cpp b/frameworks/kits/ability/native/src/ability_context.cpp index f2d7584623e..38fd5442d85 100755 --- a/frameworks/kits/ability/native/src/ability_context.cpp +++ b/frameworks/kits/ability/native/src/ability_context.cpp @@ -96,6 +96,15 @@ void AbilityContext::StartAbility(const Want &want, int requestCode, const Abili APP_LOGE("AbilityContext::StartAbility AbilityType = %{public}d", type); return; } + + APP_LOGI("%{public}s. Start calling ams->StartAbility.", __func__); + ErrCode err = + AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, abilityStartSetting, token_, requestCode); + APP_LOGI("%{public}s. End calling ams->StartAbility. ret=%{public}d", __func__, err); + if (err != ERR_OK) { + APP_LOGE("AbilityContext::StartAbility is failed %{public}d", err); + } + APP_LOGI("%{public}s end.", __func__); } @@ -820,9 +829,11 @@ Uri AbilityContext::GetCaller() */ void AbilityContext::AttachBaseContext(const std::shared_ptr &base) { + APP_LOGI("AbilityContext::AttachBaseContext. Start."); ContextContainer::AttachBaseContext(base); + APP_LOGI("AbilityContext::AttachBaseContext. End."); } - + /** * @brief Obtains the absolute path to the application-specific cache directory * on the primary external or shared storage device. diff --git a/frameworks/kits/ability/native/src/ability_process.cpp b/frameworks/kits/ability/native/src/ability_process.cpp index 507f1a7eb20..88a68530f2a 100755 --- a/frameworks/kits/ability/native/src/ability_process.cpp +++ b/frameworks/kits/ability/native/src/ability_process.cpp @@ -57,9 +57,11 @@ void AbilityProcess::StartAbility(Ability *ability, CallAbilityParam param, Call if (param.forResultOption == true) { if (param.setting == nullptr) { + APP_LOGI("%{public}s param.setting == nullptr call StartAbilityForResult.", __func__); ability->StartAbilityForResult(param.want, param.requestCode); } else { - ability->StartAbilityForResult(param.want, param.requestCode, *(param.setting.get())); + APP_LOGI("%{public}s param.setting != nullptr call StartAbilityForResult.", __func__); + ability->StartAbilityForResult(param.want, param.requestCode, *(param.setting)); } std::lock_guard lock_l(mutex_); @@ -77,9 +79,11 @@ void AbilityProcess::StartAbility(Ability *ability, CallAbilityParam param, Call abilityResultMap_[ability] = map; } else { if (param.setting == nullptr) { + APP_LOGI("%{public}s param.setting == nullptr call StartAbility.", __func__); ability->StartAbility(param.want); } else { - ability->StartAbility(param.want, *(param.setting.get())); + APP_LOGI("%{public}s param.setting != nullptr call StartAbility.", __func__); + ability->StartAbility(param.want, *(param.setting)); } } APP_LOGI("AbilityProcess::StartAbility end"); diff --git a/frameworks/kits/ability/native/src/ability_thread.cpp b/frameworks/kits/ability/native/src/ability_thread.cpp index db87af94789..2a1e7d3d5f3 100644 --- a/frameworks/kits/ability/native/src/ability_thread.cpp +++ b/frameworks/kits/ability/native/src/ability_thread.cpp @@ -852,12 +852,42 @@ int AbilityThread::BatchInsert(const Uri &uri, const std::vector & void AbilityThread::NotifyMultiWinModeChanged(int32_t winModeKey, bool flag) { APP_LOGI("NotifyMultiWinModeChanged.key:%{public}d,flag:%{public}d", winModeKey, flag); + sptr window = currentAbility_->GetWindow(); + if (window == nullptr) { + APP_LOGE("NotifyMultiWinModeChanged window == nullptr"); + return; + } + + if (flag) { + // true: normal windowMode -> free windowMode + if (winModeKey == MULTI_WINDOW_DISPLAY_FLOATING) { + APP_LOGI("NotifyMultiWinModeChanged.SetWindowMode:WINDOW_MODE_FREE begin."); + window->SetWindowType(WINDOW_TYPE_FLOAT); + APP_LOGI("NotifyMultiWinModeChanged.SetWindowMode:WINDOW_MODE_FREE end."); + } else { + APP_LOGI("NotifyMultiWinModeChanged.key:%{public}d", winModeKey); + } + } else { + // false: free windowMode -> normal windowMode + APP_LOGI("NotifyMultiWinModeChanged.SetWindowMode:WINDOW_MODE_TOP begin."); + window->SetWindowType(WINDOW_TYPE_NORMAL); + APP_LOGI("NotifyMultiWinModeChanged.SetWindowMode:WINDOW_MODE_TOP end."); + } + return; } void AbilityThread::NotifyTopActiveAbilityChanged(bool flag) { APP_LOGI("NotifyTopActiveAbilityChanged,flag:%{public}d", flag); + sptr window = currentAbility_->GetWindow(); + if (window == nullptr) { + APP_LOGE("NotifyMultiWinModeChanged window == nullptr"); + return; + } + if (flag) { + window->SwitchTop(); + } return; } diff --git a/frameworks/kits/ability/native/src/page_ability_impl.cpp b/frameworks/kits/ability/native/src/page_ability_impl.cpp index f46dbcb5908..84244afd5a1 100755 --- a/frameworks/kits/ability/native/src/page_ability_impl.cpp +++ b/frameworks/kits/ability/native/src/page_ability_impl.cpp @@ -41,6 +41,7 @@ void PageAbilityImpl::HandleAbilityTransaction(const Want &want, const AAFwk::Li SetLifeCycleStateInfo(targetState); if (lifecycleState_ == AAFwk::ABILITY_STATE_INITIAL) { + ability_->SetStartAbilitySetting(targetState.setting); Start(want); CheckAndRestore(); } diff --git a/frameworks/kits/ability/native/test/mock/include/mock_ability_manager_client_interface1.cpp b/frameworks/kits/ability/native/test/mock/include/mock_ability_manager_client_interface1.cpp index 14c5c1d6631..7dc0b223c3b 100644 --- a/frameworks/kits/ability/native/test/mock/include/mock_ability_manager_client_interface1.cpp +++ b/frameworks/kits/ability/native/test/mock/include/mock_ability_manager_client_interface1.cpp @@ -236,7 +236,7 @@ ErrCode AbilityManagerClient::UnlockMission(int missionId) } ErrCode AbilityManagerClient::SetMissionDescriptionInfo( - const sptr &token, const MissionDescriptionInfo &missionDescriptionInfo) + const sptr &token, const MissionDescriptionInfo &missionDescriptionInfo) { return ERR_OK; } diff --git a/frameworks/kits/ability/native/test/mock/include/mock_resourceManager_interface1.cpp b/frameworks/kits/ability/native/test/mock/include/mock_resourceManager_interface1.cpp index f06f2782128..34c4fd14c91 100644 --- a/frameworks/kits/ability/native/test/mock/include/mock_resourceManager_interface1.cpp +++ b/frameworks/kits/ability/native/test/mock/include/mock_resourceManager_interface1.cpp @@ -31,7 +31,7 @@ public: ResourceManagerTestInstance(){}; virtual ~ResourceManagerTestInstance(){}; - virtual bool AddResource(const char *path) + virtual bool AddResource(const char *path) override { return false; }; @@ -52,7 +52,7 @@ public: outValue = iter->second; return SUCCESS; }; - virtual void SetStringById(uint32_t id, std::string &inValue) + virtual void SetStringById(uint32_t id, std::string &inValue) override { if (!StringById_.empty()) { StringById_.clear(); @@ -69,7 +69,7 @@ public: { return ERROR; }; - virtual void SetStringFormatById(std::string &inValue, uint32_t id, ...){}; + virtual void SetStringFormatById(std::string &inValue, uint32_t id, ...) override{}; virtual RState GetStringFormatByName(std::string &outValue, const char *name, ...) { @@ -224,7 +224,7 @@ public: outValue = iter->second; return SUCCESS; }; - virtual void SetColorById(uint32_t id, uint32_t &inValue) + virtual void SetColorById(uint32_t id, uint32_t &inValue) override { if (!ColorById_.empty()) { ColorById_.clear(); diff --git a/frameworks/kits/ability/native/test/unittest/ability_context_interface1_test.cpp b/frameworks/kits/ability/native/test/unittest/ability_context_interface1_test.cpp index 75fe6708480..249d2584fa6 100644 --- a/frameworks/kits/ability/native/test/unittest/ability_context_interface1_test.cpp +++ b/frameworks/kits/ability/native/test/unittest/ability_context_interface1_test.cpp @@ -224,7 +224,6 @@ HWTEST_F(AbilityContextInterfaceTest, AaFwk_AbilityContext_SetTheme_0100, Functi contextDeal->initResourceManager(resourceManager); abilityContext->AttachBaseContext(contextDeal); abilityContext->SetTheme(testValue); - // EXPECT_EQ(testValue, contextDeal->GetHapModuleInfo()->themeId); GTEST_LOG_(INFO) << "AaFwk_AbilityContext_SetTheme_0100 end"; } @@ -241,13 +240,10 @@ HWTEST_F(AbilityContextInterfaceTest, AaFwk_AbilityContext_SetTheme_0200, Functi std::shared_ptr contextDeal = std::make_shared(); std::shared_ptr abilityContext = std::make_shared(); abilityContext->SetTheme(testValue); - // EXPECT_NE(testValue, contextDeal->GetHapModuleInfo()->themeId); abilityContext->AttachBaseContext(contextDeal); abilityContext->SetTheme(testValue); - // EXPECT_NE(testValue, contextDeal->GetHapModuleInfo()->themeId); contextDeal->initResourceManager(resourceManager); abilityContext->SetTheme(testValue); - // EXPECT_NE(testValue, contextDeal->GetHapModuleInfo()->themeId); GTEST_LOG_(INFO) << "AaFwk_AbilityContext_SetTheme_0200 end"; } @@ -333,7 +329,6 @@ HWTEST_F(AbilityContextInterfaceTest, AaFwk_AbilityContext_GetTheme_0100, Functi contextDeal->initResourceManager(resourceManager); abilityContext->AttachBaseContext(contextDeal); std::map retVal = abilityContext->GetTheme(); - // EXPECT_TRUE((retVal == testList)); GTEST_LOG_(INFO) << "AaFwk_AbilityContext_GetTheme_0100 end"; } @@ -368,11 +363,7 @@ HWTEST_F(AbilityContextInterfaceTest, AaFwk_AbilityContext_GetString_ByIdAndForm std::shared_ptr resourceManager(Global::Resource::CreateResourceManager2()); std::shared_ptr contextDeal = std::make_shared(); std::shared_ptr abilityContext = std::make_shared(); - // int testCount = 0; std::string testByName = ""; - // std::string retVal = contextDeal->GetString(testCount, testByName); - // EXPECT_STREQ(retVal, testValue); - // EXPECT_TRUE(false); GTEST_LOG_(INFO) << "AaFwk_AbilityContext_GetString_ByIdAndFormat_0100 end"; } @@ -512,10 +503,7 @@ HWTEST_F(AbilityContextInterfaceTest, AaFwk_AbilityContext_GetThemeId_0100, Func GTEST_LOG_(INFO) << "AaFwk_AbilityContext_GetThemeId_0100 start"; std::shared_ptr contextDeal = std::make_shared(); std::shared_ptr abilityContext = std::make_shared(); - // const int testValue = 1; - // contextDeal->GetHapModuleInfo()->themeId = testValue; abilityContext->AttachBaseContext(contextDeal); - // EXPECT_EQ(testValue, abilityContext->GetThemeId()); GTEST_LOG_(INFO) << "AaFwk_AbilityContext_GetThemeId_0100 end"; } diff --git a/frameworks/kits/ability/native/test/unittest/ability_impl_test.cpp b/frameworks/kits/ability/native/test/unittest/ability_impl_test.cpp index 92f975505c9..bf41d9e5965 100644 --- a/frameworks/kits/ability/native/test/unittest/ability_impl_test.cpp +++ b/frameworks/kits/ability/native/test/unittest/ability_impl_test.cpp @@ -61,13 +61,7 @@ void AbilityImplTest::SetUp(void) } void AbilityImplTest::TearDown(void) -{ - // delete AbilityImpl_; - // delete MocKPageAbility_; - - // AbilityImpl_ = nullptr; - // MocKPageAbility_ = nullptr; -} +{} /* * Feature: AbilityImpl diff --git a/frameworks/kits/ability/native/test/unittest/data_ability_operation_test.cpp b/frameworks/kits/ability/native/test/unittest/data_ability_operation_test.cpp index a030989dd08..f6c7cfd9e80 100644 --- a/frameworks/kits/ability/native/test/unittest/data_ability_operation_test.cpp +++ b/frameworks/kits/ability/native/test/unittest/data_ability_operation_test.cpp @@ -310,10 +310,6 @@ HWTEST_F(DataAbilityOperationTest, AaFwk_DataAbilityOperation_GetExpectedCount_0 std::shared_ptr uri = std::make_shared(URI); std::shared_ptr dataAbilityOperation = DataAbilityOperation::NewAssertBuilder(uri)->WithExpectedCount(10)->Build(); - // CreateBuilder(DataAbilityOperation::TYPE_UPDATE, uri); - // // builder = builder->WithExpectedCount(10); - // // EXPECT_NE(builder, nullptr); - // std::shared_ptr dataAbilityOperation = builder->WithExpectedCount(10)->Build(); EXPECT_EQ(dataAbilityOperation->GetExpectedCount(), 10); GTEST_LOG_(INFO) << "AaFwk_DataAbilityOperation_GetExpectedCount_0200 end"; } @@ -624,7 +620,7 @@ HWTEST_F(DataAbilityOperationTest, AaFwk_DataAbilityOperation_Unmarshalling_0100 ->WithInterruptionAllowed(true) ->WithPredicatesBackReference(0, 0) ->Build(); - Parcel in; + Parcel in; dataAbilityOperation->Marshalling(in); DataAbilityOperation *pDataAbilityOperation = DataAbilityOperation::Unmarshalling(in); std::map references = dataAbilityOperation->GetDataAbilityPredicatesBackReferences(); diff --git a/frameworks/kits/ability/native/test/unittest/data_ability_result_test.cpp b/frameworks/kits/ability/native/test/unittest/data_ability_result_test.cpp index 72c6f315f6f..37afa74f910 100644 --- a/frameworks/kits/ability/native/test/unittest/data_ability_result_test.cpp +++ b/frameworks/kits/ability/native/test/unittest/data_ability_result_test.cpp @@ -26,13 +26,13 @@ static const int SET_COUNT = 1; static const Uri uri("scheme://authority/path1/path2/path3?id = 1&name = mingming&old#fragment"); class DataAbilityResultTest : public testing::Test { public: - DataAbilityResultTest() + DataAbilityResultTest() {} ~DataAbilityResultTest() {} std::shared_ptr Base_ = nullptr; - + static void SetUpTestCase(void); static void TearDownTestCase(void); void SetUp(); @@ -47,21 +47,20 @@ void DataAbilityResultTest::TearDownTestCase(void) void DataAbilityResultTest::SetUp(void) { - Base_ = std::make_shared(COUNT_NULL); + Base_ = std::make_shared(COUNT_NULL); } void DataAbilityResultTest::TearDown(void) {} - /** - * @tc.number: - * @tc.name: + * @tc.number: + * @tc.name: * @tc.desc: */ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_DataAbilityResult0100, Function | MediumTest | Level1) { - DataAbilityResult test1(SET_COUNT); + DataAbilityResult test1(SET_COUNT); EXPECT_EQ(SET_COUNT, test1.GetCount()); EXPECT_EQ(std::string(""), test1.GetUri().ToString()); @@ -69,14 +68,14 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_DataAbilityResult01 EXPECT_EQ(COUNT_NULL, test2.GetCount()); EXPECT_EQ(uri.ToString(), test2.GetUri().ToString()); - DataAbilityResult test3(uri,SET_COUNT); + DataAbilityResult test3(uri, SET_COUNT); EXPECT_EQ(SET_COUNT, test3.GetCount()); EXPECT_EQ(uri.ToString(), test3.GetUri().ToString()); - + *Base_ = test3; - if(Base_){ - Parcel in; - Base_->Marshalling(in); + if (Base_) { + Parcel in; + Base_->Marshalling(in); DataAbilityResult test4(in); EXPECT_EQ(SET_COUNT, test4.GetCount()); EXPECT_EQ(uri.ToString(), test4.GetUri().ToString()); @@ -84,13 +83,13 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_DataAbilityResult01 } /** - * @tc.number: - * @tc.name: + * @tc.number: + * @tc.name: * @tc.desc: */ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_DataAbilityResult0200, Function | MediumTest | Level1) { - DataAbilityResult test1(COUNT_NULL); + DataAbilityResult test1(COUNT_NULL); EXPECT_EQ(COUNT_NULL, test1.GetCount()); EXPECT_EQ(std::string(""), test1.GetUri().ToString()); @@ -99,7 +98,7 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_DataAbilityResult02 EXPECT_EQ(COUNT_NULL, test2.GetCount()); EXPECT_EQ(zero.ToString(), test2.GetUri().ToString()); - DataAbilityResult test3(uri,SET_COUNT); + DataAbilityResult test3(uri, SET_COUNT); EXPECT_EQ(SET_COUNT, test3.GetCount()); EXPECT_EQ(uri.ToString(), test3.GetUri().ToString()); @@ -111,21 +110,21 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_DataAbilityResult02 } /** - * @tc.number: - * @tc.name: + * @tc.number: + * @tc.name: * @tc.desc: */ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_Get0100, Function | MediumTest | Level1) { // Uri uri("abc"); - DataAbilityResult test(uri,SET_COUNT); + DataAbilityResult test(uri, SET_COUNT); EXPECT_EQ(SET_COUNT, test.GetCount()); EXPECT_EQ(uri.ToString(), test.GetUri().ToString()); } /** - * @tc.number: - * @tc.name: + * @tc.number: + * @tc.name: * @tc.desc: */ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_Get0200, Function | MediumTest | Level1) @@ -135,8 +134,8 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_Get0200, Function | } /** - * @tc.number: - * @tc.name: + * @tc.number: + * @tc.name: * @tc.desc: */ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_CreateFromParcel0100, Function | MediumTest | Level1) @@ -144,7 +143,7 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_CreateFromParcel010 Parcel in; Base_->Marshalling(in); DataAbilityResult *ptr = Base_->CreateFromParcel(in); - + if (ptr != nullptr) { EXPECT_EQ(true, ptr->GetUri().ToString().empty()); EXPECT_EQ(COUNT_NULL, ptr->GetCount()); @@ -152,25 +151,25 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_CreateFromParcel010 } /** - * @tc.number: - * @tc.name: + * @tc.number: + * @tc.name: * @tc.desc: */ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_ToString0100, Function | MediumTest | Level1) { std::string search = "DataAbilityResult("; - std::size_t pos = 0; + std::size_t pos = 0; std::size_t result = 0; - DataAbilityResult dataAbilityResult(uri,SET_COUNT); + DataAbilityResult dataAbilityResult(uri, SET_COUNT); std::string str = dataAbilityResult.ToString(); - + result = str.find(search, pos); EXPECT_EQ(result, pos); if (result != std::string::npos) { - pos += search.length() - 1; + pos += search.length() - 1; } - + search = std::string("uri=") + uri.ToString() + std::string(" "); result = str.find(search, pos); @@ -192,26 +191,26 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_ToString0100, Funct } /** - * @tc.number: - * @tc.name: + * @tc.number: + * @tc.name: * @tc.desc: */ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_ToString0200, Function | MediumTest | Level1) { - + std::string search = "DataAbilityResult("; - std::size_t pos = 0; + std::size_t pos = 0; std::size_t result = 0; DataAbilityResult dataAbilityResult(COUNT_NULL); std::string str = dataAbilityResult.ToString(); - + result = str.find(search, pos); EXPECT_EQ(result, pos); if (result != std::string::npos) { - pos += search.length() - 1; + pos += search.length() - 1; } - + search = std::string("uri=") + dataAbilityResult.GetUri().ToString() + std::string(" "); result = str.find(search, pos); @@ -233,9 +232,9 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_ToString0200, Funct } /** - * @tc.number: + * @tc.number: * @tc.name: Marshalling/Unmarshalling - * @tc.desc: marshalling + * @tc.desc: marshalling */ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_Parcelable_0100, Function | MediumTest | Level1) { @@ -243,7 +242,6 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_Parcelable_0100, Fu if (ResultIn_ == nullptr) { return; } - Parcel in; ResultIn_->Marshalling(in); @@ -256,9 +254,9 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_Parcelable_0100, Fu } /** - * @tc.number: + * @tc.number: * @tc.name: Marshalling/Unmarshalling - * @tc.desc: + * @tc.desc: */ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_Parcelable_0200, Function | MediumTest | Level1) { @@ -266,7 +264,7 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_Parcelable_0200, Fu if (ResultIn_ == nullptr) { return; } - + Parcel in; ResultIn_->Marshalling(in); std::shared_ptr ResultOut_(DataAbilityResult::Unmarshalling(in)); @@ -274,8 +272,8 @@ HWTEST_F(DataAbilityResultTest, AppExecFwk_DataAbilityResult_Parcelable_0200, Fu if (ResultOut_ != nullptr) { EXPECT_EQ(ResultIn_->GetUri().ToString(), ResultOut_->GetUri().ToString()); EXPECT_EQ(ResultIn_->GetCount(), ResultOut_->GetCount()); - } + } } -} // namespace AppExecFwk +} // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/frameworks/kits/base/cpp/test/unittest/common/base_test.cpp b/frameworks/kits/base/cpp/test/unittest/common/base_test.cpp index a3462f02575..40f182a0239 100644 --- a/frameworks/kits/base/cpp/test/unittest/common/base_test.cpp +++ b/frameworks/kits/base/cpp/test/unittest/common/base_test.cpp @@ -58,37 +58,37 @@ class LightRefCountBaseTestClass : public LightRefCountBase { public: LightRefCountBaseTestClass() { - g_destructorCalled = false; + gDestructorCalled_ = false; } virtual ~LightRefCountBaseTestClass() { - g_destructorCalled = true; + gDestructorCalled_ = true; } public: - static bool g_destructorCalled; + static bool gDestructorCalled_; }; -bool LightRefCountBaseTestClass::g_destructorCalled = false; +bool LightRefCountBaseTestClass::gDestructorCalled_ = false; class RefBaseTestClass : public RefBase { public: RefBaseTestClass() { - g_destructorCalled = false; + gDestructorCalled_ = false; } virtual ~RefBaseTestClass() { - g_destructorCalled = true; + gDestructorCalled_ = true; } public: - static bool g_destructorCalled; + static bool gDestructorCalled_; }; -bool RefBaseTestClass::g_destructorCalled = false; +bool RefBaseTestClass::gDestructorCalled_ = false; CLASS(ObjectTestClass, 5afc4756 - 8f3c - 4d80 - a88b - 54521890beca) { @@ -97,12 +97,12 @@ public: /* [in] */ int type) : type_(type) { - g_destructorCalled = false; + gDestructorCalled_ = false; } ~ObjectTestClass() { - g_destructorCalled = true; + gDestructorCalled_ = true; } OBJECT_DECL(); @@ -134,16 +134,19 @@ public: } public: - static bool g_destructorCalled; + static bool gDestructorCalled_; private: int type_; }; const ClassID CID_ObjectTestClass = { - 0x5afc4756, 0x8f3c, 0x4d80, 0xa88b, {0x5, 0x4, 0x5, 0x2, 0x1, 0x8, 0x9, 0x0, 0xb, 0xe, 0xc, 0xa}}; + 0x5afc4756, 0x8f3c, 0x4d80, 0xa88b, { + 0x5, 0x4, 0x5, 0x2, 0x1, 0x8, 0x9, 0x0, 0xb, 0xe, 0xc, 0xa + } + }; -bool ObjectTestClass::g_destructorCalled = false; +bool ObjectTestClass::gDestructorCalled_ = false; OBJECT_IMPL(ObjectTestClass); @@ -158,10 +161,10 @@ OBJECT_IMPL(ObjectTestClass); HWTEST_F(AAFwkBaseTest, LightRefCountBase_test_001, TestSize.Level1) { sptr testObject = new LightRefCountBaseTestClass(); - EXPECT_FALSE(LightRefCountBaseTestClass::g_destructorCalled); + EXPECT_FALSE(LightRefCountBaseTestClass::gDestructorCalled_); EXPECT_EQ(testObject->GetRefCount(), 1); testObject = nullptr; - EXPECT_TRUE(LightRefCountBaseTestClass::g_destructorCalled); + EXPECT_TRUE(LightRefCountBaseTestClass::gDestructorCalled_); } /* @@ -175,9 +178,9 @@ HWTEST_F(AAFwkBaseTest, LightRefCountBase_test_001, TestSize.Level1) HWTEST_F(AAFwkBaseTest, RefBase_test_001, TestSize.Level1) { sptr testObject = new RefBaseTestClass(); - EXPECT_FALSE(RefBaseTestClass::g_destructorCalled); + EXPECT_FALSE(RefBaseTestClass::gDestructorCalled_); testObject = nullptr; - EXPECT_TRUE(RefBaseTestClass::g_destructorCalled); + EXPECT_TRUE(RefBaseTestClass::gDestructorCalled_); } /* @@ -208,7 +211,7 @@ HWTEST_F(AAFwkBaseTest, object_test_002, TestSize.Level1) sptr testObject = new ObjectTestClass(999); EXPECT_EQ(CID_ObjectTestClass, testObject->GetClassID()); testObject = nullptr; - EXPECT_TRUE(ObjectTestClass::g_destructorCalled); + EXPECT_TRUE(ObjectTestClass::gDestructorCalled_); } /* @@ -224,7 +227,7 @@ HWTEST_F(AAFwkBaseTest, object_test_003, TestSize.Level1) sptr testObject = new ObjectTestClass(999); EXPECT_EQ(19, testObject->GetHashCode()); testObject = nullptr; - EXPECT_TRUE(ObjectTestClass::g_destructorCalled); + EXPECT_TRUE(ObjectTestClass::gDestructorCalled_); } /* @@ -270,7 +273,7 @@ HWTEST_F(AAFwkBaseTest, object_test_005, TestSize.Level1) HWTEST_F(AAFwkBaseTest, object_test_006, TestSize.Level1) { sptr testObject1 = new ObjectTestClass(999); - EXPECT_FALSE(ObjectTestClass::g_destructorCalled); + EXPECT_FALSE(ObjectTestClass::gDestructorCalled_); sptr weakRef; testObject1->GetWeakReference(weakRef); EXPECT_TRUE(weakRef != nullptr); @@ -280,7 +283,7 @@ HWTEST_F(AAFwkBaseTest, object_test_006, TestSize.Level1) EXPECT_EQ(static_cast(object.GetRefPtr())->GetHashCode(), 19); testObject1 = nullptr; object = nullptr; - EXPECT_TRUE(ObjectTestClass::g_destructorCalled); + EXPECT_TRUE(ObjectTestClass::gDestructorCalled_); weakRef->Resolve(g_IID_IObject, reinterpret_cast(&object)); EXPECT_TRUE(object == nullptr); } @@ -296,10 +299,10 @@ HWTEST_F(AAFwkBaseTest, object_test_006, TestSize.Level1) HWTEST_F(AAFwkBaseTest, object_test_007, TestSize.Level1) { sptr testObject1 = new ObjectTestClass(999); - EXPECT_FALSE(ObjectTestClass::g_destructorCalled); + EXPECT_FALSE(ObjectTestClass::gDestructorCalled_); wptr weakObject(testObject1); testObject1 = nullptr; - EXPECT_TRUE(ObjectTestClass::g_destructorCalled); + EXPECT_TRUE(ObjectTestClass::gDestructorCalled_); EXPECT_TRUE(weakObject.promote() == nullptr); } @@ -1103,5 +1106,5 @@ HWTEST_F(AAFwkBaseTest, array_test_005, TestSize.Level1) EXPECT_FALSE(Object::Equals(arrayObj1, arrayObj2)); EXPECT_TRUE(Object::Equals(arrayObj1, arrayObj3)); } -} -} +} // namespace AAFwk +} // namespace OHOS diff --git a/frameworks/kits/content/cpp/test/unittest/common/operation_test.cpp b/frameworks/kits/content/cpp/test/unittest/common/operation_test.cpp index f078a915aa5..761e0cf0c8b 100755 --- a/frameworks/kits/content/cpp/test/unittest/common/operation_test.cpp +++ b/frameworks/kits/content/cpp/test/unittest/common/operation_test.cpp @@ -320,7 +320,6 @@ HWTEST_F(OperationBaseTest, AaFwk_Operation_Marshalling_0100, Function | MediumT */ HWTEST_F(OperationBaseTest, AaFwk_Operation_Operator_0100, Function | MediumTest | Level1) { - // std::shared_ptr operation_ = std::make_shared(); Operation operation_; std::string value = "value"; OHOS::Uri uri(value); diff --git a/frameworks/kits/content/cpp/test/unittest/common/skills_test.cpp b/frameworks/kits/content/cpp/test/unittest/common/skills_test.cpp index 4a901df1a50..eaa0ba86be1 100755 --- a/frameworks/kits/content/cpp/test/unittest/common/skills_test.cpp +++ b/frameworks/kits/content/cpp/test/unittest/common/skills_test.cpp @@ -699,17 +699,15 @@ HWTEST_F(SkillsBaseTest, AaFwk_Skills_Type_0300, Function | MediumTest | Level1) using SkillsMatchType = std::tuple; class SkillsMatchTest : public testing::TestWithParam { public: - SkillsMatchTest() : skills_(nullptr) + SkillsMatchTest() {} ~SkillsMatchTest() - { - skills_ = nullptr; - } + {} static void SetUpTestCase(void); static void TearDownTestCase(void); void SetUp(); void TearDown(); - Skills *skills_; + std::shared_ptr skills_ = nullptr; }; void SkillsMatchTest::SetUpTestCase(void) @@ -720,14 +718,11 @@ void SkillsMatchTest::TearDownTestCase(void) void SkillsMatchTest::SetUp(void) { - skills_ = new Skills(); + skills_ = std::make_shared(); } void SkillsMatchTest::TearDown(void) -{ - delete skills_; - skills_ = nullptr; -} +{} /** * @tc.number: AaFwk_Skills_match_0100 diff --git a/frameworks/kits/test/moduletest/cpp/abilitytest/data_ability_operation_module_test.cpp b/frameworks/kits/test/moduletest/cpp/abilitytest/data_ability_operation_module_test.cpp index 11462db916e..d8afe8183b6 100644 --- a/frameworks/kits/test/moduletest/cpp/abilitytest/data_ability_operation_module_test.cpp +++ b/frameworks/kits/test/moduletest/cpp/abilitytest/data_ability_operation_module_test.cpp @@ -15,20 +15,6 @@ #include #include -#include "ability_thread.h" -#include "ability_local_record.h" -#include "ability_loader.h" -#include "ability_impl_factory.h" -#include "data_ability_helper.h" -#include "context_deal.h" -#include "ohos_application.h" -#include "sys_mgr_client.h" -#include "ability_manager_interface.h" -#include "ability_manager_client.h" -#include "system_ability_definition.h" -#include "demo_ability_test.h" -#include "mock_bundle_manager.h" -#include "mock_ability_manager_service.h" #include "data_ability_operation.h" #include "data_ability_operation_builder.h" @@ -36,26 +22,13 @@ namespace OHOS { namespace AppExecFwk { using namespace testing::ext; using namespace OHOS; -using namespace AAFwk; -using OHOS::AppExecFwk::ElementName; using namespace OHOS::AppExecFwk; -/* - * Parameters: - * Action - * Entity - * Flag - * ElementName - */ -// const std::string ABILITY_NAME("DemoAbility"); class DataAbilityOperationModuleTest : public testing::Test { public: static void SetUpTestCase(void); static void TearDownTestCase(void); void SetUp(); void TearDown(); - OHOS::sptr abilityObject_; - // static constexpr int TEST_WAIT_TIME = 500 * 1000; // 500 ms - // static const int RESULT_CODE = 1992; }; void DataAbilityOperationModuleTest::SetUpTestCase(void) @@ -65,18 +38,7 @@ void DataAbilityOperationModuleTest::TearDownTestCase(void) {} void DataAbilityOperationModuleTest::SetUp(void) -{ - abilityObject_ = new MockAbilityManagerService(); - OHOS::sptr bundleObject = new BundleMgrService(); - auto sysMgr = OHOS::DelayedSingleton::GetInstance(); - if (sysMgr == NULL) { - GTEST_LOG_(ERROR) << "fail to get ISystemAbilityManager"; - return; - } - - sysMgr->RegisterSystemAbility(OHOS::ABILITY_MGR_SERVICE_ID, abilityObject_); - sysMgr->RegisterSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, bundleObject); -} +{} void DataAbilityOperationModuleTest::TearDown(void) {} @@ -90,38 +52,20 @@ void DataAbilityOperationModuleTest::TearDown(void) HWTEST_F(DataAbilityOperationModuleTest, AaFwk_DataAbilityOperation_InsertBuilder_0100, Function | MediumTest | Level1) { GTEST_LOG_(INFO) << "AaFwk_DataAbilityOperation_InsertBuilder_0100"; - - std::shared_ptr application = std::make_shared(); - sptr abilityToken = sptr(new AbilityThread()); - EXPECT_NE(abilityToken, nullptr); - if (abilityToken != nullptr) { - std::shared_ptr abilityInfo = std::make_shared(); - abilityInfo->type = AppExecFwk::AbilityType::DATA; - abilityInfo->name = "DemoAbility"; - std::shared_ptr abilityRecord = - std::make_shared(abilityInfo, abilityToken); - - AbilityThread::AbilityThreadMain(application, abilityRecord); - - std::shared_ptr context = std::make_shared(); - std::shared_ptr uri = std::make_shared("dataability://com.example.myapplication5.DataAbilityTest"); - std::shared_ptr helper = DataAbilityHelper::Creator(context, uri, true); - - EXPECT_NE(helper, nullptr); - std::shared_ptr values = std::make_shared(); - std::shared_ptr operation = + std::shared_ptr values = std::make_shared(); + std::shared_ptr uri = std::make_shared("dataability://com.example.myapplication5.DataAbilityTest"); + std::shared_ptr operation = DataAbilityOperation::NewInsertBuilder(uri)->WithValuesBucket(values)->Build(); - EXPECT_NE(operation, nullptr); + EXPECT_NE(operation, nullptr); - bool isInsertOperation = operation->IsInsertOperation(); - EXPECT_TRUE(isInsertOperation); + bool isInsertOperation = operation->IsInsertOperation(); + EXPECT_TRUE(isInsertOperation); - std::shared_ptr operationUri = operation->GetUri(); - EXPECT_EQ(operationUri->ToString(), "dataability://com.example.myapplication5.DataAbilityTest"); + std::shared_ptr operationUri = operation->GetUri(); + EXPECT_EQ(operationUri->ToString(), "dataability://com.example.myapplication5.DataAbilityTest"); - int type = operation->GetType(); - EXPECT_EQ(type, DataAbilityOperation::TYPE_INSERT); - } + int type = operation->GetType(); + EXPECT_EQ(type, DataAbilityOperation::TYPE_INSERT); GTEST_LOG_(INFO) << "AaFwk_DataAbilityOperation_InsertBuilder_0100"; } @@ -134,43 +78,25 @@ HWTEST_F(DataAbilityOperationModuleTest, AaFwk_DataAbilityOperation_InsertBuilde HWTEST_F(DataAbilityOperationModuleTest, AaFwk_DataAbilityOperation_UpdateBuilder_0100, Function | MediumTest | Level1) { GTEST_LOG_(INFO) << "AaFwk_DataAbilityOperation_UpdateBuilder_0100"; - - std::shared_ptr application = std::make_shared(); - sptr abilityToken = sptr(new AbilityThread()); - EXPECT_NE(abilityToken, nullptr); - if (abilityToken != nullptr) { - std::shared_ptr abilityInfo = std::make_shared(); - abilityInfo->type = AppExecFwk::AbilityType::DATA; - abilityInfo->name = "DemoAbility"; - std::shared_ptr abilityRecord = - std::make_shared(abilityInfo, abilityToken); - - AbilityThread::AbilityThreadMain(application, abilityRecord); - - std::shared_ptr context = std::make_shared(); - std::shared_ptr uri = std::make_shared("dataability://com.example.myapplication5.DataAbilityTest"); - std::shared_ptr helper = DataAbilityHelper::Creator(context, uri, true); - - EXPECT_NE(helper, nullptr); - std::shared_ptr values = std::make_shared(); - std::shared_ptr predicates = std::make_shared(); - std::shared_ptr operation = DataAbilityOperation::NewUpdateBuilder(uri) + std::shared_ptr values = std::make_shared(); + std::shared_ptr predicates = std::make_shared(); + std::shared_ptr uri = std::make_shared("dataability://com.example.myapplication5.DataAbilityTest"); + std::shared_ptr operation = DataAbilityOperation::NewUpdateBuilder(uri) ->WithValuesBucket(values) ->WithPredicatesBackReference(0, 0) ->WithPredicates(predicates) ->WithInterruptionAllowed(true) ->Build(); - EXPECT_NE(operation, nullptr); + EXPECT_NE(operation, nullptr); - bool isUpdateOperation = operation->IsUpdateOperation(); - EXPECT_TRUE(isUpdateOperation); + bool isUpdateOperation = operation->IsUpdateOperation(); + EXPECT_TRUE(isUpdateOperation); - bool isInterruptionAllowed = operation->IsInterruptionAllowed(); - EXPECT_TRUE(isInterruptionAllowed); + bool isInterruptionAllowed = operation->IsInterruptionAllowed(); + EXPECT_TRUE(isInterruptionAllowed); - std::shared_ptr valuesBucket = operation->GetValuesBucket(); - EXPECT_NE(valuesBucket, nullptr); - } + std::shared_ptr valuesBucket = operation->GetValuesBucket(); + EXPECT_NE(valuesBucket, nullptr); GTEST_LOG_(INFO) << "AaFwk_DataAbilityOperation_UpdateBuilder_0100"; } @@ -183,35 +109,17 @@ HWTEST_F(DataAbilityOperationModuleTest, AaFwk_DataAbilityOperation_UpdateBuilde HWTEST_F(DataAbilityOperationModuleTest, AaFwk_DataAbilityOperation_AssertBuilder_0100, Function | MediumTest | Level1) { GTEST_LOG_(INFO) << "AaFwk_DataAbilityOperation_AssertBuilder_0100"; - - std::shared_ptr application = std::make_shared(); - sptr abilityToken = sptr(new AbilityThread()); - EXPECT_NE(abilityToken, nullptr); - if (abilityToken != nullptr) { - std::shared_ptr abilityInfo = std::make_shared(); - abilityInfo->type = AppExecFwk::AbilityType::DATA; - abilityInfo->name = "DemoAbility"; - std::shared_ptr abilityRecord = - std::make_shared(abilityInfo, abilityToken); - - AbilityThread::AbilityThreadMain(application, abilityRecord); - - std::shared_ptr context = std::make_shared(); - std::shared_ptr uri = std::make_shared("dataability://com.example.myapplication5.DataAbilityTest"); - std::shared_ptr helper = DataAbilityHelper::Creator(context, uri, true); - - EXPECT_NE(helper, nullptr); - - std::shared_ptr operation = + std::shared_ptr uri = std::make_shared("dataability://com.example.myapplication5.DataAbilityTest"); + std::shared_ptr operation = DataAbilityOperation::NewAssertBuilder(uri)->WithExpectedCount(2)->Build(); - EXPECT_NE(operation, nullptr); + EXPECT_NE(operation, nullptr); - bool isAssertOperation = operation->IsAssertOperation(); - EXPECT_TRUE(isAssertOperation); + bool isAssertOperation = operation->IsAssertOperation(); + EXPECT_TRUE(isAssertOperation); - int count = operation->GetExpectedCount(); - EXPECT_EQ(count, 2); - } + int count = operation->GetExpectedCount(); + EXPECT_EQ(count, 2); + GTEST_LOG_(INFO) << "AaFwk_DataAbilityOperation_AssertBuilder_0100"; } @@ -224,32 +132,16 @@ HWTEST_F(DataAbilityOperationModuleTest, AaFwk_DataAbilityOperation_AssertBuilde HWTEST_F(DataAbilityOperationModuleTest, AaFwk_DataAbilityOperation_DeleteBuilder_0100, Function | MediumTest | Level1) { GTEST_LOG_(INFO) << "AaFwk_DataAbilityOperation_DeleteBuilder_0100"; - - std::shared_ptr application = std::make_shared(); - sptr abilityToken = sptr(new AbilityThread()); - EXPECT_NE(abilityToken, nullptr); - if (abilityToken != nullptr) { - std::shared_ptr abilityInfo = std::make_shared(); - abilityInfo->type = AppExecFwk::AbilityType::DATA; - abilityInfo->name = "DemoAbility"; - std::shared_ptr abilityRecord = - std::make_shared(abilityInfo, abilityToken); - - AbilityThread::AbilityThreadMain(application, abilityRecord); - - std::shared_ptr context = std::make_shared(); - std::shared_ptr uri = std::make_shared("dataability://com.example.myapplication5.DataAbilityTest"); - std::shared_ptr helper = DataAbilityHelper::Creator(context, uri, true); - EXPECT_NE(helper, nullptr); - std::shared_ptr predicates = std::make_shared(); - EXPECT_NE(predicates, nullptr); - std::shared_ptr builder = DataAbilityOperation::NewDeleteBuilder(uri); - EXPECT_NE(builder, nullptr); - std::shared_ptr operation = builder->WithPredicates(predicates)->Build(); - EXPECT_NE(operation, nullptr); - bool isDeleteOperation = operation->IsDeleteOperation(); - EXPECT_TRUE(isDeleteOperation); - } + std::shared_ptr predicates = std::make_shared(); + EXPECT_NE(predicates, nullptr); + std::shared_ptr uri = std::make_shared("dataability://com.example.myapplication5.DataAbilityTest"); + std::shared_ptr builder = DataAbilityOperation::NewDeleteBuilder(uri); + EXPECT_NE(builder, nullptr); + std::shared_ptr operation = builder->WithPredicates(predicates)->Build(); + EXPECT_NE(operation, nullptr); + bool isDeleteOperation = operation->IsDeleteOperation(); + EXPECT_TRUE(isDeleteOperation); + GTEST_LOG_(INFO) << "AaFwk_DataAbilityOperation_DeleteBuilder_0100"; } diff --git a/interfaces/kits/js/@ohos.ability.featureAbility.d.ts b/interfaces/kits/js/@ohos.ability.featureAbility.d.ts old mode 100755 new mode 100644 index e0c406e16fd..043a7951ec2 --- a/interfaces/kits/js/@ohos.ability.featureAbility.d.ts +++ b/interfaces/kits/js/@ohos.ability.featureAbility.d.ts @@ -17,10 +17,7 @@ import { Want } from './ability/want'; import { StartAbilityParameter } from './ability/startAbilityParameter'; import { AbilityResult } from './ability/abilityResult'; import { Context } from './app/context'; -import { ConnectOptions } from './ability/connectOptions'; import { DataAbilityHelper } from './ability/dataAbilityHelper'; -import { ProcessInfo } from './app/processInfo'; -import { ElementName } from './bundle/elementName'; /** * A Feature Ability represents an ability with a UI and is designed to interact with users. @@ -36,6 +33,7 @@ declare namespace featureAbility { * @devices phone, tablet * @since 6 * @sysCap AAFwk + * @param parameter Indicates the ability to start. * @return - */ function getWant(callback: AsyncCallback): void; @@ -92,78 +90,37 @@ declare namespace featureAbility { */ function terminateSelf(callback: AsyncCallback): void; - /** - * Checks whether the main window of this ability has window focus. - * @devices phone, tablet - * @since 7 - * @sysCap AAFwk - */ - function hasWindowFocus(callback: AsyncCallback): void; - function hasWindowFocus(): Promise; - - /** - * Obtains the type of this application. - * @devices phone, tablet - * @since 7 - * @sysCap appexecfwk - * @return Returns {@code system} if this application is a system application; - * returns {@code normal} if it is released in Ohos AppGallery; - * returns {@code other} if it is released by a third-party vendor; - * returns an empty string if the query fails. - */ - function getAppType(callback: AsyncCallback): void; - function getAppType(): Promise; - - /** - * Obtains the bundle name of the current ability. - * @devices phone, tablet - * @since 7 - * @sysCap appexecfwk - * @return Returns the bundle name of the current ability. - */ - function getBundleName(callback: AsyncCallback): void; - function getBundleName(): Promise; - /** - * Obtains the bundle name of the calling ability. + * Obtains the dataAbilityHelper. * @devices phone, tablet * @since 7 - * @sysCap appexecfwk - * @return Returns the bundle name of the current ability. + * @sysCap AAFwk + * @param uri Indicates the path of the file to open. + * @return Returns the dataAbilityHelper. */ - function getCallingBundle(callback: AsyncCallback): void; - function getCallingBundle(): Promise; + function acquireDataAbilityHelper(uri: string): DataAbilityHelper; - /** - * Connects an ability to a Service ability. + /** + * Checks whether the main window of this ability has window focus. * @devices phone, tablet * @since 7 * @sysCap AAFwk - * @param want Indicates the Service ability to connect. - * @param connectionCallback Indicates the callback object when the Service ability is connected. - * @return Returns true if the connection is successful; returns false otherwise. */ - function getDataAbilityHelper(URI: string, callback:AsyncCallback): void; - function getDataAbilityHelper(URI: string): Promise; + function hasWindowFocus(callback: AsyncCallback): void; + function hasWindowFocus(): Promise; - /** - * Obtains the element name of the current ability. - * @devices phone, tablet - * @since 7 - * @sysCap appexecfwk - * @return Returns the element name of the current ability. - */ - function getElementName(callback: AsyncCallback): void; - function getElementName(): Promise; + export enum AbilityWindowConfiguration { + WINDOW_MODE_UNDEFINED = 0, + WINDOW_MODE_FULLSCREEN = 1, + WINDOW_MODE_SPLIT_PRIMARY = 100, + WINDOW_MODE_SPLIT_SECONDARY = 101, + WINDOW_MODE_FLOATING = 102 + } - /** - * Obtains information about the current process, including the process ID and name. - * @devices phone, tablet - * @since 7 - * @sysCap appxecfwk - * @return Returns the process info of the current process. - */ - function getProcessInfo(callback: AsyncCallback): void; - function getProcessInfo(): Promise; + export enum AbilityStartSetting { + BOUNDS_KEY = "abilityBounds", + WINDOW_MODE_KEY = "windowMode", + DISPLAY_ID_KEY = "displayId" + } } -export default featureAbility; \ No newline at end of file +export default featureAbility; diff --git a/interfaces/kits/js/@ohos.ability.particleAbility.d.ts b/interfaces/kits/js/@ohos.ability.particleAbility.d.ts index ba62b6f6bd0..e6388be0229 100644 --- a/interfaces/kits/js/@ohos.ability.particleAbility.d.ts +++ b/interfaces/kits/js/@ohos.ability.particleAbility.d.ts @@ -14,13 +14,11 @@ */ import { AsyncCallback } from './basic'; import { StartAbilityParameter } from './ability/startAbilityParameter'; -import { Want } from './ability/want'; -import { ConnectOptions } from './ability/connectOptions'; /** * A Particle Ability represents an ability with service. * @name particleAbility - * @since 6 + * @since 7 * @sysCap AAFwk * @devices phone, tablet * @permission N/A @@ -30,22 +28,12 @@ declare namespace particleAbility { /** * Service ability uses this method to start a specific ability. * @devices phone, tablet - * @since 6 + * @since 7 * @sysCap AAFwk * @param parameter Indicates the ability to start. * @return - */ - function startAbility(parameter: StartAbilityParameter, callback: AsyncCallback): void; - function startAbility(parameter: StartAbilityParameter): Promise; - - /** - * Destroys another Service ability. - * - * @param want Indicates information about the Service ability. - * @return Returns true if the Service ability is destroyed; returns false otherwise. - * @since 6 - */ - function stopAbility(request: Want, callback:AsyncCallback): void; - function stopAbility(request: Want): Promise; + function startAbility(parameter: StartAbilityParameter, callback: AsyncCallback): void; + function startAbility(parameter: StartAbilityParameter): Promise; } -export default particleAbility; +export default particleAbility; \ No newline at end of file diff --git a/interfaces/kits/js/@ohos.ability.wantConstant.d.ts b/interfaces/kits/js/@ohos.ability.wantConstant.d.ts index e05916b8cff..edae0e993c1 100644 --- a/interfaces/kits/js/@ohos.ability.wantConstant.d.ts +++ b/interfaces/kits/js/@ohos.ability.wantConstant.d.ts @@ -21,7 +21,7 @@ * @devices phone, tablet * @permission N/A */ - declare namespace wantConstant { +declare namespace wantConstant { /** * the constant for action of the want * @name Action diff --git a/interfaces/kits/js/@ohos.app.abilityManager.d.ts b/interfaces/kits/js/@ohos.app.abilityManager.d.ts index f9393afbaf2..f5b7ce142bb 100644 --- a/interfaces/kits/js/@ohos.app.abilityManager.d.ts +++ b/interfaces/kits/js/@ohos.app.abilityManager.d.ts @@ -22,7 +22,7 @@ import { ActiveProcessInfo } from './app/activeProcessInfo'; * * @since 7 * @SysCap SystemCapability.Appexecfwk - * @devices phone, tablet, tv, wearable + * @devices phone, tablet, tv, wearable, car * @import import abilityManager from '@ohos.app.abilityManager' * @permission N/A */ @@ -39,7 +39,7 @@ declare namespace abilityManager { * Get information about running processes * @since 7 * @SysCap SystemCapability.Appexecfwk - * @devices phone, tablet, tv, wearable + * @devices phone, tablet, tv, wearable, car * @return a list of ActiveProcessInfo records describing each process. * @permission ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION * @systemapi hide this for inner system use @@ -51,7 +51,7 @@ declare namespace abilityManager { * Get information about the running ability missions * @since 7 * @SysCap SystemCapability.Appexecfwk - * @devices phone, tablet, tv, wearable + * @devices phone, tablet, tv, wearable, car * @param upperLimit The maximum number of mission infos to return in the array. * @return an array of AbilityMissionInfo records describing each active mission. * @permission ohos.permission.ACCESS_MISSIONS @@ -64,7 +64,7 @@ declare namespace abilityManager { * Get information about recently run missions * @since 7 * @SysCap SystemCapability.Appexecfwk - * @devices phone, tablet, tv, wearable + * @devices phone, tablet, tv, wearable, car * @param upperLimit The maximum number of previous mission infos to return in the array. * @return an array of AbilityMissionInfo records describing each of the previous mission. * @permission ohos.permission.ACCESS_MISSIONS_EXTRA @@ -77,7 +77,7 @@ declare namespace abilityManager { * Delete the specified missions * @since 7 * @SysCap SystemCapability.Appexecfwk - * @devices phone, tablet, tv, wearable + * @devices phone, tablet, tv, wearable, car * @param missionIds An array of missions, representing the missions that need to be deleted. * @permission ohos.permission.DELETE_MISSIONS * @systemapi hide this for inner system use diff --git a/interfaces/kits/js/@ohos.bundle.d.ts b/interfaces/kits/js/@ohos.bundle.d.ts index 096175baf74..6e9ff46b8dd 100644 --- a/interfaces/kits/js/@ohos.bundle.d.ts +++ b/interfaces/kits/js/@ohos.bundle.d.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + import { AsyncCallback, Callback } from './basic'; import { ApplicationInfo } from './bundle/applicationInfo'; import { BundleInfo } from './bundle/bundleInfo'; @@ -21,14 +22,6 @@ import { BundleInstaller } from './bundle/bundleInstaller'; import { ElementName } from './bundle/elementName'; import { ShortcutInfo } from './bundle/shortcutInfo'; import { ModuleUsageRecord } from './bundle/moduleUsageRecord'; -import permission from './@ohos.security.permission'; - -/** - * @default Indicates the permission event - * @since 7 - * @SysCap SystemCapability.Appexecfwk - */ -type PermissionEvent = 'permissionChange' | 'anyPermissionChange'; /** * bundle. @@ -40,28 +33,28 @@ type PermissionEvent = 'permissionChange' | 'anyPermissionChange'; */ declare namespace bundle { - /** - * @name BundleFlag - * @since 7 - * @SysCap SystemCapability.Appexecfwk - * @import NA - * @permission NA - * @devices phone, tablet, tv, wearable - */ +/** + * @name BundleFlag + * @since 7 + * @SysCap SystemCapability.Appexecfwk + * @import NA + * @permission NA + * @devices phone, tablet, tv, wearable + */ enum BundleFlag { GET_BUNDLE_DEFAULT = 0x00000000, GET_BUNDLE_WITH_ABILITIES = 0x00000001, GET_APPLICATION_INFO_WITH_PERMISSION = 0x00000008, } - /** - * @name GrantStatus - * @since 7 - * @SysCap SystemCapability.Appexecfwk - * @import NA - * @permission NA - * @devices phone, tablet, tv, wearable - */ +/** + * @name GrantStatus + * @since 7 + * @SysCap SystemCapability.Appexecfwk + * @import NA + * @permission NA + * @devices phone, tablet, tv, wearable + */ export enum GrantStatus { PERMISSION_DENIED = -1, PERMISSION_GRANTED = 0, @@ -220,6 +213,7 @@ declare namespace bundle { * @permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED,ohos.permission.GET_BUNDLE_INFO */ function getBundleInfo(bundleName: string, bundleFlags: number, callback: AsyncCallback): void; + function getBundleInfo(bundleName: string, bundleFlags: number): Promise; /** * Obtains the interface used to install bundles. @@ -247,7 +241,7 @@ declare namespace bundle { * @permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED, ohos.permission.GET_BUNDLE_INFO */ function getApplicationInfo(bundleName: string, bundleFlags: number, userId: number, callback: AsyncCallback) : void; - function getApplicationInfo(bundleName: string, bundleFlags: number, userId?: number) : Promise; + function getApplicationInfo(bundleName: string, bundleFlags: number, userId: number) : Promise; /** * Checks whether a specified bundle has been granted a specific permission. @@ -263,44 +257,35 @@ declare namespace bundle { function checkPermission(bundleName: string, permission: string): Promise; /** - * Obtains BundleInfo of all bundles available in the system. - * - * @devices phone, tablet, tv, wearable - * @since 7 - * @SysCap SystemCapability.Appexecfwk - * @param flags Indicates the flag used to specify information contained in the BundleInfo that will be - * returned. - * @return Returns a list of BundleInfo objects. - * @permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED - */ - function getAllBundleInfo(bundlelFlag: BundleFlag, callback: AsyncCallback>) : void; - function getAllBundleInfo(bundlelFlag: BundleFlag) : Promise>; - - /** - * Registers a callback for listening for permission changes of all UIDs. + * Query the AbilityInfo by the given Want. * * @devices phone, tablet, tv, wearable * @since 7 * @SysCap SystemCapability.Appexecfwk - * @param - * @return - * @permission ohos.permission.LISTEN_PERMISSION_CHANGE + * @param intent Indicates the Intent containing the application bundle name to + * be queried. + * @param flags Indicates the flag used to specify information contained in the AbilityInfo objects that + * will be returned. + * @param userId Indicates the user ID. + * @return Returns a list of AbilityInfo objects. + * @permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED, ohos.permission.GET_BUNDLE_INFO */ - function on(type: PermissionEvent, callback: Callback): void; - function on(type: PermissionEvent, uids: Array, callback: Callback): void; + function queryAbilityByWant(want: Want, bundleFlags: number, userId: number, callback: AsyncCallback>): void; + function queryAbilityByWant(want: Want, bundleFlags: number, userId:number): Promise>; /** - * Unregister a specified callback for listening for permission changes. + * Obtains BundleInfo of all bundles available in the system. * * @devices phone, tablet, tv, wearable * @since 7 * @SysCap SystemCapability.Appexecfwk - * @param - * @return - * @permission ohos.permission.LISTEN_PERMISSION_CHANGE + * @param flags Indicates the flag used to specify information contained in the BundleInfo that will be + * returned. + * @return Returns a list of BundleInfo objects. + * @permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED */ - function off(type: PermissionEvent, callback?: Callback):void; - function off(type: PermissionEvent, uids: Array, callback?: Callback): void; + function getAllBundleInfo(bundlelFlag: BundleFlag, callback: AsyncCallback>) : void; + function getAllBundleInfo(bundlelFlag: BundleFlag) : Promise>; /** * Obtains information about all installed applications of a specified user. @@ -315,7 +300,7 @@ declare namespace bundle { * @permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED */ function getAllApplicationInfo(bundleFlags: number, userId: number, callback: AsyncCallback>) : void; - function getAllApplicationInfo(bundleFlags: number, userId?: number) : Promise>; + function getAllApplicationInfo(bundleFlags: number, userId: number) : Promise>; /** * Obtains information about an application bundle contained in an ohos Ability Package (HAP). @@ -357,18 +342,6 @@ declare namespace bundle { */ function getModuleUsageRecords(maxNum: number, callback: AsyncCallback>): void; function getModuleUsageRecords(maxNum: number): Promise>; - - /** - * Obtains detailed information about a specified permission. - * - * @devices phone, tablet, tv, wearable - * @since 7 - * @SysCap SystemCapability.Appexecfwk - * @param permissionName Indicates the name of the permission. - * @return Returns PermissionDef. - */ - function getPermissionDef(permissionName: string, callback: AsyncCallback) : void; - function getPermissionDef(permissionName: string) : Promise; } export default bundle; diff --git a/interfaces/kits/js/ability/dataAbilityHelper.d.ts b/interfaces/kits/js/ability/dataAbilityHelper.d.ts index 9ac55382f67..b37781aec0f 100644 --- a/interfaces/kits/js/ability/dataAbilityHelper.d.ts +++ b/interfaces/kits/js/ability/dataAbilityHelper.d.ts @@ -13,7 +13,7 @@ * limitations under the License. */ -import { AsyncCallback } from './../.basic'; +import { AsyncCallback } from './../basic'; import { DataAbilityOperation } from './dataAbilityOperation'; import { ValuesBucket, ResultSet, DataAbilityPredicates } from './../@ohos.data.rdb'; diff --git a/interfaces/kits/js/ability/startAbilityParameter.d.ts b/interfaces/kits/js/ability/startAbilityParameter.d.ts index 3e449112768..a628cffd1d3 100644 --- a/interfaces/kits/js/ability/startAbilityParameter.d.ts +++ b/interfaces/kits/js/ability/startAbilityParameter.d.ts @@ -38,11 +38,13 @@ export interface StartAbilityParameter { /** * Indicates the special start setting used in starting ability with TaskSyncAnimation. + *

Requires Permission: {@code ohos.permission.CONTROL_TASK_SYNC_ANIMATOR} * * @default - * @devices phone, tablet - * @since 8 + * @permission ohos.permission.CONTROL_TASK_SYNC_ANIMATOR + * @since 7 * @SysCap aafwk */ taskSyncAnimationOptions?: TaskSyncAnimationOptions; -} \ No newline at end of file +} diff --git a/interfaces/kits/js/ability/want.d.ts b/interfaces/kits/js/ability/want.d.ts index 90a6b23ddd5..af2183ded2e 100644 --- a/interfaces/kits/js/ability/want.d.ts +++ b/interfaces/kits/js/ability/want.d.ts @@ -21,7 +21,7 @@ * @devices phone, tablet * @permission N/A */ - export declare interface Want { +export declare interface Want { /** * device id * @default - diff --git a/interfaces/kits/js/app/abilityMissionInfo.d.ts b/interfaces/kits/js/app/abilityMissionInfo.d.ts old mode 100644 new mode 100755 index eb7d23cb39e..bd219349286 --- a/interfaces/kits/js/app/abilityMissionInfo.d.ts +++ b/interfaces/kits/js/app/abilityMissionInfo.d.ts @@ -21,37 +21,37 @@ import { ElementName } from '../bundle/elementName'; * @SysCap SystemCapability.Appexecfwk * @import import AbilityMissionInfo from 'app/abilityMissionInfo' * @permission N/A - * @devices phone, tablet, tv, wearable + * @devices phone, tablet, tv, wearable, car */ export interface AbilityMissionInfo { - /** - * @default Unique identification of task stack information corresponding to ability - * @since 7 - * @SysCap SystemCapability.Appexecfwk - */ - missionId: number; + /** + * @default Unique identification of task stack information corresponding to ability + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + missionId: number; - /** - * @default The component launched as the first ability in the task stack - * This can be considered the "application" of this task stack - * @since 7 - * @SysCap SystemCapability.Appexecfwk - */ - bottomAbility: ElementName; + /** + * @default The component launched as the first ability in the task stack + * This can be considered the "application" of this task stack + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + bottomAbility: ElementName; - /** - * @default The ability component at the top of the history stack of the task - * This is what the user is currently doing - * @since 7 - * @SysCap SystemCapability.Appexecfwk - */ - topAbility: ElementName; + /** + * @default The ability component at the top of the history stack of the task + * This is what the user is currently doing + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + topAbility: ElementName; - /** - * @default The corresponding ability description information in the task stack - * @since 7 - * @SysCap SystemCapability.Appexecfwk - */ - windowMode: number; + /** + * @default The corresponding ability description information in the task stack + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + windowMode: number; } diff --git a/interfaces/kits/js/app/activeProcessInfo.d.ts b/interfaces/kits/js/app/activeProcessInfo.d.ts old mode 100644 new mode 100755 index 865f77595c8..4ab5a6aa3d1 --- a/interfaces/kits/js/app/activeProcessInfo.d.ts +++ b/interfaces/kits/js/app/activeProcessInfo.d.ts @@ -13,14 +13,13 @@ * limitations under the License. */ - /** * @name This class saves process information about an application * @since 7 * @SysCap SystemCapability.Appexecfwk * @import import app from 'app/activeProcessInfo' * @permission N/A - * @devices phone, tablet, tv, wearable + * @devices phone, tablet, tv, wearable, car */ export interface ActiveProcessInfo { /** diff --git a/interfaces/kits/js/app/activeServiceAbilityInfo.d.ts b/interfaces/kits/js/app/activeServiceAbilityInfo.d.ts new file mode 100755 index 00000000000..8e9c14e1142 --- /dev/null +++ b/interfaces/kits/js/app/activeServiceAbilityInfo.d.ts @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ElementName } from '../bundle/elementName'; + +/** + * @name information corresponding to active service ability + * @since 7 + * @SysCap SystemCapability.Appexecfwk + * @import import AbilityMissionInfo from 'app/activeServiceAbilityInfo' + * @permission N/A + * @devices phone, tablet, tv, wearable, car + */ +export interface ActiveServiceAbilityInfo { + /** + * @default process id + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + pid: number; + + /** + * @default user id + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + uid: number; + + /** + * @default the name of the process + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + processName: string; + + /** + * @default The element name of the service ability + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + serviceAbility: ElementName; +} + diff --git a/interfaces/kits/js/app/context.d.ts b/interfaces/kits/js/app/context.d.ts index 2dc41d7f460..4ccb5f05a9e 100755 --- a/interfaces/kits/js/app/context.d.ts +++ b/interfaces/kits/js/app/context.d.ts @@ -15,12 +15,55 @@ import { AsyncCallback } from '../basic'; +/** + * The context of an ability or an application. It allows access to + * application-specific resources, request and verification permissions. + * Can only be obtained through the ability. + * + * @since 6 + * @SysCap SystemCapability.Appexecfwk + * @devices phone, tablet, tv, wearable, car + * @import import abilityManager from 'app/context' + * @permission N/A + */ +export interface Context { + + /** + * Verify whether the specified permission is allowed for a particular + * pid and uid running in the system. + * @param permission The name of the specified permission + * @param pid process id + * @param uid user id + * @note Pid and uid are optional. If you do not pass in pid and uid, + * it will check your own permission. + * @since 7 + * @sysCap SystemCapability.Appexecfwk + * @devices phone, tablet, tv, wearable, car + * @return asynchronous callback with {@code 0} if the PID + * and UID have the permission; callback with {@code -1} otherwise. + */ + verifyPermission(permission: string, options?: PermissionOptions): Promise; + verifyPermission(permission: string, options: PermissionOptions, callback: AsyncCallback): void; + verifyPermission(permission: string, callback: AsyncCallback): void; + + /** + * Requests certain permissions from the system. + * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null. + * @param requestCode Indicates the request code to be passed to the PermissionRequestResult + * @since 7 + * @sysCap SystemCapability.Appexecfwk + * @devices phone, tablet, tv, wearable, car + */ + requestPermissionsFromUser(permissions: Array, requestCode: number, resultCallback: AsyncCallback): void; + +} + /** * @name the result of requestPermissionsFromUser with asynchronous callback * @since 7 * @SysCap SystemCapability.Appexecfwk * @permission N/A - * @devices phone, tablet, tv, wearable + * @devices phone, tablet, tv, wearable, car */ interface PermissionRequestResult { /** @@ -47,52 +90,16 @@ interface PermissionRequestResult { interface PermissionOptions { /** - * @default The process id - * @since 7 - * @SysCap SystemCapability.Appexecfwk - */ + * @default The process id + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ pid?: number; /** - * @default The user id - * @since 7 - * @SysCap SystemCapability.Appexecfwk - */ - uid?: number; -} - -/** -* Requests certain permissions from the system. -* @param permissions Indicates the list of permissions to be requested. This parameter cannot be null. -* @param requestCode Indicates the request code to be passed to the PermissionRequestResult -* @since 7 -* @sysCap SystemCapability.Appexecfwk -* @devices phone, tablet, tv, wearable -*/ -export interface Context { - /** - * Verify whether the specified permission is allowed for a particular - * pid and uid running in the system. - * @param permission The name of the specified permission - * @param options process id and user id - * @since 7 - * @sysCap SystemCapability.Appexecfwk - * @devices phone, tablet, tv, wearable - * @return asynchronous callback with {@code 0} if the PID - * and UID have the permission; callback with {@code -1} otherwise. - */ - verifyPermission(permission: string, callback: AsyncCallback): void; - verifyPermission(permission: string, options: PermissionOptions, callback: AsyncCallback): void; - verifyPermission(permission: string, options?: PermissionOptions): Promise; - - /** - * Requests certain permissions from the system. - * required for granting a certain permission. - * @devices phone + * @default The user id * @since 7 - * @sysCap AAFwk - * @param - - * @return Return grant result + * @SysCap SystemCapability.Appexecfwk */ - requestPermissionsFromUser(permissions: Array, requestCode: number, resultCallback: AsyncCallback): void; -} \ No newline at end of file + uid?: number; +} diff --git a/interfaces/kits/js/app/memoryMapInfo.d.ts b/interfaces/kits/js/app/memoryMapInfo.d.ts new file mode 100755 index 00000000000..4c8ab45c3b1 --- /dev/null +++ b/interfaces/kits/js/app/memoryMapInfo.d.ts @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @name This class saves memory map information about an application. + * All results are in kB. + * @since 7 + * @SysCap SystemCapability.Appexecfwk + * @import import MemoryMapInfo from 'app/memoryMapInfo' + * @permission N/A + * @devices phone, tablet, tv, wearable, car + * @systemapi hide this for inner system use + */ +export interface MemoryMapInfo { + /** + * @default The pss page size for virtual machine heap + */ + vmPss: number; + /** + * @default The private dirty page size for virtual machine heap + */ + vmPrivateDirty: number; + /** + * @default The shared dirty page size for virtual machine heap + */ + vmSharedDirty: number; + /** + * @default The pss page size for native heap + */ + nativePss: number; + /** + * @default The private dirty page size for native heap + */ + nativePrivateDirty: number; + /** + * @default The shared dirty page size for native heap + */ + nativeSharedDirty: number; + /** + * @default The pss page size for others + */ + otherPss: number; + /** + * @default The private dirty page size for others + */ + otherPrivateDirty: number; + /** + * @default The shared dirty page size for others + */ + otherSharedDirty: number; +} diff --git a/interfaces/kits/napi/aafwk/context/napi_error.h b/interfaces/kits/js/app/missionSnapshot.d.ts old mode 100644 new mode 100755 similarity index 52% rename from interfaces/kits/napi/aafwk/context/napi_error.h rename to interfaces/kits/js/app/missionSnapshot.d.ts index f2199a40c7d..01ae787ee8c --- a/interfaces/kits/napi/aafwk/context/napi_error.h +++ b/interfaces/kits/js/app/missionSnapshot.d.ts @@ -13,9 +13,25 @@ * limitations under the License. */ -#ifndef NAPI_CONTEXT_ERROR_H -#define NAPI_CONTEXT_ERROR_H +import { ElementName } from '../bundle/elementName'; +import { image } from '../@ohos.multimedia.image'; -#define NAPI_ERR_NO_PERMISSION -100 +/** + * @name This class represents a mission snapshot. + * @since 7 + * @SysCap appexecfwk + * @import import MissionSnapshot from 'app/missionSnapshot' + * @devices phone, tablet + * @systemapi hide this for inner system use + */ +export interface MissionSnapshot { + /** + * @default The top ability in this mission snapshot + */ + topAbility: ElementName; -#endif + /** + * @default The entity of snapshot + */ + snapshot: image.PixelMap; +} diff --git a/interfaces/kits/js/app/processErrorInfo.d.ts b/interfaces/kits/js/app/processErrorInfo.d.ts new file mode 100755 index 00000000000..f08a28f7059 --- /dev/null +++ b/interfaces/kits/js/app/processErrorInfo.d.ts @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ProcessErrCode } from '../@ohos.app.abilityManager.d.ts'; + +/** + * @name This class saves process error information + * @since 7 + * @SysCap appexecfwk + * @import import app from 'app/processErrorInfo' + * @devices phone, tablet + * @systemapi hide this for inner system use + */ +export interface ProcessErrorInfo { + /** + * @default Error reason, see { ProcessErrCode } in abilityManager + */ + errStatus: ProcessErrCode; + /** + * @default the name of the error process + */ + processName: string; + /** + * @default the process ID + */ + pid: number; + /** + * @default the user ID + */ + uid: number; + /** + * @default the name of the ability corresponding with the error + */ + abilityName: string; + /** + * @default the error message + */ + errMsg: string; + /** + * @default the error trace, for you to find the original error + */ + backTrace: string; +} + diff --git a/interfaces/kits/js/app/processInfo.d.ts b/interfaces/kits/js/app/processInfo.d.ts deleted file mode 100644 index 8af0490e0c4..00000000000 --- a/interfaces/kits/js/app/processInfo.d.ts +++ /dev/null @@ -1,44 +0,0 @@ -/* -* Copyright (c) 2021 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/** - * @name This class saves process information about an application - * @since 6 - * @SysCap appexecfwk - * @import import app from '@system.app' - * @permission N/A - * @devices phone - */ - export interface ProcessInfo { - /** - * The id of the current process - * - * @default - - * @devices phone - * @since 6 - * @SysCap appexecfwk - */ - pid: number; - - /** - * The name of the current process - * - * @default - - * @devices phone - * @since 6 - * @SysCap appexecfwk - */ - processName: string; -} diff --git a/interfaces/kits/js/app/splitMissionInfo.d.ts b/interfaces/kits/js/app/splitMissionInfo.d.ts new file mode 100755 index 00000000000..6f911d73360 --- /dev/null +++ b/interfaces/kits/js/app/splitMissionInfo.d.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { RectBounds } from '../wallpaper'; + +/** + * @name This class is the input param for { moveMissionToFirstSplitScreen }. + * @since 7 + * @SysCap appexecfwk + * @import import app from 'app/splitMissionInfo' + * @devices phone, tablet + * @systemapi hide this for inner system use + */ +export interface SplitMissionInfo { + /** + * @default the mission id that you want to move + */ + missionId: number; + /** + * @default whether the target window is the first one + */ + isFirstWindow: boolean; + /** + * @default whether to move the mission to top + */ + isToTop: boolean; + /** + * @default Whether you want an animation when moving the task + */ + isAnimating: boolean; + /** + * @default The bounds of the split window + */ + initialBounds: RectBounds; +} diff --git a/interfaces/kits/js/app/systemMemoryAttr.d.ts b/interfaces/kits/js/app/systemMemoryAttr.d.ts new file mode 100755 index 00000000000..eee6e70681a --- /dev/null +++ b/interfaces/kits/js/app/systemMemoryAttr.d.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @name This class saves system memory attributes + * @since 7 + * @SysCap SystemCapability.Appexecfwk + * @import import MemoryMapInfo from 'app/systemMemoryAttr' + * @devices phone, tablet, tv, wearable, car + * @systemapi hide this for inner system use + */ + export interface SystemMemoryAttr { + /** + * @default The available memory on the system + */ + availSysMem: number; + /** + * @default The total memory on the system + */ + totalSysMem: number; + /** + * @default The low memory threshold under which the system + * will kill background processes + */ + threshold: number; + /** + * @default Whether the system is in low memory status + */ + isSysInlowMem: boolean; +} + + diff --git a/interfaces/kits/js/bundle/hapModuleInfo.d.ts b/interfaces/kits/js/bundle/hapModuleInfo.d.ts new file mode 100644 index 00000000000..a62a66955d3 --- /dev/null +++ b/interfaces/kits/js/bundle/hapModuleInfo.d.ts @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { AbilityInfo } from "./abilityInfo"; + +/** + * @name Obtains configuration information about an module. + * @since 7 + * @SysCap SystemCapability.Appexecfwk + * @permission NA + * @devices phone, tablet, tv, wearable, car + */ +export interface HapModuleInfo { + /** + * @default Indicates the name of this hapmodule + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly name: string; + /** + * @default Describes the hapmodule + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly description: string; + /** + * @default Indicates the description of this hapmodule + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly descriptionId: number; + /** + * @default Indicates the icon of this hapmodule + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly icon: string; + /** + * @default Indicates the label of this hapmodule + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly label: string; + /** + * @default Indicates the label id of this hapmodule + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly labelId: number; + /** + * @default Indicates the icon id of this hapmodule + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly iconId: number; + /** + * @default Indicates the background img of this hapmodule + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly backgroundImg: string; + /** + * @default Indicates the supported modes of this hapmodule + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly supportedModes: number; + /** + * @default Indicates the req capabilities of this hapmodule + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly reqCapabilities: Array; + /** + * @default The device types that this hapmodule can run on + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly deviceTypes: Array; + /** + * @default Obtains configuration information about ability + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly abilityInfo: Array; + /** + * @default Indicates the name of the .hap package to which the capability belongs + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly moduleName: string; + /** + * @default Indicates the main ability name of this hapmodule + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly mainAbilityName: string; + /** + * @default Indicates whether free installation of the hapmodule is supported + * @since 7 + * @SysCap SystemCapability.Appexecfwk + */ + readonly installationFree: boolean; +} \ No newline at end of file diff --git a/interfaces/kits/napi/aafwk/context/BUILD.gn b/interfaces/kits/napi/aafwk/context/BUILD.gn deleted file mode 100644 index 373c8d581bc..00000000000 --- a/interfaces/kits/napi/aafwk/context/BUILD.gn +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# import("//build/config/ohos/rules.gni") -import("//build/ohos.gni") -ohos_shared_library("context") { - include_dirs = [ - "//foundation/ace/napi/interfaces/kits", - "//third_party/node/src", - "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/featureAbility", - "//foundation/aafwk/standard/frameworks/kits/ability/native/include", - "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/context", - ] - - sources = [ - "napi_context.cpp", - "native_module.cpp", - ] - - deps = [ - "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", - "//foundation/ace/napi:ace_napi", - "//foundation/appexecfwk/standard/kits:appkit_native", - "//utils/native/base:utils", - ] - - external_deps = [ - "aafwk_standard:ability_manager", - "aafwk_standard:want", - "appexecfwk_standard:appexecfwk_base", - "appexecfwk_standard:appexecfwk_core", - "appexecfwk_standard:libeventhandler", - "communication_L2:ipc_core", - "hiviewdfx_hilog_native:libhilog", - ] - - relative_install_dir = "module" - - subsystem_name = "aafwk" - part_name = "aafwk_standard" -} diff --git a/interfaces/kits/napi/aafwk/context/napi_context.cpp b/interfaces/kits/napi/aafwk/context/napi_context.cpp deleted file mode 100644 index 770eb523111..00000000000 --- a/interfaces/kits/napi/aafwk/context/napi_context.cpp +++ /dev/null @@ -1,876 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include -#include "securec.h" -#include "napi_context.h" -#include "hilog_wrapper.h" -#include "ability_process.h" -#include "napi_error.h" - -using namespace OHOS::AAFwk; -using namespace OHOS::AppExecFwk; - -#define PERMISSOIN_EVENT_VERIFYSELFPERMISSION 1 -#define PERMISSION_EVENT_REQUEST_PERMISSION 2 - -namespace OHOS { -namespace AppExecFwk { -CallbackInfo aceCallbackInfoPermission; - -static void VerifySelfPermissionExecuteCallback(napi_env env, void *data); -static void RequestPermissionsFromUserExecuteCallback(napi_env env, void *data); -static void RequestPermissionsFromUserExecutePromise(napi_env env, void *data); -static napi_value NAPI_RequestPermissionsFromUserWrap( - napi_env env, napi_callback_info info, AsyncPermissionCallbackInfo *asyncCallbackInfo); -static napi_value NAPI_VerifySelfPermissionWrap( - napi_env env, napi_callback_info info, AsyncPermissionCallbackInfo *asyncCallbackInfo); -static napi_value NAPI_VerifySelfPermission(napi_env env, napi_callback_info info); -static napi_value NAPI_RequestPermissionsFromUser(napi_env env, napi_callback_info info); -static napi_value NAPI_OnRequestPermissionsFromUserResultWrap( - napi_env env, napi_callback_info info, AsyncPermissionCallbackInfo *asyncCallbackInfo); -static napi_value NAPI_OnRequestPermissionsFromUserResult(napi_env env, napi_callback_info info); -static void GetBundleNameExecuteCallback(napi_env env, void *data); -static napi_value NAPI_GetBundleNameWrap( - napi_env env, napi_callback_info info, AsyncPermissionCallbackInfo *asyncCallbackInfo); -static napi_value NAPI_GetBundleName(napi_env env, napi_callback_info info); - -/** - * @brief Context NAPI module registration. - * - * @param env The environment that the Node-API call is invoked under. - * @param exports An empty object via the exports parameter as a convenience. - * - * @return The return value from Init is treated as the exports object for the module. - */ -napi_value ContextPermissionInit(napi_env env, napi_value exports) -{ - HILOG_INFO("Context::ContextPermissionInit called."); - - napi_property_descriptor properties[] = { - DECLARE_NAPI_FUNCTION("verifySelfPermission", NAPI_VerifySelfPermission), - DECLARE_NAPI_FUNCTION("requestPermissionsFromUser", NAPI_RequestPermissionsFromUser), - DECLARE_NAPI_FUNCTION("onRequestPermissionsFromUserResult", NAPI_OnRequestPermissionsFromUserResult), - DECLARE_NAPI_FUNCTION("getBundleName", NAPI_GetBundleName), - }; - - NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties)); - return exports; -} - -void ClearNativeData(ThreadReturnData *data) -{ - if (data != nullptr) { - data->data_type = NVT_NONE; - data->int32_value = 0; - data->bool_value = false; - data->str_value = ""; - } -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AsyncPermissionCallbackInfo on success, nullptr on failure - */ -AsyncPermissionCallbackInfo *CreateAsyncPermissionCallbackInfo(napi_env env) -{ - napi_value global = 0; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = 0; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - AsyncPermissionCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncPermissionCallbackInfo{ - .env = env, - .asyncWork = nullptr, - .deferred = nullptr, - .ability = ability, - .aceCallback = nullptr, - }; - if (asyncCallbackInfo != nullptr) { - ClearNativeData(&asyncCallbackInfo->native_data); - } - return asyncCallbackInfo; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * @param argc Number of parameters. - * @param startIndex The position of the first event parameter. - * @param args Parameter list. - * @param callback Point to asynchronous processing of data. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value CreateAsyncCallback( - napi_env env, size_t argc, size_t startIndex, napi_value *args, AsyncPermissionCallbackInfo *callback) -{ - if (args == nullptr) { - return nullptr; - } - - size_t argCount = (argc - startIndex > PERMISSION_EVENT_SIZE) ? (startIndex + PERMISSION_EVENT_SIZE) : argc; - - for (size_t i = startIndex; i < argCount; i++) { - napi_valuetype valueType = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[i], &valueType)); - if (valueType != napi_function) { - return nullptr; - } - NAPI_CALL(env, napi_create_reference(env, args[i], 1, &callback->callback[i - startIndex])); - } - - napi_value result = 0; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; -} - -/** - * @brief Parse JS string parameters. - * - * @param env The environment that the Node-API call is invoked under. - * @param value Return stirng value. - * @param args Parameter list. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value UnwrapParamString(std::vector &value, napi_env env, napi_value args) -{ - HILOG_INFO("Context::UnwrapParamString in."); - char buf[PERMISSION_C_BUFFER_SIZE] = {0}; - size_t len = 0; - - // unwrap the param : permission - memset_s(buf, sizeof(buf), 0x0, sizeof(buf)); - NAPI_CALL(env, napi_get_value_string_utf8(env, args, buf, PERMISSION_C_BUFFER_SIZE, &len)); - value.push_back(std::string(buf)); - HILOG_INFO("Context::UnwrapParamString permission=%{public}s.", buf); - - // create result code - napi_value result = 0; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; -} - -/** - * @brief Parse JS numeric parameters. - * - * @param env The environment that the Node-API call is invoked under. - * @param value Return integer value. - * @param args Parameter list. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value UnwrapParamInt(int *value, napi_env env, napi_value args) -{ - if (value == nullptr) { - HILOG_INFO("Invalid param(value = nullptr)"); - return nullptr; - } - - napi_valuetype valueType = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args, &valueType)); - if (valueType != napi_number) { - return nullptr; - } - - int32_t value32 = 0; - NAPI_CALL(env, napi_get_value_int32(env, args, &value32)); - HILOG_INFO("UnwrapParamInt value=%{public}d", value32); - *value = value32; - napi_value result = 0; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; -} - -/** - * @brief Parse JS array of string parameters. - * - * @param env The environment that the Node-API call is invoked under. - * @param value Return array of stirng value. - * @param args Parameter list. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value UnwrapParamArrayString(std::vector &value, napi_env env, napi_value args) -{ - value.clear(); - char buf[PERMISSION_C_BUFFER_SIZE] = {0}; - size_t len = 0; - napi_valuetype valueType = napi_undefined; - - bool isArray = false; - uint32_t arrayLength = 0; - napi_value valueArray = 0; - - NAPI_CALL(env, napi_is_array(env, args, &isArray)); - if (!isArray) { - HILOG_INFO("property type mismatch"); - return nullptr; - } - - NAPI_CALL(env, napi_get_array_length(env, args, &arrayLength)); - HILOG_INFO("property is array, length=%{public}d", arrayLength); - - for (uint32_t i = 0; i < arrayLength; i++) { - memset_s(buf, sizeof(buf), 0x0, sizeof(buf)); - - NAPI_CALL(env, napi_get_element(env, args, i, &valueArray)); - NAPI_CALL(env, napi_typeof(env, valueArray, &valueType)); - if (valueType != napi_string) { - return nullptr; - } - - NAPI_CALL(env, napi_get_value_string_utf8(env, valueArray, buf, PERMISSION_C_BUFFER_SIZE, &len)); - HILOG_INFO("UnwrapParamInt array string value=%{public}s", buf); - - value.emplace_back(std::string(buf)); - } - - napi_value result = 0; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; -} - -/** - * @brief Convert local data to JS data. - * - * @param env The environment that the Node-API call is invoked under. - * @param data The local data. - * @param value the JS data. - * - * @return The return value from NAPI C++ to JS for the module. - */ -void NAPI_GetNapiValue(napi_env env, const ThreadReturnData *data, napi_value *value) -{ - if (data == nullptr || value == nullptr) { - return; - } - switch (data->data_type) { - case NVT_INT32: - napi_create_int32(env, data->int32_value, value); - break; - case NVT_BOOL: - napi_create_int32(env, data->bool_value ? 1 : 0, value); - break; - case NVT_STRING: - napi_create_string_utf8(env, data->str_value.c_str(), NAPI_AUTO_LENGTH, value); - break; - default: - napi_get_null(env, value); - break; - } -} - -/** - * @brief VerifySelfPermission asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void VerifySelfPermissionExecuteCallback(napi_env env, void *data) -{ - HILOG_INFO("Context::NAPI_VerifySelfPermission worker pool thread execute."); - - AsyncPermissionCallbackInfo *asyncCallbackInfo = (AsyncPermissionCallbackInfo *)data; - if (asyncCallbackInfo != nullptr) { - int rev = -1; - if (asyncCallbackInfo->ability != nullptr && asyncCallbackInfo->param.permission_list.size() > 0) { - rev = asyncCallbackInfo->ability->VerifySelfPermission(asyncCallbackInfo->param.permission_list[0]); - } - - asyncCallbackInfo->native_data.data_type = NVT_INT32; - asyncCallbackInfo->native_data.int32_value = rev; - - asyncCallbackInfo->run_status = (rev == 0) ? true : false; - if (asyncCallbackInfo->run_status == false) { - asyncCallbackInfo->error_code = NAPI_ERR_NO_PERMISSION; - } - } -} - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void AsyncCompleteCallback(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("main event thread complete."); - AsyncPermissionCallbackInfo *asyncCallbackInfo = (AsyncPermissionCallbackInfo *)data; - if (asyncCallbackInfo == nullptr) { - return; - } - - const size_t cbCount = 2; - napi_value callback = 0; - napi_value undefined = 0; - napi_get_undefined(env, &undefined); - napi_value result = 0; - napi_value callResult = 0; - NAPI_GetNapiValue(env, &asyncCallbackInfo->native_data, &result); - if (asyncCallbackInfo->run_status) { - if (asyncCallbackInfo->callback[0] != nullptr) { - napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback); - napi_call_function(env, undefined, callback, 1, &result, &callResult); - - napi_delete_reference(env, asyncCallbackInfo->callback[0]); - } - } else { - if (asyncCallbackInfo->callback[1] != nullptr) { - napi_value rev[2] = {nullptr}; - napi_create_int32(env, asyncCallbackInfo->error_code, &rev[0]); - napi_create_int32(env, asyncCallbackInfo->run_status ? 0 : -1, &rev[1]); - napi_get_reference_value(env, asyncCallbackInfo->callback[1], &callback); - napi_call_function(env, undefined, callback, cbCount, rev, &callResult); - napi_delete_reference(env, asyncCallbackInfo->callback[1]); - } - } - - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; -} - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void PromiseCompleteCallback(napi_env env, napi_status status, void *data) -{ - AsyncPermissionCallbackInfo *asyncCallbackInfo = (AsyncPermissionCallbackInfo *)data; - if (asyncCallbackInfo != nullptr) { - napi_value result = 0; - NAPI_GetNapiValue(env, &asyncCallbackInfo->native_data, &result); - napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - } -} - -/** - * @brief Asynchronous callback processing. - * - * @param env The environment that the Node-API call is invoked under. - * @param args list of JS of param. - * @param asyncCallbackInfo Process data asynchronously. - * @param param other param. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value ExecuteAsyncCallback( - napi_env env, napi_value *args, AsyncPermissionCallbackInfo *asyncCallbackInfo, const AsyncParamEx *param) -{ - if (param == nullptr) { - return nullptr; - } - - napi_value resourceName = 0; - NAPI_CALL(env, napi_create_string_latin1(env, param->resource.c_str(), NAPI_AUTO_LENGTH, &resourceName)); - if (CreateAsyncCallback(env, param->argc, param->startIndex, args, asyncCallbackInfo) == nullptr) { - return nullptr; - } - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - param->execute, - param->complete, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork)); - - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - // create result code - napi_value result = 0; - NAPI_CALL(env, napi_get_null(env, &result)); - - return result; -} - -/** - * @brief Asynchronous promise processing. - * - * @param env The environment that the Node-API call is invoked under. - * @param args list of JS of param. - * @param asyncCallbackInfo Process data asynchronously. - * @param param other param. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value ExecutePromiseCallback( - napi_env env, napi_value *args, AsyncPermissionCallbackInfo *asyncCallbackInfo, const AsyncParamEx *param) -{ - if (args == nullptr) { - return nullptr; - } - - napi_value resourceName = 0; - NAPI_CALL(env, napi_create_string_latin1(env, param->resource.c_str(), NAPI_AUTO_LENGTH, &resourceName)); - - napi_deferred deferred = 0; - napi_value promise = 0; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - - asyncCallbackInfo->deferred = deferred; - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - param->execute, - param->complete, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork)); - - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - return promise; -} - -/** - * @brief VerifySelfPermission processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param asyncCallbackInfo Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value NAPI_VerifySelfPermissionWrap( - napi_env env, napi_callback_info info, AsyncPermissionCallbackInfo *asyncCallbackInfo) -{ - size_t argc = 3; - const size_t argCount = 1; - napi_value args[PERMISSION_ARGS_SIZE] = {nullptr}; - napi_value jsthis = 0; - void *data = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsthis, &data)); - - CallAbilityPermissionParam param; - if (UnwrapParamString(param.permission_list, env, args[0]) == nullptr) { - return nullptr; - } - asyncCallbackInfo->param = param; - - AsyncParamEx asyncParamEx; - asyncParamEx.argc = argc; - - if (argc > argCount) { - HILOG_INFO("Context::NAPI_VerifySelfPermission asyncCallback."); - asyncParamEx.resource = "NAPI_VerifySelfPermission1"; - asyncParamEx.startIndex = PERMISSOIN_EVENT_VERIFYSELFPERMISSION; - asyncParamEx.execute = VerifySelfPermissionExecuteCallback; - asyncParamEx.complete = AsyncCompleteCallback; - return ExecuteAsyncCallback(env, args, asyncCallbackInfo, &asyncParamEx); - } else { - HILOG_INFO("Context::NAPI_VerifySelfPermission promise."); - asyncParamEx.resource = "NAPI_VerifySelfPermission2"; - asyncParamEx.startIndex = 0; - asyncParamEx.execute = VerifySelfPermissionExecuteCallback; - asyncParamEx.complete = PromiseCompleteCallback; - - return ExecutePromiseCallback(env, args, asyncCallbackInfo, &asyncParamEx); - } -} - -/** - * @brief Checks whether the current process has the given permission. - * You need to call requestPermissionsFromUser(java.lang.std::string[],int) to request a permission only - * if the current process does not have the specific permission. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission; - * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise. - */ -napi_value NAPI_VerifySelfPermission(napi_env env, napi_callback_info info) -{ - HILOG_INFO("Context::NAPI_VerifySelfPermission. called."); - - AsyncPermissionCallbackInfo *asyncCallbackInfo = CreateAsyncPermissionCallbackInfo(env); - if (asyncCallbackInfo == nullptr) { - return nullptr; - } - - napi_value rev = NAPI_VerifySelfPermissionWrap(env, info, asyncCallbackInfo); - if (rev == nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - napi_value result = 0; - napi_get_null(env, &result); - return result; - } - return rev; -} - -/** - * @brief RequestPermissionsFromUser asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void RequestPermissionsFromUserExecuteCallback(napi_env env, void *data) -{ - HILOG_INFO("Context::NAPI_VerifySelfPermission worker pool thread execute."); - AsyncPermissionCallbackInfo *asyncCallbackInfo = (AsyncPermissionCallbackInfo *)data; - if (asyncCallbackInfo != nullptr) { - AbilityProcess::GetInstance()->RequestPermissionsFromUser( - asyncCallbackInfo->ability, asyncCallbackInfo->param, *asyncCallbackInfo->aceCallback); - asyncCallbackInfo->run_status = true; - asyncCallbackInfo->native_data.data_type = NVT_INT32; - asyncCallbackInfo->native_data.int32_value = 1; - } -} - -void RequestPermissionsFromUserExecutePromise(napi_env env, void *data) -{ - HILOG_INFO("Context::NAPI_VerifySelfPermission worker pool thread execute."); - AsyncPermissionCallbackInfo *asyncCallbackInfo = (AsyncPermissionCallbackInfo *)data; - if (asyncCallbackInfo != nullptr) { - AbilityProcess::GetInstance()->RequestPermissionsFromUser( - asyncCallbackInfo->ability, asyncCallbackInfo->param, *asyncCallbackInfo->aceCallback); - asyncCallbackInfo->run_status = true; - asyncCallbackInfo->native_data.data_type = NVT_NONE; - } -} - -/** - * @brief RequestPermissionsFromUser processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param asyncCallbackInfo Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value NAPI_RequestPermissionsFromUserWrap( - napi_env env, napi_callback_info info, AsyncPermissionCallbackInfo *asyncCallbackInfo) -{ - size_t argc = 4; - const size_t argCount = 2; - napi_value args[PERMISSION_ARGS_SIZE] = {nullptr}; - napi_value jsthis = 0; - void *data = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsthis, &data)); - - CallAbilityPermissionParam param; - if (UnwrapParamArrayString(param.permission_list, env, args[0]) == nullptr) { - return nullptr; - } - - if (UnwrapParamInt(¶m.requestCode, env, args[1]) == nullptr) { - return nullptr; - } - - asyncCallbackInfo->param = param; - asyncCallbackInfo->aceCallback = &aceCallbackInfoPermission; - AsyncParamEx asyncParamEx; - asyncParamEx.argc = argc; - if (argc > argCount) { - HILOG_INFO("Context::NAPI_RequestPermissionsFromUser asyncCallback."); - asyncParamEx.resource = "NAPI_RequestPermissionsFromUser_Callback"; - asyncParamEx.startIndex = PERMISSION_EVENT_REQUEST_PERMISSION; - asyncParamEx.execute = RequestPermissionsFromUserExecuteCallback; - asyncParamEx.complete = AsyncCompleteCallback; - return ExecuteAsyncCallback(env, args, asyncCallbackInfo, &asyncParamEx); - } else { - HILOG_INFO("Context::NAPI_RequestPermissionsFromUser promise."); - asyncParamEx.resource = "NAPI_RequestPermissionsFromUser_Promise"; - asyncParamEx.startIndex = 0; - asyncParamEx.execute = RequestPermissionsFromUserExecutePromise; - asyncParamEx.complete = PromiseCompleteCallback; - return ExecutePromiseCallback(env, args, asyncCallbackInfo, &asyncParamEx); - } -} - -/** - * @brief Requests certain permissions from the system. - * This method is called for permission request. This is an asynchronous method. When it is executed, - * the Ability.onRequestPermissionsFromUserResult(int, String[], int[]) method will be called back. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - */ -napi_value NAPI_RequestPermissionsFromUser(napi_env env, napi_callback_info info) -{ - HILOG_INFO("Context::NAPI_RequestPermissionsFromUser. called."); - AsyncPermissionCallbackInfo *asyncCallbackInfo = CreateAsyncPermissionCallbackInfo(env); - if (asyncCallbackInfo == nullptr) { - return nullptr; - } - - napi_value rev = NAPI_RequestPermissionsFromUserWrap(env, info, asyncCallbackInfo); - if (rev == nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - napi_value result = 0; - napi_get_null(env, &result); - return result; - } - return rev; -} - -/** - * @brief OnRequestPermissionsFromUserResult processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param asyncCallbackInfo Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value NAPI_OnRequestPermissionsFromUserResultWrap( - napi_env env, napi_callback_info info, AsyncPermissionCallbackInfo *asyncCallbackInfo) -{ - size_t argc = 2; - napi_value args[PERMISSION_ARGS_SIZE] = {nullptr}; - napi_value jsthis = 0; - void *data = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsthis, &data)); - - if (argc > 0) { - HILOG_INFO("Context::NAPI_OnRequestPermissionsFromUserResult"); - - napi_value resourceName = 0; - NAPI_CALL(env, - napi_create_string_latin1(env, "NAPI_OnRequestPermissionsFromUserResult", NAPI_AUTO_LENGTH, &resourceName)); - - aceCallbackInfoPermission.env = env; - size_t argcCount = (argc > PERMISSION_EVENT_SIZE) ? PERMISSION_EVENT_SIZE : argc; - for (size_t i = 0; i < argcCount; i++) { - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[i], &valuetype)); - if (valuetype != napi_function) { - return nullptr; - } - napi_create_reference(env, args[i], 1, &aceCallbackInfoPermission.callback[i]); - } - - NAPI_CALL(env, - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - HILOG_INFO("Context::NAPI_OnRequestPermissionsFromUserResult worker pool thread execute."); - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("Conext::NAPI_OnRequestPermissionsFromUserResult main event thread complete."); - - AsyncPermissionCallbackInfo *asyncCallbackInfo = (AsyncPermissionCallbackInfo *)data; - if (asyncCallbackInfo != nullptr) { - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - } - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork)); - - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - } - - // create result code - napi_value result = 0; - NAPI_CALL(env, napi_create_int32(env, 1, &result)); - return result; -} - -/** - * @brief Called back after permissions are requested by using - * AbilityContext.requestPermissionsFromUser(java.lang.String[],int). - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - */ -napi_value NAPI_OnRequestPermissionsFromUserResult(napi_env env, napi_callback_info info) -{ - HILOG_INFO("Context::NAPI_OnRequestPermissionsFromUserResult. called."); - - AsyncPermissionCallbackInfo *asyncCallbackInfo = CreateAsyncPermissionCallbackInfo(env); - if (asyncCallbackInfo == nullptr) { - return nullptr; - } - - napi_value rev = NAPI_OnRequestPermissionsFromUserResultWrap(env, info, asyncCallbackInfo); - if (rev == nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - } - return rev; -} - -/** - * @brief The interface of onRequestPermissionsFromUserResult provided for ACE to call back to JS. - * - * @param requestCode Indicates the request code returned after the ability is started. - * @param permissions Indicates list of permission. - * @param grantResults Indicates List of authorization results. - * @param callbackInfo The environment and call back info that the Node-API call is invoked under. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value CallOnRequestPermissionsFromUserResult(int requestCode, const std::vector &permissions, - const std::vector &grantResults, CallbackInfo callbackInfo) -{ - HILOG_INFO("Context::CallOnRequestPermissionsFromUserResult called"); - napi_value result = 0; - NAPI_CALL(callbackInfo.env, napi_create_object(callbackInfo.env, &result)); - - // create requestCode - napi_value jsValue = 0; - NAPI_CALL(callbackInfo.env, napi_create_int32(callbackInfo.env, requestCode, &jsValue)); - NAPI_CALL(callbackInfo.env, napi_set_named_property(callbackInfo.env, result, "requestCode", jsValue)); - - // create permissions - napi_value perValue = 0; - napi_value perArray = 0; - NAPI_CALL(callbackInfo.env, napi_create_array(callbackInfo.env, &perArray)); - - for (size_t i = 0; i < permissions.size(); i++) { - NAPI_CALL(callbackInfo.env, - napi_create_string_utf8(callbackInfo.env, permissions[i].c_str(), NAPI_AUTO_LENGTH, &perValue)); - NAPI_CALL(callbackInfo.env, napi_set_element(callbackInfo.env, perArray, i, perValue)); - } - NAPI_CALL(callbackInfo.env, napi_set_named_property(callbackInfo.env, result, "permissions", perArray)); - - // create grantResults - napi_value grantArray; - NAPI_CALL(callbackInfo.env, napi_create_array(callbackInfo.env, &grantArray)); - - for (size_t i = 0; i < grantResults.size(); i++) { - NAPI_CALL(callbackInfo.env, napi_create_int32(callbackInfo.env, grantResults[i], &perValue)); - NAPI_CALL(callbackInfo.env, napi_set_element(callbackInfo.env, grantArray, i, perValue)); - } - NAPI_CALL(callbackInfo.env, napi_set_named_property(callbackInfo.env, result, "grantResults", grantArray)); - - // call CB function - napi_value callback = 0; - napi_value undefined = 0; - NAPI_CALL(callbackInfo.env, napi_get_undefined(callbackInfo.env, &undefined)); - - napi_value callResult = 0; - NAPI_CALL(callbackInfo.env, napi_get_reference_value(callbackInfo.env, callbackInfo.callback[0], &callback)); - NAPI_CALL(callbackInfo.env, napi_call_function(callbackInfo.env, undefined, callback, 1, &result, &callResult)); - - if (callbackInfo.callback[0] != nullptr) { - NAPI_CALL(callbackInfo.env, napi_delete_reference(callbackInfo.env, callbackInfo.callback[0])); - } - if (callbackInfo.callback[1] != nullptr) { - NAPI_CALL(callbackInfo.env, napi_delete_reference(callbackInfo.env, callbackInfo.callback[1])); - } - - // create reutrn - napi_value ret = 0; - NAPI_CALL(callbackInfo.env, napi_create_int32(callbackInfo.env, 1, &ret)); - return ret; -} - -/** - * @brief GetBundleName asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetBundleNameExecuteCallback(napi_env env, void *data) -{ - HILOG_INFO("FeatureAbility::NAPI_GetBundleName worker pool thread execute."); - AsyncPermissionCallbackInfo *asyncCallbackInfo = (AsyncPermissionCallbackInfo *)data; - - if (asyncCallbackInfo->ability != nullptr) { - asyncCallbackInfo->native_data.data_type = NVT_STRING; - asyncCallbackInfo->native_data.str_value = asyncCallbackInfo->ability->GetBundleName(); - HILOG_INFO("FeatureAbility::NAPI_GetBundleName bundleName = %{public}s.", - asyncCallbackInfo->native_data.str_value.c_str()); - asyncCallbackInfo->run_status = true; - } -} - -/** - * @brief GetBundleName processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param asyncCallbackInfo Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value NAPI_GetBundleNameWrap(napi_env env, napi_callback_info info, AsyncPermissionCallbackInfo *asyncCallbackInfo) -{ - size_t argc = 2; - const size_t argCount = 0; - napi_value args[PERMISSION_ARGS_SIZE] = {nullptr}; - napi_value jsthis = 0; - void *data = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsthis, &data)); - - AsyncParamEx asyncParamEx; - asyncParamEx.argc = argc; - if (argc > argCount) { - HILOG_INFO("Context::NAPI_GetBundleName asyncCallback."); - asyncParamEx.resource = "NAPI_GetBundleName_Callback"; - asyncParamEx.startIndex = 0; - asyncParamEx.execute = GetBundleNameExecuteCallback; - asyncParamEx.complete = AsyncCompleteCallback; - return ExecuteAsyncCallback(env, args, asyncCallbackInfo, &asyncParamEx); - } else { - HILOG_INFO("Context::NAPI_GetBundleName promise."); - asyncParamEx.resource = "NAPI_GetBundleName_Promise"; - asyncParamEx.startIndex = 0; - asyncParamEx.execute = GetBundleNameExecuteCallback; - asyncParamEx.complete = PromiseCompleteCallback; - return ExecutePromiseCallback(env, args, asyncCallbackInfo, &asyncParamEx); - } -} - -/** - * @brief Get bundle name. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetBundleName(napi_env env, napi_callback_info info) -{ - HILOG_INFO("FeatureAbility::NAPI_GetBundleName. called."); - - AsyncPermissionCallbackInfo *asyncCallbackInfo = CreateAsyncPermissionCallbackInfo(env); - if (asyncCallbackInfo == nullptr) { - return nullptr; - } - - napi_value rev = NAPI_GetBundleNameWrap(env, info, asyncCallbackInfo); - if (rev == nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - napi_value result = 0; - napi_get_null(env, &result); - return result; - } - - return rev; -} -} // namespace AppExecFwk -} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/aafwk/context/napi_context.h b/interfaces/kits/napi/aafwk/context/napi_context.h deleted file mode 100644 index d1d58d568e8..00000000000 --- a/interfaces/kits/napi/aafwk/context/napi_context.h +++ /dev/null @@ -1,205 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef NAPI_CONTEXT_PERMISSION_H_ -#define NAPI_CONTEXT_PERMISSION_H_ -#include "napi/native_common.h" -#include "napi/native_node_api.h" -#include "ability.h" -#include "feature_ability_common.h" - -#define PERMISSION_C_BUFFER_SIZE 1024 /* Converted to C-style string buffer size */ -#define PERMISSION_ARGS_SIZE 10 -#define PERMISSION_EVENT_SIZE 2 -using Ability = OHOS::AppExecFwk::Ability; - -namespace OHOS { -namespace AppExecFwk { -struct CallAbilityPermissionParam { - std::vector permission_list; - int requestCode = 0; - int syncOption = false; -}; - -typedef enum { - NVT_NONE = 0, - NVT_INT32, - NVT_BOOL, - NVT_STRING, -} TNativeValueType; - -typedef struct __ThreadReturnData { - TNativeValueType data_type; - int32_t int32_value; - bool bool_value; - std::string str_value; -} ThreadReturnData; - -struct AsyncPermissionCallbackInfo { - napi_env env; - napi_async_work asyncWork; - napi_deferred deferred; - Ability *ability; - CallAbilityPermissionParam param; - napi_ref callback[2] = {0}; - ThreadReturnData native_data; - napi_value result; - bool run_status; - int error_code; - OHOS::AppExecFwk::CallbackInfo *aceCallback; -}; - -struct AsyncParamEx { - std::string resource; - size_t argc; - size_t startIndex; - napi_async_execute_callback execute; - napi_async_complete_callback complete; -}; - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AsyncPermissionCallbackInfo on success, nullptr on failure - */ -AsyncPermissionCallbackInfo *CreateAsyncPermissionCallbackInfo(napi_env env); - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * @param argc Number of parameters. - * @param startIndex The position of the first event parameter. - * @param args Parameter list. - * @param callback Point to asynchronous processing of data. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value CreateAsyncCallback( - napi_env env, size_t argc, size_t startIndex, napi_value *args, AsyncPermissionCallbackInfo *callback); - -/** - * @brief Parse JS numeric parameters. - * - * @param env The environment that the Node-API call is invoked under. - * @param value Return integer value. - * @param args Parameter list. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value UnwrapParamInt(int *value, napi_env env, napi_value args); - -/** - * @brief Parse JS string parameters. - * - * @param env The environment that the Node-API call is invoked under. - * @param value Return stirng value. - * @param args Parameter list. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value UnwrapParamString(std::vector &value, napi_env env, napi_value args); - -/** - * @brief Parse JS array of string parameters. - * - * @param env The environment that the Node-API call is invoked under. - * @param value Return array of stirng value. - * @param args Parameter list. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value UnwrapParamArrayString(std::vector &value, napi_env env, napi_value args); - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void AsyncCompleteCallback(napi_env env, napi_status status, void *data); - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void PromiseCompleteCallback(napi_env env, napi_status status, void *data); - -/** - * @brief Asynchronous callback processing. - * - * @param env The environment that the Node-API call is invoked under. - * @param args list of JS of param. - * @param asyncCallbackInfo Process data asynchronously. - * @param param other param. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value ExecuteAsyncCallback( - napi_env env, napi_value *args, AsyncPermissionCallbackInfo *asyncCallbackInfo, const AsyncParamEx *param); - -/** - * @brief Asynchronous promise processing. - * - * @param env The environment that the Node-API call is invoked under. - * @param args list of JS of param. - * @param asyncCallbackInfo Process data asynchronously. - * @param param other param. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value ExecutePromiseCallback( - napi_env env, napi_value *args, AsyncPermissionCallbackInfo *asyncCallbackInfo, const AsyncParamEx *param); - -/** - * @brief Convert local data to JS data. - * - * @param env The environment that the Node-API call is invoked under. - * @param data The local data. - * @param value the JS data. - * - * @return The return value from NAPI C++ to JS for the module. - */ -void NAPI_GetNapiValue(napi_env env, const ThreadReturnData *data, napi_value *value); - -/** - * @brief Context NAPI module registration. - * - * @param env The environment that the Node-API call is invoked under. - * @param exports An empty object via the exports parameter as a convenience. - * - * @return The return value from Init is treated as the exports object for the module. - */ -napi_value ContextPermissionInit(napi_env env, napi_value exports); - -/** - * @brief The interface of onRequestPermissionsFromUserResult provided for ACE to call back to JS. - * - * @param requestCode Indicates the request code returned after the ability is started. - * @param permissions Indicates list of permission. - * @param grantResults Indicates List of authorization results. - * @param callbackInfo The environment and call back info that the Node-API call is invoked under. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value CallOnRequestPermissionsFromUserResult(int requestCode, const std::vector &permissions, - const std::vector &grantResults, OHOS::AppExecFwk::CallbackInfo callbackInfo); -} // namespace AppExecFwk -} // namespace OHOS -#endif /* ABILITY_PERMISSION_H_ */ diff --git a/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn b/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn index 89e899452f7..e87b658e33b 100755 --- a/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn +++ b/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn @@ -25,6 +25,7 @@ ohos_shared_library("featureability") { sources = [ "feature_ability.cpp", + "feature_ability_constant.cpp", "napi_context.cpp", "napi_data_ability_helper.cpp", "native_module.cpp", diff --git a/interfaces/kits/napi/aafwk/featureAbility/feature_ability.cpp b/interfaces/kits/napi/aafwk/featureAbility/feature_ability.cpp index 7922ce8adbc..ec8bb06a627 100755 --- a/interfaces/kits/napi/aafwk/featureAbility/feature_ability.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/feature_ability.cpp @@ -31,7 +31,7 @@ namespace OHOS { namespace AppExecFwk { extern napi_value g_classContext; extern napi_value g_dataAbilityHelper; - +static int64_t dummyRequestCode_ = 0; CallbackInfo g_aceCallbackInfo; const int PARA_SIZE_IS_ONE = 1; @@ -58,13 +58,8 @@ napi_value FeatureAbilityInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("hasWindowFocus", NAPI_HasWindowFocus), DECLARE_NAPI_FUNCTION("getContext", NAPI_GetContext), DECLARE_NAPI_FUNCTION("getWant", NAPI_GetWant), - DECLARE_NAPI_FUNCTION("getApplicationInfo", NAPI_GetApplicationInfo), DECLARE_NAPI_FUNCTION("getAppType", NAPI_GetAppType), - DECLARE_NAPI_FUNCTION("getElementName", NAPI_GetElementName), DECLARE_NAPI_FUNCTION("getAbilityName", NAPI_GetAbilityName), - DECLARE_NAPI_FUNCTION("getProcessInfo", NAPI_GetProcessInfo), - DECLARE_NAPI_FUNCTION("getProcessName", NAPI_GetProcessName), - DECLARE_NAPI_FUNCTION("getCallingBundle", NAPI_GetCallingBundle), DECLARE_NAPI_FUNCTION("getAbilityInfo", NAPI_GetAbilityInfo), DECLARE_NAPI_FUNCTION("getHapModuleInfo", NAPI_GetHapModuleInfo), DECLARE_NAPI_FUNCTION("getDataAbilityHelper", NAPI_GetDataAbilityHelper), @@ -160,7 +155,7 @@ napi_value StartAbilityForResultWrap(napi_env env, napi_callback_info info, Asyn } if (argcAsync > argcPromise) { - ret = StartAbilityForResultAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); + ret = StartAbilityForResultAsync(env, asyncCallbackInfo); } else { ret = StartAbilityForResultPromise(env, asyncCallbackInfo); } @@ -168,11 +163,10 @@ napi_value StartAbilityForResultWrap(napi_env env, napi_callback_info info, Asyn return ret; } -napi_value StartAbilityForResultAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) +napi_value StartAbilityForResultAsync(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) { HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || asyncCallbackInfo == nullptr) { + if (asyncCallbackInfo == nullptr) { HILOG_ERROR("%{public}s, param == nullptr.", __func__); return nullptr; } @@ -320,7 +314,7 @@ napi_value SetResultWrap(napi_env env, napi_callback_info info, AsyncCallbackInf asyncCallbackInfo->param = param; if (argcAsync > argcPromise) { - ret = SetResultAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); + ret = SetResultAsync(env, args, 1, asyncCallbackInfo); } else { ret = SetResultPromise(env, asyncCallbackInfo); } @@ -329,7 +323,7 @@ napi_value SetResultWrap(napi_env env, napi_callback_info info, AsyncCallbackInf } napi_value SetResultAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) + napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || asyncCallbackInfo == nullptr) { @@ -340,9 +334,9 @@ napi_value SetResultAsync( napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); napi_valuetype valuetype = napi_undefined; - napi_typeof(env, args[argcPromise], &valuetype); + napi_typeof(env, args[argCallback], &valuetype); if (valuetype == napi_function) { - napi_create_reference(env, args[argcPromise], 1, &asyncCallbackInfo->cbInfo.callback); + napi_create_reference(env, args[argCallback], 1, &asyncCallbackInfo->cbInfo.callback); } napi_create_async_work( @@ -496,7 +490,7 @@ napi_value TerminateAbilityWrap(napi_env env, napi_callback_info info, AsyncCall } if (argcAsync > argcPromise) { - ret = TerminateAbilityAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); + ret = TerminateAbilityAsync(env, args, 0, asyncCallbackInfo); } else { ret = TerminateAbilityPromise(env, asyncCallbackInfo); } @@ -505,7 +499,7 @@ napi_value TerminateAbilityWrap(napi_env env, napi_callback_info info, AsyncCall } napi_value TerminateAbilityAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) + napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || asyncCallbackInfo == nullptr) { @@ -516,9 +510,9 @@ napi_value TerminateAbilityAsync( napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); napi_valuetype valuetype = napi_undefined; - napi_typeof(env, args[argcPromise], &valuetype); + napi_typeof(env, args[argCallback], &valuetype); if (valuetype == napi_function) { - napi_create_reference(env, args[argcPromise], 1, &asyncCallbackInfo->cbInfo.callback); + napi_create_reference(env, args[argCallback], 1, &asyncCallbackInfo->cbInfo.callback); } napi_create_async_work( @@ -683,7 +677,7 @@ napi_value HasWindowFocusWrap(napi_env env, napi_callback_info info, AsyncCallba } if (argcAsync > argcPromise) { - ret = HasWindowFocusAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); + ret = HasWindowFocusAsync(env, args, 0, asyncCallbackInfo); } else { ret = HasWindowFocusPromise(env, asyncCallbackInfo); } @@ -692,7 +686,7 @@ napi_value HasWindowFocusWrap(napi_env env, napi_callback_info info, AsyncCallba } napi_value HasWindowFocusAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) + napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || asyncCallbackInfo == nullptr) { @@ -703,9 +697,9 @@ napi_value HasWindowFocusAsync( napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); napi_valuetype valuetype = napi_undefined; - napi_typeof(env, args[argcPromise], &valuetype); + napi_typeof(env, args[argCallback], &valuetype); if (valuetype == napi_function) { - napi_create_reference(env, args[argcPromise], 1, &asyncCallbackInfo->cbInfo.callback); + napi_create_reference(env, args[argCallback], 1, &asyncCallbackInfo->cbInfo.callback); } napi_create_async_work( env, @@ -813,9 +807,7 @@ void CallOnAbilityResult(int requestCode, int resultCode, const Want &resultData HILOG_INFO("%{public}s,called", __func__); uv_loop_s *loop = nullptr; -#if NAPI_VERSION >= 2 napi_get_uv_event_loop(cb.env, &loop); -#endif // NAPI_VERSION >= 2 if (loop == nullptr) { return; } @@ -825,18 +817,20 @@ void CallOnAbilityResult(int requestCode, int resultCode, const Want &resultData HILOG_ERROR("%{public}s, work == nullptr.", __func__); return; } - OnAbilityCallback *onAbilityCB = new (std::nothrow) OnAbilityCallback{ - .requestCode = requestCode, - .resultCode = resultCode, - .resultData = resultData, - .cb = cb, - }; + OnAbilityCallback *onAbilityCB = new (std::nothrow) OnAbilityCallback; if (onAbilityCB == nullptr) { HILOG_ERROR("%{public}s, onAbilityCB == nullptr.", __func__); - delete work; - work = nullptr; + if (work != nullptr) { + delete work; + work = nullptr; + } return; } + onAbilityCB->requestCode = requestCode; + onAbilityCB->resultCode = resultCode; + onAbilityCB->resultData = resultData; + onAbilityCB->cb = cb; + work->data = (void *)onAbilityCB; int rev = uv_queue_work( @@ -846,52 +840,60 @@ void CallOnAbilityResult(int requestCode, int resultCode, const Want &resultData [](uv_work_t *work, int status) { HILOG_INFO("CallOnAbilityResult, uv_queue_work"); // JS Thread - OnAbilityCallback *event = (OnAbilityCallback *)work->data; + OnAbilityCallback *onAbilityCB = (OnAbilityCallback *)work->data; napi_value result[ARGS_TWO] = {0}; - result[PARAM0] = GetCallbackErrorValue(event->cb.env, NO_ERROR); + result[PARAM0] = GetCallbackErrorValue(onAbilityCB->cb.env, NO_ERROR); - napi_create_object(event->cb.env, &result[PARAM1]); + napi_create_object(onAbilityCB->cb.env, &result[PARAM1]); // create resultCode napi_value jsValue = 0; - napi_create_int32(event->cb.env, event->resultCode, &jsValue); - napi_set_named_property(event->cb.env, result[PARAM1], "resultCode", jsValue); + napi_create_int32(onAbilityCB->cb.env, onAbilityCB->resultCode, &jsValue); + napi_set_named_property(onAbilityCB->cb.env, result[PARAM1], "resultCode", jsValue); // create want - napi_value jsWant = WrapWant(event->cb.env, event->resultData); - napi_set_named_property(event->cb.env, result[PARAM1], "want", jsWant); + napi_value jsWant = WrapWant(onAbilityCB->cb.env, onAbilityCB->resultData); + napi_set_named_property(onAbilityCB->cb.env, result[PARAM1], "want", jsWant); - if (event->cb.callback != nullptr) { + if (onAbilityCB->cb.callback != nullptr) { // asynccallback HILOG_INFO("CallOnAbilityResult, asynccallback"); napi_value callback = 0; napi_value undefined = 0; - napi_get_undefined(event->cb.env, &undefined); + napi_get_undefined(onAbilityCB->cb.env, &undefined); napi_value callResult = 0; - napi_get_reference_value(event->cb.env, event->cb.callback, &callback); + napi_get_reference_value(onAbilityCB->cb.env, onAbilityCB->cb.callback, &callback); - napi_call_function(event->cb.env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); - if (event->cb.callback != nullptr) { - napi_delete_reference(event->cb.env, event->cb.callback); + napi_call_function(onAbilityCB->cb.env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); + if (onAbilityCB->cb.callback != nullptr) { + napi_delete_reference(onAbilityCB->cb.env, onAbilityCB->cb.callback); } HILOG_INFO("CallOnAbilityResult, asynccallback end"); } else { // promise HILOG_INFO("CallOnAbilityResult, promise"); - napi_resolve_deferred(event->cb.env, event->cb.deferred, result[PARAM1]); + napi_resolve_deferred(onAbilityCB->cb.env, onAbilityCB->cb.deferred, result[PARAM1]); HILOG_INFO("CallOnAbilityResult, promise end"); } - delete event; - delete work; - work = nullptr; - event = nullptr; + if (onAbilityCB != nullptr) { + delete onAbilityCB; + onAbilityCB = nullptr; + } + if (work != nullptr) { + delete work; + work = nullptr; + } HILOG_INFO("CallOnAbilityResult, uv_queue_work end"); }); if (rev != 0) { - delete onAbilityCB; - onAbilityCB = nullptr; - delete work; - work = nullptr; + if (onAbilityCB != nullptr) { + delete onAbilityCB; + onAbilityCB = nullptr; + } + if (work != nullptr) { + delete work; + work = nullptr; + } } HILOG_INFO("%{public}s,end", __func__); } @@ -928,28 +930,26 @@ bool InnerUnwrapWant(napi_env env, napi_value args, Want &want) napi_value UnwrapForResultParam(CallAbilityParam ¶m, napi_env env, napi_value args) { HILOG_INFO("%{public}s,called", __func__); - // unwrap the param - napi_valuetype valueType = napi_undefined; - // unwrap the param : want object InnerUnwrapWant(env, args, param.want); // dummy requestCode for NativeC++ interface and onabilityresult callback param.requestCode = dummyRequestCode_; param.forResultOption = true; - dummyRequestCode_ < INT64_MAX ? dummyRequestCode_++ : dummyRequestCode_ = 0; + dummyRequestCode_ < INT64_MAX ? dummyRequestCode_ = dummyRequestCode_ + 1 : dummyRequestCode_ = 0; HILOG_INFO("%{public}s, reqCode=%{public}d forResultOption=%{public}d.", __func__, param.requestCode, param.forResultOption); // unwrap the param : abilityStartSetting (optional) - napi_value abilityStartSettingProp = nullptr; - NAPI_CALL(env, napi_get_named_property(env, args, "abilityStartSetting", &abilityStartSettingProp)); - NAPI_CALL(env, napi_typeof(env, abilityStartSettingProp, &valueType)); - if (valueType == napi_object) { + napi_value jsSettingObj = GetPropertyValueByPropertyName(env, args, "abilityStartSetting", napi_object); + if (jsSettingObj != nullptr) { param.setting = AbilityStartSetting::GetEmptySetting(); - HILOG_INFO("%{public}s, abilityStartSetting=%{public}p.", __func__, param.setting.get()); + if (!UnwrapAbilityStartSetting(env, jsSettingObj, *(param.setting))) { + HILOG_ERROR("%{public}s, unwrap abilityStartSetting falied.", __func__); + } + HILOG_INFO("%{public}s abilityStartSetting = %{public}p.", __func__, param.setting.get()); } napi_value result; @@ -1005,20 +1005,6 @@ napi_value NAPI_GetWant(napi_env env, napi_callback_info info) return NAPI_GetWantCommon(env, info, AbilityType::PAGE); } -/** - * @brief Obtains information about the current application. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetApplicationInfo(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s,called", __func__); - return NAPI_GetApplicationInfoCommon(env, info, AbilityType::PAGE); -} - /** * @brief Obtains the type of this application. * @@ -1033,20 +1019,6 @@ napi_value NAPI_GetAppType(napi_env env, napi_callback_info info) return NAPI_GetAppTypeCommon(env, info, AbilityType::PAGE); } -/** - * @brief Obtains the elementName object of the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetElementName(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s called.", __func__); - return NAPI_GetElementNameCommon(env, info, AbilityType::PAGE); -} - /** * @brief Obtains the class name in this ability name, without the prefixed bundle name. * @@ -1061,48 +1033,6 @@ napi_value NAPI_GetAbilityName(napi_env env, napi_callback_info info) return NAPI_GetAbilityNameCommon(env, info, AbilityType::PAGE); } -/** - * @brief Obtains the process Info this application. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetProcessInfo(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s called.", __func__); - return NAPI_GetProcessInfoCommon(env, info, AbilityType::PAGE); -} - -/** - * @brief Obtains the name of the current process. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetProcessName(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s called.", __func__); - return NAPI_GetProcessNameCommon(env, info, AbilityType::PAGE); -} - -/** - * @brief Obtains the bundle name of the ability that called the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetCallingBundle(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s called.", __func__); - return NAPI_GetCallingBundleCommon(env, info, AbilityType::PAGE); -} - /** * @brief Obtains information about the current ability. * @@ -1196,7 +1126,7 @@ napi_value GetDataAbilityHelperWrap(napi_env env, napi_callback_info info, DataA } if (argcAsync > argcPromise) { - ret = GetDataAbilityHelperAsync(env, args, argcAsync, argcPromise, dataAbilityHelperCB); + ret = GetDataAbilityHelperAsync(env, args, 1, dataAbilityHelperCB); } else { ret = GetDataAbilityHelperPromise(env, dataAbilityHelperCB); } @@ -1204,8 +1134,8 @@ napi_value GetDataAbilityHelperWrap(napi_env env, napi_callback_info info, DataA return ret; } -napi_value GetDataAbilityHelperAsync(napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, - DataAbilityHelperCB *dataAbilityHelperCB) +napi_value GetDataAbilityHelperAsync( + napi_env env, napi_value *args, const size_t argCallback, DataAbilityHelperCB *dataAbilityHelperCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || dataAbilityHelperCB == nullptr) { @@ -1216,9 +1146,9 @@ napi_value GetDataAbilityHelperAsync(napi_env env, napi_value *args, size_t argc NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &dataAbilityHelperCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &dataAbilityHelperCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, @@ -1320,63 +1250,7 @@ void GetDataAbilityHelperPromiseCompleteCB(napi_env env, napi_status status, voi napi_value NAPI_AcquireDataAbilityHelper(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s,called", __func__); - DataAbilityHelperCB *dataAbilityHelperCB = new (std::nothrow) DataAbilityHelperCB; - dataAbilityHelperCB->cbBase.cbInfo.env = env; - if (dataAbilityHelperCB == nullptr) { - HILOG_ERROR("%{public}s, dataAbilityHelperCB == nullptr", __func__); - return WrapVoidToJS(env); - } - napi_value ret = AcquireDataAbilityHelperWrap(env, info, dataAbilityHelperCB); - if (ret == nullptr) { - HILOG_ERROR("%{public}s, ret == nullptr", __func__); - if (dataAbilityHelperCB != nullptr) { - delete dataAbilityHelperCB; - dataAbilityHelperCB = nullptr; - } - ret = WrapVoidToJS(env); - } - HILOG_INFO("%{public}s,end", __func__); - return ret; -} - -/** - * @brief acquireDataAbilityHelper processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param dataAbilityHelperCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value AcquireDataAbilityHelperWrap(napi_env env, napi_callback_info info, DataAbilityHelperCB *dataAbilityHelperCB) -{ - HILOG_INFO("%{public}s,called", __func__); - if (dataAbilityHelperCB == nullptr) { - HILOG_ERROR("%{public}s,dataAbilityHelperCB == nullptr", __func__); - return nullptr; - } - - size_t requireArgc = ARGS_ONE; - size_t argc = ARGS_ONE; - napi_value args[ARGS_ONE] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); - if (argc > requireArgc) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[PARAM0], &valuetype)); - if (valuetype != napi_string) { - HILOG_ERROR("%{public}s, Wrong argument type.", __func__); - return nullptr; - } - - napi_value result = nullptr; - NAPI_CALL(env, napi_new_instance(env, g_dataAbilityHelper, 1, &args[PARAM0], &result)); - delete dataAbilityHelperCB; - dataAbilityHelperCB = nullptr; - HILOG_INFO("%{public}s,end", __func__); - return result; + return NAPI_AcquireDataAbilityHelperCommon(env, info, AbilityType::PAGE); } /** diff --git a/interfaces/kits/napi/aafwk/featureAbility/feature_ability.h b/interfaces/kits/napi/aafwk/featureAbility/feature_ability.h index 64839aca40f..56f88f59a90 100755 --- a/interfaces/kits/napi/aafwk/featureAbility/feature_ability.h +++ b/interfaces/kits/napi/aafwk/featureAbility/feature_ability.h @@ -114,16 +114,6 @@ napi_value NAPI_GetContext(napi_env env, napi_callback_info info); */ napi_value NAPI_GetWant(napi_env env, napi_callback_info info); -/** - * @brief Obtains information about the current application. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetApplicationInfo(napi_env env, napi_callback_info info); - /** * @brief Obtains the type of this application. * @@ -134,16 +124,6 @@ napi_value NAPI_GetApplicationInfo(napi_env env, napi_callback_info info); */ napi_value NAPI_GetAppType(napi_env env, napi_callback_info info); -/** - * @brief Obtains the elementName object of the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetElementName(napi_env env, napi_callback_info info); - /** * @brief Obtains the class name in this ability name, without the prefixed bundle name. * @@ -154,36 +134,6 @@ napi_value NAPI_GetElementName(napi_env env, napi_callback_info info); */ napi_value NAPI_GetAbilityName(napi_env env, napi_callback_info info); -/** - * @brief Obtains the process Info this application. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetProcessInfo(napi_env env, napi_callback_info info); - -/** - * @brief Obtains the name of the current process. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetProcessName(napi_env env, napi_callback_info info); - -/** - * @brief Obtains the bundle name of the ability that called the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetCallingBundle(napi_env env, napi_callback_info info); - /** * @brief Obtains information about the current ability. * @@ -255,7 +205,7 @@ AppInfoCB *CreateAppInfoCBInfo(napi_env env); */ napi_value HasWindowFocusWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo); napi_value HasWindowFocusAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo); + napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo); napi_value HasWindowFocusPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); /** @@ -268,7 +218,7 @@ napi_value HasWindowFocusPromise(napi_env env, AsyncCallbackInfo *asyncCallbackI */ napi_value TerminateAbilityWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo); napi_value TerminateAbilityAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo); + napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo); napi_value TerminateAbilityPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); /** @@ -281,7 +231,7 @@ napi_value TerminateAbilityPromise(napi_env env, AsyncCallbackInfo *asyncCallbac */ napi_value SetResultWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo); napi_value SetResultAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo); + napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo); napi_value SetResultPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); /** @@ -293,8 +243,7 @@ napi_value SetResultPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); * @return Return JS data successfully, otherwise return nullptr. */ napi_value StartAbilityForResultWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo); -napi_value StartAbilityForResultAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo); +napi_value StartAbilityForResultAsync(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); napi_value StartAbilityForResultPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); /** @@ -306,8 +255,7 @@ napi_value StartAbilityForResultPromise(napi_env env, AsyncCallbackInfo *asyncCa * @return Return JS data successfully, otherwise return nullptr. */ napi_value ContinueAbilityWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo); -napi_value ContinueAbilityAsync( - napi_env env, napi_value *args, AsyncCallbackInfo *asyncCallbackInfo, size_t argc); +napi_value ContinueAbilityAsync(napi_env env, napi_value *args, AsyncCallbackInfo *asyncCallbackInfo, size_t argc); napi_value ContinueAbilityPromise(napi_env env, napi_value *args, AsyncCallbackInfo *asyncCallbackInfo, size_t argc); /** @@ -340,8 +288,8 @@ napi_value GetDataAbilityHelperWrap(napi_env env, napi_callback_info info, DataA * * @return Return JS data successfully, otherwise return nullptr. */ -napi_value GetDataAbilityHelperAsync(napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, - DataAbilityHelperCB *dataAbilityHelperCB); +napi_value GetDataAbilityHelperAsync( + napi_env env, napi_value *args, const size_t argCallback, DataAbilityHelperCB *dataAbilityHelperCB); /** * @brief GetDataAbilityHelper Promise. @@ -369,17 +317,6 @@ void GetDataAbilityHelperAsyncCompleteCB(napi_env env, napi_status status, void */ void GetDataAbilityHelperPromiseCompleteCB(napi_env env, napi_status status, void *data); -/** - * @brief acquireDataAbilityHelper processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param dataAbilityHelperCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value AcquireDataAbilityHelperWrap( - napi_env env, napi_callback_info info, DataAbilityHelperCB *dataAbilityHelperCB); - /** * @brief FeatureAbility NAPI method : connectAbility. * @@ -399,7 +336,7 @@ napi_value NAPI_FAConnectAbility(napi_env env, napi_callback_info info); * @return The return value from NAPI C++ to JS for the module. */ napi_value NAPI_FADisConnectAbility(napi_env env, napi_callback_info info); -static int64_t dummyRequestCode_ = 0; + /** * @brief FeatureAbility NAPI method : continueAbility. * diff --git a/interfaces/kits/napi/aafwk/featureAbility/feature_ability_constant.cpp b/interfaces/kits/napi/aafwk/featureAbility/feature_ability_constant.cpp new file mode 100644 index 00000000000..3da89cf09b7 --- /dev/null +++ b/interfaces/kits/napi/aafwk/featureAbility/feature_ability_constant.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "feature_ability_constant.h" +#include +#include +#include "securec.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace AppExecFwk { +/** + * @brief FeatureAbilityConstantInit NAPI module registration. + * + * @param env The environment that the Node-API call is invoked under. + * @param exports An empty object via the exports parameter as a convenience. + * + * @return The return value from Init is treated as the exports object for the module. + */ +napi_value FAConstantInit(napi_env env, napi_value exports) +{ + HILOG_INFO("%{public}s,called", __func__); + napi_value abilityStartSetting = nullptr; + napi_value abilityWindowConfiguration = nullptr; + napi_create_object(env, &abilityStartSetting); + napi_create_object(env, &abilityWindowConfiguration); + + SetNamedProperty(env, abilityStartSetting, "abilityBounds", "BOUNDS_KEY"); + SetNamedProperty(env, abilityStartSetting, "windowMode", "WINDOW_MODE_KEY"); + SetNamedProperty(env, abilityStartSetting, "displayId", "DISPLAY_ID_KEY"); + + SetNamedProperty(env, abilityWindowConfiguration, 0, "WINDOW_MODE_UNDEFINED"); + SetNamedProperty(env, abilityWindowConfiguration, 1, "WINDOW_MODE_FULLSCREEN"); + SetNamedProperty(env, abilityWindowConfiguration, 100, "WINDOW_MODE_SPLIT_PRIMARY"); + SetNamedProperty(env, abilityWindowConfiguration, 101, "WINDOW_MODE_SPLIT_SECONDARY"); + SetNamedProperty(env, abilityWindowConfiguration, 102, "WINDOW_MODE_FLOATING"); + + napi_property_descriptor exportFuncs[] = { + DECLARE_NAPI_PROPERTY("AbilityStartSetting", abilityStartSetting), + DECLARE_NAPI_PROPERTY("AbilityWindowConfiguration", abilityWindowConfiguration), + }; + napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs); + + return exports; +} + +void SetNamedProperty(napi_env env, napi_value dstObj, const char *objName, const char *propName) +{ + napi_value prop = nullptr; + napi_create_string_utf8(env, objName, NAPI_AUTO_LENGTH, &prop); + napi_set_named_property(env, dstObj, propName, prop); +} + +void SetNamedProperty(napi_env env, napi_value dstObj, const int32_t objValue, const char *propName) +{ + napi_value prop = nullptr; + napi_create_int32(env, objValue, &prop); + napi_set_named_property(env, dstObj, propName, prop); +} + +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/aafwk/context/native_module.cpp b/interfaces/kits/napi/aafwk/featureAbility/feature_ability_constant.h similarity index 49% rename from interfaces/kits/napi/aafwk/context/native_module.cpp rename to interfaces/kits/napi/aafwk/featureAbility/feature_ability_constant.h index a1a5191a093..a2eb59c82b3 100644 --- a/interfaces/kits/napi/aafwk/context/native_module.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/feature_ability_constant.h @@ -13,44 +13,24 @@ * limitations under the License. */ -#include -#include -#include -#include - -#include "napi_context.h" -#include "napi/native_api.h" +#ifndef OHOS_APPEXECFWK_FEATURE_ABILITY_CONSTANT_H +#define OHOS_APPEXECFWK_FEATURE_ABILITY_CONSTANT_H +#include "napi/native_common.h" #include "napi/native_node_api.h" namespace OHOS { namespace AppExecFwk { -EXTERN_C_START -/* - * Module initialization - */ -static napi_value Init(napi_env env, napi_value exports) -{ - ContextPermissionInit(env, exports); - return exports; -} -EXTERN_C_END - -/* - * Module definition - */ -static napi_module _module = {.nm_version = 1, - .nm_flags = 0, - .nm_filename = nullptr, - .nm_register_func = Init, - .nm_modname = "context", - .nm_priv = ((void *)0), - .reserved = {0}}; -/* - * Module registration function +/** + * @brief FeatureAbilityConstantInit NAPI module registration. + * + * @param env The environment that the Node-API call is invoked under. + * @param exports An empty object via the exports parameter as a convenience. + * + * @return The return value from Init is treated as the exports object for the module. */ -extern "C" __attribute__((constructor)) void RegisterModule(void) -{ - napi_module_register(&_module); -} +napi_value FAConstantInit(napi_env env, napi_value exports); +void SetNamedProperty(napi_env env, napi_value dstObj, const char *objName, const char *propName); +void SetNamedProperty(napi_env env, napi_value dstObj, const int32_t objValue, const char *propName); } // namespace AppExecFwk } // namespace OHOS +#endif /* OHOS_APPEXECFWK_FEATURE_ABILITY_CONSTANT_H */ diff --git a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp index 6fc1906790e..f83e36fc532 100755 --- a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp @@ -20,13 +20,15 @@ #include "ability_process.h" #include "feature_ability_common.h" #include "../inner/napi_common/napi_common_ability.h" +#include "file_ex.h" +#include "directory_ex.h" using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; namespace OHOS { namespace AppExecFwk { - +const std::string CONTEXT_DEAL_FILE_SEPARATOR = std::string("/"); CallbackInfo aceCallbackInfoPermission; napi_value ContextConstructor(napi_env env, napi_callback_info info) @@ -350,9 +352,7 @@ void CallOnRequestPermissionsFromUserResult(int requestCode, const std::vector= 2 napi_get_uv_event_loop(callbackInfo.env, &loop); -#endif // NAPI_VERSION >= 2 if (loop == nullptr) { return; } @@ -363,18 +363,20 @@ void CallOnRequestPermissionsFromUserResult(int requestCode, const std::vectorrequestCode = requestCode; + onRequestPermissionCB->permissions = permissions; + onRequestPermissionCB->grantResults = grantResults; + onRequestPermissionCB->cb = callbackInfo; + work->data = (void *)onRequestPermissionCB; int rev = uv_queue_work( @@ -382,81 +384,93 @@ void CallOnRequestPermissionsFromUserResult(int requestCode, const std::vectordata; - if (event == nullptr) { + if (onRequestPermissionCB == nullptr) { return; } napi_value result[ARGS_TWO] = {0}; - result[PARAM0] = GetCallbackErrorValue(event->cb.env, 0); - napi_create_object(event->cb.env, &result[PARAM1]); + result[PARAM0] = GetCallbackErrorValue(onRequestPermissionCB->cb.env, 0); + napi_create_object(onRequestPermissionCB->cb.env, &result[PARAM1]); // create requestCode napi_value jsValue = 0; - napi_create_int32(event->cb.env, event->requestCode, &jsValue); - napi_set_named_property(event->cb.env, result[PARAM1], "requestCode", jsValue); + napi_create_int32(onRequestPermissionCB->cb.env, onRequestPermissionCB->requestCode, &jsValue); + napi_set_named_property(onRequestPermissionCB->cb.env, result[PARAM1], "requestCode", jsValue); // create permissions napi_value perValue = 0; napi_value perArray = 0; - napi_create_array(event->cb.env, &perArray); - - for (size_t i = 0; i < event->permissions.size(); i++) { - napi_create_string_utf8(event->cb.env, event->permissions[i].c_str(), NAPI_AUTO_LENGTH, &perValue); - napi_set_element(event->cb.env, perArray, i, perValue); + napi_create_array(onRequestPermissionCB->cb.env, &perArray); + + for (size_t i = 0; i < onRequestPermissionCB->permissions.size(); i++) { + napi_create_string_utf8(onRequestPermissionCB->cb.env, + onRequestPermissionCB->permissions[i].c_str(), + NAPI_AUTO_LENGTH, + &perValue); + napi_set_element(onRequestPermissionCB->cb.env, perArray, i, perValue); } - napi_set_named_property(event->cb.env, result[PARAM1], "permissions", perArray); + napi_set_named_property(onRequestPermissionCB->cb.env, result[PARAM1], "permissions", perArray); // create grantResults napi_value grantArray; - napi_create_array(event->cb.env, &grantArray); + napi_create_array(onRequestPermissionCB->cb.env, &grantArray); - for (size_t i = 0; i < event->grantResults.size(); i++) { - napi_create_int32(event->cb.env, event->grantResults[i], &perValue); - napi_set_element(event->cb.env, grantArray, i, perValue); + for (size_t i = 0; i < onRequestPermissionCB->grantResults.size(); i++) { + napi_create_int32(onRequestPermissionCB->cb.env, onRequestPermissionCB->grantResults[i], &perValue); + napi_set_element(onRequestPermissionCB->cb.env, grantArray, i, perValue); } - napi_set_named_property(event->cb.env, result[PARAM1], "authResults", grantArray); + napi_set_named_property(onRequestPermissionCB->cb.env, result[PARAM1], "authResults", grantArray); // call CB function napi_value callback = 0; napi_value undefined = 0; - napi_get_undefined(event->cb.env, &undefined); + napi_get_undefined(onRequestPermissionCB->cb.env, &undefined); napi_value callResult = 0; - napi_get_reference_value(event->cb.env, event->cb.callback, &callback); - napi_call_function(event->cb.env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); + napi_get_reference_value(onRequestPermissionCB->cb.env, onRequestPermissionCB->cb.callback, &callback); + napi_call_function( + onRequestPermissionCB->cb.env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); - if (event->cb.callback != nullptr) { - napi_delete_reference(event->cb.env, event->cb.callback); + if (onRequestPermissionCB->cb.callback != nullptr) { + napi_delete_reference(onRequestPermissionCB->cb.env, onRequestPermissionCB->cb.callback); + } + if (onRequestPermissionCB != nullptr) { + delete onRequestPermissionCB; + onRequestPermissionCB = nullptr; + } + if (work != nullptr) { + delete work; + work = nullptr; } - delete event; - delete work; - event = nullptr; - work = nullptr; }); if (rev != 0) { - delete onRequestPermissionCB; - onRequestPermissionCB = nullptr; - delete work; - work = nullptr; + if (onRequestPermissionCB != nullptr) { + delete onRequestPermissionCB; + onRequestPermissionCB = nullptr; + } + if (work != nullptr) { + delete work; + work = nullptr; + } } } EXTERN_C_END /** - * @brief Get bundle name. + * @brief Get Diles Dir. * * @param env The environment that the Node-API call is invoked under. * @param info The callback info passed into the callback function. * * @return The return value from NAPI C++ to JS for the module. */ -napi_value NAPI_GetBundleName(napi_env env, napi_callback_info info) +napi_value NAPI_GetFilesDir(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s called", __func__); - return NAPI_GetBundleNameCommon(env, info, AbilityType::PAGE); + return NAPI_GetFilesDirCommon(env, info, AbilityType::PAGE); } void CanRequestPermissionExecuteCallback(napi_env env, void *data) @@ -833,6 +847,1794 @@ napi_value NAPI_VerifyCallingOrSelfPermission(napi_env env, napi_callback_info i return rev; } +/** + * @brief GetApplicationInfo asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAppInfoExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); + AppInfoCB *appInfoCB = (AppInfoCB *)data; + appInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + + if (appInfoCB->cbBase.ability == nullptr) { + HILOG_ERROR("NAPI_GetApplicationInfo, ability == nullptr"); + appInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + std::shared_ptr appInfoPtr = appInfoCB->cbBase.ability->GetApplicationInfo(); + if (appInfoPtr != nullptr) { + SaveAppInfo(appInfoCB->appInfo, *appInfoPtr); + } else { + HILOG_ERROR("NAPI_GetApplicationInfo, appInfoPtr == nullptr"); + appInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; + } + HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute end."); +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAppInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); + AppInfoCB *appInfoCB = (AppInfoCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, appInfoCB->cbBase.errCode); + if (appInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapAppInfo(env, appInfoCB->appInfo); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, appInfoCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (appInfoCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, appInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, appInfoCB->cbBase.asyncWork)); + delete appInfoCB; + appInfoCB = nullptr; + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete end."); +} + +/** + * @brief GetApplicationInfo Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param appInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetApplicationInfoAsync(napi_env env, napi_value *args, const size_t argCallback, AppInfoCB *appInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || appInfoCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &appInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetAppInfoExecuteCB, + GetAppInfoAsyncCompleteCB, + (void *)appInfoCB, + &appInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, appInfoCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return result; +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAppInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); + AppInfoCB *appInfoCB = (AppInfoCB *)data; + if (appInfoCB == nullptr) { + HILOG_ERROR("NAPI_GetApplicationInfo, appInfoCB == nullptr"); + return; + } + + napi_value result = nullptr; + if (appInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapAppInfo(env, appInfoCB->appInfo); + napi_resolve_deferred(env, appInfoCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, appInfoCB->cbBase.errCode); + napi_reject_deferred(env, appInfoCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, appInfoCB->cbBase.asyncWork); + delete appInfoCB; + appInfoCB = nullptr; + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete end."); +} + +/** + * @brief GetApplicationInfo Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param appInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetApplicationInfoPromise(napi_env env, AppInfoCB *appInfoCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (appInfoCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + appInfoCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetAppInfoExecuteCB, + GetAppInfoPromiseCompleteCB, + (void *)appInfoCB, + &appInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, appInfoCB->cbBase.asyncWork)); + HILOG_INFO("%{public}s, promise end.", __func__); + return promise; +} + +/** + * @brief GetApplicationInfo processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param appInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetApplicationInfoWrap(napi_env env, napi_callback_info info, AppInfoCB *appInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (appInfoCB == nullptr) { + HILOG_ERROR("%{public}s, appInfoCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetApplicationInfoAsync(env, args, 0, appInfoCB); + } else { + ret = GetApplicationInfoPromise(env, appInfoCB); + } + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to AppInfoCB on success, nullptr on failure. + */ +AppInfoCB *CreateAppInfoCBInfo(napi_env env) +{ + HILOG_INFO("%{public}s, called.", __func__); + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + AppInfoCB *appInfoCB = new (std::nothrow) AppInfoCB; + if (appInfoCB == nullptr) { + HILOG_ERROR("%{public}s, appInfoCB == nullptr.", __func__); + return nullptr; + } + appInfoCB->cbBase.cbInfo.env = env; + appInfoCB->cbBase.asyncWork = nullptr; + appInfoCB->cbBase.deferred = nullptr; + appInfoCB->cbBase.ability = ability; + appInfoCB->cbBase.abilityType = AbilityType::UNKNOWN; + appInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + + HILOG_INFO("%{public}s, end.", __func__); + return appInfoCB; +} + +/** + * @brief GetBundleName asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetBundleNameExecuteCallback(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = (AsyncJSCallbackInfo *)data; + if (asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s. asyncCallbackInfo is null", __func__); + return; + } + + asyncCallbackInfo->error_code = NAPI_ERR_NO_ERROR; + asyncCallbackInfo->native_data.data_type = NVT_NONE; + if (asyncCallbackInfo->ability == nullptr) { + HILOG_ERROR("%{public}s ability == nullptr", __func__); + asyncCallbackInfo->error_code = NAPI_ERR_ACE_ABILITY; + return; + } + + asyncCallbackInfo->native_data.data_type = NVT_STRING; + asyncCallbackInfo->native_data.str_value = asyncCallbackInfo->ability->GetBundleName(); + HILOG_INFO("%{public}s end. bundleName=%{public}s", __func__, asyncCallbackInfo->native_data.str_value.c_str()); +} + +/** + * @brief GetBundleName processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param asyncCallbackInfo Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value NAPI_GetBundleNameWrap(napi_env env, napi_callback_info info, AsyncJSCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s called", __func__); + size_t argc = ARGS_MAX_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value jsthis = 0; + void *data = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsthis, &data)); + + if (argc > ARGS_ONE) { + HILOG_INFO("%{public}s called, parameters is invalid.", __func__); + return nullptr; + } + + if (argc == ARGS_ONE) { + if (!CreateAsyncCallback(env, args[PARAM0], asyncCallbackInfo)) { + HILOG_INFO("%{public}s called, the first parameter is invalid.", __func__); + return nullptr; + } + } + + AsyncParamEx asyncParamEx; + if (asyncCallbackInfo->cbInfo.callback != nullptr) { + HILOG_INFO("%{public}s called. asyncCallback.", __func__); + asyncParamEx.resource = "NAPI_GetBundleNameCallback"; + asyncParamEx.execute = GetBundleNameExecuteCallback; + asyncParamEx.complete = CompleteAsyncCallbackWork; + + return ExecuteAsyncCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } else { + HILOG_INFO("%{public}s called. promise.", __func__); + asyncParamEx.resource = "NAPI_GetBundleNamePromise"; + asyncParamEx.execute = GetBundleNameExecuteCallback; + asyncParamEx.complete = CompletePromiseCallbackWork; + + return ExecutePromiseCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } +} + +napi_value WrapProcessInfo(napi_env env, ProcessInfoCB *processInfoCB) +{ + HILOG_INFO("%{public}s called", __func__); + if (processInfoCB == nullptr) { + HILOG_ERROR("%{public}s Invalid param(processInfoCB == nullptr)", __func__); + return nullptr; + } + napi_value result = nullptr; + napi_value proValue = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + NAPI_CALL(env, napi_create_int32(env, processInfoCB->pid, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "pid", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, processInfoCB->processName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "processName", proValue)); + HILOG_INFO("%{public}s end", __func__); + return result; +} + +/** + * @brief GetProcessInfo asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetProcessInfoExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetProcessInfo, worker pool thread execute."); + ProcessInfoCB *processInfoCB = (ProcessInfoCB *)data; + if (processInfoCB == nullptr) { + return; + } + + processInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (processInfoCB->cbBase.ability == nullptr) { + HILOG_ERROR("NAPI_GetProcessInfo, ability == nullptr"); + processInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + std::shared_ptr processInfoPtr = processInfoCB->cbBase.ability->GetProcessInfo(); + if (processInfoPtr != nullptr) { + processInfoCB->processName = processInfoPtr->GetProcessName(); + processInfoCB->pid = processInfoPtr->GetPid(); + } else { + HILOG_ERROR("NAPI_GetProcessInfo, processInfoPtr == nullptr"); + processInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; + } + HILOG_INFO("NAPI_GetProcessInfo, worker pool thread execute end."); +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetProcessInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetProcessInfo, main event thread complete."); + ProcessInfoCB *processInfoCB = (ProcessInfoCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, processInfoCB->cbBase.errCode); + if (processInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapProcessInfo(env, processInfoCB); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, processInfoCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (processInfoCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, processInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, processInfoCB->cbBase.asyncWork)); + delete processInfoCB; + processInfoCB = nullptr; + HILOG_INFO("NAPI_GetProcessInfo, main event thread complete end."); +} + +/** + * @brief GetProcessInfo Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param ProcessInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetProcessInfoAsync(napi_env env, napi_value *args, const size_t argCallback, ProcessInfoCB *processInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || processInfoCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &processInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetProcessInfoExecuteCB, + GetProcessInfoAsyncCompleteCB, + (void *)processInfoCB, + &processInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, processInfoCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return result; +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetProcessInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetProcessInfo, main event thread complete."); + ProcessInfoCB *processInfoCB = (ProcessInfoCB *)data; + napi_value result = nullptr; + if (processInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapProcessInfo(env, processInfoCB); + napi_resolve_deferred(env, processInfoCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, processInfoCB->cbBase.errCode); + napi_reject_deferred(env, processInfoCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, processInfoCB->cbBase.asyncWork); + delete processInfoCB; + processInfoCB = nullptr; + HILOG_INFO("NAPI_GetProcessInfo, main event thread complete end."); +} + +/** + * @brief GetProcessInfo Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param ProcessInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetProcessInfoPromise(napi_env env, ProcessInfoCB *processInfoCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (processInfoCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + processInfoCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetProcessInfoExecuteCB, + GetProcessInfoPromiseCompleteCB, + (void *)processInfoCB, + &processInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, processInfoCB->cbBase.asyncWork)); + HILOG_INFO("%{public}s, promise end.", __func__); + return promise; +} + +/** + * @brief GetProcessInfo processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param ProcessInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetProcessInfoWrap(napi_env env, napi_callback_info info, ProcessInfoCB *processInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (processInfoCB == nullptr) { + HILOG_ERROR("%{public}s, processInfoCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetProcessInfoAsync(env, args, 0, processInfoCB); + } else { + ret = GetProcessInfoPromise(env, processInfoCB); + } + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to ProcessInfoCB on success, nullptr on failure. + */ +ProcessInfoCB *CreateProcessInfoCBInfo(napi_env env) +{ + HILOG_INFO("%{public}s, called.", __func__); + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + ProcessInfoCB *processInfoCB = new (std::nothrow) ProcessInfoCB; + if (processInfoCB == nullptr) { + HILOG_ERROR("%{public}s, processInfoCB == nullptr.", __func__); + return nullptr; + } + processInfoCB->cbBase.cbInfo.env = env; + processInfoCB->cbBase.asyncWork = nullptr; + processInfoCB->cbBase.deferred = nullptr; + processInfoCB->cbBase.ability = ability; + + HILOG_INFO("%{public}s, end.", __func__); + return processInfoCB; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to ElementNameCB on success, nullptr on failure. + */ +ElementNameCB *CreateElementNameCBInfo(napi_env env) +{ + HILOG_INFO("%{public}s, called.", __func__); + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + ElementNameCB *elementNameCB = new (std::nothrow) ElementNameCB; + if (elementNameCB == nullptr) { + HILOG_ERROR("%{public}s, elementNameCB == nullptr.", __func__); + return nullptr; + } + elementNameCB->cbBase.cbInfo.env = env; + elementNameCB->cbBase.asyncWork = nullptr; + elementNameCB->cbBase.deferred = nullptr; + elementNameCB->cbBase.ability = ability; + + HILOG_INFO("%{public}s, end.", __func__); + return elementNameCB; +} + +napi_value WrapElementName(napi_env env, ElementNameCB *elementNameCB) +{ + HILOG_INFO("%{public}s, called.", __func__); + if (elementNameCB == nullptr) { + HILOG_ERROR("%{public}s,Invalid param(appInfoCB = nullptr)", __func__); + return nullptr; + } + napi_value result = nullptr; + napi_value proValue = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->abilityName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "abilityName", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->bundleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "bundleName", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->deviceId.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "deviceId", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->shortName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "shortName", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->uri.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "uri", proValue)); + HILOG_INFO("%{public}s, end.", __func__); + return result; +} + +/** + * @brief GetElementName asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetElementNameExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetElementName, worker pool thread execute."); + if (data == nullptr) { + HILOG_ERROR("%{public}s, data == nullptr.", __func__); + return; + } + ElementNameCB *elementNameCB = (ElementNameCB *)data; + if (elementNameCB == nullptr) { + HILOG_ERROR("NAPI_GetElementName, elementNameCB == nullptr"); + return; + } + + elementNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (elementNameCB->cbBase.ability == nullptr) { + HILOG_ERROR("NAPI_GetElementName, ability == nullptr"); + elementNameCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + std::shared_ptr elementName = elementNameCB->cbBase.ability->GetElementName(); + if (elementNameCB != nullptr) { + elementNameCB->deviceId = elementName->GetDeviceID(); + elementNameCB->bundleName = elementName->GetBundleName(); + elementNameCB->abilityName = elementName->GetAbilityName(); + elementNameCB->uri = elementNameCB->cbBase.ability->GetWant()->GetUriString(); + elementNameCB->shortName = ""; + } else { + elementNameCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; + } + HILOG_INFO("NAPI_GetElementName, worker pool thread execute end."); +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetElementNameAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetElementName, main event thread complete."); + ElementNameCB *elementNameCB = (ElementNameCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, elementNameCB->cbBase.errCode); + if (elementNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapElementName(env, elementNameCB); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, elementNameCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (elementNameCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, elementNameCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, elementNameCB->cbBase.asyncWork)); + delete elementNameCB; + elementNameCB = nullptr; + HILOG_INFO("NAPI_GetElementName, main event thread complete end."); +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetElementNamePromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetElementName, main event thread complete."); + ElementNameCB *elementNameCB = (ElementNameCB *)data; + napi_value result = nullptr; + if (elementNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapElementName(env, elementNameCB); + napi_resolve_deferred(env, elementNameCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, elementNameCB->cbBase.errCode); + napi_reject_deferred(env, elementNameCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, elementNameCB->cbBase.asyncWork); + delete elementNameCB; + elementNameCB = nullptr; + HILOG_INFO("NAPI_GetElementName, main event thread complete end."); +} + +/** + * @brief GetElementName Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param elementNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetElementNamePromise(napi_env env, ElementNameCB *elementNameCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (elementNameCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + elementNameCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetElementNameExecuteCB, + GetElementNamePromiseCompleteCB, + (void *)elementNameCB, + &elementNameCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, elementNameCB->cbBase.asyncWork)); + HILOG_INFO("%{public}s, promise end.", __func__); + return promise; +} + +/** + * @brief GetElementName Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param elementNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetElementNameAsync(napi_env env, napi_value *args, const size_t argCallback, ElementNameCB *elementNameCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || elementNameCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &elementNameCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetElementNameExecuteCB, + GetElementNameAsyncCompleteCB, + (void *)elementNameCB, + &elementNameCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, elementNameCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; + HILOG_INFO("%{public}s, asyncCallback end.", __func__); +} + +/** + * @brief GetElementName processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param elementNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetElementNameWrap(napi_env env, napi_callback_info info, ElementNameCB *elementNameCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (elementNameCB == nullptr) { + HILOG_ERROR("%{public}s, appInfoCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetElementNameAsync(env, args, 0, elementNameCB); + } else { + ret = GetElementNamePromise(env, elementNameCB); + } + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to ProcessNameCB on success, nullptr on failure. + */ +ProcessNameCB *CreateProcessNameCBInfo(napi_env env) +{ + HILOG_INFO("%{public}s called.", __func__); + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + ProcessNameCB *processNameCB = new (std::nothrow) ProcessNameCB; + if (processNameCB == nullptr) { + HILOG_ERROR("%{public}s, processNameCB == nullptr.", __func__); + return nullptr; + } + processNameCB->cbBase.cbInfo.env = env; + processNameCB->cbBase.asyncWork = nullptr; + processNameCB->cbBase.deferred = nullptr; + processNameCB->cbBase.ability = ability; + + HILOG_INFO("%{public}s end.", __func__); + return processNameCB; +} + +/** + * @brief GetProcessName asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetProcessNameExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetProcessName, worker pool thread execute."); + ProcessNameCB *processNameCB = (ProcessNameCB *)data; + if (processNameCB == nullptr) { + HILOG_ERROR("NAPI_GetProcessName, processNameCB == nullptr"); + return; + } + + processNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (processNameCB->cbBase.ability == nullptr) { + HILOG_ERROR("NAPI_GetProcessName, ability == nullptr"); + processNameCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + processNameCB->processName = processNameCB->cbBase.ability->GetProcessName(); + HILOG_INFO("NAPI_GetProcessName, worker pool thread execute end."); +} + +napi_value WrapProcessName(napi_env env, ProcessNameCB *processNameCB) +{ + HILOG_INFO("%{public}s, called.", __func__); + if (processNameCB == nullptr) { + HILOG_ERROR("%{public}s, Invalid param(processNameCB == nullptr)", __func__); + return nullptr; + } + napi_value result = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, processNameCB->processName.c_str(), NAPI_AUTO_LENGTH, &result)); + HILOG_INFO("%{public}s, end.", __func__); + return result; +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetProcessNameAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetProcessName, main event thread complete."); + ProcessNameCB *processNameCB = (ProcessNameCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, processNameCB->cbBase.errCode); + if (processNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapProcessName(env, processNameCB); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, processNameCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (processNameCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, processNameCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, processNameCB->cbBase.asyncWork)); + delete processNameCB; + processNameCB = nullptr; + HILOG_INFO("NAPI_GetProcessName, main event thread complete end."); +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetProcessNamePromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetProcessName, main event thread complete."); + ProcessNameCB *processNameCB = (ProcessNameCB *)data; + napi_value result = nullptr; + if (processNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapProcessName(env, processNameCB); + napi_resolve_deferred(env, processNameCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, processNameCB->cbBase.errCode); + napi_reject_deferred(env, processNameCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, processNameCB->cbBase.asyncWork); + delete processNameCB; + processNameCB = nullptr; + HILOG_INFO("NAPI_GetProcessName, main event thread complete end."); +} + +/** + * @brief GetProcessName Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param ProcessNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetProcessNameAsync(napi_env env, napi_value *args, const size_t argCallback, ProcessNameCB *processNameCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || processNameCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &processNameCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetProcessNameExecuteCB, + GetProcessNameAsyncCompleteCB, + (void *)processNameCB, + &processNameCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, processNameCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return result; +} + +/** + * @brief GetProcessName Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param ProcessNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetProcessNamePromise(napi_env env, ProcessNameCB *processNameCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (processNameCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + processNameCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetProcessNameExecuteCB, + GetProcessNamePromiseCompleteCB, + (void *)processNameCB, + &processNameCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, processNameCB->cbBase.asyncWork)); + HILOG_INFO("%{public}s, promise end.", __func__); + return promise; +} + +/** + * @brief GetProcessName processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param ProcessNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetProcessNameWrap(napi_env env, napi_callback_info info, ProcessNameCB *processNameCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (processNameCB == nullptr) { + HILOG_ERROR("%{public}s, processNameCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetProcessNameAsync(env, args, 0, processNameCB); + } else { + ret = GetProcessNamePromise(env, processNameCB); + } + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to CallingBundleCB on success, nullptr on failure. + */ +CallingBundleCB *CreateCallingBundleCBInfo(napi_env env) +{ + HILOG_INFO("%{public}s called.", __func__); + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + CallingBundleCB *callingBundleCB = new (std::nothrow) CallingBundleCB; + if (callingBundleCB == nullptr) { + HILOG_ERROR("%{public}s, callingBundleCB == nullptr.", __func__); + return nullptr; + } + callingBundleCB->cbBase.cbInfo.env = env; + callingBundleCB->cbBase.asyncWork = nullptr; + callingBundleCB->cbBase.deferred = nullptr; + callingBundleCB->cbBase.ability = ability; + + HILOG_INFO("%{public}s end.", __func__); + return callingBundleCB; +} + +/** + * @brief GetCallingBundle asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetCallingBundleExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetCallingBundle, worker pool thread execute."); + CallingBundleCB *callingBundleCB = (CallingBundleCB *)data; + if (callingBundleCB == nullptr) { + HILOG_ERROR("NAPI_GetCallingBundle, callingBundleCB == nullptr"); + return; + } + + callingBundleCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (callingBundleCB->cbBase.ability == nullptr) { + HILOG_ERROR("NAPI_GetCallingBundle, ability == nullptr"); + callingBundleCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + callingBundleCB->callingBundleName = callingBundleCB->cbBase.ability->GetCallingBundle(); + HILOG_INFO("NAPI_GetCallingBundle, worker pool thread execute end."); +} + +napi_value WrapCallingBundle(napi_env env, CallingBundleCB *callingBundleCB) +{ + HILOG_INFO("%{public}s, called.", __func__); + if (callingBundleCB == nullptr) { + HILOG_ERROR("%{public}s,Invalid param(callingBundleCB == nullptr)", __func__); + return nullptr; + } + napi_value result = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, callingBundleCB->callingBundleName.c_str(), NAPI_AUTO_LENGTH, &result)); + HILOG_INFO("%{public}s, end.", __func__); + return result; +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetCallingBundleAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetCallingBundle, main event thread complete."); + CallingBundleCB *callingBundleCB = (CallingBundleCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, callingBundleCB->cbBase.errCode); + if (callingBundleCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapCallingBundle(env, callingBundleCB); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, callingBundleCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (callingBundleCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, callingBundleCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, callingBundleCB->cbBase.asyncWork)); + delete callingBundleCB; + callingBundleCB = nullptr; + HILOG_INFO("NAPI_GetCallingBundle, main event thread complete end."); +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetCallingBundlePromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetCallingBundle, main event thread complete."); + CallingBundleCB *callingBundleCB = (CallingBundleCB *)data; + napi_value result = nullptr; + if (callingBundleCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapCallingBundle(env, callingBundleCB); + napi_resolve_deferred(env, callingBundleCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, callingBundleCB->cbBase.errCode); + napi_reject_deferred(env, callingBundleCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, callingBundleCB->cbBase.asyncWork); + delete callingBundleCB; + callingBundleCB = nullptr; + HILOG_INFO("NAPI_GetCallingBundle, main event thread complete end."); +} + +/** + * @brief GetCallingBundle Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param CallingBundleCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetCallingBundleAsync( + napi_env env, napi_value *args, const size_t argCallback, CallingBundleCB *callingBundleCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || callingBundleCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &callingBundleCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetCallingBundleExecuteCB, + GetCallingBundleAsyncCompleteCB, + (void *)callingBundleCB, + &callingBundleCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, callingBundleCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return result; +} + +/** + * @brief GetCallingBundle Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param CallingBundleCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetCallingBundlePromise(napi_env env, CallingBundleCB *callingBundleCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (callingBundleCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + callingBundleCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetCallingBundleExecuteCB, + GetCallingBundlePromiseCompleteCB, + (void *)callingBundleCB, + &callingBundleCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, callingBundleCB->cbBase.asyncWork)); + HILOG_INFO("%{public}s, promise end.", __func__); + return promise; +} + +/** + * @brief GetCallingBundle processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param CallingBundleCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetCallingBundleWrap(napi_env env, napi_callback_info info, CallingBundleCB *callingBundleCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (callingBundleCB == nullptr) { + HILOG_ERROR("%{public}s, callingBundleCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetCallingBundleAsync(env, args, 0, callingBundleCB); + } else { + ret = GetCallingBundlePromise(env, callingBundleCB); + } + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to CallingBundleCB on success, nullptr on failure. + */ +GetOrCreateLocalDirCB *CreateGetOrCreateLocalDirCBInfo(napi_env env) +{ + HILOG_INFO("%{public}s called.", __func__); + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + GetOrCreateLocalDirCB *getOrCreateLocalDirCB = new (std::nothrow) GetOrCreateLocalDirCB; + if (getOrCreateLocalDirCB == nullptr) { + HILOG_ERROR("%{public}s, getOrCreateLocalDirCB == nullptr.", __func__); + return nullptr; + } + getOrCreateLocalDirCB->cbBase.cbInfo.env = env; + getOrCreateLocalDirCB->cbBase.asyncWork = nullptr; + getOrCreateLocalDirCB->cbBase.deferred = nullptr; + getOrCreateLocalDirCB->cbBase.ability = ability; + + HILOG_INFO("%{public}s end.", __func__); + return getOrCreateLocalDirCB; +} + +/** + * @brief GetOrCreateLocalDir asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetOrCreateLocalDirExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetOrCreateLocalDir, worker pool thread execute."); + GetOrCreateLocalDirCB *getOrCreateLocalDirCB = (GetOrCreateLocalDirCB *)data; + if (getOrCreateLocalDirCB == nullptr) { + HILOG_ERROR("NAPI_GetOrCreateLocalDir, callingBundleCB == nullptr"); + return; + } + + getOrCreateLocalDirCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (getOrCreateLocalDirCB->cbBase.ability == nullptr) { + HILOG_ERROR("NAPI_GetOrCreateLocalDir, ability == nullptr"); + getOrCreateLocalDirCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + std::string abilityName = getOrCreateLocalDirCB->cbBase.ability->GetAbilityInfo()->name; + std::string dataDir = getOrCreateLocalDirCB->cbBase.ability->GetAbilityInfo()->applicationInfo.dataDir; + std::shared_ptr hap = getOrCreateLocalDirCB->cbBase.ability->GetHapModuleInfo(); + std::string moduleName = (hap != nullptr) ? hap->name : std::string(); + + HILOG_INFO("NAPI_GetOrCreateLocalDir, abilityName:%{public}s moduleName:%{public}s", + abilityName.c_str(), + moduleName.c_str()); + getOrCreateLocalDirCB->rootDir = + dataDir + CONTEXT_DEAL_FILE_SEPARATOR + moduleName + CONTEXT_DEAL_FILE_SEPARATOR + abilityName; + HILOG_INFO("NAPI_GetOrCreateLocalDir, GetDir rootDir:%{public}s", getOrCreateLocalDirCB->rootDir.c_str()); + if (!OHOS::FileExists(getOrCreateLocalDirCB->rootDir)) { + HILOG_INFO("NAPI_GetOrCreateLocalDir dir is not exits, create dir."); + OHOS::ForceCreateDirectory(getOrCreateLocalDirCB->rootDir); + OHOS::ChangeModeDirectory(getOrCreateLocalDirCB->rootDir, MODE); + } + HILOG_INFO("NAPI_GetOrCreateLocalDir, worker pool thread execute end."); +} + +napi_value WrapGetOrCreateLocalDir(napi_env env, GetOrCreateLocalDirCB *getOrCreateLocalDirCB) +{ + HILOG_INFO("%{public}s, called.", __func__); + if (getOrCreateLocalDirCB == nullptr) { + HILOG_ERROR("%{public}s,Invalid param(getOrCreateLocalDirCB == nullptr)", __func__); + return nullptr; + } + napi_value result = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, getOrCreateLocalDirCB->rootDir.c_str(), NAPI_AUTO_LENGTH, &result)); + HILOG_INFO("%{public}s, end.", __func__); + return result; +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetOrCreateLocalDirAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetOrCreateLocalDir, main event thread complete."); + GetOrCreateLocalDirCB *getOrCreateLocalDirCB = (GetOrCreateLocalDirCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, getOrCreateLocalDirCB->cbBase.errCode); + if (getOrCreateLocalDirCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapGetOrCreateLocalDir(env, getOrCreateLocalDirCB); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, getOrCreateLocalDirCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (getOrCreateLocalDirCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, getOrCreateLocalDirCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, getOrCreateLocalDirCB->cbBase.asyncWork)); + delete getOrCreateLocalDirCB; + getOrCreateLocalDirCB = nullptr; + HILOG_INFO("NAPI_GetOrCreateLocalDir, main event thread complete end."); +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetOrCreateLocalDirPromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetOrCreateLocalDir, main event thread complete."); + GetOrCreateLocalDirCB *getOrCreateLocalDirCB = (GetOrCreateLocalDirCB *)data; + napi_value result = nullptr; + if (getOrCreateLocalDirCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapGetOrCreateLocalDir(env, getOrCreateLocalDirCB); + napi_resolve_deferred(env, getOrCreateLocalDirCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, getOrCreateLocalDirCB->cbBase.errCode); + napi_reject_deferred(env, getOrCreateLocalDirCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, getOrCreateLocalDirCB->cbBase.asyncWork); + delete getOrCreateLocalDirCB; + getOrCreateLocalDirCB = nullptr; + HILOG_INFO("NAPI_GetOrCreateLocalDir, main event thread complete end."); +} + +/** + * @brief GetOrCreateLocalDir Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param CallingBundleCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetOrCreateLocalDirAsync( + napi_env env, napi_value *args, const size_t argCallback, GetOrCreateLocalDirCB *getOrCreateLocalDirCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || getOrCreateLocalDirCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL( + env, napi_create_reference(env, args[argCallback], 1, &getOrCreateLocalDirCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetOrCreateLocalDirExecuteCB, + GetOrCreateLocalDirAsyncCompleteCB, + (void *)getOrCreateLocalDirCB, + &getOrCreateLocalDirCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, getOrCreateLocalDirCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return result; +} + +/** + * @brief GetOrCreateLocalDir Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param CallingBundleCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetOrCreateLocalDirPromise(napi_env env, GetOrCreateLocalDirCB *getOrCreateLocalDirCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (getOrCreateLocalDirCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + getOrCreateLocalDirCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetOrCreateLocalDirExecuteCB, + GetOrCreateLocalDirPromiseCompleteCB, + (void *)getOrCreateLocalDirCB, + &getOrCreateLocalDirCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, getOrCreateLocalDirCB->cbBase.asyncWork)); + HILOG_INFO("%{public}s, promise end.", __func__); + return promise; +} + +/** + * @brief GetOrCreateLocalDir processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param CallingBundleCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetOrCreateLocalDirWrap(napi_env env, napi_callback_info info, GetOrCreateLocalDirCB *getOrCreateLocalDirCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (getOrCreateLocalDirCB == nullptr) { + HILOG_ERROR("%{public}s, getOrCreateLocalDirCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetOrCreateLocalDirAsync(env, args, 0, getOrCreateLocalDirCB); + } else { + ret = GetOrCreateLocalDirPromise(env, getOrCreateLocalDirCB); + } + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return ret; +} + +/** + * @brief Get bundle name. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetBundleName(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = CreateAsyncJSCallbackInfo(env); + if (asyncCallbackInfo == nullptr) { + HILOG_INFO("%{public}s called. Invoke CreateAsyncJSCallbackInfo failed.", __func__); + return WrapVoidToJS(env); + } + + napi_value ret = NAPI_GetBundleNameWrap(env, info, asyncCallbackInfo); + if (ret == nullptr) { + HILOG_ERROR("%{public}s ret == nullptr", __func__); + FreeAsyncJSCallbackInfo(&asyncCallbackInfo); + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s end", __func__); + return ret; +} + +/** + * @brief Obtains information about the current application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetApplicationInfo(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s,called", __func__); + AppInfoCB *appInfoCB = CreateAppInfoCBInfo(env); + if (appInfoCB == nullptr) { + return WrapVoidToJS(env); + } + + napi_value ret = GetApplicationInfoWrap(env, info, appInfoCB); + if (ret == nullptr) { + HILOG_ERROR("%{public}s,ret == nullptr", __func__); + if (appInfoCB != nullptr) { + delete appInfoCB; + appInfoCB = nullptr; + } + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s,end", __func__); + return ret; +} + +/** + * @brief Obtains the process Info this application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetProcessInfo(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + ProcessInfoCB *processInfoCB = CreateProcessInfoCBInfo(env); + if (processInfoCB == nullptr) { + return WrapVoidToJS(env); + } + + processInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + napi_value ret = GetProcessInfoWrap(env, info, processInfoCB); + if (ret == nullptr) { + HILOG_ERROR("%{public}s, ret == nullptr.", __func__); + if (processInfoCB != nullptr) { + delete processInfoCB; + processInfoCB = nullptr; + } + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s, end.", __func__); + return ret; +} + +/** + * @brief Obtains the elementName object of the current ability. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetElementName(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + ElementNameCB *elementNameCB = CreateElementNameCBInfo(env); + if (elementNameCB == nullptr) { + return WrapVoidToJS(env); + } + + elementNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + napi_value ret = GetElementNameWrap(env, info, elementNameCB); + if (ret == nullptr) { + HILOG_ERROR("%{public}s ret == nullptr.", __func__); + if (elementNameCB != nullptr) { + delete elementNameCB; + elementNameCB = nullptr; + } + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s end.", __func__); + return ret; +} + +/** + * @brief Obtains the name of the current process. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetProcessName(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + ProcessNameCB *processNameCB = CreateProcessNameCBInfo(env); + if (processNameCB == nullptr) { + return WrapVoidToJS(env); + } + + processNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + napi_value ret = GetProcessNameWrap(env, info, processNameCB); + if (ret == nullptr) { + HILOG_ERROR("%{public}s ret == nullptr.", __func__); + if (processNameCB != nullptr) { + delete processNameCB; + processNameCB = nullptr; + } + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s end.", __func__); + return ret; +} + +/** + * @brief Obtains the bundle name of the ability that called the current ability. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetCallingBundle(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + CallingBundleCB *callingBundleCB = CreateCallingBundleCBInfo(env); + if (callingBundleCB == nullptr) { + return WrapVoidToJS(env); + } + + callingBundleCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + napi_value ret = GetCallingBundleWrap(env, info, callingBundleCB); + if (ret == nullptr) { + HILOG_ERROR("%{public}s ret == nullptr", __func__); + if (callingBundleCB != nullptr) { + delete callingBundleCB; + callingBundleCB = nullptr; + } + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s end.", __func__); + return ret; +} + +/** + * @brief Get the local root dir of an app. If it is the first call, the dir will be created. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetOrCreateLocalDir(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + GetOrCreateLocalDirCB *getOrCreateLocalDirCB = CreateGetOrCreateLocalDirCBInfo(env); + if (getOrCreateLocalDirCB == nullptr) { + return WrapVoidToJS(env); + } + + getOrCreateLocalDirCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + napi_value ret = GetOrCreateLocalDirWrap(env, info, getOrCreateLocalDirCB); + if (ret == nullptr) { + HILOG_ERROR("%{public}s ret == nullptr", __func__); + if (getOrCreateLocalDirCB != nullptr) { + delete getOrCreateLocalDirCB; + getOrCreateLocalDirCB = nullptr; + } + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s end.", __func__); + return ret; +} /** * @brief Context NAPI module registration. * @@ -853,6 +2655,13 @@ napi_value ContextPermissionInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("verifyPermission", NAPI_VerifyPermission), DECLARE_NAPI_FUNCTION("verifyCallingPermission", NAPI_VerifyCallingPermission), DECLARE_NAPI_FUNCTION("verifyCallingOrSelfPermission", NAPI_VerifyCallingOrSelfPermission), + DECLARE_NAPI_FUNCTION("getApplicationInfo", NAPI_GetApplicationInfo), + DECLARE_NAPI_FUNCTION("getProcessInfo", NAPI_GetProcessInfo), + DECLARE_NAPI_FUNCTION("getElementName", NAPI_GetElementName), + DECLARE_NAPI_FUNCTION("getProcessName", NAPI_GetProcessName), + DECLARE_NAPI_FUNCTION("getCallingBundle", NAPI_GetCallingBundle), + DECLARE_NAPI_FUNCTION("getOrCreateLocalDir", NAPI_GetOrCreateLocalDir), + DECLARE_NAPI_FUNCTION("getFilesDir", NAPI_GetFilesDir), }; NAPI_CALL(env, diff --git a/interfaces/kits/napi/aafwk/featureAbility/napi_context.h b/interfaces/kits/napi/aafwk/featureAbility/napi_context.h index f6a1eb27373..aa26368a7c1 100755 --- a/interfaces/kits/napi/aafwk/featureAbility/napi_context.h +++ b/interfaces/kits/napi/aafwk/featureAbility/napi_context.h @@ -21,7 +21,7 @@ #include "../inner/napi_common/napi_common.h" using Ability = OHOS::AppExecFwk::Ability; - +#define MODE 0771 namespace OHOS { namespace AppExecFwk { @@ -62,6 +62,55 @@ void CallOnRequestPermissionsFromUserResult(int requestCode, const std::vectorcbBase.cbInfo.env = env; + insertCB->cbBase.asyncWork = nullptr; + insertCB->cbBase.deferred = nullptr; + insertCB->cbBase.ability = nullptr; napi_value ret = InsertWrap(env, info, insertCB); if (ret == nullptr) { @@ -212,7 +211,7 @@ napi_value InsertWrap(napi_env env, napi_callback_info info, DAHelperInsertCB *i insertCB->dataAbilityHelper = objectInfo; if (argcAsync > argcPromise) { - ret = InsertAsync(env, args, argcAsync, argcPromise, insertCB); + ret = InsertAsync(env, args, ARGS_TWO, insertCB); } else { ret = InsertPromise(env, insertCB); } @@ -220,8 +219,7 @@ napi_value InsertWrap(napi_env env, napi_callback_info info, DAHelperInsertCB *i return ret; } -napi_value InsertAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperInsertCB *insertCB) +napi_value InsertAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperInsertCB *insertCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || insertCB == nullptr) { @@ -232,9 +230,9 @@ napi_value InsertAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &insertCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &insertCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, @@ -366,12 +364,15 @@ napi_value UnwrapValuesBucket(std::string &value, napi_env env, napi_value args) napi_value NAPI_GetType(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s,called", __func__); - DAHelperGetTypeCB *gettypeCB = new (std::nothrow) DAHelperGetTypeCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = nullptr, - }; + DAHelperGetTypeCB *gettypeCB = new (std::nothrow) DAHelperGetTypeCB; + if (gettypeCB == nullptr) { + HILOG_ERROR("%{public}s, gettypeCB == nullptr.", __func__); + return WrapVoidToJS(env); + } + gettypeCB->cbBase.cbInfo.env = env; + gettypeCB->cbBase.asyncWork = nullptr; + gettypeCB->cbBase.deferred = nullptr; + gettypeCB->cbBase.ability = nullptr; napi_value ret = GetTypeWrap(env, info, gettypeCB); if (ret == nullptr) { @@ -418,7 +419,7 @@ napi_value GetTypeWrap(napi_env env, napi_callback_info info, DAHelperGetTypeCB gettypeCB->dataAbilityHelper = objectInfo; if (argcAsync > argcPromise) { - ret = GetTypeAsync(env, args, argcAsync, argcPromise, gettypeCB); + ret = GetTypeAsync(env, args, ARGS_ONE, gettypeCB); } else { ret = GetTypePromise(env, gettypeCB); } @@ -426,8 +427,7 @@ napi_value GetTypeWrap(napi_env env, napi_callback_info info, DAHelperGetTypeCB return ret; } -napi_value GetTypeAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperGetTypeCB *gettypeCB) +napi_value GetTypeAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperGetTypeCB *gettypeCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || gettypeCB == nullptr) { @@ -438,9 +438,9 @@ napi_value GetTypeAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &gettypeCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &gettypeCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, @@ -539,12 +539,15 @@ void GetTypePromiseCompleteCB(napi_env env, napi_status status, void *data) napi_value NAPI_GetFileTypes(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s,called", __func__); - DAHelperGetFileTypesCB *getfiletypesCB = new (std::nothrow) DAHelperGetFileTypesCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = nullptr, - }; + DAHelperGetFileTypesCB *getfiletypesCB = new (std::nothrow) DAHelperGetFileTypesCB; + if (getfiletypesCB == nullptr) { + HILOG_ERROR("%{public}s, getfiletypesCB == nullptr.", __func__); + return WrapVoidToJS(env); + } + getfiletypesCB->cbBase.cbInfo.env = env; + getfiletypesCB->cbBase.asyncWork = nullptr; + getfiletypesCB->cbBase.deferred = nullptr; + getfiletypesCB->cbBase.ability = nullptr; napi_value ret = GetFileTypesWrap(env, info, getfiletypesCB); if (ret == nullptr) { @@ -594,7 +597,7 @@ napi_value GetFileTypesWrap(napi_env env, napi_callback_info info, DAHelperGetFi getfiletypesCB->dataAbilityHelper = objectInfo; if (argcAsync > argcPromise) { - ret = GetFileTypesAsync(env, args, argcAsync, argcPromise, getfiletypesCB); + ret = GetFileTypesAsync(env, args, ARGS_TWO, getfiletypesCB); } else { ret = GetFileTypesPromise(env, getfiletypesCB); } @@ -602,7 +605,7 @@ napi_value GetFileTypesWrap(napi_env env, napi_callback_info info, DAHelperGetFi return ret; } napi_value GetFileTypesAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperGetFileTypesCB *getfiletypesCB) + napi_env env, napi_value *args, const size_t argCallback, DAHelperGetFileTypesCB *getfiletypesCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || getfiletypesCB == nullptr) { @@ -613,9 +616,9 @@ napi_value GetFileTypesAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &getfiletypesCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &getfiletypesCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, @@ -738,12 +741,15 @@ void GetFileTypesPromiseCompleteCB(napi_env env, napi_status status, void *data) napi_value NAPI_NormalizeUri(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s,called", __func__); - DAHelperNormalizeUriCB *normalizeuriCB = new (std::nothrow) DAHelperNormalizeUriCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = nullptr, - }; + DAHelperNormalizeUriCB *normalizeuriCB = new (std::nothrow) DAHelperNormalizeUriCB; + if (normalizeuriCB == nullptr) { + HILOG_ERROR("%{public}s, normalizeuriCB == nullptr.", __func__); + return WrapVoidToJS(env); + } + normalizeuriCB->cbBase.cbInfo.env = env; + normalizeuriCB->cbBase.asyncWork = nullptr; + normalizeuriCB->cbBase.deferred = nullptr; + normalizeuriCB->cbBase.ability = nullptr; napi_value ret = NormalizeUriWrap(env, info, normalizeuriCB); if (ret == nullptr) { @@ -787,7 +793,7 @@ napi_value NormalizeUriWrap(napi_env env, napi_callback_info info, DAHelperNorma normalizeuriCB->dataAbilityHelper = objectInfo; if (argcAsync > argcPromise) { - ret = NormalizeUriAsync(env, args, argcAsync, argcPromise, normalizeuriCB); + ret = NormalizeUriAsync(env, args, ARGS_ONE, normalizeuriCB); } else { ret = NormalizeUriPromise(env, normalizeuriCB); } @@ -795,7 +801,7 @@ napi_value NormalizeUriWrap(napi_env env, napi_callback_info info, DAHelperNorma return ret; } napi_value NormalizeUriAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperNormalizeUriCB *normalizeuriCB) + napi_env env, napi_value *args, const size_t argCallback, DAHelperNormalizeUriCB *normalizeuriCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || normalizeuriCB == nullptr) { @@ -806,9 +812,9 @@ napi_value NormalizeUriAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &normalizeuriCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &normalizeuriCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, @@ -910,12 +916,15 @@ void NormalizeUriPromiseCompleteCB(napi_env env, napi_status status, void *data) napi_value NAPI_DenormalizeUri(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s,called", __func__); - DAHelperDenormalizeUriCB *denormalizeuriCB = new (std::nothrow) DAHelperDenormalizeUriCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = nullptr, - }; + DAHelperDenormalizeUriCB *denormalizeuriCB = new (std::nothrow) DAHelperDenormalizeUriCB; + if (denormalizeuriCB == nullptr) { + HILOG_ERROR("%{public}s, denormalizeuriCB == nullptr.", __func__); + return WrapVoidToJS(env); + } + denormalizeuriCB->cbBase.cbInfo.env = env; + denormalizeuriCB->cbBase.asyncWork = nullptr; + denormalizeuriCB->cbBase.deferred = nullptr; + denormalizeuriCB->cbBase.ability = nullptr; napi_value ret = DenormalizeUriWrap(env, info, denormalizeuriCB); if (ret == nullptr) { @@ -959,15 +968,15 @@ napi_value DenormalizeUriWrap(napi_env env, napi_callback_info info, DAHelperDen denormalizeuriCB->dataAbilityHelper = objectInfo; if (argcAsync > argcPromise) { - ret = DenormalizeUriAsync(env, args, argcAsync, argcPromise, denormalizeuriCB); + ret = DenormalizeUriAsync(env, args, ARGS_ONE, denormalizeuriCB); } else { ret = DenormalizeUriPromise(env, denormalizeuriCB); } HILOG_INFO("%{public}s,end", __func__); return ret; } -napi_value DenormalizeUriAsync(napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, - DAHelperDenormalizeUriCB *denormalizeuriCB) +napi_value DenormalizeUriAsync( + napi_env env, napi_value *args, const size_t argCallback, DAHelperDenormalizeUriCB *denormalizeuriCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || denormalizeuriCB == nullptr) { @@ -978,9 +987,9 @@ napi_value DenormalizeUriAsync(napi_env env, napi_value *args, size_t argcAsync, NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &denormalizeuriCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &denormalizeuriCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, @@ -1116,12 +1125,15 @@ napi_value UnwrapDataAbilityPredicates(std::string &value, napi_env env, napi_va napi_value NAPI_Delete(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s,called", __func__); - DAHelperDeleteCB *deleteCB = new (std::nothrow) DAHelperDeleteCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = nullptr, - }; + DAHelperDeleteCB *deleteCB = new (std::nothrow) DAHelperDeleteCB; + if (deleteCB == nullptr) { + HILOG_ERROR("%{public}s, deleteCB == nullptr.", __func__); + return WrapVoidToJS(env); + } + deleteCB->cbBase.cbInfo.env = env; + deleteCB->cbBase.asyncWork = nullptr; + deleteCB->cbBase.deferred = nullptr; + deleteCB->cbBase.ability = nullptr; napi_value ret = DeleteWrap(env, info, deleteCB); if (ret == nullptr) { @@ -1179,7 +1191,7 @@ napi_value DeleteWrap(napi_env env, napi_callback_info info, DAHelperDeleteCB *d deleteCB->dataAbilityHelper = objectInfo; if (argcAsync > argcPromise) { - ret = DeleteAsync(env, args, argcAsync, argcPromise, deleteCB); + ret = DeleteAsync(env, args, ARGS_TWO, deleteCB); } else { ret = DeletePromise(env, deleteCB); } @@ -1187,8 +1199,7 @@ napi_value DeleteWrap(napi_env env, napi_callback_info info, DAHelperDeleteCB *d return ret; } -napi_value DeleteAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperDeleteCB *deleteCB) +napi_value DeleteAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperDeleteCB *deleteCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || deleteCB == nullptr) { @@ -1199,9 +1210,9 @@ napi_value DeleteAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &deleteCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &deleteCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, @@ -1307,12 +1318,15 @@ void DeletePromiseCompleteCB(napi_env env, napi_status status, void *data) napi_value NAPI_Update(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s,called", __func__); - DAHelperUpdateCB *updateCB = new (std::nothrow) DAHelperUpdateCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = nullptr, - }; + DAHelperUpdateCB *updateCB = new (std::nothrow) DAHelperUpdateCB; + if (updateCB == nullptr) { + HILOG_ERROR("%{public}s, updateCB == nullptr.", __func__); + return WrapVoidToJS(env); + } + updateCB->cbBase.cbInfo.env = env; + updateCB->cbBase.asyncWork = nullptr; + updateCB->cbBase.deferred = nullptr; + updateCB->cbBase.ability = nullptr; napi_value ret = UpdateWrap(env, info, updateCB); if (ret == nullptr) { @@ -1376,7 +1390,7 @@ napi_value UpdateWrap(napi_env env, napi_callback_info info, DAHelperUpdateCB *u updateCB->dataAbilityHelper = objectInfo; if (argcAsync > argcPromise) { - ret = UpdateAsync(env, args, argcAsync, argcPromise, updateCB); + ret = UpdateAsync(env, args, ARGS_THREE, updateCB); } else { ret = UpdatePromise(env, updateCB); } @@ -1384,8 +1398,7 @@ napi_value UpdateWrap(napi_env env, napi_callback_info info, DAHelperUpdateCB *u return ret; } -napi_value UpdateAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperUpdateCB *updateCB) +napi_value UpdateAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperUpdateCB *updateCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || updateCB == nullptr) { @@ -1396,9 +1409,9 @@ napi_value UpdateAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &updateCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &updateCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, @@ -1504,12 +1517,15 @@ void UpdatePromiseCompleteCB(napi_env env, napi_status status, void *data) napi_value NAPI_OpenFile(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s,called", __func__); - DAHelperOpenFileCB *openFileCB = new (std::nothrow) DAHelperOpenFileCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = nullptr, - }; + DAHelperOpenFileCB *openFileCB = new (std::nothrow) DAHelperOpenFileCB; + if (openFileCB == nullptr) { + HILOG_ERROR("%{public}s, openFileCB == nullptr.", __func__); + return WrapVoidToJS(env); + } + openFileCB->cbBase.cbInfo.env = env; + openFileCB->cbBase.asyncWork = nullptr; + openFileCB->cbBase.deferred = nullptr; + openFileCB->cbBase.ability = nullptr; napi_value ret = OpenFileWrap(env, info, openFileCB); if (ret == nullptr) { @@ -1567,7 +1583,7 @@ napi_value OpenFileWrap(napi_env env, napi_callback_info info, DAHelperOpenFileC openFileCB->dataAbilityHelper = objectInfo; if (argcAsync > argcPromise) { - ret = OpenFileAsync(env, args, argcAsync, argcPromise, openFileCB); + ret = OpenFileAsync(env, args, ARGS_TWO, openFileCB); } else { ret = OpenFilePromise(env, openFileCB); } @@ -1575,8 +1591,7 @@ napi_value OpenFileWrap(napi_env env, napi_callback_info info, DAHelperOpenFileC return ret; } -napi_value OpenFileAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperOpenFileCB *openFileCB) +napi_value OpenFileAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperOpenFileCB *openFileCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || openFileCB == nullptr) { @@ -1587,9 +1602,9 @@ napi_value OpenFileAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &openFileCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &openFileCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, @@ -1695,12 +1710,15 @@ void OpenFilePromiseCompleteCB(napi_env env, napi_status status, void *data) napi_value NAPI_BatchInsert(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s,called", __func__); - DAHelperBatchInsertCB *BatchInsertCB = new (std::nothrow) DAHelperBatchInsertCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = nullptr, - }; + DAHelperBatchInsertCB *BatchInsertCB = new (std::nothrow) DAHelperBatchInsertCB; + if (BatchInsertCB == nullptr) { + HILOG_ERROR("%{public}s, BatchInsertCB == nullptr.", __func__); + return WrapVoidToJS(env); + } + BatchInsertCB->cbBase.cbInfo.env = env; + BatchInsertCB->cbBase.asyncWork = nullptr; + BatchInsertCB->cbBase.deferred = nullptr; + BatchInsertCB->cbBase.ability = nullptr; napi_value ret = BatchInsertWrap(env, info, BatchInsertCB); if (ret == nullptr) { @@ -1792,7 +1810,7 @@ napi_value BatchInsertWrap(napi_env env, napi_callback_info info, DAHelperBatchI batchInsertCB->dataAbilityHelper = objectInfo; if (argcAsync > argcPromise) { - ret = BatchInsertAsync(env, args, argcAsync, argcPromise, batchInsertCB); + ret = BatchInsertAsync(env, args, ARGS_TWO, batchInsertCB); } else { ret = BatchInsertPromise(env, batchInsertCB); } @@ -1801,7 +1819,7 @@ napi_value BatchInsertWrap(napi_env env, napi_callback_info info, DAHelperBatchI } napi_value BatchInsertAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperBatchInsertCB *batchInsertCB) + napi_env env, napi_value *args, const size_t argCallback, DAHelperBatchInsertCB *batchInsertCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || batchInsertCB == nullptr) { @@ -1812,9 +1830,9 @@ napi_value BatchInsertAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &batchInsertCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &batchInsertCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, @@ -1920,12 +1938,15 @@ void BatchInsertPromiseCompleteCB(napi_env env, napi_status status, void *data) napi_value NAPI_Query(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s,called", __func__); - DAHelperQueryCB *QueryCB = new (std::nothrow) DAHelperQueryCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = nullptr, - }; + DAHelperQueryCB *QueryCB = new (std::nothrow) DAHelperQueryCB; + if (QueryCB == nullptr) { + HILOG_ERROR("%{public}s, QueryCB == nullptr.", __func__); + return WrapVoidToJS(env); + } + QueryCB->cbBase.cbInfo.env = env; + QueryCB->cbBase.asyncWork = nullptr; + QueryCB->cbBase.deferred = nullptr; + QueryCB->cbBase.ability = nullptr; napi_value ret = QueryWrap(env, info, QueryCB); if (ret == nullptr) { @@ -1995,7 +2016,7 @@ napi_value QueryWrap(napi_env env, napi_callback_info info, DAHelperQueryCB *que queryCB->dataAbilityHelper = objectInfo; if (argcAsync > argcPromise) { - ret = QueryAsync(env, args, argcAsync, argcPromise, queryCB); + ret = QueryAsync(env, args, ARGS_THREE, queryCB); } else { ret = QueryPromise(env, queryCB); } @@ -2003,8 +2024,7 @@ napi_value QueryWrap(napi_env env, napi_callback_info info, DAHelperQueryCB *que return ret; } -napi_value QueryAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperQueryCB *queryCB) +napi_value QueryAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperQueryCB *queryCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || queryCB == nullptr) { @@ -2015,9 +2035,9 @@ napi_value QueryAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &queryCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &queryCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, @@ -2134,11 +2154,14 @@ napi_value WrapResultSet(napi_env env, const ResultSet &resultSet) napi_value NAPI_Release(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s,called", __func__); - DAHelperReleaseCB *releaseCB = new (std::nothrow) DAHelperReleaseCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - }; + DAHelperReleaseCB *releaseCB = new (std::nothrow) DAHelperReleaseCB; + if (releaseCB == nullptr) { + HILOG_ERROR("%{public}s, releaseCB == nullptr.", __func__); + return WrapVoidToJS(env); + } + releaseCB->cbBase.cbInfo.env = env; + releaseCB->cbBase.asyncWork = nullptr; + releaseCB->cbBase.deferred = nullptr; napi_value ret = ReleaseWrap(env, info, releaseCB); if (ret == nullptr) { @@ -2175,7 +2198,7 @@ napi_value ReleaseWrap(napi_env env, napi_callback_info info, DAHelperReleaseCB releaseCB->dataAbilityHelper = objectInfo; if (argcAsync > argcPromise) { - ret = ReleaseAsync(env, args, argcAsync, argcPromise, releaseCB); + ret = ReleaseAsync(env, args, ARGS_ONE, releaseCB); } else { ret = ReleasePromise(env, releaseCB); } @@ -2183,8 +2206,7 @@ napi_value ReleaseWrap(napi_env env, napi_callback_info info, DAHelperReleaseCB return ret; } -napi_value ReleaseAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperReleaseCB *releaseCB) +napi_value ReleaseAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperReleaseCB *releaseCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || releaseCB == nullptr) { @@ -2195,9 +2217,9 @@ napi_value ReleaseAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &releaseCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &releaseCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, diff --git a/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.h b/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.h index 5289835da5b..6c594568642 100644 --- a/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.h +++ b/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.h @@ -60,8 +60,7 @@ napi_value InsertWrap(napi_env env, napi_callback_info info, DAHelperInsertCB *i * * @return Return JS data successfully, otherwise return nullptr. */ -napi_value InsertAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperInsertCB *insertCB); +napi_value InsertAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperInsertCB *insertCB); /** * @brief Insert Promise. @@ -113,8 +112,7 @@ napi_value NAPI_Release(napi_env env, napi_callback_info info); napi_value ReleaseWrap(napi_env env, napi_callback_info info, DAHelperReleaseCB *releaseCB); -napi_value ReleaseAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperReleaseCB *releaseCB); +napi_value ReleaseAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperReleaseCB *releaseCB); napi_value ReleasePromise(napi_env env, DAHelperReleaseCB *releaseCB); @@ -127,8 +125,7 @@ void ReleasePromiseCompleteCB(napi_env env, napi_status status, void *data); napi_value NAPI_GetType(napi_env env, napi_callback_info info); napi_value NAPI_GetType(napi_env env, napi_callback_info info); napi_value GetTypeWrap(napi_env env, napi_callback_info info, DAHelperGetTypeCB *gettypeCB); -napi_value GetTypeAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperGetTypeCB *gettypeCB); +napi_value GetTypeAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperGetTypeCB *gettypeCB); napi_value GetTypePromise(napi_env env, DAHelperGetTypeCB *gettypeCB); void GetTypeExecuteCB(napi_env env, void *data); void GetTypeAsyncCompleteCB(napi_env env, napi_status status, void *data); @@ -138,7 +135,7 @@ napi_value NAPI_GetFileTypes(napi_env env, napi_callback_info info); napi_value NAPI_GetFileTypes(napi_env env, napi_callback_info info); napi_value GetFileTypesWrap(napi_env env, napi_callback_info info, DAHelperGetFileTypesCB *getfiletypesCB); napi_value GetFileTypesAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperGetFileTypesCB *getfiletypesCB); + napi_env env, napi_value *args, const size_t argCallback, DAHelperGetFileTypesCB *getfiletypesCB); napi_value GetFileTypesPromise(napi_env env, DAHelperGetFileTypesCB *getfiletypesCB); void GetFileTypesExecuteCB(napi_env env, void *data); void GetFileTypesAsyncCompleteCB(napi_env env, napi_status status, void *data); @@ -149,7 +146,7 @@ napi_value NAPI_NormalizeUri(napi_env env, napi_callback_info info); napi_value NAPI_NormalizeUri(napi_env env, napi_callback_info info); napi_value NormalizeUriWrap(napi_env env, napi_callback_info info, DAHelperNormalizeUriCB *normalizeuriCB); napi_value NormalizeUriAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperNormalizeUriCB *normalizeuriCB); + napi_env env, napi_value *args, const size_t argCallback, DAHelperNormalizeUriCB *normalizeuriCB); napi_value NormalizeUriPromise(napi_env env, DAHelperNormalizeUriCB *normalizeuriCB); void NormalizeUriExecuteCB(napi_env env, void *data); void NormalizeUriAsyncCompleteCB(napi_env env, napi_status status, void *data); @@ -158,8 +155,8 @@ void NormalizeUriPromiseCompleteCB(napi_env env, napi_status status, void *data) napi_value NAPI_DenormalizeUri(napi_env env, napi_callback_info info); napi_value NAPI_DenormalizeUri(napi_env env, napi_callback_info info); napi_value DenormalizeUriWrap(napi_env env, napi_callback_info info, DAHelperDenormalizeUriCB *denormalizeuriCB); -napi_value DenormalizeUriAsync(napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, - DAHelperDenormalizeUriCB *denormalizeuriCB); +napi_value DenormalizeUriAsync( + napi_env env, napi_value *args, const size_t argCallback, DAHelperDenormalizeUriCB *denormalizeuriCB); napi_value DenormalizeUriPromise(napi_env env, DAHelperDenormalizeUriCB *denormalizeuriCB); void DenormalizeUriExecuteCB(napi_env env, void *data); void DenormalizeUriAsyncCompleteCB(napi_env env, napi_status status, void *data); @@ -168,8 +165,7 @@ void DenormalizeUriPromiseCompleteCB(napi_env env, napi_status status, void *dat napi_value NAPI_Delete(napi_env env, napi_callback_info info); napi_value DeleteWrap(napi_env env, napi_callback_info info, DAHelperDeleteCB *deleteCB); -napi_value DeleteAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperDeleteCB *deleteCB); +napi_value DeleteAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperDeleteCB *deleteCB); napi_value DeletePromise(napi_env env, DAHelperDeleteCB *deleteCB); @@ -182,8 +178,7 @@ void DeletePromiseCompleteCB(napi_env env, napi_status status, void *data); napi_value NAPI_Update(napi_env env, napi_callback_info info); napi_value UpdateWrap(napi_env env, napi_callback_info info, DAHelperUpdateCB *updateCB); -napi_value UpdateAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperUpdateCB *updateCB); +napi_value UpdateAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperUpdateCB *updateCB); napi_value UpdatePromise(napi_env env, DAHelperUpdateCB *updateCB); @@ -196,8 +191,7 @@ void UpdatePromiseCompleteCB(napi_env env, napi_status status, void *data); napi_value NAPI_OpenFile(napi_env env, napi_callback_info info); napi_value OpenFileWrap(napi_env env, napi_callback_info info, DAHelperOpenFileCB *openFileCB); -napi_value OpenFileAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperOpenFileCB *openFileCB); +napi_value OpenFileAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperOpenFileCB *openFileCB); napi_value OpenFilePromise(napi_env env, DAHelperOpenFileCB *openFileCB); @@ -211,7 +205,7 @@ napi_value NAPI_BatchInsert(napi_env env, napi_callback_info info); napi_value BatchInsertWrap(napi_env env, napi_callback_info info, DAHelperBatchInsertCB *batchInsertCB); napi_value BatchInsertAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperBatchInsertCB *batchInsertCB); + napi_env env, napi_value *args, const size_t argCallback, DAHelperBatchInsertCB *batchInsertCB); napi_value BatchInsertPromise(napi_env env, DAHelperBatchInsertCB *batchInsertCB); @@ -229,8 +223,7 @@ napi_value NAPI_Query(napi_env env, napi_callback_info info); napi_value QueryWrap(napi_env env, napi_callback_info info, DAHelperQueryCB *queryCB); -napi_value QueryAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperQueryCB *queryCB); +napi_value QueryAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperQueryCB *queryCB); napi_value QueryPromise(napi_env env, DAHelperQueryCB *queryCB); diff --git a/interfaces/kits/napi/aafwk/featureAbility/native_module.cpp b/interfaces/kits/napi/aafwk/featureAbility/native_module.cpp index c01d8940d99..fae1617e5b1 100644 --- a/interfaces/kits/napi/aafwk/featureAbility/native_module.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/native_module.cpp @@ -19,6 +19,7 @@ #include #include "feature_ability.h" +#include "feature_ability_constant.h" #include "napi_context.h" #include "napi_data_ability_helper.h" #include "napi/native_api.h" @@ -34,6 +35,7 @@ static napi_value Init(napi_env env, napi_value exports) FeatureAbilityInit(env, exports); ContextPermissionInit(env, exports); DataAbilityHelperInit(env, exports); + FAConstantInit(env, exports); return exports; } EXTERN_C_END diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/feature_ability_common.h b/interfaces/kits/napi/aafwk/inner/napi_common/feature_ability_common.h index 15d08b5e766..edbed418f0b 100644 --- a/interfaces/kits/napi/aafwk/inner/napi_common/feature_ability_common.h +++ b/interfaces/kits/napi/aafwk/inner/napi_common/feature_ability_common.h @@ -159,6 +159,11 @@ struct CallingBundleCB { std::string callingBundleName; }; +struct GetOrCreateLocalDirCB { + CBBase cbBase; + std::string rootDir; +}; + struct ElementNameCB { CBBase cbBase; std::string deviceId; diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp index ceaf1f2f6ee..0eafc257120 100755 --- a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp @@ -25,7 +25,7 @@ namespace OHOS { namespace AppExecFwk { napi_value g_classContext; - +napi_value g_dataAbilityHelper; using NAPICreateJsRemoteObject = napi_value (*)(napi_env env, const sptr target); napi_value *GetGlobalClassContext(void) @@ -135,40 +135,6 @@ void SaveAppInfo(AppInfo_ &appInfo, const ApplicationInfo &appInfoOrg) HILOG_INFO("%{public}s end.", __func__); } -/** - * @brief GetApplicationInfo asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppInfoExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); - AppInfoCB *appInfoCB = (AppInfoCB *)data; - appInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - - if (appInfoCB->cbBase.ability == nullptr) { - HILOG_ERROR("NAPI_GetApplicationInfo, ability == nullptr"); - appInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; - return; - } - - if (!CheckAbilityType(&appInfoCB->cbBase)) { - HILOG_ERROR("NAPI_GetApplicationInfo, wrong ability type"); - appInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; - return; - } - - std::shared_ptr appInfoPtr = appInfoCB->cbBase.ability->GetApplicationInfo(); - if (appInfoPtr != nullptr) { - SaveAppInfo(appInfoCB->appInfo, *appInfoPtr); - } else { - HILOG_ERROR("NAPI_GetApplicationInfo, appInfoPtr == nullptr"); - appInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; - } - HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute end."); -} - napi_value GetContinueAbilityOptionsInfoCommon( const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info) { @@ -304,36 +270,205 @@ napi_value WrapAppInfo(napi_env env, const AppInfo_ &appInfo) return result; } +/** + * @brief GetFilesDir asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetFilesDirExecuteCallback(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = (AsyncJSCallbackInfo *)data; + if (asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s. asyncCallbackInfo is null", __func__); + return; + } + + asyncCallbackInfo->error_code = NAPI_ERR_NO_ERROR; + asyncCallbackInfo->native_data.data_type = NVT_NONE; + if (asyncCallbackInfo->ability == nullptr) { + HILOG_ERROR("%{public}s ability == nullptr", __func__); + asyncCallbackInfo->error_code = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(asyncCallbackInfo)) { + HILOG_ERROR("%{public}s wrong ability type", __func__); + asyncCallbackInfo->error_code = NAPI_ERR_ABILITY_TYPE_INVALID; + asyncCallbackInfo->native_data.data_type = NVT_UNDEFINED; + return; + } + + asyncCallbackInfo->native_data.data_type = NVT_STRING; + asyncCallbackInfo->native_data.str_value = asyncCallbackInfo->ability->GetFilesDir(); + HILOG_INFO("%{public}s end. filesDir=%{public}s", __func__, asyncCallbackInfo->native_data.str_value.c_str()); +} +/** + * @brief GetFilesDir processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param asyncCallbackInfo Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value NAPI_GetFilesDirWrap(napi_env env, napi_callback_info info, AsyncJSCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s called", __func__); + size_t argc = ARGS_MAX_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value jsthis = 0; + void *data = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsthis, &data)); + + if (argc > ARGS_ONE) { + HILOG_INFO("%{public}s called, parameters is invalid.", __func__); + return nullptr; + } + + if (argc == ARGS_ONE) { + if (!CreateAsyncCallback(env, args[PARAM0], asyncCallbackInfo)) { + HILOG_INFO("%{public}s called, the first parameter is invalid.", __func__); + return nullptr; + } + } + + AsyncParamEx asyncParamEx; + if (asyncCallbackInfo->cbInfo.callback != nullptr) { + HILOG_INFO("%{public}s called. asyncCallback.", __func__); + asyncParamEx.resource = "NAPI_GetFilesDirCallback"; + asyncParamEx.execute = GetFilesDirExecuteCallback; + asyncParamEx.complete = CompleteAsyncCallbackWork; + + return ExecuteAsyncCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } else { + HILOG_INFO("%{public}s called. promise.", __func__); + asyncParamEx.resource = "NAPI_GetFilesDirPromise"; + asyncParamEx.execute = GetFilesDirExecuteCallback; + asyncParamEx.complete = CompletePromiseCallbackWork; + + return ExecutePromiseCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } +} +napi_value NAPI_GetFilesDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = CreateAsyncJSCallbackInfo(env); + if (asyncCallbackInfo == nullptr) { + HILOG_INFO("%{public}s called. Invoke CreateAsyncJSCallbackInfo failed.", __func__); + return WrapVoidToJS(env); + } + + asyncCallbackInfo->abilityType = abilityType; + napi_value ret = NAPI_GetFilesDirWrap(env, info, asyncCallbackInfo); + if (ret == nullptr) { + HILOG_ERROR("%{public}s ret == nullptr", __func__); + FreeAsyncJSCallbackInfo(&asyncCallbackInfo); + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s end", __func__); + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to AppTypeCB on success, nullptr on failure. + */ +AppTypeCB *CreateAppTypeCBInfo(napi_env env) +{ + HILOG_INFO("%{public}s, called.", __func__); + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + AppTypeCB *appTypeCB = new (std::nothrow) AppTypeCB; + if (appTypeCB == nullptr) { + HILOG_ERROR("%{public}s, appTypeCB == nullptr.", __func__); + return nullptr; + } + appTypeCB->cbBase.cbInfo.env = env; + appTypeCB->cbBase.asyncWork = nullptr; + appTypeCB->cbBase.deferred = nullptr; + appTypeCB->cbBase.ability = ability; + + HILOG_INFO("%{public}s, end.", __func__); + return appTypeCB; +} + +/** + * @brief GetAppType asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAppTypeExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); + AppTypeCB *appTypeCB = (AppTypeCB *)data; + if (appTypeCB == nullptr) { + HILOG_ERROR("NAPI_GetApplicationInfo,appTypeCB == nullptr"); + return; + } + + appTypeCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (appTypeCB->cbBase.ability == nullptr) { + HILOG_ERROR("NAPI_GetApplicationInfo,ability == nullptr"); + appTypeCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&appTypeCB->cbBase)) { + HILOG_ERROR("NAPI_GetApplicationInfo,wrong ability type"); + appTypeCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + appTypeCB->name = appTypeCB->cbBase.ability->GetAppType(); + HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute end."); +} + /** * @brief The callback at the end of the asynchronous callback. * * @param env The environment that the Node-API call is invoked under. * @param data Point to asynchronous processing of data. */ -void GetAppInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) +void GetAppTypeAsyncCompleteCB(napi_env env, napi_status status, void *data) { HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - AppInfoCB *appInfoCB = (AppInfoCB *)data; + AppTypeCB *appTypeCB = (AppTypeCB *)data; napi_value callback = nullptr; napi_value undefined = nullptr; napi_value result[ARGS_TWO] = {nullptr}; napi_value callResult = nullptr; NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, appInfoCB->cbBase.errCode); - if (appInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result[PARAM1] = WrapAppInfo(env, appInfoCB->appInfo); + + result[PARAM0] = GetCallbackErrorValue(env, appTypeCB->cbBase.errCode); + if (appTypeCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + NAPI_CALL_RETURN_VOID(env, + napi_create_string_utf8( + env, appTypeCB->cbBase.ability->GetAppType().c_str(), NAPI_AUTO_LENGTH, &result[PARAM1])); } else { result[PARAM1] = WrapUndefinedToJS(env); } - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, appInfoCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, appTypeCB->cbBase.cbInfo.callback, &callback)); NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - if (appInfoCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, appInfoCB->cbBase.cbInfo.callback)); + if (appTypeCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, appTypeCB->cbBase.cbInfo.callback)); } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, appInfoCB->cbBase.asyncWork)); - delete appInfoCB; - appInfoCB = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, appTypeCB->cbBase.asyncWork)); + delete appTypeCB; + appTypeCB = nullptr; HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete end."); } @@ -343,45 +478,39 @@ void GetAppInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) * @param env The environment that the Node-API call is invoked under. * @param data Point to asynchronous processing of data. */ -void GetAppInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) +void GetAppTypePromiseCompleteCB(napi_env env, napi_status status, void *data) { - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - AppInfoCB *appInfoCB = (AppInfoCB *)data; - if (appInfoCB == nullptr) { - HILOG_ERROR("NAPI_GetApplicationInfo, appInfoCB == nullptr"); - return; - } - + HILOG_INFO("GetAppTypePromiseCompleteCB, main event thread complete."); + AppTypeCB *appTypeCB = (AppTypeCB *)data; napi_value result = nullptr; - if (appInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result = WrapAppInfo(env, appInfoCB->appInfo); - napi_resolve_deferred(env, appInfoCB->cbBase.deferred, result); + if (appTypeCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + napi_create_string_utf8(env, appTypeCB->cbBase.ability->GetAppType().c_str(), NAPI_AUTO_LENGTH, &result); + napi_resolve_deferred(env, appTypeCB->cbBase.deferred, result); } else { - result = GetCallbackErrorValue(env, appInfoCB->cbBase.errCode); - napi_reject_deferred(env, appInfoCB->cbBase.deferred, result); + result = GetCallbackErrorValue(env, appTypeCB->cbBase.errCode); + napi_reject_deferred(env, appTypeCB->cbBase.deferred, result); } - napi_delete_async_work(env, appInfoCB->cbBase.asyncWork); - delete appInfoCB; - appInfoCB = nullptr; - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete end."); + napi_delete_async_work(env, appTypeCB->cbBase.asyncWork); + delete appTypeCB; + appTypeCB = nullptr; + HILOG_INFO("GetAppTypePromiseCompleteCB, main event thread complete end."); } /** - * @brief GetApplicationInfo Async. + * @brief GetAppType Async. * * @param env The environment that the Node-API call is invoked under. * @param args Indicates the arguments passed into the callback. * @param argcPromise Asynchronous data processing. - * @param appInfoCB Process data asynchronously. + * @param appTypeCB Process data asynchronously. * * @return Return JS data successfully, otherwise return nullptr. */ -napi_value GetApplicationInfoAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AppInfoCB *appInfoCB) +napi_value GetAppTypeAsync(napi_env env, napi_value *args, const size_t argCallback, AppTypeCB *appTypeCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || appInfoCB == nullptr) { + if (args == nullptr || appTypeCB == nullptr) { HILOG_ERROR("%{public}s, param == nullptr.", __func__); return nullptr; } @@ -389,19 +518,19 @@ napi_value GetApplicationInfoAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &appInfoCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &appTypeCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, - GetAppInfoExecuteCB, - GetAppInfoAsyncCompleteCB, - (void *)appInfoCB, - &appInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, appInfoCB->cbBase.asyncWork)); + GetAppTypeExecuteCB, + GetAppTypeAsyncCompleteCB, + (void *)appTypeCB, + &appTypeCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, appTypeCB->cbBase.asyncWork)); napi_value result = nullptr; NAPI_CALL(env, napi_get_null(env, &result)); HILOG_INFO("%{public}s, asyncCallback end.", __func__); @@ -409,17 +538,17 @@ napi_value GetApplicationInfoAsync( } /** - * @brief GetApplicationInfo Promise. + * @brief GetAppType Promise. * * @param env The environment that the Node-API call is invoked under. - * @param appInfoCB Process data asynchronously. + * @param appTypeCB Process data asynchronously. * * @return Return JS data successfully, otherwise return nullptr. */ -napi_value GetApplicationInfoPromise(napi_env env, AppInfoCB *appInfoCB) +napi_value GetAppTypePromise(napi_env env, AppTypeCB *appTypeCB) { HILOG_INFO("%{public}s, promise.", __func__); - if (appInfoCB == nullptr) { + if (appTypeCB == nullptr) { HILOG_ERROR("%{public}s, param == nullptr.", __func__); return nullptr; } @@ -428,34 +557,34 @@ napi_value GetApplicationInfoPromise(napi_env env, AppInfoCB *appInfoCB) napi_deferred deferred; napi_value promise = nullptr; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - appInfoCB->cbBase.deferred = deferred; + appTypeCB->cbBase.deferred = deferred; NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, - GetAppInfoExecuteCB, - GetAppInfoPromiseCompleteCB, - (void *)appInfoCB, - &appInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, appInfoCB->cbBase.asyncWork)); + GetAppTypeExecuteCB, + GetAppTypePromiseCompleteCB, + (void *)appTypeCB, + &appTypeCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, appTypeCB->cbBase.asyncWork)); HILOG_INFO("%{public}s, promise end.", __func__); return promise; } /** - * @brief GetApplicationInfo processing function. + * @brief GetAppType processing function. * * @param env The environment that the Node-API call is invoked under. - * @param appInfoCB Process data asynchronously. + * @param appTypeCB Process data asynchronously. * * @return Return JS data successfully, otherwise return nullptr. */ -napi_value GetApplicationInfoWrap(napi_env env, napi_callback_info info, AppInfoCB *appInfoCB) +napi_value GetAppTypeWrap(napi_env env, napi_callback_info info, AppTypeCB *appTypeCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (appInfoCB == nullptr) { - HILOG_ERROR("%{public}s, appInfoCB == nullptr.", __func__); + if (appTypeCB == nullptr) { + HILOG_ERROR("%{public}s, appTypeCB == nullptr.", __func__); return nullptr; } @@ -472,1932 +601,281 @@ napi_value GetApplicationInfoWrap(napi_env env, napi_callback_info info, AppInfo } if (argcAsync > argcPromise) { - ret = GetApplicationInfoAsync(env, args, argcAsync, argcPromise, appInfoCB); + ret = GetAppTypeAsync(env, args, 0, appTypeCB); } else { - ret = GetApplicationInfoPromise(env, appInfoCB); + ret = GetAppTypePromise(env, appTypeCB); } HILOG_INFO("%{public}s, asyncCallback end.", __func__); return ret; } /** - * @brief Create asynchronous data. + * @brief Obtains the type of this application. * * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. * - * @return Return a pointer to AppInfoCB on success, nullptr on failure. + * @return The return value from NAPI C++ to JS for the module. */ -AppInfoCB *CreateAppInfoCBInfo(napi_env env) +napi_value NAPI_GetAppTypeCommon(napi_env env, napi_callback_info info, AbilityType abilityType) { - HILOG_INFO("%{public}s, called.", __func__); - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + HILOG_INFO("%{public}s called.", __func__); + AppTypeCB *appTypeCB = CreateAppTypeCBInfo(env); + if (appTypeCB == nullptr) { + return WrapVoidToJS(env); + } - AppInfoCB *appInfoCB = new (std::nothrow) AppInfoCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - .cbBase.abilityType = AbilityType::UNKNOWN, - .cbBase.errCode = NAPI_ERR_NO_ERROR, - }; - if (appInfoCB == nullptr) { - HILOG_ERROR("%{public}s, appInfoCB == nullptr.", __func__); + appTypeCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + appTypeCB->cbBase.abilityType = abilityType; + napi_value ret = GetAppTypeWrap(env, info, appTypeCB); + if (ret == nullptr) { + HILOG_ERROR("%{public}s, ret == nullptr.", __func__); + if (appTypeCB != nullptr) { + delete appTypeCB; + appTypeCB = nullptr; + } + ret = WrapVoidToJS(env); } HILOG_INFO("%{public}s, end.", __func__); - return appInfoCB; + return ret; } /** - * @brief Obtains information about the current application. + * @brief Create asynchronous data. * * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. * - * @return The return value from NAPI C++ to JS for the module. + * @return Return a pointer to AbilityInfoCB on success, nullptr on failure. */ -napi_value NAPI_GetApplicationInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +AbilityInfoCB *CreateAbilityInfoCBInfo(napi_env env) { - HILOG_INFO("%{public}s,called", __func__); - AppInfoCB *appInfoCB = CreateAppInfoCBInfo(env); - if (appInfoCB == nullptr) { - return WrapVoidToJS(env); - } - - appInfoCB->cbBase.abilityType = abilityType; - napi_value ret = GetApplicationInfoWrap(env, info, appInfoCB); - if (ret == nullptr) { - HILOG_ERROR("%{public}s,ret == nullptr", __func__); - if (appInfoCB != nullptr) { - delete appInfoCB; - appInfoCB = nullptr; - } - ret = WrapVoidToJS(env); - } - HILOG_INFO("%{public}s,end", __func__); - return ret; -} + HILOG_INFO("%{public}s called.", __func__); + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); -/** - * @brief GetBundleName asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetBundleNameExecuteCallback(napi_env env, void *data) -{ - HILOG_INFO("%{public}s called", __func__); - AsyncJSCallbackInfo *asyncCallbackInfo = (AsyncJSCallbackInfo *)data; - if (asyncCallbackInfo == nullptr) { - HILOG_ERROR("%{public}s. asyncCallbackInfo is null", __func__); - return; - } + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - asyncCallbackInfo->error_code = NAPI_ERR_NO_ERROR; - asyncCallbackInfo->native_data.data_type = NVT_NONE; - if (asyncCallbackInfo->ability == nullptr) { - HILOG_ERROR("%{public}s ability == nullptr", __func__); - asyncCallbackInfo->error_code = NAPI_ERR_ACE_ABILITY; - return; - } + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - if (!CheckAbilityType(asyncCallbackInfo)) { - HILOG_ERROR("%{public}s wrong ability type", __func__); - asyncCallbackInfo->error_code = NAPI_ERR_ABILITY_TYPE_INVALID; - asyncCallbackInfo->native_data.data_type = NVT_UNDEFINED; - return; + AbilityInfoCB *abilityInfoCB = new (std::nothrow) AbilityInfoCB; + if (abilityInfoCB == nullptr) { + HILOG_ERROR("%{public}s, abilityInfoCB == nullptr.", __func__); + return nullptr; } + abilityInfoCB->cbBase.cbInfo.env = env; + abilityInfoCB->cbBase.asyncWork = nullptr; + abilityInfoCB->cbBase.deferred = nullptr; + abilityInfoCB->cbBase.ability = ability; - asyncCallbackInfo->native_data.data_type = NVT_STRING; - asyncCallbackInfo->native_data.str_value = asyncCallbackInfo->ability->GetBundleName(); - HILOG_INFO("%{public}s end. bundleName=%{public}s", __func__, asyncCallbackInfo->native_data.str_value.c_str()); + HILOG_INFO("%{public}s end.", __func__); + return abilityInfoCB; } -/** - * @brief GetBundleName processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param asyncCallbackInfo Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value NAPI_GetBundleNameWrap(napi_env env, napi_callback_info info, AsyncJSCallbackInfo *asyncCallbackInfo) +void SaveAbilityInfo(AbilityInfo_ &abilityInfo, const AbilityInfo &abilityInfoOrg) { - HILOG_INFO("%{public}s called", __func__); - size_t argc = ARGS_MAX_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value jsthis = 0; - void *data = nullptr; + HILOG_INFO("%{public}s called.", __func__); + abilityInfo.bundleName = abilityInfoOrg.bundleName; + abilityInfo.name = abilityInfoOrg.name; + abilityInfo.label = abilityInfoOrg.label; + abilityInfo.description = abilityInfoOrg.description; + abilityInfo.icon = abilityInfoOrg.iconPath; + abilityInfo.labelId = abilityInfoOrg.applicationInfo.labelId; + abilityInfo.descriptionId = abilityInfoOrg.applicationInfo.descriptionId; + abilityInfo.iconId = abilityInfoOrg.applicationInfo.iconId; + abilityInfo.moduleName = abilityInfoOrg.moduleName; + abilityInfo.process = abilityInfoOrg.process; + abilityInfo.isVisible = abilityInfoOrg.visible; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsthis, &data)); + abilityInfo.type = static_cast(abilityInfoOrg.type); + abilityInfo.orientation = static_cast(abilityInfoOrg.orientation); + abilityInfo.launchMode = static_cast(abilityInfoOrg.launchMode); - if (argc > ARGS_ONE) { - HILOG_INFO("%{public}s called, parameters is invalid.", __func__); - return nullptr; - } + abilityInfo.uri = abilityInfoOrg.uri; + abilityInfo.targetAbility = abilityInfoOrg.targetAbility; - if (argc == ARGS_ONE) { - if (!CreateAsyncCallback(env, args[PARAM0], asyncCallbackInfo)) { - HILOG_INFO("%{public}s called, the first parameter is invalid.", __func__); - return nullptr; - } + for (size_t i = 0; i < abilityInfoOrg.permissions.size(); i++) { + abilityInfo.permissions.emplace_back(abilityInfoOrg.permissions.at(i)); } - - AsyncParamEx asyncParamEx; - if (asyncCallbackInfo->cbInfo.callback != nullptr) { - HILOG_INFO("%{public}s called. asyncCallback.", __func__); - asyncParamEx.resource = "NAPI_GetBundleNameCallback"; - asyncParamEx.execute = GetBundleNameExecuteCallback; - asyncParamEx.complete = CompleteAsyncCallbackWork; - - return ExecuteAsyncCallbackWork(env, asyncCallbackInfo, &asyncParamEx); - } else { - HILOG_INFO("%{public}s called. promise.", __func__); - asyncParamEx.resource = "NAPI_GetBundleNamePromise"; - asyncParamEx.execute = GetBundleNameExecuteCallback; - asyncParamEx.complete = CompletePromiseCallbackWork; - - return ExecutePromiseCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + for (size_t i = 0; i < abilityInfoOrg.deviceTypes.size(); i++) { + abilityInfo.deviceTypes.emplace_back(abilityInfoOrg.deviceTypes.at(i)); } -} - -/** - * @brief Get bundle name. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetBundleNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType) -{ - HILOG_INFO("%{public}s called", __func__); - AsyncJSCallbackInfo *asyncCallbackInfo = CreateAsyncJSCallbackInfo(env); - if (asyncCallbackInfo == nullptr) { - HILOG_INFO("%{public}s called. Invoke CreateAsyncJSCallbackInfo failed.", __func__); - return WrapVoidToJS(env); + for (size_t i = 0; i < abilityInfoOrg.deviceCapabilities.size(); i++) { + abilityInfo.deviceCapabilities.emplace_back(abilityInfoOrg.deviceCapabilities.at(i)); } - asyncCallbackInfo->abilityType = abilityType; - napi_value ret = NAPI_GetBundleNameWrap(env, info, asyncCallbackInfo); - if (ret == nullptr) { - HILOG_ERROR("%{public}s ret == nullptr", __func__); - FreeAsyncJSCallbackInfo(&asyncCallbackInfo); - ret = WrapVoidToJS(env); - } - HILOG_INFO("%{public}s end", __func__); - return ret; + SaveAppInfo(abilityInfo.appInfo, abilityInfoOrg.applicationInfo); + + abilityInfo.readPermission = abilityInfoOrg.readPermission; + abilityInfo.writePermission = abilityInfoOrg.writePermission; + abilityInfo.formEntity = 0; // no data + abilityInfo.minFormHeight = 0; // no data + abilityInfo.defaultFormHeight = 0; // no data + abilityInfo.minFormWidth = 0; // no data + abilityInfo.defaultFormWidth = 0; // no data + abilityInfo.backgroundModes = 0; // no data + abilityInfo.subType = 0; // no data + abilityInfo.formEnabled = false; // no data + HILOG_INFO("%{public}s end.", __func__); } -napi_value WrapProcessInfo(napi_env env, ProcessInfoCB *processInfoCB) +napi_value WrapAbilityInfo(napi_env env, const AbilityInfo_ &abilityInfo) { - HILOG_INFO("%{public}s called", __func__); - if (processInfoCB == nullptr) { - HILOG_ERROR("%{public}s Invalid param(processInfoCB == nullptr)", __func__); - return nullptr; - } + HILOG_INFO("%{public}s called.", __func__); napi_value result = nullptr; napi_value proValue = nullptr; NAPI_CALL(env, napi_create_object(env, &result)); - NAPI_CALL(env, napi_create_int32(env, processInfoCB->pid, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "pid", proValue)); + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.bundleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "bundleName", proValue)); - NAPI_CALL(env, napi_create_string_utf8(env, processInfoCB->processName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "processName", proValue)); - HILOG_INFO("%{public}s end", __func__); - return result; -} + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.name.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "name", proValue)); -/** - * @brief GetProcessInfo asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessInfoExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetProcessInfo, worker pool thread execute."); - ProcessInfoCB *processInfoCB = (ProcessInfoCB *)data; - if (processInfoCB == nullptr) { - return; - } + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.label.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "label", proValue)); - processInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - if (processInfoCB->cbBase.ability == nullptr) { - HILOG_ERROR("NAPI_GetProcessInfo, ability == nullptr"); - processInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; - return; - } + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.description.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "description", proValue)); - if (!CheckAbilityType(&processInfoCB->cbBase)) { - HILOG_ERROR("NAPI_GetProcessInfo, wrong ability type"); - processInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; - return; - } + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.icon.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "icon", proValue)); - std::shared_ptr processInfoPtr = processInfoCB->cbBase.ability->GetProcessInfo(); - if (processInfoPtr != nullptr) { - processInfoCB->processName = processInfoPtr->GetProcessName(); - processInfoCB->pid = processInfoPtr->GetPid(); - } else { - HILOG_ERROR("NAPI_GetProcessInfo, processInfoPtr == nullptr"); - processInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; - } - HILOG_INFO("NAPI_GetProcessInfo, worker pool thread execute end."); -} + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.moduleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "moduleName", proValue)); -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetProcessInfo, main event thread complete."); - ProcessInfoCB *processInfoCB = (ProcessInfoCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, processInfoCB->cbBase.errCode); - if (processInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result[PARAM1] = WrapProcessInfo(env, processInfoCB); - } else { - result[PARAM1] = WrapUndefinedToJS(env); - } + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.process.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "process", proValue)); - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, processInfoCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + NAPI_CALL(env, napi_create_int32(env, static_cast(abilityInfo.type), &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "type", proValue)); - if (processInfoCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, processInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, processInfoCB->cbBase.asyncWork)); - delete processInfoCB; - processInfoCB = nullptr; - HILOG_INFO("NAPI_GetProcessInfo, main event thread complete end."); -} + NAPI_CALL(env, napi_create_int32(env, static_cast(abilityInfo.orientation), &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "orientation", proValue)); -/** - * @brief GetProcessInfo Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param ProcessInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessInfoAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ProcessInfoCB *processInfoCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || processInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + NAPI_CALL(env, napi_create_int32(env, static_cast(abilityInfo.launchMode), &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "launchMode", proValue)); - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &processInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetProcessInfoExecuteCB, - GetProcessInfoAsyncCompleteCB, - (void *)processInfoCB, - &processInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, processInfoCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - HILOG_INFO("%{public}s, asyncCallback end.", __func__); - return result; -} + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.uri.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "uri", proValue)); -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetProcessInfo, main event thread complete."); - ProcessInfoCB *processInfoCB = (ProcessInfoCB *)data; - napi_value result = nullptr; - if (processInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result = WrapProcessInfo(env, processInfoCB); - napi_resolve_deferred(env, processInfoCB->cbBase.deferred, result); - } else { - result = GetCallbackErrorValue(env, processInfoCB->cbBase.errCode); - napi_reject_deferred(env, processInfoCB->cbBase.deferred, result); - } + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.readPermission.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "readPermission", proValue)); - napi_delete_async_work(env, processInfoCB->cbBase.asyncWork); - delete processInfoCB; - processInfoCB = nullptr; - HILOG_INFO("NAPI_GetProcessInfo, main event thread complete end."); -} + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.writePermission.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "writePermission", proValue)); -/** - * @brief GetProcessInfo Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param ProcessInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessInfoPromise(napi_env env, ProcessInfoCB *processInfoCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (processInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - processInfoCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetProcessInfoExecuteCB, - GetProcessInfoPromiseCompleteCB, - (void *)processInfoCB, - &processInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, processInfoCB->cbBase.asyncWork)); - HILOG_INFO("%{public}s, promise end.", __func__); - return promise; -} - -/** - * @brief GetProcessInfo processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param ProcessInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessInfoWrap(napi_env env, napi_callback_info info, ProcessInfoCB *processInfoCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (processInfoCB == nullptr) { - HILOG_ERROR("%{public}s, processInfoCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetProcessInfoAsync(env, args, argcAsync, argcPromise, processInfoCB); - } else { - ret = GetProcessInfoPromise(env, processInfoCB); - } - HILOG_INFO("%{public}s, asyncCallback end.", __func__); - return ret; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to ProcessInfoCB on success, nullptr on failure. - */ -ProcessInfoCB *CreateProcessInfoCBInfo(napi_env env) -{ - HILOG_INFO("%{public}s, called.", __func__); - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - ProcessInfoCB *processInfoCB = new (std::nothrow) ProcessInfoCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - if (processInfoCB == nullptr) { - HILOG_ERROR("%{public}s, processInfoCB == nullptr.", __func__); - } - HILOG_INFO("%{public}s, end.", __func__); - return processInfoCB; -} - -/** - * @brief Obtains the process Info this application. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetProcessInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType) -{ - HILOG_INFO("%{public}s called.", __func__); - ProcessInfoCB *processInfoCB = CreateProcessInfoCBInfo(env); - if (processInfoCB == nullptr) { - return WrapVoidToJS(env); - } - - processInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - processInfoCB->cbBase.abilityType = abilityType; - napi_value ret = GetProcessInfoWrap(env, info, processInfoCB); - if (ret == nullptr) { - HILOG_ERROR("%{public}s, ret == nullptr.", __func__); - if (processInfoCB != nullptr) { - delete processInfoCB; - processInfoCB = nullptr; - } - ret = WrapVoidToJS(env); - } - HILOG_INFO("%{public}s, end.", __func__); - return ret; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AppTypeCB on success, nullptr on failure. - */ -AppTypeCB *CreateAppTypeCBInfo(napi_env env) -{ - HILOG_INFO("%{public}s, called.", __func__); - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - AppTypeCB *appTypeCB = new (std::nothrow) AppTypeCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - if (appTypeCB == nullptr) { - HILOG_ERROR("%{public}s, appTypeCB == nullptr.", __func__); - } - HILOG_INFO("%{public}s, end.", __func__); - return appTypeCB; -} - -/** - * @brief GetAppType asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppTypeExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); - AppTypeCB *appTypeCB = (AppTypeCB *)data; - if (appTypeCB == nullptr) { - HILOG_ERROR("NAPI_GetApplicationInfo,appTypeCB == nullptr"); - return; - } - - appTypeCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - if (appTypeCB->cbBase.ability == nullptr) { - HILOG_ERROR("NAPI_GetApplicationInfo,ability == nullptr"); - appTypeCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; - return; - } - - if (!CheckAbilityType(&appTypeCB->cbBase)) { - HILOG_ERROR("NAPI_GetApplicationInfo,wrong ability type"); - appTypeCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; - return; - } - - appTypeCB->name = appTypeCB->cbBase.ability->GetAppType(); - HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute end."); -} - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppTypeAsyncCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - AppTypeCB *appTypeCB = (AppTypeCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - - result[PARAM0] = GetCallbackErrorValue(env, appTypeCB->cbBase.errCode); - if (appTypeCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - NAPI_CALL_RETURN_VOID(env, - napi_create_string_utf8( - env, appTypeCB->cbBase.ability->GetAppType().c_str(), NAPI_AUTO_LENGTH, &result[PARAM1])); - } else { - result[PARAM1] = WrapUndefinedToJS(env); - } - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, appTypeCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - - if (appTypeCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, appTypeCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, appTypeCB->cbBase.asyncWork)); - delete appTypeCB; - appTypeCB = nullptr; - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete end."); -} - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppTypePromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("GetAppTypePromiseCompleteCB, main event thread complete."); - AppTypeCB *appTypeCB = (AppTypeCB *)data; - napi_value result = nullptr; - if (appTypeCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - napi_create_string_utf8(env, appTypeCB->cbBase.ability->GetAppType().c_str(), NAPI_AUTO_LENGTH, &result); - napi_resolve_deferred(env, appTypeCB->cbBase.deferred, result); - } else { - result = GetCallbackErrorValue(env, appTypeCB->cbBase.errCode); - napi_reject_deferred(env, appTypeCB->cbBase.deferred, result); - } - - napi_delete_async_work(env, appTypeCB->cbBase.asyncWork); - delete appTypeCB; - appTypeCB = nullptr; - HILOG_INFO("GetAppTypePromiseCompleteCB, main event thread complete end."); -} - -/** - * @brief GetAppType Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param appTypeCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAppTypeAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AppTypeCB *appTypeCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || appTypeCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &appTypeCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetAppTypeExecuteCB, - GetAppTypeAsyncCompleteCB, - (void *)appTypeCB, - &appTypeCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, appTypeCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - HILOG_INFO("%{public}s, asyncCallback end.", __func__); - return result; -} - -/** - * @brief GetAppType Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param appTypeCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAppTypePromise(napi_env env, AppTypeCB *appTypeCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (appTypeCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - appTypeCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetAppTypeExecuteCB, - GetAppTypePromiseCompleteCB, - (void *)appTypeCB, - &appTypeCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, appTypeCB->cbBase.asyncWork)); - HILOG_INFO("%{public}s, promise end.", __func__); - return promise; -} - -/** - * @brief GetAppType processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param appTypeCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAppTypeWrap(napi_env env, napi_callback_info info, AppTypeCB *appTypeCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (appTypeCB == nullptr) { - HILOG_ERROR("%{public}s, appTypeCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetAppTypeAsync(env, args, argcAsync, argcPromise, appTypeCB); - } else { - ret = GetAppTypePromise(env, appTypeCB); - } - HILOG_INFO("%{public}s, asyncCallback end.", __func__); - return ret; -} - -/** - * @brief Obtains the type of this application. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetAppTypeCommon(napi_env env, napi_callback_info info, AbilityType abilityType) -{ - HILOG_INFO("%{public}s called.", __func__); - AppTypeCB *appTypeCB = CreateAppTypeCBInfo(env); - if (appTypeCB == nullptr) { - return WrapVoidToJS(env); - } - - appTypeCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - appTypeCB->cbBase.abilityType = abilityType; - napi_value ret = GetAppTypeWrap(env, info, appTypeCB); - if (ret == nullptr) { - HILOG_ERROR("%{public}s, ret == nullptr.", __func__); - if (appTypeCB != nullptr) { - delete appTypeCB; - appTypeCB = nullptr; - } - ret = WrapVoidToJS(env); - } - HILOG_INFO("%{public}s, end.", __func__); - return ret; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to ElementNameCB on success, nullptr on failure. - */ -ElementNameCB *CreateElementNameCBInfo(napi_env env) -{ - HILOG_INFO("%{public}s, called.", __func__); - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - ElementNameCB *elementNameCB = new (std::nothrow) ElementNameCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - if (elementNameCB == nullptr) { - HILOG_ERROR("%{public}s, elementNameCB == nullptr.", __func__); - } - HILOG_INFO("%{public}s, end.", __func__); - return elementNameCB; -} - -napi_value WrapElementName(napi_env env, ElementNameCB *elementNameCB) -{ - HILOG_INFO("%{public}s, called.", __func__); - if (elementNameCB == nullptr) { - HILOG_ERROR("%{public}s,Invalid param(appInfoCB = nullptr)", __func__); - return nullptr; - } - napi_value result = nullptr; - napi_value proValue = nullptr; - NAPI_CALL(env, napi_create_object(env, &result)); - NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->abilityName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "abilityName", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->bundleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "bundleName", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->deviceId.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "deviceId", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->shortName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "shortName", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->uri.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "uri", proValue)); - HILOG_INFO("%{public}s, end.", __func__); - return result; -} - -/** - * @brief GetElementName asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetElementNameExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); - if (data == nullptr) { - HILOG_ERROR("%{public}s, data == nullptr.", __func__); - return; - } - ElementNameCB *elementNameCB = (ElementNameCB *)data; - if (elementNameCB == nullptr) { - HILOG_ERROR("NAPI_GetApplicationInfo, elementNameCB == nullptr"); - return; - } - - elementNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - if (elementNameCB->cbBase.ability == nullptr) { - HILOG_ERROR("NAPI_GetApplicationInfo, ability == nullptr"); - elementNameCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; - return; - } - - if (!CheckAbilityType(&elementNameCB->cbBase)) { - HILOG_ERROR("NAPI_GetApplicationInfo,wrong ability type"); - elementNameCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; - return; - } - - std::shared_ptr elementName = elementNameCB->cbBase.ability->GetElementName(); - if (elementNameCB != nullptr) { - elementNameCB->deviceId = elementName->GetDeviceID(); - elementNameCB->bundleName = elementName->GetBundleName(); - elementNameCB->abilityName = elementName->GetAbilityName(); - elementNameCB->uri = elementNameCB->cbBase.ability->GetWant()->GetUriString(); - elementNameCB->shortName = ""; - } else { - elementNameCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; - } - HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute end."); -} - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetElementNameAsyncCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - ElementNameCB *elementNameCB = (ElementNameCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, elementNameCB->cbBase.errCode); - if (elementNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result[PARAM1] = WrapElementName(env, elementNameCB); - } else { - result[PARAM1] = WrapUndefinedToJS(env); - } - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, elementNameCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - - if (elementNameCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, elementNameCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, elementNameCB->cbBase.asyncWork)); - delete elementNameCB; - elementNameCB = nullptr; - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete end."); -} - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetElementNamePromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - ElementNameCB *elementNameCB = (ElementNameCB *)data; - napi_value result = nullptr; - if (elementNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result = WrapElementName(env, elementNameCB); - napi_resolve_deferred(env, elementNameCB->cbBase.deferred, result); - } else { - result = GetCallbackErrorValue(env, elementNameCB->cbBase.errCode); - napi_reject_deferred(env, elementNameCB->cbBase.deferred, result); - } - - napi_delete_async_work(env, elementNameCB->cbBase.asyncWork); - delete elementNameCB; - elementNameCB = nullptr; - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete end."); -} - -/** - * @brief GetElementName Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param elementNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetElementNamePromise(napi_env env, ElementNameCB *elementNameCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (elementNameCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - elementNameCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetElementNameExecuteCB, - GetElementNamePromiseCompleteCB, - (void *)elementNameCB, - &elementNameCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, elementNameCB->cbBase.asyncWork)); - HILOG_INFO("%{public}s, promise end.", __func__); - return promise; -} - -/** - * @brief GetElementName Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param elementNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetElementNameAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ElementNameCB *elementNameCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || elementNameCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &elementNameCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetElementNameExecuteCB, - GetElementNameAsyncCompleteCB, - (void *)elementNameCB, - &elementNameCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, elementNameCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - return result; - HILOG_INFO("%{public}s, asyncCallback end.", __func__); -} - -/** - * @brief GetElementName processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param elementNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetElementNameWrap(napi_env env, napi_callback_info info, ElementNameCB *elementNameCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (elementNameCB == nullptr) { - HILOG_ERROR("%{public}s, appInfoCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetElementNameAsync(env, args, argcAsync, argcPromise, elementNameCB); - } else { - ret = GetElementNamePromise(env, elementNameCB); - } - HILOG_INFO("%{public}s, asyncCallback end.", __func__); - return ret; -} - -/** - * @brief Obtains the elementName object of the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetElementNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType) -{ - HILOG_INFO("%{public}s called.", __func__); - ElementNameCB *elementNameCB = CreateElementNameCBInfo(env); - if (elementNameCB == nullptr) { - return WrapVoidToJS(env); - } - - elementNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - elementNameCB->cbBase.abilityType = abilityType; - napi_value ret = GetElementNameWrap(env, info, elementNameCB); - if (ret == nullptr) { - HILOG_ERROR("%{public}s ret == nullptr.", __func__); - if (elementNameCB != nullptr) { - delete elementNameCB; - elementNameCB = nullptr; - } - ret = WrapVoidToJS(env); - } - HILOG_INFO("%{public}s end.", __func__); - return ret; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AbilityInfoCB on success, nullptr on failure. - */ -AbilityInfoCB *CreateAbilityInfoCBInfo(napi_env env) -{ - HILOG_INFO("%{public}s called.", __func__); - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - AbilityInfoCB *abilityInfoCB = new (std::nothrow) AbilityInfoCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - if (abilityInfoCB == nullptr) { - HILOG_ERROR("%{public}s, abilityInfoCB == nullptr.", __func__); - } - HILOG_INFO("%{public}s end.", __func__); - return abilityInfoCB; -} - -void SaveAbilityInfo(AbilityInfo_ &abilityInfo, const AbilityInfo &abilityInfoOrg) -{ - HILOG_INFO("%{public}s called.", __func__); - abilityInfo.bundleName = abilityInfoOrg.bundleName; - abilityInfo.name = abilityInfoOrg.name; - abilityInfo.label = abilityInfoOrg.label; - abilityInfo.description = abilityInfoOrg.description; - abilityInfo.icon = abilityInfoOrg.iconPath; - abilityInfo.labelId = abilityInfoOrg.applicationInfo.labelId; - abilityInfo.descriptionId = abilityInfoOrg.applicationInfo.descriptionId; - abilityInfo.iconId = abilityInfoOrg.applicationInfo.iconId; - abilityInfo.moduleName = abilityInfoOrg.moduleName; - abilityInfo.process = abilityInfoOrg.process; - abilityInfo.isVisible = abilityInfoOrg.visible; - - abilityInfo.type = static_cast(abilityInfoOrg.type); - abilityInfo.orientation = static_cast(abilityInfoOrg.orientation); - abilityInfo.launchMode = static_cast(abilityInfoOrg.launchMode); - - abilityInfo.uri = abilityInfoOrg.uri; - abilityInfo.targetAbility = abilityInfoOrg.targetAbility; - - for (size_t i = 0; i < abilityInfoOrg.permissions.size(); i++) { - abilityInfo.permissions.emplace_back(abilityInfoOrg.permissions.at(i)); - } - for (size_t i = 0; i < abilityInfoOrg.deviceTypes.size(); i++) { - abilityInfo.deviceTypes.emplace_back(abilityInfoOrg.deviceTypes.at(i)); - } - for (size_t i = 0; i < abilityInfoOrg.deviceCapabilities.size(); i++) { - abilityInfo.deviceCapabilities.emplace_back(abilityInfoOrg.deviceCapabilities.at(i)); - } - - SaveAppInfo(abilityInfo.appInfo, abilityInfoOrg.applicationInfo); - - abilityInfo.readPermission = abilityInfoOrg.readPermission; - abilityInfo.writePermission = abilityInfoOrg.writePermission; - abilityInfo.formEntity = 0; // no data - abilityInfo.minFormHeight = 0; // no data - abilityInfo.defaultFormHeight = 0; // no data - abilityInfo.minFormWidth = 0; // no data - abilityInfo.defaultFormWidth = 0; // no data - abilityInfo.backgroundModes = 0; // no data - abilityInfo.subType = 0; // no data - abilityInfo.formEnabled = false; // no data - HILOG_INFO("%{public}s end.", __func__); -} - -napi_value WrapAbilityInfo(napi_env env, const AbilityInfo_ &abilityInfo) -{ - HILOG_INFO("%{public}s called.", __func__); - napi_value result = nullptr; - napi_value proValue = nullptr; - NAPI_CALL(env, napi_create_object(env, &result)); - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.bundleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "bundleName", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.name.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "name", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.label.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "label", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.description.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "description", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.icon.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "icon", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.moduleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "moduleName", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.process.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "process", proValue)); - - NAPI_CALL(env, napi_create_int32(env, static_cast(abilityInfo.type), &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "type", proValue)); - - NAPI_CALL(env, napi_create_int32(env, static_cast(abilityInfo.orientation), &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "orientation", proValue)); - - NAPI_CALL(env, napi_create_int32(env, static_cast(abilityInfo.launchMode), &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "launchMode", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.uri.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "uri", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.readPermission.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "readPermission", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.writePermission.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "writePermission", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.targetAbility.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "targetAbility", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.labelId, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "labelId", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.descriptionId, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "descriptionId", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.iconId, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "iconId", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.formEntity, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "formEntity", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.minFormHeight, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "minFormHeight", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.defaultFormHeight, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "defaultFormHeight", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.minFormWidth, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "minFormWidth", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.defaultFormWidth, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "defaultFormWidth", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.backgroundModes, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "backgroundModes", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.subType, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "subType", proValue)); - - NAPI_CALL(env, napi_get_boolean(env, abilityInfo.isVisible, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "isVisible", proValue)); - - NAPI_CALL(env, napi_get_boolean(env, abilityInfo.formEnabled, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "formEnabled", proValue)); - - napi_value jsArrayPermissions = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArrayPermissions)); - for (size_t i = 0; i < abilityInfo.permissions.size(); i++) { - proValue = nullptr; - NAPI_CALL( - env, napi_create_string_utf8(env, abilityInfo.permissions.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_element(env, jsArrayPermissions, i, proValue)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "permissions", jsArrayPermissions)); - - napi_value jsArrayDeviceCapabilities = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArrayDeviceCapabilities)); - for (size_t i = 0; i < abilityInfo.deviceCapabilities.size(); i++) { - proValue = nullptr; - NAPI_CALL(env, - napi_create_string_utf8(env, abilityInfo.deviceCapabilities.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_element(env, jsArrayDeviceCapabilities, i, proValue)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "deviceCapabilities", jsArrayDeviceCapabilities)); - - napi_value jsArrayDeviceTypes = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArrayDeviceTypes)); - for (size_t i = 0; i < abilityInfo.deviceTypes.size(); i++) { - proValue = nullptr; - NAPI_CALL( - env, napi_create_string_utf8(env, abilityInfo.deviceTypes.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_element(env, jsArrayDeviceTypes, i, proValue)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "deviceTypes", jsArrayDeviceTypes)); - - napi_value appInfo = nullptr; - appInfo = WrapAppInfo(env, abilityInfo.appInfo); - NAPI_CALL(env, napi_set_named_property(env, result, "applicationInfo", appInfo)); - HILOG_INFO("%{public}s end.", __func__); - return result; -} - -/** - * @brief GetAbilityInfo asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAbilityInfoExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); - AbilityInfoCB *abilityInfoCB = (AbilityInfoCB *)data; - if (abilityInfoCB == nullptr) { - HILOG_ERROR("NAPI_GetApplicationInfo, abilityInfoCB == nullptr"); - return; - } - - abilityInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - if (abilityInfoCB->cbBase.ability == nullptr) { - HILOG_ERROR("NAPI_GetApplicationInfo, ability == nullptr"); - abilityInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; - return; - } - - if (!CheckAbilityType(&abilityInfoCB->cbBase)) { - HILOG_ERROR("NAPI_GetApplicationInfo,wrong ability type"); - abilityInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; - return; - } - - std::shared_ptr abilityInfoPtr = abilityInfoCB->cbBase.ability->GetAbilityInfo(); - if (abilityInfoPtr != nullptr) { - SaveAbilityInfo(abilityInfoCB->abilityInfo, *abilityInfoPtr); - } else { - abilityInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; - } - HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute end."); -} - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAbilityInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - AbilityInfoCB *abilityInfoCB = (AbilityInfoCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, abilityInfoCB->cbBase.errCode); - if (abilityInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result[PARAM1] = WrapAbilityInfo(env, abilityInfoCB->abilityInfo); - } else { - result[PARAM1] = WrapUndefinedToJS(env); - } - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, abilityInfoCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - - if (abilityInfoCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, abilityInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, abilityInfoCB->cbBase.asyncWork)); - delete abilityInfoCB; - abilityInfoCB = nullptr; - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete end."); -} - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAbilityInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - AbilityInfoCB *abilityInfoCB = (AbilityInfoCB *)data; - napi_value result = nullptr; - if (abilityInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result = WrapAbilityInfo(env, abilityInfoCB->abilityInfo); - napi_resolve_deferred(env, abilityInfoCB->cbBase.deferred, result); - } else { - result = GetCallbackErrorValue(env, abilityInfoCB->cbBase.errCode); - napi_reject_deferred(env, abilityInfoCB->cbBase.deferred, result); - } - - napi_delete_async_work(env, abilityInfoCB->cbBase.asyncWork); - delete abilityInfoCB; - abilityInfoCB = nullptr; - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete end."); -} - -/** - * @brief GetAbilityInfo Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param abilityInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityInfoAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AbilityInfoCB *abilityInfoCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || abilityInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &abilityInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetAbilityInfoExecuteCB, - GetAbilityInfoAsyncCompleteCB, - (void *)abilityInfoCB, - &abilityInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, abilityInfoCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - HILOG_INFO("%{public}s, asyncCallback end.", __func__); - return result; -} - -/** - * @brief GetAbilityInfo Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param abilityInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityInfoPromise(napi_env env, AbilityInfoCB *abilityInfoCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (abilityInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - abilityInfoCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetAbilityInfoExecuteCB, - GetAbilityInfoPromiseCompleteCB, - (void *)abilityInfoCB, - &abilityInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, abilityInfoCB->cbBase.asyncWork)); - HILOG_INFO("%{public}s, promise end.", __func__); - return promise; -} - -/** - * @brief GetAbilityInfo processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param abilityInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityInfoWrap(napi_env env, napi_callback_info info, AbilityInfoCB *abilityInfoCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (abilityInfoCB == nullptr) { - HILOG_ERROR("%{public}s, abilityInfoCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetAbilityInfoAsync(env, args, argcAsync, argcPromise, abilityInfoCB); - } else { - ret = GetAbilityInfoPromise(env, abilityInfoCB); - } - HILOG_INFO("%{public}s, asyncCallback end.", __func__); - return ret; -} - -/** - * @brief Obtains information about the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetAbilityInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType) -{ - HILOG_INFO("%{public}s called.", __func__); - AbilityInfoCB *abilityInfoCB = CreateAbilityInfoCBInfo(env); - if (abilityInfoCB == nullptr) { - return WrapVoidToJS(env); - } - - abilityInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - abilityInfoCB->cbBase.abilityType = abilityType; - napi_value ret = GetAbilityInfoWrap(env, info, abilityInfoCB); - if (ret == nullptr) { - HILOG_ERROR("%{public}s ret == nullptr", __func__); - if (abilityInfoCB != nullptr) { - delete abilityInfoCB; - abilityInfoCB = nullptr; - } - ret = WrapVoidToJS(env); - } - HILOG_INFO("%{public}s end.", __func__); - return ret; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to HapModuleInfoCB on success, nullptr on failure. - */ -HapModuleInfoCB *CreateHapModuleInfoCBInfo(napi_env env) -{ - HILOG_INFO("%{public}s called.", __func__); - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - HapModuleInfoCB *hapModuleInfoCB = new (std::nothrow) HapModuleInfoCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - if (hapModuleInfoCB == nullptr) { - HILOG_ERROR("%{public}s, hapModuleInfoCB == nullptr.", __func__); - } - HILOG_INFO("%{public}s end.", __func__); - return hapModuleInfoCB; -} - -void SaveHapModuleInfo(HapModuleInfo_ &hapModuleInfo, const HapModuleInfo &hapModuleInfoOrg) -{ - HILOG_INFO("%{public}s called.", __func__); - hapModuleInfo.name = hapModuleInfoOrg.name; - hapModuleInfo.description = hapModuleInfoOrg.description; - hapModuleInfo.icon = hapModuleInfoOrg.iconPath; - hapModuleInfo.label = hapModuleInfoOrg.label; - hapModuleInfo.backgroundImg = hapModuleInfoOrg.backgroundImg; - hapModuleInfo.moduleName = hapModuleInfoOrg.moduleName; - hapModuleInfo.supportedModes = hapModuleInfoOrg.supportedModes; - hapModuleInfo.descriptionId = 0; // no data - hapModuleInfo.labelId = 0; // no data - hapModuleInfo.iconId = 0; // no data - hapModuleInfo.mainAbilityName = ""; // no data - hapModuleInfo.installationFree = false; // no data - - for (size_t i = 0; i < hapModuleInfoOrg.reqCapabilities.size(); i++) { - hapModuleInfo.reqCapabilities.emplace_back(hapModuleInfoOrg.reqCapabilities.at(i)); - } - - for (size_t i = 0; i < hapModuleInfoOrg.deviceTypes.size(); i++) { - hapModuleInfo.deviceTypes.emplace_back(hapModuleInfoOrg.deviceTypes.at(i)); - } - - for (size_t i = 0; i < hapModuleInfoOrg.abilityInfos.size(); i++) { - AbilityInfo_ abilityInfo; - SaveAbilityInfo(abilityInfo, hapModuleInfoOrg.abilityInfos.at(i)); - hapModuleInfo.abilityInfos.emplace_back(abilityInfo); - } - HILOG_INFO("%{public}s end.", __func__); -} - -napi_value WrapHapModuleInfo(napi_env env, const HapModuleInfoCB &hapModuleInfoCB) -{ - HILOG_INFO("%{public}s called.", __func__); - napi_value result = nullptr; - napi_value proValue = nullptr; - NAPI_CALL(env, napi_create_object(env, &result)); - NAPI_CALL( - env, napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.name.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "name", proValue)); - - NAPI_CALL(env, - napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.description.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "description", proValue)); - - NAPI_CALL( - env, napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.icon.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "icon", proValue)); - - NAPI_CALL( - env, napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.label.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "label", proValue)); - - NAPI_CALL(env, - napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.backgroundImg.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "backgroundImg", proValue)); - - NAPI_CALL(env, - napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.moduleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "moduleName", proValue)); + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.targetAbility.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "targetAbility", proValue)); - NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.supportedModes, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "supportedModes", proValue)); + NAPI_CALL(env, napi_create_int32(env, abilityInfo.labelId, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "labelId", proValue)); - NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.descriptionId, &proValue)); + NAPI_CALL(env, napi_create_int32(env, abilityInfo.descriptionId, &proValue)); NAPI_CALL(env, napi_set_named_property(env, result, "descriptionId", proValue)); - NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.labelId, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "labelId", proValue)); - - NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.iconId, &proValue)); + NAPI_CALL(env, napi_create_int32(env, abilityInfo.iconId, &proValue)); NAPI_CALL(env, napi_set_named_property(env, result, "iconId", proValue)); - NAPI_CALL(env, - napi_create_string_utf8( - env, hapModuleInfoCB.hapModuleInfo.mainAbilityName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "mainAbilityName", proValue)); - - NAPI_CALL(env, napi_get_boolean(env, hapModuleInfoCB.hapModuleInfo.installationFree, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "installationFree", proValue)); - - napi_value jsArrayreqCapabilities = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArrayreqCapabilities)); - for (size_t i = 0; i < hapModuleInfoCB.hapModuleInfo.reqCapabilities.size(); i++) { - proValue = nullptr; - NAPI_CALL(env, - napi_create_string_utf8( - env, hapModuleInfoCB.hapModuleInfo.reqCapabilities.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_element(env, jsArrayreqCapabilities, i, proValue)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "reqCapabilities", jsArrayreqCapabilities)); - - napi_value jsArraydeviceTypes = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArraydeviceTypes)); - for (size_t i = 0; i < hapModuleInfoCB.hapModuleInfo.deviceTypes.size(); i++) { - proValue = nullptr; - NAPI_CALL(env, - napi_create_string_utf8( - env, hapModuleInfoCB.hapModuleInfo.deviceTypes.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_element(env, jsArraydeviceTypes, i, proValue)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "deviceTypes", jsArraydeviceTypes)); - - napi_value abilityInfos = nullptr; - NAPI_CALL(env, napi_create_array(env, &abilityInfos)); - for (size_t i = 0; i < hapModuleInfoCB.hapModuleInfo.abilityInfos.size(); i++) { - napi_value abilityInfo = nullptr; - abilityInfo = WrapAbilityInfo(env, hapModuleInfoCB.hapModuleInfo.abilityInfos.at(i)); - NAPI_CALL(env, napi_set_element(env, abilityInfos, i, abilityInfo)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "abilityInfos", abilityInfos)); - HILOG_INFO("%{public}s end.", __func__); - return result; -} - -void GetHapModuleInfoExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetHapModuleInfo, worker pool thread execute."); - HapModuleInfoCB *hapModuleInfoCB = (HapModuleInfoCB *)data; - if (hapModuleInfoCB == nullptr) { - HILOG_ERROR("NAPI_GetHapModuleInfo, hapModuleInfoCB == nullptr"); - return; - } - - hapModuleInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - if (hapModuleInfoCB->cbBase.ability == nullptr) { - HILOG_ERROR("NAPI_GetHapModuleInfo, ability == nullptr"); - hapModuleInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; - return; - } - - if (!CheckAbilityType(&hapModuleInfoCB->cbBase)) { - HILOG_ERROR("NAPI_GetHapModuleInfo,wrong ability type"); - hapModuleInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; - return; - } - - std::shared_ptr hapModuleInfoPtr = hapModuleInfoCB->cbBase.ability->GetHapModuleInfo(); - if (hapModuleInfoPtr != nullptr) { - SaveHapModuleInfo(hapModuleInfoCB->hapModuleInfo, *hapModuleInfoPtr); - } else { - hapModuleInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; - } - HILOG_INFO("NAPI_GetHapModuleInfo, worker pool thread execute end."); -} - -void GetHapModuleInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetHapModuleInfo, main event thread complete."); - HapModuleInfoCB *hapModuleInfoCB = (HapModuleInfoCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, hapModuleInfoCB->cbBase.errCode); - if (hapModuleInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result[PARAM1] = WrapHapModuleInfo(env, *hapModuleInfoCB); - } else { - result[PARAM1] = WrapUndefinedToJS(env); - } - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, hapModuleInfoCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - - if (hapModuleInfoCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, hapModuleInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); - delete hapModuleInfoCB; - hapModuleInfoCB = nullptr; - HILOG_INFO("NAPI_GetHapModuleInfo, main event thread complete end."); -} - -void GetHapModuleInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetHapModuleInfo, main event thread complete."); - HapModuleInfoCB *hapModuleInfoCB = (HapModuleInfoCB *)data; - napi_value result = nullptr; - if (hapModuleInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result = WrapHapModuleInfo(env, *hapModuleInfoCB); - napi_resolve_deferred(env, hapModuleInfoCB->cbBase.deferred, result); - } else { - result = GetCallbackErrorValue(env, hapModuleInfoCB->cbBase.errCode); - napi_reject_deferred(env, hapModuleInfoCB->cbBase.deferred, result); - } - - napi_delete_async_work(env, hapModuleInfoCB->cbBase.asyncWork); - delete hapModuleInfoCB; - hapModuleInfoCB = nullptr; - HILOG_INFO("NAPI_GetHapModuleInfo, main event thread complete end."); -} - -/** - * @brief GetHapModuleInfo Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param hapModuleInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetHapModuleInfoAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, HapModuleInfoCB *hapModuleInfoCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || hapModuleInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &hapModuleInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetHapModuleInfoExecuteCB, - GetHapModuleInfoAsyncCompleteCB, - (void *)hapModuleInfoCB, - &hapModuleInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - HILOG_INFO("%{public}s, asyncCallback end.", __func__); - return result; -} - -/** - * @brief GetHapModuleInfo Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param hapModuleInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetHapModuleInfoPromise(napi_env env, HapModuleInfoCB *hapModuleInfoCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (hapModuleInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - hapModuleInfoCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetHapModuleInfoExecuteCB, - GetHapModuleInfoPromiseCompleteCB, - (void *)hapModuleInfoCB, - &hapModuleInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); - HILOG_INFO("%{public}s, promise end.", __func__); - return promise; -} - -napi_value GetHapModuleInfoWrap(napi_env env, napi_callback_info info, HapModuleInfoCB *hapModuleInfoCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (hapModuleInfoCB == nullptr) { - HILOG_ERROR("%{public}s, hapModuleInfoCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; + NAPI_CALL(env, napi_create_int32(env, abilityInfo.formEntity, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "formEntity", proValue)); - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } + NAPI_CALL(env, napi_create_int32(env, abilityInfo.minFormHeight, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "minFormHeight", proValue)); - if (argcAsync > argcPromise) { - ret = GetHapModuleInfoAsync(env, args, argcAsync, argcPromise, hapModuleInfoCB); - } else { - ret = GetHapModuleInfoPromise(env, hapModuleInfoCB); - } - HILOG_INFO("%{public}s, asyncCallback end.", __func__); - return ret; -} + NAPI_CALL(env, napi_create_int32(env, abilityInfo.defaultFormHeight, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "defaultFormHeight", proValue)); -/** - * @brief Obtains the HapModuleInfo object of the application. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetHapModuleInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType) -{ - HILOG_INFO("%{public}s called.", __func__); - HapModuleInfoCB *hapModuleInfoCB = CreateHapModuleInfoCBInfo(env); - if (hapModuleInfoCB == nullptr) { - return WrapVoidToJS(env); - } + NAPI_CALL(env, napi_create_int32(env, abilityInfo.minFormWidth, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "minFormWidth", proValue)); - hapModuleInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - hapModuleInfoCB->cbBase.abilityType = abilityType; - napi_value ret = GetHapModuleInfoWrap(env, info, hapModuleInfoCB); - if (ret == nullptr) { - HILOG_ERROR("%{public}s ret == nullptr", __func__); - if (hapModuleInfoCB != nullptr) { - delete hapModuleInfoCB; - hapModuleInfoCB = nullptr; - } - ret = WrapVoidToJS(env); - } - HILOG_INFO("%{public}s end.", __func__); - return ret; -} + NAPI_CALL(env, napi_create_int32(env, abilityInfo.defaultFormWidth, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "defaultFormWidth", proValue)); -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to ProcessNameCB on success, nullptr on failure. - */ -ProcessNameCB *CreateProcessNameCBInfo(napi_env env) -{ - HILOG_INFO("%{public}s called.", __func__); - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); + NAPI_CALL(env, napi_create_int32(env, abilityInfo.backgroundModes, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "backgroundModes", proValue)); - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + NAPI_CALL(env, napi_create_int32(env, abilityInfo.subType, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "subType", proValue)); - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + NAPI_CALL(env, napi_get_boolean(env, abilityInfo.isVisible, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "isVisible", proValue)); + + NAPI_CALL(env, napi_get_boolean(env, abilityInfo.formEnabled, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "formEnabled", proValue)); + + napi_value jsArrayPermissions = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArrayPermissions)); + for (size_t i = 0; i < abilityInfo.permissions.size(); i++) { + proValue = nullptr; + NAPI_CALL( + env, napi_create_string_utf8(env, abilityInfo.permissions.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_element(env, jsArrayPermissions, i, proValue)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "permissions", jsArrayPermissions)); + + napi_value jsArrayDeviceCapabilities = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArrayDeviceCapabilities)); + for (size_t i = 0; i < abilityInfo.deviceCapabilities.size(); i++) { + proValue = nullptr; + NAPI_CALL(env, + napi_create_string_utf8(env, abilityInfo.deviceCapabilities.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_element(env, jsArrayDeviceCapabilities, i, proValue)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "deviceCapabilities", jsArrayDeviceCapabilities)); - ProcessNameCB *processNameCB = new (std::nothrow) ProcessNameCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - if (processNameCB == nullptr) { - HILOG_ERROR("%{public}s, processNameCB == nullptr.", __func__); + napi_value jsArrayDeviceTypes = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArrayDeviceTypes)); + for (size_t i = 0; i < abilityInfo.deviceTypes.size(); i++) { + proValue = nullptr; + NAPI_CALL( + env, napi_create_string_utf8(env, abilityInfo.deviceTypes.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_element(env, jsArrayDeviceTypes, i, proValue)); } + NAPI_CALL(env, napi_set_named_property(env, result, "deviceTypes", jsArrayDeviceTypes)); + + napi_value appInfo = nullptr; + appInfo = WrapAppInfo(env, abilityInfo.appInfo); + NAPI_CALL(env, napi_set_named_property(env, result, "applicationInfo", appInfo)); HILOG_INFO("%{public}s end.", __func__); - return processNameCB; + return result; } /** - * @brief GetProcessName asynchronous processing function. + * @brief GetAbilityInfo asynchronous processing function. * * @param env The environment that the Node-API call is invoked under. * @param data Point to asynchronous processing of data. */ -void GetProcessNameExecuteCB(napi_env env, void *data) +void GetAbilityInfoExecuteCB(napi_env env, void *data) { - HILOG_INFO("NAPI_GetProcessName, worker pool thread execute."); - ProcessNameCB *processNameCB = (ProcessNameCB *)data; - if (processNameCB == nullptr) { - HILOG_ERROR("NAPI_GetProcessName, processNameCB == nullptr"); + HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); + AbilityInfoCB *abilityInfoCB = (AbilityInfoCB *)data; + if (abilityInfoCB == nullptr) { + HILOG_ERROR("NAPI_GetApplicationInfo, abilityInfoCB == nullptr"); return; } - processNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - if (processNameCB->cbBase.ability == nullptr) { - HILOG_ERROR("NAPI_GetProcessName, ability == nullptr"); - processNameCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + abilityInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (abilityInfoCB->cbBase.ability == nullptr) { + HILOG_ERROR("NAPI_GetApplicationInfo, ability == nullptr"); + abilityInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; return; } - if (!CheckAbilityType(&processNameCB->cbBase)) { - HILOG_ERROR("NAPI_GetProcessName,wrong ability type"); - processNameCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + if (!CheckAbilityType(&abilityInfoCB->cbBase)) { + HILOG_ERROR("NAPI_GetApplicationInfo,wrong ability type"); + abilityInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; return; } - processNameCB->processName = processNameCB->cbBase.ability->GetProcessName(); - HILOG_INFO("NAPI_GetProcessName, worker pool thread execute end."); -} - -napi_value WrapProcessName(napi_env env, ProcessNameCB *processNameCB) -{ - HILOG_INFO("%{public}s, called.", __func__); - if (processNameCB == nullptr) { - HILOG_ERROR("%{public}s, Invalid param(processNameCB == nullptr)", __func__); - return nullptr; + std::shared_ptr abilityInfoPtr = abilityInfoCB->cbBase.ability->GetAbilityInfo(); + if (abilityInfoPtr != nullptr) { + SaveAbilityInfo(abilityInfoCB->abilityInfo, *abilityInfoPtr); + } else { + abilityInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; } - napi_value result = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, processNameCB->processName.c_str(), NAPI_AUTO_LENGTH, &result)); - HILOG_INFO("%{public}s, end.", __func__); - return result; + HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute end."); } /** @@ -2406,31 +884,31 @@ napi_value WrapProcessName(napi_env env, ProcessNameCB *processNameCB) * @param env The environment that the Node-API call is invoked under. * @param data Point to asynchronous processing of data. */ -void GetProcessNameAsyncCompleteCB(napi_env env, napi_status status, void *data) +void GetAbilityInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) { - HILOG_INFO("NAPI_GetProcessName, main event thread complete."); - ProcessNameCB *processNameCB = (ProcessNameCB *)data; + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); + AbilityInfoCB *abilityInfoCB = (AbilityInfoCB *)data; napi_value callback = nullptr; napi_value undefined = nullptr; napi_value result[ARGS_TWO] = {nullptr}; napi_value callResult = nullptr; NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, processNameCB->cbBase.errCode); - if (processNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result[PARAM1] = WrapProcessName(env, processNameCB); + result[PARAM0] = GetCallbackErrorValue(env, abilityInfoCB->cbBase.errCode); + if (abilityInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapAbilityInfo(env, abilityInfoCB->abilityInfo); } else { result[PARAM1] = WrapUndefinedToJS(env); } - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, processNameCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, abilityInfoCB->cbBase.cbInfo.callback, &callback)); NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - if (processNameCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, processNameCB->cbBase.cbInfo.callback)); + if (abilityInfoCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, abilityInfoCB->cbBase.cbInfo.callback)); } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, processNameCB->cbBase.asyncWork)); - delete processNameCB; - processNameCB = nullptr; - HILOG_INFO("NAPI_GetProcessName, main event thread complete end."); + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, abilityInfoCB->cbBase.asyncWork)); + delete abilityInfoCB; + abilityInfoCB = nullptr; + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete end."); } /** @@ -2439,40 +917,39 @@ void GetProcessNameAsyncCompleteCB(napi_env env, napi_status status, void *data) * @param env The environment that the Node-API call is invoked under. * @param data Point to asynchronous processing of data. */ -void GetProcessNamePromiseCompleteCB(napi_env env, napi_status status, void *data) +void GetAbilityInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) { - HILOG_INFO("NAPI_GetProcessName, main event thread complete."); - ProcessNameCB *processNameCB = (ProcessNameCB *)data; + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); + AbilityInfoCB *abilityInfoCB = (AbilityInfoCB *)data; napi_value result = nullptr; - if (processNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result = WrapProcessName(env, processNameCB); - napi_resolve_deferred(env, processNameCB->cbBase.deferred, result); + if (abilityInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapAbilityInfo(env, abilityInfoCB->abilityInfo); + napi_resolve_deferred(env, abilityInfoCB->cbBase.deferred, result); } else { - result = GetCallbackErrorValue(env, processNameCB->cbBase.errCode); - napi_reject_deferred(env, processNameCB->cbBase.deferred, result); + result = GetCallbackErrorValue(env, abilityInfoCB->cbBase.errCode); + napi_reject_deferred(env, abilityInfoCB->cbBase.deferred, result); } - napi_delete_async_work(env, processNameCB->cbBase.asyncWork); - delete processNameCB; - processNameCB = nullptr; - HILOG_INFO("NAPI_GetProcessName, main event thread complete end."); + napi_delete_async_work(env, abilityInfoCB->cbBase.asyncWork); + delete abilityInfoCB; + abilityInfoCB = nullptr; + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete end."); } /** - * @brief GetProcessName Async. + * @brief GetAbilityInfo Async. * * @param env The environment that the Node-API call is invoked under. * @param args Indicates the arguments passed into the callback. * @param argcPromise Asynchronous data processing. - * @param ProcessNameCB Process data asynchronously. + * @param abilityInfoCB Process data asynchronously. * * @return Return JS data successfully, otherwise return nullptr. */ -napi_value GetProcessNameAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ProcessNameCB *processNameCB) +napi_value GetAbilityInfoAsync(napi_env env, napi_value *args, const size_t argCallback, AbilityInfoCB *abilityInfoCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || processNameCB == nullptr) { + if (args == nullptr || abilityInfoCB == nullptr) { HILOG_ERROR("%{public}s, param == nullptr.", __func__); return nullptr; } @@ -2480,19 +957,19 @@ napi_value GetProcessNameAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &processNameCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &abilityInfoCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, - GetProcessNameExecuteCB, - GetProcessNameAsyncCompleteCB, - (void *)processNameCB, - &processNameCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, processNameCB->cbBase.asyncWork)); + GetAbilityInfoExecuteCB, + GetAbilityInfoAsyncCompleteCB, + (void *)abilityInfoCB, + &abilityInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, abilityInfoCB->cbBase.asyncWork)); napi_value result = nullptr; NAPI_CALL(env, napi_get_null(env, &result)); HILOG_INFO("%{public}s, asyncCallback end.", __func__); @@ -2500,17 +977,17 @@ napi_value GetProcessNameAsync( } /** - * @brief GetProcessName Promise. + * @brief GetAbilityInfo Promise. * * @param env The environment that the Node-API call is invoked under. - * @param ProcessNameCB Process data asynchronously. + * @param abilityInfoCB Process data asynchronously. * * @return Return JS data successfully, otherwise return nullptr. */ -napi_value GetProcessNamePromise(napi_env env, ProcessNameCB *processNameCB) +napi_value GetAbilityInfoPromise(napi_env env, AbilityInfoCB *abilityInfoCB) { HILOG_INFO("%{public}s, promise.", __func__); - if (processNameCB == nullptr) { + if (abilityInfoCB == nullptr) { HILOG_ERROR("%{public}s, param == nullptr.", __func__); return nullptr; } @@ -2519,34 +996,34 @@ napi_value GetProcessNamePromise(napi_env env, ProcessNameCB *processNameCB) napi_deferred deferred; napi_value promise = nullptr; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - processNameCB->cbBase.deferred = deferred; + abilityInfoCB->cbBase.deferred = deferred; NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, - GetProcessNameExecuteCB, - GetProcessNamePromiseCompleteCB, - (void *)processNameCB, - &processNameCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, processNameCB->cbBase.asyncWork)); + GetAbilityInfoExecuteCB, + GetAbilityInfoPromiseCompleteCB, + (void *)abilityInfoCB, + &abilityInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, abilityInfoCB->cbBase.asyncWork)); HILOG_INFO("%{public}s, promise end.", __func__); return promise; } /** - * @brief GetProcessName processing function. + * @brief GetAbilityInfo processing function. * * @param env The environment that the Node-API call is invoked under. - * @param ProcessNameCB Process data asynchronously. + * @param abilityInfoCB Process data asynchronously. * * @return Return JS data successfully, otherwise return nullptr. */ -napi_value GetProcessNameWrap(napi_env env, napi_callback_info info, ProcessNameCB *processNameCB) +napi_value GetAbilityInfoWrap(napi_env env, napi_callback_info info, AbilityInfoCB *abilityInfoCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (processNameCB == nullptr) { - HILOG_ERROR("%{public}s, processNameCB == nullptr.", __func__); + if (abilityInfoCB == nullptr) { + HILOG_ERROR("%{public}s, abilityInfoCB == nullptr.", __func__); return nullptr; } @@ -2563,38 +1040,38 @@ napi_value GetProcessNameWrap(napi_env env, napi_callback_info info, ProcessName } if (argcAsync > argcPromise) { - ret = GetProcessNameAsync(env, args, argcAsync, argcPromise, processNameCB); + ret = GetAbilityInfoAsync(env, args, 0, abilityInfoCB); } else { - ret = GetProcessNamePromise(env, processNameCB); + ret = GetAbilityInfoPromise(env, abilityInfoCB); } HILOG_INFO("%{public}s, asyncCallback end.", __func__); return ret; } /** - * @brief Obtains the name of the current process. + * @brief Obtains information about the current ability. * * @param env The environment that the Node-API call is invoked under. * @param info The callback info passed into the callback function. * * @return The return value from NAPI C++ to JS for the module. */ -napi_value NAPI_GetProcessNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +napi_value NAPI_GetAbilityInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType) { HILOG_INFO("%{public}s called.", __func__); - ProcessNameCB *processNameCB = CreateProcessNameCBInfo(env); - if (processNameCB == nullptr) { + AbilityInfoCB *abilityInfoCB = CreateAbilityInfoCBInfo(env); + if (abilityInfoCB == nullptr) { return WrapVoidToJS(env); } - processNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - processNameCB->cbBase.abilityType = abilityType; - napi_value ret = GetProcessNameWrap(env, info, processNameCB); + abilityInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + abilityInfoCB->cbBase.abilityType = abilityType; + napi_value ret = GetAbilityInfoWrap(env, info, abilityInfoCB); if (ret == nullptr) { - HILOG_ERROR("%{public}s ret == nullptr.", __func__); - if (processNameCB != nullptr) { - delete processNameCB; - processNameCB = nullptr; + HILOG_ERROR("%{public}s ret == nullptr", __func__); + if (abilityInfoCB != nullptr) { + delete abilityInfoCB; + abilityInfoCB = nullptr; } ret = WrapVoidToJS(env); } @@ -2607,150 +1084,242 @@ napi_value NAPI_GetProcessNameCommon(napi_env env, napi_callback_info info, Abil * * @param env The environment that the Node-API call is invoked under. * - * @return Return a pointer to CallingBundleCB on success, nullptr on failure. + * @return Return a pointer to HapModuleInfoCB on success, nullptr on failure. */ -CallingBundleCB *CreateCallingBundleCBInfo(napi_env env) +HapModuleInfoCB *CreateHapModuleInfoCBInfo(napi_env env) { HILOG_INFO("%{public}s called.", __func__); napi_value global = nullptr; NAPI_CALL(env, napi_get_global(env, &global)); - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + HapModuleInfoCB *hapModuleInfoCB = new (std::nothrow) HapModuleInfoCB; + if (hapModuleInfoCB == nullptr) { + HILOG_ERROR("%{public}s, hapModuleInfoCB == nullptr.", __func__); + return nullptr; + } + hapModuleInfoCB->cbBase.cbInfo.env = env; + hapModuleInfoCB->cbBase.asyncWork = nullptr; + hapModuleInfoCB->cbBase.deferred = nullptr; + hapModuleInfoCB->cbBase.ability = ability; + + HILOG_INFO("%{public}s end.", __func__); + return hapModuleInfoCB; +} + +void SaveHapModuleInfo(HapModuleInfo_ &hapModuleInfo, const HapModuleInfo &hapModuleInfoOrg) +{ + HILOG_INFO("%{public}s called.", __func__); + hapModuleInfo.name = hapModuleInfoOrg.name; + hapModuleInfo.description = hapModuleInfoOrg.description; + hapModuleInfo.icon = hapModuleInfoOrg.iconPath; + hapModuleInfo.label = hapModuleInfoOrg.label; + hapModuleInfo.backgroundImg = hapModuleInfoOrg.backgroundImg; + hapModuleInfo.moduleName = hapModuleInfoOrg.moduleName; + hapModuleInfo.supportedModes = hapModuleInfoOrg.supportedModes; + hapModuleInfo.descriptionId = 0; // no data + hapModuleInfo.labelId = 0; // no data + hapModuleInfo.iconId = 0; // no data + hapModuleInfo.mainAbilityName = ""; // no data + hapModuleInfo.installationFree = false; // no data + + for (size_t i = 0; i < hapModuleInfoOrg.reqCapabilities.size(); i++) { + hapModuleInfo.reqCapabilities.emplace_back(hapModuleInfoOrg.reqCapabilities.at(i)); + } + + for (size_t i = 0; i < hapModuleInfoOrg.deviceTypes.size(); i++) { + hapModuleInfo.deviceTypes.emplace_back(hapModuleInfoOrg.deviceTypes.at(i)); + } + + for (size_t i = 0; i < hapModuleInfoOrg.abilityInfos.size(); i++) { + AbilityInfo_ abilityInfo; + SaveAbilityInfo(abilityInfo, hapModuleInfoOrg.abilityInfos.at(i)); + hapModuleInfo.abilityInfos.emplace_back(abilityInfo); + } + HILOG_INFO("%{public}s end.", __func__); +} + +napi_value WrapHapModuleInfo(napi_env env, const HapModuleInfoCB &hapModuleInfoCB) +{ + HILOG_INFO("%{public}s called.", __func__); + napi_value result = nullptr; + napi_value proValue = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + NAPI_CALL( + env, napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.name.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "name", proValue)); + + NAPI_CALL(env, + napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.description.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "description", proValue)); + + NAPI_CALL( + env, napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.icon.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "icon", proValue)); + + NAPI_CALL( + env, napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.label.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "label", proValue)); + + NAPI_CALL(env, + napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.backgroundImg.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "backgroundImg", proValue)); + + NAPI_CALL(env, + napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.moduleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "moduleName", proValue)); + + NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.supportedModes, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "supportedModes", proValue)); + + NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.descriptionId, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "descriptionId", proValue)); + + NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.labelId, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "labelId", proValue)); + + NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.iconId, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "iconId", proValue)); + + NAPI_CALL(env, + napi_create_string_utf8( + env, hapModuleInfoCB.hapModuleInfo.mainAbilityName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "mainAbilityName", proValue)); + + NAPI_CALL(env, napi_get_boolean(env, hapModuleInfoCB.hapModuleInfo.installationFree, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "installationFree", proValue)); - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + napi_value jsArrayreqCapabilities = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArrayreqCapabilities)); + for (size_t i = 0; i < hapModuleInfoCB.hapModuleInfo.reqCapabilities.size(); i++) { + proValue = nullptr; + NAPI_CALL(env, + napi_create_string_utf8( + env, hapModuleInfoCB.hapModuleInfo.reqCapabilities.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_element(env, jsArrayreqCapabilities, i, proValue)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "reqCapabilities", jsArrayreqCapabilities)); + + napi_value jsArraydeviceTypes = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArraydeviceTypes)); + for (size_t i = 0; i < hapModuleInfoCB.hapModuleInfo.deviceTypes.size(); i++) { + proValue = nullptr; + NAPI_CALL(env, + napi_create_string_utf8( + env, hapModuleInfoCB.hapModuleInfo.deviceTypes.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_element(env, jsArraydeviceTypes, i, proValue)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "deviceTypes", jsArraydeviceTypes)); - CallingBundleCB *callingBundleCB = new (std::nothrow) CallingBundleCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - if (callingBundleCB == nullptr) { - HILOG_ERROR("%{public}s, callingBundleCB == nullptr.", __func__); + napi_value abilityInfos = nullptr; + NAPI_CALL(env, napi_create_array(env, &abilityInfos)); + for (size_t i = 0; i < hapModuleInfoCB.hapModuleInfo.abilityInfos.size(); i++) { + napi_value abilityInfo = nullptr; + abilityInfo = WrapAbilityInfo(env, hapModuleInfoCB.hapModuleInfo.abilityInfos.at(i)); + NAPI_CALL(env, napi_set_element(env, abilityInfos, i, abilityInfo)); } + NAPI_CALL(env, napi_set_named_property(env, result, "abilityInfos", abilityInfos)); HILOG_INFO("%{public}s end.", __func__); - return callingBundleCB; + return result; } -/** - * @brief GetCallingBundle asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetCallingBundleExecuteCB(napi_env env, void *data) +void GetHapModuleInfoExecuteCB(napi_env env, void *data) { - HILOG_INFO("NAPI_GetCallingBundle, worker pool thread execute."); - CallingBundleCB *callingBundleCB = (CallingBundleCB *)data; - if (callingBundleCB == nullptr) { - HILOG_ERROR("NAPI_GetCallingBundle, callingBundleCB == nullptr"); + HILOG_INFO("NAPI_GetHapModuleInfo, worker pool thread execute."); + HapModuleInfoCB *hapModuleInfoCB = (HapModuleInfoCB *)data; + if (hapModuleInfoCB == nullptr) { + HILOG_ERROR("NAPI_GetHapModuleInfo, hapModuleInfoCB == nullptr"); return; } - callingBundleCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - if (callingBundleCB->cbBase.ability == nullptr) { - HILOG_ERROR("NAPI_GetCallingBundle, ability == nullptr"); - callingBundleCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + hapModuleInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (hapModuleInfoCB->cbBase.ability == nullptr) { + HILOG_ERROR("NAPI_GetHapModuleInfo, ability == nullptr"); + hapModuleInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; return; } - if (!CheckAbilityType(&callingBundleCB->cbBase)) { - HILOG_ERROR("NAPI_GetCallingBundle,wrong ability type"); - callingBundleCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + if (!CheckAbilityType(&hapModuleInfoCB->cbBase)) { + HILOG_ERROR("NAPI_GetHapModuleInfo,wrong ability type"); + hapModuleInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; return; } - callingBundleCB->callingBundleName = callingBundleCB->cbBase.ability->GetCallingBundle(); - HILOG_INFO("NAPI_GetCallingBundle, worker pool thread execute end."); -} -napi_value WrapCallingBundle(napi_env env, CallingBundleCB *callingBundleCB) -{ - HILOG_INFO("%{public}s, called.", __func__); - if (callingBundleCB == nullptr) { - HILOG_ERROR("%{public}s,Invalid param(callingBundleCB == nullptr)", __func__); - return nullptr; + std::shared_ptr hapModuleInfoPtr = hapModuleInfoCB->cbBase.ability->GetHapModuleInfo(); + if (hapModuleInfoPtr != nullptr) { + SaveHapModuleInfo(hapModuleInfoCB->hapModuleInfo, *hapModuleInfoPtr); + } else { + hapModuleInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; } - napi_value result = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, callingBundleCB->callingBundleName.c_str(), NAPI_AUTO_LENGTH, &result)); - HILOG_INFO("%{public}s, end.", __func__); - return result; + HILOG_INFO("NAPI_GetHapModuleInfo, worker pool thread execute end."); } -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetCallingBundleAsyncCompleteCB(napi_env env, napi_status status, void *data) +void GetHapModuleInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) { - HILOG_INFO("NAPI_GetCallingBundle, main event thread complete."); - CallingBundleCB *callingBundleCB = (CallingBundleCB *)data; + HILOG_INFO("NAPI_GetHapModuleInfo, main event thread complete."); + HapModuleInfoCB *hapModuleInfoCB = (HapModuleInfoCB *)data; napi_value callback = nullptr; napi_value undefined = nullptr; napi_value result[ARGS_TWO] = {nullptr}; napi_value callResult = nullptr; NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, callingBundleCB->cbBase.errCode); - if (callingBundleCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result[PARAM1] = WrapCallingBundle(env, callingBundleCB); + result[PARAM0] = GetCallbackErrorValue(env, hapModuleInfoCB->cbBase.errCode); + if (hapModuleInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapHapModuleInfo(env, *hapModuleInfoCB); } else { result[PARAM1] = WrapUndefinedToJS(env); } - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, callingBundleCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, hapModuleInfoCB->cbBase.cbInfo.callback, &callback)); NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - if (callingBundleCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, callingBundleCB->cbBase.cbInfo.callback)); + if (hapModuleInfoCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, hapModuleInfoCB->cbBase.cbInfo.callback)); } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, callingBundleCB->cbBase.asyncWork)); - delete callingBundleCB; - callingBundleCB = nullptr; - HILOG_INFO("NAPI_GetCallingBundle, main event thread complete end."); + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); + delete hapModuleInfoCB; + hapModuleInfoCB = nullptr; + HILOG_INFO("NAPI_GetHapModuleInfo, main event thread complete end."); } -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetCallingBundlePromiseCompleteCB(napi_env env, napi_status status, void *data) +void GetHapModuleInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) { - HILOG_INFO("NAPI_GetCallingBundle, main event thread complete."); - CallingBundleCB *callingBundleCB = (CallingBundleCB *)data; + HILOG_INFO("NAPI_GetHapModuleInfo, main event thread complete."); + HapModuleInfoCB *hapModuleInfoCB = (HapModuleInfoCB *)data; napi_value result = nullptr; - if (callingBundleCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { - result = WrapCallingBundle(env, callingBundleCB); - napi_resolve_deferred(env, callingBundleCB->cbBase.deferred, result); + if (hapModuleInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapHapModuleInfo(env, *hapModuleInfoCB); + napi_resolve_deferred(env, hapModuleInfoCB->cbBase.deferred, result); } else { - result = GetCallbackErrorValue(env, callingBundleCB->cbBase.errCode); - napi_reject_deferred(env, callingBundleCB->cbBase.deferred, result); + result = GetCallbackErrorValue(env, hapModuleInfoCB->cbBase.errCode); + napi_reject_deferred(env, hapModuleInfoCB->cbBase.deferred, result); } - napi_delete_async_work(env, callingBundleCB->cbBase.asyncWork); - delete callingBundleCB; - callingBundleCB = nullptr; - HILOG_INFO("NAPI_GetCallingBundle, main event thread complete end."); + napi_delete_async_work(env, hapModuleInfoCB->cbBase.asyncWork); + delete hapModuleInfoCB; + hapModuleInfoCB = nullptr; + HILOG_INFO("NAPI_GetHapModuleInfo, main event thread complete end."); } /** - * @brief GetCallingBundle Async. + * @brief GetHapModuleInfo Async. * * @param env The environment that the Node-API call is invoked under. * @param args Indicates the arguments passed into the callback. * @param argcPromise Asynchronous data processing. - * @param CallingBundleCB Process data asynchronously. + * @param hapModuleInfoCB Process data asynchronously. * * @return Return JS data successfully, otherwise return nullptr. */ -napi_value GetCallingBundleAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, CallingBundleCB *callingBundleCB) +napi_value GetHapModuleInfoAsync( + napi_env env, napi_value *args, const size_t argCallback, HapModuleInfoCB *hapModuleInfoCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || callingBundleCB == nullptr) { + if (args == nullptr || hapModuleInfoCB == nullptr) { HILOG_ERROR("%{public}s, param == nullptr.", __func__); return nullptr; } @@ -2758,19 +1327,19 @@ napi_value GetCallingBundleAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &callingBundleCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &hapModuleInfoCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, - GetCallingBundleExecuteCB, - GetCallingBundleAsyncCompleteCB, - (void *)callingBundleCB, - &callingBundleCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, callingBundleCB->cbBase.asyncWork)); + GetHapModuleInfoExecuteCB, + GetHapModuleInfoAsyncCompleteCB, + (void *)hapModuleInfoCB, + &hapModuleInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); napi_value result = nullptr; NAPI_CALL(env, napi_get_null(env, &result)); HILOG_INFO("%{public}s, asyncCallback end.", __func__); @@ -2778,17 +1347,17 @@ napi_value GetCallingBundleAsync( } /** - * @brief GetCallingBundle Promise. + * @brief GetHapModuleInfo Promise. * * @param env The environment that the Node-API call is invoked under. - * @param CallingBundleCB Process data asynchronously. + * @param hapModuleInfoCB Process data asynchronously. * * @return Return JS data successfully, otherwise return nullptr. */ -napi_value GetCallingBundlePromise(napi_env env, CallingBundleCB *callingBundleCB) +napi_value GetHapModuleInfoPromise(napi_env env, HapModuleInfoCB *hapModuleInfoCB) { HILOG_INFO("%{public}s, promise.", __func__); - if (callingBundleCB == nullptr) { + if (hapModuleInfoCB == nullptr) { HILOG_ERROR("%{public}s, param == nullptr.", __func__); return nullptr; } @@ -2797,34 +1366,26 @@ napi_value GetCallingBundlePromise(napi_env env, CallingBundleCB *callingBundleC napi_deferred deferred; napi_value promise = nullptr; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - callingBundleCB->cbBase.deferred = deferred; + hapModuleInfoCB->cbBase.deferred = deferred; NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, - GetCallingBundleExecuteCB, - GetCallingBundlePromiseCompleteCB, - (void *)callingBundleCB, - &callingBundleCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, callingBundleCB->cbBase.asyncWork)); + GetHapModuleInfoExecuteCB, + GetHapModuleInfoPromiseCompleteCB, + (void *)hapModuleInfoCB, + &hapModuleInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); HILOG_INFO("%{public}s, promise end.", __func__); return promise; } -/** - * @brief GetCallingBundle processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param CallingBundleCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetCallingBundleWrap(napi_env env, napi_callback_info info, CallingBundleCB *callingBundleCB) +napi_value GetHapModuleInfoWrap(napi_env env, napi_callback_info info, HapModuleInfoCB *hapModuleInfoCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (callingBundleCB == nullptr) { - HILOG_ERROR("%{public}s, callingBundleCB == nullptr.", __func__); + if (hapModuleInfoCB == nullptr) { + HILOG_ERROR("%{public}s, hapModuleInfoCB == nullptr.", __func__); return nullptr; } @@ -2841,38 +1402,38 @@ napi_value GetCallingBundleWrap(napi_env env, napi_callback_info info, CallingBu } if (argcAsync > argcPromise) { - ret = GetCallingBundleAsync(env, args, argcAsync, argcPromise, callingBundleCB); + ret = GetHapModuleInfoAsync(env, args, 0, hapModuleInfoCB); } else { - ret = GetCallingBundlePromise(env, callingBundleCB); + ret = GetHapModuleInfoPromise(env, hapModuleInfoCB); } HILOG_INFO("%{public}s, asyncCallback end.", __func__); return ret; } /** - * @brief Obtains the bundle name of the ability that called the current ability. + * @brief Obtains the HapModuleInfo object of the application. * * @param env The environment that the Node-API call is invoked under. * @param info The callback info passed into the callback function. * * @return The return value from NAPI C++ to JS for the module. */ -napi_value NAPI_GetCallingBundleCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +napi_value NAPI_GetHapModuleInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType) { HILOG_INFO("%{public}s called.", __func__); - CallingBundleCB *calloingBundleCB = CreateCallingBundleCBInfo(env); - if (calloingBundleCB == nullptr) { + HapModuleInfoCB *hapModuleInfoCB = CreateHapModuleInfoCBInfo(env); + if (hapModuleInfoCB == nullptr) { return WrapVoidToJS(env); } - calloingBundleCB->cbBase.errCode = NAPI_ERR_NO_ERROR; - calloingBundleCB->cbBase.abilityType = abilityType; - napi_value ret = GetCallingBundleWrap(env, info, calloingBundleCB); + hapModuleInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + hapModuleInfoCB->cbBase.abilityType = abilityType; + napi_value ret = GetHapModuleInfoWrap(env, info, hapModuleInfoCB); if (ret == nullptr) { HILOG_ERROR("%{public}s ret == nullptr", __func__); - if (calloingBundleCB != nullptr) { - delete calloingBundleCB; - calloingBundleCB = nullptr; + if (hapModuleInfoCB != nullptr) { + delete hapModuleInfoCB; + hapModuleInfoCB = nullptr; } ret = WrapVoidToJS(env); } @@ -2913,18 +1474,19 @@ AsyncCallbackInfo *CreateAsyncCallbackInfo(napi_env env) HILOG_ERROR("%{public}s get_value_external=%{public}d err:%{public}s", __func__, ret, errorInfo->error_message); } - AsyncCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo{ - .cbInfo.env = env, - .asyncWork = nullptr, - .deferred = nullptr, - .ability = ability, - .native_result = false, - .errCode = NAPI_ERR_NO_ERROR, - .abilityType = AbilityType::UNKNOWN, - }; + AsyncCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo; if (asyncCallbackInfo == nullptr) { HILOG_ERROR("%{public}s asyncCallbackInfo == nullptr", __func__); + return nullptr; } + asyncCallbackInfo->cbInfo.env = env; + asyncCallbackInfo->asyncWork = nullptr; + asyncCallbackInfo->deferred = nullptr; + asyncCallbackInfo->ability = ability; + asyncCallbackInfo->native_result = false; + asyncCallbackInfo->errCode = NAPI_ERR_NO_ERROR; + asyncCallbackInfo->abilityType = AbilityType::UNKNOWN; + HILOG_INFO("%{public}s end.", __func__); return asyncCallbackInfo; } @@ -2953,7 +1515,7 @@ void GetContextAsyncExecuteCB(napi_env env, void *data) } napi_value GetContextAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) + napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || asyncCallbackInfo == nullptr) { @@ -2964,9 +1526,9 @@ napi_value GetContextAsync( napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); napi_valuetype valuetype = napi_undefined; - napi_typeof(env, args[argcPromise], &valuetype); + napi_typeof(env, args[argCallback], &valuetype); if (valuetype == napi_function) { - napi_create_reference(env, args[argcPromise], 1, &asyncCallbackInfo->cbInfo.callback); + napi_create_reference(env, args[argCallback], 1, &asyncCallbackInfo->cbInfo.callback); } napi_create_async_work( env, @@ -3143,8 +1705,7 @@ void GetWantExecuteCB(napi_env env, void *data) HILOG_INFO("%{public}s, end.", __func__); } -napi_value GetWantAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) +napi_value GetWantAsync(napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || asyncCallbackInfo == nullptr) { @@ -3155,9 +1716,9 @@ napi_value GetWantAsync( napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); napi_valuetype valuetype = napi_undefined; - napi_typeof(env, args[argcPromise], &valuetype); + napi_typeof(env, args[argCallback], &valuetype); if (valuetype == napi_function) { - napi_create_reference(env, args[argcPromise], 1, &asyncCallbackInfo->cbInfo.callback); + napi_create_reference(env, args[argCallback], 1, &asyncCallbackInfo->cbInfo.callback); } napi_create_async_work( env, @@ -3270,7 +1831,7 @@ napi_value GetWantWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo } if (argcAsync > argcPromise) { - ret = GetWantAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); + ret = GetWantAsync(env, args, 0, asyncCallbackInfo); } else { ret = GetWantPromise(env, asyncCallbackInfo); } @@ -3329,15 +1890,16 @@ AbilityNameCB *CreateAbilityNameCBInfo(napi_env env) Ability *ability = nullptr; NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - AbilityNameCB *abilityNameCB = new (std::nothrow) AbilityNameCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; + AbilityNameCB *abilityNameCB = new (std::nothrow) AbilityNameCB; if (abilityNameCB == nullptr) { HILOG_ERROR("%{public}s, abilityNameCB == nullptr.", __func__); + return nullptr; } + abilityNameCB->cbBase.cbInfo.env = env; + abilityNameCB->cbBase.asyncWork = nullptr; + abilityNameCB->cbBase.deferred = nullptr; + abilityNameCB->cbBase.ability = ability; + HILOG_INFO("%{public}s, end.", __func__); return abilityNameCB; } @@ -3453,8 +2015,7 @@ void GetAbilityNamePromiseCompleteCB(napi_env env, napi_status status, void *dat * * @return Return JS data successfully, otherwise return nullptr. */ -napi_value GetAbilityNameAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AbilityNameCB *abilityNameCB) +napi_value GetAbilityNameAsync(napi_env env, napi_value *args, const size_t argCallback, AbilityNameCB *abilityNameCB) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (args == nullptr || abilityNameCB == nullptr) { @@ -3465,9 +2026,9 @@ napi_value GetAbilityNameAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &abilityNameCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &abilityNameCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, napi_create_async_work(env, @@ -3548,7 +2109,7 @@ napi_value GetAbilityNameWrap(napi_env env, napi_callback_info info, AbilityName } if (argcAsync > argcPromise) { - ret = GetAbilityNameAsync(env, args, argcAsync, argcPromise, abilityNameCB); + ret = GetAbilityNameAsync(env, args, 0, abilityNameCB); } else { ret = GetAbilityNamePromise(env, abilityNameCB); } @@ -3588,6 +2149,82 @@ napi_value NAPI_GetAbilityNameCommon(napi_env env, napi_callback_info info, Abil return ret; } +bool UnwrapAbilityStartSetting(napi_env env, napi_value param, AAFwk::AbilityStartSetting &setting) +{ + HILOG_INFO("%{public}s called.", __func__); + + if (!IsTypeForNapiValue(env, param, napi_object)) { + return false; + } + + napi_valuetype jsValueType = napi_undefined; + napi_value jsProNameList = nullptr; + uint32_t jsProCount = 0; + + NAPI_CALL_BASE(env, napi_get_property_names(env, param, &jsProNameList), false); + NAPI_CALL_BASE(env, napi_get_array_length(env, jsProNameList, &jsProCount), false); + HILOG_INFO("%{public}s called. Property size=%{public}d.", __func__, jsProCount); + + napi_value jsProName = nullptr; + napi_value jsProValue = nullptr; + for (uint32_t index = 0; index < jsProCount; index++) { + NAPI_CALL_BASE(env, napi_get_element(env, jsProNameList, index, &jsProName), false); + + std::string strProName = UnwrapStringFromJS(env, jsProName); + HILOG_INFO("%{public}s called. Property name=%{public}s.", __func__, strProName.c_str()); + NAPI_CALL_BASE(env, napi_get_named_property(env, param, strProName.c_str(), &jsProValue), false); + NAPI_CALL_BASE(env, napi_typeof(env, jsProValue, &jsValueType), false); + + switch (jsValueType) { + case napi_string: { + std::string natValue = UnwrapStringFromJS(env, jsProValue); + HILOG_INFO("%{public}s called. Property value=%{public}s.", __func__, natValue.c_str()); + setting.AddProperty(strProName, natValue); + break; + } + case napi_boolean: { + bool natValue = false; + NAPI_CALL_BASE(env, napi_get_value_bool(env, jsProValue, &natValue), false); + HILOG_INFO("%{public}s called. Property value=%{public}s.", __func__, natValue ? "true" : "false"); + setting.AddProperty(strProName, std::to_string(natValue)); + break; + } + case napi_number: { + int32_t natValue32 = 0; + double natValueDouble = 0.0; + bool isReadValue32 = false; + bool isReadDouble = false; + if (napi_get_value_int32(env, jsProValue, &natValue32) == napi_ok) { + HILOG_INFO("%{public}s called. Property value=%{public}d.", __func__, natValue32); + isReadValue32 = true; + } + + if (napi_get_value_double(env, jsProValue, &natValueDouble) == napi_ok) { + HILOG_INFO("%{public}s called. Property value=%{public}lf.", __func__, natValueDouble); + isReadDouble = true; + } + + if (isReadValue32 && isReadDouble) { + if (abs(natValueDouble - natValue32 * 1.0) > 0.0) { + setting.AddProperty(strProName, std::to_string(natValueDouble)); + } else { + setting.AddProperty(strProName, std::to_string(natValue32)); + } + } else if (isReadValue32) { + setting.AddProperty(strProName, std::to_string(natValue32)); + } else if (isReadDouble) { + setting.AddProperty(strProName, std::to_string(natValueDouble)); + } + break; + } + default: + break; + } + } + + return true; +} + /** * @brief Parse the parameters. * @@ -3620,6 +2257,9 @@ bool UnwrapParamForWant(napi_env env, napi_value args, AbilityType abilityType, napi_value jsSettingObj = GetPropertyValueByPropertyName(env, args, "abilityStartSetting", napi_object); if (jsSettingObj != nullptr) { param.setting = AbilityStartSetting::GetEmptySetting(); + if (!UnwrapAbilityStartSetting(env, jsSettingObj, *(param.setting))) { + HILOG_ERROR("%{public}s, unwrap abilityStartSetting falied.", __func__); + } HILOG_INFO("%{public}s abilityStartSetting = %{public}p.", __func__, param.setting.get()); } @@ -3649,10 +2289,11 @@ void StartAbilityExecuteCB(napi_env env, void *data) } if (asyncCallbackInfo->param.setting == nullptr) { + HILOG_INFO("%{public}s param.setting == nullptr call StartAbility.", __func__); asyncCallbackInfo->ability->StartAbility(asyncCallbackInfo->param.want); } else { - asyncCallbackInfo->ability->StartAbility( - asyncCallbackInfo->param.want, *(asyncCallbackInfo->param.setting.get())); + HILOG_INFO("%{public}s param.setting != nullptr call StartAbility.", __func__); + asyncCallbackInfo->ability->StartAbility(asyncCallbackInfo->param.want, *(asyncCallbackInfo->param.setting)); } HILOG_INFO("%{public}s end.", __func__); } @@ -3704,7 +2345,7 @@ void StartAbilityPromiseCompletedCB(napi_env env, napi_status status, void *data } napi_value StartAbilityAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) + napi_env env, napi_value *args, const size_t argCallback, AsyncCallbackInfo *asyncCallbackInfo) { HILOG_INFO("%{public}s asyncCallback.", __func__); if (args == nullptr || asyncCallbackInfo == nullptr) { @@ -3715,9 +2356,9 @@ napi_value StartAbilityAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &asyncCallbackInfo->cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &asyncCallbackInfo->cbInfo.callback)); } NAPI_CALL(env, @@ -3792,7 +2433,7 @@ napi_value StartAbilityWrap(napi_env env, napi_callback_info info, AsyncCallback asyncCallbackInfo->param = param; if (argcAsync > argcPromise) { - ret = StartAbilityAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); + ret = StartAbilityAsync(env, args, 1, asyncCallbackInfo); } else { ret = StartAbilityPromise(env, asyncCallbackInfo); } @@ -3963,15 +2604,16 @@ ConnectAbilityCB *CreateConnectAbilityCBInfo(napi_env env) Ability *ability = nullptr; NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - ConnectAbilityCB *connectAbilityCB = new (std::nothrow) ConnectAbilityCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; + ConnectAbilityCB *connectAbilityCB = new (std::nothrow) ConnectAbilityCB; if (connectAbilityCB == nullptr) { HILOG_ERROR("%{public}s connectAbilityCB == nullptr", __func__); + return nullptr; } + connectAbilityCB->cbBase.cbInfo.env = env; + connectAbilityCB->cbBase.asyncWork = nullptr; + connectAbilityCB->cbBase.deferred = nullptr; + connectAbilityCB->cbBase.ability = ability; + HILOG_INFO("%{public}s end.", __func__); return connectAbilityCB; } @@ -4054,7 +2696,7 @@ void ConnectAbilityCallbackCompletedCB(napi_env env, napi_status status, void *d HILOG_INFO("%{public}s end.", __func__); } -napi_value ConnectAbilityAsync(napi_env env, napi_value *args, size_t argcAsync, ConnectAbilityCB *connectAbilityCB) +napi_value ConnectAbilityAsync(napi_env env, napi_value *args, ConnectAbilityCB *connectAbilityCB) { HILOG_INFO("%{public}s asyncCallback.", __func__); if (args == nullptr || connectAbilityCB == nullptr) { @@ -4163,7 +2805,7 @@ napi_value ConnectAbilityWrap(napi_env env, napi_callback_info info, ConnectAbil } } - ret = ConnectAbilityAsync(env, args, argcAsync, connectAbilityCB); + ret = ConnectAbilityAsync(env, args, connectAbilityCB); if (ret != nullptr) { // return number to js NAPI_CALL(env, napi_create_int64(env, connectAbilityCB->id, &ret)); @@ -4224,15 +2866,16 @@ ConnectAbilityCB *CreateDisConnectAbilityCBInfo(napi_env env) Ability *ability = nullptr; NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - ConnectAbilityCB *connectAbilityCB = new (std::nothrow) ConnectAbilityCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; + ConnectAbilityCB *connectAbilityCB = new (std::nothrow) ConnectAbilityCB; if (connectAbilityCB == nullptr) { HILOG_ERROR("%{public}s connectAbilityCB == nullptr", __func__); + return nullptr; } + connectAbilityCB->cbBase.cbInfo.env = env; + connectAbilityCB->cbBase.asyncWork = nullptr; + connectAbilityCB->cbBase.deferred = nullptr; + connectAbilityCB->cbBase.ability = ability; + HILOG_INFO("%{public}s end.", __func__); return connectAbilityCB; } @@ -4313,7 +2956,7 @@ void DisConnectAbilityPromiseCompletedCB(napi_env env, napi_status status, void } napi_value DisConnectAbilityAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ConnectAbilityCB *connectAbilityCB) + napi_env env, napi_value *args, const size_t argCallback, ConnectAbilityCB *connectAbilityCB) { HILOG_INFO("%{public}s asyncCallback.", __func__); if (args == nullptr || connectAbilityCB == nullptr) { @@ -4324,9 +2967,9 @@ napi_value DisConnectAbilityAsync( NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + NAPI_CALL(env, napi_typeof(env, args[argCallback], &valuetype)); if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &connectAbilityCB->cbBase.cbInfo.callback)); + NAPI_CALL(env, napi_create_reference(env, args[argCallback], 1, &connectAbilityCB->cbBase.cbInfo.callback)); } NAPI_CALL(env, @@ -4417,7 +3060,7 @@ napi_value DisConnectAbilityWrap(napi_env env, napi_callback_info info, ConnectA } if (argcAsync > argcPromise) { - ret = DisConnectAbilityAsync(env, args, argcAsync, argcPromise, connectAbilityCB); + ret = DisConnectAbilityAsync(env, args, ARGS_ONE, connectAbilityCB); } else { ret = DisConnectAbilityPromise(env, connectAbilityCB); } @@ -4478,46 +3121,73 @@ void NAPIAbilityConnection::OnAbilityConnectDone( HILOG_INFO("%{public}s, called.", __func__); uv_loop_s *loop = nullptr; -#if NAPI_VERSION >= 2 napi_get_uv_event_loop(env_, &loop); -#endif // NAPI_VERSION >= 2 uv_work_t *work = new uv_work_t; - ConnectAbilityCB *connectAbilityCB = - new (std::nothrow) ConnectAbilityCB{.cbBase.cbInfo.env = env_, .cbBase.cbInfo.callback = connectRef_}; + ConnectAbilityCB *connectAbilityCB = new (std::nothrow) ConnectAbilityCB; + if (connectAbilityCB == nullptr) { + HILOG_ERROR("%{public}s, connectAbilityCB == nullptr.", __func__); + if (work != nullptr) { + delete work; + work = nullptr; + } + return; + } + connectAbilityCB->cbBase.cbInfo.env = env_; + connectAbilityCB->cbBase.cbInfo.callback = connectRef_; connectAbilityCB->abilityConnectionCB.elementName = element; connectAbilityCB->abilityConnectionCB.resultCode = resultCode; connectAbilityCB->abilityConnectionCB.connection = remoteObject; work->data = (void *)connectAbilityCB; - uv_queue_work( + int rev = uv_queue_work( loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { HILOG_INFO("OnAbilityConnectDone, uv_queue_work"); // JS Thread - ConnectAbilityCB *event = (ConnectAbilityCB *)work->data; + ConnectAbilityCB *connectAbilityCB = (ConnectAbilityCB *)work->data; napi_value result[ARGS_TWO] = {0}; - result[PARAM0] = WrapElementName(event->cbBase.cbInfo.env, event->abilityConnectionCB.elementName); - napi_value jsRemoteObject = - NAPI_ohos_rpc_CreateJsRemoteObject(event->cbBase.cbInfo.env, event->abilityConnectionCB.connection); + result[PARAM0] = + WrapElementName(connectAbilityCB->cbBase.cbInfo.env, connectAbilityCB->abilityConnectionCB.elementName); + napi_value jsRemoteObject = NAPI_ohos_rpc_CreateJsRemoteObject( + connectAbilityCB->cbBase.cbInfo.env, connectAbilityCB->abilityConnectionCB.connection); result[PARAM1] = jsRemoteObject; napi_value callback = 0; napi_value undefined = 0; - napi_get_undefined(event->cbBase.cbInfo.env, &undefined); + napi_get_undefined(connectAbilityCB->cbBase.cbInfo.env, &undefined); napi_value callResult = 0; - napi_get_reference_value(event->cbBase.cbInfo.env, event->cbBase.cbInfo.callback, &callback); + napi_get_reference_value( + connectAbilityCB->cbBase.cbInfo.env, connectAbilityCB->cbBase.cbInfo.callback, &callback); - napi_call_function(event->cbBase.cbInfo.env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); - if (event->cbBase.cbInfo.callback != nullptr) { - napi_delete_reference(event->cbBase.cbInfo.env, event->cbBase.cbInfo.callback); + napi_call_function( + connectAbilityCB->cbBase.cbInfo.env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); + if (connectAbilityCB->cbBase.cbInfo.callback != nullptr) { + napi_delete_reference(connectAbilityCB->cbBase.cbInfo.env, connectAbilityCB->cbBase.cbInfo.callback); + } + if (connectAbilityCB != nullptr) { + delete connectAbilityCB; + connectAbilityCB = nullptr; + } + if (work != nullptr) { + delete work; + work = nullptr; } - delete event; - delete work; HILOG_INFO("OnAbilityConnectDone, uv_queue_work end"); }); + + if (rev != 0) { + if (connectAbilityCB != nullptr) { + delete connectAbilityCB; + connectAbilityCB = nullptr; + } + if (work != nullptr) { + delete work; + work = nullptr; + } + } HILOG_INFO("%{public}s, end.", __func__); } @@ -4527,46 +3197,56 @@ void NAPIAbilityConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementNam uv_loop_s *loop = nullptr; -#if NAPI_VERSION >= 2 napi_get_uv_event_loop(env_, &loop); -#endif // NAPI_VERSION >= 2 HILOG_INFO("OnAbilityDisconnectDone bundleName:%{public}s abilityName:%{public}s resultCode:%{public}d", element.GetBundleName().c_str(), element.GetAbilityName().c_str(), resultCode); uv_work_t *work = new uv_work_t; - ConnectAbilityCB *connectAbilityCB = - new (std::nothrow) ConnectAbilityCB{.cbBase.cbInfo.env = env_, .cbBase.cbInfo.callback = disconnectRef_}; + ConnectAbilityCB *connectAbilityCB = new (std::nothrow) ConnectAbilityCB; + if (connectAbilityCB == nullptr) { + HILOG_ERROR("%{public}s, connectAbilityCB == nullptr.", __func__); + if (work != nullptr) { + delete work; + work = nullptr; + } + return; + } + connectAbilityCB->cbBase.cbInfo.env = env_; + connectAbilityCB->cbBase.cbInfo.callback = disconnectRef_; connectAbilityCB->abilityConnectionCB.elementName = element; connectAbilityCB->abilityConnectionCB.resultCode = resultCode; work->data = (void *)connectAbilityCB; - uv_queue_work( + int rev = uv_queue_work( loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { HILOG_INFO("OnAbilityDisconnectDone, uv_queue_work"); // JS Thread - ConnectAbilityCB *event = (ConnectAbilityCB *)work->data; + ConnectAbilityCB *connectAbilityCB = (ConnectAbilityCB *)work->data; napi_value result = nullptr; - result = WrapElementName(event->cbBase.cbInfo.env, event->abilityConnectionCB.elementName); + result = + WrapElementName(connectAbilityCB->cbBase.cbInfo.env, connectAbilityCB->abilityConnectionCB.elementName); napi_value callback = 0; napi_value undefined = 0; - napi_get_undefined(event->cbBase.cbInfo.env, &undefined); + napi_get_undefined(connectAbilityCB->cbBase.cbInfo.env, &undefined); napi_value callResult = 0; - napi_get_reference_value(event->cbBase.cbInfo.env, event->cbBase.cbInfo.callback, &callback); + napi_get_reference_value( + connectAbilityCB->cbBase.cbInfo.env, connectAbilityCB->cbBase.cbInfo.callback, &callback); - napi_call_function(event->cbBase.cbInfo.env, undefined, callback, ARGS_ONE, &result, &callResult); - if (event->cbBase.cbInfo.callback != nullptr) { - napi_delete_reference(event->cbBase.cbInfo.env, event->cbBase.cbInfo.callback); + napi_call_function( + connectAbilityCB->cbBase.cbInfo.env, undefined, callback, ARGS_ONE, &result, &callResult); + if (connectAbilityCB->cbBase.cbInfo.callback != nullptr) { + napi_delete_reference(connectAbilityCB->cbBase.cbInfo.env, connectAbilityCB->cbBase.cbInfo.callback); } // release connect HILOG_INFO("OnAbilityDisconnectDone connects_.size:%{public}zu", connects_.size()); - std::string bundleName = event->abilityConnectionCB.elementName.GetBundleName(); - std::string abilityName = event->abilityConnectionCB.elementName.GetAbilityName(); + std::string bundleName = connectAbilityCB->abilityConnectionCB.elementName.GetBundleName(); + std::string abilityName = connectAbilityCB->abilityConnectionCB.elementName.GetAbilityName(); auto item = std::find_if(connects_.begin(), connects_.end(), [bundleName, abilityName]( @@ -4580,12 +3260,107 @@ void NAPIAbilityConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementNam HILOG_INFO("OnAbilityDisconnectDone erase connects_.size:%{public}zu", connects_.size()); } - delete event; - delete work; + if (connectAbilityCB != nullptr) { + delete connectAbilityCB; + connectAbilityCB = nullptr; + } + if (work != nullptr) { + delete work; + work = nullptr; + } HILOG_INFO("OnAbilityDisconnectDone, uv_queue_work end"); }); + + if (rev != 0) { + if (connectAbilityCB != nullptr) { + delete connectAbilityCB; + connectAbilityCB = nullptr; + } + if (work != nullptr) { + delete work; + work = nullptr; + } + } HILOG_INFO("%{public}s, end.", __func__); } +/** + * @brief AcquireDataAbilityHelper. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_AcquireDataAbilityHelperCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s,called", __func__); + DataAbilityHelperCB *dataAbilityHelperCB = new (std::nothrow) DataAbilityHelperCB; + dataAbilityHelperCB->cbBase.cbInfo.env = env; + if (dataAbilityHelperCB == nullptr) { + HILOG_ERROR("%{public}s, dataAbilityHelperCB == nullptr", __func__); + return WrapVoidToJS(env); + } + + dataAbilityHelperCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + dataAbilityHelperCB->cbBase.abilityType = abilityType; + napi_value ret = AcquireDataAbilityHelperWrap(env, info, dataAbilityHelperCB); + if (ret == nullptr) { + HILOG_ERROR("%{public}s, ret == nullptr", __func__); + if (dataAbilityHelperCB != nullptr) { + delete dataAbilityHelperCB; + dataAbilityHelperCB = nullptr; + } + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s,end", __func__); + return ret; +} + +/** + * @brief acquireDataAbilityHelper processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param dataAbilityHelperCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value AcquireDataAbilityHelperWrap(napi_env env, napi_callback_info info, DataAbilityHelperCB *dataAbilityHelperCB) +{ + HILOG_INFO("%{public}s,called", __func__); + if (dataAbilityHelperCB == nullptr) { + HILOG_ERROR("%{public}s,dataAbilityHelperCB == nullptr", __func__); + return nullptr; + } + + size_t requireArgc = ARGS_ONE; + size_t argc = ARGS_ONE; + napi_value args[ARGS_ONE] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + if (argc > requireArgc) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[PARAM0], &valuetype)); + if (valuetype != napi_string) { + HILOG_ERROR("%{public}s, Wrong argument type.", __func__); + return nullptr; + } + + if (!CheckAbilityType(&dataAbilityHelperCB->cbBase)) { + dataAbilityHelperCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + HILOG_ERROR("%{public}s ability type invalid.", __func__); + return nullptr; + } + + napi_value result = nullptr; + NAPI_CALL(env, napi_new_instance(env, g_dataAbilityHelper, 1, &args[PARAM0], &result)); + delete dataAbilityHelperCB; + dataAbilityHelperCB = nullptr; + HILOG_INFO("%{public}s,end", __func__); + return result; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.h b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.h index fc37a2f470a..77d48455eda 100755 --- a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.h +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.h @@ -27,35 +27,16 @@ const std::int32_t STR_MAX_SIZE = 128; napi_value *GetGlobalClassContext(void); void SaveAppInfo(AppInfo_ &appInfo, const ApplicationInfo &appInfoOrg); napi_value WrapAppInfo(napi_env env, const AppInfo_ &appInfo); -/** - * @brief Obtains information about the current application. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetApplicationInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); - -/** - * @brief Get bundle name. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetBundleNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType); /** - * @brief Obtains the process Info this application. + * @brief Get Files Dir. * * @param env The environment that the Node-API call is invoked under. * @param info The callback info passed into the callback function. * * @return The return value from NAPI C++ to JS for the module. */ -napi_value NAPI_GetProcessInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); +napi_value NAPI_GetFilesDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); /** * @brief Obtains the type of this application. @@ -67,16 +48,6 @@ napi_value NAPI_GetProcessInfoCommon(napi_env env, napi_callback_info info, Abil */ napi_value NAPI_GetAppTypeCommon(napi_env env, napi_callback_info info, AbilityType abilityType); -/** - * @brief Obtains the elementName object of the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetElementNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType); - /** * @brief Obtains information about the current ability. * @@ -97,26 +68,6 @@ napi_value NAPI_GetAbilityInfoCommon(napi_env env, napi_callback_info info, Abil */ napi_value NAPI_GetHapModuleInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); -/** - * @brief Obtains the name of the current process. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetProcessNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType); - -/** - * @brief Obtains the bundle name of the ability that called the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetCallingBundleCommon(napi_env env, napi_callback_info info, AbilityType abilityType); - /** * @brief Create asynchronous data. * @@ -184,7 +135,8 @@ napi_value NAPI_StopAbilityCommon(napi_env env, napi_callback_info info, Ability * * @return The return value from NAPI C++ to JS for the module. */ -napi_value GetContinueAbilityOptionsInfoCommon(const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info); +napi_value GetContinueAbilityOptionsInfoCommon( + const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info); /** * @brief Obtains the continue ability can reversible or not @@ -210,6 +162,8 @@ napi_value GetContinueAbilityOptionsReversible( napi_value GetContinueAbilityOptionsDeviceID( const napi_env &env, const napi_value &value, ContinueAbilityOptionsInfo &info); +bool UnwrapAbilityStartSetting(napi_env env, napi_value param, AAFwk::AbilityStartSetting &setting); + class NAPIAbilityConnection : public AAFwk::AbilityConnectionStub { public: void OnAbilityConnectDone( @@ -245,6 +199,27 @@ napi_value NAPI_ConnectAbilityCommon(napi_env env, napi_callback_info info, Abil */ napi_value NAPI_DisConnectAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType); +/** + * @brief acquireDataAbilityHelper processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param dataAbilityHelperCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value AcquireDataAbilityHelperWrap( + napi_env env, napi_callback_info info, DataAbilityHelperCB *dataAbilityHelperCB); + +/** + * @brief AcquireDataAbilityHelper. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_AcquireDataAbilityHelperCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + struct ConnecttionKey { Want want; int64_t id; diff --git a/interfaces/kits/napi/aafwk/particleAbility/particle_ability.cpp b/interfaces/kits/napi/aafwk/particleAbility/particle_ability.cpp index 0150609d7e2..2de3dd1ea82 100755 --- a/interfaces/kits/napi/aafwk/particleAbility/particle_ability.cpp +++ b/interfaces/kits/napi/aafwk/particleAbility/particle_ability.cpp @@ -26,24 +26,6 @@ using namespace OHOS::AppExecFwk; namespace OHOS { namespace AppExecFwk { -napi_value NAPI_PAGetApplicationInfo(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s called.", __func__); - return NAPI_GetApplicationInfoCommon(env, info, AbilityType::UNKNOWN); -} - -napi_value NAPI_PAGetBundleName(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s called.", __func__); - return NAPI_GetBundleNameCommon(env, info, AbilityType::UNKNOWN); -} - -napi_value NAPI_PAGetProcessInfo(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s called.", __func__); - return NAPI_GetProcessInfoCommon(env, info, AbilityType::UNKNOWN); -} - /** * @brief Obtains the type of this application. * @@ -58,20 +40,6 @@ napi_value NAPI_PAGetAppType(napi_env env, napi_callback_info info) return NAPI_GetAppTypeCommon(env, info, AbilityType::UNKNOWN); } -/** - * @brief Obtains the elementName object of the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_PAGetElementName(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s called.", __func__); - return NAPI_GetElementNameCommon(env, info, AbilityType::UNKNOWN); -} - /** * @brief Obtains information about the current ability. * @@ -100,34 +68,6 @@ napi_value NAPI_PAGetHapModuleInfo(napi_env env, napi_callback_info info) return NAPI_GetHapModuleInfoCommon(env, info, AbilityType::UNKNOWN); } -/** - * @brief Obtains the name of the current process. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_PAProcessName(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s called.", __func__); - return NAPI_GetProcessNameCommon(env, info, AbilityType::UNKNOWN); -} - -/** - * @brief Obtains the bundle name of the ability that called the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_PAGetCallingBundle(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s called.", __func__); - return NAPI_GetCallingBundleCommon(env, info, AbilityType::UNKNOWN); -} - /** * @brief Get context. * @@ -226,6 +166,20 @@ napi_value NAPI_PADisConnectAbility(napi_env env, napi_callback_info info) return NAPI_DisConnectAbilityCommon(env, info, AbilityType::UNKNOWN); } +/** + * @brief FeatureAbility NAPI method : acquireDataAbilityHelper. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAAcquireDataAbilityHelper(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s,called", __func__); + return NAPI_AcquireDataAbilityHelperCommon(env, info, AbilityType::UNKNOWN); +} + /** * @brief ParticleAbility NAPI module registration. * @@ -238,15 +192,9 @@ napi_value ParticleAbilityInit(napi_env env, napi_value exports) { HILOG_INFO("%{public}s called.", __func__); napi_property_descriptor properties[] = { - DECLARE_NAPI_FUNCTION("getApplicationInfo", NAPI_PAGetApplicationInfo), - DECLARE_NAPI_FUNCTION("getBundleName", NAPI_PAGetBundleName), - DECLARE_NAPI_FUNCTION("getProcessInfo", NAPI_PAGetProcessInfo), DECLARE_NAPI_FUNCTION("getAppType", NAPI_PAGetAppType), - DECLARE_NAPI_FUNCTION("getElementName", NAPI_PAGetElementName), DECLARE_NAPI_FUNCTION("getAbilityInfo", NAPI_PAGetAbilityInfo), DECLARE_NAPI_FUNCTION("getHapModuleInfo", NAPI_PAGetHapModuleInfo), - DECLARE_NAPI_FUNCTION("getProcessName", NAPI_PAProcessName), - DECLARE_NAPI_FUNCTION("getCallingBundle", NAPI_PAGetCallingBundle), DECLARE_NAPI_FUNCTION("getContext", NAPI_PAGetContext), DECLARE_NAPI_FUNCTION("getWant", NAPI_PAGetWant), DECLARE_NAPI_FUNCTION("getAbilityName", NAPI_PAGetAbilityName), @@ -254,6 +202,7 @@ napi_value ParticleAbilityInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("stopAbility", NAPI_PAStopAbility), DECLARE_NAPI_FUNCTION("connectAbility", NAPI_PAConnectAbility), DECLARE_NAPI_FUNCTION("disconnectAbility", NAPI_PADisConnectAbility), + DECLARE_NAPI_FUNCTION("acquireDataAbilityHelper", NAPI_PAAcquireDataAbilityHelper), }; napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties); diff --git a/ohos.build b/ohos.build old mode 100644 new mode 100755 index 3bb586dd004..0f802dabcd7 --- a/ohos.build +++ b/ohos.build @@ -81,7 +81,8 @@ "//foundation/aafwk/standard/services:unittest", "//foundation/aafwk/standard/tools/test:systemtest", "//foundation/aafwk/standard/tools/test:moduletest", - "//foundation/aafwk/standard/tools/test:unittest" + "//foundation/aafwk/standard/tools/test:unittest", + "//foundation/aafwk/standard/tools/zip/test:unittest" ] } }, diff --git a/services/abilitymgr/BUILD.gn b/services/abilitymgr/BUILD.gn index 5c39f87a528..ceee5a8f622 100644 --- a/services/abilitymgr/BUILD.gn +++ b/services/abilitymgr/BUILD.gn @@ -17,7 +17,10 @@ import("//foundation/aafwk/standard/services/abilitymgr/abilitymgr.gni") EVENT_DIR = "//base/notification/ces_standard/" group("abilityms_target") { - deps = [ ":abilityms" ] + deps = [ + ":abilityms", + ":ams_service_config", + ] } group("unittest") { @@ -81,3 +84,13 @@ ohos_shared_library("abilityms") { subsystem_name = "aafwk" part_name = "aafwk_standard" } + +ohos_prebuilt_etc("ams_service_config.json") { + source = "//foundation/aafwk/standard/services/abilitymgr/resource/ams_service_config.json" + subsystem_name = "aafwk" + part_name = "aafwk_standard" +} + +group("ams_service_config") { + deps = [ ":ams_service_config.json" ] +} diff --git a/services/abilitymgr/abilitymgr.gni b/services/abilitymgr/abilitymgr.gni index 6d46d2ecc91..fe5beb7822a 100644 --- a/services/abilitymgr/abilitymgr.gni +++ b/services/abilitymgr/abilitymgr.gni @@ -63,4 +63,5 @@ abilityms_files = [ "${services_path}/abilitymgr/src/aafwk_dummy_configuration.cpp", "${services_path}/abilitymgr/src/configuration_holder.cpp", "${services_path}/abilitymgr/src/resume_mission_container.cpp", + "${services_path}/abilitymgr/src/ams_configuration_parameter.cpp", ] diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index 74da3e2137a..7f28e830576 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -35,6 +35,7 @@ #include "uri.h" #include "ability_config.h" #include "pending_want_manager.h" +#include "ams_configuration_parameter.h" namespace OHOS { namespace AAFwk { @@ -569,10 +570,22 @@ private: */ bool Init(); /** - * wait for starting lanucher ability. + * starting lanucher ability. * */ - void WaitForStartingLauncherAbility(); + void StartingLauncherAbility(); + + /** + *starting system ui abilites. + * + */ + void StartingSystemUiAbility(SatrtUiMode mode); + + /** + * connet bms. + * + */ + void ConnectBmsService(); /** * get the user id. * @@ -586,6 +599,11 @@ private: */ int GenerateAbilityRequest( const Want &want, int requestCode, AbilityRequest &request, const sptr &callerToken); + /** + * Select to start the application according to the configuration file of AMS + * + */ + void StartSystemApplication(); sptr GetBundleManager(); int PreLoadAppDataAbilities(const std::string &bundleName); @@ -620,8 +638,10 @@ private: std::shared_ptr dataAbilityManager_; std::shared_ptr pendingWantManager_; std::shared_ptr systemAppManager_; + std::shared_ptr amsConfigResolver_; const static std::map dumpMap; }; + } // namespace AAFwk } // namespace OHOS #endif // OHOS_AAFWK_ABILITY_MANAGER_SERVICE_H diff --git a/services/abilitymgr/include/ams_configuration_parameter.h b/services/abilitymgr/include/ams_configuration_parameter.h new file mode 100644 index 00000000000..c803db74d36 --- /dev/null +++ b/services/abilitymgr/include/ams_configuration_parameter.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_AAFWK_AMS_CONFIGURATION_PARAMETER_H +#define OHOS_AAFWK_AMS_CONFIGURATION_PARAMETER_H + +#include +#include + +namespace OHOS { +namespace AAFwk { + +namespace AmsConfig { + +const std::string AMS_CONFIG_FILE_PATH{"/system/etc/ams_service_config.json"}; +const std::string SERVICE_ITEM_AMS{"service_startup_config"}; +const std::string STARTUP_LUNCHER{"startup_launcher"}; +const std::string STARTUP_STATUS_BAR{"startup_system_ui_status_bar"}; +const std::string STARTUP_NAVIGATION_BAR{"startup_system_ui_navigation_bar"}; + +} // namespace AmsConfig + +class AmsConfigurationParameter final{ + +private: + bool nonConfigFile{false}; + bool canStartLuncher{false}; + bool canStartUiStatusBar{false}; + bool canStartUiNavigationBar{false}; + +public: + AmsConfigurationParameter(); + ~AmsConfigurationParameter(); + /** + * return true : ams no config file + * return false : ams have config file + */ + bool NonConfigFile() const; + /** + * return true : ams can start luncher + * return false : ams do not start luncher + */ + bool GetStartLuncherState() const; + /** + * return true : ams can start system ui status bar + * return false : ams do not start system ui status bar + */ + bool GetStatusBarState() const; + /** + * return true : ams can start system ui navigation bar + * return false : ams do not start system ui navigation bar + */ + bool GetNavigationBarState() const; + /** + * Get profile information + */ + void Parse(); + + enum { READ_OK = 0, READ_FAIL = 1, READ_JSON_FAIL = 2 }; + +private: + /** + * Read the configuration file of ams + * + */ + int LoadAmsConfiguration(const std::string &filePath); +}; + +enum SatrtUiMode { STATUSBAR = 1, NAVIGATIONBAR = 2, STARTUIBOTH = 3 }; + +} // namespace AAFwk +} // namespace OHOS +#endif // OHOS_AAFWK_AMS_CONFIGURATION_PARAMETER_H diff --git a/services/abilitymgr/resource/ams_service_config.json b/services/abilitymgr/resource/ams_service_config.json new file mode 100644 index 00000000000..0dd3ebfb965 --- /dev/null +++ b/services/abilitymgr/resource/ams_service_config.json @@ -0,0 +1,8 @@ +{ + "service_startup_config":{ + "startup_launcher":true, + "startup_system_ui_status_bar" : true, + "startup_system_ui_navigation_bar" : true + } + +} diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index efb99dd6145..d9114d500ae 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -115,6 +115,12 @@ bool AbilityManagerService::Init() auto dataAbilityManager = std::make_shared(); CHECK_POINTER_RETURN_BOOL(dataAbilityManager); + amsConfigResolver_ = std::make_shared(); + if (amsConfigResolver_) { + amsConfigResolver_->Parse(); + HILOG_INFO("ams config parse"); + } + auto pendingWantManager = std::make_shared(); if (!pendingWantManager) { HILOG_ERROR("Failed to init pending want ability manager."); @@ -126,11 +132,7 @@ bool AbilityManagerService::Init() systemAppManager_ = std::make_shared(userId); CHECK_POINTER_RETURN_BOOL(systemAppManager_); - auto startLauncherAbilityTask = [aams = shared_from_this()]() { - aams->WaitForStartingLauncherAbility(); - aams->StartSystemUi(AbilityConfig::SYSTEM_UI_STATUS_BAR); - aams->StartSystemUi(AbilityConfig::SYSTEM_UI_NAVIGATION_BAR); - }; + auto startLauncherAbilityTask = [aams = shared_from_this()]() { aams->StartSystemApplication(); }; handler_->PostTask(startLauncherAbilityTask, "startLauncherAbility"); dataAbilityManager_ = dataAbilityManager; pendingWantManager_ = pendingWantManager; @@ -167,7 +169,6 @@ int AbilityManagerService::StartAbility( const Want &want, const sptr &callerToken, int requestCode, int callerUid) { HILOG_INFO("%{public}s", __func__); - if (callerToken != nullptr && !VerificationToken(callerToken)) { return ERR_INVALID_VALUE; } @@ -1041,29 +1042,13 @@ int AbilityManagerService::GetUserId() return DEFAULT_USER_ID; } -void AbilityManagerService::WaitForStartingLauncherAbility() +void AbilityManagerService::StartingLauncherAbility() { - HILOG_INFO("Waiting AppMgr Service run completed."); - while (!appScheduler_->Init(shared_from_this())) { - HILOG_ERROR("Failed to init appScheduler_"); - usleep(REPOLL_TIME_MICRO_SECONDS); - } - - HILOG_INFO("Waiting BundleMgr Service run completed."); - /* wait until connected to bundle manager service */ - while (iBundleManager_ == nullptr) { - sptr bundle_obj = - OHOS::DelayedSingleton::GetInstance()->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (bundle_obj == nullptr) { - HILOG_ERROR("Failed to get bundle manager service"); - usleep(REPOLL_TIME_MICRO_SECONDS); - continue; - } - iBundleManager_ = iface_cast(bundle_obj); + HILOG_DEBUG("%{public}s", __func__); + if (!iBundleManager_) { + HILOG_INFO("bms service is null"); } - HILOG_INFO("Waiting Home Launcher Ability install completed."); - /* query if launcher ability has installed */ AppExecFwk::AbilityInfo abilityInfo; /* First stage, hardcoding for the first launcher App */ @@ -1074,22 +1059,7 @@ void AbilityManagerService::WaitForStartingLauncherAbility() usleep(REPOLL_TIME_MICRO_SECONDS); } - AppExecFwk::AbilityInfo statusBarInfo; - AppExecFwk::AbilityInfo navigationBarInfo; - Want statusBarWant; - Want navigationBarWant; - statusBarWant.SetElementName(AbilityConfig::SYSTEM_UI_BUNDLE_NAME, AbilityConfig::SYSTEM_UI_STATUS_BAR); - navigationBarWant.SetElementName(AbilityConfig::SYSTEM_UI_BUNDLE_NAME, AbilityConfig::SYSTEM_UI_NAVIGATION_BAR); - uint32_t waitCnt = 0; - // Wait 10 minutes for the installation to complete. - while ((!(iBundleManager_->QueryAbilityInfo(statusBarWant, statusBarInfo)) || - !(iBundleManager_->QueryAbilityInfo(navigationBarWant, navigationBarInfo))) && - waitCnt < MAX_WAIT_SYSTEM_UI_NUM) { - HILOG_INFO("Waiting query system ui info completed."); - usleep(REPOLL_TIME_MICRO_SECONDS); - waitCnt++; - } - HILOG_INFO("Waiting boot animation for 5 seconds."); + HILOG_INFO("waiting boot animation for 5 seconds."); usleep(REPOLL_TIME_MICRO_SECONDS * 5); HILOG_INFO("Start Home Launcher Ability."); /* start launch ability */ @@ -1519,11 +1489,100 @@ void AbilityManagerService::NotifyBmsAbilityLifeStatus( bundleManager->NotifyActivityLifeStatus(bundleName, abilityName, launchTime); } -int AbilityManagerService::CheckPermission(const std::string &bundleName, const std::string &permission) +void AbilityManagerService::StartSystemApplication() { - auto bundleManager = GetBundleManager(); - CHECK_POINTER_AND_RETURN(bundleManager, INNER_ERR); - return bundleManager->CheckPermission(bundleName, permission); + HILOG_DEBUG("%{public}s", __func__); + + ConnectBmsService(); + + if (!amsConfigResolver_ || amsConfigResolver_->NonConfigFile()) { + HILOG_INFO("start all"); + StartingLauncherAbility(); + StartingSystemUiAbility(SatrtUiMode::STARTUIBOTH); + return; + } + + if (amsConfigResolver_->GetStartLuncherState()) { + HILOG_INFO("start luncher"); + StartingLauncherAbility(); + } + + if (amsConfigResolver_->GetStatusBarState()) { + HILOG_INFO("start status bar"); + StartingSystemUiAbility(SatrtUiMode::STATUSBAR); + } + + if (amsConfigResolver_->GetNavigationBarState()) { + HILOG_INFO("start navigation bar"); + StartingSystemUiAbility(SatrtUiMode::NAVIGATIONBAR); + } } + +void AbilityManagerService::ConnectBmsService() +{ + HILOG_DEBUG("%{public}s", __func__); + HILOG_INFO("Waiting AppMgr Service run completed."); + while (!appScheduler_->Init(shared_from_this())) { + HILOG_ERROR("failed to init appScheduler_"); + usleep(REPOLL_TIME_MICRO_SECONDS); + } + + HILOG_INFO("Waiting BundleMgr Service run completed."); + /* wait until connected to bundle manager service */ + while (iBundleManager_ == nullptr) { + sptr bundle_obj = + OHOS::DelayedSingleton::GetInstance()->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundle_obj == nullptr) { + HILOG_ERROR("failed to get bundle manager service"); + usleep(REPOLL_TIME_MICRO_SECONDS); + continue; + } + iBundleManager_ = iface_cast(bundle_obj); + } + + HILOG_INFO("Connect bms success!"); +} + +void AbilityManagerService::StartingSystemUiAbility(SatrtUiMode mode) +{ + HILOG_DEBUG("%{public}s", __func__); + if (!iBundleManager_) { + HILOG_INFO("bms service is null"); + } + + AppExecFwk::AbilityInfo statusBarInfo; + AppExecFwk::AbilityInfo navigationBarInfo; + Want statusBarWant; + Want navigationBarWant; + statusBarWant.SetElementName(AbilityConfig::SYSTEM_UI_BUNDLE_NAME, AbilityConfig::SYSTEM_UI_STATUS_BAR); + navigationBarWant.SetElementName(AbilityConfig::SYSTEM_UI_BUNDLE_NAME, AbilityConfig::SYSTEM_UI_NAVIGATION_BAR); + uint32_t waitCnt = 0; + // Wait 10 minutes for the installation to complete. + while ((!(iBundleManager_->QueryAbilityInfo(statusBarWant, statusBarInfo)) || + !(iBundleManager_->QueryAbilityInfo(navigationBarWant, navigationBarInfo))) && + waitCnt < MAX_WAIT_SYSTEM_UI_NUM) { + HILOG_INFO("Waiting query system ui info completed."); + usleep(REPOLL_TIME_MICRO_SECONDS); + waitCnt++; + } + + HILOG_INFO("start ui mode : %{public}d", mode); + switch (mode) { + case SatrtUiMode::STATUSBAR: + StartSystemUi(AbilityConfig::SYSTEM_UI_STATUS_BAR); + break; + case SatrtUiMode::NAVIGATIONBAR: + StartSystemUi(AbilityConfig::SYSTEM_UI_NAVIGATION_BAR); + break; + case SatrtUiMode::STARTUIBOTH: + StartSystemUi(AbilityConfig::SYSTEM_UI_STATUS_BAR); + StartSystemUi(AbilityConfig::SYSTEM_UI_NAVIGATION_BAR); + break; + default: + HILOG_INFO("Input mode error ..."); + break; + } +} + } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/ability_stack_manager.cpp b/services/abilitymgr/src/ability_stack_manager.cpp index aceaae1791c..db461cfb9a6 100644 --- a/services/abilitymgr/src/ability_stack_manager.cpp +++ b/services/abilitymgr/src/ability_stack_manager.cpp @@ -296,9 +296,6 @@ void AbilityStackManager::MoveMissionAndAbility(const std::shared_ptrSetIsLauncherCreate(); } } - - // add caller record - targetAbilityRecord->SetMissionRecord(targetMissionRecord); // check mission window mode. if (targetAbilityRecord->GetMissionRecord() == nullptr) { auto option = targetMissionRecord->GetMissionOption(); @@ -310,6 +307,9 @@ void AbilityStackManager::MoveMissionAndAbility(const std::shared_ptrSetStartSetting(setting); } } + + // add caller record + targetAbilityRecord->SetMissionRecord(targetMissionRecord); // add ability record to mission record. // if this ability record exist this mission record, do not add. targetMissionRecord->AddAbilityRecordToTop(targetAbilityRecord); diff --git a/services/abilitymgr/src/ams_configuration_parameter.cpp b/services/abilitymgr/src/ams_configuration_parameter.cpp new file mode 100644 index 00000000000..b3180fa181f --- /dev/null +++ b/services/abilitymgr/src/ams_configuration_parameter.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ams_configuration_parameter.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace AAFwk { +using json = nlohmann::json; + +AmsConfigurationParameter::AmsConfigurationParameter() +{} + +AmsConfigurationParameter::~AmsConfigurationParameter() +{} + +void AmsConfigurationParameter::Parse() +{ + auto ref = LoadAmsConfiguration(AmsConfig::AMS_CONFIG_FILE_PATH); + HILOG_INFO("load config ref : %{public}d", ref); +} + +bool AmsConfigurationParameter::GetStartLuncherState() const +{ + return canStartLuncher; +} + +bool AmsConfigurationParameter::GetStatusBarState() const +{ + return canStartUiStatusBar; +} + +bool AmsConfigurationParameter::GetNavigationBarState() const +{ + return canStartUiNavigationBar; +} + +bool AmsConfigurationParameter::NonConfigFile() const +{ + return nonConfigFile; +} + +int AmsConfigurationParameter::LoadAmsConfiguration(const std::string &filePath) +{ + HILOG_DEBUG("%{public}s", __func__); + + std::ifstream inFile; + inFile.open(filePath, std::ios::in); + if (!inFile.is_open()) { + HILOG_INFO("read ams config error ..."); + nonConfigFile = true; + return READ_FAIL; + } + + json amsJson; + inFile >> amsJson; + if (amsJson.is_discarded()) { + HILOG_INFO("json discarded error ..."); + nonConfigFile = true; + inFile.close(); + return READ_JSON_FAIL; + } + + if (amsJson.contains(AmsConfig::SERVICE_ITEM_AMS)) { + canStartLuncher = amsJson.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::STARTUP_LUNCHER).get(); + canStartUiStatusBar = amsJson.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::STARTUP_STATUS_BAR).get(); + canStartUiNavigationBar = + amsJson.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::STARTUP_NAVIGATION_BAR).get(); + HILOG_INFO("get ams service config succes!"); + } else { + HILOG_INFO("json no have service item ..."); + nonConfigFile = true; + amsJson.clear(); + inFile.close(); + return READ_JSON_FAIL; + } + + amsJson.clear(); + inFile.close(); + HILOG_INFO("read ams config succes!"); + return READ_OK; +} + +} // namespace AAFwk +} // namespace OHOS \ No newline at end of file diff --git a/services/abilitymgr/test/unittest/phone/ability_manager_service_test/ability_manager_service_test.cpp b/services/abilitymgr/test/unittest/phone/ability_manager_service_test/ability_manager_service_test.cpp index 6b943551b96..bca90d0b7e2 100644 --- a/services/abilitymgr/test/unittest/phone/ability_manager_service_test/ability_manager_service_test.cpp +++ b/services/abilitymgr/test/unittest/phone/ability_manager_service_test/ability_manager_service_test.cpp @@ -2555,5 +2555,51 @@ HWTEST_F(AbilityManagerServiceTest, compelverifypermission_001, TestSize.Level1) EXPECT_EQ(resultFunction, ERR_OK); } +/* + * Feature: AbilityManagerService + * Function: AmsConfigurationParameter + * SubFunction: NA + * FunctionPoints: AbilityManagerService CompelVerifyPermission + * EnvConditions: NA + * CaseDescription: Already configured + */ +HWTEST_F(AbilityManagerServiceTest, AmsConfigurationParameter_001, TestSize.Level1) +{ + EXPECT_TRUE(abilityMs_->amsConfigResolver_); + EXPECT_FALSE(abilityMs_->amsConfigResolver_->NonConfigFile()); +} + +/* + * Feature: AbilityManagerService + * Function: AmsConfigurationParameter + * SubFunction: NA + * FunctionPoints: AbilityManagerService CompelVerifyPermission + * EnvConditions: NA + * CaseDescription: Already configured + */ +HWTEST_F(AbilityManagerServiceTest, AmsConfigurationParameter_002, TestSize.Level1) +{ + EXPECT_TRUE(abilityMs_->amsConfigResolver_); + // At present, all three are started and may be changed later + EXPECT_TRUE(abilityMs_->amsConfigResolver_->GetStartLuncherState()); + EXPECT_TRUE(abilityMs_->amsConfigResolver_->GetStatusBarState()); + EXPECT_TRUE(abilityMs_->amsConfigResolver_->GetNavigationBarState()); +} + +/* + * Feature: AbilityManagerService + * Function: AmsConfigurationParameter + * SubFunction: NA + * FunctionPoints: AbilityManagerService CompelVerifyPermission + * EnvConditions: NA + * CaseDescription: Already configured + */ +HWTEST_F(AbilityManagerServiceTest, AmsConfigurationParameter_003, TestSize.Level1) +{ + EXPECT_TRUE(abilityMs_->amsConfigResolver_); + auto ref = abilityMs_->amsConfigResolver_->LoadAmsConfiguration(" "); + EXPECT_EQ(ref, 1); +} + } // namespace AAFwk } // namespace OHOS \ No newline at end of file diff --git a/services/abilitymgr/test/unittest/phone/app_scheduler_test/app_scheduler_test.cpp b/services/abilitymgr/test/unittest/phone/app_scheduler_test/app_scheduler_test.cpp index 0d46d6b69af..cd559e3e2de 100644 --- a/services/abilitymgr/test/unittest/phone/app_scheduler_test/app_scheduler_test.cpp +++ b/services/abilitymgr/test/unittest/phone/app_scheduler_test/app_scheduler_test.cpp @@ -242,6 +242,29 @@ HWTEST_F(AppSchedulerTest, AppScheduler_oprator_008, TestSize.Level0) DelayedSingleton::GetInstance()->MoveToForground(token); } +/* + * Feature: AppScheduler + * Function: MoveToForground + * SubFunction: NA + * FunctionPoints: AppScheduler MoveToForground + * EnvConditions:NA + * CaseDescription: Verify the normal process of movetoforground + */ +HWTEST_F(AppSchedulerTest, AppScheduler_oprator_009, TestSize.Level0) +{ + DelayedSingleton::GetInstance()->appMgrClient_ = std::make_unique(); + + std::string deviceName = "device"; + std::string abilityName = "FirstAbility"; + std::string appName = "FirstApp"; + std::string bundleName = "com.ix.First"; + auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName); + auto record = AbilityRecord::CreateAbilityRecord(abilityReq); + auto token = record->GetToken(); + + DelayedSingleton::GetInstance()->MoveToForground(token); +} + /* * Feature: AppScheduler * Function: MoveToBackground @@ -265,6 +288,32 @@ HWTEST_F(AppSchedulerTest, AppScheduler_oprator_010, TestSize.Level0) DelayedSingleton::GetInstance()->MoveToBackground(token); } +/* + * Feature: AppScheduler + * Function: MoveToBackground GetAbilityState + * SubFunction: NA + * FunctionPoints: AppScheduler MoveToBackground and GetAbilityState + * EnvConditions:NA + * CaseDescription: Verify appmgrclient_ Is not nullptr causes onabilityrequestdone invoke + */ +HWTEST_F(AppSchedulerTest, AppScheduler_oprator_011, TestSize.Level0) +{ + DelayedSingleton::GetInstance()->appMgrClient_ = std::make_unique(); + + std::string deviceName = "device"; + std::string abilityName = "FirstAbility"; + std::string appName = "FirstApp"; + std::string bundleName = "com.ix.First"; + auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName); + auto record = AbilityRecord::CreateAbilityRecord(abilityReq); + auto token = record->GetToken(); + + DelayedSingleton::GetInstance()->MoveToBackground(token); + + EXPECT_EQ( + AppAbilityState::ABILITY_STATE_UNDEFINED, DelayedSingleton::GetInstance()->GetAbilityState()); +} + /* * Feature: AppScheduler * Function: ConvertToAppAbilityState @@ -418,4 +467,4 @@ HWTEST_F(AppSchedulerTest, AppScheduler_oprator_017, TestSize.Level0) DelayedSingleton::GetInstance()->KillProcessByAbilityToken(token); } } // namespace AAFwk -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/services/test/moduletest/ability_mgr_service_test/ability_mgr_module_test.cpp b/services/test/moduletest/ability_mgr_service_test/ability_mgr_module_test.cpp index 511b49308e7..ca01102c0fc 100644 --- a/services/test/moduletest/ability_mgr_service_test/ability_mgr_module_test.cpp +++ b/services/test/moduletest/ability_mgr_service_test/ability_mgr_module_test.cpp @@ -15,6 +15,8 @@ #include #include +#include +#include #include #include @@ -1632,6 +1634,51 @@ HWTEST_F(AbilityMgrModuleTest, ability_mgr_service_test_025, TestSize.Level1) auto resultFunction = abilityMgrServ_->CompelVerifyPermission(permission, pid, uid, message); EXPECT_EQ(resultFunction, 0); } +/* + * Feature: AbilityManagerService + * Function: AmsConfigurationParameter + * SubFunction: NA + * FunctionPoints: AbilityManagerService CompelVerifyPermission + * EnvConditions: NA + * CaseDescription: Judge the acquired properties + */ +HWTEST_F(AbilityMgrModuleTest, AmsConfigurationParameter_026, TestSize.Level1) +{ + EXPECT_TRUE(abilityMgrServ_->amsConfigResolver_); + EXPECT_FALSE(abilityMgrServ_->amsConfigResolver_->NonConfigFile()); + + // Open a path that does not exist + auto ref = abilityMgrServ_->amsConfigResolver_->LoadAmsConfiguration("/system/etc/ams.txt"); + // faild return 1 + EXPECT_EQ(ref, 1); +} + +/* + * Feature: AbilityManagerService + * Function: AmsConfigurationParameter + * SubFunction: NA + * FunctionPoints: AbilityManagerService CompelVerifyPermission + * EnvConditions: NA + * CaseDescription: Judge the acquired properties + */ +HWTEST_F(AbilityMgrModuleTest, AmsConfigurationParameter_027, TestSize.Level1) +{ + bool startLuncher = false; + bool startstatusbar = false; + bool startnavigationbar = false; + nlohmann::json info; + std::ifstream file(AmsConfig::AMS_CONFIG_FILE_PATH, std::ios::in); + if (file.is_open()) { + file >> info; + info.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::STARTUP_LUNCHER).get_to(startLuncher); + info.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::STARTUP_STATUS_BAR).get_to(startstatusbar); + info.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::STARTUP_NAVIGATION_BAR).get_to(startnavigationbar); + } + + EXPECT_EQ(startLuncher, abilityMgrServ_->amsConfigResolver_->GetStartLuncherState()); + EXPECT_EQ(startstatusbar, abilityMgrServ_->amsConfigResolver_->GetStatusBarState()); + EXPECT_EQ(startnavigationbar, abilityMgrServ_->amsConfigResolver_->GetNavigationBarState()); +} } // namespace AAFwk } // namespace OHOS diff --git a/tools/zip/kits/napi/napi_zlib.cpp b/tools/zip/kits/napi/napi_zlib.cpp index 296e8ed7836..bfae464c669 100755 --- a/tools/zip/kits/napi/napi_zlib.cpp +++ b/tools/zip/kits/napi/napi_zlib.cpp @@ -425,9 +425,20 @@ napi_value NAPI_UnzipFile(napi_env env, napi_callback_info info) ret = UnzipFileAsync(env, args, argcAsync, asyncZipCallbackInfo); } else { HILOG_INFO("%{public}s called, wrong number of parameters", __func__); + if (asyncZipCallbackInfo != nullptr) { + delete asyncZipCallbackInfo; + asyncZipCallbackInfo = nullptr; + } return nullptr; } + if (ret == nullptr) { + if (asyncZipCallbackInfo != nullptr) { + delete asyncZipCallbackInfo; + asyncZipCallbackInfo = nullptr; + } + } + return ret; } @@ -508,6 +519,10 @@ void ZipAndUnzipFileAsyncCallBack(std::shared_ptr &zipAceCallb } ZlibCallbackInfo *asyncCallbackInfo = new (std::nothrow) ZlibCallbackInfo(); if (asyncCallbackInfo == nullptr) { + if (work != nullptr) { + delete work; + work = nullptr; + } return; } asyncCallbackInfo->callbackResult = result; @@ -516,7 +531,7 @@ void ZipAndUnzipFileAsyncCallBack(std::shared_ptr &zipAceCallb zipAceCallbackInfo = nullptr; work->data = (void *)asyncCallbackInfo; - uv_queue_work( + int rev = uv_queue_work( loop, work, [](uv_work_t *work) {}, @@ -525,6 +540,10 @@ void ZipAndUnzipFileAsyncCallBack(std::shared_ptr &zipAceCallb // JS Thread ZlibCallbackInfo *asyncCallbackInfo = (ZlibCallbackInfo *)work->data; if (asyncCallbackInfo == nullptr) { + if (work != nullptr) { + delete work; + work = nullptr; + } return; } @@ -548,12 +567,25 @@ void ZipAndUnzipFileAsyncCallBack(std::shared_ptr &zipAceCallb if (asyncCallbackInfo->callback != nullptr) { napi_delete_reference(asyncCallbackInfo->env, asyncCallbackInfo->callback); } + if (asyncCallbackInfo != nullptr) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + } + if (work != nullptr) { + delete work; + work = nullptr; + } + }); + if (rev != 0) { + if (asyncCallbackInfo != nullptr) { delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + } + if (work != nullptr) { delete work; work = nullptr; - asyncCallbackInfo = nullptr; - }); - + } + } return; } diff --git a/tools/zip/src/file_path.cpp b/tools/zip/src/file_path.cpp index 197b1550844..555791d7582 100755 --- a/tools/zip/src/file_path.cpp +++ b/tools/zip/src/file_path.cpp @@ -324,32 +324,32 @@ bool FilePath::AppendRelativePath(const FilePath &child, FilePath *path) } std::vector parentComponentsReverse; - std::vector childComponents_reverse; + std::vector childComponentsReverse; std::vector::reverse_iterator riter; for (riter = parentComponents.rbegin(); riter != parentComponents.rend(); riter++) { parentComponentsReverse.push_back(*riter); } for (riter = childComponents.rbegin(); riter != childComponents.rend(); riter++) { - childComponents_reverse.push_back(*riter); + childComponentsReverse.push_back(*riter); } - std::vector::const_iterator parent_it = parentComponentsReverse.begin(); - std::vector::const_iterator child_it = childComponents_reverse.begin(); - while (parent_it != parentComponentsReverse.end()) { - if (*parent_it != *child_it) + std::vector::const_iterator parentIt = parentComponentsReverse.begin(); + std::vector::const_iterator childIt = childComponentsReverse.begin(); + while (parentIt != parentComponentsReverse.end()) { + if (*parentIt != *childIt) return false; - ++parent_it; - ++child_it; + ++parentIt; + ++childIt; } if (path != nullptr) { // Relative paths do not include separator - if ((child_it != childComponents_reverse.end()) && (*child_it == SEPARATOR)) { - ++child_it; + if ((childIt != childComponentsReverse.end()) && (*childIt == SEPARATOR)) { + ++childIt; } - for (; child_it != childComponents_reverse.end(); child_it++) { - *path = path->Append(*child_it); + for (; childIt != childComponentsReverse.end(); childIt++) { + *path = path->Append(*childIt); } } return true; diff --git a/tools/zip/src/zip.cpp b/tools/zip/src/zip.cpp index ae7ccf8cb81..7c3372726dd 100755 --- a/tools/zip/src/zip.cpp +++ b/tools/zip/src/zip.cpp @@ -148,12 +148,14 @@ bool Zip(const ZipParams ¶ms, const OPTIONS &options, CALLBACK callback) if (params.DestFd() != kInvalidPlatformFile) { zipWriter = ZipWriter::CreateWithFd(params.DestFd(), rootPath); if (!zipWriter) { + CALLING_CALL_BACK(callback, ERROR_CODE_STREAM_ERROR) return false; } } if (!zipWriter) { zipWriter = ZipWriter::Create(params.DestFile(), rootPath); if (!zipWriter) { + CALLING_CALL_BACK(callback, ERROR_CODE_STREAM_ERROR) return false; } } @@ -254,9 +256,11 @@ bool ZipWithFilterCallback(const FilePath &srcDir, const FilePath &destFile, con FilePath srcPath = srcDir; if (!EndsWith(srcPath.Value(), SEPARATOR)) { if (!FilePath::DirectoryExists(srcPath.DirName())) { + CALLING_CALL_BACK(callback, ERROR_CODE_DATA_ERROR) return false; } } else if (!FilePath::DirectoryExists(srcDir)) { + CALLING_CALL_BACK(callback, ERROR_CODE_DATA_ERROR) return false; } ZipParams params(srcDir, destFile); -- Gitee