From 915c844982f202c05841c87fef6eac2eed44cfad Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Tue, 21 Jun 2022 10:07:10 +0800 Subject: [PATCH 1/4] add move client and napi Signed-off-by: wangjianqiang --- .../file_access/include/file_access_helper.h | 1 + .../file_access/src/file_access_helper.cpp | 26 +++ .../file_access_module/file_access_common.h | 9 + .../napi_fileaccess_helper.cpp | 189 ++++++++++++++++++ .../napi_fileaccess_helper.h | 11 + .../napi/file_ext_ability/file_ext_ability.js | 5 + 6 files changed, 241 insertions(+) diff --git a/frameworks/innerkits/file_access/include/file_access_helper.h b/frameworks/innerkits/file_access/include/file_access_helper.h index 7e4f4c0f..59be925c 100644 --- a/frameworks/innerkits/file_access/include/file_access_helper.h +++ b/frameworks/innerkits/file_access/include/file_access_helper.h @@ -46,6 +46,7 @@ public: int CreateFile(Uri &parentUri, const std::string &displayName, Uri &newFileUri); int Mkdir(Uri &parentUri, const std::string &displayName, Uri &newDirUri); int Delete(Uri &selectFileUri); + int Move(Uri &sourceFileUri, Uri &targetParentUri, Uri &newFileUri); int Rename(Uri &sourceFileUri, const std::string &displayName, Uri &newFileUri); std::vector ListFile(Uri &sourceFileUri); diff --git a/frameworks/innerkits/file_access/src/file_access_helper.cpp b/frameworks/innerkits/file_access/src/file_access_helper.cpp index 64f4c986..e55b72ad 100644 --- a/frameworks/innerkits/file_access/src/file_access_helper.cpp +++ b/frameworks/innerkits/file_access/src/file_access_helper.cpp @@ -255,6 +255,32 @@ int FileAccessHelper::Delete(Uri &selectFileUri) return index; } +int FileAccessHelper::Move(Uri &sourceFileUri, Uri &targetParentUri, Uri &newFileUri) +{ + HILOG_INFO("FileAccessHelper::Move start."); + + int index = -1; + + HILOG_INFO("FileAccessHelper::Move before ConnectFileExtAbility."); + if (!fileExtConnection_->IsExtAbilityConnected()) { + fileExtConnection_->ConnectFileExtAbility(want_, token_); + } + fileExtProxy_ = fileExtConnection_->GetFileExtProxy(); + HILOG_INFO("FileAccessHelper::Move after ConnectFileExtAbility."); + if (isSystemCaller_ && fileExtProxy_) { + AddFileAccessDeathRecipient(fileExtProxy_->AsObject()); + } + + if (fileExtProxy_ == nullptr) { + HILOG_ERROR("%{public}s failed with invalid fileExtProxy_", __func__); + return index; + } + + HILOG_INFO("FileAccessHelper::Move before fileExtProxy_->Move."); + index = fileExtProxy_->Move(sourceFileUri, targetParentUri, newFileUri); + return index; +} + int FileAccessHelper::Rename(Uri &sourceFileUri, const std::string &displayName, Uri &newFileUri) { HILOG_INFO("%{public}s begin.", __func__); diff --git a/interfaces/kits/napi/file_access_module/file_access_common.h b/interfaces/kits/napi/file_access_module/file_access_common.h index 8dd9ff17..4bc188b4 100644 --- a/interfaces/kits/napi/file_access_module/file_access_common.h +++ b/interfaces/kits/napi/file_access_module/file_access_common.h @@ -75,6 +75,15 @@ struct FileAccessHelperDeleteCB { int execResult; }; +struct FileAccessHelperMoveCB { + CBBase cbBase; + FileAccessHelper *fileAccessHelper = nullptr; + std::string sourceFileUri; + std::string targetParentUri; + std::string result; + int execResult; +}; + struct FileAccessHelperRenameCB { CBBase cbBase; FileAccessHelper *fileAccessHelper = nullptr; diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp index b213522d..29316355 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp @@ -135,6 +135,7 @@ napi_value FileAccessHelperInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("mkdir", NAPI_Mkdir), DECLARE_NAPI_FUNCTION("createFile", NAPI_CreateFile), DECLARE_NAPI_FUNCTION("delete", NAPI_Delete), + DECLARE_NAPI_FUNCTION("move", NAPI_Move), DECLARE_NAPI_FUNCTION("rename", NAPI_Rename), DECLARE_NAPI_FUNCTION("listFile", NAPI_ListFile), DECLARE_NAPI_FUNCTION("getRoots", NAPI_GetRoots), @@ -936,6 +937,194 @@ void DeletePromiseCompleteCB(napi_env env, napi_status status, void *data) HILOG_INFO("NAPI_Delete, %{public}s end.", __func__); } +napi_value NAPI_Move(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s,called", __func__); + FileAccessHelperMoveCB *moveCB = new (std::nothrow) FileAccessHelperMoveCB; + if (moveCB == nullptr) { + HILOG_ERROR("%{public}s, moveCB == nullptr.", __func__); + return WrapVoidToJS(env); + } + moveCB->cbBase.cbInfo.env = env; + moveCB->cbBase.asyncWork = nullptr; + moveCB->cbBase.deferred = nullptr; + moveCB->cbBase.ability = nullptr; + + napi_value ret = MoveWrap(env, info, moveCB); + if (ret == nullptr) { + HILOG_ERROR("%{public}s,ret == nullptr", __func__); + if (moveCB != nullptr) { + delete moveCB; + moveCB = nullptr; + } + ret = WrapVoidToJS(env); + } + HILOG_INFO("%{public}s,end", __func__); + return ret; +} + +napi_value MoveWrap(napi_env env, napi_callback_info info, FileAccessHelperMoveCB *moveCB) +{ + HILOG_INFO("%{public}s,called", __func__); + size_t argcAsync = ARGS_THREE; + const size_t argcPromise = ARGS_TWO; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = 0; + 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; + } + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[PARAM0], &valuetype)); + if (valuetype == napi_string) { + moveCB->sourceFileUri = NapiValueToStringUtf8(env, args[PARAM0]); + HILOG_INFO("%{public}s,sourceFileUri=%{public}s", __func__, moveCB->sourceFileUri.c_str()); + } + + NAPI_CALL(env, napi_typeof(env, args[PARAM1], &valuetype)); + if (valuetype == napi_string) { + moveCB->targetParentUri = NapiValueToStringUtf8(env, args[PARAM1]); + HILOG_INFO("%{public}s,targetParentUri=%{public}s", __func__, moveCB->targetParentUri.c_str()); + } + + FileAccessHelper *objectInfo = nullptr; + napi_unwrap(env, thisVar, (void **)&objectInfo); + HILOG_INFO("%{public}s,FileAccessHelper objectInfo", __func__); + moveCB->fileAccessHelper = objectInfo; + + if (argcAsync > argcPromise) { + ret = MoveAsync(env, args, ARGS_TWO, moveCB); + } else { + ret = MovePromise(env, moveCB); + } + HILOG_INFO("%{public}s,end", __func__); + return ret; +} + +napi_value MoveAsync(napi_env env, napi_value *args, const size_t argCallback, FileAccessHelperMoveCB *moveCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || moveCB == 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, &moveCB->cbBase.cbInfo.callback)); + } + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + MoveExecuteCB, + MoveAsyncCompleteCB, + (void *)moveCB, + &moveCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, moveCB->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 MovePromise(napi_env env, FileAccessHelperMoveCB *moveCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (moveCB == 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)); + moveCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + MoveExecuteCB, + MovePromiseCompleteCB, + (void *)moveCB, + &moveCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, moveCB->cbBase.asyncWork)); + HILOG_INFO("%{public}s, promise end.", __func__); + return promise; +} + +void MoveExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_Move, worker pool thread execute."); + FileAccessHelperMoveCB *moveCB = static_cast(data); + if (moveCB->fileAccessHelper != nullptr) { + moveCB->execResult = -1; + if (!moveCB->sourceFileUri.empty()) { + OHOS::Uri sourceFileUri(moveCB->sourceFileUri); + OHOS::Uri targetParentUri(moveCB->targetParentUri); + std::string newFile = ""; + OHOS::Uri newFileUri(newFile); + int err = moveCB->fileAccessHelper->Move(sourceFileUri, targetParentUri, newFileUri); + moveCB->result = newFileUri.ToString(); + moveCB->execResult = err; + } else { + HILOG_ERROR("NAPI_Move, fileAccessHelper sourceFileUri is empty"); + } + } else { + HILOG_ERROR("NAPI_Move, fileAccessHelper == nullptr"); + } + HILOG_INFO("NAPI_Move, worker pool thread execute end."); +} + +void MoveAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_Move, main event thread complete."); + FileAccessHelperMoveCB *moveCB = 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, moveCB->cbBase.cbInfo.callback, &callback)); + + result[PARAM0] = GetCallbackErrorValue(env, moveCB->execResult); + NAPI_CALL_RETURN_VOID( + env, napi_create_string_utf8(env, moveCB->result.c_str(), NAPI_AUTO_LENGTH, &result[PARAM1])); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (moveCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, moveCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, moveCB->cbBase.asyncWork)); + delete moveCB; + moveCB = nullptr; + HILOG_INFO("NAPI_Move, main event thread complete end."); +} + +void MovePromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_Move, main event thread complete."); + FileAccessHelperMoveCB *moveCB = static_cast(data); + napi_value result = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, moveCB->result.c_str(), NAPI_AUTO_LENGTH, &result)); + NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, moveCB->cbBase.deferred, result)); + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, moveCB->cbBase.asyncWork)); + delete moveCB; + moveCB = nullptr; + HILOG_INFO("NAPI_Move, main event thread complete end."); +} + napi_value NAPI_Rename(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s,called", __func__); diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h index 45ef1413..0f4fb297 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.h @@ -66,6 +66,17 @@ namespace FileAccessFwk { void DeleteAsyncCompleteCB(napi_env env, napi_status status, void *data); void DeletePromiseCompleteCB(napi_env env, napi_status status, void *data); + napi_value NAPI_Move(napi_env env, napi_callback_info info); + napi_value MoveWrap(napi_env env, napi_callback_info info, FileAccessHelperMoveCB *moveCB); + napi_value MoveAsync(napi_env env, + napi_value *args, + const size_t argCallback, + FileAccessHelperMoveCB *moveCB); + napi_value MovePromise(napi_env env, FileAccessHelperMoveCB *moveCB); + void MoveExecuteCB(napi_env env, void *data); + void MoveAsyncCompleteCB(napi_env env, napi_status status, void *data); + void MovePromiseCompleteCB(napi_env env, napi_status status, void *data); + napi_value NAPI_Rename(napi_env env, napi_callback_info info); napi_value RenameWrap(napi_env env, napi_callback_info info, FileAccessHelperRenameCB *renameCB); napi_value RenameAsync(napi_env env, diff --git a/interfaces/kits/napi/file_ext_ability/file_ext_ability.js b/interfaces/kits/napi/file_ext_ability/file_ext_ability.js index ac434438..af76fcf3 100644 --- a/interfaces/kits/napi/file_ext_ability/file_ext_ability.js +++ b/interfaces/kits/napi/file_ext_ability/file_ext_ability.js @@ -38,6 +38,11 @@ class FileExtensionAbility { return 0; } + move(sourceFileUri, targetParentUri) { + console.log('js c++ tag dsa move, sourceFileUri:' + sourceFileUri + ',targetParentUri:' + targetParentUri); + return "filetest://fileext.share/temp/test/move000.xl"; + } + rename(sourceFileUri, displayName) { console.log('js c++ tag dsa rename, sourceFileUri:' + sourceFileUri + ',displayName:' + displayName); return "filetest://fileext.share/temp/test/rename000.ttt"; -- Gitee From bcaf1ab566d783c6e60f3d228975cf8617190770 Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Tue, 21 Jun 2022 15:35:05 +0800 Subject: [PATCH 2/4] Modify the question Signed-off-by: wangjianqiang --- frameworks/innerkits/file_access/src/file_access_helper.cpp | 4 ---- .../kits/napi/file_access_module/napi_fileaccess_helper.cpp | 3 +++ 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/frameworks/innerkits/file_access/src/file_access_helper.cpp b/frameworks/innerkits/file_access/src/file_access_helper.cpp index e55b72ad..56c753a1 100644 --- a/frameworks/innerkits/file_access/src/file_access_helper.cpp +++ b/frameworks/innerkits/file_access/src/file_access_helper.cpp @@ -258,15 +258,12 @@ int FileAccessHelper::Delete(Uri &selectFileUri) int FileAccessHelper::Move(Uri &sourceFileUri, Uri &targetParentUri, Uri &newFileUri) { HILOG_INFO("FileAccessHelper::Move start."); - int index = -1; - HILOG_INFO("FileAccessHelper::Move before ConnectFileExtAbility."); if (!fileExtConnection_->IsExtAbilityConnected()) { fileExtConnection_->ConnectFileExtAbility(want_, token_); } fileExtProxy_ = fileExtConnection_->GetFileExtProxy(); - HILOG_INFO("FileAccessHelper::Move after ConnectFileExtAbility."); if (isSystemCaller_ && fileExtProxy_) { AddFileAccessDeathRecipient(fileExtProxy_->AsObject()); } @@ -276,7 +273,6 @@ int FileAccessHelper::Move(Uri &sourceFileUri, Uri &targetParentUri, Uri &newFil return index; } - HILOG_INFO("FileAccessHelper::Move before fileExtProxy_->Move."); index = fileExtProxy_->Move(sourceFileUri, targetParentUri, newFileUri); return index; } diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp index 29316355..0c4c48a2 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp @@ -1091,6 +1091,9 @@ void MoveAsyncCompleteCB(napi_env env, napi_status status, void *data) { HILOG_INFO("NAPI_Move, main event thread complete."); FileAccessHelperMoveCB *moveCB = static_cast(data); + if(moveCB == nullptr) { + return ; + } napi_value callback = nullptr; napi_value undefined = nullptr; napi_value result[ARGS_TWO] = {nullptr}; -- Gitee From b3f30c14e2d864994c54e1195dacf94d87dd0117 Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Tue, 21 Jun 2022 15:41:24 +0800 Subject: [PATCH 3/4] clean codecheck Signed-off-by: wangjianqiang --- .../kits/napi/file_access_module/napi_fileaccess_helper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp index 0c4c48a2..ce974272 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp @@ -1091,7 +1091,7 @@ void MoveAsyncCompleteCB(napi_env env, napi_status status, void *data) { HILOG_INFO("NAPI_Move, main event thread complete."); FileAccessHelperMoveCB *moveCB = static_cast(data); - if(moveCB == nullptr) { + if( moveCB == nullptr ) { return ; } napi_value callback = nullptr; -- Gitee From fa57935a3c12516f15a74a46b05ed0b182bc5463 Mon Sep 17 00:00:00 2001 From: wangjianqiang Date: Tue, 21 Jun 2022 15:48:22 +0800 Subject: [PATCH 4/4] clean codecheck Signed-off-by: wangjianqiang --- .../kits/napi/file_access_module/napi_fileaccess_helper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp index ce974272..190b386b 100644 --- a/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/interfaces/kits/napi/file_access_module/napi_fileaccess_helper.cpp @@ -1091,7 +1091,7 @@ void MoveAsyncCompleteCB(napi_env env, napi_status status, void *data) { HILOG_INFO("NAPI_Move, main event thread complete."); FileAccessHelperMoveCB *moveCB = static_cast(data); - if( moveCB == nullptr ) { + if (moveCB == nullptr) { return ; } napi_value callback = nullptr; -- Gitee