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