11ebd3d54Sopenharmony_ci/* 21ebd3d54Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 31ebd3d54Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 41ebd3d54Sopenharmony_ci * you may not use this file except in compliance with the License. 51ebd3d54Sopenharmony_ci * You may obtain a copy of the License at 61ebd3d54Sopenharmony_ci * 71ebd3d54Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 81ebd3d54Sopenharmony_ci * 91ebd3d54Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 101ebd3d54Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 111ebd3d54Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 121ebd3d54Sopenharmony_ci * See the License for the specific language governing permissions and 131ebd3d54Sopenharmony_ci * limitations under the License. 141ebd3d54Sopenharmony_ci */ 151ebd3d54Sopenharmony_ci 161ebd3d54Sopenharmony_ci#include "common.h" 171ebd3d54Sopenharmony_ci 181ebd3d54Sopenharmony_ci#include "cancel_suspend_delay.h" 191ebd3d54Sopenharmony_ci#include "transient_task_log.h" 201ebd3d54Sopenharmony_ci 211ebd3d54Sopenharmony_cinamespace OHOS { 221ebd3d54Sopenharmony_cinamespace BackgroundTaskMgr { 231ebd3d54Sopenharmony_ciconst uint32_t STR_MAX_SIZE = 64; 241ebd3d54Sopenharmony_ciconst uint32_t EXPIRE_CALLBACK_PARAM_NUM = 1; 251ebd3d54Sopenharmony_ciconst uint32_t ASYNC_CALLBACK_PARAM_NUM = 2; 261ebd3d54Sopenharmony_ci 271ebd3d54Sopenharmony_ciAsyncWorkData::AsyncWorkData(napi_env napiEnv) 281ebd3d54Sopenharmony_ci{ 291ebd3d54Sopenharmony_ci env = napiEnv; 301ebd3d54Sopenharmony_ci} 311ebd3d54Sopenharmony_ci 321ebd3d54Sopenharmony_ciAsyncWorkData::~AsyncWorkData() 331ebd3d54Sopenharmony_ci{ 341ebd3d54Sopenharmony_ci if (callback) { 351ebd3d54Sopenharmony_ci BGTASK_LOGD("AsyncWorkData::~AsyncWorkData delete callback"); 361ebd3d54Sopenharmony_ci napi_delete_reference(env, callback); 371ebd3d54Sopenharmony_ci callback = nullptr; 381ebd3d54Sopenharmony_ci } 391ebd3d54Sopenharmony_ci if (asyncWork) { 401ebd3d54Sopenharmony_ci BGTASK_LOGD("AsyncWorkData::~AsyncWorkData delete asyncWork"); 411ebd3d54Sopenharmony_ci napi_delete_async_work(env, asyncWork); 421ebd3d54Sopenharmony_ci asyncWork = nullptr; 431ebd3d54Sopenharmony_ci } 441ebd3d54Sopenharmony_ci} 451ebd3d54Sopenharmony_ci 461ebd3d54Sopenharmony_cinapi_value Common::NapiGetboolean(const napi_env &env, const bool isValue) 471ebd3d54Sopenharmony_ci{ 481ebd3d54Sopenharmony_ci napi_value result = nullptr; 491ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_get_boolean(env, isValue, &result)); 501ebd3d54Sopenharmony_ci return result; 511ebd3d54Sopenharmony_ci} 521ebd3d54Sopenharmony_ci 531ebd3d54Sopenharmony_cinapi_value Common::NapiGetNull(napi_env env) 541ebd3d54Sopenharmony_ci{ 551ebd3d54Sopenharmony_ci napi_value result = nullptr; 561ebd3d54Sopenharmony_ci napi_get_null(env, &result); 571ebd3d54Sopenharmony_ci return result; 581ebd3d54Sopenharmony_ci} 591ebd3d54Sopenharmony_ci 601ebd3d54Sopenharmony_cinapi_value Common::GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg) 611ebd3d54Sopenharmony_ci{ 621ebd3d54Sopenharmony_ci if (errCode == ERR_OK) { 631ebd3d54Sopenharmony_ci return NapiGetNull(env); 641ebd3d54Sopenharmony_ci } 651ebd3d54Sopenharmony_ci napi_value error = nullptr; 661ebd3d54Sopenharmony_ci napi_value eCode = nullptr; 671ebd3d54Sopenharmony_ci napi_value eMsg = nullptr; 681ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_create_int32(env, errCode, &eCode)); 691ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(), 701ebd3d54Sopenharmony_ci errMsg.length(), &eMsg)); 711ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_create_object(env, &error)); 721ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, error, "code", eCode)); 731ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, error, "message", eMsg)); 741ebd3d54Sopenharmony_ci return error; 751ebd3d54Sopenharmony_ci} 761ebd3d54Sopenharmony_ci 771ebd3d54Sopenharmony_cinapi_value Common::GetExpireCallbackValue(napi_env env, int32_t errCode, const napi_value &value) 781ebd3d54Sopenharmony_ci{ 791ebd3d54Sopenharmony_ci napi_value result = nullptr; 801ebd3d54Sopenharmony_ci napi_value eCode = nullptr; 811ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_create_int32(env, errCode, &eCode)); 821ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_create_object(env, &result)); 831ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode)); 841ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, result, "data", value)); 851ebd3d54Sopenharmony_ci return result; 861ebd3d54Sopenharmony_ci} 871ebd3d54Sopenharmony_ci 881ebd3d54Sopenharmony_civoid Common::SetCallback(const napi_env &env, const napi_ref &callbackIn, const napi_value &result) 891ebd3d54Sopenharmony_ci{ 901ebd3d54Sopenharmony_ci napi_value undefined = nullptr; 911ebd3d54Sopenharmony_ci napi_get_undefined(env, &undefined); 921ebd3d54Sopenharmony_ci 931ebd3d54Sopenharmony_ci napi_value callback = nullptr; 941ebd3d54Sopenharmony_ci napi_value resultout = nullptr; 951ebd3d54Sopenharmony_ci napi_value res = nullptr; 961ebd3d54Sopenharmony_ci res = GetExpireCallbackValue(env, 0, result); 971ebd3d54Sopenharmony_ci napi_get_reference_value(env, callbackIn, &callback); 981ebd3d54Sopenharmony_ci NAPI_CALL_RETURN_VOID(env, 991ebd3d54Sopenharmony_ci napi_call_function(env, undefined, callback, EXPIRE_CALLBACK_PARAM_NUM, &res, &resultout)); 1001ebd3d54Sopenharmony_ci} 1011ebd3d54Sopenharmony_ci 1021ebd3d54Sopenharmony_civoid Common::SetCallback( 1031ebd3d54Sopenharmony_ci const napi_env &env, const napi_ref &callbackIn, const int32_t &errCode, const napi_value &result) 1041ebd3d54Sopenharmony_ci{ 1051ebd3d54Sopenharmony_ci napi_value undefined = nullptr; 1061ebd3d54Sopenharmony_ci napi_get_undefined(env, &undefined); 1071ebd3d54Sopenharmony_ci 1081ebd3d54Sopenharmony_ci napi_value callback = nullptr; 1091ebd3d54Sopenharmony_ci napi_value resultout = nullptr; 1101ebd3d54Sopenharmony_ci napi_get_reference_value(env, callbackIn, &callback); 1111ebd3d54Sopenharmony_ci napi_value results[ASYNC_CALLBACK_PARAM_NUM] = {nullptr}; 1121ebd3d54Sopenharmony_ci if (errCode == ERR_OK) { 1131ebd3d54Sopenharmony_ci results[0] = NapiGetNull(env); 1141ebd3d54Sopenharmony_ci } else { 1151ebd3d54Sopenharmony_ci int32_t errCodeInfo = FindErrCode(env, errCode); 1161ebd3d54Sopenharmony_ci std::string errMsg = FindErrMsg(env, errCode); 1171ebd3d54Sopenharmony_ci results[0] = GetCallbackErrorValue(env, errCodeInfo, errMsg); 1181ebd3d54Sopenharmony_ci } 1191ebd3d54Sopenharmony_ci results[1] = result; 1201ebd3d54Sopenharmony_ci NAPI_CALL_RETURN_VOID(env, 1211ebd3d54Sopenharmony_ci napi_call_function(env, undefined, callback, ASYNC_CALLBACK_PARAM_NUM, &results[0], &resultout)); 1221ebd3d54Sopenharmony_ci} 1231ebd3d54Sopenharmony_ci 1241ebd3d54Sopenharmony_cinapi_value Common::SetPromise( 1251ebd3d54Sopenharmony_ci const napi_env &env, const AsyncWorkData &info, const napi_value &result) 1261ebd3d54Sopenharmony_ci{ 1271ebd3d54Sopenharmony_ci if (info.errCode == ERR_OK) { 1281ebd3d54Sopenharmony_ci napi_resolve_deferred(env, info.deferred, result); 1291ebd3d54Sopenharmony_ci } else { 1301ebd3d54Sopenharmony_ci int32_t errCodeInfo = FindErrCode(env, info.errCode); 1311ebd3d54Sopenharmony_ci std::string errMsg = FindErrMsg(env, info.errCode); 1321ebd3d54Sopenharmony_ci napi_value error = nullptr; 1331ebd3d54Sopenharmony_ci napi_value eCode = nullptr; 1341ebd3d54Sopenharmony_ci napi_value eMsg = nullptr; 1351ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_create_int32(env, errCodeInfo, &eCode)); 1361ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(), 1371ebd3d54Sopenharmony_ci errMsg.length(), &eMsg)); 1381ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_create_object(env, &error)); 1391ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, error, "data", eCode)); 1401ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, error, "code", eCode)); 1411ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, error, "message", eMsg)); 1421ebd3d54Sopenharmony_ci napi_reject_deferred(env, info.deferred, error); 1431ebd3d54Sopenharmony_ci } 1441ebd3d54Sopenharmony_ci return result; 1451ebd3d54Sopenharmony_ci} 1461ebd3d54Sopenharmony_ci 1471ebd3d54Sopenharmony_civoid Common::ReturnCallbackPromise(const napi_env &env, const AsyncWorkData &info, const napi_value &result) 1481ebd3d54Sopenharmony_ci{ 1491ebd3d54Sopenharmony_ci if (info.isCallback) { 1501ebd3d54Sopenharmony_ci SetCallback(env, info.callback, info.errCode, result); 1511ebd3d54Sopenharmony_ci } else { 1521ebd3d54Sopenharmony_ci SetPromise(env, info, result); 1531ebd3d54Sopenharmony_ci } 1541ebd3d54Sopenharmony_ci} 1551ebd3d54Sopenharmony_ci 1561ebd3d54Sopenharmony_cinapi_value Common::JSParaError(const napi_env &env, const napi_ref &callback) 1571ebd3d54Sopenharmony_ci{ 1581ebd3d54Sopenharmony_ci if (callback) { 1591ebd3d54Sopenharmony_ci SetCallback(env, callback, ERR_BGTASK_INVALID_PARAM, nullptr); 1601ebd3d54Sopenharmony_ci return Common::NapiGetNull(env); 1611ebd3d54Sopenharmony_ci } else { 1621ebd3d54Sopenharmony_ci napi_value promise = nullptr; 1631ebd3d54Sopenharmony_ci napi_deferred deferred = nullptr; 1641ebd3d54Sopenharmony_ci napi_create_promise(env, &deferred, &promise); 1651ebd3d54Sopenharmony_ci 1661ebd3d54Sopenharmony_ci napi_value res = nullptr; 1671ebd3d54Sopenharmony_ci napi_value eCode = nullptr; 1681ebd3d54Sopenharmony_ci napi_value eMsg = nullptr; 1691ebd3d54Sopenharmony_ci std::string errMsg = FindErrMsg(env, ERR_BGTASK_INVALID_PARAM); 1701ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_create_int32(env, ERR_BGTASK_INVALID_PARAM, &eCode)); 1711ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(), 1721ebd3d54Sopenharmony_ci errMsg.length(), &eMsg)); 1731ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_create_object(env, &res)); 1741ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, res, "data", eCode)); 1751ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, res, "code", eCode)); 1761ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_set_named_property(env, res, "message", eMsg)); 1771ebd3d54Sopenharmony_ci napi_reject_deferred(env, deferred, res); 1781ebd3d54Sopenharmony_ci return promise; 1791ebd3d54Sopenharmony_ci } 1801ebd3d54Sopenharmony_ci} 1811ebd3d54Sopenharmony_ci 1821ebd3d54Sopenharmony_cinapi_value Common::GetU16StringValue(const napi_env &env, const napi_value &value, std::u16string &result) 1831ebd3d54Sopenharmony_ci{ 1841ebd3d54Sopenharmony_ci napi_valuetype valuetype = napi_undefined; 1851ebd3d54Sopenharmony_ci 1861ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_typeof(env, value, &valuetype)); 1871ebd3d54Sopenharmony_ci if (valuetype == napi_string) { 1881ebd3d54Sopenharmony_ci char str[STR_MAX_SIZE] = {0}; 1891ebd3d54Sopenharmony_ci size_t strLen = 0; 1901ebd3d54Sopenharmony_ci NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen)); 1911ebd3d54Sopenharmony_ci 1921ebd3d54Sopenharmony_ci result = Str8ToStr16((std::string)str); 1931ebd3d54Sopenharmony_ci BGTASK_LOGD("GetU16StringValue result: %{public}s", Str16ToStr8(result).c_str()); 1941ebd3d54Sopenharmony_ci } else { 1951ebd3d54Sopenharmony_ci return nullptr; 1961ebd3d54Sopenharmony_ci } 1971ebd3d54Sopenharmony_ci 1981ebd3d54Sopenharmony_ci return Common::NapiGetNull(env); 1991ebd3d54Sopenharmony_ci} 2001ebd3d54Sopenharmony_ci 2011ebd3d54Sopenharmony_cinapi_value Common::GetInt32NumberValue(const napi_env &env, const napi_value &value, int32_t &result) 2021ebd3d54Sopenharmony_ci{ 2031ebd3d54Sopenharmony_ci napi_valuetype valuetype = napi_undefined; 2041ebd3d54Sopenharmony_ci BGTASK_NAPI_CALL(env, napi_typeof(env, value, &valuetype)); 2051ebd3d54Sopenharmony_ci if (valuetype != napi_number) { 2061ebd3d54Sopenharmony_ci return nullptr; 2071ebd3d54Sopenharmony_ci } 2081ebd3d54Sopenharmony_ci BGTASK_NAPI_CALL(env, napi_get_value_int32(env, value, &result)); 2091ebd3d54Sopenharmony_ci BGTASK_LOGD("GetInt32NumberValue result: %{public}d", result); 2101ebd3d54Sopenharmony_ci return Common::NapiGetNull(env); 2111ebd3d54Sopenharmony_ci} 2121ebd3d54Sopenharmony_ci 2131ebd3d54Sopenharmony_civoid Common::PaddingAsyncWorkData( 2141ebd3d54Sopenharmony_ci const napi_env &env, const napi_ref &callback, AsyncWorkData &info, napi_value &promise) 2151ebd3d54Sopenharmony_ci{ 2161ebd3d54Sopenharmony_ci if (callback) { 2171ebd3d54Sopenharmony_ci info.callback = callback; 2181ebd3d54Sopenharmony_ci info.isCallback = true; 2191ebd3d54Sopenharmony_ci } else { 2201ebd3d54Sopenharmony_ci napi_deferred deferred = nullptr; 2211ebd3d54Sopenharmony_ci NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); 2221ebd3d54Sopenharmony_ci info.deferred = deferred; 2231ebd3d54Sopenharmony_ci info.isCallback = false; 2241ebd3d54Sopenharmony_ci } 2251ebd3d54Sopenharmony_ci} 2261ebd3d54Sopenharmony_ci 2271ebd3d54Sopenharmony_cinapi_value Common::SetDelaySuspendInfo( 2281ebd3d54Sopenharmony_ci const napi_env &env, std::shared_ptr<DelaySuspendInfo>& delaySuspendInfo, napi_value &result) 2291ebd3d54Sopenharmony_ci{ 2301ebd3d54Sopenharmony_ci if (delaySuspendInfo == nullptr) { 2311ebd3d54Sopenharmony_ci BGTASK_LOGI("delaySuspendInfo is nullptr"); 2321ebd3d54Sopenharmony_ci return NapiGetboolean(env, false); 2331ebd3d54Sopenharmony_ci } 2341ebd3d54Sopenharmony_ci napi_value value = nullptr; 2351ebd3d54Sopenharmony_ci 2361ebd3d54Sopenharmony_ci // readonly requestId?: number 2371ebd3d54Sopenharmony_ci napi_create_int32(env, delaySuspendInfo->GetRequestId(), &value); 2381ebd3d54Sopenharmony_ci napi_set_named_property(env, result, "requestId", value); 2391ebd3d54Sopenharmony_ci 2401ebd3d54Sopenharmony_ci // readonly actualDelayTime?: number 2411ebd3d54Sopenharmony_ci napi_create_int32(env, delaySuspendInfo->GetActualDelayTime(), &value); 2421ebd3d54Sopenharmony_ci napi_set_named_property(env, result, "actualDelayTime", value); 2431ebd3d54Sopenharmony_ci 2441ebd3d54Sopenharmony_ci return NapiGetboolean(env, true); 2451ebd3d54Sopenharmony_ci} 2461ebd3d54Sopenharmony_ci 2471ebd3d54Sopenharmony_cinapi_value Common::GetStringValue(const napi_env &env, const napi_value &value, std::string &result) 2481ebd3d54Sopenharmony_ci{ 2491ebd3d54Sopenharmony_ci napi_valuetype valuetype = napi_undefined; 2501ebd3d54Sopenharmony_ci BGTASK_NAPI_CALL(env, napi_typeof(env, value, &valuetype)); 2511ebd3d54Sopenharmony_ci if (valuetype != napi_string) { 2521ebd3d54Sopenharmony_ci return nullptr; 2531ebd3d54Sopenharmony_ci } 2541ebd3d54Sopenharmony_ci 2551ebd3d54Sopenharmony_ci char str[STR_MAX_SIZE] = {0}; 2561ebd3d54Sopenharmony_ci size_t strLen = 0; 2571ebd3d54Sopenharmony_ci napi_status status = napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen); 2581ebd3d54Sopenharmony_ci if (status != napi_ok) { 2591ebd3d54Sopenharmony_ci return nullptr; 2601ebd3d54Sopenharmony_ci } 2611ebd3d54Sopenharmony_ci result = std::string(str); 2621ebd3d54Sopenharmony_ci BGTASK_LOGD("GetStringValue result: %{public}s", result.c_str()); 2631ebd3d54Sopenharmony_ci return Common::NapiGetNull(env); 2641ebd3d54Sopenharmony_ci} 2651ebd3d54Sopenharmony_ci 2661ebd3d54Sopenharmony_civoid Common::HandleErrCode(const napi_env &env, int32_t errCode, bool isThrow) 2671ebd3d54Sopenharmony_ci{ 2681ebd3d54Sopenharmony_ci BGTASK_LOGD("HandleErrCode errCode = %{public}d, isThrow = %{public}d", errCode, isThrow); 2691ebd3d54Sopenharmony_ci if (!isThrow || errCode == ERR_OK) { 2701ebd3d54Sopenharmony_ci return; 2711ebd3d54Sopenharmony_ci } 2721ebd3d54Sopenharmony_ci std::string errMsg = FindErrMsg(env, errCode); 2731ebd3d54Sopenharmony_ci int32_t errCodeInfo = FindErrCode(env, errCode); 2741ebd3d54Sopenharmony_ci if (errMsg != "") { 2751ebd3d54Sopenharmony_ci napi_throw_error(env, std::to_string(errCodeInfo).c_str(), errMsg.c_str()); 2761ebd3d54Sopenharmony_ci } 2771ebd3d54Sopenharmony_ci} 2781ebd3d54Sopenharmony_ci 2791ebd3d54Sopenharmony_cibool Common::HandleParamErr(const napi_env &env, int32_t errCode, bool isThrow) 2801ebd3d54Sopenharmony_ci{ 2811ebd3d54Sopenharmony_ci BGTASK_LOGD("HandleParamErr errCode = %{public}d, isThrow = %{public}d", errCode, isThrow); 2821ebd3d54Sopenharmony_ci if (!isThrow || errCode == ERR_OK) { 2831ebd3d54Sopenharmony_ci return false; 2841ebd3d54Sopenharmony_ci } 2851ebd3d54Sopenharmony_ci auto iter = paramErrCodeMsgMap.find(errCode); 2861ebd3d54Sopenharmony_ci if (iter != paramErrCodeMsgMap.end()) { 2871ebd3d54Sopenharmony_ci std::string errMessage = "BussinessError 401: Parameter error. "; 2881ebd3d54Sopenharmony_ci errMessage.append(iter->second); 2891ebd3d54Sopenharmony_ci napi_throw_error(env, std::to_string(ERR_BGTASK_INVALID_PARAM).c_str(), errMessage.c_str()); 2901ebd3d54Sopenharmony_ci return true; 2911ebd3d54Sopenharmony_ci } 2921ebd3d54Sopenharmony_ci return false; 2931ebd3d54Sopenharmony_ci} 2941ebd3d54Sopenharmony_ci 2951ebd3d54Sopenharmony_cistd::string Common::FindErrMsg(const napi_env &env, const int32_t errCode) 2961ebd3d54Sopenharmony_ci{ 2971ebd3d54Sopenharmony_ci if (errCode == ERR_OK) { 2981ebd3d54Sopenharmony_ci return ""; 2991ebd3d54Sopenharmony_ci } 3001ebd3d54Sopenharmony_ci auto iter = saErrCodeMsgMap.find(errCode); 3011ebd3d54Sopenharmony_ci if (iter != saErrCodeMsgMap.end()) { 3021ebd3d54Sopenharmony_ci std::string errMessage = "BussinessError "; 3031ebd3d54Sopenharmony_ci int32_t errCodeInfo = FindErrCode(env, errCode); 3041ebd3d54Sopenharmony_ci errMessage.append(std::to_string(errCodeInfo)).append(": ").append(iter->second); 3051ebd3d54Sopenharmony_ci return errMessage; 3061ebd3d54Sopenharmony_ci } 3071ebd3d54Sopenharmony_ci iter = paramErrCodeMsgMap.find(errCode); 3081ebd3d54Sopenharmony_ci if (iter != paramErrCodeMsgMap.end()) { 3091ebd3d54Sopenharmony_ci std::string errMessage = "BussinessError 401: Parameter error. "; 3101ebd3d54Sopenharmony_ci errMessage.append(iter->second); 3111ebd3d54Sopenharmony_ci return errMessage; 3121ebd3d54Sopenharmony_ci } 3131ebd3d54Sopenharmony_ci return "Inner error."; 3141ebd3d54Sopenharmony_ci} 3151ebd3d54Sopenharmony_ci 3161ebd3d54Sopenharmony_ciint32_t Common::FindErrCode(const napi_env &env, const int32_t errCodeIn) 3171ebd3d54Sopenharmony_ci{ 3181ebd3d54Sopenharmony_ci auto iter = paramErrCodeMsgMap.find(errCodeIn); 3191ebd3d54Sopenharmony_ci if (iter != paramErrCodeMsgMap.end()) { 3201ebd3d54Sopenharmony_ci return ERR_BGTASK_INVALID_PARAM; 3211ebd3d54Sopenharmony_ci } 3221ebd3d54Sopenharmony_ci return errCodeIn > THRESHOLD ? errCodeIn / OFFSET : errCodeIn; 3231ebd3d54Sopenharmony_ci} 3241ebd3d54Sopenharmony_ci 3251ebd3d54Sopenharmony_cinapi_value Common::GetBooleanValue(const napi_env &env, const napi_value &value, bool &result) 3261ebd3d54Sopenharmony_ci{ 3271ebd3d54Sopenharmony_ci napi_valuetype valuetype = napi_undefined; 3281ebd3d54Sopenharmony_ci BGTASK_NAPI_CALL(env, napi_typeof(env, value, &valuetype)); 3291ebd3d54Sopenharmony_ci if (valuetype != napi_boolean) { 3301ebd3d54Sopenharmony_ci return nullptr; 3311ebd3d54Sopenharmony_ci } 3321ebd3d54Sopenharmony_ci BGTASK_NAPI_CALL(env, napi_get_value_bool(env, value, &result)); 3331ebd3d54Sopenharmony_ci BGTASK_LOGD("GetBooleanValue result: %{public}d", result); 3341ebd3d54Sopenharmony_ci 3351ebd3d54Sopenharmony_ci return Common::NapiGetNull(env); 3361ebd3d54Sopenharmony_ci} 3371ebd3d54Sopenharmony_ci} // namespace BackgroundTaskMgr 3381ebd3d54Sopenharmony_ci} // namespace OHOS 339