1 /*
2 * Copyright (c) 2023 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 "system_defined_form_napi.h"
17
18 #include "napi_data_utils.h"
19 #include "napi_error_utils.h"
20 #include "napi_queue.h"
21 #include "system_defined_form.h"
22 #include "system_defined_record_napi.h"
23 #include "unified_record_napi.h"
24
25 namespace OHOS {
26 namespace UDMF {
Constructor(napi_env env)27 napi_value SystemDefinedFormNapi::Constructor(napi_env env)
28 {
29 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
30 napi_property_descriptor properties[] = {
31 /* SystemDefinedForm extends UnifiedRecord */
32 DECLARE_NAPI_FUNCTION("getType", UnifiedRecordNapi::GetType),
33 /* SystemDefinedForm extends SystemDefinedRecord */
34 DECLARE_NAPI_GETTER_SETTER("details", SystemDefinedRecordNapi::GetDetails, SystemDefinedRecordNapi::SetDetails),
35 /* SystemDefinedForm properties */
36 DECLARE_NAPI_GETTER_SETTER("formId", GetFormId, SetFormId),
37 DECLARE_NAPI_GETTER_SETTER("formName", GetFormName, SetFormName),
38 DECLARE_NAPI_GETTER_SETTER("bundleName", GetBundleName, SetBundleName),
39 DECLARE_NAPI_GETTER_SETTER("abilityName", GetAbilityName, SetAbilityName),
40 DECLARE_NAPI_GETTER_SETTER("module", GetModule, SetModule),
41 };
42 size_t count = sizeof(properties) / sizeof(properties[0]);
43 return NapiDataUtils::DefineClass(env, "SDForm", properties, count, SystemDefinedFormNapi::New);
44 }
45
New(napi_env env,napi_callback_info info)46 napi_value SystemDefinedFormNapi::New(napi_env env, napi_callback_info info)
47 {
48 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
49 auto ctxt = std::make_shared<ContextBase>();
50 ctxt->GetCbInfoSync(env, info);
51 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
52
53 auto *sdForm = new (std::nothrow) SystemDefinedFormNapi();
54 ASSERT_ERR(ctxt->env, sdForm != nullptr, Status::E_UNKNOWN, "no memory for system defined form!");
55 sdForm->value_ = std::make_shared<SystemDefinedForm>();
56 ASSERT_CALL(env, napi_wrap(env, ctxt->self, sdForm, Destructor, nullptr, nullptr), sdForm);
57 return ctxt->self;
58 }
59
NewInstance(napi_env env,std::shared_ptr<UnifiedRecord> in,napi_value & out)60 void SystemDefinedFormNapi::NewInstance(napi_env env, std::shared_ptr<UnifiedRecord> in, napi_value &out)
61 {
62 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
63 ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
64 auto *sdForm = new (std::nothrow) SystemDefinedFormNapi();
65 ASSERT_ERR_VOID(env, sdForm != nullptr, Status::E_UNKNOWN, "no memory for system defined form!");
66 sdForm->value_ = std::static_pointer_cast<SystemDefinedForm>(in);
67 ASSERT_CALL_DELETE(env, napi_wrap(env, out, sdForm, Destructor, nullptr, nullptr), sdForm);
68 }
69
Destructor(napi_env env,void * data,void * hint)70 void SystemDefinedFormNapi::Destructor(napi_env env, void *data, void *hint)
71 {
72 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi finalize.");
73 auto *sdForm = static_cast<SystemDefinedFormNapi *>(data);
74 ASSERT_VOID(sdForm != nullptr, "finalize null!");
75 delete sdForm;
76 }
77
GetSystemDefinedForm(napi_env env,napi_callback_info info,std::shared_ptr<ContextBase> ctxt)78 SystemDefinedFormNapi *SystemDefinedFormNapi::GetSystemDefinedForm(
79 napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
80 {
81 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
82 ctxt->GetCbInfoSync(env, info);
83 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
84 return static_cast<SystemDefinedFormNapi *>(ctxt->native);
85 }
86
GetFormId(napi_env env,napi_callback_info info)87 napi_value SystemDefinedFormNapi::GetFormId(napi_env env, napi_callback_info info)
88 {
89 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
90 auto ctxt = std::make_shared<ContextBase>();
91 auto sdForm = GetSystemDefinedForm(env, info, ctxt);
92 ASSERT_ERR(
93 ctxt->env, (sdForm != nullptr && sdForm->value_ != nullptr), Status::E_INVALID_PARAMETERS, "invalid object!");
94 ctxt->status = NapiDataUtils::SetValue(env, sdForm->value_->GetFormId(), ctxt->output);
95 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "set form id failed!");
96 return ctxt->output;
97 }
98
SetFormId(napi_env env,napi_callback_info info)99 napi_value SystemDefinedFormNapi::SetFormId(napi_env env, napi_callback_info info)
100 {
101 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
102 auto ctxt = std::make_shared<ContextBase>();
103 int32_t formId = 0;
104 auto input = [env, ctxt, &formId](size_t argc, napi_value *argv) {
105 ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::E_INVALID_PARAMETERS, "invalid arguments!");
106 ctxt->status = NapiDataUtils::GetValue(env, argv[0], formId);
107 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
108 };
109 ctxt->GetCbInfoSync(env, info, input);
110 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
111 auto sdForm = static_cast<SystemDefinedFormNapi *>(ctxt->native);
112 ASSERT_ERR(
113 ctxt->env, (sdForm != nullptr && sdForm->value_ != nullptr), Status::E_INVALID_PARAMETERS, "invalid object!");
114 sdForm->value_->SetFormId(formId);
115 return nullptr;
116 }
117
GetFormName(napi_env env,napi_callback_info info)118 napi_value SystemDefinedFormNapi::GetFormName(napi_env env, napi_callback_info info)
119 {
120 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
121 auto ctxt = std::make_shared<ContextBase>();
122 auto sdForm = GetSystemDefinedForm(env, info, ctxt);
123 ASSERT_ERR(
124 ctxt->env, (sdForm != nullptr && sdForm->value_ != nullptr), Status::E_INVALID_PARAMETERS, "invalid object!");
125 ctxt->status = NapiDataUtils::SetValue(env, sdForm->value_->GetFormName(), ctxt->output);
126 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "set form name failed!");
127 return ctxt->output;
128 }
129
SetFormName(napi_env env,napi_callback_info info)130 napi_value SystemDefinedFormNapi::SetFormName(napi_env env, napi_callback_info info)
131 {
132 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
133 auto ctxt = std::make_shared<ContextBase>();
134 std::string formName;
135 auto input = [env, ctxt, &formName](size_t argc, napi_value *argv) {
136 ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::E_INVALID_PARAMETERS, "invalid arguments!");
137 ctxt->status = NapiDataUtils::GetValue(env, argv[0], formName);
138 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
139 };
140 ctxt->GetCbInfoSync(env, info, input);
141 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
142 auto sdForm = static_cast<SystemDefinedFormNapi *>(ctxt->native);
143 ASSERT_ERR(
144 ctxt->env, (sdForm != nullptr && sdForm->value_ != nullptr), Status::E_INVALID_PARAMETERS, "invalid object!");
145 sdForm->value_->SetFormName(formName);
146 return nullptr;
147 }
148
GetBundleName(napi_env env,napi_callback_info info)149 napi_value SystemDefinedFormNapi::GetBundleName(napi_env env, napi_callback_info info)
150 {
151 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
152 auto ctxt = std::make_shared<ContextBase>();
153 auto sdForm = GetSystemDefinedForm(env, info, ctxt);
154 ASSERT_ERR(
155 ctxt->env, (sdForm != nullptr && sdForm->value_ != nullptr), Status::E_INVALID_PARAMETERS, "invalid object!");
156 ctxt->status = NapiDataUtils::SetValue(env, sdForm->value_->GetBundleName(), ctxt->output);
157 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "set bundle name failed!");
158 return ctxt->output;
159 }
160
SetBundleName(napi_env env,napi_callback_info info)161 napi_value SystemDefinedFormNapi::SetBundleName(napi_env env, napi_callback_info info)
162 {
163 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
164 auto ctxt = std::make_shared<ContextBase>();
165 std::string bundleName;
166 auto input = [env, ctxt, &bundleName](size_t argc, napi_value *argv) {
167 ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::E_INVALID_PARAMETERS, "invalid arguments!");
168 ctxt->status = NapiDataUtils::GetValue(env, argv[0], bundleName);
169 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
170 };
171 ctxt->GetCbInfoSync(env, info, input);
172 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
173 auto sdForm = static_cast<SystemDefinedFormNapi *>(ctxt->native);
174 ASSERT_ERR(
175 ctxt->env, (sdForm != nullptr && sdForm->value_ != nullptr), Status::E_INVALID_PARAMETERS, "invalid object!");
176 sdForm->value_->SetBundleName(bundleName);
177 return nullptr;
178 }
179
GetAbilityName(napi_env env,napi_callback_info info)180 napi_value SystemDefinedFormNapi::GetAbilityName(napi_env env, napi_callback_info info)
181 {
182 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
183 auto ctxt = std::make_shared<ContextBase>();
184 auto sdForm = GetSystemDefinedForm(env, info, ctxt);
185 ASSERT_ERR(
186 ctxt->env, (sdForm != nullptr && sdForm->value_ != nullptr), Status::E_INVALID_PARAMETERS, "invalid object!");
187 ctxt->status = NapiDataUtils::SetValue(env, sdForm->value_->GetAbilityName(), ctxt->output);
188 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "set ability name failed!");
189 return ctxt->output;
190 }
191
SetAbilityName(napi_env env,napi_callback_info info)192 napi_value SystemDefinedFormNapi::SetAbilityName(napi_env env, napi_callback_info info)
193 {
194 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
195 auto ctxt = std::make_shared<ContextBase>();
196 std::string abilityName;
197 auto input = [env, ctxt, &abilityName](size_t argc, napi_value *argv) {
198 ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::E_INVALID_PARAMETERS, "invalid arguments!");
199 ctxt->status = NapiDataUtils::GetValue(env, argv[0], abilityName);
200 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
201 };
202 ctxt->GetCbInfoSync(env, info, input);
203 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
204 auto sdForm = static_cast<SystemDefinedFormNapi *>(ctxt->native);
205 ASSERT_ERR(
206 ctxt->env, (sdForm != nullptr && sdForm->value_ != nullptr), Status::E_INVALID_PARAMETERS, "invalid object!");
207 sdForm->value_->SetAbilityName(abilityName);
208 return nullptr;
209 }
210
GetModule(napi_env env,napi_callback_info info)211 napi_value SystemDefinedFormNapi::GetModule(napi_env env, napi_callback_info info)
212 {
213 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
214 auto ctxt = std::make_shared<ContextBase>();
215 auto sdForm = GetSystemDefinedForm(env, info, ctxt);
216 ASSERT_ERR(
217 ctxt->env, (sdForm != nullptr && sdForm->value_ != nullptr), Status::E_INVALID_PARAMETERS, "invalid object!");
218 ctxt->status = NapiDataUtils::SetValue(env, sdForm->value_->GetModule(), ctxt->output);
219 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "set module failed!");
220 return ctxt->output;
221 }
222
SetModule(napi_env env,napi_callback_info info)223 napi_value SystemDefinedFormNapi::SetModule(napi_env env, napi_callback_info info)
224 {
225 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedFormNapi");
226 auto ctxt = std::make_shared<ContextBase>();
227 std::string module;
228 auto input = [env, ctxt, &module](size_t argc, napi_value *argv) {
229 ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::E_INVALID_PARAMETERS, "invalid arguments!");
230 ctxt->status = NapiDataUtils::GetValue(env, argv[0], module);
231 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
232 };
233 ctxt->GetCbInfoSync(env, info, input);
234 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
235 auto sdForm = static_cast<SystemDefinedFormNapi *>(ctxt->native);
236 ASSERT_ERR(
237 ctxt->env, (sdForm != nullptr && sdForm->value_ != nullptr), Status::E_INVALID_PARAMETERS, "invalid object!");
238 sdForm->value_->SetModule(module);
239 return nullptr;
240 }
241 } // namespace UDMF
242 } // namespace OHOS