1 /*
2 * Copyright (c) 2024 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 "task_signal_entity.h"
17
18 #include <uv.h>
19 namespace OHOS::FileManagement::ModuleFileIO {
~TaskSignalEntity()20 TaskSignalEntity::~TaskSignalEntity() {}
21
OnCancel()22 void TaskSignalEntity::OnCancel()
23 {
24 uv_loop_s *loop = nullptr;
25 if (!callbackContext_) {
26 return;
27 }
28 auto env = callbackContext_->env_;
29 callbackContext_->filePath_ = taskSignal_->filePath_;
30 napi_get_uv_event_loop(env, &loop);
31 if (loop == nullptr) {
32 return;
33 }
34 uv_work_t *work = new (std::nothrow) uv_work_t;
35 if (work == nullptr) {
36 return;
37 }
38 work->data = reinterpret_cast<void *>(callbackContext_.get());
39 int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {},
40 [](uv_work_t *work, int status) {
41 JSCallbackContext *callbackContext = reinterpret_cast<JSCallbackContext *>(work->data);
42 if (callbackContext == nullptr) {
43 return;
44 }
45 if (!callbackContext->ref_) {
46 return;
47 }
48 napi_handle_scope scope = nullptr;
49 napi_status ret = napi_open_handle_scope(callbackContext->env_, &scope);
50 if (ret != napi_ok) {
51 return;
52 }
53 napi_env env = callbackContext->env_;
54 napi_value jsCallback = callbackContext->ref_.Deref(env).val_;
55 napi_value filePath = LibN::NVal::CreateUTF8String(env, callbackContext->filePath_).val_;
56 napi_value retVal = nullptr;
57 ret = napi_call_function(env, nullptr, jsCallback, 1, &filePath, &retVal);
58 if (ret != napi_ok) {
59 HILOGE("Failed to call napi_call_function, ret: %{public}d", ret);
60 }
61 ret = napi_close_handle_scope(callbackContext->env_, scope);
62 if (ret != napi_ok) {
63 HILOGE("Failed to close handle scope, ret: %{public}d", ret);
64 }
65 delete work;
66 }, uv_qos_user_initiated);
67 if (ret != 0) {
68 HILOGE("Failed to uv_queue_work_with_qos, ret: %{public}d", ret);
69 delete work;
70 }
71 }
72 } // namespace OHOS::FileManagement::ModuleFileIO