From a5c55f62f249bbace6fc60576dbb6e1f1483606c Mon Sep 17 00:00:00 2001 From: jz <2544106453@qq.com> Date: Tue, 9 May 2023 17:46:45 +0800 Subject: [PATCH] add gallerySync Class. Signed-off-by: jz <2544106453@qq.com> --- bundle.json | 3 +- .../include/cloud_sync_manager_impl.h | 2 + .../src/cloud_sync_manager_impl.cpp | 38 +++ .../cloudsync_kit_inner/cloud_sync_callback.h | 1 + .../cloud_sync_constants.h | 18 ++ .../cloudsync_kit_inner/cloud_sync_manager.h | 13 + interfaces/kits/js/cloudfilesync/BUILD.gn | 33 ++ .../cloudfilesync/cloud_sync_n_exporter.cpp | 88 ++++++ .../js/cloudfilesync/cloud_sync_n_exporter.h | 25 ++ .../js/cloudfilesync/gallery_sync_napi.cpp | 281 ++++++++++++++++++ .../kits/js/cloudfilesync/gallery_sync_napi.h | 67 +++++ 11 files changed, 568 insertions(+), 1 deletion(-) create mode 100644 interfaces/kits/js/cloudfilesync/BUILD.gn create mode 100644 interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp create mode 100644 interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.h create mode 100644 interfaces/kits/js/cloudfilesync/gallery_sync_napi.cpp create mode 100644 interfaces/kits/js/cloudfilesync/gallery_sync_napi.h diff --git a/bundle.json b/bundle.json index a6ffd5913..9a1352afd 100644 --- a/bundle.json +++ b/bundle.json @@ -49,7 +49,8 @@ "//foundation/filemanagement/dfs_service:services_target", "//foundation/filemanagement/dfs_service:cloudsync_kit_inner_target", "//foundation/filemanagement/dfs_service:cloud_daemon_kit_inner_target", - "//foundation/filemanagement/dfs_service/interfaces/kits/js/cloudsyncmanager:cloudsyncmanager" + "//foundation/filemanagement/dfs_service/interfaces/kits/js/cloudsyncmanager:cloudsyncmanager", + "//foundation/filemanagement/dfs_service/interfaces/kits/js/cloudfilesync:cloudfilesync" ] }, "inner_kits": [ 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 fe933f222..f6d0385b0 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 @@ -29,6 +29,8 @@ class CloudSyncManagerImpl final : public CloudSyncManager, public NoCopyable { public: static CloudSyncManagerImpl &GetInstance(); + int32_t RegisterCallback(const std::shared_ptr callback) override; + int32_t StartSync() override; int32_t StartSync(bool forceFlag, const std::shared_ptr callback) override; int32_t StopSync() override; int32_t ChangeAppSwitch(const std::string &accoutId, const std::string &bundleName, bool status) override; 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 51e8261f6..4ec596de8 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 @@ -27,6 +27,44 @@ CloudSyncManagerImpl &CloudSyncManagerImpl::GetInstance() return instance; } +int32_t CloudSyncManagerImpl::RegisterCallback(const std::shared_ptr callback) +{ + if (!callback) { + LOGE("callback is null"); + return E_INVAL_ARG; + } + auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance(); + if (!CloudSyncServiceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + + if (!isFirstCall_.test_and_set()) { + LOGI("Register callback"); + auto ret = + CloudSyncServiceProxy->RegisterCallbackInner(sptr(new (std::nothrow) CloudSyncCallbackClient(callback))); + if (ret) { + LOGE("Register callback failed"); + isFirstCall_.clear(); + return ret; + } + callback_ = callback; + SetDeathRecipient(CloudSyncServiceProxy->AsObject()); + } + return E_OK; +} + +int32_t CloudSyncManagerImpl::StartSync() +{ + bool forceFlag = false; + auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance(); + if (!CloudSyncServiceProxy) { + LOGE("proxy is null"); + return E_SA_LOAD_FAILED; + } + return CloudSyncServiceProxy->StartSyncInner(forceFlag); +} + int32_t CloudSyncManagerImpl::StartSync(bool forceFlag, const std::shared_ptr callback) { if (!callback) { diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_callback.h b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_callback.h index fb0132356..7834474a8 100644 --- a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_callback.h +++ b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_callback.h @@ -39,6 +39,7 @@ public: * SYNC_STATE_PAUSED_FOR_SPACE_TOO_LOW 云空间满 */ virtual void OnSyncStateChanged(SyncType type, SyncPromptState state) = 0; + virtual void OnCloudSyncStateChanged(CloudSyncState state, ErrorType type) {}; }; } // namespace OHOS::FileManagement::CloudSync diff --git a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_constants.h b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_constants.h index 000d67451..8e4bb71f1 100644 --- a/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_constants.h +++ b/interfaces/inner_api/native/cloudsync_kit_inner/cloud_sync_constants.h @@ -34,6 +34,24 @@ enum class SyncPromptState : int32_t { SYNC_STATE_PAUSED_FOR_SPACE_TOO_LOW, }; +enum CloudSyncState { + UPLOADING = 0, + UPLOAD_FAILED, + DOWNLOADING, + DOWNLOAD_FAILED, + COMPLETED, + STOPPED, +}; + +enum ErrorType { + NETWORK_UNAVAILABLE = 0, + WIFI_UNAVAILABLE, + BATTERY_LEVEL_LOW, + BATTERY_LEVEL_WARNING, + CLOUD_STORAGE_FULL, + LOCAL_STORAGE_FULL, +}; + } // namespace OHOS::FileManagement::CloudSync #endif // OHOS_FILEMGMT_CLOUD_SYNC_CONSTANTS_H \ No newline at end of file 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 cd4959ca9..b335e929f 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 @@ -24,6 +24,19 @@ namespace OHOS::FileManagement::CloudSync { class CloudSyncManager { public: static CloudSyncManager &GetInstance(); + /** + * @brief 注册 + * + * @param callback 注册同步回调 + * @return int32_t 同步返回执行结果 + */ + virtual int32_t RegisterCallback(const std::shared_ptr callback) = 0; + /** + * @brief 启动同步 + * + * @return int32_t 同步返回执行结果 + */ + virtual int32_t StartSync() = 0; /** * @brief 启动同步 * diff --git a/interfaces/kits/js/cloudfilesync/BUILD.gn b/interfaces/kits/js/cloudfilesync/BUILD.gn new file mode 100644 index 000000000..b26233c60 --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/BUILD.gn @@ -0,0 +1,33 @@ +# 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. + +import("//build/ohos.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") + +ohos_shared_library("cloudfilesync") { + sources = [ + "cloud_sync_n_exporter.cpp", + "gallery_sync_napi.cpp", + ] + deps = [ + "${innerkits_native_path}/cloudsync_kit_inner:cloudsync_kit_inner", + "${utils_path}:libdistributedfileutils", + ] + + external_deps = [ "file_api:filemgmt_libn" ] + + relative_install_dir = "module/file" + + part_name = "dfs_service" + subsystem_name = "filemanagement" +} diff --git a/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp b/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp new file mode 100644 index 000000000..ff73344cb --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.cpp @@ -0,0 +1,88 @@ +/* + * 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 "gallery_sync_napi.h" +#include "cloud_sync_n_exporter.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace FileManagement::LibN; +using namespace std; +/*********************************************** + * Module export and register + ***********************************************/ +napi_value CloudSyncExport(napi_env env, napi_value exports) +{ + InitCloudSyncState(env, exports); + InitErrorType(env, exports); + + std::vector> products; + products.emplace_back(std::make_unique(env, exports)); + for (auto &&product : products) { + if (!product->Export()) { + return nullptr; + } + } + return exports; +} + +void InitCloudSyncState(napi_env env, napi_value exports) +{ + char propertyName[] = "SyncState"; + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("UPLOADING", NVal::CreateInt32(env, UPLOADING).val_), + DECLARE_NAPI_STATIC_PROPERTY("UPLOAD_FAILED", NVal::CreateInt32(env, UPLOAD_FAILED).val_), + DECLARE_NAPI_STATIC_PROPERTY("DOWNLOADING", NVal::CreateInt32(env, DOWNLOADING).val_), + DECLARE_NAPI_STATIC_PROPERTY("DOWNLOAD_FAILED", NVal::CreateInt32(env, DOWNLOAD_FAILED).val_), + DECLARE_NAPI_STATIC_PROPERTY("COMPLETED", NVal::CreateInt32(env, COMPLETED).val_), + DECLARE_NAPI_STATIC_PROPERTY("STOPPED", NVal::CreateInt32(env, 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 "; + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("NETWORK_UNAVAILABLE", NVal::CreateInt32(env, NETWORK_UNAVAILABLE).val_), + DECLARE_NAPI_STATIC_PROPERTY("WIFI_UNAVAILABLE", NVal::CreateInt32(env, WIFI_UNAVAILABLE).val_), + DECLARE_NAPI_STATIC_PROPERTY("BATTERY_LEVEL_LOW", NVal::CreateInt32(env, BATTERY_LEVEL_LOW).val_), + DECLARE_NAPI_STATIC_PROPERTY("BATTERY_LEVEL_WARNING", NVal::CreateInt32(env, BATTERY_LEVEL_WARNING).val_), + DECLARE_NAPI_STATIC_PROPERTY("CLOUD_STORAGE_FULL", NVal::CreateInt32(env, CLOUD_STORAGE_FULL).val_), + DECLARE_NAPI_STATIC_PROPERTY("LOCAL_STORAGE_FULL", NVal::CreateInt32(env, LOCAL_STORAGE_FULL).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); +} + +static napi_module _module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = CloudSyncExport, + .nm_modname = "file.cloudSync", + .nm_priv = ((void *)0), + .reserved = {0} +}; + +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module); +} +} // namespace OHOS::FileManagement::CloudSync diff --git a/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.h b/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.h new file mode 100644 index 000000000..6e72b7d70 --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/cloud_sync_n_exporter.h @@ -0,0 +1,25 @@ +/* + * 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_N_EXPORTER_H +#define OHOS_FILEMGMT_CLOUD_SYNC_N_EXPORTER_H + +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::CloudSync { + void InitCloudSyncState(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/gallery_sync_napi.cpp b/interfaces/kits/js/cloudfilesync/gallery_sync_napi.cpp new file mode 100644 index 000000000..203ee74ef --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/gallery_sync_napi.cpp @@ -0,0 +1,281 @@ +/* + * 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 "gallery_sync_napi.h" + +#include + +#include "cloud_sync_manager.h" +#include "dfs_error.h" +#include "filemgmt_libhilog.h" +#include "utils_log.h" +#include "uv.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace std; + +napi_ref cbOnRef = nullptr; + +struct SyncProgressEntity { + CloudSyncState state; + ErrorType type; +}; + +void CloudSyncCallbackImpl::OnCloudSyncStateChanged(CloudSyncState state, ErrorType type) +{ + HILOGE("jz------OnCloudSyncStateChanged start"); + 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) { + return; + } + + cbOnRef_ = cbOnRef; + UvChangeMsg *msg = new (std::nothrow) UvChangeMsg(env_, cbOnRef, state, type); + if (msg == nullptr) { + delete work; + return; + } + + work->data = reinterpret_cast(msg); + int ret = uv_queue_work(loop, work, [](uv_work_t *w) {}, [](uv_work_t *w, int s) { + // js thread + if (w == nullptr) { + return; + } + UvChangeMsg *msg = reinterpret_cast(w->data); + do { + if (msg == nullptr) { + HILOGE("UvChangeMsg is null"); + break; + } + napi_env env = msg->env_; + napi_value jsCallback = nullptr; + napi_status status = napi_get_reference_value(env, msg->ref_, &jsCallback); + if (status != napi_ok) { + HILOGE("jz------OnCloudSyncStateChanged napi_get_reference_value error"); + break; + } + napi_value result[ARGS_TWO] = { nullptr }; + result[PARAM0] = NVal::CreateInt32(env, (int32_t)msg->state_).val_; + result[PARAM1] = NVal::CreateInt32(env, (int32_t)msg->type_).val_; + napi_value retVal = nullptr; + napi_call_function(env, nullptr, jsCallback, ARGS_TWO, result, &retVal); + if (status != napi_ok) { + HILOGE("jz------OnCloudSyncStateChanged napi_call_function error"); + break; + } + } while (0); + delete w; + }); + if (ret != 0) { + HILOGE("jz------Failed to execute libuv work queue, ret: %{public}d", ret); + delete work; + } +} + +void CloudSyncCallbackImpl::OnSyncStateChanged(SyncType type, SyncPromptState state) +{ + return; +} + +napi_value GallerySyncNapi::Constructor(napi_env env, napi_callback_info info) +{ + HILOGI("jz------Constructor Start"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto syncProgressEntity = make_unique(); + if (syncProgressEntity == nullptr) { + NError(EIO).ThrowErr(env); + return nullptr; + } + + if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(syncProgressEntity))) { + NError(EIO).ThrowErr(env); + return nullptr; + } + + return funcArg.GetThisVar(); +} + +napi_value GallerySyncNapi::OnCallback(napi_env env, napi_callback_info info) +{ + HILOGI("jz------OnCallback Start"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + HILOGE("jz------OnCallback Number of arguments unmatched"); + return nullptr; + } + + auto [succ, type, ignore] = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + const int32_t refCount = 1; + napi_create_reference(env, funcArg[(int)NARG_POS::SECOND], refCount, &cbOnRef); + + shared_ptr callback = make_shared(env, cbOnRef); + int32_t result = CloudSyncManager::GetInstance().RegisterCallback(callback); + if (result != E_OK) { + HILOGE("jz------OnCallback error"); + return nullptr; + } + HILOGI("jz------OnCallback end"); + return NVal::CreateBool(env, true).val_; +} + +napi_value GallerySyncNapi::OffCallback(napi_env env, napi_callback_info info) +{ + HILOGI("jz------OffCallback Start"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + NError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [succ, type, ignore] = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String(); + if (!succ) { + NError(E_PARAMS).ThrowErr(env); + return nullptr; + } + + cbOnRef = nullptr; + shared_ptr callback = make_shared(env, cbOnRef); + int32_t result = CloudSyncManager::GetInstance().RegisterCallback(callback); + if (result != E_OK) { + HILOGE("jz------OffCallback error"); + return nullptr; + } + HILOGI("jz------OffCallback end"); + return NVal::CreateBool(env, true).val_; +} + +napi_value GallerySyncNapi::Start(napi_env env, napi_callback_info info) +{ + HILOGI("jz------Start Start"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + NError(EINVAL).ThrowErr(env); + } + + auto cbExec = []() -> NError { + int32_t result = CloudSyncManager::GetInstance().StartSync(); + if (result != E_OK) { + HILOGE("jz------Start StartSync error"); + return NError(result); + } + 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"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; + } + HILOGI("jz------Start end"); +} + +napi_value GallerySyncNapi::Stop(napi_env env, napi_callback_info info) +{ + HILOGI("jz------Stop Start"); + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto cbExec = []() -> NError { + int32_t result = CloudSyncManager::GetInstance().StopSync(); + if (result != E_OK) { + HILOGE("jz------Stop StopSync error"); + return NError(result); + } + 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"; + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::ZERO) { + return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FIRST]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_; + } + HILOGI("jz------Stop end"); +} + +std::string GallerySyncNapi::GetClassName() +{ + return GallerySyncNapi::className_; +} + +bool GallerySyncNapi::Export() +{ + HILOGI("jz------Export Start"); + std::vector props = { + NVal::DeclareNapiFunction("on", OnCallback), + NVal::DeclareNapiFunction("off", OffCallback), + NVal::DeclareNapiFunction("start", Start), + NVal::DeclareNapiFunction("stop", Stop), + }; + + std::string className = GetClassName(); + bool succ = false; + napi_value classValue = nullptr; + std::tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, + GallerySyncNapi::Constructor, std::move(props)); + if (!succ) { + NError(E_GETRESULT).ThrowErr(exports_.env_); + return false; + } + + succ = NClass::SaveClass(exports_.env_, className, classValue); + if (!succ) { + NError(E_GETRESULT).ThrowErr(exports_.env_); + return false; + } + + return exports_.AddProp(className, classValue); +} +} // namespace OHOS::FileManagement::CloudSync diff --git a/interfaces/kits/js/cloudfilesync/gallery_sync_napi.h b/interfaces/kits/js/cloudfilesync/gallery_sync_napi.h new file mode 100644 index 000000000..72ea97c10 --- /dev/null +++ b/interfaces/kits/js/cloudfilesync/gallery_sync_napi.h @@ -0,0 +1,67 @@ +/* + * 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_GALLERY_SYNC_NAPI_H +#define OHOS_FILEMGMT_GALLERY_SYNC_NAPI_H + +#include "cloud_sync_callback.h" +#include "filemgmt_libn.h" + +namespace OHOS::FileManagement::CloudSync { +using namespace FileManagement::LibN; + +const int32_t ARGS_TWO = 2; +const int32_t PARAM0 = 0; +const int32_t PARAM1 = 1; + +class GallerySyncNapi final : public NExporter { +public: + inline static const std::string className_ = "GallerySync"; + + GallerySyncNapi(napi_env env, napi_value exports) : NExporter(env, exports) {}; + ~GallerySyncNapi() = default; + + static napi_value Constructor(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); + static napi_value Start(napi_env env, napi_callback_info info); + static napi_value Stop(napi_env env, napi_callback_info info); + + bool Export() override; + std::string GetClassName() override; +}; + +class CloudSyncCallbackImpl : public CloudSyncCallback { +public: + CloudSyncCallbackImpl(napi_env env, napi_ref Ref) : env_(env), cbOnRef_(Ref) {}; + void OnSyncStateChanged(SyncType type, SyncPromptState state) override; + void OnCloudSyncStateChanged(CloudSyncState state, ErrorType type) override; + + class UvChangeMsg { + public: + UvChangeMsg(napi_env env, napi_ref ref, CloudSyncState state, ErrorType type) + : env_(env), ref_(ref), state_(state), type_(type) {} + ~UvChangeMsg() {} + napi_env env_; + napi_ref ref_; + CloudSyncState state_; + ErrorType type_; + }; +private: + napi_env env_; + napi_ref cbOnRef_; +}; +} // namespace OHOS::FileManagement::CloudSync +#endif // OHOS_FILEMGMT_GALLERY_SYNC_NAPI_H -- Gitee