From 718888bbf5507b86da2b46a682a0538c925ac4e2 Mon Sep 17 00:00:00 2001 From: dy_study Date: Tue, 1 Mar 2022 12:50:34 +0000 Subject: [PATCH] fixed c643ee0 from https://gitee.com/dy_study/aafwk_standard/pulls/923 IssueNo:api7 context interface Description:commit applicationFramework api7 interface about context Sig:SIG_ApplicationFramework Feature or Bugfix: Feature Binary Source: No Signed-off-by: dy_study Change-Id: If783773dfa58139046a075827ea96522d96769f6 --- .../include/ability_context_impl.h | 2 + .../src/ability_context_impl.cpp | 10 + .../kits/ability/native/include/ability.h | 14 + .../ability/native/include/ability_context.h | 13 + .../kits/ability/native/src/ability.cpp | 20 + .../ability/native/src/ability_context.cpp | 10 + .../native/ability_runtime/context/context.h | 14 + .../ability_runtime/context/context_impl.cpp | 10 + .../ability_runtime/context/context_impl.h | 14 + .../kits/appkit/native/app/include/context.h | 14 + .../native/app/include/context_container.h | 14 + .../appkit/native/app/include/context_deal.h | 28 + .../native/app/src/context_container.cpp | 16 + .../appkit/native/app/src/context_deal.cpp | 73 +- .../include/ability_manager_interface.h | 14 + .../abilityManager/napi_ability_manager.cpp | 264 ++++ .../abilityManager/napi_ability_manager.h | 10 + .../aafwk/abilityManager/native_module.cpp | 2 + .../kits/napi/aafwk/featureAbility/BUILD.gn | 3 + .../aafwk/featureAbility/napi_context.cpp | 152 ++- .../napi/aafwk/featureAbility/napi_context.h | 42 + .../napi_data_ability_helper.cpp | 1093 +++++++++++++++++ .../featureAbility/napi_data_ability_helper.h | 2 + .../napi_common/feature_ability_common.h | 15 + .../inner/napi_common/napi_common_ability.cpp | 766 +++++++++++- .../inner/napi_common/napi_common_ability.h | 28 + .../inner/napi_common/napi_common_util.cpp | 78 ++ .../inner/napi_common/napi_common_util.h | 18 + .../kits/napi/aafwk/particleAbility/BUILD.gn | 3 + services/abilitymgr/BUILD.gn | 2 + .../include/ability_manager_proxy.h | 2 + .../include/ability_manager_service.h | 2 + .../abilitymgr/include/ability_manager_stub.h | 2 + .../abilitymgr/src/ability_manager_proxy.cpp | 34 + .../src/ability_manager_service.cpp | 56 + .../abilitymgr/src/ability_manager_stub.cpp | 23 + .../moduletest/ability_record_test/BUILD.gn | 2 + 37 files changed, 2858 insertions(+), 7 deletions(-) diff --git a/frameworks/kits/ability/ability_runtime/include/ability_context_impl.h b/frameworks/kits/ability/ability_runtime/include/ability_context_impl.h index a648d32b42..02354590cd 100644 --- a/frameworks/kits/ability/ability_runtime/include/ability_context_impl.h +++ b/frameworks/kits/ability/ability_runtime/include/ability_context_impl.h @@ -33,6 +33,8 @@ public: std::string GetCacheDir() override; std::string GetTempDir() override; std::string GetFilesDir() override; + bool IsUpdatingConfigurations() override; + bool PrintDrawnCompleted() override; std::string GetDatabaseDir() override; std::string GetStorageDir() override; std::string GetDistributedFilesDir() override; diff --git a/frameworks/kits/ability/ability_runtime/src/ability_context_impl.cpp b/frameworks/kits/ability/ability_runtime/src/ability_context_impl.cpp index f66d7cdb33..7fe1a471cf 100644 --- a/frameworks/kits/ability/ability_runtime/src/ability_context_impl.cpp +++ b/frameworks/kits/ability/ability_runtime/src/ability_context_impl.cpp @@ -64,6 +64,16 @@ std::string AbilityContextImpl::GetDistributedFilesDir() return stageContext_ ? stageContext_->GetDistributedFilesDir() : ""; } +bool AbilityContextImpl::IsUpdatingConfigurations() +{ + return stageContext_ ? stageContext_->IsUpdatingConfigurations() : false; +} + +bool AbilityContextImpl::PrintDrawnCompleted() +{ + return stageContext_ ? stageContext_->PrintDrawnCompleted() : false; +} + void AbilityContextImpl::SwitchArea(int mode) { HILOG_DEBUG("AbilityContextImpl::SwitchArea."); diff --git a/frameworks/kits/ability/native/include/ability.h b/frameworks/kits/ability/native/include/ability.h index 6f4d56544b..6ea00909be 100755 --- a/frameworks/kits/ability/native/include/ability.h +++ b/frameworks/kits/ability/native/include/ability.h @@ -160,6 +160,20 @@ public: */ std::shared_ptr GetResourceManager() const override; + /** + * @brief Checks whether the configuration of this ability is changing. + * + * @return Returns true if the configuration of this ability is changing and false otherwise. + */ + bool IsUpdatingConfigurations() override; + + /** + * @brief Informs the system of the time required for drawing this Page ability. + * + * @return Returns the notification is successful or fail + */ + bool PrintDrawnCompleted() override; + /** * @brief Inflates UI controls by using ComponentContainer. * You can create a ComponentContainer instance that contains multiple components. diff --git a/frameworks/kits/ability/native/include/ability_context.h b/frameworks/kits/ability/native/include/ability_context.h index 7c4ffcf967..c916da960f 100644 --- a/frameworks/kits/ability/native/include/ability_context.h +++ b/frameworks/kits/ability/native/include/ability_context.h @@ -65,6 +65,19 @@ public: */ std::string GetFilesDir() override; + /** + * @brief IsUpdatingConfigurations + * + * @return true or false + */ + bool IsUpdatingConfigurations() override; + + /** + * @brief PrintDrawnCompleted + * + * @return true or false + */ + bool PrintDrawnCompleted() override; /** * @brief Obtains the absolute path which app created and will be excluded from automatic backup to remote storage. * The returned path maybe changed if the application is moved to an adopted storage device. diff --git a/frameworks/kits/ability/native/src/ability.cpp b/frameworks/kits/ability/native/src/ability.cpp index 3098fbe504..0163ce5858 100755 --- a/frameworks/kits/ability/native/src/ability.cpp +++ b/frameworks/kits/ability/native/src/ability.cpp @@ -169,6 +169,26 @@ std::shared_ptr Ability::GetResourceManager() return AbilityContext::GetResourceManager(); } +/** + * @brief Checks whether the configuration of this ability is changing. + * + * @return Returns true if the configuration of this ability is changing and false otherwise. + */ +bool Ability::IsUpdatingConfigurations() +{ + return AbilityContext::IsUpdatingConfigurations(); +} + +/** + * @brief Informs the system of the time required for drawing this Page ability. + * + * @return Returns the notification is successful or fail + */ +bool Ability::PrintDrawnCompleted() +{ + return AbilityContext::PrintDrawnCompleted(); +} + /** * Will be called when ability start. You should override this function * diff --git a/frameworks/kits/ability/native/src/ability_context.cpp b/frameworks/kits/ability/native/src/ability_context.cpp index 89a120c423..f30357e733 100644 --- a/frameworks/kits/ability/native/src/ability_context.cpp +++ b/frameworks/kits/ability/native/src/ability_context.cpp @@ -756,5 +756,15 @@ void AbilityContext::SetShowOnLockScreen(bool isAllow) { ContextContainer::SetShowOnLockScreen(isAllow); } + +bool AbilityContext::IsUpdatingConfigurations() +{ + return ContextContainer::IsUpdatingConfigurations(); +} + +bool AbilityContext::PrintDrawnCompleted() +{ + return ContextContainer::PrintDrawnCompleted(); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/kits/appkit/native/ability_runtime/context/context.h b/frameworks/kits/appkit/native/ability_runtime/context/context.h index 6d7c81be50..76ed697285 100755 --- a/frameworks/kits/appkit/native/ability_runtime/context/context.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/context.h @@ -119,6 +119,20 @@ public: */ virtual std::string GetFilesDir() = 0; + /** + * @brief Checks whether the configuration of this ability is changing. + * + * @return Returns true if the configuration of this ability is changing and false otherwise. + */ + virtual bool IsUpdatingConfigurations() = 0; + + /** + * @brief Informs the system of the time required for drawing this Page ability. + * + * @return Returns the notification is successful or fail + */ + virtual bool PrintDrawnCompleted() = 0; + /** * @brief Obtains the local database path. * If the local database path does not exist, the system creates one and returns the created path. diff --git a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp index b26827017f..732cd45765 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp +++ b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp @@ -85,6 +85,16 @@ std::string ContextImpl::GetCacheDir() return dir; } +bool ContextImpl::IsUpdatingConfigurations() +{ + return false; +} + +bool ContextImpl::PrintDrawnCompleted() +{ + return false; +} + std::string ContextImpl::GetDatabaseDir() { std::string dir; diff --git a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h index bd0e3b5934..2c18c436a5 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h @@ -52,6 +52,20 @@ public: */ std::string GetCacheDir() override; + /** + * @brief Checks whether the configuration of this ability is changing. + * + * @return Returns true if the configuration of this ability is changing and false otherwise. + */ + bool IsUpdatingConfigurations() override; + + /** + * @brief Informs the system of the time required for drawing this Page ability. + * + * @return Returns the notification is successful or fail + */ + bool PrintDrawnCompleted() override; + /** * @brief Obtains the temporary directory. * diff --git a/frameworks/kits/appkit/native/app/include/context.h b/frameworks/kits/appkit/native/app/include/context.h index 96360e5afd..e802af4e42 100644 --- a/frameworks/kits/appkit/native/app/include/context.h +++ b/frameworks/kits/appkit/native/app/include/context.h @@ -718,6 +718,20 @@ public: */ virtual void SetShowOnLockScreen(bool isAllow) = 0; + /** + * @brief Checks whether the configuration of this ability is changing. + * + * @return Returns true if the configuration of this ability is changing and false otherwise. + */ + virtual bool IsUpdatingConfigurations() = 0; + + /** + * @brief Informs the system of the time required for drawing this Page ability. + * + * @return Returns the notification is successful or fail + */ + virtual bool PrintDrawnCompleted() = 0; + friend DataAbilityHelper; friend DataShareHelper; friend ContinuationConnector; diff --git a/frameworks/kits/appkit/native/app/include/context_container.h b/frameworks/kits/appkit/native/app/include/context_container.h index 51827dd13a..3dfc6e05b7 100755 --- a/frameworks/kits/appkit/native/app/include/context_container.h +++ b/frameworks/kits/appkit/native/app/include/context_container.h @@ -72,6 +72,20 @@ public: */ virtual const std::shared_ptr GetAbilityInfo() override; + /** + * @brief Checks whether the configuration of this ability is changing. + * + * @return Returns true if the configuration of this ability is changing and false otherwise. + */ + virtual bool IsUpdatingConfigurations() override; + + /** + * @brief Informs the system of the time required for drawing this Page ability. + * + * @return Returns the notification is successful or fail + */ + virtual bool PrintDrawnCompleted() override; + /** * @brief Obtains the Context object of the application. * diff --git a/frameworks/kits/appkit/native/app/include/context_deal.h b/frameworks/kits/appkit/native/app/include/context_deal.h index 9d3e0bc2dd..71c21c3dda 100755 --- a/frameworks/kits/appkit/native/app/include/context_deal.h +++ b/frameworks/kits/appkit/native/app/include/context_deal.h @@ -174,6 +174,20 @@ public: */ std::string GetCacheDir() override; + /** + * @brief Checks whether the configuration of this ability is changing. + * + * @return Returns true if the configuration of this ability is changing and false otherwise. + */ + bool IsUpdatingConfigurations() override; + + /** + * @brief Informs the system of the time required for drawing this Page ability. + * + * @return Returns the notification is successful or fail + */ + bool PrintDrawnCompleted() override; + /** * @brief Obtains the application-specific code-cache directory on the device's internal storage. * The system will delete any files stored in this location both when your specific application is upgraded, @@ -732,17 +746,30 @@ public: */ AAFwk::LifeCycleStateInfo GetLifeCycleStateInfo() const; public: + static const int64_t CONTEXT_CREATE_BY_SYSTEM_APP; static const std::string CONTEXT_DEAL_FILE_SEPARATOR; static const std::string CONTEXT_DEAL_CODE_CACHE; static const std::string CONTEXT_DEAL_Files; static const std::string CONTEXT_DEAL_NO_BACKUP_Files; static const std::string CONTEXT_DEAL_DIRNAME; + static const std::string CONTEXT_FILE_SEPARATOR; + static const std::string CONTEXT_DISTRIBUTED_BASE_BEFORE; + static const std::string CONTEXT_DISTRIBUTED_BASE_MIDDLE; + static const std::string CONTEXT_DISTRIBUTED; + static const std::string CONTEXT_DATA_STORAGE; + static const std::string CONTEXT_ELS[]; + static const int EL_DEFAULT = 1; + int flags_ = 0x00000000; protected: sptr GetToken() override; bool HapModuleInfoRequestInit(); private: + bool IsCreateBySystemApp() const; + int GetCurrentAccountId() const; + void CreateDirIfNotExist(const std::string& dirPath) const; + std::shared_ptr processInfo_ = nullptr; std::shared_ptr applicationInfo_ = nullptr; std::shared_ptr abilityInfo_ = nullptr; @@ -760,6 +787,7 @@ private: std::shared_ptr mainEventRunner_; std::shared_ptr hapModuleInfoLocal_ = nullptr; bool isCreateBySystemApp_ = false; + std::string currArea_ = CONTEXT_ELS[EL_DEFAULT]; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/kits/appkit/native/app/src/context_container.cpp b/frameworks/kits/appkit/native/app/src/context_container.cpp index a63cb549b9..bc744977f8 100644 --- a/frameworks/kits/appkit/native/app/src/context_container.cpp +++ b/frameworks/kits/appkit/native/app/src/context_container.cpp @@ -1130,5 +1130,21 @@ void ContextContainer::SetShowOnLockScreen(bool isAllow) baseContext_->SetShowOnLockScreen(isAllow); APP_LOGI("ContextContainer::SetShowOnLockScreen called end."); } + +bool ContextContainer::IsUpdatingConfigurations() +{ + if (baseContext_ != nullptr) { + return baseContext_->IsUpdatingConfigurations(); + } + return false; +} + +bool ContextContainer::PrintDrawnCompleted() +{ + if (baseContext_ != nullptr) { + return baseContext_->PrintDrawnCompleted(); + } + return false; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/kits/appkit/native/app/src/context_deal.cpp b/frameworks/kits/appkit/native/app/src/context_deal.cpp index d495386d0b..4f0f3835a5 100644 --- a/frameworks/kits/appkit/native/app/src/context_deal.cpp +++ b/frameworks/kits/appkit/native/app/src/context_deal.cpp @@ -25,6 +25,7 @@ #include "directory_ex.h" #include "file_ex.h" #include "iservice_registry.h" +#include "os_account_manager.h" #include "spec_task_dispatcher.h" #include "sys_mgr_client.h" #include "system_ability_definition.h" @@ -40,6 +41,13 @@ const std::string ContextDeal::CONTEXT_DEAL_CODE_CACHE("code_cache"); const std::string ContextDeal::CONTEXT_DEAL_Files("files"); const std::string ContextDeal::CONTEXT_DEAL_NO_BACKUP_Files("no_backup"); const std::string ContextDeal::CONTEXT_DEAL_DIRNAME("preferences"); +const int64_t ContextDeal::CONTEXT_CREATE_BY_SYSTEM_APP(0x00000001); +const std::string ContextDeal::CONTEXT_FILE_SEPARATOR("/"); +const std::string ContextDeal::CONTEXT_DISTRIBUTED_BASE_BEFORE("/mnt/hmdfs/"); +const std::string ContextDeal::CONTEXT_DISTRIBUTED_BASE_MIDDLE("/device_view/local/data/"); +const std::string ContextDeal::CONTEXT_DISTRIBUTED("distributedfiles"); +const std::string ContextDeal::CONTEXT_DATA_STORAGE("/data/storage/"); +const std::string ContextDeal::CONTEXT_ELS[] = {"el1", "el2", "el3", "el4"}; ContextDeal::ContextDeal(bool isCreateBySystemApp) : isCreateBySystemApp_(isCreateBySystemApp) {} @@ -309,6 +317,16 @@ std::string ContextDeal::GetCacheDir() return (applicationInfo_ != nullptr) ? applicationInfo_->cacheDir : ""; } +bool ContextDeal::IsUpdatingConfigurations() +{ + return false; +} + +bool ContextDeal::PrintDrawnCompleted() +{ + return false; +} + /** * @brief Obtains the application-specific code-cache directory on the device's internal storage. * The system will delete any files stored in this location both when your specific application is upgraded, @@ -621,6 +639,31 @@ int ContextDeal::VerifyPermission(const std::string &permission, int pid, int ui return 0; } +bool ContextDeal::IsCreateBySystemApp() const +{ + return (static_cast(flags_) & CONTEXT_CREATE_BY_SYSTEM_APP) == 1; +} + +int ContextDeal::GetCurrentAccountId() const +{ + int userId = 0; + AccountSA::OsAccountManager::GetOsAccountLocalIdFromProcess(userId); + return userId; +} + +void ContextDeal::CreateDirIfNotExist(const std::string &dirPath) const +{ + APP_LOGI("CreateDirIfNotExist: create directory if not exists."); + if (!OHOS::FileExists(dirPath)) { + APP_LOGI("ContextDeal::CreateDirIfNotExist File is not exits"); + bool createDir = OHOS::ForceCreateDirectory(dirPath); + if (!createDir) { + APP_LOGI("CreateDirIfNotExist: create dir %{public}s failed.", dirPath.c_str()); + return; + } + } +} + /** * @brief Obtains the distributed file path. * If the distributed file path does not exist, the system creates one and returns the created path. This method is @@ -630,7 +673,18 @@ int ContextDeal::VerifyPermission(const std::string &permission, int pid, int ui */ std::string ContextDeal::GetDistributedDir() { - return ""; + APP_LOGI("ContextImpl::GetDistributedDir"); + std::string dir; + if (IsCreateBySystemApp()) { + dir = CONTEXT_DISTRIBUTED_BASE_BEFORE + std::to_string(GetCurrentAccountId()) + + CONTEXT_DISTRIBUTED_BASE_MIDDLE + GetBundleName(); + } else { + dir = CONTEXT_DATA_STORAGE + currArea_ + CONTEXT_FILE_SEPARATOR + CONTEXT_DISTRIBUTED + CONTEXT_FILE_SEPARATOR + + ((GetHapModuleInfo() == nullptr) ? "" : GetHapModuleInfo()->moduleName); + } + CreateDirIfNotExist(dir); + APP_LOGI("ContextImpl::GetDistributedDir:%{public}s", dir.c_str()); + return dir; } /** * @brief Sets the pattern of this Context based on the specified pattern ID. @@ -677,7 +731,22 @@ std::shared_ptr ContextDeal::GetHapModuleInfo() if (hapModuleInfoLocal_ == nullptr) { HapModuleInfoRequestInit(); } - + + sptr ptr = GetBundleManager(); + if (ptr == nullptr) { + APP_LOGE("GetAppType failed to get bundle manager service"); + return hapModuleInfoLocal_; + } + Want want; + ElementName name; + name.SetBundleName(GetBundleName()); + want.SetElement(name); + std::vector abilityInfos; + bool isSuc = ptr->QueryAbilityInfos(want, abilityInfos); + if (isSuc) { + hapModuleInfoLocal_->abilityInfos = abilityInfos; + } + APP_LOGI("ContextDeal::GetHapModuleInfo end"); return hapModuleInfoLocal_; } diff --git a/interfaces/innerkits/ability_manager/include/ability_manager_interface.h b/interfaces/innerkits/ability_manager/include/ability_manager_interface.h index 5a72c29893..ea33c2478c 100644 --- a/interfaces/innerkits/ability_manager/include/ability_manager_interface.h +++ b/interfaces/innerkits/ability_manager/include/ability_manager_interface.h @@ -122,6 +122,16 @@ public: int32_t userId = DEFAULT_INVAL_VALUE, int requestCode = DEFAULT_INVAL_VALUE) = 0; + virtual int GetAppMemorySize() + { + return 0; + } + + virtual bool IsRamConstrainedDevice() + { + return false; + } + /** * TerminateAbility, terminate the special ability. * @@ -938,6 +948,10 @@ public: GET_SYSTEM_MEMORY_ATTR, + GET_APP_MEMORY_SIZE, + + IS_RAM_CONSTRAINED_DEVICE, + GET_ABILITY_RUNNING_INFO, GET_EXTENSION_RUNNING_INFO, diff --git a/interfaces/kits/napi/aafwk/abilityManager/napi_ability_manager.cpp b/interfaces/kits/napi/aafwk/abilityManager/napi_ability_manager.cpp index 8f6613b9fe..4aa93b8b61 100644 --- a/interfaces/kits/napi/aafwk/abilityManager/napi_ability_manager.cpp +++ b/interfaces/kits/napi/aafwk/abilityManager/napi_ability_manager.cpp @@ -2239,5 +2239,269 @@ napi_value NAPI_GetSystemMemoryAttr(napi_env env, napi_callback_info info) HILOG_INFO("%{public}s end", __func__); return ret; } + +static void GetAppMemorySizeExecute(napi_env env, void *data) +{ + CallbackInfo *cb = static_cast(data); + cb->result = GetAbilityManagerInstance()->GetAppMemorySize(); + HILOG_INFO("%{public}s, result = %{public}d", __func__, cb->result); +} + +static void GetAppMemorySizePromiseComplete(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s, main event thread complete.", __func__); + CallbackInfo *cb = static_cast(data); + if (cb == nullptr) { + HILOG_ERROR("%{public}s, main event thread complete end.", __func__); + return; + } + napi_value result = nullptr; + napi_create_int32(env, cb->result, &result); + NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, cb->deferred, result)); + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, cb->asyncWork)); + if (cb != nullptr) { + delete cb; + cb = nullptr; + } + HILOG_INFO("%{public}s, main event thread complete end.", __func__); +} + +static void GetAppMemorySizeAsyncComplete(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s, main event thread complete.", __func__); + CallbackInfo *cb = static_cast(data); + if (cb == nullptr) { + HILOG_ERROR("%{public}s, main event thread complete end.", __func__); + return; + } + const int errorCodeFailed = -1; + const int errorCodeSuccess = 0; + const unsigned int argsCount = 2; + const unsigned int paramFirst = 0; + const unsigned int paramSecond = 1; + napi_value result[argsCount] = {nullptr}; + if (cb->result == errorCodeFailed) { + napi_create_int32(env, 1, &result[paramFirst]); + napi_create_int32(env, cb->result, &result[paramSecond]); + } else { + napi_create_int32(env, errorCodeSuccess, &result[paramFirst]); + napi_create_int32(env, cb->result, &result[paramSecond]); + } + napi_value undefined = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + napi_value callback = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, cb->callback, &callback)); + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, argsCount, result, &callResult)); + if (cb->callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, cb->callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, cb->asyncWork)); + if (cb != nullptr) { + delete cb; + cb = nullptr; + } + HILOG_INFO("%{public}s, main event thread complete end.", __func__); +} + +static napi_value GetAppMemorySizePromise(napi_env env) +{ + napi_value resourceName = nullptr; + napi_value retPromise = nullptr; + CallbackInfo *cb = new (std::nothrow) CallbackInfo; + if (cb == nullptr) { + HILOG_INFO("%{public}s, promise cb new failed", __func__); + NAPI_CALL(env, napi_get_null(env, &retPromise)); + return retPromise; + } + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + NAPI_CALL(env, napi_create_promise(env, &cb->deferred, &retPromise)); + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, GetAppMemorySizeExecute, + GetAppMemorySizePromiseComplete, (void *)cb, &cb->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, cb->asyncWork)); + HILOG_INFO("%{public}s, promise end", __func__); + return retPromise; +} + +static napi_value GetAppMemorySizeAsync(napi_env env, napi_value args) +{ + napi_value resourceName = nullptr; + napi_value retAsync = nullptr; + napi_valuetype valuetype = napi_undefined; + CallbackInfo *cb = new (std::nothrow) CallbackInfo; + if (cb == nullptr) { + HILOG_ERROR("%{public}s, async cb new failed", __func__); + NAPI_CALL(env, napi_get_null(env, &retAsync)); + return retAsync; + } + + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + NAPI_CALL(env, napi_typeof(env, args, &valuetype)); + if (valuetype != napi_function) { + return retAsync; + } + NAPI_CALL(env, napi_create_reference(env, args, 1, &cb->callback)); + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, GetAppMemorySizeExecute, + GetAppMemorySizeAsyncComplete, (void *)cb, &cb->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, cb->asyncWork)); + HILOG_INFO("%{public}s, async end", __func__); + NAPI_CALL(env, napi_create_int32(env, 0, &retAsync)); + return retAsync; +} + +napi_value NAPI_GetAppMemorySize(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value argv[1] = {nullptr}; + napi_value ret = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); + HILOG_INFO("%{public}s argc = [%{public}zu]", __func__, argc); + if (argc == 0) { + // promiss + ret = GetAppMemorySizePromise(env); + } else if (argc == 1) { + // async + ret = GetAppMemorySizeAsync(env, argv[0]); + } else { + HILOG_ERROR("%{public}s js input param error", __func__); + NAPI_CALL(env, napi_get_null(env, &ret)); + } + HILOG_INFO("%{public}s end", __func__); + return ret; +} + +static void IsRamConstrainedDeviceExecute(napi_env env, void *data) +{ + CallbackInfo *cb = static_cast(data); + cb->isRamConstrainedDevice = GetAbilityManagerInstance()->IsRamConstrainedDevice(); + if (cb->isRamConstrainedDevice) { + HILOG_ERROR("%{public}s, true", __func__); + } else { + HILOG_ERROR("%{public}s, false", __func__); + } +} + +static void IsRamConstrainedDevicePromiseComplete(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s, main event thread complete.", __func__); + CallbackInfo *cb = static_cast(data); + if (cb == nullptr) { + HILOG_ERROR("%{public}s, main event thread complete end.", __func__); + return; + } + napi_value result = nullptr; + napi_get_boolean(env, cb->isRamConstrainedDevice, &result); + NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, cb->deferred, result)); + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, cb->asyncWork)); + if (cb != nullptr) { + delete cb; + cb = nullptr; + } + HILOG_INFO("%{public}s, main event thread complete end.", __func__); +} + +static napi_value IsRamConstrainedDevicePromise(napi_env env) +{ + napi_value resourceName = nullptr; + napi_value retPromise = nullptr; + CallbackInfo *cb = new (std::nothrow) CallbackInfo; + if (cb == nullptr) { + HILOG_INFO("%{public}s, promise cb new failed", __func__); + NAPI_CALL(env, napi_get_null(env, &retPromise)); + return retPromise; + } + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + NAPI_CALL(env, napi_create_promise(env, &cb->deferred, &retPromise)); + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, IsRamConstrainedDeviceExecute, + IsRamConstrainedDevicePromiseComplete, (void *)cb, &cb->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, cb->asyncWork)); + HILOG_INFO("%{public}s, promise end", __func__); + return retPromise; +} + +static void IsRamConstrainedDeviceAsyncComplete(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s, main event thread complete.", __func__); + CallbackInfo *cb = static_cast(data); + if (cb == nullptr) { + HILOG_ERROR("%{public}s, main event thread complete end.", __func__); + return; + } + const int errorCodeFailed = 1; + const int errorCodeSuccess = 0; + const unsigned int argsCount = 2; + const unsigned int paramFirst = 0; + const unsigned int paramSecond = 1; + napi_value result[argsCount] = {nullptr}; + if (cb->isRamConstrainedDevice) { + napi_create_int32(env, errorCodeSuccess, &result[paramFirst]); + napi_get_boolean(env, cb->isRamConstrainedDevice, &result[paramSecond]); + } else { + napi_create_int32(env, errorCodeFailed, &result[paramFirst]); + napi_get_boolean(env, cb->isRamConstrainedDevice, &result[paramSecond]); + } + napi_value undefined = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + napi_value callback = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, cb->callback, &callback)); + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, argsCount, result, &callResult)); + if (cb->callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, cb->callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, cb->asyncWork)); + if (cb != nullptr) { + delete cb; + cb = nullptr; + } + HILOG_INFO("%{public}s, main event thread complete end.", __func__); +} + +static napi_value IsRamConstrainedDeviceAsync(napi_env env, napi_value args) +{ + napi_value resourceName = nullptr; + napi_value retAsync = nullptr; + napi_valuetype valuetype = napi_undefined; + CallbackInfo *cb = new (std::nothrow) CallbackInfo; + if (cb == nullptr) { + HILOG_ERROR("%{public}s, async cb new failed", __func__); + NAPI_CALL(env, napi_get_null(env, &retAsync)); + return retAsync; + } + + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + NAPI_CALL(env, napi_typeof(env, args, &valuetype)); + if (valuetype != napi_function) { + return retAsync; + } + NAPI_CALL(env, napi_create_reference(env, args, 1, &cb->callback)); + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, IsRamConstrainedDeviceExecute, + IsRamConstrainedDeviceAsyncComplete, (void *)cb, &cb->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, cb->asyncWork)); + HILOG_INFO("%{public}s, async end", __func__); + NAPI_CALL(env, napi_create_int32(env, 0, &retAsync)); + return retAsync; +} + +napi_value NAPI_IsRamConstrainedDevice(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value argv[1] = {nullptr}; + napi_value ret = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); + HILOG_INFO("%{public}s argc = [%{public}zu]", __func__, argc); + if (argc == 0) { + // promiss + ret = IsRamConstrainedDevicePromise(env); + } else if (argc == 1) { + // async + ret = IsRamConstrainedDeviceAsync(env, argv[0]); + } else { + HILOG_ERROR("%{public}s js input param error", __func__); + NAPI_CALL(env, napi_get_null(env, &ret)); + } + HILOG_INFO("%{public}s end", __func__); + return ret; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/kits/napi/aafwk/abilityManager/napi_ability_manager.h b/interfaces/kits/napi/aafwk/abilityManager/napi_ability_manager.h index 63ef91ff3c..4a6a6dbc27 100644 --- a/interfaces/kits/napi/aafwk/abilityManager/napi_ability_manager.h +++ b/interfaces/kits/napi/aafwk/abilityManager/napi_ability_manager.h @@ -143,6 +143,14 @@ struct SystemMemroyInfoCB { napi_ref callback = nullptr; }; +struct CallbackInfo { + napi_async_work asyncWork = nullptr; + napi_deferred deferred = nullptr; + napi_ref callback = nullptr; + int result = -1; + bool isRamConstrainedDevice = false; +}; + napi_value NAPI_GetAllRunningProcesses(napi_env env, napi_callback_info info); napi_value NAPI_GetActiveProcessInfos(napi_env env, napi_callback_info info); napi_value NAPI_QueryRunningAbilityMissionInfos(napi_env env, napi_callback_info info); @@ -160,6 +168,8 @@ void CreateWeightReasonCodeObject(napi_env env, napi_value value); napi_value GetCallbackErrorValue(napi_env env, int errCode); napi_value NapiGetNull(napi_env env); napi_value NAPI_GetSystemMemoryAttr(napi_env env, napi_callback_info); +napi_value NAPI_GetAppMemorySize(napi_env env, napi_callback_info info); +napi_value NAPI_IsRamConstrainedDevice(napi_env env, napi_callback_info info); } // namespace AppExecFwk } // namespace OHOS #endif // NAPI_ABILITY_MANAGER_H diff --git a/interfaces/kits/napi/aafwk/abilityManager/native_module.cpp b/interfaces/kits/napi/aafwk/abilityManager/native_module.cpp index 63dbcc6530..088ba3be01 100644 --- a/interfaces/kits/napi/aafwk/abilityManager/native_module.cpp +++ b/interfaces/kits/napi/aafwk/abilityManager/native_module.cpp @@ -56,6 +56,8 @@ static napi_value Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getAbilityMissionSnapshot", NAPI_GetAbilityMissionSnapshot), DECLARE_NAPI_PROPERTY("WeightReasonCode", nWeightReasonCode), DECLARE_NAPI_FUNCTION("getSystemMemoryAttr", NAPI_GetSystemMemoryAttr), + DECLARE_NAPI_FUNCTION("getAppMemorySize", NAPI_GetAppMemorySize), + DECLARE_NAPI_FUNCTION("isRamConstrainedDevice", NAPI_IsRamConstrainedDevice), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); diff --git a/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn b/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn index a8503a0897..8c007a5842 100644 --- a/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn +++ b/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn @@ -25,6 +25,8 @@ ohos_shared_library("featureability") { "//foundation/distributeddatamgr/appdatamgr/frameworks/jskitsimpl/common/include", "//foundation/distributeddatamgr/appdatamgr/frameworks/jskitsimpl/napi_dataability/include", "//foundation/distributeddatamgr/appdatamgr/frameworks/jskitsimpl/napi_resultset/include", + "//third_party/jsoncpp", + "//third_party/jsoncpp/include/json", ] sources = [ @@ -49,6 +51,7 @@ ohos_shared_library("featureability") { "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/inner/napi_common:napi_common", "//foundation/ace/napi:ace_napi", "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + "//third_party/jsoncpp:jsoncpp", "//third_party/libuv:uv_static", "//utils/native/base:utils", ] diff --git a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp index 6f73e78312..2ae57a00d0 100644 --- a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp @@ -498,7 +498,7 @@ void CallOnRequestPermissionsFromUserResult(int requestCode, const std::vector #include #include +#include #include "data_ability_helper.h" #include "data_ability_observer_interface.h" @@ -33,6 +34,7 @@ #include "napi_rdb_predicates.h" #include "napi_result_set.h" #include "securec.h" +#include "json.h" using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; @@ -68,6 +70,7 @@ napi_value DataAbilityHelperInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("denormalizeUri", NAPI_DenormalizeUri), DECLARE_NAPI_FUNCTION("release", NAPI_Release), DECLARE_NAPI_FUNCTION("executeBatch", NAPI_ExecuteBatch), + DECLARE_NAPI_FUNCTION("call", NAPI_Call), }; napi_value constructor; @@ -2449,6 +2452,1096 @@ void UpdatePromiseCompleteCB(napi_env env, napi_status status, void *data) HILOG_INFO("NAPI_Update, main event thread complete end."); } +void Stringsplit(const string &str, const string &splits, std::vector &res) +{ + if (str == "") { + return; + } + string strs = str + splits; + size_t pos = strs.find(splits); + int step = splits.size(); + + while (pos != strs.npos) { + string temp = strs.substr(0, pos); + HILOG_INFO("Stringsplit temp:%{public}s", temp.c_str()); + res.push_back(temp); + strs = strs.substr(pos + step, strs.size()); + pos = strs.find(splits); + } +} + +void SetPacMapObject(AppExecFwk::PacMap &pacMap, const napi_env &env, std::string keyStr, napi_value value) +{ + napi_valuetype valueType = napi_undefined; + napi_typeof(env, value, &valueType); + HILOG_INFO("ValueObject--------> type:%{public}d", valueType); + if (valueType == napi_string) { + std::string valueString = UnwrapStringFromJS(env, value); + HILOG_INFO("ValueObject type:%{public}d, key:%{public}s, value:%{public}s", valueType, keyStr.c_str(), + valueString.c_str()); + pacMap.PutStringValue(keyStr, valueString); + } else if (valueType == napi_number) { + double valueNumber = 0; + napi_get_value_double(env, value, &valueNumber); + pacMap.PutDoubleValue(keyStr, valueNumber); + HILOG_INFO( + "ValueObject type:%{public}d, key:%{public}s, value:%{public}lf", valueType, keyStr.c_str(), valueNumber); + } else if (valueType == napi_boolean) { + bool valueBool = false; + napi_get_value_bool(env, value, &valueBool); + HILOG_INFO( + "ValueObject type:%{public}d, key:%{public}s, value:%{public}d", valueType, keyStr.c_str(), valueBool); + pacMap.PutBooleanValue(keyStr, valueBool); + } else if (valueType == napi_null) { + pacMap.PutObject(keyStr, nullptr); + HILOG_INFO("ValueObject type:%{public}d, key:%{public}s, value:null", valueType, keyStr.c_str()); + } else if (valueType == napi_object) { + HILOG_INFO("ValueObject type:%{public}d, key:%{public}s, value:Uint8Array", valueType, keyStr.c_str()); + pacMap.PutStringValueArray(keyStr, ConvertStringVector(env, value)); + } else { + HILOG_ERROR("pacMap error"); + } +} + +void AnalysisPacMap(AppExecFwk::PacMap &pacMap, const napi_env &env, const napi_value &arg) +{ + napi_value keys = 0; + napi_get_property_names(env, arg, &keys); + uint32_t arrLen = 0; + napi_status status = napi_get_array_length(env, keys, &arrLen); + if (status != napi_ok) { + HILOG_ERROR("PacMap errr"); + return; + } + HILOG_INFO("PacMap num:%{public}d ", arrLen); + for (size_t i = 0; i < arrLen; ++i) { + napi_value key = 0; + status = napi_get_element(env, keys, i, &key); + std::string keyStr = UnwrapStringFromJS(env, key); + HILOG_INFO("PacMap keyStr:%{public}s ", keyStr.c_str()); + napi_value value = 0; + napi_get_property(env, arg, key, &value); + + SetPacMapObject(pacMap, env, keyStr, value); + } +} + +void ParseEqualTo(AppExecFwk::PacMap &pacMap, std::string &value_str, NativeRdb::DataAbilityPredicates &predicates) +{ + std::string equalValue = pacMap.GetStringValue(value_str, ""); + HILOG_INFO("ParseJsonKey equalTo equalValue1:%{public}s ", equalValue.c_str()); + if (equalValue != "") { + if (equalValue.find(",") == string::npos) { + predicates.EqualTo(value_str, equalValue); + pacMap.Remove(value_str); + } else { + std::vector value_vec; + Stringsplit(equalValue, ",", value_vec); + predicates.EqualTo(value_str, value_vec[0]); + pacMap.Remove(value_str); + pacMap.PutStringValue(value_str, equalValue.substr(value_vec[0].length() + 1, equalValue.length())); + HILOG_INFO("ParseJsonKey notEqualTo equalValue2:%{public}s ", + equalValue.substr(value_vec[0].length() + 1, equalValue.length()).c_str()); + } + } else { + double equalDoubleValue = pacMap.GetDoubleValue(value_str, DBL_MIN); + predicates.EqualTo(value_str, std::to_string(equalDoubleValue)); + HILOG_INFO("ParseJsonKey notEqualTo equalValue3:%{public}s ", + std::to_string(pacMap.GetDoubleValue(value_str, DBL_MIN)).c_str()); + pacMap.Remove(value_str); + } +} + +void ParseNotEqualTo(AppExecFwk::PacMap &pacMap, std::string &value_str, NativeRdb::DataAbilityPredicates &predicates) +{ + std::string equalValue = pacMap.GetStringValue(value_str, "123##*##abc"); + HILOG_INFO("ParseJsonKey notEqualTo equalValue1:%{public}s ", equalValue.c_str()); + if (equalValue != "123##*##abc") { + if (equalValue.find(",") == string::npos) { + predicates.NotEqualTo(value_str, equalValue); + pacMap.Remove(value_str); + } else { + std::vector value_vec; + Stringsplit(equalValue, ",", value_vec); + predicates.NotEqualTo(value_str, value_vec[0]); + pacMap.Remove(value_str); + pacMap.PutStringValue(value_str, equalValue.substr(value_vec[0].length() + 1, equalValue.length())); + HILOG_INFO("ParseJsonKey notEqualTo equalValue2:%{public}s ", + equalValue.substr(value_vec[0].length() + 1, equalValue.length()).c_str()); + } + } else { + predicates.NotEqualTo(value_str, std::to_string(pacMap.GetDoubleValue(value_str, DBL_MIN))); + HILOG_INFO("ParseJsonKey notEqualTo equalValue3:%{public}s ", + std::to_string(pacMap.GetDoubleValue(value_str, DBL_MIN)).c_str()); + pacMap.Remove(value_str); + } +} + +void ParseBetween(AppExecFwk::PacMap &pacMap, std::string &value_str, NativeRdb::DataAbilityPredicates &predicates) +{ + std::string betweenValue = pacMap.GetStringValue(value_str, ""); + HILOG_INFO("ParseJsonKey between betweenValue1:%{public}s ", betweenValue.c_str()); + std::vector value_vec; + Stringsplit(betweenValue, ",", value_vec); + predicates.Between(value_str, value_vec[0], value_vec[1]); + int tempLength = value_vec[0].length() + 1 + value_vec[1].length() + 1; + value_vec.erase(value_vec.begin(), value_vec.begin() + 1); + pacMap.Remove(value_str); + if (value_vec.size() == 1) { + pacMap.PutDoubleValue(value_str, stod(value_vec[0])); + HILOG_INFO("ParseJsonKey between betweenValue2:%{public}s ", value_vec[0].c_str()); + } else if (value_vec.size() > 1) { + pacMap.PutStringValue(value_str, betweenValue.substr(tempLength, betweenValue.length())); + HILOG_INFO("ParseJsonKey between betweenValue3:%{public}s ", + betweenValue.substr(tempLength, betweenValue.length()).c_str()); + } +} + +void ParseNotBetween(AppExecFwk::PacMap &pacMap, std::string &value_str, NativeRdb::DataAbilityPredicates &predicates) +{ + std::string betweenValue = pacMap.GetStringValue(value_str, ""); + HILOG_INFO("ParseJsonKey notBetween betweenValue1:%{public}s ", betweenValue.c_str()); + std::vector value_vec; + Stringsplit(betweenValue, ",", value_vec); + predicates.NotBetween(value_str, value_vec[0], value_vec[1]); + int tempLength = value_vec[0].length() + 1 + value_vec[1].length() + 1; + value_vec.erase(value_vec.begin(), value_vec.begin() + 1); + pacMap.Remove(value_str); + if (value_vec.size() == 1) { + pacMap.PutDoubleValue(value_str, stod(value_vec[0])); + HILOG_INFO("ParseJsonKey notBetween betweenValue2:%{public}s ", value_vec[0].c_str()); + } else if (value_vec.size() > 1) { + pacMap.PutStringValue(value_str, betweenValue.substr(tempLength, betweenValue.length())); + HILOG_INFO("ParseJsonKey notBetween betweenValue3:%{public}s ", + betweenValue.substr(tempLength, betweenValue.length()).c_str()); + } +} + +void ParseIn(AppExecFwk::PacMap &pacMap, std::string &value_str, NativeRdb::DataAbilityPredicates &predicates) +{ + std::string inValue = pacMap.GetStringValue(value_str, ""); + HILOG_INFO("ParseJsonKey in value_str1:%{public}s ", inValue.c_str()); + std::vector value_vec; + Stringsplit(inValue, ",", value_vec); + if (value_vec.size() >= ARGS_TWO) { + std::vector in_vec; + in_vec.push_back(value_vec[0]); + in_vec.push_back(value_vec[1]); + predicates.In(value_str, in_vec); + int tempLength = value_vec[0].length() + 1 + value_vec[1].length() + 1; + value_vec.erase(value_vec.begin(), value_vec.begin() + 1); + pacMap.Remove(value_str); + if (value_vec.size() == 1) { + pacMap.PutDoubleValue(value_str, stod(value_vec[0])); + HILOG_INFO("ParseJsonKey in value_str2:%{public}s ", value_vec[0].c_str()); + } else if (value_vec.size() > 1) { + pacMap.PutStringValue(value_str, inValue.substr(tempLength, inValue.length())); + HILOG_INFO("ParseJsonKey in value_str3:%{public}s ", inValue.substr(tempLength, inValue.length()).c_str()); + } + } else { + pacMap.Remove(value_str); + } +} + +void ParseNotIn(AppExecFwk::PacMap &pacMap, std::string &value_str, NativeRdb::DataAbilityPredicates &predicates) +{ + std::string inValue = pacMap.GetStringValue(value_str, ""); + HILOG_INFO("ParseJsonKey notIn value_str1:%{public}s ", inValue.c_str()); + std::vector value_vec; + Stringsplit(inValue, ",", value_vec); + if (value_vec.size() >= ARGS_TWO) { + std::vector in_vec; + in_vec.push_back(value_vec[0]); + in_vec.push_back(value_vec[1]); + predicates.NotIn(value_str, in_vec); + int tempLength = value_vec[0].length() + 1 + value_vec[1].length() + 1; + value_vec.erase(value_vec.begin(), value_vec.begin() + 1); + pacMap.Remove(value_str); + if (value_vec.size() == 1) { + pacMap.PutDoubleValue(value_str, stod(value_vec[0])); + HILOG_INFO("ParseJsonKey notIn value_str2:%{public}s ", value_vec[0].c_str()); + } else if (value_vec.size() > 1) { + pacMap.PutStringValue(value_str, inValue.substr(tempLength, inValue.length())); + HILOG_INFO( + "ParseJsonKey notIn value_str3:%{public}s ", inValue.substr(tempLength, inValue.length()).c_str()); + } + } else { + pacMap.Remove(value_str); + } +} + +void ParseNormal(AppExecFwk::PacMap &pacMap, std::string &value_str, NativeRdb::DataAbilityPredicates &predicates, + std::string key_str) +{ + if (key_str == "equalTo") { + ParseEqualTo(pacMap, value_str, predicates); + } else if (key_str == "notEqualTo") { + ParseNotEqualTo(pacMap, value_str, predicates); + } else if (key_str == "contains") { + predicates.Contains(value_str, pacMap.GetStringValue(value_str, "")); + HILOG_INFO("ParseJsonKey contains :%{public}s ", pacMap.GetStringValue(value_str, "").c_str()); + pacMap.Remove(value_str); + } else if (key_str == "beginsWith") { + predicates.BeginsWith(value_str, pacMap.GetStringValue(value_str, "")); + HILOG_INFO("ParseJsonKey beginsWith :%{public}s ", pacMap.GetStringValue(value_str, "").c_str()); + pacMap.Remove(value_str); + } else if (key_str == "endsWith") { + predicates.EndsWith(value_str, pacMap.GetStringValue(value_str, "")); + HILOG_INFO("ParseJsonKey endsWithValue1:%{public}s ", pacMap.GetStringValue(value_str, "").c_str()); + pacMap.Remove(value_str); + } else if (key_str == "greaterThan") { + double greateValue = pacMap.GetDoubleValue(value_str, DBL_MIN); + predicates.GreaterThan(value_str, std::to_string(greateValue)); + pacMap.Remove(value_str); + } else if (key_str == "lessThan") { + double lessValue = pacMap.GetDoubleValue(value_str, DBL_MAX); + predicates.LessThan(value_str, std::to_string(lessValue)); + pacMap.Remove(value_str); + } else if (key_str == "greaterThanOrEqualTo") { + double greateValue = pacMap.GetDoubleValue(value_str, DBL_MIN); + predicates.GreaterThanOrEqualTo(value_str, std::to_string(greateValue)); + pacMap.Remove(value_str); + } else if (key_str == "lessThanOrEqualTo") { + double lessValue = pacMap.GetDoubleValue(value_str, DBL_MAX); + predicates.LessThanOrEqualTo(value_str, std::to_string(lessValue)); + pacMap.Remove(value_str); + } +} + +void ParseUnNormal(AppExecFwk::PacMap &pacMap, std::string &value_str, NativeRdb::DataAbilityPredicates &predicates, + std::string key_str) +{ + if (key_str == "beginWrap") { + predicates.BeginWrap(); + } else if (key_str == "endWrap") { + predicates.EndWrap(); + } else if (key_str == "or") { + predicates.Or(); + } else if (key_str == "and") { + predicates.And(); + } else if (key_str == "orderByAsc") { + predicates.OrderByAsc(value_str); + } else if (key_str == "orderByDesc") { + predicates.OrderByDesc(value_str); + } else if (key_str == "distinct") { + predicates.Distinct(); + } else if (key_str == "limitAs") { + predicates.Limit(pacMap.GetIntValue(value_str, 0)); + } else if (key_str == "offsetAs") { + predicates.Offset(pacMap.GetIntValue(value_str, 0)); + } else if (key_str == "groupBy") { + std::string groupByValue = pacMap.GetStringValue(value_str, ""); + HILOG_INFO("ParseJsonKey groupBy groupByValue:%{public}s ", groupByValue.c_str()); + std::vector res; + Stringsplit(groupByValue, ",", res); + predicates.GroupBy(res); + } else if (key_str == "indexedBy") { + predicates.IndexedBy(value_str); + } else if (key_str == "in") { + ParseIn(pacMap, value_str, predicates); + } else if (key_str == "notIn") { + ParseNotIn(pacMap, value_str, predicates); + } +} + +void ParseJsonKey(std::string &jsonStr, AppExecFwk::PacMap &pacMap, NativeRdb::DataAbilityPredicates &predicates) +{ + HILOG_INFO("%{public}s, called.", __func__); + Json::Reader reader; + Json::Value devJson; + Json::Value::iterator iter; + Json::Value::Members members; + bool parseSuc = reader.parse(jsonStr, devJson); + if (parseSuc) { + members = devJson.getMemberNames(); + for (Json::Value::Members::iterator it = members.begin(); it != members.end(); ++it) { + std::string key_str = *it; + std::string value_str = devJson[key_str].asString(); + HILOG_INFO("%{public}s, called,value_str = %{public}s", __func__, value_str.c_str()); + if (key_str == "equalTo" || key_str == "notEqualTo" || key_str == "contains" || key_str == "beginsWith" || + key_str == "endsWith" || key_str == "greaterThan" || key_str == "lessThan" || + key_str == "greaterThanOrEqualTo" || key_str == "lessThanOrEqualTo") { + ParseNormal(pacMap, value_str, predicates, key_str); + } else { + ParseUnNormal(pacMap, value_str, predicates, key_str); + } + } + } + HILOG_INFO("%{public}s, called end.", __func__); +} + +napi_value GetCallbackPacmapValue(napi_env env, int index) +{ + HILOG_INFO("GetCallbackPacmapValue is called."); + napi_value result = nullptr; + napi_value eCode = nullptr; + NAPI_CALL(env, napi_create_int32(env, index, &eCode)); + NAPI_CALL(env, napi_create_object(env, &result)); + NAPI_CALL(env, napi_set_named_property(env, result, "result", eCode)); + HILOG_INFO("GetCallbackPacmapValue is called end."); + return result; +} + +void CallInsertExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_Insert, worker pool thread execute."); + DAHelperInsertCB *insertCB = static_cast(data); + if (insertCB->dataAbilityHelper != nullptr) { + insertCB->execResult = INVALID_PARAMETER; + if (!insertCB->uri.empty()) { + OHOS::Uri uri(insertCB->uri); + insertCB->result = insertCB->dataAbilityHelper->Insert(uri, insertCB->valueBucket); + HILOG_INFO("NAPI_Insert, result = %{public}d", insertCB->result); + if (insertCB->result > 0) { + insertCB->execResult = NO_ERROR; + } + } + } else { + HILOG_ERROR("NAPI_Insert, dataAbilityHelper == nullptr."); + } + HILOG_INFO("NAPI_Insert, worker pool thread execute end."); +} + +void CallInsertAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_Insert, main event thread complete."); + DAHelperInsertCB *insertCB = static_cast(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)); + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, insertCB->cbBase.cbInfo.callback, &callback)); + + napi_create_int32(env, insertCB->execResult, &result[PARAM0]); + result[PARAM1] = GetCallbackPacmapValue(env, insertCB->result); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, result, &callResult)); + + if (insertCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, insertCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, insertCB->cbBase.asyncWork)); + delete insertCB; + insertCB = nullptr; + HILOG_INFO("NAPI_Insert, main event thread complete end."); +} + +void CallInsertPromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_Insert, main event thread complete."); + DAHelperInsertCB *insertCB = static_cast(data); + napi_value result = GetCallbackPacmapValue(env, insertCB->result); + NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, insertCB->cbBase.deferred, result)); + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, insertCB->cbBase.asyncWork)); + delete insertCB; + insertCB = nullptr; + HILOG_INFO("NAPI_Insert, main event thread complete end."); +} + +napi_value CallInsertAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperInsertCB *insertCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || insertCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + 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, &insertCB->cbBase.cbInfo.callback)); + } + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, CallInsertExecuteCB, CallInsertAsyncCompleteCB, + (void *)insertCB, &insertCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, insertCB->cbBase.asyncWork)); + napi_value result = 0; + NAPI_CALL(env, napi_get_null(env, &result)); + HILOG_INFO("%{public}s, asyncCallback end", __func__); + return result; +} + +napi_value CallInsertPromise(napi_env env, DAHelperInsertCB *insertCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (insertCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = 0; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + insertCB->cbBase.deferred = deferred; + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, CallInsertExecuteCB, CallInsertPromiseCompleteCB, + (void *)insertCB, &insertCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, insertCB->cbBase.asyncWork)); + HILOG_INFO("%{public}s, promise end", __func__); + return promise; +} + +napi_value CallInsertWrap(napi_env env, napi_value thisVar, napi_callback_info info, napi_value *args, size_t argcAsync, + const size_t argcPromise) +{ + HILOG_INFO("%{public}s,called", __func__); + DAHelperInsertCB *insertCB = new (std::nothrow) DAHelperInsertCB; + if (insertCB == nullptr) { + HILOG_ERROR("%{public}s, insertCB == nullptr.", __func__); + return WrapVoidToJS(env); + } + insertCB->cbBase.cbInfo.env = env; + insertCB->cbBase.asyncWork = nullptr; + insertCB->cbBase.deferred = nullptr; + insertCB->cbBase.ability = nullptr; + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[PARAM0], &valuetype)); + if (valuetype == napi_string) { + insertCB->uri = NapiValueToStringUtf8(env, args[PARAM0]); + HILOG_INFO("%{public}s,uri=%{public}s", __func__, insertCB->uri.c_str()); + } + + insertCB->valueBucket.Clear(); + AnalysisValuesBucket(insertCB->valueBucket, env, args[PARAM3]); + + DataAbilityHelper *objectInfo = nullptr; + napi_unwrap(env, thisVar, (void **)&objectInfo); + HILOG_INFO("%{public}s,DataAbilityHelper objectInfo = %{public}p", __func__, objectInfo); + insertCB->dataAbilityHelper = objectInfo; + + napi_value ret = nullptr; + if (argcAsync > argcPromise) { + ret = CallInsertAsync(env, args, ARGS_FOUR, insertCB); + } else { + ret = CallInsertPromise(env, insertCB); + } + if (ret == nullptr) { + HILOG_ERROR("%{public}s, ret == nullptr.", __func__); + if (insertCB != nullptr) { + delete insertCB; + insertCB = nullptr; + } + } + HILOG_INFO("%{public}s,called end", __func__); + return ret; +} + +void CallDeleteExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_Delete, worker pool thread execute."); + DAHelperDeleteCB *deleteCB = static_cast(data); + if (deleteCB->dataAbilityHelper != nullptr) { + deleteCB->execResult = INVALID_PARAMETER; + if (!deleteCB->uri.empty()) { + OHOS::Uri uri(deleteCB->uri); + deleteCB->result = deleteCB->dataAbilityHelper->Delete(uri, deleteCB->predicates); + if (deleteCB->result >= 0) { + deleteCB->execResult = NO_ERROR; + } + } else { + HILOG_ERROR("NAPI_Delete, dataAbilityHelper uri is empty"); + } + } else { + HILOG_ERROR("NAPI_Delete, dataAbilityHelper == nullptr"); + } + HILOG_INFO("NAPI_Delete, worker pool thread execute end."); +} + +void CallDeleteAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_Delete, main event thread complete."); + DAHelperDeleteCB *DeleteCB = static_cast(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)); + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, DeleteCB->cbBase.cbInfo.callback, &callback)); + + napi_create_int32(env, DeleteCB->execResult, &result[PARAM0]); + result[PARAM1] = GetCallbackPacmapValue(env, DeleteCB->result); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, result, &callResult)); + + if (DeleteCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, DeleteCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, DeleteCB->cbBase.asyncWork)); + delete DeleteCB; + DeleteCB = nullptr; + HILOG_INFO("NAPI_Delete, main event thread complete end."); +} + +void CallDeletePromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_Delete, main event thread complete."); + DAHelperDeleteCB *DeleteCB = static_cast(data); + + napi_value result = GetCallbackPacmapValue(env, DeleteCB->result); + NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, DeleteCB->cbBase.deferred, result)); + + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, DeleteCB->cbBase.asyncWork)); + delete DeleteCB; + DeleteCB = nullptr; + HILOG_INFO("NAPI_Delete, main event thread complete end."); +} + +napi_value CallDeleteAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperDeleteCB *deleteCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || deleteCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + 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, &deleteCB->cbBase.cbInfo.callback)); + } + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, CallDeleteExecuteCB, CallDeleteAsyncCompleteCB, + (void *)deleteCB, &deleteCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, deleteCB->cbBase.asyncWork)); + napi_value result = 0; + NAPI_CALL(env, napi_get_null(env, &result)); + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return result; +} + +napi_value CallDeletePromise(napi_env env, DAHelperDeleteCB *deleteCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (deleteCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = 0; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + deleteCB->cbBase.deferred = deferred; + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, CallDeleteExecuteCB, CallDeletePromiseCompleteCB, + (void *)deleteCB, &deleteCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, deleteCB->cbBase.asyncWork)); + HILOG_INFO("%{public}s, promise end.", __func__); + return promise; +} + +napi_value CallDeleteWrap(napi_env env, napi_value thisVar, napi_callback_info info, napi_value *args, size_t argcAsync, + const size_t argcPromise) +{ + HILOG_INFO("%{public}s,called", __func__); + 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_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[PARAM0], &valuetype)); + if (valuetype == napi_string) { + deleteCB->uri = NapiValueToStringUtf8(env, args[PARAM0]); + HILOG_INFO("%{public}s,uri=%{public}s", __func__, deleteCB->uri.c_str()); + } + valuetype = napi_undefined; + std::string predicateArg; + NAPI_CALL(env, napi_typeof(env, args[PARAM2], &valuetype)); + if (valuetype == napi_string) { + predicateArg = NapiValueToStringUtf8(env, args[PARAM2]); + HILOG_INFO("%{public}s,predicateArg=%{public}s", __func__, predicateArg.c_str()); + } + + deleteCB->pacMap.Clear(); + HILOG_INFO("%{public}s,AnalysisPacMap start", __func__); + AnalysisPacMap(deleteCB->pacMap, env, args[PARAM3]); + HILOG_INFO("%{public}s, AnalysisPacMap end", __func__); + + ParseJsonKey(predicateArg, deleteCB->pacMap, deleteCB->predicates); + DataAbilityHelper *objectInfo = nullptr; + napi_unwrap(env, thisVar, (void **)&objectInfo); + HILOG_INFO("%{public}s,DataAbilityHelper objectInfo = %{public}p", __func__, objectInfo); + deleteCB->dataAbilityHelper = objectInfo; + + napi_value ret = nullptr; + if (argcAsync > argcPromise) { + ret = CallDeleteAsync(env, args, ARGS_FOUR, deleteCB); + } else { + ret = CallDeletePromise(env, deleteCB); + } + if (ret == nullptr) { + HILOG_ERROR("%{public}s,ret == nullptr", __func__); + if (deleteCB != nullptr) { + delete deleteCB; + deleteCB = nullptr; + } + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s,called end", __func__); + return ret; +} + +void CallUpdateExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_Update, worker pool thread execute."); + DAHelperUpdateCB *updateCB = static_cast(data); + if (updateCB->dataAbilityHelper != nullptr) { + updateCB->execResult = INVALID_PARAMETER; + if (!updateCB->uri.empty()) { + OHOS::Uri uri(updateCB->uri); + updateCB->result = updateCB->dataAbilityHelper->Update(uri, updateCB->valueBucket, updateCB->predicates); + if (updateCB->result >= 0) { + updateCB->execResult = NO_ERROR; + } + } else { + HILOG_ERROR("NAPI_Update, dataAbilityHelper uri is empty"); + } + } else { + HILOG_ERROR("NAPI_Update, dataAbilityHelper == nullptr"); + } + HILOG_INFO("NAPI_Update, worker pool thread execute end."); +} + +void CallUpdateAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_Update, main event thread complete."); + DAHelperUpdateCB *updateCB = static_cast(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)); + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, updateCB->cbBase.cbInfo.callback, &callback)); + + napi_create_int32(env, updateCB->execResult, &result[PARAM0]); + result[PARAM1] = GetCallbackPacmapValue(env, updateCB->result); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, result, &callResult)); + + if (updateCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, updateCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, updateCB->cbBase.asyncWork)); + delete updateCB; + updateCB = nullptr; + HILOG_INFO("NAPI_Update, main event thread complete end."); +} + +void CallUpdatePromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_Update, main event thread complete."); + DAHelperUpdateCB *updateCB = static_cast(data); + + napi_value result = GetCallbackPacmapValue(env, updateCB->result); + NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, updateCB->cbBase.deferred, result)); + + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, updateCB->cbBase.asyncWork)); + delete updateCB; + updateCB = nullptr; + HILOG_INFO("NAPI_Update, main event thread complete end."); +} + +napi_value CallUpdateAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperUpdateCB *updateCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || updateCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + 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, &updateCB->cbBase.cbInfo.callback)); + } + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, CallUpdateExecuteCB, CallUpdateAsyncCompleteCB, + (void *)updateCB, &updateCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, updateCB->cbBase.asyncWork)); + napi_value result = 0; + NAPI_CALL(env, napi_get_null(env, &result)); + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return result; +} + +napi_value CallUpdatePromise(napi_env env, DAHelperUpdateCB *updateCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (updateCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = 0; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + updateCB->cbBase.deferred = deferred; + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, CallUpdateExecuteCB, CallUpdatePromiseCompleteCB, + (void *)updateCB, &updateCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, updateCB->cbBase.asyncWork)); + HILOG_INFO("%{public}s, promise end.", __func__); + return promise; +} + +napi_value CallUpdateWrap(napi_env env, napi_value thisVar, napi_callback_info info, napi_value *args, size_t argcAsync, + const size_t argcPromise) +{ + HILOG_INFO("%{public}s,called", __func__); + 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_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[PARAM0], &valuetype)); + if (valuetype == napi_string) { + updateCB->uri = NapiValueToStringUtf8(env, args[PARAM0]); + HILOG_INFO("%{public}s,uri=%{public}s", __func__, updateCB->uri.c_str()); + } + valuetype = napi_undefined; + std::string predicateArg; + NAPI_CALL(env, napi_typeof(env, args[PARAM2], &valuetype)); + if (valuetype == napi_string) { + predicateArg = NapiValueToStringUtf8(env, args[PARAM2]); + HILOG_INFO("%{public}s,predicateArg=%{public}s", __func__, predicateArg.c_str()); + } + updateCB->pacMap.Clear(); + AnalysisPacMap(updateCB->pacMap, env, args[PARAM3]); + ParseJsonKey(predicateArg, updateCB->pacMap, updateCB->predicates); + updateCB->valueBucket.Clear(); + std::set set_update = updateCB->pacMap.GetKeys(); + std::set::iterator it; + for (it = set_update.begin(); it != set_update.end(); it++) { + std::string pacValue = updateCB->pacMap.GetStringValue(*it, ""); + if (pacValue != "") { + updateCB->valueBucket.PutString(*it, pacValue); + } else { + updateCB->valueBucket.PutDouble(*it, updateCB->pacMap.GetDoubleValue(*it, DBL_MIN)); + } + } + DataAbilityHelper *objectInfo = nullptr; + napi_unwrap(env, thisVar, (void **)&objectInfo); + HILOG_INFO("%{public}s,DataAbilityHelper objectInfo = %{public}p", __func__, objectInfo); + updateCB->dataAbilityHelper = objectInfo; + napi_value ret = nullptr; + if (argcAsync > argcPromise) { + ret = CallUpdateAsync(env, args, ARGS_FOUR, updateCB); + } else { + ret = CallUpdatePromise(env, updateCB); + } + HILOG_INFO("%{public}s,end", __func__); + return ret; +} + +void GetValue(std::vector &columnNames, int &index, Json::Value &data, + std::shared_ptr &resultSet) +{ + std::string typeValue = columnNames[index]; + int columnIndex; + resultSet->GetColumnIndex(typeValue, columnIndex); + OHOS::NativeRdb::ColumnType columnType; + resultSet->GetColumnType(columnIndex, columnType); + if (columnType == OHOS::NativeRdb::ColumnType::TYPE_INTEGER) { + int intValue; + resultSet->GetInt(columnIndex, intValue); + data[typeValue] = intValue; + } else if (columnType == OHOS::NativeRdb::ColumnType::TYPE_FLOAT) { + double doubleValue; + resultSet->GetDouble(columnIndex, doubleValue); + data[typeValue] = doubleValue; + } else if (columnType == OHOS::NativeRdb::ColumnType::TYPE_STRING) { + std::string stringValue; + resultSet->GetString(columnIndex, stringValue); + if (!stringValue.empty()) { + data[typeValue] = stringValue; + } + } +} + +void ConvertResultSet(Json::Value &arrayValue, std::shared_ptr &resultSet) +{ + int resultSetNum = resultSet->GoToFirstRow(); + std::vector columnNames; + resultSet->GetAllColumnNames(columnNames); + while (resultSetNum == NO_ERROR) { + Json::Value data; + int size = columnNames.size(); + for (int i = 0; i < size; i++) { + GetValue(columnNames, i, data, resultSet); + } + arrayValue.append(data); + resultSetNum = resultSet->GoToNextRow(); + } +} + +std::string GetResultData(std::shared_ptr &resultSet) +{ + Json::Value arrayValue; + ConvertResultSet(arrayValue, resultSet); + Json::StreamWriterBuilder builder; + const std::string personal_ringtone = Json::writeString(builder, arrayValue); + return personal_ringtone; +} + +napi_value GetQueryCallbackPacmapValue(napi_env env, std::shared_ptr &resultSet) +{ + HILOG_INFO("GetQueryCallbackPacmapValue is called."); + napi_value result = nullptr; + napi_value valueSet = nullptr; + std::string result_values = GetResultData(resultSet); + napi_create_string_utf8(env, result_values.c_str(), NAPI_AUTO_LENGTH, &valueSet); + + NAPI_CALL(env, napi_create_object(env, &result)); + NAPI_CALL(env, napi_set_named_property(env, result, "result", valueSet)); + HILOG_INFO("GetQueryCallbackPacmapValue is called end."); + return result; +} + +void CallQueryAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_Query, main event thread complete."); + DAHelperQueryCB *queryCB = static_cast(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)); + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, queryCB->cbBase.cbInfo.callback, &callback)); + + napi_create_int32(env, queryCB->execResult, &result[PARAM0]); + result[PARAM1] = GetQueryCallbackPacmapValue(env, queryCB->result); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, result, &callResult)); + + if (queryCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, queryCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, queryCB->cbBase.asyncWork)); + delete queryCB; + queryCB = nullptr; + HILOG_INFO("NAPI_Query, main event thread complete end."); +} + +void CallQueryPromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_DAHelperQueryCB, main event thread complete."); + DAHelperQueryCB *QueryCB = static_cast(data); + napi_value result = GetQueryCallbackPacmapValue(env, QueryCB->result); + NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, QueryCB->cbBase.deferred, result)); + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, QueryCB->cbBase.asyncWork)); + delete QueryCB; + QueryCB = nullptr; + HILOG_INFO("NAPI_DAHelperQueryCB, main event thread complete end."); +} + +void CallQueryExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_Query, worker pool thread execute."); + DAHelperQueryCB *queryCB = static_cast(data); + if (queryCB->dataAbilityHelper != nullptr) { + queryCB->execResult = INVALID_PARAMETER; + if (!queryCB->uri.empty()) { + OHOS::Uri uri(queryCB->uri); + auto resultset = queryCB->dataAbilityHelper->Query(uri, queryCB->columns, queryCB->predicates); + if (resultset != nullptr) { + queryCB->result = resultset; + queryCB->execResult = NO_ERROR; + } else { + HILOG_INFO("NAPI_Query, resultset == nullptr."); + } + } else { + HILOG_ERROR("NAPI_Query, dataAbilityHelper uri is empty"); + } + } else { + HILOG_ERROR("NAPI_Query, dataAbilityHelper == nullptr"); + } + HILOG_INFO("NAPI_Query, worker pool thread execute end."); +} + +napi_value CallQueryAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperQueryCB *queryCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || queryCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + 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, &queryCB->cbBase.cbInfo.callback)); + } + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, CallQueryExecuteCB, CallQueryAsyncCompleteCB, + (void *)queryCB, &queryCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, queryCB->cbBase.asyncWork)); + napi_value result = 0; + NAPI_CALL(env, napi_get_null(env, &result)); + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return result; +} + +napi_value CallQueryPromise(napi_env env, DAHelperQueryCB *queryCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (queryCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = 0; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + queryCB->cbBase.deferred = deferred; + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, CallQueryExecuteCB, CallQueryPromiseCompleteCB, + (void *)queryCB, &queryCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, queryCB->cbBase.asyncWork)); + HILOG_INFO("%{public}s, promise end.", __func__); + return promise; +} + +napi_value CallQueryWrap(napi_env env, napi_value thisVar, napi_callback_info info, napi_value *args, size_t argcAsync, + const size_t argcPromise) +{ + HILOG_INFO("%{public}s,called", __func__); + 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_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[PARAM0], &valuetype)); + if (valuetype == napi_string) { + queryCB->uri = NapiValueToStringUtf8(env, args[PARAM0]); + HILOG_INFO("%{public}s,uri=%{public}s", __func__, queryCB->uri.c_str()); + } + valuetype = napi_undefined; + std::string predicateArg; + NAPI_CALL(env, napi_typeof(env, args[PARAM2], &valuetype)); + if (valuetype == napi_string) { + predicateArg = NapiValueToStringUtf8(env, args[PARAM2]); + HILOG_INFO("%{public}s,predicateArg=%{public}s", __func__, predicateArg.c_str()); + } + queryCB->pacMap.Clear(); + AnalysisPacMap(queryCB->pacMap, env, args[PARAM3]); + + std::string columns = queryCB->pacMap.GetStringValue("columns", ""); + HILOG_INFO("%{public}s,columns=%{public}s", __func__, columns.c_str()); + Stringsplit(columns, ",", queryCB->columns); + HILOG_INFO("%{public}s,columns.size=%{public}d", __func__, queryCB->columns.size()); + queryCB->pacMap.Remove("columns"); + for (size_t i = 0; i < queryCB->columns.size(); i++) { + HILOG_INFO("%{public}s,columns=%{public}s", __func__, queryCB->columns.at(i).c_str()); + } + + ParseJsonKey(predicateArg, queryCB->pacMap, queryCB->predicates); + DataAbilityHelper *objectInfo = nullptr; + napi_unwrap(env, thisVar, (void **)&objectInfo); + HILOG_INFO("%{public}s,DataAbilityHelper objectInfo = %{public}p", __func__, objectInfo); + queryCB->dataAbilityHelper = objectInfo; + + napi_value ret = nullptr; + if (argcAsync > argcPromise) { + ret = CallQueryAsync(env, args, ARGS_FOUR, queryCB); + } else { + ret = CallQueryPromise(env, queryCB); + } + HILOG_INFO("%{public}s,end", __func__); + return ret; +} + +/** + * @brief Call processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param insertCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value CallWrap(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s,called", __func__); + size_t argcAsync = ARGS_FIVE; + const size_t argcPromise = ARGS_FOUR; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + napi_value thisVar = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + std::string method; + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[PARAM1], &valuetype)); + if (valuetype == napi_string) { + method = NapiValueToStringUtf8(env, args[PARAM1]); + HILOG_INFO("%{public}s,method=%{public}s", __func__, method.c_str()); + } else { + HILOG_ERROR("%{public}s, Wrong argument type.", __func__); + return nullptr; + } + if (method == "insert") { + ret = CallInsertWrap(env, thisVar, info, args, argcAsync, argcPromise); + } else if (method == "delete") { + ret = CallDeleteWrap(env, thisVar, info, args, argcAsync, argcPromise); + } else if (method == "update") { + ret = CallUpdateWrap(env, thisVar, info, args, argcAsync, argcPromise); + } else if (method == "query") { + ret = CallQueryWrap(env, thisVar, info, args, argcAsync, argcPromise); + } + HILOG_INFO("%{public}s,end", __func__); + return ret; +} + +/** + * @brief DataAbilityHelper NAPI method : call. + * + * @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_Call(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s,called", __func__); + napi_value ret = CallWrap(env, info); + if (ret == nullptr) { + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s,end", __func__); + return ret; +} + /** * @brief DataAbilityHelper NAPI method : insert. * 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 f759e1126c..234af349d7 100644 --- a/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.h +++ b/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.h @@ -369,6 +369,8 @@ void UpdateAsyncCompleteCB(napi_env env, napi_status status, void *data); void UpdatePromiseCompleteCB(napi_env env, napi_status status, void *data); +napi_value NAPI_Call(napi_env env, napi_callback_info info); + napi_value NAPI_OpenFile(napi_env env, napi_callback_info info); napi_value OpenFileWrap(napi_env env, napi_callback_info info, DAHelperOpenFileCB *openFileCB); 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 39bf4287bc..750d423d8d 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 @@ -23,6 +23,7 @@ #include "napi/native_node_api.h" #include "napi_common.h" #include "napi_common_util.h" +#include "pac_map.h" #include "values_bucket.h" #include "want.h" @@ -209,6 +210,17 @@ struct HapModuleInfoCB { HapModuleInfo_ hapModuleInfo; }; +struct AppVersionInfo { + std::string appName; + std::string versionName; + int32_t versionCode = 0; +}; + +struct AppVersionInfoCB { + CBBase cbBase; + AppVersionInfo appVersionInfo; +}; + struct DataAbilityHelperCB { CBBase cbBase; napi_ref uri = nullptr; @@ -313,6 +325,7 @@ struct DAHelperDeleteCB { DataAbilityHelper *dataAbilityHelper = nullptr; std::string uri; NativeRdb::DataAbilityPredicates predicates; + AppExecFwk::PacMap pacMap; int result = 0; int execResult; }; @@ -323,6 +336,7 @@ struct DAHelperQueryCB { std::string uri; std::vector columns; NativeRdb::DataAbilityPredicates predicates; + AppExecFwk::PacMap pacMap; std::shared_ptr result; int execResult; }; @@ -333,6 +347,7 @@ struct DAHelperUpdateCB { std::string uri; NativeRdb::ValuesBucket valueBucket; NativeRdb::DataAbilityPredicates predicates; + AppExecFwk::PacMap pacMap; int result = 0; int execResult; }; 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 757021a279..8f5ce8b92a 100644 --- a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp @@ -336,14 +336,71 @@ void GetFilesDirExecuteCallback(napi_env env, void *data) asyncCallbackInfo->native_data.str_value = asyncCallbackInfo->ability->GetFilesDir(); HILOG_INFO("%{public}s end. filesDir=%{public}s", __func__, asyncCallbackInfo->native_data.str_value.c_str()); } + +void IsUpdatingConfigurationsExecuteCallback(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = static_cast(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_BOOL; + asyncCallbackInfo->native_data.bool_value = asyncCallbackInfo->ability->IsUpdatingConfigurations(); + HILOG_INFO("%{public}s end", __func__); +} + /** - * @brief GetFilesDir processing function. + * @brief PrintDrawnCompleted asynchronous 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. + * @param data Point to asynchronous processing of data. */ +void PrintDrawnCompletedExecuteCallback(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = static_cast(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_NONE; + asyncCallbackInfo->native_data.bool_value = asyncCallbackInfo->ability->PrintDrawnCompleted(); + HILOG_INFO("%{public}s end", __func__); +} + napi_value NAPI_GetFilesDirWrap(napi_env env, napi_callback_info info, AsyncJSCallbackInfo *asyncCallbackInfo) { HILOG_INFO("%{public}s called", __func__); @@ -403,6 +460,432 @@ napi_value NAPI_GetFilesDirCommon(napi_env env, napi_callback_info info, Ability return ret; } +void GetOrCreateDistributedDirExecuteCallback(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = static_cast(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->GetDistributedDir(); + 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_GetOrCreateDistributedDirWrap( + 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_ERROR("%{public}s called, parameters is invalid.", __func__); + return nullptr; + } + + if (argc == ARGS_ONE) { + if (!CreateAsyncCallback(env, args[PARAM0], asyncCallbackInfo)) { + HILOG_ERROR("%{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 = GetOrCreateDistributedDirExecuteCallback; + asyncParamEx.complete = CompleteAsyncCallbackWork; + + return ExecuteAsyncCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } else { + HILOG_INFO("%{public}s called. promise.", __func__); + asyncParamEx.resource = "NAPI_GetFilesDirPromise"; + asyncParamEx.execute = GetOrCreateDistributedDirExecuteCallback; + asyncParamEx.complete = CompletePromiseCallbackWork; + + return ExecutePromiseCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } +} + +napi_value NAPI_GetOrCreateDistributedDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = CreateAsyncJSCallbackInfo(env); + if (asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s called. Invoke CreateAsyncJSCallbackInfo failed.", __func__); + return WrapVoidToJS(env); + } + + asyncCallbackInfo->abilityType = abilityType; + napi_value ret = NAPI_GetOrCreateDistributedDirWrap(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 GetCacheDir asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetCacheDirExecuteCallback(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = static_cast(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->GetCacheDir(); + HILOG_INFO("%{public}s end. CacheDir=%{public}s", __func__, asyncCallbackInfo->native_data.str_value.c_str()); +} + +/** + * @brief NAPI_GetCacheDirWrap 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_GetCacheDirWrap(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_ERROR("%{public}s called, parameters is invalid.", __func__); + return nullptr; + } + + if (argc == ARGS_ONE) { + if (!CreateAsyncCallback(env, args[PARAM0], asyncCallbackInfo)) { + HILOG_ERROR("%{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_GetCacheDirCallback"; + asyncParamEx.execute = GetCacheDirExecuteCallback; + asyncParamEx.complete = CompleteAsyncCallbackWork; + + return ExecuteAsyncCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } else { + HILOG_INFO("%{public}s called. promise.", __func__); + asyncParamEx.resource = "NAPI_GetCacheDirPromise"; + asyncParamEx.execute = GetCacheDirExecuteCallback; + asyncParamEx.complete = CompletePromiseCallbackWork; + + return ExecutePromiseCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } +} + +napi_value NAPI_GetCacheDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = CreateAsyncJSCallbackInfo(env); + if (asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s called. Invoke CreateAsyncJSCallbackInfo failed.", __func__); + return WrapVoidToJS(env); + } + + asyncCallbackInfo->abilityType = abilityType; + napi_value ret = NAPI_GetCacheDirWrap(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 GetExternalCacheDir asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetExternalCacheDirExecuteCallback(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = static_cast(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->GetExternalCacheDir(); + HILOG_INFO( + "%{public}s end. ExternalCacheDir=%{public}s", __func__, asyncCallbackInfo->native_data.str_value.c_str()); +} + +/** + * @brief NAPI_GetExternalCacheDirWrap 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_GetExternalCacheDirWrap(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_GetExternalCacheDirCallback"; + asyncParamEx.execute = GetExternalCacheDirExecuteCallback; + asyncParamEx.complete = CompleteAsyncCallbackWork; + + return ExecuteAsyncCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } else { + HILOG_INFO("%{public}s called. promise.", __func__); + asyncParamEx.resource = "NAPI_GetExternalCacheDirPromise"; + asyncParamEx.execute = GetExternalCacheDirExecuteCallback; + asyncParamEx.complete = CompletePromiseCallbackWork; + + return ExecutePromiseCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } +} + +napi_value NAPI_GetExternalCacheDirCommon(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_GetExternalCacheDirWrap(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; +} + +napi_value NAPI_IsUpdatingConfigurationsWrap( + 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_IsUpdatingConfigurationsCallback"; + asyncParamEx.execute = IsUpdatingConfigurationsExecuteCallback; + asyncParamEx.complete = CompleteAsyncCallbackWork; + + return ExecuteAsyncCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } else { + HILOG_INFO("%{public}s called. promise.", __func__); + asyncParamEx.resource = "NAPI_IsUpdatingConfigurationsPromise"; + asyncParamEx.execute = IsUpdatingConfigurationsExecuteCallback; + asyncParamEx.complete = CompletePromiseCallbackWork; + + return ExecutePromiseCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } +} + +napi_value NAPI_PrintDrawnCompletedWrap(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_PrintDrawnCompletedCallback"; + asyncParamEx.execute = PrintDrawnCompletedExecuteCallback; + asyncParamEx.complete = CompleteAsyncVoidCallbackWork; + + return ExecuteAsyncCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } else { + HILOG_INFO("%{public}s called. promise.", __func__); + asyncParamEx.resource = "NAPI_PrintDrawnCompletedPromise"; + asyncParamEx.execute = PrintDrawnCompletedExecuteCallback; + asyncParamEx.complete = CompletePromiseVoidCallbackWork; + + return ExecutePromiseCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } +} + +napi_value NAPI_IsUpdatingConfigurationsCommon(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_IsUpdatingConfigurationsWrap(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; +} + +napi_value NAPI_PrintDrawnCompletedCommon(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_PrintDrawnCompletedWrap(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. * @@ -1480,6 +1963,281 @@ napi_value NAPI_GetHapModuleInfoCommon(napi_env env, napi_callback_info info, Ab return ret; } +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to AppVersionInfoCB on success, nullptr on failure. + */ +AppVersionInfoCB *CreateAppVersionInfoCBInfo(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)); + + AppVersionInfoCB *appVersionInfoCB = new (std::nothrow) AppVersionInfoCB; + if (appVersionInfoCB == nullptr) { + HILOG_ERROR("%{public}s, appVersionInfoCB == nullptr.", __func__); + return nullptr; + } + appVersionInfoCB->cbBase.cbInfo.env = env; + appVersionInfoCB->cbBase.asyncWork = nullptr; + appVersionInfoCB->cbBase.deferred = nullptr; + appVersionInfoCB->cbBase.ability = ability; + + HILOG_INFO("%{public}s end.", __func__); + return appVersionInfoCB; +} + +void SaveAppVersionInfo(AppVersionInfo &appVersionInfo, const std::string appName, const std::string versionName, + const int32_t versionCode) +{ + HILOG_INFO("%{public}s called.", __func__); + appVersionInfo.appName = appName; + appVersionInfo.versionName = versionName; + appVersionInfo.versionCode = versionCode; + HILOG_INFO("%{public}s end.", __func__); +} + +napi_value WrapAppVersionInfo(napi_env env, const AppVersionInfoCB &appVersionInfoCB) +{ + 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, appVersionInfoCB.appVersionInfo.appName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "appName", proValue)); + + NAPI_CALL(env, + napi_create_string_utf8(env, appVersionInfoCB.appVersionInfo.versionName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "versionName", proValue)); + + NAPI_CALL(env, napi_create_int32(env, appVersionInfoCB.appVersionInfo.versionCode, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "versionCode", proValue)); + + HILOG_INFO("%{public}s end.", __func__); + return result; +} + +void GetAppVersionInfoExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetAppVersionInfo, worker pool thread execute."); + AppVersionInfoCB *appVersionInfoCB = static_cast(data); + if (appVersionInfoCB == nullptr) { + HILOG_ERROR("NAPI_GetAppVersionInfo, appVersionInfoCB == nullptr"); + return; + } + + appVersionInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (appVersionInfoCB->cbBase.ability == nullptr) { + HILOG_ERROR("NAPI_GetAppVersionInfo, ability == nullptr"); + appVersionInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&appVersionInfoCB->cbBase)) { + HILOG_ERROR("NAPI_GetAppVersionInfo,wrong ability type"); + appVersionInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + std::shared_ptr abilityInfoPtr = appVersionInfoCB->cbBase.ability->GetAbilityInfo(); + std::shared_ptr appInfoPtr = appVersionInfoCB->cbBase.ability->GetApplicationInfo(); + if (abilityInfoPtr != nullptr && appInfoPtr != nullptr) { + HILOG_ERROR("NAPI_GetAppVersionInfo, bundleName = %{public}s", abilityInfoPtr->bundleName.c_str()); + HILOG_ERROR("NAPI_GetAppVersionInfo, appName = %{public}s", abilityInfoPtr->appName.c_str()); + HILOG_ERROR("NAPI_GetAppVersionInfo, versionName1 = %{public}s", abilityInfoPtr->versionName.c_str()); + HILOG_ERROR("NAPI_GetAppVersionInfo, name = %{public}s", appInfoPtr->name.c_str()); + HILOG_ERROR("NAPI_GetAppVersionInfo, versionCode = %{public}d", appInfoPtr->versionCode); + HILOG_ERROR("NAPI_GetAppVersionInfo, versionName2 = %{public}s", appInfoPtr->versionName.c_str()); + SaveAppVersionInfo(appVersionInfoCB->appVersionInfo, abilityInfoPtr->appName, appInfoPtr->versionName, + appInfoPtr->versionCode); + } else { + appVersionInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; + } + HILOG_INFO("NAPI_GetAppVersionInfo, worker pool thread execute end."); +} + +void GetAppVersionInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetAppVersionInfo, main event thread complete."); + AppVersionInfoCB *appVersionInfoCB = static_cast(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, appVersionInfoCB->cbBase.errCode); + if (appVersionInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapAppVersionInfo(env, *appVersionInfoCB); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, appVersionInfoCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (appVersionInfoCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, appVersionInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, appVersionInfoCB->cbBase.asyncWork)); + delete appVersionInfoCB; + appVersionInfoCB = nullptr; + HILOG_INFO("NAPI_GetAppVersionInfo, main event thread complete end."); +} + +void GetAppVersionInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetAppVersionInfo, main event thread complete."); + AppVersionInfoCB *appVersionInfoCB = static_cast(data); + napi_value result = nullptr; + if (appVersionInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapAppVersionInfo(env, *appVersionInfoCB); + napi_resolve_deferred(env, appVersionInfoCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, appVersionInfoCB->cbBase.errCode); + napi_reject_deferred(env, appVersionInfoCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, appVersionInfoCB->cbBase.asyncWork); + delete appVersionInfoCB; + appVersionInfoCB = nullptr; + HILOG_INFO("NAPI_GetAppVersionInfo, main event thread complete end."); +} + +/** + * @brief GetAppVersionInfo 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 AppVersionInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetAppVersionInfoAsync( + napi_env env, napi_value *args, const size_t argCallback, AppVersionInfoCB *appVersionInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || appVersionInfoCB == 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, &appVersionInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL( + env, napi_create_async_work(env, nullptr, resourceName, GetAppVersionInfoExecuteCB, + GetAppVersionInfoAsyncCompleteCB, (void *)appVersionInfoCB, &appVersionInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, appVersionInfoCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return result; +} + +/** + * @brief GetAppVersionInfo Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param AppVersionInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetAppVersionInfoPromise(napi_env env, AppVersionInfoCB *appVersionInfoCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (appVersionInfoCB == 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)); + appVersionInfoCB->cbBase.deferred = deferred; + + NAPI_CALL( + env, napi_create_async_work(env, nullptr, resourceName, GetAppVersionInfoExecuteCB, + GetAppVersionInfoPromiseCompleteCB, (void *)appVersionInfoCB, &appVersionInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, appVersionInfoCB->cbBase.asyncWork)); + HILOG_INFO("%{public}s, promise end.", __func__); + return promise; +} + +napi_value GetAppVersionInfoWrap(napi_env env, napi_callback_info info, AppVersionInfoCB *appVersionInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (appVersionInfoCB == nullptr) { + HILOG_ERROR("%{public}s, appVersionInfoCB == 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 = GetAppVersionInfoAsync(env, args, 0, appVersionInfoCB); + } else { + ret = GetAppVersionInfoPromise(env, appVersionInfoCB); + } + HILOG_INFO("%{public}s, asyncCallback end.", __func__); + return ret; +} + +/** + * @brief Obtains the AppVersionInfo 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_GetAppVersionInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + AppVersionInfoCB *appVersionInfoCB = CreateAppVersionInfoCBInfo(env); + if (appVersionInfoCB == nullptr) { + return WrapVoidToJS(env); + } + + appVersionInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + appVersionInfoCB->cbBase.abilityType = abilityType; + napi_value ret = GetAppVersionInfoWrap(env, info, appVersionInfoCB); + if (ret == nullptr) { + HILOG_ERROR("%{public}s ret == nullptr", __func__); + if (appVersionInfoCB != nullptr) { + delete appVersionInfoCB; + appVersionInfoCB = nullptr; + } + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s end.", __func__); + return ret; +} + /** * @brief Create asynchronous data. * 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 c15bb499a3..6e5c231003 100644 --- a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.h +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.h @@ -43,6 +43,24 @@ napi_value WrapAppInfo(napi_env env, const AppInfo_ &appInfo); */ napi_value NAPI_GetFilesDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); +/** + * @brief Get OrCreateDistribute 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_GetOrCreateDistributedDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +napi_value NAPI_GetCacheDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +napi_value NAPI_GetExternalCacheDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +napi_value NAPI_IsUpdatingConfigurationsCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +napi_value NAPI_PrintDrawnCompletedCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + /** * @brief Obtains the type of this application. * @@ -73,6 +91,16 @@ 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 AppVersionInfo 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_GetAppVersionInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + /** * @brief Create asynchronous data. * diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.cpp b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.cpp index d3cecda195..f1a5de4aaa 100644 --- a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.cpp +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.cpp @@ -1064,6 +1064,36 @@ void CompleteAsyncCallbackWork(napi_env env, napi_status status, void *data) asyncCallbackInfo = nullptr; } +void CompleteAsyncVoidCallbackWork(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = (AsyncJSCallbackInfo *)data; + if (asyncCallbackInfo == nullptr) { + HILOG_INFO("%{public}s called, asyncCallbackInfo is null", __func__); + return; + } + napi_value callback = 0; + napi_value undefined = 0; + napi_get_undefined(env, &undefined); + napi_value callResult = 0; + napi_value result[ARGS_TWO] = {nullptr}; + result[PARAM0] = GetCallbackErrorValue(env, asyncCallbackInfo->error_code); + if (asyncCallbackInfo->error_code == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapVoidToJS(env); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + if (asyncCallbackInfo->cbInfo.callback != nullptr) { + napi_get_reference_value(env, asyncCallbackInfo->cbInfo.callback, &callback); + napi_call_function(env, undefined, callback, ARGS_TWO, result, &callResult); + napi_delete_reference(env, asyncCallbackInfo->cbInfo.callback); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; +} + /** * @brief The callback at the end of the Promise callback. * @@ -1093,6 +1123,28 @@ void CompletePromiseCallbackWork(napi_env env, napi_status status, void *data) asyncCallbackInfo = nullptr; } +void CompletePromiseVoidCallbackWork(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + + AsyncJSCallbackInfo *asyncCallbackInfo = (AsyncJSCallbackInfo *)data; + if (asyncCallbackInfo == nullptr) { + HILOG_INFO("%{public}s called, asyncCallbackInfo is null", __func__); + return; + } + napi_value result = 0; + if (asyncCallbackInfo->error_code == NAPI_ERR_NO_ERROR) { + result = WrapVoidToJS(env); + napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); + } else { + result = GetCallbackErrorValue(env, asyncCallbackInfo->error_code); + napi_reject_deferred(env, asyncCallbackInfo->deferred, result); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; +} + std::vector ConvertU8Vector(napi_env env, napi_value jsValue) { bool isTypedArray = false; @@ -1117,5 +1169,31 @@ std::vector ConvertU8Vector(napi_env env, napi_value jsValue) return result; } +std::vector ConvertStringVector(napi_env env, napi_value jsValue) +{ + bool isTypedArray = false; + napi_status status = napi_is_typedarray(env, jsValue, &isTypedArray); + if (status != napi_ok || !isTypedArray) { + HILOG_INFO("%{public}s called, napi_is_typedarray error", __func__); + return {}; + } + + napi_typedarray_type type; + size_t length = 0; + napi_value buffer = nullptr; + size_t offset = 0; + NAPI_CALL_BASE(env, napi_get_typedarray_info(env, jsValue, &type, &length, nullptr, &buffer, &offset), {}); + if (type != napi_uint8_array) { + HILOG_ERROR("%{public}s called, napi_uint8_array is null", __func__); + return {}; + } + std::string *data = nullptr; + size_t total = 0; + NAPI_CALL_BASE(env, napi_get_arraybuffer_info(env, buffer, reinterpret_cast(&data), &total), {}); + length = std::min(length, total - offset); + std::vector result(sizeof(std::string) + length); + memcpy_s(result.data(), result.size(), &data[offset], length); + return result; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.h b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.h index 249ce4505f..2aed629c92 100644 --- a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.h +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.h @@ -228,6 +228,14 @@ napi_value ExecutePromiseCallbackWork(napi_env env, AsyncJSCallbackInfo *asyncCa */ void CompleteAsyncCallbackWork(napi_env env, napi_status status, void *data); +/** + * @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 CompleteAsyncVoidCallbackWork(napi_env env, napi_status status, void *data); + /** * @brief The callback at the end of the Promise callback. * @@ -236,8 +244,18 @@ void CompleteAsyncCallbackWork(napi_env env, napi_status status, void *data); */ void CompletePromiseCallbackWork(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 CompletePromiseVoidCallbackWork(napi_env env, napi_status status, void *data); + std::vector ConvertU8Vector(napi_env env, napi_value jsValue); +std::vector ConvertStringVector(napi_env env, napi_value jsValue); + } // namespace AppExecFwk } // namespace OHOS #endif // OHOS_APPEXECFWK_NAPI_COMMON_UTIL_H diff --git a/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn b/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn index fd38c59346..648cf53394 100644 --- a/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn +++ b/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn @@ -27,6 +27,8 @@ ohos_shared_library("particleability") { "//foundation/distributeddatamgr/appdatamgr/frameworks/jskitsimpl/common/include", "//foundation/distributeddatamgr/appdatamgr/frameworks/jskitsimpl/napi_dataability/include", "//foundation/distributeddatamgr/appdatamgr/frameworks/jskitsimpl/napi_resultset/include", + "//third_party/jsoncpp", + "//third_party/jsoncpp/include/json", ] sources = [ @@ -48,6 +50,7 @@ ohos_shared_library("particleability") { "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/inner/napi_common:napi_common", "//foundation/ace/napi:ace_napi", "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + "//third_party/jsoncpp:jsoncpp", "//third_party/libuv:uv_static", "//utils/native/base:utils", ] diff --git a/services/abilitymgr/BUILD.gn b/services/abilitymgr/BUILD.gn index 212cda4f75..182ac69338 100644 --- a/services/abilitymgr/BUILD.gn +++ b/services/abilitymgr/BUILD.gn @@ -64,6 +64,7 @@ config("abilityms_config") { "//base/account/os_account/frameworks/common/log/include", "//base/account/os_account/interfaces/innerkits/osaccount/native/include", "//base/hiviewdfx/hicollie/interfaces/native/innerkits/include", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara/include", ] cflags = [] if (target_cpu == "arm") { @@ -84,6 +85,7 @@ ohos_shared_library("abilityms") { "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", "//base/global/i18n_standard/frameworks/intl:intl_util", "//base/hiviewdfx/hiview/adapter/utility:hiview_adapter_utility", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", "//foundation/aafwk/standard/frameworks/kits/ability/ability_runtime:ability_context_native", "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", "//foundation/aafwk/standard/frameworks/kits/ability/native:dummy_classes", diff --git a/services/abilitymgr/include/ability_manager_proxy.h b/services/abilitymgr/include/ability_manager_proxy.h index ae0ddce94c..ea5ba87c93 100644 --- a/services/abilitymgr/include/ability_manager_proxy.h +++ b/services/abilitymgr/include/ability_manager_proxy.h @@ -529,7 +529,9 @@ public: * @param SystemMemoryAttr, memory information. */ virtual void GetSystemMemoryAttr(AppExecFwk::SystemMemoryAttr &memoryInfo) override; + virtual int GetAppMemorySize() override; + virtual bool IsRamConstrainedDevice() override; virtual int ContinueMission(const std::string &srcDeviceId, const std::string &dstDeviceId, int32_t missionId, const sptr &callBack, AAFwk::WantParams &wantParams) override; diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index 328b1bec8a..db6df79606 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -686,7 +686,9 @@ public: * @param SystemMemoryAttr, memory information. */ virtual void GetSystemMemoryAttr(AppExecFwk::SystemMemoryAttr &memoryInfo) override; + virtual int GetAppMemorySize() override; + virtual bool IsRamConstrainedDevice() override; /** * Start Ability, connect session with common ability. * diff --git a/services/abilitymgr/include/ability_manager_stub.h b/services/abilitymgr/include/ability_manager_stub.h index 2ec98a94ac..402f451790 100644 --- a/services/abilitymgr/include/ability_manager_stub.h +++ b/services/abilitymgr/include/ability_manager_stub.h @@ -123,6 +123,8 @@ private: int SetShowOnLockScreenInner(MessageParcel &data, MessageParcel &reply); int GetSystemMemoryAttrInner(MessageParcel &data, MessageParcel &reply); + int GetAppMemorySizeInner(MessageParcel &data, MessageParcel &reply); + int IsRamConstrainedDeviceInner(MessageParcel &data, MessageParcel &reply); int ClearUpApplicationDataInner(MessageParcel &data, MessageParcel &reply); int ContinueMissionInner(MessageParcel &data, MessageParcel &reply); diff --git a/services/abilitymgr/src/ability_manager_proxy.cpp b/services/abilitymgr/src/ability_manager_proxy.cpp index bfafa7d5d2..90b1bd0b43 100644 --- a/services/abilitymgr/src/ability_manager_proxy.cpp +++ b/services/abilitymgr/src/ability_manager_proxy.cpp @@ -1638,6 +1638,40 @@ void AbilityManagerProxy::GetSystemMemoryAttr(AppExecFwk::SystemMemoryAttr &memo memoryInfo = *remoteRetsult; } +int AbilityManagerProxy::GetAppMemorySize() +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!WriteInterfaceToken(data)) { + HILOG_ERROR("WriteInterfaceToken faild"); + return INNER_ERR; + } + auto error = Remote()->SendRequest(IAbilityManager::GET_APP_MEMORY_SIZE, data, reply, option); + if (error != NO_ERROR) { + HILOG_ERROR("Send request error: %{public}d", error); + return error; + } + return reply.ReadInt32(); +} + +bool AbilityManagerProxy::IsRamConstrainedDevice() +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!WriteInterfaceToken(data)) { + HILOG_ERROR("WriteInterfaceToken faild"); + return false; + } + auto error = Remote()->SendRequest(IAbilityManager::IS_RAM_CONSTRAINED_DEVICE, data, reply, option); + if (error != NO_ERROR) { + HILOG_ERROR("Send request error: %{public}d", error); + return false; + } + return reply.ReadBool(); +} + int AbilityManagerProxy::ContinueMission(const std::string &srcDeviceId, const std::string &dstDeviceId, int32_t missionId, const sptr &callBack, AAFwk::WantParams &wantParams) { diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index f1e7837125..09ff5c0989 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include "ability_info.h" #include "ability_manager_errors.h" @@ -52,6 +53,7 @@ #include "ui_service_mgr_client.h" #include "uri_permission_manager_client.h" #include "xcollie/watchdog.h" +#include "parameter.h" using OHOS::AppExecFwk::ElementName; using OHOS::Security::AccessToken::AccessTokenKit; @@ -70,6 +72,13 @@ constexpr uint32_t SCENE_FLAG_NORMAL = 0; const int32_t MAX_NUMBER_OF_DISTRIBUTED_MISSIONS = 20; const int32_t MAX_NUMBER_OF_CONNECT_BMS = 15; const std::string EMPTY_DEVICE_ID = ""; +const int32_t APP_MEMORY_SIZE = 512; +const int32_t GET_PARAMETER_INCORRECT = -9; +const int32_t GET_PARAMETER_OTHER = -1; +const int32_t SIZE_10 = -1; +const bool isRamConstrainedDevice = false; +const std::string APP_MEMORY_MAX_SIZE_PARAMETER = "const.product.dalvikheaplimit"; +const std::string RAM_CONSTRAINED_DEVICE_SIGN = "const.product.islowram"; const std::string PKG_NAME = "ohos.distributedhardware.devicemanager"; const std::string ACTION_CHOOSE = "ohos.want.action.select"; const std::string PERMISSION_SET_ABILITY_CONTROLLER = "ohos.permission.SET_ABILITY_CONTROLLER"; @@ -3409,6 +3418,53 @@ void AbilityManagerService::GetSystemMemoryAttr(AppExecFwk::SystemMemoryAttr &me appScheduler->GetSystemMemoryAttr(memoryInfo, memConfig); } +int AbilityManagerService::GetAppMemorySize() +{ + HILOG_INFO("service GetAppMemorySize start"); + const char *key = "const.product.dalvikheaplimit"; + const char *def = "512m"; + char *valueGet = nullptr; + unsigned int len = 128; + int ret = GetParameter(key, def, valueGet, len); + if ((ret != GET_PARAMETER_OTHER) && (ret != GET_PARAMETER_INCORRECT)) { + int *size = 0; + int len = strlen(valueGet); + int index = 0; + for (int i = 0; i < len; i++) { + while (!(valueGet[i] > '0' && valueGet[i] < '9')) { + i++; + } + while (valueGet[i] >= '0' && valueGet[i] < '9') { + int t = valueGet[i] - '0'; + size[index] = size[index] * SIZE_10 + t; + i++; + } + index++; + } + return *size; + } + return APP_MEMORY_SIZE; +} + +bool AbilityManagerService::IsRamConstrainedDevice() +{ + HILOG_INFO("service IsRamConstrainedDevice start"); + const char *key = "const.product.islowram"; + const char *def = "0"; + char *valueGet = nullptr; + unsigned int len = 128; + int ret = GetParameter(key, def, valueGet, len); + if ((ret != GET_PARAMETER_OTHER) && (ret != GET_PARAMETER_INCORRECT)) { + int value = atoi(valueGet); + if (value) { + return true; + } else { + return isRamConstrainedDevice; + } + } + return isRamConstrainedDevice; +} + int AbilityManagerService::GetMissionSaveTime() const { if (!amsConfigResolver_) { diff --git a/services/abilitymgr/src/ability_manager_stub.cpp b/services/abilitymgr/src/ability_manager_stub.cpp index c5ec17c914..46af7b3d49 100644 --- a/services/abilitymgr/src/ability_manager_stub.cpp +++ b/services/abilitymgr/src/ability_manager_stub.cpp @@ -116,6 +116,8 @@ void AbilityManagerStub::SecondStepInit() requestFuncMap_[UPDATE_CONFIGURATION] = &AbilityManagerStub::UpdateConfigurationInner; requestFuncMap_[SET_SHOW_ON_LOCK_SCREEN] = &AbilityManagerStub::SetShowOnLockScreenInner; requestFuncMap_[GET_SYSTEM_MEMORY_ATTR] = &AbilityManagerStub::GetSystemMemoryAttrInner; + requestFuncMap_[GET_APP_MEMORY_SIZE] = &AbilityManagerStub::GetAppMemorySizeInner; + requestFuncMap_[IS_RAM_CONSTRAINED_DEVICE] = &AbilityManagerStub::IsRamConstrainedDeviceInner; requestFuncMap_[CLEAR_UP_APPLICATION_DATA] = &AbilityManagerStub::ClearUpApplicationDataInner; requestFuncMap_[LOCK_MISSION_FOR_CLEANUP] = &AbilityManagerStub::LockMissionForCleanupInner; requestFuncMap_[UNLOCK_MISSION_FOR_CLEANUP] = &AbilityManagerStub::UnlockMissionForCleanupInner; @@ -970,6 +972,27 @@ int AbilityManagerStub::GetSystemMemoryAttrInner(MessageParcel &data, MessagePar return NO_ERROR; } +int AbilityManagerStub::GetAppMemorySizeInner(MessageParcel &data, MessageParcel &reply) +{ + int32_t result = GetAppMemorySize(); + HILOG_INFO("GetAppMemorySizeInner result %{public}d", result); + if (!reply.WriteInt32(result)) { + HILOG_ERROR("GetAppMemorySize error"); + return ERR_INVALID_VALUE; + } + return NO_ERROR; +} + +int AbilityManagerStub::IsRamConstrainedDeviceInner(MessageParcel &data, MessageParcel &reply) +{ + auto result = IsRamConstrainedDevice(); + if (!reply.WriteBool(result)) { + HILOG_ERROR("reply write failed."); + return ERR_INVALID_VALUE; + } + return NO_ERROR; +} + int AbilityManagerStub::ContinueMissionInner(MessageParcel &data, MessageParcel &reply) { std::string srcDeviceId = data.ReadString(); diff --git a/services/test/moduletest/ability_record_test/BUILD.gn b/services/test/moduletest/ability_record_test/BUILD.gn index 6f423903cc..cc69c20331 100644 --- a/services/test/moduletest/ability_record_test/BUILD.gn +++ b/services/test/moduletest/ability_record_test/BUILD.gn @@ -27,6 +27,7 @@ ohos_moduletest("AbilityRecordModuleTest") { "//foundation/aafwk/standard/frameworks/kits/ability/native/include/distributed_ability_runtime", "//foundation/aafwk/standard/interfaces/innerkits/dataobs_manager/include", "//base/hiviewdfx/hicollie/interfaces/native/innerkits/include/xcollie", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara/include", ] sources = [ @@ -94,6 +95,7 @@ ohos_moduletest("AbilityRecordModuleTest") { "${services_path}/common:perm_verification", "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", "//base/global/i18n_standard/frameworks/intl:intl_util", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", "//foundation/aafwk/standard/frameworks/kits/ability/native:dummy_classes", "//foundation/aafwk/standard/interfaces/innerkits/app_manager:app_manager",