1 /*
2 * Copyright (c) 2025 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_listener_ani.h"
17
18 #include <memory>
19 #include "ani_helper.h"
20 #include "ani_signature.h"
21 #include "file_utils.h"
22 #include "filemgmt_libhilog.h"
23 #include "type_converter.h"
24
25 namespace OHOS::FileManagement::ModuleFileIO::ANI {
26 using namespace std;
27 using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature;
28
CreateGlobalReference()29 bool TaskSignalListenerAni::CreateGlobalReference()
30 {
31 if (signalRef) {
32 return true;
33 }
34 ani_env *env = AniHelper::GetThreadEnv(vm);
35 if (env == nullptr) {
36 return false;
37 }
38 int ret = 0;
39 if ((ret = env->GlobalReference_Create(static_cast<ani_ref>(signalObj), &signalRef)) != ANI_OK) {
40 HILOGE("TaskSignalListenerAni GlobalReference_Create failed: %{public}d", ret);
41 signalRef = nullptr;
42 return false;
43 }
44 return true;
45 }
46
OnCancel()47 void TaskSignalListenerAni::OnCancel()
48 {
49 auto filepath = taskSignal->filePath_;
50 auto task = [this, filepath]() { SendCancelEvent(filepath); };
51 AniHelper::SendEventToMainThread(task);
52 }
53
SendCancelEvent(const string & filepath) const54 void TaskSignalListenerAni::SendCancelEvent(const string &filepath) const
55 {
56 if (vm == nullptr) {
57 HILOGE("Cannot send cancel event because the vm is null.");
58 return;
59 }
60 if (signalRef == nullptr) {
61 HILOGE("Cannot send cancel event because the signalRef is null.");
62 return;
63 }
64
65 ani_env *env = AniHelper::GetThreadEnv(vm);
66 if (env == nullptr) {
67 HILOGE("Cannot send cancel event because the env is null.");
68 return;
69 }
70 auto [succ, aniPath] = TypeConverter::ToAniString(env, filepath);
71 if (!succ) {
72 HILOGE("Cannot convert filepath to ani string!");
73 return;
74 }
75
76 auto ret = env->Object_CallMethodByName_Void(static_cast<ani_object>(signalRef), "onCancelCallback", nullptr,
77 aniPath);
78 if (ret != ANI_OK) {
79 HILOGE("Call onCancelCallback failed, err: %{public}d", ret);
80 return;
81 }
82 }
83
~TaskSignalListenerAni()84 TaskSignalListenerAni::~TaskSignalListenerAni()
85 {
86 if (signalRef == nullptr) {
87 return;
88 }
89 ani_env *env = AniHelper::GetThreadEnv(vm);
90 if (env == nullptr) {
91 HILOGE("~TaskSignalListenerAni env is nullptr");
92 return;
93 }
94 int ret = 0;
95 if ((ret = env->GlobalReference_Delete(signalRef)) != ANI_OK) {
96 HILOGE("TaskSignalListenerAni GlobalReference_Delete: %{public}d", ret);
97 }
98 }
99
100 } // namespace OHOS::FileManagement::ModuleFileIO::ANI
101