From 24b3d44af984dece61e8d53b9307ecb70220a896 Mon Sep 17 00:00:00 2001 From: dy Date: Fri, 25 Feb 2022 20:44:15 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E6=8F=90=E4=BA=A4context=E9=83=A8?= =?UTF-8?q?=E5=88=86Api7=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy --- .../aafwk/featureAbility/napi_context.cpp | 122 +++- .../napi/aafwk/featureAbility/napi_context.h | 42 ++ .../inner/napi_common/napi_common_ability.cpp | 580 ++++++++++++++++++ .../inner/napi_common/napi_common_ability.h | 24 + .../inner/napi_common/napi_common_util.cpp | 23 + .../inner/napi_common/napi_common_util.h | 2 + 6 files changed, 792 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp index 6f73e78312c..537f4b79ad3 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(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_INFO("%{public}s called, parameters is invalid.", __func__); + return nullptr; + } + + if (argc == ARGS_ONE) { + if (!CreateAsyncCallback(env, args[PARAM0], asyncCallbackInfo)) { + HILOG_INFO("%{public}s called, the first parameter is invalid.", __func__); + return nullptr; + } + } + + AsyncParamEx asyncParamEx; + if (asyncCallbackInfo->cbInfo.callback != nullptr) { + HILOG_INFO("%{public}s called. asyncCallback.", __func__); + asyncParamEx.resource = "NAPI_GetFilesDirCallback"; + asyncParamEx.execute = 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_INFO("%{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_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_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_INFO("%{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; +} + /** * @brief Create asynchronous data. * @@ -1480,6 +1785,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 c15bb499a33..553a606d02f 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,20 @@ 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); + /** * @brief Obtains the type of this application. * @@ -73,6 +87,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 d3cecda195f..24e02f5d693 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 @@ -1117,5 +1117,28 @@ std::vector ConvertU8Vector(napi_env env, napi_value jsValue) return result; } +std::vector ConvertStringVector(napi_env env, napi_value jsValue) +{ + bool isTypedArray = false; + if (napi_is_typedarray(env, jsValue, &isTypedArray) != napi_ok || !isTypedArray) { + 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) { + 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 249ce4505fc..4a337f92859 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 @@ -238,6 +238,8 @@ void CompletePromiseCallbackWork(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 -- Gitee From 1d36e4cff6decf22b29215e37cc66437f12237b2 Mon Sep 17 00:00:00 2001 From: dy Date: Sat, 26 Feb 2022 13:49:02 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0napi=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E7=9A=84=E7=BB=93=E6=9E=84=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy --- .../inner/napi_common/feature_ability_common.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 39bf4287bc7..82661408efc 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; }; -- Gitee From ffd15b3d433d2cdaf6bdda32145b83b21941e0cc Mon Sep 17 00:00:00 2001 From: dy Date: Sun, 27 Feb 2022 22:43:41 +0800 Subject: [PATCH 03/11] =?UTF-8?q?abilityApi7=E6=8E=A5=E5=8F=A3=E6=96=B0?= =?UTF-8?q?=E5=A2=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy --- .../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 | 31 +- .../ability/native/src/ability_context.cpp | 11 + .../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 | 13 + .../native/app/include/context_container.h | 16 ++ .../appkit/native/app/include/context_deal.h | 16 ++ .../native/app/src/context_container.cpp | 16 ++ .../appkit/native/app/src/context_deal.cpp | 10 + .../include/ability_manager_interface.h | 14 + .../abilityManager/napi_ability_manager.cpp | 264 ++++++++++++++++++ .../abilityManager/napi_ability_manager.h | 10 + .../aafwk/abilityManager/native_module.cpp | 2 + .../aafwk/featureAbility/napi_context.cpp | 28 ++ .../inner/napi_common/napi_common_ability.cpp | 185 +++++++++++- .../inner/napi_common/napi_common_ability.h | 18 ++ .../inner/napi_common/napi_common_util.cpp | 52 ++++ .../inner/napi_common/napi_common_util.h | 16 ++ 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 | 33 +++ .../abilitymgr/src/ability_manager_stub.cpp | 21 ++ 30 files changed, 866 insertions(+), 9 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 a648d32b426..02354590cd7 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 f66d7cdb33c..13d27f911b9 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_->IsUpdatingConfigurations() : 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 6557de40571..2b6a1b2cf26 100755 --- a/frameworks/kits/ability/native/include/ability.h +++ b/frameworks/kits/ability/native/include/ability.h @@ -159,6 +159,20 @@ public: */ std::shared_ptr GetResourceManager() const override; + /** + * @brief Obtains a resource manager. + * + * @return Returns a ResourceManager object. + */ + bool IsUpdatingConfigurations() override; + + /** + * @brief Obtains a resource manager. + * + * @return Returns a ResourceManager object. + */ + 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 7c4ffcf967c..46799a7de1f 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 96b450aa285..6caf0c59ee2 100755 --- a/frameworks/kits/ability/native/src/ability.cpp +++ b/frameworks/kits/ability/native/src/ability.cpp @@ -170,6 +170,28 @@ std::shared_ptr Ability::GetResourceManager() return AbilityContext::GetResourceManager(); } +/** + * @brief IsUpdatingConfigurations + * + * @return true + * @return false + */ +bool Ability::IsUpdatingConfigurations() +{ + return AbilityContext::IsUpdatingConfigurations(); +} + +/** + * @brief PrintDrawnCompleted + * + * @return true + * @return false + */ +bool Ability::PrintDrawnCompleted() +{ + return AbilityContext::PrintDrawnCompleted(); +} + /** * Will be called when ability start. You should override this function * @@ -241,9 +263,8 @@ void Ability::OnStart(const Want &want) int32_t width = display->GetWidth(); int32_t height = display->GetHeight(); auto configuration = application_->GetConfiguration(); - configuration->AddItem(displayId, ConfigurationInner::APPLICATION_DIRECTION, - GetDirectionStr(height, width)); - configuration->AddItem(displayId, ConfigurationInner::APPLICATION_DENSITYDPI, GetDensityStr(density)); + configuration->AddItem(ConfigurationInner::APPLICATION_DIRECTION, GetDirectionStr(height, width)); + configuration->AddItem(ConfigurationInner::APPLICATION_DENSITYDPI, GetDensityStr(density)); configuration->AddItem(ConfigurationInner::APPLICATION_DISPLAYID, std::to_string(displayId)); UpdateContextConfiguration(); @@ -3466,8 +3487,8 @@ void Ability::OnChange(Rosen::DisplayId displayId, Rosen::DisplayChangeEvent cha // Notify ability Configuration newConfig; - newConfig.AddItem(displayId, ConfigurationInner::APPLICATION_DIRECTION, GetDirectionStr(height, width)); - newConfig.AddItem(displayId, ConfigurationInner::APPLICATION_DENSITYDPI, GetDensityStr(density)); + newConfig.AddItem(ConfigurationInner::APPLICATION_DIRECTION, GetDirectionStr(height, width)); + newConfig.AddItem(ConfigurationInner::APPLICATION_DENSITYDPI, GetDensityStr(density)); std::vector changeKeyV; auto configuration = application_->GetConfiguration(); diff --git a/frameworks/kits/ability/native/src/ability_context.cpp b/frameworks/kits/ability/native/src/ability_context.cpp index 89a120c423f..e308c1d23e4 100644 --- a/frameworks/kits/ability/native/src/ability_context.cpp +++ b/frameworks/kits/ability/native/src/ability_context.cpp @@ -756,5 +756,16 @@ 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 6d7c81be502..0353384ab52 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 isUpdatingConfigurations + * + * @return true or false + */ + virtual bool IsUpdatingConfigurations() = 0; + /** + * @brief PrintDrawnCompleted + * + * @return true or false + */ + + 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 b26827017f1..6b1e823a4d6 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 bd0e3b59344..46c9cd24d68 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 IsUpdatingConfigurations + * + * @return true or false + */ + bool IsUpdatingConfigurations() override; + + /** + * @brief PrintDrawnCompleted + * + * @return true or false + */ + 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 96360e5afd2..d19b5fa5bb3 100644 --- a/frameworks/kits/appkit/native/app/include/context.h +++ b/frameworks/kits/appkit/native/app/include/context.h @@ -717,6 +717,19 @@ public: * */ virtual void SetShowOnLockScreen(bool isAllow) = 0; + + /** + * @brief isUpdatingConfigurations + * + * @return true or false + */ + virtual bool IsUpdatingConfigurations() = 0; + /** + * @brief PrintDrawnCompleted + * + * @return true or false + */ + virtual bool PrintDrawnCompleted() = 0; friend DataAbilityHelper; friend DataShareHelper; diff --git a/frameworks/kits/appkit/native/app/include/context_container.h b/frameworks/kits/appkit/native/app/include/context_container.h index 51827dd13ad..ca73894beee 100755 --- a/frameworks/kits/appkit/native/app/include/context_container.h +++ b/frameworks/kits/appkit/native/app/include/context_container.h @@ -72,6 +72,22 @@ public: */ virtual const std::shared_ptr GetAbilityInfo() override; + /** + * @brief IsUpdatingConfigurations + * + * @return true + * @return false + */ + virtual bool IsUpdatingConfigurations() override; + + /** + * @brief PrintDrawnCompleted + * + * @return true + * @return false + */ + 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 9d3e0bc2ddc..a69f1444924 100755 --- a/frameworks/kits/appkit/native/app/include/context_deal.h +++ b/frameworks/kits/appkit/native/app/include/context_deal.h @@ -174,6 +174,22 @@ public: */ std::string GetCacheDir() override; + /** + * @brief + * + * @return true Is updating configurations + * @return false is not updating configurations + */ + bool IsUpdatingConfigurations() override; + + /** + * @brief + * + * @return true print drawn completed success + * @return false print drawn completed 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, diff --git a/frameworks/kits/appkit/native/app/src/context_container.cpp b/frameworks/kits/appkit/native/app/src/context_container.cpp index a63cb549b9d..6cf49fa6bce 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 d495386d0bd..c09a48dfe3e 100644 --- a/frameworks/kits/appkit/native/app/src/context_deal.cpp +++ b/frameworks/kits/appkit/native/app/src/context_deal.cpp @@ -309,6 +309,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, diff --git a/interfaces/innerkits/ability_manager/include/ability_manager_interface.h b/interfaces/innerkits/ability_manager/include/ability_manager_interface.h index 5a72c298931..ea33c2478c8 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 8f6613b9fea..0a69a2c0b28 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_ERROR("%{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 63ef91ff3c7..4a6a6dbc271 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 63dbcc65307..088ba3be018 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/napi_context.cpp b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp index 537f4b79ad3..0f1735bc3bd 100644 --- a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp @@ -3039,6 +3039,32 @@ napi_value NAPI_GetPreferencesDirSync(napi_env env, napi_callback_info info) return ret; } +/** + * @brief + * + * @param env + * @param info + * @return napi_value + */ +napi_value NAPI_IsUpdatingConfigurations(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called", __func__); + return NAPI_IsUpdatingConfigurationsCommon(env, info, AbilityType::PAGE); +} + +/** + * @brief + * + * @param env + * @param info + * @return napi_value + */ +napi_value NAPI_PrintDrawnCompleted(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called", __func__); + return NAPI_PrintDrawnCompletedCommon(env, info, AbilityType::PAGE); +} + /** * @brief Context NAPI module registration. * @@ -3066,6 +3092,8 @@ napi_value ContextPermissionInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getCallingBundle", NAPI_GetCallingBundle), DECLARE_NAPI_FUNCTION("getOrCreateLocalDir", NAPI_GetOrCreateLocalDir), DECLARE_NAPI_FUNCTION("getFilesDir", NAPI_GetFilesDir), + DECLARE_NAPI_FUNCTION("isUpdatingConfigurations", NAPI_IsUpdatingConfigurations), + DECLARE_NAPI_FUNCTION("printDrawnCompleted", NAPI_PrintDrawnCompleted), DECLARE_NAPI_FUNCTION("getDatabaseDirSync", NAPI_GetDatabaseDirSync), DECLARE_NAPI_FUNCTION("getPreferencesDirSync", NAPI_GetPreferencesDirSync), DECLARE_NAPI_FUNCTION("getCacheDir", NAPI_GetCacheDir), 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 c5f01832a39..60cc344fdf3 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__); @@ -708,6 +765,126 @@ napi_value NAPI_GetExternalCacheDirCommon(napi_env env, napi_callback_info info, 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. * 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 553a606d02f..55551423433 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 @@ -56,7 +56,25 @@ napi_value NAPI_GetOrCreateDistributedDirCommon(napi_env env, napi_callback_info 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); +/** + * @brief + * + * @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_IsUpdatingConfigurationsCommon(napi_env env, napi_callback_info info, AbilityType abilityType); +/** + * @brief + * + * @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_PrintDrawnCompletedCommon(napi_env env, napi_callback_info info, AbilityType abilityType); /** * @brief Obtains the type of this application. * 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 24e02f5d693..138178c3ff0 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; 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 4a337f92859..2aed629c92a 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,6 +244,14 @@ 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); diff --git a/services/abilitymgr/BUILD.gn b/services/abilitymgr/BUILD.gn index 99e4e269f86..c3cc3a7ce70 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") { @@ -100,6 +101,7 @@ ohos_shared_library("abilityms") { "//foundation/multimedia/image_standard/interfaces/innerkits:image_native", "//third_party/libpng:libpng", "//utils/native/base:utils", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", ] external_deps = [ diff --git a/services/abilitymgr/include/ability_manager_proxy.h b/services/abilitymgr/include/ability_manager_proxy.h index ae0ddce94c8..ea5ba87c932 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 1801d6dea4f..d4a1ea62c83 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 2ec98a94ac7..402f451790d 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 bfafa7d5d21..955120622e8 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 INNER_ERR; + } + 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 error; + } + 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 a14b44ad042..e019d332cfe 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -52,6 +52,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 +71,10 @@ 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 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"; @@ -3388,6 +3393,34 @@ void AbilityManagerService::GetSystemMemoryAttr(AppExecFwk::SystemMemoryAttr &me appScheduler->GetSystemMemoryAttr(memoryInfo, memConfig); } +int AbilityManagerService::GetAppMemorySize() +{ + const char *key = "const.product.dalvikheaplimit"; + const char *def = "10.1.0"; + char *valueGet = nullptr; + unsigned int len = 128; + int ret = GetParameter(key, def, valueGet, len); + if (valueGet != nullptr) { + HILOG_INFO("AbilityManagerService GetAppMemorySize value is not nullptr"); + return ret; + } + return APP_MEMORY_SIZE; +} + +bool AbilityManagerService::IsRamConstrainedDevice() +{ + const char *key = "const.product.islowram"; + const char *def = "10.1.0"; + char *valueGet = nullptr; + unsigned int len = 128; + GetParameter(key, def, valueGet, len); + if (valueGet != nullptr) { + HILOG_INFO("AbilityManagerService IsRamConstrainedDevice value is not nullptr"); + return true; + } + 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 c5ec17c9147..f6d09fb41ef 100644 --- a/services/abilitymgr/src/ability_manager_stub.cpp +++ b/services/abilitymgr/src/ability_manager_stub.cpp @@ -970,6 +970,27 @@ int AbilityManagerStub::GetSystemMemoryAttrInner(MessageParcel &data, MessagePar return NO_ERROR; } +int AbilityManagerStub::GetAppMemorySizeInner(MessageParcel &data, MessageParcel &reply) +{ + int32_t result = GetAppMemorySize(); + HILOG_ERROR("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(); -- Gitee From 0a6fb6ac5249c6d0132768f318d0f0a8e0a0e5fb Mon Sep 17 00:00:00 2001 From: dy Date: Sun, 27 Feb 2022 23:22:52 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy --- .../src/ability_context_impl.cpp | 4 ++-- .../ability/native/include/ability_context.h | 4 ++-- .../kits/ability/native/src/ability.cpp | 16 +++++++------- .../ability/native/src/ability_context.cpp | 1 - .../native/ability_runtime/context/context.h | 10 ++++----- .../ability_runtime/context/context_impl.cpp | 2 +- .../ability_runtime/context/context_impl.h | 4 ++-- .../kits/appkit/native/app/include/context.h | 14 ++++++------ .../native/app/include/context_container.h | 22 +++++++++---------- .../appkit/native/app/include/context_deal.h | 8 +++---- .../native/app/src/context_container.cpp | 4 ++-- .../appkit/native/app/src/context_deal.cpp | 2 +- .../aafwk/featureAbility/napi_context.cpp | 20 ++++++++--------- .../inner/napi_common/napi_common_ability.cpp | 3 ++- .../inner/napi_common/napi_common_ability.h | 4 ++-- 15 files changed, 59 insertions(+), 59 deletions(-) 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 13d27f911b9..1664959cfd8 100644 --- a/frameworks/kits/ability/ability_runtime/src/ability_context_impl.cpp +++ b/frameworks/kits/ability/ability_runtime/src/ability_context_impl.cpp @@ -66,12 +66,12 @@ std::string AbilityContextImpl::GetDistributedFilesDir() bool AbilityContextImpl::IsUpdatingConfigurations() { - return stageContext_ ? stageContext_->IsUpdatingConfigurations() : false; + return stageContext_ ? stageContext_->IsUpdatingConfigurations() : false; } bool AbilityContextImpl::PrintDrawnCompleted() { - return stageContext_ ? stageContext_->IsUpdatingConfigurations() : false; + return stageContext_ ? stageContext_->IsUpdatingConfigurations() : false; } void AbilityContextImpl::SwitchArea(int mode) diff --git a/frameworks/kits/ability/native/include/ability_context.h b/frameworks/kits/ability/native/include/ability_context.h index 46799a7de1f..c916da960fb 100644 --- a/frameworks/kits/ability/native/include/ability_context.h +++ b/frameworks/kits/ability/native/include/ability_context.h @@ -67,14 +67,14 @@ public: /** * @brief IsUpdatingConfigurations - * + * * @return true or false */ bool IsUpdatingConfigurations() override; /** * @brief PrintDrawnCompleted - * + * * @return true or false */ bool PrintDrawnCompleted() override; diff --git a/frameworks/kits/ability/native/src/ability.cpp b/frameworks/kits/ability/native/src/ability.cpp index 6caf0c59ee2..e1e2b6bc2ba 100755 --- a/frameworks/kits/ability/native/src/ability.cpp +++ b/frameworks/kits/ability/native/src/ability.cpp @@ -172,22 +172,22 @@ std::shared_ptr Ability::GetResourceManager() /** * @brief IsUpdatingConfigurations - * - * @return true - * @return false + * + * @return true + * @return false */ -bool Ability::IsUpdatingConfigurations() +bool Ability::IsUpdatingConfigurations() { return AbilityContext::IsUpdatingConfigurations(); } /** * @brief PrintDrawnCompleted - * - * @return true - * @return false + * + * @return true + * @return false */ -bool Ability::PrintDrawnCompleted() +bool Ability::PrintDrawnCompleted() { return AbilityContext::PrintDrawnCompleted(); } diff --git a/frameworks/kits/ability/native/src/ability_context.cpp b/frameworks/kits/ability/native/src/ability_context.cpp index e308c1d23e4..f30357e733a 100644 --- a/frameworks/kits/ability/native/src/ability_context.cpp +++ b/frameworks/kits/ability/native/src/ability_context.cpp @@ -766,6 +766,5 @@ 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 0353384ab52..e644be9fe2d 100755 --- a/frameworks/kits/appkit/native/ability_runtime/context/context.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/context.h @@ -121,16 +121,16 @@ public: /** * @brief isUpdatingConfigurations - * - * @return true or false + * + * @return true or false */ virtual bool IsUpdatingConfigurations() = 0; + /** * @brief PrintDrawnCompleted - * - * @return true or false + * + * @return true or false */ - virtual bool PrintDrawnCompleted() = 0; /** 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 6b1e823a4d6..732cd457655 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp +++ b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.cpp @@ -92,7 +92,7 @@ bool ContextImpl::IsUpdatingConfigurations() bool ContextImpl::PrintDrawnCompleted() { - return false; + return false; } std::string ContextImpl::GetDatabaseDir() 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 46c9cd24d68..783626319f5 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h @@ -54,14 +54,14 @@ public: /** * @brief IsUpdatingConfigurations - * + * * @return true or false */ bool IsUpdatingConfigurations() override; /** * @brief PrintDrawnCompleted - * + * * @return true or false */ bool PrintDrawnCompleted() override; diff --git a/frameworks/kits/appkit/native/app/include/context.h b/frameworks/kits/appkit/native/app/include/context.h index d19b5fa5bb3..2b193d5694d 100644 --- a/frameworks/kits/appkit/native/app/include/context.h +++ b/frameworks/kits/appkit/native/app/include/context.h @@ -717,18 +717,18 @@ public: * */ virtual void SetShowOnLockScreen(bool isAllow) = 0; - - /** + + /** * @brief isUpdatingConfigurations - * - * @return true or false + * + * @return true or false */ virtual bool IsUpdatingConfigurations() = 0; /** * @brief PrintDrawnCompleted - * - * @return true or false - */ + * + * @return true or false + */ virtual bool PrintDrawnCompleted() = 0; friend DataAbilityHelper; diff --git a/frameworks/kits/appkit/native/app/include/context_container.h b/frameworks/kits/appkit/native/app/include/context_container.h index ca73894beee..3620898ed08 100755 --- a/frameworks/kits/appkit/native/app/include/context_container.h +++ b/frameworks/kits/appkit/native/app/include/context_container.h @@ -72,21 +72,21 @@ public: */ virtual const std::shared_ptr GetAbilityInfo() override; - /** - * @brief IsUpdatingConfigurations - * - * @return true - * @return false - */ - virtual bool IsUpdatingConfigurations() override; + /** + * @brief IsUpdatingConfigurations + * + * @return true + * @return false + */ + virtual bool IsUpdatingConfigurations() override; /** * @brief PrintDrawnCompleted - * - * @return true - * @return false + * + * @return true + * @return false */ - virtual bool PrintDrawnCompleted() override; + 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 a69f1444924..3b6125dd9ba 100755 --- a/frameworks/kits/appkit/native/app/include/context_deal.h +++ b/frameworks/kits/appkit/native/app/include/context_deal.h @@ -175,16 +175,16 @@ public: std::string GetCacheDir() override; /** - * @brief - * + * @brief + * * @return true Is updating configurations * @return false is not updating configurations */ bool IsUpdatingConfigurations() override; /** - * @brief - * + * @brief + * * @return true print drawn completed success * @return false print drawn completed fail */ diff --git a/frameworks/kits/appkit/native/app/src/context_container.cpp b/frameworks/kits/appkit/native/app/src/context_container.cpp index 6cf49fa6bce..bc744977f8f 100644 --- a/frameworks/kits/appkit/native/app/src/context_container.cpp +++ b/frameworks/kits/appkit/native/app/src/context_container.cpp @@ -1133,7 +1133,7 @@ void ContextContainer::SetShowOnLockScreen(bool isAllow) bool ContextContainer::IsUpdatingConfigurations() { - if (baseContext_ != nullptr) { + if (baseContext_ != nullptr) { return baseContext_->IsUpdatingConfigurations(); } return false; @@ -1141,7 +1141,7 @@ bool ContextContainer::IsUpdatingConfigurations() bool ContextContainer::PrintDrawnCompleted() { - if (baseContext_ != nullptr) { + if (baseContext_ != nullptr) { return baseContext_->PrintDrawnCompleted(); } return false; diff --git a/frameworks/kits/appkit/native/app/src/context_deal.cpp b/frameworks/kits/appkit/native/app/src/context_deal.cpp index c09a48dfe3e..7d51b85f708 100644 --- a/frameworks/kits/appkit/native/app/src/context_deal.cpp +++ b/frameworks/kits/appkit/native/app/src/context_deal.cpp @@ -316,7 +316,7 @@ bool ContextDeal::IsUpdatingConfigurations() bool ContextDeal::PrintDrawnCompleted() { - return false; + return false; } /** diff --git a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp index 0f1735bc3bd..938f8520896 100644 --- a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp @@ -3040,11 +3040,11 @@ napi_value NAPI_GetPreferencesDirSync(napi_env env, napi_callback_info info) } /** - * @brief - * - * @param env - * @param info - * @return napi_value + * @brief + * + * @param env + * @param info + * @return napi_value */ napi_value NAPI_IsUpdatingConfigurations(napi_env env, napi_callback_info info) { @@ -3053,11 +3053,11 @@ napi_value NAPI_IsUpdatingConfigurations(napi_env env, napi_callback_info info) } /** - * @brief - * - * @param env - * @param info - * @return napi_value + * @brief + * + * @param env + * @param info + * @return napi_value */ napi_value NAPI_PrintDrawnCompleted(napi_env env, napi_callback_info info) { 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 60cc344fdf3..c3d34af4763 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 @@ -765,7 +765,8 @@ napi_value NAPI_GetExternalCacheDirCommon(napi_env env, napi_callback_info info, return ret; } -napi_value NAPI_IsUpdatingConfigurationsWrap(napi_env env, napi_callback_info info, AsyncJSCallbackInfo *asyncCallbackInfo) +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; 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 55551423433..79835f900a0 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 @@ -57,7 +57,7 @@ napi_value NAPI_GetCacheDirCommon(napi_env env, napi_callback_info info, Ability napi_value NAPI_GetExternalCacheDirCommon(napi_env env, napi_callback_info info, AbilityType abilityType); /** - * @brief + * @brief * * @param env The environment that the Node-API call is invoked under. * @param info The callback info passed into the callback function. @@ -67,7 +67,7 @@ napi_value NAPI_GetExternalCacheDirCommon(napi_env env, napi_callback_info info, napi_value NAPI_IsUpdatingConfigurationsCommon(napi_env env, napi_callback_info info, AbilityType abilityType); /** - * @brief + * @brief * * @param env The environment that the Node-API call is invoked under. * @param info The callback info passed into the callback function. -- Gitee From 964b56806cb65a8f07d18114cec7db9c26282af7 Mon Sep 17 00:00:00 2001 From: dy Date: Mon, 28 Feb 2022 12:05:47 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E6=96=B0=E5=A2=9Etest=E4=B8=AD=E5=BC=95?= =?UTF-8?q?=E5=85=A5syspara?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy --- services/test/moduletest/ability_record_test/BUILD.gn | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/test/moduletest/ability_record_test/BUILD.gn b/services/test/moduletest/ability_record_test/BUILD.gn index e59b1ed2936..fbe2657ab94 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 = [ @@ -111,6 +112,7 @@ ohos_moduletest("AbilityRecordModuleTest") { "//third_party/jsoncpp:jsoncpp", "//third_party/libpng:libpng", "//utils/native/base:utilsbase", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", ] external_deps = [ -- Gitee From 09f1e43629aa72200c40b97794da92820a4505c4 Mon Sep 17 00:00:00 2001 From: dy Date: Tue, 1 Mar 2022 02:18:48 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0contextapi=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy --- .../kits/ability/native/include/ability.h | 10 +- .../kits/ability/native/src/ability.cpp | 10 +- .../native/ability_runtime/context/context.h | 10 +- .../ability_runtime/context/context_impl.h | 8 +- .../kits/appkit/native/app/include/context.h | 9 +- .../native/app/include/context_container.h | 10 +- .../appkit/native/app/include/context_deal.h | 24 +- .../appkit/native/app/src/context_deal.cpp | 63 +- .../abilityManager/napi_ability_manager.cpp | 2 +- .../kits/napi/aafwk/featureAbility/BUILD.gn | 3 + .../aafwk/featureAbility/napi_context.cpp | 18 +- .../napi_data_ability_helper.cpp | 1106 +++++++++++++++++ .../featureAbility/napi_data_ability_helper.h | 2 + .../napi_common/feature_ability_common.h | 4 +- .../inner/napi_common/napi_common_ability.cpp | 14 +- .../inner/napi_common/napi_common_ability.h | 18 +- .../inner/napi_common/napi_common_util.cpp | 5 +- .../kits/napi/aafwk/particleAbility/BUILD.gn | 3 + .../abilitymgr/src/ability_manager_proxy.cpp | 4 +- .../src/ability_manager_service.cpp | 40 +- .../abilitymgr/src/ability_manager_stub.cpp | 4 +- 21 files changed, 1282 insertions(+), 85 deletions(-) diff --git a/frameworks/kits/ability/native/include/ability.h b/frameworks/kits/ability/native/include/ability.h index aeb130ab64b..40a7746fc22 100755 --- a/frameworks/kits/ability/native/include/ability.h +++ b/frameworks/kits/ability/native/include/ability.h @@ -160,16 +160,16 @@ public: std::shared_ptr GetResourceManager() const override; /** - * @brief Obtains a resource manager. + * @brief Checks whether the configuration of this ability is changing. * - * @return Returns a ResourceManager object. + * @return Returns true if the configuration of this ability is changing and false otherwise. */ bool IsUpdatingConfigurations() override; - /** - * @brief Obtains a resource manager. + /** + * @brief Informs the system of the time required for drawing this Page ability. * - * @return Returns a ResourceManager object. + * @return Returns the notification is successful or fail */ bool PrintDrawnCompleted() override; diff --git a/frameworks/kits/ability/native/src/ability.cpp b/frameworks/kits/ability/native/src/ability.cpp index 5bfe1a3a9d9..950376c2477 100755 --- a/frameworks/kits/ability/native/src/ability.cpp +++ b/frameworks/kits/ability/native/src/ability.cpp @@ -171,10 +171,9 @@ std::shared_ptr Ability::GetResourceManager() } /** - * @brief IsUpdatingConfigurations + * @brief Checks whether the configuration of this ability is changing. * - * @return true - * @return false + * @return Returns true if the configuration of this ability is changing and false otherwise. */ bool Ability::IsUpdatingConfigurations() { @@ -182,10 +181,9 @@ bool Ability::IsUpdatingConfigurations() } /** - * @brief PrintDrawnCompleted + * @brief Informs the system of the time required for drawing this Page ability. * - * @return true - * @return false + * @return Returns the notification is successful or fail */ bool Ability::PrintDrawnCompleted() { diff --git a/frameworks/kits/appkit/native/ability_runtime/context/context.h b/frameworks/kits/appkit/native/ability_runtime/context/context.h index e644be9fe2d..76ed6972852 100755 --- a/frameworks/kits/appkit/native/ability_runtime/context/context.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/context.h @@ -120,16 +120,16 @@ public: virtual std::string GetFilesDir() = 0; /** - * @brief isUpdatingConfigurations + * @brief Checks whether the configuration of this ability is changing. * - * @return true or false + * @return Returns true if the configuration of this ability is changing and false otherwise. */ virtual bool IsUpdatingConfigurations() = 0; - + /** - * @brief PrintDrawnCompleted + * @brief Informs the system of the time required for drawing this Page ability. * - * @return true or false + * @return Returns the notification is successful or fail */ virtual bool PrintDrawnCompleted() = 0; 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 783626319f5..2c18c436a54 100644 --- a/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h +++ b/frameworks/kits/appkit/native/ability_runtime/context/context_impl.h @@ -53,16 +53,16 @@ public: std::string GetCacheDir() override; /** - * @brief IsUpdatingConfigurations + * @brief Checks whether the configuration of this ability is changing. * - * @return true or false + * @return Returns true if the configuration of this ability is changing and false otherwise. */ bool IsUpdatingConfigurations() override; /** - * @brief PrintDrawnCompleted + * @brief Informs the system of the time required for drawing this Page ability. * - * @return true or false + * @return Returns the notification is successful or fail */ bool PrintDrawnCompleted() override; diff --git a/frameworks/kits/appkit/native/app/include/context.h b/frameworks/kits/appkit/native/app/include/context.h index 2b193d5694d..e802af4e421 100644 --- a/frameworks/kits/appkit/native/app/include/context.h +++ b/frameworks/kits/appkit/native/app/include/context.h @@ -719,15 +719,16 @@ public: virtual void SetShowOnLockScreen(bool isAllow) = 0; /** - * @brief isUpdatingConfigurations + * @brief Checks whether the configuration of this ability is changing. * - * @return true or false + * @return Returns true if the configuration of this ability is changing and false otherwise. */ virtual bool IsUpdatingConfigurations() = 0; + /** - * @brief PrintDrawnCompleted + * @brief Informs the system of the time required for drawing this Page ability. * - * @return true or false + * @return Returns the notification is successful or fail */ virtual bool PrintDrawnCompleted() = 0; diff --git a/frameworks/kits/appkit/native/app/include/context_container.h b/frameworks/kits/appkit/native/app/include/context_container.h index 3620898ed08..3dfc6e05b70 100755 --- a/frameworks/kits/appkit/native/app/include/context_container.h +++ b/frameworks/kits/appkit/native/app/include/context_container.h @@ -73,18 +73,16 @@ public: virtual const std::shared_ptr GetAbilityInfo() override; /** - * @brief IsUpdatingConfigurations + * @brief Checks whether the configuration of this ability is changing. * - * @return true - * @return false + * @return Returns true if the configuration of this ability is changing and false otherwise. */ virtual bool IsUpdatingConfigurations() override; /** - * @brief PrintDrawnCompleted + * @brief Informs the system of the time required for drawing this Page ability. * - * @return true - * @return false + * @return Returns the notification is successful or fail */ virtual bool PrintDrawnCompleted() override; diff --git a/frameworks/kits/appkit/native/app/include/context_deal.h b/frameworks/kits/appkit/native/app/include/context_deal.h index 3b6125dd9ba..71c21c3dda2 100755 --- a/frameworks/kits/appkit/native/app/include/context_deal.h +++ b/frameworks/kits/appkit/native/app/include/context_deal.h @@ -175,18 +175,16 @@ public: std::string GetCacheDir() override; /** - * @brief + * @brief Checks whether the configuration of this ability is changing. * - * @return true Is updating configurations - * @return false is not updating configurations + * @return Returns true if the configuration of this ability is changing and false otherwise. */ bool IsUpdatingConfigurations() override; /** - * @brief + * @brief Informs the system of the time required for drawing this Page ability. * - * @return true print drawn completed success - * @return false print drawn completed fail + * @return Returns the notification is successful or fail */ bool PrintDrawnCompleted() override; @@ -748,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; @@ -776,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_deal.cpp b/frameworks/kits/appkit/native/app/src/context_deal.cpp index 7d51b85f708..907486479fd 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) {} @@ -631,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 @@ -640,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. @@ -687,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/kits/napi/aafwk/abilityManager/napi_ability_manager.cpp b/interfaces/kits/napi/aafwk/abilityManager/napi_ability_manager.cpp index 0a69a2c0b28..4aa93b8b617 100644 --- a/interfaces/kits/napi/aafwk/abilityManager/napi_ability_manager.cpp +++ b/interfaces/kits/napi/aafwk/abilityManager/napi_ability_manager.cpp @@ -2244,7 +2244,7 @@ static void GetAppMemorySizeExecute(napi_env env, void *data) { CallbackInfo *cb = static_cast(data); cb->result = GetAbilityManagerInstance()->GetAppMemorySize(); - HILOG_ERROR("%{public}s, result = %{public}d", __func__, cb->result); + HILOG_INFO("%{public}s, result = %{public}d", __func__, cb->result); } static void GetAppMemorySizePromiseComplete(napi_env env, napi_status status, void *data) diff --git a/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn b/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn index a8503a0897b..af74e0fdb60 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 = [ @@ -51,6 +53,7 @@ ohos_shared_library("featureability") { "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", "//third_party/libuv:uv_static", "//utils/native/base:utils", + "//third_party/jsoncpp:jsoncpp", ] external_deps = [ diff --git a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp index 938f8520896..2ae57a00d06 100644 --- a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp @@ -3040,11 +3040,12 @@ napi_value NAPI_GetPreferencesDirSync(napi_env env, napi_callback_info info) } /** - * @brief + * @brief Checks whether the configuration of this ability is changing. * - * @param env - * @param info - * @return napi_value + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return Returns true if the configuration of this ability is changing and false otherwise. */ napi_value NAPI_IsUpdatingConfigurations(napi_env env, napi_callback_info info) { @@ -3053,11 +3054,12 @@ napi_value NAPI_IsUpdatingConfigurations(napi_env env, napi_callback_info info) } /** - * @brief + * @brief Informs the system of the time required for drawing this Page ability. * - * @param env - * @param info - * @return napi_value + * @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_PrintDrawnCompleted(napi_env env, napi_callback_info info) { diff --git a/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.cpp b/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.cpp index 5335c41e15a..9935ba7b24f 100644 --- a/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.cpp @@ -17,6 +17,7 @@ #include #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,1109 @@ 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() >= 2) { + 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() >= 2) { + 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, "123##*##abc"); + if (pacValue != "123##*##abc") { + 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); + } + + if (ret == nullptr) { + HILOG_ERROR("%{public}s,ret == nullptr", __func__); + if (updateCB != nullptr) { + delete updateCB; + updateCB = nullptr; + } + ret = WrapVoidToJS(env); + } + 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 f759e1126c3..234af349d78 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 82661408efc..750d423d8d8 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 @@ -210,7 +210,7 @@ struct HapModuleInfoCB { HapModuleInfo_ hapModuleInfo; }; -struct AppVersionInfo_ { +struct AppVersionInfo { std::string appName; std::string versionName; int32_t versionCode = 0; @@ -218,7 +218,7 @@ struct AppVersionInfo_ { struct AppVersionInfoCB { CBBase cbBase; - AppVersionInfo_ appVersionInfo; + AppVersionInfo appVersionInfo; }; struct DataAbilityHelperCB { 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 c3d34af4763..8f5ce8b92ab 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 @@ -509,13 +509,13 @@ napi_value NAPI_GetOrCreateDistributedDirWrap( 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__); + HILOG_ERROR("%{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__); + HILOG_ERROR("%{public}s called, the first parameter is invalid.", __func__); return nullptr; } } @@ -543,7 +543,7 @@ napi_value NAPI_GetOrCreateDistributedDirCommon(napi_env env, napi_callback_info HILOG_INFO("%{public}s called", __func__); AsyncJSCallbackInfo *asyncCallbackInfo = CreateAsyncJSCallbackInfo(env); if (asyncCallbackInfo == nullptr) { - HILOG_INFO("%{public}s called. Invoke CreateAsyncJSCallbackInfo failed.", __func__); + HILOG_ERROR("%{public}s called. Invoke CreateAsyncJSCallbackInfo failed.", __func__); return WrapVoidToJS(env); } @@ -612,13 +612,13 @@ napi_value NAPI_GetCacheDirWrap(napi_env env, napi_callback_info info, AsyncJSCa 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__); + HILOG_ERROR("%{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__); + HILOG_ERROR("%{public}s called, the first parameter is invalid.", __func__); return nullptr; } } @@ -646,7 +646,7 @@ napi_value NAPI_GetCacheDirCommon(napi_env env, napi_callback_info info, Ability HILOG_INFO("%{public}s called", __func__); AsyncJSCallbackInfo *asyncCallbackInfo = CreateAsyncJSCallbackInfo(env); if (asyncCallbackInfo == nullptr) { - HILOG_INFO("%{public}s called. Invoke CreateAsyncJSCallbackInfo failed.", __func__); + HILOG_ERROR("%{public}s called. Invoke CreateAsyncJSCallbackInfo failed.", __func__); return WrapVoidToJS(env); } @@ -1996,7 +1996,7 @@ AppVersionInfoCB *CreateAppVersionInfoCBInfo(napi_env env) return appVersionInfoCB; } -void SaveAppVersionInfo(AppVersionInfo_ &appVersionInfo, const std::string appName, const std::string versionName, +void SaveAppVersionInfo(AppVersionInfo &appVersionInfo, const std::string appName, const std::string versionName, const int32_t versionCode) { HILOG_INFO("%{public}s called.", __func__); 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 79835f900a0..6e5c231003e 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 @@ -56,25 +56,11 @@ napi_value NAPI_GetOrCreateDistributedDirCommon(napi_env env, napi_callback_info 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); -/** - * @brief - * - * @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_IsUpdatingConfigurationsCommon(napi_env env, napi_callback_info info, AbilityType abilityType); -/** - * @brief - * - * @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_PrintDrawnCompletedCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + /** * @brief Obtains the type of this application. * 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 138178c3ff0..f1a5de4aaa5 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 @@ -1172,7 +1172,9 @@ std::vector ConvertU8Vector(napi_env env, napi_value jsValue) std::vector ConvertStringVector(napi_env env, napi_value jsValue) { bool isTypedArray = false; - if (napi_is_typedarray(env, jsValue, &isTypedArray) != napi_ok || !isTypedArray) { + 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 {}; } @@ -1182,6 +1184,7 @@ std::vector ConvertStringVector(napi_env env, napi_value jsValue) 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; diff --git a/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn b/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn index fd38c59346c..750e463fc57 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 = [ @@ -50,6 +52,7 @@ ohos_shared_library("particleability") { "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", "//third_party/libuv:uv_static", "//utils/native/base:utils", + "//third_party/jsoncpp:jsoncpp", ] external_deps = [ diff --git a/services/abilitymgr/src/ability_manager_proxy.cpp b/services/abilitymgr/src/ability_manager_proxy.cpp index 955120622e8..90b1bd0b434 100644 --- a/services/abilitymgr/src/ability_manager_proxy.cpp +++ b/services/abilitymgr/src/ability_manager_proxy.cpp @@ -1662,12 +1662,12 @@ bool AbilityManagerProxy::IsRamConstrainedDevice() MessageOption option; if (!WriteInterfaceToken(data)) { HILOG_ERROR("WriteInterfaceToken faild"); - return INNER_ERR; + 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 error; + return false; } return reply.ReadBool(); } diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 6fa6befb5b1..e04508f6721 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" @@ -72,6 +73,8 @@ 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 bool isRamConstrainedDevice = false; const std::string APP_MEMORY_MAX_SIZE_PARAMETER = "const.product.dalvikheaplimit"; const std::string RAM_CONSTRAINED_DEVICE_SIGN = "const.product.islowram"; @@ -3396,28 +3399,47 @@ void AbilityManagerService::GetSystemMemoryAttr(AppExecFwk::SystemMemoryAttr &me int AbilityManagerService::GetAppMemorySize() { + HILOG_INFO("service GetAppMemorySize start"); const char *key = "const.product.dalvikheaplimit"; - const char *def = "10.1.0"; + const char *def = "512m"; char *valueGet = nullptr; unsigned int len = 128; int ret = GetParameter(key, def, valueGet, len); - if (valueGet != nullptr) { - HILOG_INFO("AbilityManagerService GetAppMemorySize value is not nullptr"); - return ret; + 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] * 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 = "10.1.0"; + const char *def = "0"; char *valueGet = nullptr; unsigned int len = 128; - GetParameter(key, def, valueGet, len); - if (valueGet != nullptr) { - HILOG_INFO("AbilityManagerService IsRamConstrainedDevice value is not nullptr"); - return true; + 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; } diff --git a/services/abilitymgr/src/ability_manager_stub.cpp b/services/abilitymgr/src/ability_manager_stub.cpp index f6d09fb41ef..46af7b3d49d 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; @@ -973,7 +975,7 @@ int AbilityManagerStub::GetSystemMemoryAttrInner(MessageParcel &data, MessagePar int AbilityManagerStub::GetAppMemorySizeInner(MessageParcel &data, MessageParcel &reply) { int32_t result = GetAppMemorySize(); - HILOG_ERROR("GetAppMemorySizeInner result %{public}d", result); + HILOG_INFO("GetAppMemorySizeInner result %{public}d", result); if (!reply.WriteInt32(result)) { HILOG_ERROR("GetAppMemorySize error"); return ERR_INVALID_VALUE; -- Gitee From cf80a458153f48dc297fb965da6cbad8127bd96c Mon Sep 17 00:00:00 2001 From: dy Date: Tue, 1 Mar 2022 11:08:24 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy --- .../appkit/native/app/src/context_deal.cpp | 2 +- .../napi_data_ability_helper.cpp | 21 ++++--------------- .../src/ability_manager_service.cpp | 3 ++- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/frameworks/kits/appkit/native/app/src/context_deal.cpp b/frameworks/kits/appkit/native/app/src/context_deal.cpp index 907486479fd..4f0f3835a54 100644 --- a/frameworks/kits/appkit/native/app/src/context_deal.cpp +++ b/frameworks/kits/appkit/native/app/src/context_deal.cpp @@ -743,7 +743,7 @@ std::shared_ptr ContextDeal::GetHapModuleInfo() want.SetElement(name); std::vector abilityInfos; bool isSuc = ptr->QueryAbilityInfos(want, abilityInfos); - if(isSuc){ + if (isSuc) { hapModuleInfoLocal_->abilityInfos = abilityInfos; } APP_LOGI("ContextDeal::GetHapModuleInfo end"); diff --git a/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.cpp b/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.cpp index 9935ba7b24f..30c6232ea81 100644 --- a/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.cpp @@ -2623,7 +2623,7 @@ void ParseIn(AppExecFwk::PacMap &pacMap, std::string &value_str, NativeRdb::Data HILOG_INFO("ParseJsonKey in value_str1:%{public}s ", inValue.c_str()); std::vector value_vec; Stringsplit(inValue, ",", value_vec); - if (value_vec.size() >= 2) { + if (value_vec.size() >= ARGS_TWO) { std::vector in_vec; in_vec.push_back(value_vec[0]); in_vec.push_back(value_vec[1]); @@ -2649,7 +2649,7 @@ void ParseNotIn(AppExecFwk::PacMap &pacMap, std::string &value_str, NativeRdb::D HILOG_INFO("ParseJsonKey notIn value_str1:%{public}s ", inValue.c_str()); std::vector value_vec; Stringsplit(inValue, ",", value_vec); - if (value_vec.size() >= 2) { + if (value_vec.size() >= ARGS_TWO) { std::vector in_vec; in_vec.push_back(value_vec[0]); in_vec.push_back(value_vec[1]); @@ -3210,7 +3210,6 @@ napi_value CallUpdateWrap(napi_env env, napi_value thisVar, napi_callback_info i 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) { @@ -3224,17 +3223,15 @@ napi_value CallUpdateWrap(napi_env env, napi_value thisVar, napi_callback_info i 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, "123##*##abc"); - if (pacValue != "123##*##abc") { + 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)); @@ -3244,22 +3241,12 @@ napi_value CallUpdateWrap(napi_env env, napi_value thisVar, napi_callback_info i 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); } - - if (ret == nullptr) { - HILOG_ERROR("%{public}s,ret == nullptr", __func__); - if (updateCB != nullptr) { - delete updateCB; - updateCB = nullptr; - } - ret = WrapVoidToJS(env); - } HILOG_INFO("%{public}s,end", __func__); return ret; } diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index e04508f6721..6bcd127db08 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -75,6 +75,7 @@ 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 = 10; 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"; @@ -3415,7 +3416,7 @@ int AbilityManagerService::GetAppMemorySize() } while (valueGet[i] >= '0' && valueGet[i] < '9') { int t = valueGet[i] - '0'; - size[index] = size[index] * 10 + t; + size[index] = size[index] * SIZE_10 + t; i++; } index++; -- Gitee From 696deb439b0cf8b63f80fa0c766a3c2f72f43a9c Mon Sep 17 00:00:00 2001 From: dy Date: Tue, 1 Mar 2022 11:48:35 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E6=89=A7=E8=A1=8Cgn=20format=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy --- interfaces/kits/napi/aafwk/featureAbility/BUILD.gn | 2 +- interfaces/kits/napi/aafwk/particleAbility/BUILD.gn | 2 +- services/abilitymgr/BUILD.gn | 2 +- services/abilitymgr/src/ability_manager_service.cpp | 3 +-- .../test/moduletest/ability_record_test/BUILD.gn | 2 +- tools/aa/src/test_observer_proxy.cpp | 12 ------------ tools/aa/src/test_observer_stub.cpp | 4 ---- zidl/test/native/src/zidl_test_service_proxy.cpp | 8 -------- zidl/test/native/src/zidl_test_service_stub.cpp | 3 --- 9 files changed, 5 insertions(+), 33 deletions(-) diff --git a/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn b/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn index af74e0fdb60..8c007a5842b 100644 --- a/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn +++ b/interfaces/kits/napi/aafwk/featureAbility/BUILD.gn @@ -51,9 +51,9 @@ 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", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ diff --git a/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn b/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn index 750e463fc57..648cf53394a 100644 --- a/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn +++ b/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn @@ -50,9 +50,9 @@ 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", - "//third_party/jsoncpp:jsoncpp", ] external_deps = [ diff --git a/services/abilitymgr/BUILD.gn b/services/abilitymgr/BUILD.gn index c3cc3a7ce70..80462ba1a31 100644 --- a/services/abilitymgr/BUILD.gn +++ b/services/abilitymgr/BUILD.gn @@ -85,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", @@ -101,7 +102,6 @@ ohos_shared_library("abilityms") { "//foundation/multimedia/image_standard/interfaces/innerkits:image_native", "//third_party/libpng:libpng", "//utils/native/base:utils", - "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", ] external_deps = [ diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 6bcd127db08..e04508f6721 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -75,7 +75,6 @@ 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 = 10; 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"; @@ -3416,7 +3415,7 @@ int AbilityManagerService::GetAppMemorySize() } while (valueGet[i] >= '0' && valueGet[i] < '9') { int t = valueGet[i] - '0'; - size[index] = size[index] * SIZE_10 + t; + size[index] = size[index] * 10 + t; i++; } index++; diff --git a/services/test/moduletest/ability_record_test/BUILD.gn b/services/test/moduletest/ability_record_test/BUILD.gn index fbe2657ab94..179aa253872 100644 --- a/services/test/moduletest/ability_record_test/BUILD.gn +++ b/services/test/moduletest/ability_record_test/BUILD.gn @@ -95,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", @@ -112,7 +113,6 @@ ohos_moduletest("AbilityRecordModuleTest") { "//third_party/jsoncpp:jsoncpp", "//third_party/libpng:libpng", "//utils/native/base:utilsbase", - "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", ] external_deps = [ diff --git a/tools/aa/src/test_observer_proxy.cpp b/tools/aa/src/test_observer_proxy.cpp index e064f4a40df..bc02253e6b2 100644 --- a/tools/aa/src/test_observer_proxy.cpp +++ b/tools/aa/src/test_observer_proxy.cpp @@ -36,10 +36,6 @@ void TestObserverProxy::TestStatus(const std::string &msg, const int &resultCode MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); - if (!data.WriteInterfaceToken(GetDescriptor())) { - return; - } - sptr remote = Remote(); if (remote == nullptr) { HILOG_ERROR("Failed to send cmd to service due to remote objetc is null"); @@ -72,10 +68,6 @@ void TestObserverProxy::TestFinished(const std::string &msg, const int &resultCo MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); - if (!data.WriteInterfaceToken(GetDescriptor())) { - return; - } - sptr remote = Remote(); if (remote == nullptr) { HILOG_ERROR("Failed to send cmd to service due to remote objetc is null"); @@ -110,10 +102,6 @@ ShellCommandResult TestObserverProxy::ExecuteShellCommand( MessageParcel reply; MessageOption option(MessageOption::TF_SYNC); - if (!data.WriteInterfaceToken(GetDescriptor())) { - return result; - } - sptr remote = Remote(); if (remote == nullptr) { HILOG_ERROR("Failed to send cmd to service due to remote objetc is null"); diff --git a/tools/aa/src/test_observer_stub.cpp b/tools/aa/src/test_observer_stub.cpp index 20b1908c922..2d338f2d4f1 100644 --- a/tools/aa/src/test_observer_stub.cpp +++ b/tools/aa/src/test_observer_stub.cpp @@ -30,10 +30,6 @@ TestObserverStub::~TestObserverStub() int TestObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { - if (data.ReadInterfaceToken() != GetDescriptor()) { - HILOG_ERROR("local descriptor is not equal to remote"); - return ERR_TRANSACTION_FAILED; - } switch (code) { case static_cast(ITestObserver::Message::AA_TEST_STATUS): { std::string msg = data.ReadString(); diff --git a/zidl/test/native/src/zidl_test_service_proxy.cpp b/zidl/test/native/src/zidl_test_service_proxy.cpp index d9f3e88a760..314cadb6a24 100644 --- a/zidl/test/native/src/zidl_test_service_proxy.cpp +++ b/zidl/test/native/src/zidl_test_service_proxy.cpp @@ -24,10 +24,6 @@ ErrCode ZidlTestServiceProxy::TestIntTransaction( MessageParcel reply; MessageOption option(MessageOption::TF_SYNC); - if (!data.WriteInterfaceToken(GetDescriptor())) { - return ERR_INVALID_VALUE; - } - data.WriteInt32(_data); int32_t st = Remote()->SendRequest(COMMAND_TEST_INT_TRANSACTION, data, reply, option); @@ -51,10 +47,6 @@ ErrCode ZidlTestServiceProxy::TestStringTransaction( MessageParcel reply; MessageOption option(MessageOption::TF_SYNC); - if (!data.WriteInterfaceToken(GetDescriptor())) { - return ERR_INVALID_VALUE; - } - data.WriteString16(Str8ToStr16(_data)); int32_t st = Remote()->SendRequest(COMMAND_TEST_STRING_TRANSACTION, data, reply, option); diff --git a/zidl/test/native/src/zidl_test_service_stub.cpp b/zidl/test/native/src/zidl_test_service_stub.cpp index f68b1537755..adf63cd1dec 100644 --- a/zidl/test/native/src/zidl_test_service_stub.cpp +++ b/zidl/test/native/src/zidl_test_service_stub.cpp @@ -22,9 +22,6 @@ int ZidlTestServiceStub::OnRemoteRequest( /* [out] */ MessageParcel& reply, /* [in] */ MessageOption& option) { - if (data.ReadInterfaceToken() != GetDescriptor()) { - return ERR_TRANSACTION_FAILED; - } switch (code) { case COMMAND_TEST_INT_TRANSACTION: { int _data = data.ReadInt32(); -- Gitee From 44e263ac841d7eb852dfb419bdb0e29a144fa0c1 Mon Sep 17 00:00:00 2001 From: dy Date: Tue, 1 Mar 2022 12:07:25 +0800 Subject: [PATCH 09/11] =?UTF-8?q?C=E8=AF=AD=E8=A8=80=E7=B1=BB=E5=BA=93?= =?UTF-8?q?=E8=BD=ACc++=E7=B1=BB=E5=BA=93=20=E4=BF=AE=E6=94=B9=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy --- services/abilitymgr/src/ability_manager_service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index e04508f6721..30a10337235 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include "ability_info.h" #include "ability_manager_errors.h" -- Gitee From 822928507bab1845c8f066526132b78a4ed7bb5e Mon Sep 17 00:00:00 2001 From: dy Date: Tue, 1 Mar 2022 13:29:19 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy --- services/abilitymgr/src/ability_manager_service.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 30a10337235..d63fb4121ff 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -75,6 +75,7 @@ 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"; @@ -3415,7 +3416,7 @@ int AbilityManagerService::GetAppMemorySize() } while (valueGet[i] >= '0' && valueGet[i] < '9') { int t = valueGet[i] - '0'; - size[index] = size[index] * 10 + t; + size[index] = size[index] * SIZE_10 + t; i++; } index++; -- Gitee From e1d872f18e7e2bcc1ea0212f11fdd9261a267b5f Mon Sep 17 00:00:00 2001 From: dy Date: Tue, 1 Mar 2022 17:57:04 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E8=BF=98=E5=8E=9F=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy --- .../ability/ability_runtime/src/ability_context_impl.cpp | 2 +- tools/aa/src/test_observer_proxy.cpp | 9 +++++++++ tools/aa/src/test_observer_stub.cpp | 4 ++++ zidl/test/native/src/zidl_test_service_proxy.cpp | 8 +++++++- zidl/test/native/src/zidl_test_service_stub.cpp | 3 +++ 5 files changed, 24 insertions(+), 2 deletions(-) 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 1664959cfd8..7fe1a471cf0 100644 --- a/frameworks/kits/ability/ability_runtime/src/ability_context_impl.cpp +++ b/frameworks/kits/ability/ability_runtime/src/ability_context_impl.cpp @@ -71,7 +71,7 @@ bool AbilityContextImpl::IsUpdatingConfigurations() bool AbilityContextImpl::PrintDrawnCompleted() { - return stageContext_ ? stageContext_->IsUpdatingConfigurations() : false; + return stageContext_ ? stageContext_->PrintDrawnCompleted() : false; } void AbilityContextImpl::SwitchArea(int mode) diff --git a/tools/aa/src/test_observer_proxy.cpp b/tools/aa/src/test_observer_proxy.cpp index bc02253e6b2..a7c3ae0bcf7 100644 --- a/tools/aa/src/test_observer_proxy.cpp +++ b/tools/aa/src/test_observer_proxy.cpp @@ -36,6 +36,9 @@ void TestObserverProxy::TestStatus(const std::string &msg, const int &resultCode MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); + if (!data.WriteInterfaceToken(GetDescriptor())) { + return; + } sptr remote = Remote(); if (remote == nullptr) { HILOG_ERROR("Failed to send cmd to service due to remote objetc is null"); @@ -68,6 +71,9 @@ void TestObserverProxy::TestFinished(const std::string &msg, const int &resultCo MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); + if (!data.WriteInterfaceToken(GetDescriptor())) { + return; + } sptr remote = Remote(); if (remote == nullptr) { HILOG_ERROR("Failed to send cmd to service due to remote objetc is null"); @@ -102,6 +108,9 @@ ShellCommandResult TestObserverProxy::ExecuteShellCommand( MessageParcel reply; MessageOption option(MessageOption::TF_SYNC); + if (!data.WriteInterfaceToken(GetDescriptor())) { + return result; + } sptr remote = Remote(); if (remote == nullptr) { HILOG_ERROR("Failed to send cmd to service due to remote objetc is null"); diff --git a/tools/aa/src/test_observer_stub.cpp b/tools/aa/src/test_observer_stub.cpp index 2d338f2d4f1..20b1908c922 100644 --- a/tools/aa/src/test_observer_stub.cpp +++ b/tools/aa/src/test_observer_stub.cpp @@ -30,6 +30,10 @@ TestObserverStub::~TestObserverStub() int TestObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { + if (data.ReadInterfaceToken() != GetDescriptor()) { + HILOG_ERROR("local descriptor is not equal to remote"); + return ERR_TRANSACTION_FAILED; + } switch (code) { case static_cast(ITestObserver::Message::AA_TEST_STATUS): { std::string msg = data.ReadString(); diff --git a/zidl/test/native/src/zidl_test_service_proxy.cpp b/zidl/test/native/src/zidl_test_service_proxy.cpp index 314cadb6a24..dec88363b3f 100644 --- a/zidl/test/native/src/zidl_test_service_proxy.cpp +++ b/zidl/test/native/src/zidl_test_service_proxy.cpp @@ -24,6 +24,9 @@ ErrCode ZidlTestServiceProxy::TestIntTransaction( MessageParcel reply; MessageOption option(MessageOption::TF_SYNC); + if (!data.WriteInterfaceToken(GetDescriptor())) { + return ERR_INVALID_VALUE; + } data.WriteInt32(_data); int32_t st = Remote()->SendRequest(COMMAND_TEST_INT_TRANSACTION, data, reply, option); @@ -46,7 +49,10 @@ ErrCode ZidlTestServiceProxy::TestStringTransaction( MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_SYNC); - + + if (!data.WriteInterfaceToken(GetDescriptor())) { + return ERR_INVALID_VALUE; + } data.WriteString16(Str8ToStr16(_data)); int32_t st = Remote()->SendRequest(COMMAND_TEST_STRING_TRANSACTION, data, reply, option); diff --git a/zidl/test/native/src/zidl_test_service_stub.cpp b/zidl/test/native/src/zidl_test_service_stub.cpp index adf63cd1dec..f68b1537755 100644 --- a/zidl/test/native/src/zidl_test_service_stub.cpp +++ b/zidl/test/native/src/zidl_test_service_stub.cpp @@ -22,6 +22,9 @@ int ZidlTestServiceStub::OnRemoteRequest( /* [out] */ MessageParcel& reply, /* [in] */ MessageOption& option) { + if (data.ReadInterfaceToken() != GetDescriptor()) { + return ERR_TRANSACTION_FAILED; + } switch (code) { case COMMAND_TEST_INT_TRANSACTION: { int _data = data.ReadInt32(); -- Gitee