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 "cloud_file_download_napi.h"
17
18 #include <sys/types.h>
19
20 #include "cloud_sync_manager.h"
21 #include "cloud_file_napi.h"
22 #include "dfs_error.h"
23 #include "utils_log.h"
24 #include "async_work.h"
25 #include "uv.h"
26
27 namespace OHOS::FileManagement::CloudSync {
28 using namespace FileManagement::LibN;
29 using namespace std;
30 const int32_t ARGS_ONE = 1;
31
CloudFileNapi(napi_env env,napi_value exports)32 CloudFileNapi::CloudFileNapi(napi_env env, napi_value exports) : NExporter(env, exports) {}
33
Constructor(napi_env env,napi_callback_info info)34 napi_value CloudFileNapi::Constructor(napi_env env, napi_callback_info info)
35 {
36 LOGI("CloudFileNapi::Constructor begin");
37 NFuncArg funcArg(env, info);
38 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
39 LOGE("Start Number of arguments unmatched");
40 NError(E_PARAMS).ThrowErr(env);
41 return nullptr;
42 }
43
44 LOGI("CloudFileNapi::Constructor end");
45 return funcArg.GetThisVar();
46 }
47
Start(napi_env env,napi_callback_info info)48 napi_value CloudFileNapi::Start(napi_env env, napi_callback_info info)
49 {
50 LOGI("Start begin");
51 NFuncArg funcArg(env, info);
52 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
53 LOGE("Start Number of arguments unmatched");
54 NError(E_PARAMS).ThrowErr(env);
55 return nullptr;
56 }
57 auto [succUri, uri, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
58 if (!succUri) {
59 LOGE("Start get uri parameter failed!");
60 NError(E_PARAMS).ThrowErr(env);
61 return nullptr;
62 }
63
64 auto cbExec = [uri = string(uri.get()), env = env]() -> NError {
65 int32_t ret = CloudSyncManager::GetInstance().StartDownloadFile(uri);
66 if (ret != E_OK) {
67 LOGE("Start Download failed! ret = %{public}d", ret);
68 return NError(Convert2JsErrNum(ret));
69 }
70 LOGI("Start Download Success!");
71 return NError(ERRNO_NOERR);
72 };
73
74 auto cbCompl = [](napi_env env, NError err) -> NVal {
75 if (err) {
76 return {env, err.GetNapiErr(env)};
77 }
78 return NVal::CreateUndefined(env);
79 };
80
81 string procedureName = "cloudFileDownload";
82 auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast<size_t>(NARG_CNT::TWO));
83 return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbCompl).val_;
84 }
85
CloudDownloadCallbackImpl(napi_env env,napi_value fun)86 CloudDownloadCallbackImpl::CloudDownloadCallbackImpl(napi_env env, napi_value fun) : env_(env)
87 {
88 if (fun != nullptr) {
89 napi_create_reference(env_, fun, 1, &cbOnRef_);
90 }
91 }
92
OnComplete(UvChangeMsg * msg)93 void CloudDownloadCallbackImpl::OnComplete(UvChangeMsg *msg)
94 {
95 auto downloadcCallback = msg->CloudDownloadCallback_.lock();
96 if (downloadcCallback == nullptr || downloadcCallback->cbOnRef_ == nullptr) {
97 LOGE("downloadcCallback->cbOnRef_ is nullptr");
98 return;
99 }
100 auto env = downloadcCallback->env_;
101 auto ref = downloadcCallback->cbOnRef_;
102 napi_handle_scope scope = nullptr;
103 napi_open_handle_scope(env, &scope);
104 napi_value jsCallback = nullptr;
105 napi_status status = napi_get_reference_value(env, ref, &jsCallback);
106 if (status != napi_ok) {
107 LOGE("Create reference failed, status: %{public}d", status);
108 napi_close_handle_scope(env, scope);
109 return;
110 }
111 NVal obj = NVal::CreateObject(env);
112 obj.AddProp("state", NVal::CreateInt32(env, (int32_t)msg->downloadProgress_.state).val_);
113 obj.AddProp("processed", NVal::CreateInt64(env, (int64_t)msg->downloadProgress_.downloadedSize).val_);
114 obj.AddProp("size", NVal::CreateInt64(env, (int64_t)msg->downloadProgress_.totalSize).val_);
115 obj.AddProp("uri", NVal::CreateUTF8String(env, msg->downloadProgress_.path).val_);
116 obj.AddProp("downloadErrorType", NVal::CreateInt32(env, (int32_t)msg->downloadProgress_.downloadErrorType).val_);
117 napi_value retVal = nullptr;
118 napi_value global = nullptr;
119 napi_get_global(env, &global);
120 status = napi_call_function(env, global, jsCallback, ARGS_ONE, &(obj.val_), &retVal);
121 if (status != napi_ok) {
122 LOGE("napi call function failed, status: %{public}d", status);
123 }
124 napi_close_handle_scope(env, scope);
125 }
126
OnDownloadProcess(const DownloadProgressObj & progress)127 void CloudDownloadCallbackImpl::OnDownloadProcess(const DownloadProgressObj &progress)
128 {
129 uv_loop_s *loop = nullptr;
130 napi_get_uv_event_loop(env_, &loop);
131 if (loop == nullptr) {
132 return;
133 }
134
135 uv_work_t *work = new (nothrow) uv_work_t;
136 if (work == nullptr) {
137 LOGE("Failed to create uv work");
138 return;
139 }
140
141 UvChangeMsg *msg = new (std::nothrow) UvChangeMsg(shared_from_this(), progress);
142 if (msg == nullptr) {
143 delete work;
144 return;
145 }
146 work->data = reinterpret_cast<void *>(msg);
147 int ret = uv_queue_work(
148 loop, work, [](uv_work_t *work) {},
149 [](uv_work_t *work, int status) {
150 auto msg = reinterpret_cast<UvChangeMsg *>(work->data);
151 OnComplete(msg);
152 delete msg;
153 delete work;
154 });
155 if (ret != 0) {
156 LOGE("Failed to execute libuv work queue, ret: %{public}d", ret);
157 delete msg;
158 delete work;
159 }
160 }
161
DeleteReference()162 void CloudDownloadCallbackImpl::DeleteReference()
163 {
164 if (cbOnRef_ != nullptr) {
165 napi_delete_reference(env_, cbOnRef_);
166 cbOnRef_ = nullptr;
167 }
168 }
169
On(napi_env env,napi_callback_info info)170 napi_value CloudFileNapi::On(napi_env env, napi_callback_info info)
171 {
172 LOGI("On begin");
173 NFuncArg funcArg(env, info);
174 if (!funcArg.InitArgs(NARG_CNT::TWO)) {
175 LOGE("On Number of arguments unmatched");
176 NError(E_PARAMS).ThrowErr(env);
177 return nullptr;
178 }
179 auto [succProgress, progress, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
180 if (!(succProgress && std::string(progress.get()) == "progress")) {
181 LOGE("On get progress failed!");
182 NError(E_PARAMS).ThrowErr(env);
183 return nullptr;
184 }
185
186 if (!NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) {
187 LOGE("Argument type mismatch");
188 NError(E_PARAMS).ThrowErr(env);
189 return nullptr;
190 }
191
192 if (callback_ != nullptr) {
193 LOGI("callback already exist");
194 return NVal::CreateUndefined(env).val_;
195 }
196
197 callback_ = make_shared<CloudDownloadCallbackImpl>(env, NVal(env, funcArg[(int)NARG_POS::SECOND]).val_);
198 int32_t ret = CloudSyncManager::GetInstance().RegisterDownloadFileCallback(callback_);
199 if (ret != E_OK) {
200 LOGE("RegisterDownloadFileCallback error, ret: %{public}d", ret);
201 NError(Convert2JsErrNum(ret)).ThrowErr(env);
202 return nullptr;
203 }
204
205 return NVal::CreateUndefined(env).val_;
206 }
207
Off(napi_env env,napi_callback_info info)208 napi_value CloudFileNapi::Off(napi_env env, napi_callback_info info)
209 {
210 LOGI("Off begin");
211 NFuncArg funcArg(env, info);
212 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
213 LOGE("Off Number of arguments unmatched");
214 NError(E_PARAMS).ThrowErr(env);
215 return nullptr;
216 }
217 auto [succProgress, progress, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
218 if (!(succProgress && std::string(progress.get()) == "progress")) {
219 LOGE("Off get progress failed!");
220 NError(E_PARAMS).ThrowErr(env);
221 return nullptr;
222 }
223
224 if (funcArg.GetArgc() == (uint)NARG_CNT::TWO &&!NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_function)) {
225 LOGE("Argument type mismatch");
226 NError(E_PARAMS).ThrowErr(env);
227 return nullptr;
228 }
229
230 int32_t ret = CloudSyncManager::GetInstance().UnregisterDownloadFileCallback();
231 if (ret != E_OK) {
232 LOGE("UnregisterDownloadFileCallback error, ret: %{public}d", ret);
233 NError(Convert2JsErrNum(ret)).ThrowErr(env);
234 return nullptr;
235 }
236 if (callback_ != nullptr) {
237 /* napi delete reference */
238 callback_->DeleteReference();
239 callback_ = nullptr;
240 }
241 return NVal::CreateUndefined(env).val_;
242 }
243
Stop(napi_env env,napi_callback_info info)244 napi_value CloudFileNapi::Stop(napi_env env, napi_callback_info info)
245 {
246 LOGI("Stop begin");
247 NFuncArg funcArg(env, info);
248 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
249 LOGE("Stop Number of arguments unmatched");
250 NError(E_PARAMS).ThrowErr(env);
251 return nullptr;
252 }
253 auto [succUri, uri, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
254 if (!succUri) {
255 LOGE("Stop get uri parameter failed!");
256 NError(E_PARAMS).ThrowErr(env);
257 return nullptr;
258 }
259
260 auto cbExec = [uri = string(uri.get()), env = env]() -> NError {
261 int32_t ret = CloudSyncManager::GetInstance().StopDownloadFile(uri);
262 if (ret != E_OK) {
263 LOGE("Stop Download failed! ret = %{public}d", ret);
264 return NError(Convert2JsErrNum(ret));
265 }
266 LOGI("Stop Download Success!");
267 return NError(ERRNO_NOERR);
268 };
269
270 auto cbCompl = [](napi_env env, NError err) -> NVal {
271 if (err) {
272 return {env, err.GetNapiErr(env)};
273 }
274 return NVal::CreateUndefined(env);
275 };
276
277 string procedureName = "cloudFileDownload";
278 auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast<size_t>(NARG_CNT::TWO));
279 return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbCompl).val_;
280 }
281
ToExport(std::vector<napi_property_descriptor> props)282 bool CloudFileNapi::ToExport(std::vector<napi_property_descriptor> props)
283 {
284 std::string className = GetClassName();
285 auto [succ, classValue] =
286 NClass::DefineClass(exports_.env_, className, Constructor, std::move(props));
287 if (!succ) {
288 NError(E_GETRESULT).ThrowErr(exports_.env_);
289 LOGE("Failed to define GallerySync class");
290 return false;
291 }
292
293 succ = NClass::SaveClass(exports_.env_, className, classValue);
294 if (!succ) {
295 NError(E_GETRESULT).ThrowErr(exports_.env_);
296 LOGE("Failed to save GallerySync class");
297 return false;
298 }
299
300 return exports_.AddProp(className, classValue);
301 }
302
Export()303 bool CloudFileNapi::Export()
304 {
305 vector<napi_property_descriptor> props = {
306 NVal::DeclareNapiFunction("start", CloudFileNapi::Start),
307 NVal::DeclareNapiFunction("on", CloudFileNapi::On),
308 NVal::DeclareNapiFunction("off", CloudFileNapi::Off),
309 NVal::DeclareNapiFunction("stop", CloudFileNapi::Stop),
310 };
311 return ToExport(props);
312 }
313
SetClassName(std::string classname)314 void CloudFileNapi::SetClassName(std::string classname)
315 {
316 className_ = classname;
317 }
318
GetClassName()319 string CloudFileNapi::GetClassName()
320 {
321 return className_;
322 }
323 } // namespace OHOS::FileManagement::CloudSync