diff --git a/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h b/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h index c7df89a1c28a497334cb3ea32515de374120d2b0..113ea5c5c4b2787af9d3a6c37e9dd9a30781cd55 100644 --- a/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h +++ b/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h @@ -28,8 +28,8 @@ namespace OHOS { namespace FileAccessFwk { using namespace AbilityRuntime; -using InputArgsParser = std::function; -using ResultValueParser = std::function; +using InputArgsParser = std::function; +using ResultValueParser = std::function; struct CallJsParam { std::mutex fileOperateMutex; @@ -81,12 +81,12 @@ public: int StopWatcher(const Uri &uri, bool isUnregisterAll) override; private: - NativeValue* CallObjectMethod(const char *name, NativeValue * const *argv = nullptr, size_t argc = 0); + void CallObjectMethod(const char *name, napi_value const *argv = nullptr, size_t argc = 0); int CallJsMethod(const std::string &funcName, JsRuntime &jsRuntime, NativeReference *jsObj, InputArgsParser argParser, ResultValueParser retParser); void GetSrcPath(std::string &srcPath); static int Notify(Uri &uri, NotifyType notifyType); - static NativeValue* FuncCallback(NativeEngine *engine, NativeCallbackInfo *info); + static napi_value FuncCallback(napi_env env, napi_callback_info info); JsRuntime &jsRuntime_; std::shared_ptr jsObj_; }; diff --git a/interfaces/inner_api/file_access/include/napi_common_fileaccess.h b/interfaces/inner_api/file_access/include/napi_common_fileaccess.h index c970f4f646658179082161b618738b0b322694ca..72f977eb3e38a65286fa7bb0ed908e7ecc39113c 100644 --- a/interfaces/inner_api/file_access/include/napi_common_fileaccess.h +++ b/interfaces/inner_api/file_access/include/napi_common_fileaccess.h @@ -39,6 +39,8 @@ bool UnwrapBigIntUint64ByPropertyName(napi_env env, napi_value param, const char napi_value WrapArrayWantToJS(napi_env env, const std::vector &wantVec); bool UnwrapArrayWantFromJS(napi_env env, napi_value param, std::vector &wantVec); +napi_value GetUndefinedValue(napi_env env); +napi_status GetValueString(napi_env env, napi_value value, std::string &result); } // namespace FileAccessFwk } // namespace OHOS -#endif // NAPI_COMMON_FILE_ACCESS_H +#endif // NAPI_COMMON_FILE_ACCESS_H \ No newline at end of file diff --git a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp index a5ad41eca075004b16391f3beaaa08ff4a4428c9..dc55f40a87581e39b6bcaee5281c5552174a0e6b 100644 --- a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp +++ b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp @@ -116,8 +116,7 @@ void JsFileAccessExtAbility::OnStart(const AAFwk::Want &want) HandleScope handleScope(jsRuntime_); napi_env env = reinterpret_cast(&jsRuntime_.GetNativeEngine()); napi_value napiWant = OHOS::AppExecFwk::WrapWant(env, want); - NativeValue* nativeWant = reinterpret_cast(napiWant); - NativeValue* argv[] = {nativeWant}; + napi_value argv[] = {napiWant}; CallObjectMethod("onCreate", argv, ARGC_ONE); } @@ -137,37 +136,34 @@ sptr JsFileAccessExtAbility::OnConnect(const AAFwk::Want &want) return remoteObject->AsObject(); } -NativeValue* JsFileAccessExtAbility::CallObjectMethod(const char* name, NativeValue* const* argv, size_t argc) +void JsFileAccessExtAbility::CallObjectMethod(const char* name, napi_value const* argv, size_t argc) { UserAccessTracer trace; trace.Start("CallObjectMethod"); if (!jsObj_) { HILOG_ERROR("JsFileAccessExtAbility::CallObjectMethod jsObj Not found FileAccessExtAbility.js"); - return nullptr; + return; } HandleEscape handleEscape(jsRuntime_); auto& nativeEngine = jsRuntime_.GetNativeEngine(); + auto env = reinterpret_cast(&nativeEngine); - NativeValue* value = jsObj_->Get(); + napi_value value = jsObj_->GetNapiValue(); if (value == nullptr) { HILOG_ERROR("Failed to get FileAccessExtAbility value"); - return nullptr; - } - - NativeObject* obj = ConvertNativeValueTo(value); - if (obj == nullptr) { - HILOG_ERROR("Failed to get FileAccessExtAbility object"); - return nullptr; + return; } - NativeValue* method = obj->GetProperty(name); + napi_value method = nullptr; + napi_get_named_property(env, value, name, &method); if (method == nullptr) { HILOG_ERROR("Failed to get '%{public}s' from FileAccessExtAbility object", name); - return nullptr; + return; } - return handleEscape.Escape(nativeEngine.CallFunction(value, method, argv, argc)); + napi_value result = nullptr; + napi_call_function(env, value, method, argc, argv, &result); } static int DoCallJsMethod(CallJsParam *param) @@ -181,25 +177,25 @@ static int DoCallJsMethod(CallJsParam *param) } HandleEscape handleEscape(*jsRuntime); auto& nativeEngine = jsRuntime->GetNativeEngine(); + auto env = reinterpret_cast(&nativeEngine); size_t argc = 0; - NativeValue *argv[MAX_ARG_COUNT] = { nullptr }; + napi_value argv[MAX_ARG_COUNT] = { nullptr }; if (param->argParser != nullptr) { - if (!param->argParser(nativeEngine, argv, argc)) { + if (!param->argParser(env, argv, argc)) { HILOG_ERROR("failed to get params."); return EINVAL; } } - NativeValue *value = param->jsObj->Get(); + + napi_value value = nullptr; + auto ref = reinterpret_cast(param->jsObj); + napi_get_reference_value(env, ref, &value); if (value == nullptr) { HILOG_ERROR("failed to get native value object."); return EINVAL; } - NativeObject *obj = ConvertNativeValueTo(value); - if (obj == nullptr) { - HILOG_ERROR("failed to get FileExtAbility object."); - return EINVAL; - } - NativeValue *method = obj->GetProperty(param->funcName.c_str()); + napi_value method = nullptr; + napi_get_named_property(env, value, param->funcName.c_str(), &method); if (method == nullptr) { HILOG_ERROR("failed to get %{public}s from FileExtAbility object.", param->funcName.c_str()); return EINVAL; @@ -208,7 +204,7 @@ static int DoCallJsMethod(CallJsParam *param) HILOG_ERROR("ResultValueParser must not null."); return EINVAL; } - if (!param->retParser(nativeEngine, handleEscape.Escape(nativeEngine.CallFunction(value, method, argv, argc)))) { + if (!param->retParser(env, value, method, argc, argv)) { HILOG_INFO("Parser js result fail."); return E_GETRESULT; } @@ -305,9 +301,11 @@ int JsFileAccessExtAbility::OpenFile(const Uri &uri, int flags, int &fd) return E_GETRESULT; } - auto argParser = [uri, flags](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { - NativeValue *nativeUri = engine.CreateString(uri.ToString().c_str(), uri.ToString().length()); - NativeValue *nativeFlags = engine.CreateNumber((int32_t)flags); + auto argParser = [uri, flags](napi_env &env, napi_value argv[], size_t &argc) -> bool { + napi_value nativeUri = nullptr; + napi_create_string_utf8(env, uri.ToString().c_str(), uri.ToString().length(), &nativeUri); + napi_value nativeFlags = nullptr; + napi_create_int32(env, flags, &nativeFlags); if (nativeUri == nullptr || nativeFlags == nullptr) { HILOG_ERROR("create uri or flags native js value fail."); return false; @@ -317,19 +315,30 @@ int JsFileAccessExtAbility::OpenFile(const Uri &uri, int flags, int &fd) argc = ARGC_TWO; return true; }; - auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { - NativeObject *obj = ConvertNativeValueTo(result); - if (obj == nullptr) { + auto retParser = [value](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { HILOG_ERROR("Convert js object fail."); return false; } - bool ret = ConvertFromJsValue(engine, obj->GetProperty("fd"), value->data); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code); - if (!ret) { + napi_value fd = nullptr; + napi_get_named_property(env, result, "fd", &fd); + napi_status status = napi_get_value_int32(env, fd, &value->data); + if (status != napi_ok) { HILOG_ERROR("Convert js value fail."); + return false; } - return ret; + + napi_value code = nullptr; + napi_get_named_property(env, result, "code", &code); + status = napi_get_value_int32(env, code, &value->code); + if (status != napi_ok) { + HILOG_ERROR("Convert js value fail."); + return false; + } + return true; }; auto errCode = CallJsMethod("openFile", jsRuntime_, jsObj_.get(), argParser, retParser); @@ -361,9 +370,11 @@ int JsFileAccessExtAbility::CreateFile(const Uri &parent, const std::string &dis return E_GETRESULT; } - auto argParser = [parent, displayName](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { - NativeValue *nativeParent = engine.CreateString(parent.ToString().c_str(), parent.ToString().length()); - NativeValue *nativeDisplayName = engine.CreateString(displayName.c_str(), displayName.length()); + auto argParser = [parent, displayName](napi_env &env, napi_value argv[], size_t &argc) -> bool { + napi_value nativeParent = nullptr; + napi_create_string_utf8(env, parent.ToString().c_str(), parent.ToString().length(), &nativeParent); + napi_value nativeDisplayName = nullptr; + napi_create_string_utf8(env, displayName.c_str(), displayName.length(), &nativeDisplayName); if (nativeParent == nullptr || nativeDisplayName == nullptr) { HILOG_ERROR("create parent uri or displayName native js value fail."); return false; @@ -373,19 +384,31 @@ int JsFileAccessExtAbility::CreateFile(const Uri &parent, const std::string &dis argc = ARGC_TWO; return true; }; - auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { - NativeObject *obj = ConvertNativeValueTo(result); - if (obj == nullptr) { + auto retParser = [value](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { HILOG_ERROR("Convert js object fail."); return false; } - bool ret = ConvertFromJsValue(engine, obj->GetProperty("uri"), value->data); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code); - if (!ret) { + napi_value uri = nullptr; + napi_get_named_property(env, result, "uri", &uri); + + napi_status status = GetValueString(env, uri, value->data); + if (status != napi_ok) { HILOG_ERROR("Convert js value fail."); + return false; } - return ret; + + napi_value code = nullptr; + napi_get_named_property(env, result, "code", &code); + status = napi_get_value_int32(env, code, &value->code); + if (status != napi_ok) { + HILOG_ERROR("Convert js value fail."); + return false; + } + return true; }; auto errCode = CallJsMethod("createFile", jsRuntime_, jsObj_.get(), argParser, retParser); @@ -418,9 +441,11 @@ int JsFileAccessExtAbility::Mkdir(const Uri &parent, const std::string &displayN return E_GETRESULT; } - auto argParser = [parent, displayName](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { - NativeValue *nativeParent = engine.CreateString(parent.ToString().c_str(), parent.ToString().length()); - NativeValue *nativeDisplayName = engine.CreateString(displayName.c_str(), displayName.length()); + auto argParser = [parent, displayName](napi_env &env, napi_value *argv, size_t &argc) -> bool { + napi_value nativeParent = nullptr; + napi_create_string_utf8(env, parent.ToString().c_str(), parent.ToString().length(), &nativeParent); + napi_value nativeDisplayName = nullptr; + napi_create_string_utf8(env, displayName.c_str(), displayName.length(), &nativeDisplayName); if (nativeParent == nullptr || nativeDisplayName == nullptr) { HILOG_ERROR("create parent uri native js value fail."); return false; @@ -430,20 +455,31 @@ int JsFileAccessExtAbility::Mkdir(const Uri &parent, const std::string &displayN argc = ARGC_TWO; return true; }; - auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { - NativeObject *obj = ConvertNativeValueTo(result); - if (obj == nullptr) { + + auto retParser = [value](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { HILOG_ERROR("Convert js object fail."); return false; } - bool ret = ConvertFromJsValue(engine, obj->GetProperty("uri"), value->data); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code); - if (!ret) { + napi_value uri = nullptr; + napi_get_named_property(env, result, "uri", &uri); + napi_status status = GetValueString(env, uri, value->data); + if (status != napi_ok) { HILOG_ERROR("Convert js value fail."); + return false; } - return ret; + napi_value code = nullptr; + napi_get_named_property(env, result, "code", &code); + status = napi_get_value_int32(env, code, &value->code); + if (status != napi_ok) { + HILOG_ERROR("Convert js value fail."); + return false; + } + return true; }; auto errCode = CallJsMethod("mkdir", jsRuntime_, jsObj_.get(), argParser, retParser); @@ -475,8 +511,9 @@ int JsFileAccessExtAbility::Delete(const Uri &sourceFile) return E_GETRESULT; } - auto argParser = [uri = sourceFile](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { - NativeValue *nativeUri = engine.CreateString(uri.ToString().c_str(), uri.ToString().length()); + auto argParser = [uri = sourceFile](napi_env &env, napi_value *argv, size_t &argc) -> bool { + napi_value nativeUri = nullptr; + napi_create_string_utf8(env, uri.ToString().c_str(), uri.ToString().length(), &nativeUri); if (nativeUri == nullptr) { HILOG_ERROR("create sourceFile uri native js value fail."); return false; @@ -485,12 +522,21 @@ int JsFileAccessExtAbility::Delete(const Uri &sourceFile) argc = ARGC_ONE; return true; }; - auto retParser = [ret](NativeEngine &engine, NativeValue *result) -> bool { - bool res = ConvertFromJsValue(engine, result, *ret); - if (!res) { + + auto retParser = [ret](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { + HILOG_ERROR("Convert js object fail."); + return false; + } + + napi_status status = napi_get_value_int32(env, result, ret.get()); + if (status != napi_ok) { HILOG_ERROR("Convert js value fail."); + return false; } - return res; + return true; }; auto errCode = CallJsMethod("delete", jsRuntime_, jsObj_.get(), argParser, retParser); @@ -517,10 +563,11 @@ int JsFileAccessExtAbility::Move(const Uri &sourceFile, const Uri &targetParent, return E_GETRESULT; } - auto argParser = [sourceFile, targetParent](NativeEngine &engine, NativeValue* argv[], size_t &argc) -> bool { - NativeValue *srcUri = engine.CreateString(sourceFile.ToString().c_str(), - sourceFile.ToString().length()); - NativeValue *dstUri = engine.CreateString(targetParent.ToString().c_str(), targetParent.ToString().length()); + auto argParser = [sourceFile, targetParent](napi_env &env, napi_value *argv, size_t &argc) -> bool { + napi_value srcUri = nullptr; + napi_create_string_utf8(env, sourceFile.ToString().c_str(), sourceFile.ToString().length(), &srcUri); + napi_value dstUri = nullptr; + napi_create_string_utf8(env, targetParent.ToString().c_str(), targetParent.ToString().length(), &dstUri); if (srcUri == nullptr || dstUri == nullptr) { HILOG_ERROR("create sourceFile uri native js value fail."); return false; @@ -530,19 +577,31 @@ int JsFileAccessExtAbility::Move(const Uri &sourceFile, const Uri &targetParent, argc = ARGC_TWO; return true; }; - auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { - NativeObject *obj = ConvertNativeValueTo(result); - if (obj == nullptr) { + + auto retParser = [value](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { HILOG_ERROR("Convert js object fail."); return false; } - bool ret = ConvertFromJsValue(engine, obj->GetProperty("uri"), value->data); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code); - if (!ret) { + napi_value uri = nullptr; + napi_get_named_property(env, result, "uri", &uri); + napi_status status = GetValueString(env, uri, value->data); + if (status != napi_ok) { HILOG_ERROR("Convert js value fail."); + return false; } - return ret; + + napi_value code = nullptr; + napi_get_named_property(env, result, "code", &code); + status = napi_get_value_int32(env, code, &value->code); + if (status != napi_ok) { + HILOG_ERROR("Convert js value fail."); + return false; + } + return true; }; auto errCode = CallJsMethod("move", jsRuntime_, jsObj_.get(), argParser, retParser); @@ -574,23 +633,29 @@ static void TranslateCopyResult(CopyResult ©Result) } } -static bool GetResultByJs(NativeEngine &engine, NativeValue *nativeCopyResult, CopyResult &result, int ©Ret) +static bool GetResultByJs(napi_env &env, napi_value nativeCopyResult, CopyResult &result, const int ©Ret) { UserAccessTracer trace; trace.Start("GetResultsByJs"); - NativeObject *obj = ConvertNativeValueTo(nativeCopyResult); - if (obj == nullptr) { + if (nativeCopyResult == nullptr) { HILOG_ERROR("Convert js object fail."); return false; } bool ret = true; if (copyRet == COPY_NOEXCEPTION) { - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("sourceUri"), result.sourceUri); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("destUri"), result.destUri); + napi_value sourceUri = nullptr; + napi_get_named_property(env, nativeCopyResult, "sourceUri", &sourceUri); + GetValueString(env, sourceUri, result.sourceUri); + + napi_value destUri = nullptr; + napi_get_named_property(env, nativeCopyResult, "destUri", &destUri); + GetValueString(env, destUri, result.destUri); } if ((copyRet == COPY_NOEXCEPTION) || (copyRet == COPY_EXCEPTION)) { - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("errCode"), result.errCode); + napi_value errCode = nullptr; + napi_get_named_property(env, nativeCopyResult, "errCode", &errCode); + napi_get_value_int32(env, errCode, &result.errCode); } if (!ret) { HILOG_ERROR("Convert js value fail."); @@ -598,41 +663,48 @@ static bool GetResultByJs(NativeEngine &engine, NativeValue *nativeCopyResult, C return ret; } -static bool ParserGetJsCopyResult(NativeEngine &engine, NativeValue *nativeValue, +static bool ParserGetJsCopyResult(napi_env &env, napi_value nativeValue, std::vector ©Result, int ©Ret) { UserAccessTracer trace; trace.Start("ParserGetJsCopyResult"); - NativeObject *obj = ConvertNativeValueTo(nativeValue); - if (obj == nullptr) { + if (nativeValue == nullptr) { HILOG_ERROR("Convert js object fail."); return false; } - bool ret = ConvertFromJsValue(engine, obj->GetProperty("code"), copyRet); - if (!ret) { + napi_value code = nullptr; + napi_get_named_property(env, nativeValue, "code", &code); + napi_status status = napi_get_value_int32(env, code, ©Ret); + if (status != napi_ok) { HILOG_ERROR("Convert js value fail."); return false; } + if (copyRet == ERR_OK) { return true; } - NativeArray *nativeArray = ConvertNativeValueTo(obj->GetProperty("results")); + napi_value nativeArray = nullptr; + napi_create_array(env, &nativeArray); + napi_get_named_property(env, nativeValue, "results", &nativeArray); if (nativeArray == nullptr) { HILOG_ERROR("nativeArray is nullptr"); return false; } - for (uint32_t i = 0; i < nativeArray->GetLength(); i++) { - NativeValue *nativeCopyResult = nativeArray->GetElement(i); + uint32_t length = 0; + napi_get_array_length(env, nativeArray, &length); + for (uint32_t i = 0; i < length; i++) { + napi_value nativeCopyResult = nullptr; + napi_get_element(env, nativeArray, i, &nativeCopyResult); if (nativeCopyResult == nullptr) { HILOG_ERROR("get native FileInfo fail."); return false; } CopyResult result; - ret = GetResultByJs(engine, nativeCopyResult, result, copyRet); + bool ret = GetResultByJs(env, nativeCopyResult, result, copyRet); if (ret) { TranslateCopyResult(result); copyResult.push_back(result); @@ -647,11 +719,15 @@ int JsFileAccessExtAbility::Copy(const Uri &sourceUri, const Uri &destUri, std:: { UserAccessTracer trace; trace.Start("Copy"); - auto argParser = [sourceUri, destUri, force](NativeEngine &engine, NativeValue* argv[], size_t &argc) -> bool { - NativeValue *srcNativeUri = engine.CreateString(sourceUri.ToString().c_str(), - sourceUri.ToString().length()); - NativeValue *dstNativeUri = engine.CreateString(destUri.ToString().c_str(), destUri.ToString().length()); - NativeValue *forceCopy = engine.CreateBoolean(force); + auto argParser = [sourceUri, destUri, force](napi_env &env, napi_value *argv, size_t &argc) -> bool { + napi_value srcNativeUri = nullptr; + napi_create_string_utf8(env, sourceUri.ToString().c_str(), sourceUri.ToString().length(), &srcNativeUri); + + napi_value dstNativeUri = nullptr; + napi_create_string_utf8(env, destUri.ToString().c_str(), destUri.ToString().length(), &dstNativeUri); + + napi_value forceCopy = nullptr; + napi_get_boolean(env, force, &forceCopy); if (srcNativeUri == nullptr || dstNativeUri == nullptr || forceCopy == nullptr) { HILOG_ERROR("create arguments native js value fail."); return false; @@ -664,8 +740,15 @@ int JsFileAccessExtAbility::Copy(const Uri &sourceUri, const Uri &destUri, std:: }; int copyRet = COPY_EXCEPTION; - auto retParser = [©Result, ©Ret](NativeEngine &engine, NativeValue *result) -> bool { - return ParserGetJsCopyResult(engine, result, copyResult, copyRet); + auto retParser = [©Result, ©Ret](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { + HILOG_ERROR("Convert js object fail."); + return false; + } + + return ParserGetJsCopyResult(env, result, copyResult, copyRet); }; auto errCode = CallJsMethod("copy", jsRuntime_, jsObj_.get(), argParser, retParser); @@ -689,10 +772,12 @@ int JsFileAccessExtAbility::Rename(const Uri &sourceFile, const std::string &dis HILOG_ERROR("Rename value is nullptr."); return E_GETRESULT; } - auto argParser = [sourceFile, displayName](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { - NativeValue *nativeSourceFile = engine.CreateString(sourceFile.ToString().c_str(), - sourceFile.ToString().length()); - NativeValue *nativeDisplayName = engine.CreateString(displayName.c_str(), displayName.length()); + auto argParser = [sourceFile, displayName](napi_env &env, napi_value *argv, size_t &argc) -> bool { + napi_value nativeSourceFile = nullptr; + napi_create_string_utf8(env, sourceFile.ToString().c_str(), sourceFile.ToString().length(), &nativeSourceFile); + + napi_value nativeDisplayName = nullptr; + napi_create_string_utf8(env, displayName.c_str(), displayName.length(), &nativeDisplayName); if (nativeSourceFile == nullptr || nativeDisplayName == nullptr) { HILOG_ERROR("create sourceFile uri or displayName native js value fail."); return false; @@ -702,19 +787,31 @@ int JsFileAccessExtAbility::Rename(const Uri &sourceFile, const std::string &dis argc = ARGC_TWO; return true; }; - auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { - NativeObject *obj = ConvertNativeValueTo(result); - if (obj == nullptr) { + + auto retParser = [value](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { HILOG_ERROR("Convert js object fail."); return false; } - bool ret = ConvertFromJsValue(engine, obj->GetProperty("uri"), value->data); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code); - if (!ret) { + napi_value uri = nullptr; + napi_get_named_property(env, result, "uri", &uri); + napi_status status = GetValueString(env, uri, value->data); + if (status != napi_ok) { HILOG_ERROR("Convert js value fail."); + return false; } - return ret; + + napi_value code = nullptr; + napi_get_named_property(env, result, "code", &code); + status = napi_get_value_int32(env, code, &value->code); + if (status != napi_ok) { + HILOG_ERROR("Convert js value fail."); + return false; + } + return true; }; auto errCode = CallJsMethod("rename", jsRuntime_, jsObj_.get(), argParser, retParser); @@ -737,70 +834,83 @@ int JsFileAccessExtAbility::Rename(const Uri &sourceFile, const std::string &dis } -static bool ParserListFileJsResult(NativeEngine &engine, NativeValue *nativeValue, Value> &result) +static bool ParserListFileJsResult(napi_env &env, napi_value nativeValue, Value> &result) { - NativeObject *obj = ConvertNativeValueTo(nativeValue); - if (obj == nullptr) { - HILOG_ERROR("Convert js object fail."); - return false; - } + napi_value code = nullptr; + napi_get_named_property(env, nativeValue, "code", &code); + napi_get_value_int32(env, code, &result.code); - bool ret = ConvertFromJsValue(engine, obj->GetProperty("code"), result.code); - NativeArray *nativeArray = ConvertNativeValueTo(obj->GetProperty("infos")); + napi_value nativeArray = nullptr; + napi_create_array(env, &nativeArray); + napi_get_named_property(env, nativeValue, "infos", &nativeArray); if (nativeArray == nullptr) { HILOG_ERROR("Convert js array object fail."); return false; } - for (uint32_t i = 0; i < nativeArray->GetLength(); i++) { - NativeValue *nativeFileInfo = nativeArray->GetElement(i); + uint32_t length = 0; + napi_get_array_length(env, nativeArray, &length); + for (uint32_t i = 0; i < length; i++) { + napi_value nativeFileInfo = nullptr; + napi_get_element(env, nativeArray, i, &nativeFileInfo); if (nativeFileInfo == nullptr) { HILOG_ERROR("get native FileInfo fail."); return false; } - obj = ConvertNativeValueTo(nativeFileInfo); - if (obj == nullptr) { - HILOG_ERROR("Convert js object fail."); - return false; - } - FileInfo fileInfo; - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("uri"), fileInfo.uri); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("relativePath"), fileInfo.relativePath); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("fileName"), fileInfo.fileName); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mode"), fileInfo.mode); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("size"), fileInfo.size); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mtime"), fileInfo.mtime); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mimeType"), fileInfo.mimeType); - if (!ret) { - HILOG_ERROR("Convert js value fail."); - return ret; - } + + napi_value uri = nullptr; + napi_get_named_property(env, nativeFileInfo, "uri", &uri); + GetValueString(env, uri, fileInfo.uri); + + napi_value relativePath = nullptr; + napi_get_named_property(env, nativeFileInfo, "relativePath", &relativePath); + GetValueString(env, relativePath, fileInfo.relativePath); + + napi_value fileName = nullptr; + napi_get_named_property(env, nativeFileInfo, "fileName", &fileName); + GetValueString(env, fileName, fileInfo.fileName); + + napi_value mode = nullptr; + napi_get_named_property(env, nativeFileInfo, "mode", &mode); + napi_get_value_int32(env, mode, &fileInfo.mode); + + napi_value size = nullptr; + napi_get_named_property(env, nativeFileInfo, "size", &size); + napi_get_value_int64(env, size, &fileInfo.size); + + napi_value mtime = nullptr; + napi_get_named_property(env, nativeFileInfo, "mtime", &mtime); + napi_get_value_int64(env, mtime, &fileInfo.mtime); + + napi_value mimeType = nullptr; + napi_get_named_property(env, nativeFileInfo, "mimeType", &mimeType); + GetValueString(env, mimeType, fileInfo.mimeType); result.data.emplace_back(std::move(fileInfo)); } return true; } -static int MakeStringNativeArray(NativeEngine &engine, std::vector &inputArray, NativeValue *resultArray) +static int MakeStringNativeArray(napi_env &env, std::vector &inputArray, napi_value resultArray) { - NativeArray *nativeArray = ConvertNativeValueTo(resultArray); - if (nativeArray == nullptr) { + if (resultArray == nullptr) { HILOG_ERROR("Create NativeArray nullptr"); return E_GETRESULT; } bool ret = false; for (uint32_t i = 0; i < inputArray.size(); i++) { - NativeValue* nativeValue = engine.CreateString(inputArray[i].c_str(), inputArray[i].length()); + napi_value nativeValue = nullptr; + napi_create_string_utf8(env, inputArray[i].c_str(), inputArray[i].length(), &nativeValue); if (nativeValue == nullptr) { HILOG_ERROR("Create NativeValue fail."); return E_GETRESULT; } - ret = nativeArray->SetElement(i, nativeValue); - if (!ret) { + ret = napi_set_element(env, resultArray, i, nativeValue); + if (ret != napi_ok) { HILOG_ERROR("Add NativeValue to NativeArray fail."); return E_IPCS; } @@ -809,120 +919,149 @@ static int MakeStringNativeArray(NativeEngine &engine, std::vector return ERR_OK; } -static int MakeJsNativeFileFilter(NativeEngine &engine, const FileFilter &filter, NativeValue *nativeFilter) +static int MakeJsNativeFileFilter(napi_env &env, const FileFilter &filter, napi_value nativeFilter) { - NativeValue *suffixArray = engine.CreateArray(filter.GetSuffix().size()); + napi_value suffixArray = nullptr; + napi_create_array_with_length(env, filter.GetSuffix().size(), &suffixArray); if (suffixArray == nullptr) { HILOG_ERROR("Create Suffix native array value fail."); return E_GETRESULT; } std::vector suffixVec = filter.GetSuffix(); - int errorCode = MakeStringNativeArray(engine, suffixVec, suffixArray); + int errorCode = MakeStringNativeArray(env, suffixVec, suffixArray); if (errorCode != ERR_OK) { HILOG_ERROR("Create Suffix native array value fail, code:%{public}d.", errorCode); return errorCode; } - NativeValue *displayNameArray = engine.CreateArray(filter.GetDisplayName().size()); + napi_value displayNameArray = nullptr; + napi_create_array_with_length(env, filter.GetDisplayName().size(), &displayNameArray); if (displayNameArray == nullptr) { HILOG_ERROR("Create DisplayName native array value fail."); return E_GETRESULT; } std::vector displayNameVec = filter.GetDisplayName(); - errorCode = MakeStringNativeArray(engine, displayNameVec, displayNameArray); + errorCode = MakeStringNativeArray(env, displayNameVec, displayNameArray); if (errorCode != ERR_OK) { HILOG_ERROR("Create DisplayName native array value fail, code:%{public}d.", errorCode); return errorCode; } - NativeValue *mimeTypeArray = engine.CreateArray(filter.GetMimeType().size()); + napi_value mimeTypeArray = nullptr; + napi_create_array_with_length(env, filter.GetMimeType().size(), &mimeTypeArray); if (mimeTypeArray == nullptr) { HILOG_ERROR("Create MimeType native array value fail."); return E_GETRESULT; } std::vector mimeTypeVec = filter.GetMimeType(); - errorCode = MakeStringNativeArray(engine, mimeTypeVec, mimeTypeArray); + errorCode = MakeStringNativeArray(env, mimeTypeVec, mimeTypeArray); if (errorCode != ERR_OK) { HILOG_ERROR("Create MimeType native array value fail, code:%{public}d.", errorCode); return errorCode; } - NativeValue *nativeFileSizeOver = engine.CreateNumber(filter.GetFileSizeOver()); + napi_value nativeFileSizeOver = nullptr; + napi_create_int64(env, filter.GetFileSizeOver(), &nativeFileSizeOver); if (nativeFileSizeOver == nullptr) { HILOG_ERROR("Create NativeFileSizeOver native js value fail."); return E_GETRESULT; } - NativeValue *nativeLastModifiedAfter = engine.CreateNumber(filter.GetLastModifiedAfter()); + napi_value nativeLastModifiedAfter = nullptr; + napi_create_double(env, filter.GetLastModifiedAfter(), &nativeLastModifiedAfter); if (nativeLastModifiedAfter == nullptr) { HILOG_ERROR("Create NativeLastModifiedAfter native js value fail."); return E_GETRESULT; } - NativeValue *nativeExcludeMedia = engine.CreateBoolean(filter.GetExcludeMedia()); + napi_value nativeExcludeMedia = nullptr; + napi_get_boolean(env, filter.GetExcludeMedia(), &nativeExcludeMedia); if (nativeExcludeMedia == nullptr) { HILOG_ERROR("Create NativeExcludeMedia native js value fail."); return E_GETRESULT; } - NativeObject *objFilter = ConvertNativeValueTo(nativeFilter); - if (objFilter == nullptr) { - HILOG_ERROR("Convert js object fail."); - return E_GETRESULT; + napi_status ret = napi_set_named_property(env, nativeFilter, "suffix", suffixArray); + if (ret != napi_ok) { + HILOG_ERROR("Set suffix property to Filter NativeValue fail."); + return EINVAL; } - bool ret = objFilter->SetProperty("suffix", suffixArray); - ret = ret && objFilter->SetProperty("displayName", displayNameArray); - ret = ret && objFilter->SetProperty("mimeType", mimeTypeArray); - ret = ret && objFilter->SetProperty("fileSizeOver", nativeFileSizeOver); - ret = ret && objFilter->SetProperty("lastModifiedAfter", nativeLastModifiedAfter); - ret = ret && objFilter->SetProperty("excludeMedia", nativeExcludeMedia); - if (!ret) { - HILOG_ERROR("Set property to Filter NativeValue fail."); + + ret = napi_set_named_property(env, nativeFilter, "displayName", displayNameArray); + if (ret != napi_ok) { + HILOG_ERROR("Set displayName property to Filter NativeValue fail."); return EINVAL; } + ret = napi_set_named_property(env, nativeFilter, "mimeType", mimeTypeArray); + if (ret != napi_ok) { + HILOG_ERROR("Set mimeType property to Filter NativeValue fail."); + return EINVAL; + } + + ret = napi_set_named_property(env, nativeFilter, "fileSizeOver", nativeFileSizeOver); + if (ret != napi_ok) { + HILOG_ERROR("Set fileSizeOver property to Filter NativeValue fail."); + return EINVAL; + } + + ret = napi_set_named_property(env, nativeFilter, "lastModifiedAfter", nativeLastModifiedAfter); + if (ret != napi_ok) { + HILOG_ERROR("Set lastModifiedAfter property to Filter NativeValue fail."); + return EINVAL; + } + + ret = napi_set_named_property(env, nativeFilter, "excludeMedia", nativeExcludeMedia); + if (ret != napi_ok) { + HILOG_ERROR("Set excludeMedia property to Filter NativeValue fail."); + return EINVAL; + } return ERR_OK; } -static bool BuildFilterParam(NativeEngine &engine, const FileFilter &filter, const FilterParam ¶m, - NativeValue *argv[], size_t &argc) +static bool BuildFilterParam(napi_env &env, const FileFilter &filter, const FilterParam ¶m, + napi_value *argv, size_t &argc) { string uriStr = param.fileInfo.uri; - NativeValue *uri = engine.CreateString(uriStr.c_str(), uriStr.length()); + napi_value uri = nullptr; + napi_create_string_utf8(env, uriStr.c_str(), uriStr.length(), &uri); if (uri == nullptr) { HILOG_ERROR("Create sourceFile uri native js value fail."); return false; } - NativeValue *nativeOffset = engine.CreateNumber(param.offset); + napi_value nativeOffset = nullptr; + napi_create_int64(env, param.offset, &nativeOffset); if (nativeOffset == nullptr) { HILOG_ERROR("Create offset native js value fail."); return false; } - NativeValue *nativeMaxCount = engine.CreateNumber(param.maxCount); + napi_value nativeMaxCount = nullptr; + napi_create_int64(env, param.maxCount, &nativeMaxCount); if (nativeMaxCount == nullptr) { HILOG_ERROR("Create maxCount native js value fail."); return false; } - NativeValue *nativeFilter = nullptr; + napi_value nativeFilter = nullptr; if (filter.GetHasFilter()) { - nativeFilter = engine.CreateObject(); + napi_create_object(env, &nativeFilter); if (nativeFilter == nullptr) { HILOG_ERROR("Create js NativeValue fail."); return false; } - int ret = MakeJsNativeFileFilter(engine, filter, nativeFilter); + int ret = MakeJsNativeFileFilter(env, filter, nativeFilter); if (ret != ERR_OK) { HILOG_ERROR("Create js NativeValue fail."); return false; } } else { - nativeFilter = engine.CreateNull(); + nativeFilter = nullptr; + napi_get_null(env, &nativeFilter); if (nativeFilter == nullptr) { HILOG_ERROR("Create js NativeValue fail."); return false; @@ -934,7 +1073,6 @@ static bool BuildFilterParam(NativeEngine &engine, const FileFilter &filter, con argv[ARGC_TWO] = nativeMaxCount; argv[ARGC_THREE] = nativeFilter; argc = ARGC_FOUR; - return true; } @@ -950,17 +1088,25 @@ int JsFileAccessExtAbility::ListFile(const FileInfo &fileInfo, const int64_t off } auto argParser = - [fileInfo, offset, maxCount, filter](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { + [fileInfo, offset, maxCount, filter](napi_env &env, napi_value *argv, size_t &argc) -> bool { struct FilterParam param; param.fileInfo = fileInfo; param.offset = offset; param.maxCount = maxCount; - return BuildFilterParam(engine, filter, param, argv, argc); + return BuildFilterParam(env, filter, param, argv, argc); }; - auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { + + auto retParser = [value](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { + HILOG_ERROR("Convert js object fail."); + return false; + } + Value> fileInfo; - bool ret = ParserListFileJsResult(engine, result, fileInfo); + bool ret = ParserListFileJsResult(env, result, fileInfo); if (!ret) { HILOG_ERROR("Parser js value fail."); return ret; @@ -997,18 +1143,25 @@ int JsFileAccessExtAbility::ScanFile(const FileInfo &fileInfo, const int64_t off } auto argParser = - [fileInfo, offset, maxCount, filter](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { + [fileInfo, offset, maxCount, filter](napi_env &env, napi_value *argv, size_t &argc) -> bool { struct FilterParam param; param.fileInfo = fileInfo; param.offset = offset; param.maxCount = maxCount; - return BuildFilterParam(engine, filter, param, argv, argc); + return BuildFilterParam(env, filter, param, argv, argc); }; - auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { + auto retParser = [value](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { + HILOG_ERROR("Convert js object fail."); + return false; + } + Value> fileInfo; - bool ret = ParserListFileJsResult(engine, result, fileInfo); + bool ret = ParserListFileJsResult(env, result, fileInfo); if (!ret) { HILOG_ERROR("Parser js value fail."); return ret; @@ -1033,44 +1186,51 @@ int JsFileAccessExtAbility::ScanFile(const FileInfo &fileInfo, const int64_t off return ERR_OK; } -static bool ParserGetRootsJsResult(NativeEngine &engine, NativeValue *nativeValue, Value> &result) +static bool ParserGetRootsJsResult(napi_env &env, napi_value nativeValue, Value> &result) { - NativeObject *obj = ConvertNativeValueTo(nativeValue); - if (obj == nullptr) { - HILOG_ERROR("Convert js object fail."); - return false; - } + napi_value code = nullptr; + napi_get_named_property(env, nativeValue, "code", &code); + napi_get_value_int32(env, code, &result.code); - bool ret = ConvertFromJsValue(engine, obj->GetProperty("code"), result.code); - NativeArray *nativeArray = ConvertNativeValueTo(obj->GetProperty("roots")); + napi_value nativeArray = nullptr; + napi_create_array(env, &nativeArray); + napi_get_named_property(env, nativeValue, "roots", &nativeArray); if (nativeArray == nullptr) { HILOG_ERROR("nativeArray is nullptr"); return false; } - for (uint32_t i = 0; i < nativeArray->GetLength(); i++) { - NativeValue *nativeRootInfo = nativeArray->GetElement(i); + uint32_t length = 0; + napi_get_array_length(env, nativeArray, &length); + for (uint32_t i = 0; i < length; i++) { + napi_value nativeRootInfo = nullptr; + napi_get_element(env, nativeArray, i, &nativeRootInfo); if (nativeRootInfo == nullptr) { HILOG_ERROR("get native FileInfo fail."); return false; } - obj = ConvertNativeValueTo(nativeRootInfo); - if (obj == nullptr) { - HILOG_ERROR("Convert js object fail."); - return false; - } - RootInfo rootInfo; - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("deviceType"), rootInfo.deviceType); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("uri"), rootInfo.uri); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("relativePath"), rootInfo.relativePath); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("displayName"), rootInfo.displayName); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("deviceFlags"), rootInfo.deviceFlags); - if (!ret) { - HILOG_ERROR("Convert js value fail."); - return ret; - } + napi_value deviceType = nullptr; + napi_get_named_property(env, nativeRootInfo, "deviceType", &deviceType); + napi_get_value_int32(env, deviceType, &rootInfo.deviceType); + + napi_value uri = nullptr; + napi_get_named_property(env, nativeRootInfo, "uri", &uri); + GetValueString(env, uri, rootInfo.uri); + + + napi_value relativePath = nullptr; + napi_get_named_property(env, nativeRootInfo, "relativePath", &relativePath); + GetValueString(env, relativePath, rootInfo.relativePath); + + napi_value displayName = nullptr; + napi_get_named_property(env, nativeRootInfo, "displayName", &displayName); + GetValueString(env, displayName, rootInfo.displayName); + + napi_value deviceFlags = nullptr; + napi_get_named_property(env, nativeRootInfo, "deviceFlags", &deviceFlags); + napi_get_value_int32(env, deviceFlags, &rootInfo.deviceFlags); result.data.emplace_back(std::move(rootInfo)); } @@ -1088,13 +1248,21 @@ int JsFileAccessExtAbility::GetRoots(std::vector &rootInfoVec) return E_GETRESULT; } - auto argParser = [](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { + auto argParser = [](napi_env &env, napi_value *argv, size_t &argc) -> bool { argc = ARGC_ZERO; return true; }; - auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { + + auto retParser = [value](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { + HILOG_ERROR("Convert js object fail."); + return false; + } + Value> rootInfoVec; - bool ret = ParserGetRootsJsResult(engine, result, rootInfoVec); + bool ret = ParserGetRootsJsResult(env, result, rootInfoVec); if (!ret) { HILOG_ERROR("Parser js value fail."); return ret; @@ -1129,25 +1297,38 @@ int JsFileAccessExtAbility::Access(const Uri &uri, bool &isExist) return E_GETRESULT; } - auto argParser = [uri](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { - NativeValue *nativeUri = engine.CreateString(uri.ToString().c_str(), uri.ToString().length()); + auto argParser = [uri](napi_env &env, napi_value *argv, size_t &argc) -> bool { + napi_value nativeUri = nullptr; + napi_create_string_utf8(env, uri.ToString().c_str(), uri.ToString().length(), &nativeUri); argv[ARGC_ZERO] = nativeUri; argc = ARGC_ONE; return true; }; - auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { - NativeObject *obj = ConvertNativeValueTo(result); - if (obj == nullptr) { - HILOG_ERROR("Convert js object fail."); - return E_GETRESULT; + + auto retParser = [value](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { + HILOG_ERROR("Call function fail."); + return false; } - bool ret = ConvertFromJsValue(engine, obj->GetProperty("isExist"), value->data); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("code"), value->code); - if (!ret) { + napi_value isExist = nullptr; + napi_get_named_property(env, result, "isExist", &isExist); + napi_status status = napi_get_value_bool(env, isExist, &value->data); + if (status != napi_ok) { HILOG_ERROR("Convert js value fail."); + return false; } - return ret; + + napi_value code = nullptr; + napi_get_named_property(env, result, "code", &code); + status = napi_get_value_int32(env, code, &value->code); + if (status != napi_ok) { + HILOG_ERROR("Convert js value fail."); + return false; + } + return true; }; auto errCode = CallJsMethod("access", jsRuntime_, jsObj_.get(), argParser, retParser); @@ -1165,38 +1346,36 @@ int JsFileAccessExtAbility::Access(const Uri &uri, bool &isExist) return ERR_OK; } -static bool ParserQueryFileJsResult(NativeEngine &engine, NativeValue *nativeValue, +static bool ParserQueryFileJsResult(napi_env &env, napi_value nativeValue, Value> &results) { - NativeObject *obj = ConvertNativeValueTo(nativeValue); - if (obj == nullptr) { - HILOG_ERROR("Convert js object fail."); - return false; - } + napi_value code = nullptr; + napi_get_named_property(env, nativeValue, "code", &code); + napi_get_value_int32(env, code, &results.code); - bool ret = ConvertFromJsValue(engine, obj->GetProperty("code"), results.code); - if (!ret) { - HILOG_ERROR("Convert the code of js value fail."); - return ret; - } - - NativeArray *nativeArray = ConvertNativeValueTo(obj->GetProperty("results")); + napi_value nativeArray = nullptr; + napi_create_array(env, &nativeArray); + napi_get_named_property(env, nativeValue, "results", &nativeArray); if (nativeArray == nullptr) { HILOG_ERROR("Convert js array object fail."); return false; } - for (uint32_t i = 0; i < nativeArray->GetLength(); i++) { - NativeValue *queryResult = nativeArray->GetElement(i); + uint32_t length = 0; + napi_get_array_length(env, nativeArray, &length); + for (uint32_t i = 0; i < length; i++) { + napi_value queryResult = nullptr; + napi_get_element(env, nativeArray, i, &queryResult); if (queryResult == nullptr) { HILOG_ERROR("get native queryResult fail."); return false; } + std::string tempValue; - ret = ConvertFromJsValue(engine, queryResult, tempValue); - if (!ret) { + napi_status ret = GetValueString(env, queryResult, tempValue); + if (ret != napi_ok) { HILOG_ERROR("Convert js value fail."); - return ret; + return false; } results.data.emplace_back(std::move(tempValue)); } @@ -1225,18 +1404,21 @@ int JsFileAccessExtAbility::Query(const Uri &uri, std::vector &colu } ConvertColumn(columns); - auto argParser = [uri, &columns](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { - NativeValue *nativeUri = engine.CreateString(uri.ToString().c_str(), uri.ToString().length()); + auto argParser = [uri, &columns](napi_env &env, napi_value *argv, size_t &argc) -> bool { + napi_value nativeUri = nullptr; + napi_create_string_utf8(env, uri.ToString().c_str(), uri.ToString().length(), &nativeUri); if (nativeUri == nullptr) { HILOG_ERROR("create uri native js value fail."); return false; } - NativeValue *resultArray = engine.CreateArray(columns.size()); + + napi_value resultArray = nullptr; + napi_create_array_with_length(env, columns.size(), &resultArray); if (resultArray == nullptr) { HILOG_ERROR("Create MimeType native array value fail."); return false; } - int errorCode = MakeStringNativeArray(engine, columns, resultArray); + int errorCode = MakeStringNativeArray(env, columns, resultArray); if (errorCode != ERR_OK) { HILOG_ERROR("Create native array value fail, code:%{public}d.", errorCode); return false; @@ -1246,9 +1428,17 @@ int JsFileAccessExtAbility::Query(const Uri &uri, std::vector &colu argc = ARGC_TWO; return true; }; - auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { + + auto retParser = [value](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { + HILOG_ERROR("Convert js object fail."); + return false; + } + Value> queryResult; - bool ret = ParserQueryFileJsResult(engine, result, queryResult); + bool ret = ParserQueryFileJsResult(env, result, queryResult); if (!ret) { HILOG_ERROR("Parser js value fail."); return ret; @@ -1283,8 +1473,9 @@ int JsFileAccessExtAbility::GetFileInfoFromUri(const Uri &selectFile, FileInfo & return E_GETRESULT; } - auto argParser = [selectFile](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { - NativeValue *nativeUri = engine.CreateString(selectFile.ToString().c_str(), selectFile.ToString().length()); + auto argParser = [selectFile](napi_env &env, napi_value *argv, size_t &argc) -> bool { + napi_value nativeUri = nullptr; + napi_create_string_utf8(env, selectFile.ToString().c_str(), selectFile.ToString().length(), &nativeUri); if (nativeUri == nullptr) { HILOG_ERROR("create selectFile uri native js value fail."); return false; @@ -1293,38 +1484,56 @@ int JsFileAccessExtAbility::GetFileInfoFromUri(const Uri &selectFile, FileInfo & argc = ARGC_ONE; return true; }; - auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { - NativeObject *obj = ConvertNativeValueTo(result); - if (obj == nullptr) { - HILOG_ERROR("Convert js object fail."); - return false; - } - bool ret = ConvertFromJsValue(engine, obj->GetProperty("code"), value->code); - if (!ret) { - HILOG_ERROR("Convert js value fail."); - return false; - } - obj = ConvertNativeValueTo(obj->GetProperty("fileInfo")); - if (obj == nullptr) { - HILOG_ERROR("Convert js-fileInfo object fail."); + auto retParser = [value](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { + HILOG_ERROR("Call function fail."); return false; } - FileInfo fileInfo; - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("uri"), fileInfo.uri); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("relativePath"), fileInfo.relativePath); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("fileName"), fileInfo.fileName); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mode"), fileInfo.mode); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("size"), fileInfo.size); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mtime"), fileInfo.mtime); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mimeType"), fileInfo.mimeType); - if (!ret) { + napi_value code = nullptr; + napi_get_named_property(env, result, "code", &code); + napi_status status = napi_get_value_int32(env, code, &value->code); + if (status != napi_ok) { HILOG_ERROR("Convert js value fail."); return false; } + + FileInfo fileInfo; + + napi_value uri = nullptr; + napi_get_named_property(env, result, "uri", &uri); + GetValueString(env, uri, fileInfo.uri); + + + napi_value relativePath = nullptr; + napi_get_named_property(env, result, "relativePath", &relativePath); + GetValueString(env, relativePath, fileInfo.relativePath); + + napi_value fileName = nullptr; + napi_get_named_property(env, result, "fileName", &fileName); + GetValueString(env, fileName, fileInfo.fileName); + + napi_value mode = nullptr; + napi_get_named_property(env, result, "mode", &mode); + napi_get_value_int32(env, mode, &fileInfo.mode); + + napi_value size = nullptr; + napi_get_named_property(env, result, "size", &size); + napi_get_value_int64(env, size, &fileInfo.size); + + napi_value mtime = nullptr; + napi_get_named_property(env, result, "mtime", &mtime); + napi_get_value_int64(env, mtime, &fileInfo.mtime); + + napi_value mimeType = nullptr; + napi_get_named_property(env, result, "mimeType", &mimeType); + GetValueString(env, mimeType, fileInfo.mimeType); + value->data = std::move(fileInfo); - return ret; + return true; }; auto errCode = CallJsMethod("getFileInfoFromUri", jsRuntime_, jsObj_.get(), argParser, retParser); @@ -1352,8 +1561,9 @@ int JsFileAccessExtAbility::GetFileInfoFromRelativePath(const std::string &selec return E_GETRESULT; } - auto argParser = [selectFileRealtivePath](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { - NativeValue *nativePath = engine.CreateString(selectFileRealtivePath.c_str(), selectFileRealtivePath.length()); + auto argParser = [selectFileRealtivePath](napi_env &env, napi_value *argv, size_t &argc) -> bool { + napi_value nativePath = nullptr; + napi_create_string_utf8(env, selectFileRealtivePath.c_str(), selectFileRealtivePath.length(), &nativePath); if (nativePath == nullptr) { HILOG_ERROR("create selectFileRealtivePath native js value fail."); return false; @@ -1362,38 +1572,56 @@ int JsFileAccessExtAbility::GetFileInfoFromRelativePath(const std::string &selec argc = ARGC_ONE; return true; }; - auto retParser = [value](NativeEngine &engine, NativeValue *result) -> bool { - NativeObject *obj = ConvertNativeValueTo(result); - if (obj == nullptr) { - HILOG_ERROR("Convert js object fail."); - return false; - } - bool ret = ConvertFromJsValue(engine, obj->GetProperty("code"), value->code); - if (!ret) { - HILOG_ERROR("Convert js value fail."); - return false; - } - obj = ConvertNativeValueTo(obj->GetProperty("fileInfo")); - if (obj == nullptr) { - HILOG_ERROR("Convert js-fileInfo object fail."); + auto retParser = [value](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { + HILOG_ERROR("Call function fail."); return false; } - FileInfo fileInfo; - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("uri"), fileInfo.uri); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("relativePath"), fileInfo.relativePath); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("fileName"), fileInfo.fileName); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mode"), fileInfo.mode); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("size"), fileInfo.size); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mtime"), fileInfo.mtime); - ret = ret && ConvertFromJsValue(engine, obj->GetProperty("mimeType"), fileInfo.mimeType); - if (!ret) { + napi_value code = nullptr; + napi_get_named_property(env, result, "code", &code); + napi_status status = napi_get_value_int32(env, code, &value->code); + if (status != napi_ok) { HILOG_ERROR("Convert js value fail."); return false; } + + FileInfo fileInfo; + + napi_value uri = nullptr; + napi_get_named_property(env, result, "uri", &uri); + GetValueString(env, uri, fileInfo.uri); + + + napi_value relativePath = nullptr; + napi_get_named_property(env, result, "relativePath", &relativePath); + GetValueString(env, relativePath, fileInfo.relativePath); + + napi_value fileName = nullptr; + napi_get_named_property(env, result, "fileName", &fileName); + GetValueString(env, fileName, fileInfo.fileName); + + napi_value mode = nullptr; + napi_get_named_property(env, result, "mode", &mode); + napi_get_value_int32(env, mode, &fileInfo.mode); + + napi_value size = nullptr; + napi_get_named_property(env, result, "size", &size); + napi_get_value_int64(env, size, &fileInfo.size); + + napi_value mtime = nullptr; + napi_get_named_property(env, result, "mtime", &mtime); + napi_get_value_int64(env, mtime, &fileInfo.mtime); + + napi_value mimeType = nullptr; + napi_get_named_property(env, result, "mimeType", &mimeType); + GetValueString(env, mimeType, fileInfo.mimeType); + value->data = std::move(fileInfo); - return ret; + return true; }; auto errCode = CallJsMethod("getFileInfoFromRelativePath", jsRuntime_, jsObj_.get(), argParser, retParser); @@ -1411,34 +1639,34 @@ int JsFileAccessExtAbility::GetFileInfoFromRelativePath(const std::string &selec return ERR_OK; } -NativeValue* JsFileAccessExtAbility::FuncCallback(NativeEngine* engine, NativeCallbackInfo* info) +napi_value JsFileAccessExtAbility::FuncCallback(napi_env env, napi_callback_info info) { UserAccessTracer trace; trace.Start("FuncCallback"); - if (engine == nullptr) { + if (env == nullptr) { HILOG_ERROR("NativeEngine pointer is null."); return nullptr; } if (info == nullptr) { HILOG_ERROR("invalid param."); - return engine->CreateUndefined(); + return GetUndefinedValue(env); } - if (info->argc != ARGC_TWO) { - HILOG_ERROR("invalid args."); - return engine->CreateUndefined(); + size_t argc = ARGC_TWO; + napi_value argv[ARGC_TWO] = { nullptr }; + napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); + if (status != napi_ok) { + HILOG_ERROR("napi_get_cb_info fail."); } - - if (info->functionInfo == nullptr || info->functionInfo->data == nullptr) { - HILOG_ERROR("invalid object."); - return engine->CreateUndefined(); + if (argc != ARGC_TWO) { + HILOG_ERROR("invalid args."); + return GetUndefinedValue(env); } - std::string uriString = UnwrapStringFromJS(reinterpret_cast(engine), - reinterpret_cast(info->argv[ARGC_ZERO])); - int32_t event = UnwrapInt32FromJS(reinterpret_cast(engine), - reinterpret_cast(info->argv[ARGC_ONE])); + std::string uriString; + GetValueString(env, argv[ARGC_ZERO], uriString); + int32_t event = UnwrapInt32FromJS(env, argv[ARGC_ONE]); Uri uri(uriString); NotifyType notifyType = static_cast(event); @@ -1446,7 +1674,7 @@ NativeValue* JsFileAccessExtAbility::FuncCallback(NativeEngine* engine, NativeCa if (ret != ERR_OK) { HILOG_ERROR("JsFileAccessExtAbility notify error, ret:%{public}d", ret); } - return engine->CreateUndefined(); + return GetUndefinedValue(env); } int JsFileAccessExtAbility::StartWatcher(const Uri &uri) @@ -1459,27 +1687,36 @@ int JsFileAccessExtAbility::StartWatcher(const Uri &uri) return E_GETRESULT; } - auto argParser = [uri, this](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { - NativeValue *nativeUri = engine.CreateString(uri.ToString().c_str(), uri.ToString().length()); + auto argParser = [uri, this](napi_env &env, napi_value *argv, size_t &argc) -> bool { + napi_value nativeUri = nullptr; + napi_create_string_utf8(env, uri.ToString().c_str(), uri.ToString().length(), &nativeUri); if (nativeUri == nullptr) { HILOG_ERROR("create uri native js value fail."); return false; } const std::string funcName = "FuncCallback"; - NativeValue* func = engine.CreateFunction(funcName.c_str(), funcName.length(), - JsFileAccessExtAbility::FuncCallback, this); + napi_value func = nullptr; + napi_create_function(env, funcName.c_str(), funcName.length(), JsFileAccessExtAbility::FuncCallback, nullptr, &func); argv[ARGC_ZERO] = nativeUri; argv[ARGC_ONE] = func; argc = ARGC_TWO; return true; }; - auto retParser = [ret](NativeEngine &engine, NativeValue *result) -> bool { - bool res = ConvertFromJsValue(engine, result, *ret); - if (!res) { + auto retParser = [ret](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { + HILOG_ERROR("Call function fail."); + return false; + } + + napi_status status = napi_get_value_int32(env, result, ret.get()); + if (status != napi_ok) { HILOG_ERROR("Convert js value fail."); + return false; } - return res; + return true; }; auto errCode = CallJsMethod("startWatcher", jsRuntime_, jsObj_.get(), argParser, retParser); @@ -1506,25 +1743,35 @@ int JsFileAccessExtAbility::StopWatcher(const Uri &uri, bool isUnregisterAll) return E_GETRESULT; } - auto argParser = [uri, isUnregisterAll](NativeEngine &engine, NativeValue *argv[], size_t &argc) -> bool { - NativeValue *nativeUri = engine.CreateString(uri.ToString().c_str(), uri.ToString().length()); + auto argParser = [uri, isUnregisterAll](napi_env &env, napi_value *argv, size_t &argc) -> bool { + napi_value nativeUri = nullptr; + napi_create_string_utf8(env, uri.ToString().c_str(), uri.ToString().length(), &nativeUri); if (nativeUri == nullptr) { HILOG_ERROR("create uri native js value fail."); return false; } - NativeValue *isCleanAll = engine.CreateBoolean(isUnregisterAll); + + napi_value isCleanAll = nullptr; + napi_get_boolean(env, isUnregisterAll, &isCleanAll); argv[ARGC_ZERO] = nativeUri; argv[ARGC_ONE] = isCleanAll; argc = ARGC_TWO; return true; }; - auto retParser = [ret](NativeEngine &engine, NativeValue *result) -> bool { - bool res = ConvertFromJsValue(engine, result, *ret); - if (!res) { + auto retParser = [ret](napi_env &env, napi_value obj, napi_value method, size_t &argc, napi_value *argv) -> bool { + napi_value result = nullptr; + napi_call_function(env, obj, method, argc, argv, &result); + if (result == nullptr) { + HILOG_ERROR("Convert js object fail."); + return false; + } + napi_status status = napi_get_value_int32(env, result, ret.get()); + if (status != napi_ok) { HILOG_ERROR("Convert js value fail."); + return false; } - return res; + return true; }; auto errCode = CallJsMethod("stopWatcher", jsRuntime_, jsObj_.get(), argParser, retParser); diff --git a/interfaces/inner_api/file_access/src/napi_common_fileaccess.cpp b/interfaces/inner_api/file_access/src/napi_common_fileaccess.cpp index 2cb81b0ac6e1ffbe46ae3cb6a7c65a094d6a4efa..21932c3dd0256e0c74ca654fffe03b97f3bf46de 100644 --- a/interfaces/inner_api/file_access/src/napi_common_fileaccess.cpp +++ b/interfaces/inner_api/file_access/src/napi_common_fileaccess.cpp @@ -136,5 +136,26 @@ bool UnwrapArrayWantFromJS(napi_env env, napi_value param, std::vector