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_ani.h"
17
18 #include "ani_helper.h"
19 #include "copy_core.h"
20 #include "error_handler.h"
21 #include "filemgmt_libhilog.h"
22 #include "fs_task_signal.h"
23 #include "task_signal_listener_ani.h"
24 #include "task_signal_wrapper.h"
25 #include "type_converter.h"
26
27 namespace OHOS {
28 namespace FileManagement {
29 namespace ModuleFileIO {
30 namespace ANI {
31 using namespace std;
32 using namespace OHOS::FileManagement::ModuleFileIO;
33
Constructor(ani_env * env,ani_object signalObj)34 void TaskSignalAni::Constructor(ani_env *env, ani_object signalObj)
35 {
36 auto taskSignal = CreateSharedPtr<TaskSignal>();
37 if (taskSignal == nullptr) {
38 HILOGE("Failed to request heap memory.");
39 return;
40 }
41
42 ani_vm *vm = nullptr;
43 env->GetVM(&vm);
44 auto listener = CreateUniquePtr<TaskSignalListenerAni>(vm, signalObj, taskSignal);
45 if (listener == nullptr) {
46 HILOGE("Failed to request heap memory.");
47 return ;
48 }
49
50 FsResult<unique_ptr<FsTaskSignal>> result = FsTaskSignal::Constructor(move(taskSignal), move(listener));
51 if (!result.IsSuccess()) {
52 HILOGE("Failed to FsTaskSignal::Constructor");
53 return ;
54 }
55 TaskSignalWrapper::Wrap(env, signalObj, move(result.GetData().value()));
56 }
57
Cancel(ani_env * env,ani_object object)58 void TaskSignalAni::Cancel(ani_env *env, [[maybe_unused]] ani_object object)
59 {
60 FsTaskSignal *copySignal = TaskSignalWrapper::Unwrap(env, object);
61 if (copySignal == nullptr) {
62 HILOGE("Cannot unwrap copySignal!");
63 ErrorHandler::Throw(env, EINVAL);
64 return;
65 }
66
67 auto ret = copySignal->Cancel();
68 if (!ret.IsSuccess()) {
69 HILOGE("Cannot Cancel!");
70 const auto &err = ret.GetError();
71 ErrorHandler::Throw(env, err);
72 return;
73 }
74 }
75
OnCancel(ani_env * env,ani_object object)76 void TaskSignalAni::OnCancel(ani_env *env, [[maybe_unused]] ani_object object)
77 {
78 FsTaskSignal *copySignal = TaskSignalWrapper::Unwrap(env, object);
79 if (copySignal == nullptr) {
80 HILOGE("Cannot unwrap copySignal!");
81 ErrorHandler::Throw(env, EINVAL);
82 return;
83 }
84
85 auto ret = copySignal->OnCancel();
86 if (!ret.IsSuccess()) {
87 HILOGE("Cannot Cancel!");
88 const auto &err = ret.GetError();
89 ErrorHandler::Throw(env, err);
90 return;
91 }
92 }
93
94 } // namespace ANI
95 } // namespace ModuleFileIO
96 } // namespace FileManagement
97 } // namespace OHOS