• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     auto cbExec = [arg]() -> NError {
52         int32_t ret = CloudSyncManager::GetInstance().GetSyncTime(arg->time);
53         if (ret != E_OK) {
54             LOGE("GetLastSyncTime error, result: %{public}d", ret);
55             return NError(Convert2JsErrNum(ret));
56         }
57         return NError(ERRNO_NOERR);
58     };
59 
60     auto cbComplete = [cbArg = arg](napi_env env, NError err) -> NVal {
61         if (err) {
62             return {env, err.GetNapiErr(env)};
63         }
64         return NVal::CreateInt64(env, cbArg->time);
65     };
66 
67     const std::string procedureName = "getLastSyncTime";
68     NVal thisVar(env, funcArg.GetThisVar());
69     if (funcArg.GetArgc() == NARG_CNT::ONE) {
70         if (!NVal(env, funcArg[NARG_POS::FIRST]).TypeIs(napi_number)) {
71             NVal cb(env, funcArg[NARG_POS::FIRST]);
72             return NAsyncWorkCallback(env, thisVar, cb).Schedule(procedureName, cbExec, cbComplete).val_;
73         }
74         LOGE("Get napi_number Error");
75         NError(EINVAL).ThrowErr(env);
76         return nullptr;
77     }
78     return NAsyncWorkPromise(env, thisVar).Schedule(procedureName, cbExec, cbComplete).val_;
79 }
80 
81 struct SyncStateArg {
82     vector<int32_t> stateList {};
83 };
84 
Export()85 bool FileSyncNapi::Export()
86 {
87     std::vector<napi_property_descriptor> props = {
88         NVal::DeclareNapiFunction("on", FileSyncNapi::OnCallback),
89         NVal::DeclareNapiFunction("off", FileSyncNapi::OffCallback),
90         NVal::DeclareNapiFunction("start", FileSyncNapi::Start),
91         NVal::DeclareNapiFunction("stop", FileSyncNapi::Stop),
92         NVal::DeclareNapiFunction("getLastSyncTime", FileSyncNapi::GetLastSyncTime),
93     };
94 
95     SetClassName("FileSync");
96     return ToExport(props);
97 }
98 } // namespace OHOS::FileManagement::CloudSync