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