From bb5d9ac94c42647cedd996f5c0b2854170005047 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Thu, 9 Nov 2023 17:08:26 +0800 Subject: [PATCH 1/6] add cloudSyncNapi Signed-off-by: yang-jingbo1985 --- interfaces/kits/js/cloudfilesync/BUILD.gn | 2 + .../cloudfilesync/cloud_sync_n_exporter.cpp | 4 +- .../kits/js/cloudfilesync/cloud_sync_napi.cpp | 315 ++++++++++++++++++ .../kits/js/cloudfilesync/cloud_sync_napi.h | 73 ++++ .../kits/js/cloudfilesync/file_sync_napi.cpp | 43 +++ .../kits/js/cloudfilesync/file_sync_napi.h | 31 ++ .../js/cloudfilesync/gallery_sync_napi.cpp | 258 +------------- .../kits/js/cloudfilesync/gallery_sync_napi.h | 47 +-- 8 files changed, 474 insertions(+), 299 deletions(-) create mode 100644 interfaces/kits/js/cloudfilesync/cloud_sync_napi.cpp create mode 100644 interfaces/kits/js/cloudfilesync/cloud_sync_napi.h create mode 100644 interfaces/kits/js/cloudfilesync/file_sync_napi.cpp create mode 100644 interfaces/kits/js/cloudfilesync/file_sync_napi.h diff --git a/interfaces/kits/js/cloudfilesync/BUILD.gn b/interfaces/kits/js/cloudfilesync/BUILD.gn index 55950f66c..369a35111 100644 --- a/interfaces/kits/js/cloudfilesync/BUILD.gn +++ b/interfaces/kits/js/cloudfilesync/BUILD.gn @@ -25,6 +25,8 @@ ohos_shared_library("cloudsync") { sources = [ "cloud_file_download_napi.cpp", "cloud_sync_n_exporter.cpp", + "cloud_sync_napi.cpp", + "file_sync_napi.cpp", "gallery_sync_napi.cpp", ] deps = [ diff --git a/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp b/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp index 30bfc485b..2bdd16fc1 100644 --- a/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp +++ b/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp @@ -15,7 +15,8 @@ #include "cloud_sync_n_exporter.h" #include "cloud_file_download_napi.h" -#include "cloud_sync_n_exporter.h" +#include "cloud_sync_napi.h" +#include "file_sync_napi.h" #include "gallery_sync_napi.h" #include "utils_log.h" @@ -54,6 +55,7 @@ napi_value CloudSyncExport(napi_env env, napi_value exports) std::vector> products; products.emplace_back(std::make_unique(env, exports)); products.emplace_back(std::make_unique(env, exports)); + products.emplace_back(std::make_unique(env, exports)); for (auto &&product : products) { if (!product->Export()) { LOGE("INNER BUG. Failed to export class %{public}s for module cloudSyncDownload ", diff --git a/interfaces/kits/js/cloudfilesync/cloud_sync_napi.cpp b/interfaces/kits/js/cloudfilesync/cloud_sync_napi.cpp new file mode 100644 index 000000000..8167e57b2 --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/cloud_sync_napi.cpp @@ -0,0 +1,315 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cloud_sync_napi.h" + +#include + +#include "cloud_sync_manager.h" +#include "dfs_error.h" +#include "utils_log.h" +#include "async_work.h" +#include "uv.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace FileManagement::LibN; +using namespace std; + +CloudSyncCallbackImpl::CloudSyncCallbackImpl(napi_env env, napi_value fun) : env_(env) +{ + if (fun != nullptr) { + napi_create_reference(env_, fun, 1, &cbOnRef_); + } +} + +void CloudSyncCallbackImpl::OnComplete(UvChangeMsg *msg) +{ + auto cloudSyncCallback = msg->cloudSyncCallback_.lock(); + if (cloudSyncCallback == nullptr || cloudSyncCallback->cbOnRef_ == nullptr) { + LOGE("cloudSyncCallback->cbOnRef_ is nullptr"); + return; + } + auto env = cloudSyncCallback->env_; + auto ref = cloudSyncCallback->cbOnRef_; + napi_handle_scope scope = nullptr; + napi_open_handle_scope(env, &scope); + napi_value jsCallback = nullptr; + napi_status status = napi_get_reference_value(env, ref, &jsCallback); + if (status != napi_ok) { + LOGE("Create reference failed, status: %{public}d", status); + napi_close_handle_scope(env, scope); + return; + } + NVal obj = NVal::CreateObject(env); + obj.AddProp("state", NVal::CreateInt32(env, (int32_t)msg->state_).val_); + obj.AddProp("error", NVal::CreateInt32(env, (int32_t)msg->error_).val_); + napi_value retVal = nullptr; + napi_value global = nullptr; + napi_get_global(env, &global); + status = napi_call_function(env, global, jsCallback, ARGS_ONE, &(obj.val_), &retVal); + if (status != napi_ok) { + LOGE("napi call function failed, status: %{public}d", status); + } + napi_close_handle_scope(env, scope); +} + +void CloudSyncCallbackImpl::OnSyncStateChanged(CloudSyncState state, ErrorType error) +{ + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env_, &loop); + if (loop == nullptr) { + return; + } + + uv_work_t *work = new (nothrow) uv_work_t; + if (work == nullptr) { + LOGE("Failed to create uv work"); + return; + } + + UvChangeMsg *msg = new (std::nothrow) UvChangeMsg(shared_from_this(), state, error); + if (msg == nullptr) { + delete work; + return; + } + + work->data = reinterpret_cast(msg); + int ret = uv_queue_work( + loop, work, [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + auto msg = reinterpret_cast(work->data); + OnComplete(msg); + delete msg; + delete work; + }); + if (ret != 0) { + LOGE("Failed to execute libuv work queue, ret: %{public}d", ret); + delete msg; + delete work; + } +} + +void CloudSyncCallbackImpl::DeleteReference() +{ + if (cbOnRef_ != nullptr) { + napi_delete_reference(env_, cbOnRef_); + cbOnRef_ = nullptr; + } +} + +void CloudSyncCallbackImpl::OnSyncStateChanged(SyncType type, SyncPromptState state) +{ + return; +} + +napi_value CloudSyncNapi::Constructor(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(E_PARAMS).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + return funcArg.GetThisVar(); +} + +napi_value CloudSyncNapi::OnCallback(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO)) { + NError(E_PARAMS).ThrowErr(env, "Number of arguments unmatched"); + LOGE("OnCallback Number of arguments unmatched"); + return nullptr; + } + + auto [succ, type, ignore] = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); + if (!(succ && (type.get() == std::string("progress")))) { + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + if (!NVal(env, funcArg[(int)NARG_POS::SECOND]).TypeIs(napi_function)) { + LOGE("Argument type mismatch"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + if (callback_ != nullptr) { + LOGI("callback already exist"); + return NVal::CreateUndefined(env).val_; + } + + callback_ = make_shared(env, NVal(env, funcArg[(int)NARG_POS::SECOND]).val_); + int32_t ret = CloudSyncManager::GetInstance().RegisterCallback(callback_); + if (ret != E_OK) { + LOGE("OnCallback Register error, result: %{public}d", ret); + NError(Convert2JsErrNum(ret)).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUndefined(env).val_; +} + +napi_value CloudSyncNapi::OffCallback(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + NError(E_PARAMS).ThrowErr(env, "Number of arguments unmatched"); + LOGE("OffCallback Number of arguments unmatched"); + return nullptr; + } + + auto [succ, type, ignore] = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); + if (!(succ && (type.get() == std::string("progress")))) { + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + if (funcArg.GetArgc() == (uint)NARG_CNT::TWO && !NVal(env, funcArg[(int)NARG_POS::SECOND]).TypeIs(napi_function)) { + LOGE("Argument type mismatch"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + int32_t ret = CloudSyncManager::GetInstance().UnRegisterCallback(); + if (ret != E_OK) { + LOGE("OffCallback UnRegister error, result: %{public}d", ret); + NError(Convert2JsErrNum(ret)).ThrowErr(env); + return nullptr; + } + if (callback_ != nullptr) { + /* napi delete reference */ + callback_->DeleteReference(); + callback_ = nullptr; + } + return NVal::CreateUndefined(env).val_; +} + +napi_value CloudSyncNapi::Start(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + NError(E_PARAMS).ThrowErr(env); + } + + auto cbExec = []() -> NError { + int32_t ret = CloudSyncManager::GetInstance().StartSync(); + if (ret != E_OK) { + LOGE("Start Sync error, result: %{public}d", ret); + return NError(Convert2JsErrNum(ret)); + } + return NError(ERRNO_NOERR); + }; + + auto cbComplete = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + std::string procedureName = "Start"; + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbComplete).val_; +} + +napi_value CloudSyncNapi::Stop(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = []() -> NError { + int32_t ret = CloudSyncManager::GetInstance().StopSync(); + if (ret != E_OK) { + LOGE("Stop Sync error, result: %{public}d", ret); + return NError(Convert2JsErrNum(ret)); + } + return NError(ERRNO_NOERR); + }; + + auto cbComplete = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + std::string procedureName = "Stop"; + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbComplete).val_; +} + +void CloudSyncNapi::SetClassName(std::string classname) +{ + className_ = classname; +} + +std::string CloudSyncNapi::GetClassName() +{ + return className_; +} + +bool CloudSyncNapi::ToExport(std::vector props) +{ + std::string className = GetClassName(); + auto [succ, classValue] = + NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); + if (!succ) { + NError(E_GETRESULT).ThrowErr(exports_.env_); + LOGE("Failed to define CloudSyncNapi class"); + return false; + } + + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + NError(E_GETRESULT).ThrowErr(exports_.env_); + LOGE("Failed to save CloudSyncNapi class"); + return false; + } + + return exports_.AddProp(className, classValue); +} + +bool CloudSyncNapi::Export() +{ + std::vector props = { + NVal::DeclareNapiFunction("on", OnCallback), + NVal::DeclareNapiFunction("off", OffCallback), + NVal::DeclareNapiFunction("start", Start), + NVal::DeclareNapiFunction("stop", Stop), + }; + std::string className = GetClassName(); + auto [succ, classValue] = + NClass::DefineClass(exports_.env_, className, CloudSyncNapi::Constructor, std::move(props)); + if (!succ) { + NError(E_GETRESULT).ThrowErr(exports_.env_); + LOGE("Failed to define GallerySync class"); + return false; + } + + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + NError(E_GETRESULT).ThrowErr(exports_.env_); + LOGE("Failed to save GallerySync class"); + return false; + } + + return exports_.AddProp(className, classValue); +} + +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/cloud_sync_napi.h b/interfaces/kits/js/cloudfilesync/cloud_sync_napi.h new file mode 100644 index 000000000..6aaddae98 --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/cloud_sync_napi.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_CLOUD_SYNC_NAPI_H +#define OHOS_FILEMGMT_CLOUD_SYNC_NAPI_H + +#include "cloud_sync_callback.h" +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::CloudSync { +const int32_t ARGS_ONE = 1; + +class CloudSyncCallbackImpl; +class CloudSyncNapi : public LibN::NExporter { +public: + void SetClassName(std::string classname); + std::string GetClassName() override; + bool Export() override; + bool ToExport(std::vector props); + static napi_value Constructor(napi_env env, napi_callback_info info); + + static napi_value Start(napi_env env, napi_callback_info info); + static napi_value Stop(napi_env env, napi_callback_info info); + static napi_value OnCallback(napi_env env, napi_callback_info info); + static napi_value OffCallback(napi_env env, napi_callback_info info); + + CloudSyncNapi(napi_env env, napi_value exports) : NExporter(env, exports) {}; + ~CloudSyncNapi() = default; + +private: + static inline std::shared_ptr callback_; + std::string className_; +}; + +class CloudSyncCallbackImpl : public CloudSyncCallback, public std::enable_shared_from_this { +public: + CloudSyncCallbackImpl(napi_env env, napi_value fun); + ~CloudSyncCallbackImpl() = default; + void OnSyncStateChanged(SyncType type, SyncPromptState state) override; + void OnSyncStateChanged(CloudSyncState state, ErrorType error) override; + void DeleteReference(); + + class UvChangeMsg { + public: + UvChangeMsg(std::shared_ptr cloudSyncCallbackIn, CloudSyncState state, ErrorType error) + : cloudSyncCallback_(cloudSyncCallbackIn), state_(state), error_(error) + { + } + ~UvChangeMsg() {} + std::weak_ptr cloudSyncCallback_; + CloudSyncState state_; + ErrorType error_; + }; + +private: + static void OnComplete(UvChangeMsg *msg); + napi_env env_; + napi_ref cbOnRef_ = nullptr; +}; +} // namespace OHOS::FileManagement::CloudSync +#endif // OHOS_FILEMGMT_CLOUD_SYNC_NAPI_H \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/file_sync_napi.cpp b/interfaces/kits/js/cloudfilesync/file_sync_napi.cpp new file mode 100644 index 000000000..dbb862045 --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/file_sync_napi.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "file_sync_napi.h" + +#include +#include + +#include "async_work.h" +#include "cloud_sync_manager.h" +#include "dfs_error.h" +#include "utils_log.h" +#include "uv.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace FileManagement::LibN; +using namespace std; + +bool FileSyncNapi::Export() +{ + std::vector props = { + NVal::DeclareNapiFunction("on", FileSyncNapi::OnCallback), + NVal::DeclareNapiFunction("off", FileSyncNapi::OffCallback), + NVal::DeclareNapiFunction("start", FileSyncNapi::Start), + NVal::DeclareNapiFunction("stop", FileSyncNapi::Stop), + }; + + SetClassName("FileSync"); + return ToExport(props); +} +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/file_sync_napi.h b/interfaces/kits/js/cloudfilesync/file_sync_napi.h new file mode 100644 index 000000000..c860ab13c --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/file_sync_napi.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_FILE_SYNC_NAPI_H +#define OHOS_FILEMGMT_FILE_SYNC_NAPI_H + +#include "filemgmt_libn.h" +#include "gallery_sync_napi.h" +#include "cloud_sync_napi.h" + +namespace OHOS::FileManagement::CloudSync { +class FileSyncNapi final : public CloudSyncNapi { +public: + bool Export() override; + FileSyncNapi(napi_env env, napi_value exports) : CloudSyncNapi(env, exports) {}; + ~FileSyncNapi() = default; +}; +} // namespace OHOS::FileManagement::CloudSync +#endif // OHOS_FILEMGMT_FILE_SYNC_NAPI_H \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/gallery_sync_napi.cpp b/interfaces/kits/js/cloudfilesync/gallery_sync_napi.cpp index 4a23c6f4c..17b0456d3 100644 --- a/interfaces/kits/js/cloudfilesync/gallery_sync_napi.cpp +++ b/interfaces/kits/js/cloudfilesync/gallery_sync_napi.cpp @@ -27,263 +27,13 @@ namespace OHOS::FileManagement::CloudSync { using namespace FileManagement::LibN; using namespace std; -CloudSyncCallbackImpl::CloudSyncCallbackImpl(napi_env env, napi_value fun) : env_(env) -{ - if (fun != nullptr) { - napi_create_reference(env_, fun, 1, &cbOnRef_); - } -} - -void CloudSyncCallbackImpl::OnComplete(UvChangeMsg *msg) -{ - auto cloudSyncCallback = msg->cloudSyncCallback_.lock(); - if (cloudSyncCallback == nullptr || cloudSyncCallback->cbOnRef_ == nullptr) { - LOGE("cloudSyncCallback->cbOnRef_ is nullptr"); - return; - } - auto env = cloudSyncCallback->env_; - auto ref = cloudSyncCallback->cbOnRef_; - napi_handle_scope scope = nullptr; - napi_open_handle_scope(env, &scope); - napi_value jsCallback = nullptr; - napi_status status = napi_get_reference_value(env, ref, &jsCallback); - if (status != napi_ok) { - LOGE("Create reference failed, status: %{public}d", status); - napi_close_handle_scope(env, scope); - return; - } - NVal obj = NVal::CreateObject(env); - obj.AddProp("state", NVal::CreateInt32(env, (int32_t)msg->state_).val_); - obj.AddProp("error", NVal::CreateInt32(env, (int32_t)msg->error_).val_); - napi_value retVal = nullptr; - napi_value global = nullptr; - napi_get_global(env, &global); - status = napi_call_function(env, global, jsCallback, ARGS_ONE, &(obj.val_), &retVal); - if (status != napi_ok) { - LOGE("napi call function failed, status: %{public}d", status); - } - napi_close_handle_scope(env, scope); -} - -void CloudSyncCallbackImpl::OnSyncStateChanged(CloudSyncState state, ErrorType error) -{ - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env_, &loop); - if (loop == nullptr) { - return; - } - - uv_work_t *work = new (nothrow) uv_work_t; - if (work == nullptr) { - LOGE("Failed to create uv work"); - return; - } - - UvChangeMsg *msg = new (std::nothrow) UvChangeMsg(shared_from_this(), state, error); - if (msg == nullptr) { - delete work; - return; - } - - work->data = reinterpret_cast(msg); - int ret = uv_queue_work( - loop, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - auto msg = reinterpret_cast(work->data); - OnComplete(msg); - delete msg; - delete work; - }); - if (ret != 0) { - LOGE("Failed to execute libuv work queue, ret: %{public}d", ret); - delete msg; - delete work; - } -} - -void CloudSyncCallbackImpl::DeleteReference() -{ - if (cbOnRef_ != nullptr) { - napi_delete_reference(env_, cbOnRef_); - cbOnRef_ = nullptr; - } -} - -void CloudSyncCallbackImpl::OnSyncStateChanged(SyncType type, SyncPromptState state) -{ - return; -} - -napi_value GallerySyncNapi::Constructor(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - NError(E_PARAMS).ThrowErr(env, "Number of arguments unmatched"); - return nullptr; - } - - return funcArg.GetThisVar(); -} - -napi_value GallerySyncNapi::OnCallback(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::TWO)) { - NError(E_PARAMS).ThrowErr(env, "Number of arguments unmatched"); - LOGE("OnCallback Number of arguments unmatched"); - return nullptr; - } - - auto [succ, type, ignore] = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); - if (!(succ && (type.get() == std::string("progress")))) { - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - if (!NVal(env, funcArg[(int)NARG_POS::SECOND]).TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - if (callback_ != nullptr) { - LOGI("callback already exist"); - return NVal::CreateUndefined(env).val_; - } - - callback_ = make_shared(env, NVal(env, funcArg[(int)NARG_POS::SECOND]).val_); - int32_t ret = CloudSyncManager::GetInstance().RegisterCallback(callback_); - if (ret != E_OK) { - LOGE("OnCallback Register error, result: %{public}d", ret); - NError(Convert2JsErrNum(ret)).ThrowErr(env); - return nullptr; - } - - return NVal::CreateUndefined(env).val_; -} - -napi_value GallerySyncNapi::OffCallback(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - NError(E_PARAMS).ThrowErr(env, "Number of arguments unmatched"); - LOGE("OffCallback Number of arguments unmatched"); - return nullptr; - } - - auto [succ, type, ignore] = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); - if (!(succ && (type.get() == std::string("progress")))) { - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - if (funcArg.GetArgc() == (uint)NARG_CNT::TWO && !NVal(env, funcArg[(int)NARG_POS::SECOND]).TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - int32_t ret = CloudSyncManager::GetInstance().UnRegisterCallback(); - if (ret != E_OK) { - LOGE("OffCallback UnRegister error, result: %{public}d", ret); - NError(Convert2JsErrNum(ret)).ThrowErr(env); - return nullptr; - } - if (callback_ != nullptr) { - /* napi delete reference */ - callback_->DeleteReference(); - callback_ = nullptr; - } - return NVal::CreateUndefined(env).val_; -} - -napi_value GallerySyncNapi::Start(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - NError(E_PARAMS).ThrowErr(env); - } - - auto cbExec = []() -> NError { - int32_t ret = CloudSyncManager::GetInstance().StartSync(); - if (ret != E_OK) { - LOGE("Start Sync error, result: %{public}d", ret); - return NError(Convert2JsErrNum(ret)); - } - return NError(ERRNO_NOERR); - }; - - auto cbComplete = [](napi_env env, NError err) -> NVal { - if (err) { - return {env, err.GetNapiErr(env)}; - } - return NVal::CreateUndefined(env); - }; - - std::string PROCEDURE_NAME = "Start"; - auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); - return asyncWork == nullptr ? nullptr : asyncWork->Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; -} - -napi_value GallerySyncNapi::Stop(napi_env env, napi_callback_info info) -{ - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - auto cbExec = []() -> NError { - int32_t ret = CloudSyncManager::GetInstance().StopSync(); - if (ret != E_OK) { - LOGE("Stop Sync error, result: %{public}d", ret); - return NError(Convert2JsErrNum(ret)); - } - return NError(ERRNO_NOERR); - }; - - auto cbComplete = [](napi_env env, NError err) -> NVal { - if (err) { - return {env, err.GetNapiErr(env)}; - } - return NVal::CreateUndefined(env); - }; - - std::string PROCEDURE_NAME = "Stop"; - auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); - return asyncWork == nullptr ? nullptr : asyncWork->Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; -} - -std::string GallerySyncNapi::GetClassName() -{ - return GallerySyncNapi::className_; -} - bool GallerySyncNapi::Export() { - std::vector props = { - NVal::DeclareNapiFunction("on", OnCallback), - NVal::DeclareNapiFunction("off", OffCallback), - NVal::DeclareNapiFunction("start", Start), - NVal::DeclareNapiFunction("stop", Stop), - }; - - std::string className = GetClassName(); - auto [succ, classValue] = - NClass::DefineClass(exports_.env_, className, GallerySyncNapi::Constructor, std::move(props)); - if (!succ) { - NError(E_GETRESULT).ThrowErr(exports_.env_); - LOGE("Failed to define GallerySync class"); + SetClassName("GallerySync"); + bool success = CloudSyncNapi::Export(); + if (!success) { return false; } - - succ = NClass::SaveClass(exports_.env_, className, classValue); - if (!succ) { - NError(E_GETRESULT).ThrowErr(exports_.env_); - LOGE("Failed to save GallerySync class"); - return false; - } - - return exports_.AddProp(className, classValue); + return true; } } // namespace OHOS::FileManagement::CloudSync diff --git a/interfaces/kits/js/cloudfilesync/gallery_sync_napi.h b/interfaces/kits/js/cloudfilesync/gallery_sync_napi.h index 76c5e11d0..5ca843c17 100644 --- a/interfaces/kits/js/cloudfilesync/gallery_sync_napi.h +++ b/interfaces/kits/js/cloudfilesync/gallery_sync_napi.h @@ -16,57 +16,16 @@ #ifndef OHOS_FILEMGMT_GALLERY_SYNC_NAPI_H #define OHOS_FILEMGMT_GALLERY_SYNC_NAPI_H -#include "cloud_sync_callback.h" #include "filemgmt_libn.h" +#include "cloud_sync_napi.h" namespace OHOS::FileManagement::CloudSync { -const int32_t ARGS_ONE = 1; - -class CloudSyncCallbackImpl; -class GallerySyncNapi final : public LibN::NExporter { +class GallerySyncNapi final : public CloudSyncNapi { public: bool Export() override; - std::string GetClassName() override; - - static napi_value Constructor(napi_env env, napi_callback_info info); - - static napi_value Start(napi_env env, napi_callback_info info); - static napi_value Stop(napi_env env, napi_callback_info info); - static napi_value OnCallback(napi_env env, napi_callback_info info); - static napi_value OffCallback(napi_env env, napi_callback_info info); - - GallerySyncNapi(napi_env env, napi_value exports) : NExporter(env, exports) {}; + GallerySyncNapi(napi_env env, napi_value exports) : CloudSyncNapi(env, exports) {} ~GallerySyncNapi() = default; - -private: - static inline std::shared_ptr callback_; - inline static const std::string className_ = "GallerySync"; }; -class CloudSyncCallbackImpl : public CloudSyncCallback, public std::enable_shared_from_this { -public: - CloudSyncCallbackImpl(napi_env env, napi_value fun); - ~CloudSyncCallbackImpl() = default; - void OnSyncStateChanged(SyncType type, SyncPromptState state) override; - void OnSyncStateChanged(CloudSyncState state, ErrorType error) override; - void DeleteReference(); - - class UvChangeMsg { - public: - UvChangeMsg(std::shared_ptr cloudSyncCallbackIn, CloudSyncState state, ErrorType error) - : cloudSyncCallback_(cloudSyncCallbackIn), state_(state), error_(error) - { - } - ~UvChangeMsg() {} - std::weak_ptr cloudSyncCallback_; - CloudSyncState state_; - ErrorType error_; - }; - -private: - static void OnComplete(UvChangeMsg *msg); - napi_env env_; - napi_ref cbOnRef_ = nullptr; -}; } // namespace OHOS::FileManagement::CloudSync #endif // OHOS_FILEMGMT_GALLERY_SYNC_NAPI_H -- Gitee From 1f2a76dbb2aca2e0b45603bd894bb922a4b008cc Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Thu, 9 Nov 2023 19:39:07 +0800 Subject: [PATCH 2/6] add cloudFileNapi Signed-off-by: yang-jingbo1985 --- interfaces/kits/js/cloudfilesync/BUILD.gn | 2 + .../cloudfilesync/cloud_file_cache_napi.cpp | 84 +++++ .../js/cloudfilesync/cloud_file_cache_napi.h | 35 ++ .../cloud_file_download_napi.cpp | 283 +-------------- .../cloudfilesync/cloud_file_download_napi.h | 43 +-- .../kits/js/cloudfilesync/cloud_file_napi.cpp | 322 ++++++++++++++++++ .../kits/js/cloudfilesync/cloud_file_napi.h | 71 ++++ .../cloudfilesync/cloud_sync_n_exporter.cpp | 3 + 8 files changed, 525 insertions(+), 318 deletions(-) create mode 100644 interfaces/kits/js/cloudfilesync/cloud_file_cache_napi.cpp create mode 100644 interfaces/kits/js/cloudfilesync/cloud_file_cache_napi.h create mode 100644 interfaces/kits/js/cloudfilesync/cloud_file_napi.cpp create mode 100644 interfaces/kits/js/cloudfilesync/cloud_file_napi.h diff --git a/interfaces/kits/js/cloudfilesync/BUILD.gn b/interfaces/kits/js/cloudfilesync/BUILD.gn index 369a35111..80169b59b 100644 --- a/interfaces/kits/js/cloudfilesync/BUILD.gn +++ b/interfaces/kits/js/cloudfilesync/BUILD.gn @@ -25,6 +25,8 @@ ohos_shared_library("cloudsync") { sources = [ "cloud_file_download_napi.cpp", "cloud_sync_n_exporter.cpp", + "cloud_file_napi.cpp", + "cloud_file_cache_napi.cpp", "cloud_sync_napi.cpp", "file_sync_napi.cpp", "gallery_sync_napi.cpp", diff --git a/interfaces/kits/js/cloudfilesync/cloud_file_cache_napi.cpp b/interfaces/kits/js/cloudfilesync/cloud_file_cache_napi.cpp new file mode 100644 index 000000000..1229654ce --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/cloud_file_cache_napi.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cloud_file_cache_napi.h" + +#include + +#include "async_work.h" +#include "cloud_sync_manager.h" +#include "dfs_error.h" +#include "utils_log.h" +#include "uv.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace FileManagement::LibN; +using namespace std; + +napi_value CloudFileCacheNapi::CleanCache(napi_env env, napi_callback_info info) +{ + LOGI("CleanCache start"); + NFuncArg funcArg(env, info); + + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto [succ, uri, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + LOGE("Get uri Error"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto cbExec = [uri_ = std::string(uri.get())]() -> NError { + std::string accountId = uri_; + CleanOptions cleanOptions; + int32_t ret = CloudSyncManager::GetInstance().Clean(accountId, cleanOptions); + if (ret != E_OK) { + LOGE("Clean error, result: %{public}d", ret); + return NError(Convert2JsErrNum(ret)); + } + LOGI("CleanCache Success"); + return NError(ERRNO_NOERR); + }; + + auto cbComplete = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + std::string procedureName = "cleanCache"; + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbComplete).val_; +} + +bool CloudFileCacheNapi::Export() +{ + std::vector props = { + NVal::DeclareNapiFunction("on", CloudFileCacheNapi::On), + NVal::DeclareNapiFunction("off", CloudFileCacheNapi::Off), + NVal::DeclareNapiFunction("start", CloudFileCacheNapi::Start), + NVal::DeclareNapiFunction("stop", CloudFileCacheNapi::Stop), + NVal::DeclareNapiFunction("cleanCache", CloudFileCacheNapi::CleanCache), + }; + + SetClassName("CloudFileCache"); + return ToExport(props); +} +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/cloud_file_cache_napi.h b/interfaces/kits/js/cloudfilesync/cloud_file_cache_napi.h new file mode 100644 index 000000000..a97b38dd0 --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/cloud_file_cache_napi.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_CLOUD_FILE_CACHE_NAPI_H +#define OHOS_FILEMGMT_CLOUD_FILE_CACHE_NAPI_H + +#include "cloud_download_callback.h" +#include "cloud_file_napi.h" +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::CloudSync { + +class CloudDownloadCallbackImpl; +class CloudFileCacheNapi final : public CloudFileNapi { +public: + CloudFileCacheNapi(napi_env env, napi_value exports) : CloudFileNapi(env, exports) {} + ~CloudFileCacheNapi() = default; + + bool Export() override; + static napi_value CleanCache(napi_env env, napi_callback_info info); +}; +} // namespace OHOS::FileManagement::CloudSync +#endif // OHOS_FILEMGMT_CLOUD_FILE_DOWNLOAD_NAPI_H \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/cloud_file_download_napi.cpp b/interfaces/kits/js/cloudfilesync/cloud_file_download_napi.cpp index 6c7a6bd6f..be31b237b 100644 --- a/interfaces/kits/js/cloudfilesync/cloud_file_download_napi.cpp +++ b/interfaces/kits/js/cloudfilesync/cloud_file_download_napi.cpp @@ -26,289 +26,14 @@ namespace OHOS::FileManagement::CloudSync { using namespace FileManagement::LibN; using namespace std; -const int32_t ARGS_ONE = 1; - -CloudFileDownloadNapi::CloudFileDownloadNapi(napi_env env, napi_value exports) : NExporter(env, exports) {} - -CloudFileDownloadNapi::~CloudFileDownloadNapi() {} - -napi_value CloudFileDownloadNapi::Constructor(napi_env env, napi_callback_info info) -{ - LOGI("CloudFileDownloadNapi::Constructor begin"); - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - LOGE("Start Number of arguments unmatched"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - LOGI("CloudFileDownloadNapi::Constructor end"); - return funcArg.GetThisVar(); -} - -napi_value CloudFileDownloadNapi::Start(napi_env env, napi_callback_info info) -{ - LOGI("Start begin"); - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - LOGE("Start Number of arguments unmatched"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - auto [succUri, uri, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!succUri) { - LOGE("Start get uri parameter failed!"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - auto cbExec = [uri = string(uri.get()), env = env]() -> NError { - int32_t ret = CloudSyncManager::GetInstance().StartDownloadFile(uri); - if (ret != E_OK) { - LOGE("Start Download failed! ret = %{public}d", ret); - return NError(Convert2JsErrNum(ret)); - } - LOGI("Start Download Success!"); - return NError(ERRNO_NOERR); - }; - - auto cbCompl = [](napi_env env, NError err) -> NVal { - if (err) { - return {env, err.GetNapiErr(env)}; - } - return NVal::CreateUndefined(env); - }; - - string procedureName = "cloudFileDownload"; - auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); - return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbCompl).val_; -} - -CloudDownloadCallbackImpl::CloudDownloadCallbackImpl(napi_env env, napi_value fun) : env_(env) -{ - if (fun != nullptr) { - napi_create_reference(env_, fun, 1, &cbOnRef_); - } -} - -void CloudDownloadCallbackImpl::OnComplete(UvChangeMsg *msg) -{ - auto downloadcCallback = msg->CloudDownloadCallback_.lock(); - if (downloadcCallback == nullptr || downloadcCallback->cbOnRef_ == nullptr) { - LOGE("downloadcCallback->cbOnRef_ is nullptr"); - return; - } - auto env = downloadcCallback->env_; - auto ref = downloadcCallback->cbOnRef_; - napi_handle_scope scope = nullptr; - napi_open_handle_scope(env, &scope); - napi_value jsCallback = nullptr; - napi_status status = napi_get_reference_value(env, ref, &jsCallback); - if (status != napi_ok) { - LOGE("Create reference failed, status: %{public}d", status); - napi_close_handle_scope(env, scope); - return; - } - NVal obj = NVal::CreateObject(env); - obj.AddProp("state", NVal::CreateInt32(env, (int32_t)msg->downloadProgress_.state).val_); - obj.AddProp("processed", NVal::CreateInt64(env, (int64_t)msg->downloadProgress_.downloadedSize).val_); - obj.AddProp("size", NVal::CreateInt64(env, (int64_t)msg->downloadProgress_.totalSize).val_); - obj.AddProp("uri", NVal::CreateUTF8String(env, msg->downloadProgress_.path).val_); - napi_value retVal = nullptr; - napi_value global = nullptr; - napi_get_global(env, &global); - status = napi_call_function(env, global, jsCallback, ARGS_ONE, &(obj.val_), &retVal); - if (status != napi_ok) { - LOGE("napi call function failed, status: %{public}d", status); - } - napi_close_handle_scope(env, scope); -} - -void CloudDownloadCallbackImpl::OnDownloadProcess(DownloadProgressObj &progress) -{ - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env_, &loop); - if (loop == nullptr) { - return; - } - - uv_work_t *work = new (nothrow) uv_work_t; - if (work == nullptr) { - LOGE("Failed to create uv work"); - return; - } - - UvChangeMsg *msg = new (std::nothrow) UvChangeMsg(shared_from_this(), progress); - if (msg == nullptr) { - delete work; - return; - } - work->data = reinterpret_cast(msg); - int ret = uv_queue_work( - loop, work, [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - auto msg = reinterpret_cast(work->data); - OnComplete(msg); - delete msg; - delete work; - }); - if (ret != 0) { - LOGE("Failed to execute libuv work queue, ret: %{public}d", ret); - delete msg; - delete work; - } -} - -void CloudDownloadCallbackImpl::DeleteReference() -{ - if (cbOnRef_ != nullptr) { - napi_delete_reference(env_, cbOnRef_); - cbOnRef_ = nullptr; - } -} - -napi_value CloudFileDownloadNapi::On(napi_env env, napi_callback_info info) -{ - LOGI("On begin"); - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::TWO)) { - LOGE("On Number of arguments unmatched"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - auto [succProgress, progress, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!(succProgress && std::string(progress.get()) == "progress")) { - LOGE("On get progress failed!"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - if (!NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - if (callback_ != nullptr) { - LOGI("callback already exist"); - return NVal::CreateUndefined(env).val_; - } - - callback_ = make_shared(env, NVal(env, funcArg[(int)NARG_POS::SECOND]).val_); - int32_t ret = CloudSyncManager::GetInstance().RegisterDownloadFileCallback(callback_); - if (ret != E_OK) { - LOGE("RegisterDownloadFileCallback error, ret: %{public}d", ret); - NError(Convert2JsErrNum(ret)).ThrowErr(env); - return nullptr; - } - - return NVal::CreateUndefined(env).val_; -} - -napi_value CloudFileDownloadNapi::Off(napi_env env, napi_callback_info info) -{ - LOGI("Off begin"); - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - LOGE("Off Number of arguments unmatched"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - auto [succProgress, progress, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!(succProgress && std::string(progress.get()) == "progress")) { - LOGE("Off get progress failed!"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - if (funcArg.GetArgc() == (uint)NARG_CNT::TWO &&!NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) { - LOGE("Argument type mismatch"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - int32_t ret = CloudSyncManager::GetInstance().UnregisterDownloadFileCallback(); - if (ret != E_OK) { - LOGE("UnregisterDownloadFileCallback error, ret: %{public}d", ret); - NError(Convert2JsErrNum(ret)).ThrowErr(env); - return nullptr; - } - if (callback_ != nullptr) { - /* napi delete reference */ - callback_->DeleteReference(); - callback_ = nullptr; - } - return NVal::CreateUndefined(env).val_; -} - -napi_value CloudFileDownloadNapi::Stop(napi_env env, napi_callback_info info) -{ - LOGI("Stop begin"); - NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - LOGE("Stop Number of arguments unmatched"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - auto [succUri, uri, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); - if (!succUri) { - LOGE("Stop get uri parameter failed!"); - NError(E_PARAMS).ThrowErr(env); - return nullptr; - } - - auto cbExec = [uri = string(uri.get()), env = env]() -> NError { - int32_t ret = CloudSyncManager::GetInstance().StopDownloadFile(uri); - if (ret != E_OK) { - LOGE("Stop Download failed! ret = %{public}d", ret); - return NError(Convert2JsErrNum(ret)); - } - LOGI("Stop Download Success!"); - return NError(ERRNO_NOERR); - }; - - auto cbCompl = [](napi_env env, NError err) -> NVal { - if (err) { - return {env, err.GetNapiErr(env)}; - } - return NVal::CreateUndefined(env); - }; - - string procedureName = "cloudFileDownload"; - auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); - return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbCompl).val_; -} bool CloudFileDownloadNapi::Export() { - LOGI("CloudFileDownloadNapi::Export begin"); - vector props = { - NVal::DeclareNapiFunction("start", CloudFileDownloadNapi::Start), - NVal::DeclareNapiFunction("on", CloudFileDownloadNapi::On), - NVal::DeclareNapiFunction("off", CloudFileDownloadNapi::Off), - NVal::DeclareNapiFunction("stop", CloudFileDownloadNapi::Stop), - }; - - auto [succ, classValue] = - NClass::DefineClass(exports_.env_, className, CloudFileDownloadNapi::Constructor, std::move(props)); - if (!succ) { - LOGE("Failed to define Download class"); - NError(EIO).ThrowErr(exports_.env_); - return false; - } - succ = NClass::SaveClass(exports_.env_, className, classValue); - if (!succ) { - LOGE("Failed to save Download class"); - NError(EIO).ThrowErr(exports_.env_); + SetClassName("Download"); + bool success = CloudFileNapi::Export(); + if (!success) { return false; } - - LOGI("CloudFileDownloadNapi::Export end"); - return exports_.AddProp(className, classValue); -} - -string CloudFileDownloadNapi::GetClassName() -{ - return CloudFileDownloadNapi::className; + return true; } } // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/cloud_file_download_napi.h b/interfaces/kits/js/cloudfilesync/cloud_file_download_napi.h index 396310506..857eae756 100644 --- a/interfaces/kits/js/cloudfilesync/cloud_file_download_napi.h +++ b/interfaces/kits/js/cloudfilesync/cloud_file_download_napi.h @@ -17,53 +17,18 @@ #define OHOS_FILEMGMT_CLOUD_FILE_DOWNLOAD_NAPI_H #include "cloud_download_callback.h" +#include "cloud_file_napi.h" #include "filemgmt_libn.h" namespace OHOS::FileManagement::CloudSync { class CloudDownloadCallbackImpl; -class CloudFileDownloadNapi final : public LibN::NExporter { +class CloudFileDownloadNapi final : public CloudFileNapi { public: - CloudFileDownloadNapi(napi_env env, napi_value exports); - ~CloudFileDownloadNapi() override; + CloudFileDownloadNapi(napi_env env, napi_value exports) : CloudFileNapi(env, exports) {} + ~CloudFileDownloadNapi() = default; bool Export() override; - std::string GetClassName() override; - static napi_value Constructor(napi_env env, napi_callback_info info); - static napi_value Start(napi_env env, napi_callback_info info); - static napi_value On(napi_env env, napi_callback_info info); - static napi_value Off(napi_env env, napi_callback_info info); - static napi_value Stop(napi_env env, napi_callback_info info); - -private: - static inline std::shared_ptr callback_; - inline static const std::string className = "Download"; -}; - -class CloudDownloadCallbackImpl : public CloudDownloadCallback, - public std::enable_shared_from_this { -public: - CloudDownloadCallbackImpl(napi_env env, napi_value fun); - ~CloudDownloadCallbackImpl() = default; - void OnDownloadProcess(DownloadProgressObj &progress) override; - void DeleteReference(); - - class UvChangeMsg { - public: - UvChangeMsg(std::shared_ptr CloudDownloadCallbackIn, - DownloadProgressObj downloadProgress) - : CloudDownloadCallback_(CloudDownloadCallbackIn), downloadProgress_(downloadProgress) - { - } - ~UvChangeMsg() {} - std::weak_ptr CloudDownloadCallback_; - DownloadProgressObj downloadProgress_; - }; - -private: - static void OnComplete(UvChangeMsg *msg); - napi_env env_; - napi_ref cbOnRef_ = nullptr; }; } // namespace OHOS::FileManagement::CloudSync #endif // OHOS_FILEMGMT_CLOUD_FILE_DOWNLOAD_NAPI_H \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/cloud_file_napi.cpp b/interfaces/kits/js/cloudfilesync/cloud_file_napi.cpp new file mode 100644 index 000000000..b1ab4680d --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/cloud_file_napi.cpp @@ -0,0 +1,322 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cloud_file_download_napi.h" + +#include + +#include "cloud_sync_manager.h" +#include "cloud_file_napi.h" +#include "dfs_error.h" +#include "utils_log.h" +#include "async_work.h" +#include "uv.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace FileManagement::LibN; +using namespace std; +const int32_t ARGS_ONE = 1; + +CloudFileNapi::CloudFileNapi(napi_env env, napi_value exports) : NExporter(env, exports) {} + +napi_value CloudFileNapi::Constructor(napi_env env, napi_callback_info info) +{ + LOGI("CloudFileNapi::Constructor begin"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + LOGE("Start Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + LOGI("CloudFileNapi::Constructor end"); + return funcArg.GetThisVar(); +} + +napi_value CloudFileNapi::Start(napi_env env, napi_callback_info info) +{ + LOGI("Start begin"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + LOGE("Start Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + auto [succUri, uri, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succUri) { + LOGE("Start get uri parameter failed!"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = [uri = string(uri.get()), env = env]() -> NError { + int32_t ret = CloudSyncManager::GetInstance().StartDownloadFile(uri); + if (ret != E_OK) { + LOGE("Start Download failed! ret = %{public}d", ret); + return NError(Convert2JsErrNum(ret)); + } + LOGI("Start Download Success!"); + return NError(ERRNO_NOERR); + }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + string procedureName = "cloudFileDownload"; + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbCompl).val_; +} + +CloudDownloadCallbackImpl::CloudDownloadCallbackImpl(napi_env env, napi_value fun) : env_(env) +{ + if (fun != nullptr) { + napi_create_reference(env_, fun, 1, &cbOnRef_); + } +} + +void CloudDownloadCallbackImpl::OnComplete(UvChangeMsg *msg) +{ + auto downloadcCallback = msg->CloudDownloadCallback_.lock(); + if (downloadcCallback == nullptr || downloadcCallback->cbOnRef_ == nullptr) { + LOGE("downloadcCallback->cbOnRef_ is nullptr"); + return; + } + auto env = downloadcCallback->env_; + auto ref = downloadcCallback->cbOnRef_; + napi_handle_scope scope = nullptr; + napi_open_handle_scope(env, &scope); + napi_value jsCallback = nullptr; + napi_status status = napi_get_reference_value(env, ref, &jsCallback); + if (status != napi_ok) { + LOGE("Create reference failed, status: %{public}d", status); + napi_close_handle_scope(env, scope); + return; + } + NVal obj = NVal::CreateObject(env); + obj.AddProp("state", NVal::CreateInt32(env, (int32_t)msg->downloadProgress_.state).val_); + obj.AddProp("processed", NVal::CreateInt64(env, (int64_t)msg->downloadProgress_.downloadedSize).val_); + obj.AddProp("size", NVal::CreateInt64(env, (int64_t)msg->downloadProgress_.totalSize).val_); + obj.AddProp("uri", NVal::CreateUTF8String(env, msg->downloadProgress_.path).val_); + napi_value retVal = nullptr; + napi_value global = nullptr; + napi_get_global(env, &global); + status = napi_call_function(env, global, jsCallback, ARGS_ONE, &(obj.val_), &retVal); + if (status != napi_ok) { + LOGE("napi call function failed, status: %{public}d", status); + } + napi_close_handle_scope(env, scope); +} + +void CloudDownloadCallbackImpl::OnDownloadProcess(DownloadProgressObj &progress) +{ + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env_, &loop); + if (loop == nullptr) { + return; + } + + uv_work_t *work = new (nothrow) uv_work_t; + if (work == nullptr) { + LOGE("Failed to create uv work"); + return; + } + + UvChangeMsg *msg = new (std::nothrow) UvChangeMsg(shared_from_this(), progress); + if (msg == nullptr) { + delete work; + return; + } + work->data = reinterpret_cast(msg); + int ret = uv_queue_work( + loop, work, [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + auto msg = reinterpret_cast(work->data); + OnComplete(msg); + delete msg; + delete work; + }); + if (ret != 0) { + LOGE("Failed to execute libuv work queue, ret: %{public}d", ret); + delete msg; + delete work; + } +} + +void CloudDownloadCallbackImpl::DeleteReference() +{ + if (cbOnRef_ != nullptr) { + napi_delete_reference(env_, cbOnRef_); + cbOnRef_ = nullptr; + } +} + +napi_value CloudFileNapi::On(napi_env env, napi_callback_info info) +{ + LOGI("On begin"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO)) { + LOGE("On Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + auto [succProgress, progress, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!(succProgress && std::string(progress.get()) == "progress")) { + LOGE("On get progress failed!"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + if (!NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) { + LOGE("Argument type mismatch"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + if (callback_ != nullptr) { + LOGI("callback already exist"); + return NVal::CreateUndefined(env).val_; + } + + callback_ = make_shared(env, NVal(env, funcArg[(int)NARG_POS::SECOND]).val_); + int32_t ret = CloudSyncManager::GetInstance().RegisterDownloadFileCallback(callback_); + if (ret != E_OK) { + LOGE("RegisterDownloadFileCallback error, ret: %{public}d", ret); + NError(Convert2JsErrNum(ret)).ThrowErr(env); + return nullptr; + } + + return NVal::CreateUndefined(env).val_; +} + +napi_value CloudFileNapi::Off(napi_env env, napi_callback_info info) +{ + LOGI("Off begin"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + LOGE("Off Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + auto [succProgress, progress, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!(succProgress && std::string(progress.get()) == "progress")) { + LOGE("Off get progress failed!"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + if (funcArg.GetArgc() == (uint)NARG_CNT::TWO &&!NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) { + LOGE("Argument type mismatch"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + int32_t ret = CloudSyncManager::GetInstance().UnregisterDownloadFileCallback(); + if (ret != E_OK) { + LOGE("UnregisterDownloadFileCallback error, ret: %{public}d", ret); + NError(Convert2JsErrNum(ret)).ThrowErr(env); + return nullptr; + } + if (callback_ != nullptr) { + /* napi delete reference */ + callback_->DeleteReference(); + callback_ = nullptr; + } + return NVal::CreateUndefined(env).val_; +} + +napi_value CloudFileNapi::Stop(napi_env env, napi_callback_info info) +{ + LOGI("Stop begin"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + LOGE("Stop Number of arguments unmatched"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + auto [succUri, uri, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succUri) { + LOGE("Stop get uri parameter failed!"); + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto cbExec = [uri = string(uri.get()), env = env]() -> NError { + int32_t ret = CloudSyncManager::GetInstance().StopDownloadFile(uri); + if (ret != E_OK) { + LOGE("Stop Download failed! ret = %{public}d", ret); + return NError(Convert2JsErrNum(ret)); + } + LOGI("Stop Download Success!"); + return NError(ERRNO_NOERR); + }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + return NVal::CreateUndefined(env); + }; + + string procedureName = "cloudFileDownload"; + auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast(NARG_CNT::TWO)); + return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbCompl).val_; +} + +bool CloudFileNapi::ToExport(std::vector props) +{ + std::string className = GetClassName(); + auto [succ, classValue] = + NClass::DefineClass(exports_.env_, className, Constructor, std::move(props)); + if (!succ) { + NError(E_GETRESULT).ThrowErr(exports_.env_); + LOGE("Failed to define GallerySync class"); + return false; + } + + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + NError(E_GETRESULT).ThrowErr(exports_.env_); + LOGE("Failed to save GallerySync class"); + return false; + } + + return exports_.AddProp(className, classValue); +} + +bool CloudFileNapi::Export() +{ + vector props = { + NVal::DeclareNapiFunction("start", CloudFileNapi::Start), + NVal::DeclareNapiFunction("on", CloudFileNapi::On), + NVal::DeclareNapiFunction("off", CloudFileNapi::Off), + NVal::DeclareNapiFunction("stop", CloudFileNapi::Stop), + }; + return ToExport(props); +} + +void CloudFileNapi::SetClassName(std::string classname) +{ + className = classname; +} + +string CloudFileNapi::GetClassName() +{ + return CloudFileNapi::className; +} +} // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/cloud_file_napi.h b/interfaces/kits/js/cloudfilesync/cloud_file_napi.h new file mode 100644 index 000000000..a4d1cd5d0 --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/cloud_file_napi.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_CLOUD_FILE_NAPI_H +#define OHOS_FILEMGMT_CLOUD_FILE_NAPI_H + +#include "cloud_download_callback.h" +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::CloudSync { + +class CloudDownloadCallbackImpl; +class CloudFileNapi : public LibN::NExporter { +public: + CloudFileNapi(napi_env env, napi_value exports); + ~CloudFileNapi() = default; + + bool Export() override; + bool ToExport(std::vector props); + virtual void SetClassName(std::string classname); + std::string GetClassName() override; + static napi_value Constructor(napi_env env, napi_callback_info info); + static napi_value Start(napi_env env, napi_callback_info info); + static napi_value On(napi_env env, napi_callback_info info); + static napi_value Off(napi_env env, napi_callback_info info); + static napi_value Stop(napi_env env, napi_callback_info info); + +private: + static inline std::shared_ptr callback_; + inline static std::string className = "CloudFileNapi"; +}; + +class CloudDownloadCallbackImpl : public CloudDownloadCallback, + public std::enable_shared_from_this { +public: + CloudDownloadCallbackImpl(napi_env env, napi_value fun); + ~CloudDownloadCallbackImpl() = default; + void OnDownloadProcess(DownloadProgressObj &progress) override; + void DeleteReference(); + + class UvChangeMsg { + public: + UvChangeMsg(std::shared_ptr CloudDownloadCallbackIn, + DownloadProgressObj downloadProgress) + : CloudDownloadCallback_(CloudDownloadCallbackIn), downloadProgress_(downloadProgress) + { + } + ~UvChangeMsg() {} + std::weak_ptr CloudDownloadCallback_; + DownloadProgressObj downloadProgress_; + }; + +private: + static void OnComplete(UvChangeMsg *msg); + napi_env env_; + napi_ref cbOnRef_ = nullptr; +}; +} // namespace OHOS::FileManagement::CloudSync +#endif // OHOS_FILEMGMT_CLOUD_FILE_NAPI_H \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp b/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp index 2bdd16fc1..52103e0e2 100644 --- a/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp +++ b/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp @@ -14,7 +14,9 @@ */ #include "cloud_sync_n_exporter.h" +#include "cloud_file_cache_napi.h" #include "cloud_file_download_napi.h" +#include "cloud_file_napi.h" #include "cloud_sync_napi.h" #include "file_sync_napi.h" #include "gallery_sync_napi.h" @@ -53,6 +55,7 @@ napi_value CloudSyncExport(napi_env env, napi_value exports) InitState(env, exports); std::vector> products; + products.emplace_back(std::make_unique(env, exports)); products.emplace_back(std::make_unique(env, exports)); products.emplace_back(std::make_unique(env, exports)); products.emplace_back(std::make_unique(env, exports)); -- Gitee From d0a975020581b434d8127ae30f5a0b5e54bdad43 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Thu, 9 Nov 2023 23:40:16 +0800 Subject: [PATCH 3/6] total function Signed-off-by: yang-jingbo1985 --- .../include/cloud_sync_manager_impl.h | 2 + .../include/cloud_sync_service_proxy.h | 2 + .../src/cloud_sync_manager_impl.cpp | 24 ++++ .../src/cloud_sync_service_proxy.cpp | 67 +++++++++- .../cloud_file_sync_service_interface_code.h | 4 +- .../cloudsync_kit_inner/cloud_sync_manager.h | 2 + .../i_cloud_sync_service.h | 2 + .../cloudfilesync/cloud_sync_n_exporter.cpp | 17 +++ .../js/cloudfilesync/cloud_sync_n_exporter.h | 1 + .../kits/js/cloudfilesync/file_sync_napi.cpp | 116 ++++++++++++++++++ .../kits/js/cloudfilesync/file_sync_napi.h | 12 ++ .../kits/js/cloudfilesync/gallery_sync_napi.h | 2 + .../include/ipc/cloud_sync_service.h | 2 + .../include/ipc/cloud_sync_service_stub.h | 2 + .../include/i_cloud_sync_service_mock.h | 8 ++ .../ipc/cloud_sync_service_stub_test.cpp | 2 + 16 files changed, 263 insertions(+), 2 deletions(-) diff --git a/frameworks/native/cloudsync_kit_inner/include/cloud_sync_manager_impl.h b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_manager_impl.h index 25ff03fac..7076668fb 100644 --- a/frameworks/native/cloudsync_kit_inner/include/cloud_sync_manager_impl.h +++ b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_manager_impl.h @@ -44,6 +44,8 @@ public: int32_t StopDownloadFile(const std::string &uri) override; int32_t RegisterDownloadFileCallback(const std::shared_ptr downloadCallback) override; int32_t UnregisterDownloadFileCallback() override; + int32_t GetSyncTime() override; + int32_t GetSyncState(const std::string &uri) override; private: CloudSyncManagerImpl() = default; void SetDeathRecipient(const sptr &remoteObject); diff --git a/frameworks/native/cloudsync_kit_inner/include/cloud_sync_service_proxy.h b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_service_proxy.h index ce44e8074..93b443ea8 100644 --- a/frameworks/native/cloudsync_kit_inner/include/cloud_sync_service_proxy.h +++ b/frameworks/native/cloudsync_kit_inner/include/cloud_sync_service_proxy.h @@ -45,6 +45,8 @@ public: int32_t UploadAsset(const int32_t userId, const std::string &request, std::string &result) override; int32_t DownloadFile(const int32_t userId, const std::string &bundleName, AssetInfoObj &assetInfoObj) override; int32_t DeleteAsset(const int32_t userId, const std::string &uri) override; + int32_t GetSyncTimeInner() override; + int32_t GetSyncStateInner(const std::string &uri) override; class ServiceProxyLoadCallback : public SystemAbilityLoadCallbackStub { public: diff --git a/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager_impl.cpp b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager_impl.cpp index 89dc819ba..2d16cd7bc 100644 --- a/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager_impl.cpp +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_manager_impl.cpp @@ -74,6 +74,30 @@ int32_t CloudSyncManagerImpl::StartSync() return CloudSyncServiceProxy->StartSyncInner(false); } +int32_t CloudSyncManagerImpl::GetSyncTime() +{ + LOGI("GetSyncTime Start"); + auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance(); + if (!CloudSyncServiceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + LOGE("Get time success!"); + return CloudSyncServiceProxy->GetSyncTimeInner(); +} + +int32_t CloudSyncManagerImpl::GetSyncState(const std::string &uri) +{ + LOGI("GetSyncState Start"); + auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance(); + if (!CloudSyncServiceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + LOGE("Get state success!"); + return CloudSyncServiceProxy->GetSyncStateInner(uri); +} + int32_t CloudSyncManagerImpl::StartSync(bool forceFlag, const std::shared_ptr callback) { if (!callback) { diff --git a/frameworks/native/cloudsync_kit_inner/src/cloud_sync_service_proxy.cpp b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_service_proxy.cpp index 07d0a2554..831d5aa66 100644 --- a/frameworks/native/cloudsync_kit_inner/src/cloud_sync_service_proxy.cpp +++ b/frameworks/native/cloudsync_kit_inner/src/cloud_sync_service_proxy.cpp @@ -134,6 +134,72 @@ int32_t CloudSyncServiceProxy::StartSyncInner(bool forceFlag) return reply.ReadInt32(); } +int32_t CloudSyncServiceProxy::GetSyncTimeInner() +{ + LOGI("Start GetSyncTimeInner"); + LOGI("Start Sync"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + LOGE("Failed to write interface token"); + return E_BROKEN_IPC; + } + + auto remote = Remote(); + if (!remote) { + LOGE("remote is nullptr"); + return E_BROKEN_IPC; + } + int32_t ret = remote->SendRequest( + static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_GET_SYNC_TIME), data, reply, option); + if (ret != E_OK) { + stringstream ss; + ss << "Failed to send out the requeset, errno:" << ret; + LOGE("%{public}s", ss.str().c_str()); + return E_BROKEN_IPC; + } + + LOGI("GetSyncTimeInner Success"); + return reply.ReadInt32(); +} + +int32_t CloudSyncServiceProxy::GetSyncStateInner(const std::string &uri) +{ + LOGI("Start GetSyncStateInner"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + LOGE("Failed to write interface token"); + return E_BROKEN_IPC; + } + + if (!data.WriteString(uri)) { + LOGE("Failed to send the uri"); + return E_BROKEN_IPC; + } + + auto remote = Remote(); + if (!remote) { + LOGE("remote is nullptr"); + return E_BROKEN_IPC; + } + int32_t ret = remote->SendRequest( + static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_GET_SYNC_STATE), data, reply, option); + if (ret != E_OK) { + stringstream ss; + ss << "Failed to send out the requeset, errno:" << ret; + LOGE("%{public}s", ss.str().c_str()); + return E_BROKEN_IPC; + } + + LOGI("GetSyncStateInner Success"); + return reply.ReadInt32(); +} + int32_t CloudSyncServiceProxy::StopSyncInner() { LOGI("StopSync"); @@ -406,7 +472,6 @@ int32_t CloudSyncServiceProxy::StopDownloadFile(const std::string &uri) return E_BROKEN_IPC; } - OHOS::Media::MediaFileUri Muri(uri); string path = Muri.GetFilePath(); LOGI("StopDownloadFile Start, uri: %{public}s, path: %{public}s", uri.c_str(), path.c_str()); diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_file_sync_service_interface_code.h b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_file_sync_service_interface_code.h index 68d462063..6f487d41b 100644 --- a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_file_sync_service_interface_code.h +++ b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_file_sync_service_interface_code.h @@ -34,7 +34,9 @@ enum class CloudFileSyncServiceInterfaceCode { SERVICE_CMD_UNREGISTER_DOWNLOAD_FILE_CALLBACK, SERVICE_CMD_UPLOAD_ASSET, SERVICE_CMD_DOWNLOAD_FILE, - SERVICE_CMD_DELETE_ASSET + SERVICE_CMD_DELETE_ASSET, + SERVICE_CMD_GET_SYNC_TIME, + SERVICE_CMD_GET_SYNC_STATE }; } // namespace OHOS::FileManagement::CloudSync diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h index 2bcf89e61..c3eafca8b 100644 --- a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h +++ b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h @@ -91,6 +91,8 @@ public: virtual int32_t StopDownloadFile(const std::string &path) = 0; virtual int32_t RegisterDownloadFileCallback(const std::shared_ptr downloadCallback) = 0; virtual int32_t UnregisterDownloadFileCallback() = 0; + virtual int32_t GetSyncTime() = 0; + virtual int32_t GetSyncState(const std::string &uri) = 0; }; } // namespace OHOS::FileManagement::CloudSync diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_sync_service.h b/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_sync_service.h index e9cd86d16..e36e91e1b 100644 --- a/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_sync_service.h +++ b/interfaces/inner_api/native/cloudsync_kit_inner/i_cloud_sync_service.h @@ -43,6 +43,8 @@ public: virtual int32_t UploadAsset(const int32_t userId, const std::string &request, std::string &result) = 0; virtual int32_t DownloadFile(const int32_t userId, const std::string &bundleName, AssetInfoObj &assetInfoObj) = 0; virtual int32_t DeleteAsset(const int32_t userId, const std::string &uri) = 0; + virtual int32_t GetSyncTimeInner() = 0; + virtual int32_t GetSyncStateInner(const std::string &uri) = 0; }; } // namespace OHOS::FileManagement::CloudSync diff --git a/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp b/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp index 52103e0e2..9097395d1 100644 --- a/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp +++ b/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp @@ -53,6 +53,8 @@ napi_value CloudSyncExport(napi_env env, napi_value exports) InitCloudSyncState(env, exports); InitErrorType(env, exports); InitState(env, exports); + InitFileSyncState(env, exports); + FileSyncStateExport(env, exports); std::vector> products; products.emplace_back(std::make_unique(env, exports)); @@ -88,6 +90,21 @@ void InitCloudSyncState(napi_env env, napi_value exports) napi_set_named_property(env, exports, propertyName, obj); } +void InitFileSyncState(napi_env env, napi_value exports) +{ + char propertyName[] = "FileSyncState "; + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("UPLOADING", NVal::CreateInt32(env, FILESYNCSTATE_UPLOADING).val_), + DECLARE_NAPI_STATIC_PROPERTY("DOWNLOADING", NVal::CreateInt32(env, FILESYNCSTATE_DOWNLOADING).val_), + DECLARE_NAPI_STATIC_PROPERTY("COMPLETED", NVal::CreateInt32(env, FILESYNCSTATE_COMPLETED).val_), + DECLARE_NAPI_STATIC_PROPERTY("STOPPED", NVal::CreateInt32(env, FILESYNCSTATE_STOPPED).val_), + }; + napi_value obj = nullptr; + napi_create_object(env, &obj); + napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc); + napi_set_named_property(env, exports, propertyName, obj); +} + void InitErrorType(napi_env env, napi_value exports) { char propertyName[] = "ErrorType"; diff --git a/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.h b/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.h index 42ed1e636..4c00b923b 100644 --- a/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.h +++ b/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.h @@ -20,6 +20,7 @@ namespace OHOS::FileManagement::CloudSync { void InitCloudSyncState(napi_env env, napi_value exports); +void InitFileSyncState(napi_env env, napi_value exports); void InitErrorType(napi_env env, napi_value exports); } // namespace OHOS::FileManagement::CloudSync #endif // OHOS_FILEMGMT_CLOUD_SYNC_N_EXPORTER_H \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/file_sync_napi.cpp b/interfaces/kits/js/cloudfilesync/file_sync_napi.cpp index dbb862045..17fce5ca6 100644 --- a/interfaces/kits/js/cloudfilesync/file_sync_napi.cpp +++ b/interfaces/kits/js/cloudfilesync/file_sync_napi.cpp @@ -28,6 +28,112 @@ namespace OHOS::FileManagement::CloudSync { using namespace FileManagement::LibN; using namespace std; +struct SyncTimeArg { + int32_t time = 0; +}; + +napi_value FileSyncNapi::GetLastSyncTime(napi_env env, napi_callback_info info) +{ + LOGI("GetLastSyncTime Start"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + auto arg = make_shared(); + if (arg == nullptr) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + + auto cbExec = [arg]() -> NError { + arg->time = CloudSyncManager::GetInstance().GetSyncTime(); + LOGI("GetLastSyncTime lastSyncTime:%{public}d", arg->time); + return NError(ERRNO_NOERR); + }; + + auto cbComplete = [cbArg = arg](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + LOGI("GetLastSyncTime lastSyncTime:%{public}d", cbArg->time); + return NVal::CreateInt32(env, cbArg->time); + }; + + const std::string procedureName = "getLastSyncTime"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ONE) { + if (!NVal(env, funcArg[NARG_POS::FIRST]).TypeIs(napi_number)) { + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + LOGE("Get napi_number Error"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + } else { + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } +} + +struct SyncStateArg { + FileSyncState state; +}; +napi_value GetFileSyncState(napi_env env, napi_callback_info info) +{ + LOGI("getFileSyncState Start"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + LOGE("Failed to getFileSyncState"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + LOGI("check isUriTypeString"); + auto [succ, uri, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + LOGE("Get uri Error"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto arg = make_shared(); + auto cbExec = [arg, cbUri = std::string(uri.get())]() -> NError { + arg->state = static_cast(CloudSyncManager::GetInstance().GetSyncState(cbUri)); + LOGI("GetFileSyncState fileSyncState:%{public}d", arg->state); + return NError(ERRNO_NOERR); + }; + + auto cbComplete = [cbArg = arg](napi_env env, NError err) -> NVal { + if (err) { + return {env, err.GetNapiErr(env)}; + } + LOGI("GetFileSyncState fileSyncState:%{public}d", cbArg->state); + return NVal::CreateInt32(env, static_cast(cbArg->state)); + }; + + const std::string procedureName = "getFileSyncState"; + + LOGI("GetFileSyncState branch Run"); + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::TWO) { + if (!NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number)) { + LOGI("GetFileSyncState callback right"); + NVal cb(env, funcArg[NARG_POS::SECOND]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_; + } else { + LOGE("GetFileSyncState callback Error"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + } else { + LOGI("GetFileSyncState have no callback"); + return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_; + } +} + bool FileSyncNapi::Export() { std::vector props = { @@ -35,9 +141,19 @@ bool FileSyncNapi::Export() NVal::DeclareNapiFunction("off", FileSyncNapi::OffCallback), NVal::DeclareNapiFunction("start", FileSyncNapi::Start), NVal::DeclareNapiFunction("stop", FileSyncNapi::Stop), + NVal::DeclareNapiFunction("getLastSyncTime", FileSyncNapi::GetLastSyncTime), }; SetClassName("FileSync"); return ToExport(props); } + +void FileSyncStateExport(napi_env env, napi_value exports) +{ + static napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("getFileSyncState", GetFileSyncState), + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); +} + } // namespace OHOS::FileManagement::CloudSync \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/file_sync_napi.h b/interfaces/kits/js/cloudfilesync/file_sync_napi.h index c860ab13c..0430df141 100644 --- a/interfaces/kits/js/cloudfilesync/file_sync_napi.h +++ b/interfaces/kits/js/cloudfilesync/file_sync_napi.h @@ -16,6 +16,7 @@ #ifndef OHOS_FILEMGMT_FILE_SYNC_NAPI_H #define OHOS_FILEMGMT_FILE_SYNC_NAPI_H +#include "cloud_sync_callback.h" #include "filemgmt_libn.h" #include "gallery_sync_napi.h" #include "cloud_sync_napi.h" @@ -24,8 +25,19 @@ namespace OHOS::FileManagement::CloudSync { class FileSyncNapi final : public CloudSyncNapi { public: bool Export() override; + static napi_value GetLastSyncTime(napi_env env, napi_callback_info info); FileSyncNapi(napi_env env, napi_value exports) : CloudSyncNapi(env, exports) {}; ~FileSyncNapi() = default; }; + +enum FileSyncState { + FILESYNCSTATE_UPLOADING = 0, + FILESYNCSTATE_DOWNLOADING, + FILESYNCSTATE_COMPLETED, + FILESYNCSTATE_STOPPED, + }; + +void FileSyncStateExport(napi_env env, napi_value exports); + } // namespace OHOS::FileManagement::CloudSync #endif // OHOS_FILEMGMT_FILE_SYNC_NAPI_H \ No newline at end of file diff --git a/interfaces/kits/js/cloudfilesync/gallery_sync_napi.h b/interfaces/kits/js/cloudfilesync/gallery_sync_napi.h index 5ca843c17..f32673aad 100644 --- a/interfaces/kits/js/cloudfilesync/gallery_sync_napi.h +++ b/interfaces/kits/js/cloudfilesync/gallery_sync_napi.h @@ -16,9 +16,11 @@ #ifndef OHOS_FILEMGMT_GALLERY_SYNC_NAPI_H #define OHOS_FILEMGMT_GALLERY_SYNC_NAPI_H +#include "cloud_sync_callback.h" #include "filemgmt_libn.h" #include "cloud_sync_napi.h" + namespace OHOS::FileManagement::CloudSync { class GallerySyncNapi final : public CloudSyncNapi { public: diff --git a/services/cloudsyncservice/include/ipc/cloud_sync_service.h b/services/cloudsyncservice/include/ipc/cloud_sync_service.h index fc40e2d55..e79ba92b0 100644 --- a/services/cloudsyncservice/include/ipc/cloud_sync_service.h +++ b/services/cloudsyncservice/include/ipc/cloud_sync_service.h @@ -49,6 +49,8 @@ public: int32_t UploadAsset(const int32_t userId, const std::string &request, std::string &result) override; int32_t DownloadFile(const int32_t userId, const std::string &bundleName, AssetInfoObj &assetInfoObj) override; int32_t DeleteAsset(const int32_t userId, const std::string &uri) override; + int32_t GetSyncTimeInner() override; + int32_t GetSyncStateInner(const std::string &uri) override; private: std::string GetHmdfsPath(const std::string &uri, int32_t userId); diff --git a/services/cloudsyncservice/include/ipc/cloud_sync_service_stub.h b/services/cloudsyncservice/include/ipc/cloud_sync_service_stub.h index b07728d04..a907d4e8e 100644 --- a/services/cloudsyncservice/include/ipc/cloud_sync_service_stub.h +++ b/services/cloudsyncservice/include/ipc/cloud_sync_service_stub.h @@ -46,6 +46,8 @@ private: int32_t HandleUploadAsset(MessageParcel &data, MessageParcel &reply); int32_t HandleDownloadFile(MessageParcel &data, MessageParcel &reply); int32_t HandleDeleteAsset(MessageParcel &data, MessageParcel &reply); + int32_t HandleGetSyncTime(MessageParcel &data, MessageParcel &reply); + int32_t HandleGetSyncState(MessageParcel &data, MessageParcel &reply); }; } // namespace OHOS::FileManagement::CloudSync diff --git a/test/unittests/cloudsync_api/cloudsync_impl/include/i_cloud_sync_service_mock.h b/test/unittests/cloudsync_api/cloudsync_impl/include/i_cloud_sync_service_mock.h index 5aeb14b5b..6bb45d346 100644 --- a/test/unittests/cloudsync_api/cloudsync_impl/include/i_cloud_sync_service_mock.h +++ b/test/unittests/cloudsync_api/cloudsync_impl/include/i_cloud_sync_service_mock.h @@ -111,6 +111,14 @@ public: { return E_OK; } + int32_t GetSyncTimeInner() + { + return E_OK; + } + int32_t GetSyncStateInner(const std::string &uri) + { + return E_OK; + } }; } // namespace OHOS::FileManagement::CloudSync diff --git a/test/unittests/cloudsync_sa/ipc/cloud_sync_service_stub_test.cpp b/test/unittests/cloudsync_sa/ipc/cloud_sync_service_stub_test.cpp index a73d8b2b5..516a9c3f8 100644 --- a/test/unittests/cloudsync_sa/ipc/cloud_sync_service_stub_test.cpp +++ b/test/unittests/cloudsync_sa/ipc/cloud_sync_service_stub_test.cpp @@ -47,6 +47,8 @@ public: MOCK_METHOD3(DownloadFile, int32_t(const int32_t userId, const std::string &bundleName, AssetInfoObj &assetInfoObj)); MOCK_METHOD2(DeleteAsset, int32_t(const int32_t userId, const std::string &uri)); + MOCK_METHOD0(GetSyncTimeInner, int32_t()); + MOCK_METHOD1(GetSyncStateInner, int32_t(const std::string &uri)); }; class CloudSyncServiceStubTest : public testing::Test { -- Gitee From 2af1b18506f57b4f7dc5780238e7e941cd4985fb Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Fri, 10 Nov 2023 14:07:55 +0800 Subject: [PATCH 4/6] total function Signed-off-by: yang-jingbo1985 --- .../kits/js/cloudfilesync/cloud_sync_napi.h | 4 +- .../src/ipc/cloud_sync_service.cpp | 14 ++++++ .../src/ipc/cloud_sync_service_stub.cpp | 43 +++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/cloudfilesync/cloud_sync_napi.h b/interfaces/kits/js/cloudfilesync/cloud_sync_napi.h index 6aaddae98..8a887c742 100644 --- a/interfaces/kits/js/cloudfilesync/cloud_sync_napi.h +++ b/interfaces/kits/js/cloudfilesync/cloud_sync_napi.h @@ -25,7 +25,7 @@ const int32_t ARGS_ONE = 1; class CloudSyncCallbackImpl; class CloudSyncNapi : public LibN::NExporter { public: - void SetClassName(std::string classname); + virtual void SetClassName(std::string classname); std::string GetClassName() override; bool Export() override; bool ToExport(std::vector props); @@ -41,7 +41,7 @@ public: private: static inline std::shared_ptr callback_; - std::string className_; + std::string className_ = "CloudSyncNapi"; }; class CloudSyncCallbackImpl : public CloudSyncCallback, public std::enable_shared_from_this { diff --git a/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp b/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp index e95046dd6..7905db199 100644 --- a/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp +++ b/services/cloudsyncservice/src/ipc/cloud_sync_service.cpp @@ -159,6 +159,20 @@ int32_t CloudSyncService::StartSyncInner(bool forceFlag) SyncTriggerType::APP_TRIGGER); } +int32_t CloudSyncService::GetSyncTimeInner() +{ + int32_t syncTime = 555; + LOGE("GetLasySyncTime is ok1111"); + return syncTime; +} + +int32_t CloudSyncService::GetSyncStateInner(const std::string &uri) +{ + int32_t fileSyncState = 2; + LOGE("GetSyncStateInner is ok1111"); + return fileSyncState; +} + int32_t CloudSyncService::StopSyncInner() { auto callerUserId = DfsuAccessTokenHelper::GetUserId(); diff --git a/services/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp b/services/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp index fcec7e3df..3ea307ce1 100644 --- a/services/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp +++ b/services/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp @@ -59,6 +59,10 @@ CloudSyncServiceStub::CloudSyncServiceStub() &CloudSyncServiceStub::HandleDownloadFile; opToInterfaceMap_[static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DELETE_ASSET)] = &CloudSyncServiceStub::HandleDeleteAsset; + opToInterfaceMap_[static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_GET_SYNC_TIME)] = + &CloudSyncServiceStub::HandleGetSyncTime; + opToInterfaceMap_[static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_GET_SYNC_STATE)] = + &CloudSyncServiceStub::HandleGetSyncState; } int32_t CloudSyncServiceStub::OnRemoteRequest(uint32_t code, @@ -391,4 +395,43 @@ int32_t CloudSyncServiceStub::HandleDeleteAsset(MessageParcel &data, MessageParc LOGI("End DeleteAsset"); return res; } + +int32_t CloudSyncServiceStub::HandleGetSyncTime(MessageParcel &data, MessageParcel &reply) +{ + LOGI("Begin GetSyncTime"); + if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) { + LOGE("permission denied"); + return E_PERMISSION_DENIED; + } + if (!DfsuAccessTokenHelper::IsSystemApp()) { + LOGE("caller hap is not system hap"); + return E_PERMISSION_SYSTEM; + } + + int32_t syncTime = GetSyncTimeInner(); + reply.WriteInt32(syncTime); + + LOGI("End GetSyncTime"); + return syncTime; +} + +int32_t CloudSyncServiceStub::HandleGetSyncState(MessageParcel &data, MessageParcel &reply) +{ + LOGI("Begin GetSyncState"); + if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_CLOUD_SYNC)) { + LOGE("permission denied"); + return E_PERMISSION_DENIED; + } + if (!DfsuAccessTokenHelper::IsSystemApp()) { + LOGE("caller hap is not system hap"); + return E_PERMISSION_SYSTEM; + } + + string uri = data.ReadString(); + int32_t res = GetSyncStateInner(uri); + + reply.WriteInt32(res); + LOGI("End GetSyncState"); + return res; +} } // namespace OHOS::FileManagement::CloudSync -- Gitee From b4d9d5ccd88fab59274b25562ee6be83b7378900 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Fri, 10 Nov 2023 14:59:51 +0800 Subject: [PATCH 5/6] total function Signed-off-by: yang-jingbo1985 --- interfaces/kits/js/cloudfilesync/BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interfaces/kits/js/cloudfilesync/BUILD.gn b/interfaces/kits/js/cloudfilesync/BUILD.gn index 80169b59b..46372e32e 100644 --- a/interfaces/kits/js/cloudfilesync/BUILD.gn +++ b/interfaces/kits/js/cloudfilesync/BUILD.gn @@ -23,10 +23,10 @@ ohos_shared_library("cloudsync") { } sources = [ + "cloud_file_cache_napi.cpp", "cloud_file_download_napi.cpp", - "cloud_sync_n_exporter.cpp", "cloud_file_napi.cpp", - "cloud_file_cache_napi.cpp", + "cloud_sync_n_exporter.cpp", "cloud_sync_napi.cpp", "file_sync_napi.cpp", "gallery_sync_napi.cpp", -- Gitee From 9c8e16e03175ba6b1bba129c3bbdefcae15c903d Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Fri, 10 Nov 2023 15:38:39 +0800 Subject: [PATCH 6/6] total function Signed-off-by: yang-jingbo1985 --- .../inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h index c3eafca8b..14f46f4cc 100644 --- a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h +++ b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_manager.h @@ -91,7 +91,7 @@ public: virtual int32_t StopDownloadFile(const std::string &path) = 0; virtual int32_t RegisterDownloadFileCallback(const std::shared_ptr downloadCallback) = 0; virtual int32_t UnregisterDownloadFileCallback() = 0; - virtual int32_t GetSyncTime() = 0; + virtual int32_t GetSyncTime() = 0; virtual int32_t GetSyncState(const std::string &uri) = 0; }; } // namespace OHOS::FileManagement::CloudSync -- Gitee