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_n_exporter.h"
17
18 #include "file_utils.h"
19 #include "task_signal.h"
20 #include "task_signal_entity.h"
21
22 namespace OHOS::FileManagement::ModuleFileIO {
23 using namespace std;
24 using namespace OHOS::FileManagement::LibN;
25 using namespace DistributedFS::ModuleTaskSignal;
26 constexpr int NO_ERROR = 0;
27 constexpr int CANCEL_ERR = -3;
TaskSignalNExporter(napi_env env,napi_value exports)28 TaskSignalNExporter::TaskSignalNExporter(napi_env env, napi_value exports) : NExporter(env, exports) {}
29
~TaskSignalNExporter()30 TaskSignalNExporter::~TaskSignalNExporter() {}
31
GetClassName()32 string TaskSignalNExporter::GetClassName()
33 {
34 return TaskSignalNExporter::className_;
35 }
36
Constructor(napi_env env,napi_callback_info info)37 napi_value TaskSignalNExporter::Constructor(napi_env env, napi_callback_info info)
38 {
39 NFuncArg funcArg(env, info);
40 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
41 HILOGE("Failed to get param.");
42 NError(EINVAL).ThrowErr(env);
43 return nullptr;
44 }
45
46 auto taskSignalEntity = CreateUniquePtr<TaskSignalEntity>();
47 if (taskSignalEntity == nullptr) {
48 HILOGE("Failed to request heap memory.");
49 NError(ENOMEM).ThrowErr(env);
50 return nullptr;
51 }
52 taskSignalEntity->taskSignal_ = std::make_shared<TaskSignal>();
53 if (!NClass::SetEntityFor<TaskSignalEntity>(env, funcArg.GetThisVar(), std::move(taskSignalEntity))) {
54 HILOGE("Failed to set watcherEntity.");
55 NError(EIO).ThrowErr(env);
56 return nullptr;
57 }
58 return funcArg.GetThisVar();
59 }
60
Export()61 bool TaskSignalNExporter::Export()
62 {
63 vector<napi_property_descriptor> props = {
64 NVal::DeclareNapiFunction("cancel", Cancel),
65 NVal::DeclareNapiFunction("onCancel", OnCancel),
66 };
67 string className = GetClassName();
68
69 auto [succ, classValue] = NClass::DefineClass(exports_.env_, className_, Constructor, std::move(props));
70 if (!succ) {
71 HILOGE("Failed to DefineClass");
72 NError(EIO).ThrowErr(exports_.env_);
73 return false;
74 }
75
76 succ = NClass::SaveClass(exports_.env_, className, classValue);
77 if (!succ) {
78 HILOGE("Failed to SaveClass");
79 NError(EIO).ThrowErr(exports_.env_);
80 return false;
81 }
82 return exports_.AddProp(className, classValue);
83 }
84
Cancel(napi_env env,napi_callback_info info)85 napi_value TaskSignalNExporter::Cancel(napi_env env, napi_callback_info info)
86 {
87 NFuncArg funcArg(env, info);
88 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
89 HILOGE("Failed to get param when stop.");
90 NError(EINVAL).ThrowErr(env);
91 return nullptr;
92 }
93
94 auto taskSignalEntity = NClass::GetEntityOf<TaskSignalEntity>(env, funcArg.GetThisVar());
95 if (!taskSignalEntity || taskSignalEntity->taskSignal_ == nullptr) {
96 HILOGE("Failed to get watcherEntity when stop.");
97 NError(EINVAL).ThrowErr(env);
98 return nullptr;
99 }
100 auto ret = taskSignalEntity->taskSignal_->Cancel();
101 if (ret != NO_ERROR) {
102 HILOGE("Failed to cancel the task.");
103 NError(CANCEL_ERR).ThrowErr(env);
104 return nullptr;
105 }
106 return NVal::CreateUndefined(env).val_;
107 }
108
OnCancel(napi_env env,napi_callback_info info)109 napi_value TaskSignalNExporter::OnCancel(napi_env env, napi_callback_info info)
110 {
111 HILOGD("TaskSignal OnCancel Func Run in.");
112 NFuncArg funcArg(env, info);
113 if (!funcArg.InitArgs(NARG_CNT::ZERO)) {
114 HILOGE("Failed to get param when stop.");
115 NError(EINVAL).ThrowErr(env);
116 return nullptr;
117 }
118 auto taskSignalEntity = NClass::GetEntityOf<TaskSignalEntity>(env, funcArg.GetThisVar());
119 if (!taskSignalEntity || taskSignalEntity->taskSignal_ == nullptr) {
120 HILOGE("Failed to get watcherEntity when stop.");
121 NError(EINVAL).ThrowErr(env);
122 return nullptr;
123 }
124 taskSignalEntity->taskSignal_->SetTaskSignalListener(taskSignalEntity);
125 auto callbackContext = std::make_shared<JSCallbackContext>(NVal(env, funcArg[0]));
126 callbackContext->env_ = env;
127 taskSignalEntity->callbackContext_ = callbackContext;
128 napi_value result = nullptr;
129 napi_get_null(env, &result);
130 return result;
131 }
132 } // namespace OHOS::FileManagement::ModuleFileIO