• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     auto env = callbackContext_->env_;
25     callbackContext_->filePath_ = taskSignal_->filePath_;
26     JSCallbackContext *callbackContext = callbackContext_.get();
27     auto task = [callbackContext] () {
28         if (callbackContext == nullptr) {
29             return;
30         }
31         if (!callbackContext->ref_) {
32             return;
33         }
34         napi_handle_scope scope = nullptr;
35         napi_status ret = napi_open_handle_scope(callbackContext->env_, &scope);
36         if (ret != napi_ok) {
37             return;
38         }
39         napi_env env = callbackContext->env_;
40         napi_value jsCallback = callbackContext->ref_.Deref(env).val_;
41         napi_value filePath = LibN::NVal::CreateUTF8String(env, callbackContext->filePath_).val_;
42         napi_value retVal = nullptr;
43         ret = napi_call_function(env, nullptr, jsCallback, 1, &filePath, &retVal);
44         if (ret != napi_ok) {
45             HILOGE("Failed to call napi_call_function, ret: %{public}d", ret);
46         }
47         ret = napi_close_handle_scope(callbackContext->env_, scope);
48         if (ret != napi_ok) {
49             HILOGE("Failed to close handle scope, ret: %{public}d", ret);
50         }
51     };
52     auto ret = napi_send_event(env, task, napi_eprio_immediate);
53     if (ret != 0) {
54         HILOGE("Failed to uv_queue_work_with_qos, ret: %{public}d", ret);
55     }
56 }
57 } // namespace OHOS::FileManagement::ModuleFileIO