1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "file_sync_napi.h"
17 
18 #include <memory>
19 #include <sys/types.h>
20 
21 #include "async_work.h"
22 #include "cloud_sync_manager.h"
23 #include "dfs_error.h"
24 #include "utils_log.h"
25 #include "uv.h"
26 
27 namespace OHOS::FileManagement::CloudSync {
28 using namespace FileManagement::LibN;
29 using namespace std;
30 
31 struct SyncTimeArg {
32     int64_t time = 0;
33 };
34 
GetLastSyncTime(napi_env env,napi_callback_info info)35 napi_value FileSyncNapi::GetLastSyncTime(napi_env env, napi_callback_info info)
36 {
37     LOGI("GetLastSyncTime Start");
38     NFuncArg funcArg(env, info);
39     if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
40         NError(E_PARAMS).ThrowErr(env);
41         return nullptr;
42     }
43 
44     auto arg = make_shared<SyncTimeArg>();
45     if (arg == nullptr) {
46         HILOGE("Failed to request heap memory.");
47         NError(ENOMEM).ThrowErr(env);
48         return nullptr;
49     }
50 
51     string bundleName = GetBundleName(env, funcArg);
52     auto cbExec = [arg, bundleName]() -> NError {
53         int32_t ret = CloudSyncManager::GetInstance().GetSyncTime(arg->time, bundleName);
54         if (ret != E_OK) {
55             LOGE("GetLastSyncTime error, result: %{public}d", ret);
56             return NError(Convert2JsErrNum(ret));
57         }
58         return NError(ERRNO_NOERR);
59     };
60 
61     auto cbComplete = [cbArg = arg](napi_env env, NError err) -> NVal {
62         if (err) {
63             return {env, err.GetNapiErr(env)};
64         }
65         return NVal::CreateInt64(env, cbArg->time);
66     };
67 
68     const std::string procedureName = "getLastSyncTime";
69     NVal thisVar(env, funcArg.GetThisVar());
70     if (funcArg.GetArgc() == NARG_CNT::ONE) {
71         if (!NVal(env, funcArg[NARG_POS::FIRST]).TypeIs(napi_number)) {
72             NVal cb(env, funcArg[NARG_POS::FIRST]);
73             return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
74         }
75         LOGE("Get napi_number Error");
76         NError(EINVAL).ThrowErr(env);
77         return nullptr;
78     }
79     return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
80 }
81 
82 struct SyncStateArg {
83     vector<int32_t> stateList {};
84 };
85 
Export()86 bool FileSyncNapi::Export()
87 {
88     std::vector<napi_property_descriptor> props = {
89         NVal::DeclareNapiFunction("on", FileSyncNapi::OnCallback),
90         NVal::DeclareNapiFunction("off", FileSyncNapi::OffCallback),
91         NVal::DeclareNapiFunction("start", FileSyncNapi::Start),
92         NVal::DeclareNapiFunction("stop", FileSyncNapi::Stop),
93         NVal::DeclareNapiFunction("getLastSyncTime", FileSyncNapi::GetLastSyncTime),
94     };
95 
96     SetClassName("FileSync");
97     return ToExport(props);
98 }
99 } // namespace OHOS::FileManagement::CloudSync