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 "TypeDescriptorNapi"
16 #include "type_descriptor_napi.h"
17 #include "napi_data_utils.h"
18
19 namespace OHOS {
20 namespace UDMF {
Constructor(napi_env env)21 napi_value TypeDescriptorNapi::Constructor(napi_env env)
22 {
23 LOG_DEBUG(UDMF_KITS_NAPI, "typeDescriptorNapi");
24 napi_property_descriptor properties[] = {
25 /* TypeDescriptor properties */
26 DECLARE_NAPI_FUNCTION("belongsTo", BelongsTo),
27 DECLARE_NAPI_FUNCTION("isLowerLevelType", IsLowerLevelType),
28 DECLARE_NAPI_FUNCTION("isHigherLevelType", IsHigherLevelType),
29 DECLARE_NAPI_FUNCTION("equals", Equals),
30 DECLARE_NAPI_GETTER_SETTER("typeId", GetTypeId, nullptr),
31 DECLARE_NAPI_GETTER_SETTER("belongingToTypes", GetBelongingToTypes, nullptr),
32 DECLARE_NAPI_GETTER_SETTER("description", GetDescription, nullptr),
33 DECLARE_NAPI_GETTER_SETTER("referenceURL", GetReferenceURL, nullptr),
34 DECLARE_NAPI_GETTER_SETTER("iconFile", GetIconFile, nullptr),
35 DECLARE_NAPI_GETTER_SETTER("filenameExtensions", GetFilenameExtensions, nullptr),
36 DECLARE_NAPI_GETTER_SETTER("mimeTypes", GetMimeTypes, nullptr)
37 };
38 size_t count = sizeof(properties) / sizeof(properties[0]);
39 return NapiDataUtils::DefineClass(env, "TypeDescriptor", properties, count, TypeDescriptorNapi::New);
40 }
41
New(napi_env env,napi_callback_info info)42 napi_value TypeDescriptorNapi::New(napi_env env, napi_callback_info info)
43 {
44 LOG_DEBUG(UDMF_KITS_NAPI, "TypeDescriptorNapi.");
45 auto ctxt = std::make_shared<ContextBase>();
46 ctxt->GetCbInfoSync(env, info);
47 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
48
49 auto *descriptorNapi = new (std::nothrow) TypeDescriptorNapi();
50 ASSERT_ERR(ctxt->env, descriptorNapi != nullptr, Status::E_ERROR, "no memory for descriptorNapi!");
51 descriptorNapi->value_ = nullptr;
52 ASSERT_CALL(ctxt->env, napi_wrap(env, ctxt->self, descriptorNapi, Destructor, nullptr, nullptr), descriptorNapi);
53 return ctxt->self;
54 }
55
Destructor(napi_env env,void * data,void * hint)56 void TypeDescriptorNapi::Destructor(napi_env env, void *data, void *hint)
57 {
58 LOG_DEBUG(UDMF_KITS_NAPI, "TypeDescriptorNapi finalize.");
59 auto *uData = static_cast<TypeDescriptorNapi *>(data);
60 ASSERT_VOID(uData != nullptr, "finalize null!");
61 delete uData;
62 }
63
NewInstance(napi_env env,std::shared_ptr<TypeDescriptor> in,napi_value & out)64 void TypeDescriptorNapi::NewInstance(napi_env env, std::shared_ptr<TypeDescriptor> in, napi_value &out)
65 {
66 LOG_DEBUG(UDMF_KITS_NAPI, "TypeDescriptorNapi create NewInstance");
67 ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
68 TypeDescriptorNapi *descriptorNapi = new (std::nothrow) TypeDescriptorNapi();
69 ASSERT_ERR_VOID(env, descriptorNapi != nullptr, Status::E_ERROR, "no memory for typeDescriptorNapi!");
70 descriptorNapi->value_ = in;
71 ASSERT_CALL_DELETE(env, napi_wrap(env, out, descriptorNapi, Destructor, nullptr, nullptr), descriptorNapi);
72 }
73
GetDescriptorNapi(napi_env env,napi_callback_info info,std::shared_ptr<ContextBase> & ctxt)74 TypeDescriptorNapi *TypeDescriptorNapi::GetDescriptorNapi(napi_env env, napi_callback_info info,
75 std::shared_ptr<ContextBase> &ctxt)
76 {
77 LOG_DEBUG(UDMF_KITS_NAPI, "GetDescriptorNapi");
78 ctxt->GetCbInfoSync(env, info);
79 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
80 return static_cast<TypeDescriptorNapi *>(ctxt->native);
81 }
82
BelongsTo(napi_env env,napi_callback_info info)83 napi_value TypeDescriptorNapi::BelongsTo(napi_env env, napi_callback_info info)
84 {
85 LOG_DEBUG(UDMF_KITS_NAPI, "TypeDescriptorNapi::BelongsTo()");
86 auto ctxt = std::make_shared<ContextBase>();
87 std::string typeId;
88 auto input = [env, ctxt, &typeId](size_t argc, napi_value* argv) {
89 // required 1 arguments : descriptor
90 ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
91 Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
92 ctxt->status = NapiDataUtils::GetValue(env, argv[0], typeId);
93 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
94 Status::E_INVALID_PARAMETERS, "Parameter error: parameter type type must be string");
95 };
96
97 ctxt->GetCbInfoSync(env, info, input);
98 ASSERT_NULL(!ctxt->isThrowError, "BelongsTo Exit");
99 bool checkRet = false;
100 Status status = reinterpret_cast<TypeDescriptorNapi*>(ctxt->native)->value_->BelongsTo(typeId, checkRet);
101 ASSERT_ERR(ctxt->env, status == E_OK, status, "invalid arguments!");
102 napi_get_boolean(env, checkRet, &ctxt->output);
103 return ctxt->output;
104 }
105
IsLowerLevelType(napi_env env,napi_callback_info info)106 napi_value TypeDescriptorNapi::IsLowerLevelType(napi_env env, napi_callback_info info)
107 {
108 LOG_DEBUG(UDMF_KITS_NAPI, "TypeDescriptorNapi::IsLowerLevelType()");
109 auto ctxt = std::make_shared<ContextBase>();
110 std::string typeId;
111 auto input = [env, ctxt, &typeId](size_t argc, napi_value* argv) {
112 // required 1 arguments : descriptor
113 ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
114 Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
115 ctxt->status = NapiDataUtils::GetValue(env, argv[0], typeId);
116 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
117 Status::E_INVALID_PARAMETERS, "Parameter error: parameter type type must be string");
118 };
119
120 ctxt->GetCbInfoSync(env, info, input);
121 ASSERT_NULL(!ctxt->isThrowError, "IsLowerLevelType Exit");
122 bool checkRet = false;
123 Status status = reinterpret_cast<TypeDescriptorNapi*>(ctxt->native)->value_->IsLowerLevelType(typeId, checkRet);
124 ASSERT_ERR(ctxt->env, status == E_OK, status, "invalid arguments!");
125 napi_get_boolean(env, checkRet, &ctxt->output);
126 return ctxt->output;
127 }
128
IsHigherLevelType(napi_env env,napi_callback_info info)129 napi_value TypeDescriptorNapi::IsHigherLevelType(napi_env env, napi_callback_info info)
130 {
131 LOG_DEBUG(UDMF_KITS_NAPI, "TypeDescriptorNapi::IsHigherLevelType()");
132 auto ctxt = std::make_shared<ContextBase>();
133 std::string typeId;
134 auto input = [env, ctxt, &typeId](size_t argc, napi_value* argv) {
135 // required 1 arguments : descriptor
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], typeId);
139 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
140 Status::E_INVALID_PARAMETERS, "Parameter error: parameter field type must be string");
141 };
142
143 ctxt->GetCbInfoSync(env, info, input);
144 ASSERT_NULL(!ctxt->isThrowError, "IsHigherLevelType Exit");
145 bool checkRet = false;
146 Status status = reinterpret_cast<TypeDescriptorNapi*>(ctxt->native)->value_->IsHigherLevelType(typeId, checkRet);
147 ASSERT_ERR(ctxt->env, status == E_OK, status, "invalid arguments!");
148 napi_get_boolean(env, checkRet, &ctxt->output);
149 return ctxt->output;
150 }
151
Equals(napi_env env,napi_callback_info info)152 napi_value TypeDescriptorNapi::Equals(napi_env env, napi_callback_info info)
153 {
154 LOG_DEBUG(UDMF_KITS_NAPI, "TypeDescriptorNapi::Equals()");
155 auto ctxt = std::make_shared<ContextBase>();
156 std::shared_ptr<TypeDescriptor> typeDescriptor;
157 auto input = [env, ctxt, &typeDescriptor](size_t argc, napi_value* argv) {
158 // required 1 arguments : descriptor
159 ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
160 Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
161 ctxt->status = NapiDataUtils::GetValue(env, argv[0], typeDescriptor);
162 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS,
163 "Parameter error: parameter typeDescriptor type must be TypeDescriptor");
164 };
165
166 ctxt->GetCbInfoSync(env, info, input);
167 ASSERT_NULL(!ctxt->isThrowError, "Equals Exit");
168 bool equalsRet = reinterpret_cast<TypeDescriptorNapi*>(ctxt->native)->value_->Equals(typeDescriptor);
169 napi_get_boolean(env, equalsRet, &ctxt->output);
170 return ctxt->output;
171 }
172
GetTypeId(napi_env env,napi_callback_info info)173 napi_value TypeDescriptorNapi::GetTypeId(napi_env env, napi_callback_info info)
174 {
175 LOG_DEBUG(UDMF_KITS_NAPI, "GetTypeId");
176 auto ctxt = std::make_shared<ContextBase>();
177 auto descriptorNapi = GetDescriptorNapi(env, info, ctxt);
178 ASSERT_ERR(ctxt->env, (descriptorNapi != nullptr && descriptorNapi->value_ != nullptr),
179 Status::E_ERROR, "invalid object!");
180 ctxt->status = NapiDataUtils::SetValue(env, descriptorNapi->value_->GetTypeId(), ctxt->output);
181 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set typeId value failed!");
182 return ctxt->output;
183 }
184
GetBelongingToTypes(napi_env env,napi_callback_info info)185 napi_value TypeDescriptorNapi::GetBelongingToTypes(napi_env env, napi_callback_info info)
186 {
187 LOG_DEBUG(UDMF_KITS_NAPI, "GetBelongingToTypes");
188 auto ctxt = std::make_shared<ContextBase>();
189 auto descriptorNapi = GetDescriptorNapi(env, info, ctxt);
190 ASSERT_ERR(ctxt->env, (descriptorNapi != nullptr && descriptorNapi->value_ != nullptr),
191 Status::E_ERROR, "invalid object!");
192 std::vector<std::string> belongingTypes = descriptorNapi->value_->GetBelongingToTypes();
193 ctxt->status = NapiDataUtils::SetValue(env, belongingTypes, ctxt->output);
194 return ctxt->output;
195 }
196
GetDescription(napi_env env,napi_callback_info info)197 napi_value TypeDescriptorNapi::GetDescription(napi_env env, napi_callback_info info)
198 {
199 LOG_DEBUG(UDMF_KITS_NAPI, "GetDescription");
200 auto ctxt = std::make_shared<ContextBase>();
201 auto descriptorNapi = GetDescriptorNapi(env, info, ctxt);
202 ASSERT_ERR(ctxt->env, (descriptorNapi != nullptr && descriptorNapi->value_ != nullptr),
203 Status::E_ERROR, "invalid object!");
204 ctxt->status = NapiDataUtils::SetValue(env, descriptorNapi->value_->GetDescription(), ctxt->output);
205 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set description value failed!");
206 return ctxt->output;
207 }
208
GetReferenceURL(napi_env env,napi_callback_info info)209 napi_value TypeDescriptorNapi::GetReferenceURL(napi_env env, napi_callback_info info)
210 {
211 LOG_DEBUG(UDMF_KITS_NAPI, "GetReferenceURL");
212 auto ctxt = std::make_shared<ContextBase>();
213 auto descriptorNapi = GetDescriptorNapi(env, info, ctxt);
214 ASSERT_ERR(ctxt->env, (descriptorNapi != nullptr && descriptorNapi->value_ != nullptr),
215 Status::E_ERROR, "invalid object!");
216 ctxt->status = NapiDataUtils::SetValue(env, descriptorNapi->value_->GetReferenceURL(), ctxt->output);
217 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set referenceURL value failed!");
218 return ctxt->output;
219 }
220
GetIconFile(napi_env env,napi_callback_info info)221 napi_value TypeDescriptorNapi::GetIconFile(napi_env env, napi_callback_info info)
222 {
223 LOG_DEBUG(UDMF_KITS_NAPI, "GetIconFile");
224 auto ctxt = std::make_shared<ContextBase>();
225 auto descriptorNapi = GetDescriptorNapi(env, info, ctxt);
226 ASSERT_ERR(ctxt->env, (descriptorNapi != nullptr && descriptorNapi->value_ != nullptr),
227 Status::E_ERROR, "invalid object!");
228 ctxt->status = NapiDataUtils::SetValue(env, descriptorNapi->value_->GetIconFile(), ctxt->output);
229 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set iconFile value failed!");
230 return ctxt->output;
231 }
232
GetFilenameExtensions(napi_env env,napi_callback_info info)233 napi_value TypeDescriptorNapi::GetFilenameExtensions(napi_env env, napi_callback_info info)
234 {
235 LOG_DEBUG(UDMF_KITS_NAPI, "GetFilenameExtensions");
236 auto ctxt = std::make_shared<ContextBase>();
237 auto descriptorNapi = GetDescriptorNapi(env, info, ctxt);
238 ASSERT_ERR(ctxt->env, (descriptorNapi != nullptr && descriptorNapi->value_ != nullptr),
239 Status::E_ERROR, "invalid object!");
240 std::vector<std::string> filenameExtensions = descriptorNapi->value_->GetFilenameExtensions();
241 ctxt->status = NapiDataUtils::SetValue(env, filenameExtensions, ctxt->output);
242 return ctxt->output;
243 }
244
GetMimeTypes(napi_env env,napi_callback_info info)245 napi_value TypeDescriptorNapi::GetMimeTypes(napi_env env, napi_callback_info info)
246 {
247 LOG_DEBUG(UDMF_KITS_NAPI, "GetMimeTypes");
248 auto ctxt = std::make_shared<ContextBase>();
249 auto descriptorNapi = GetDescriptorNapi(env, info, ctxt);
250 ASSERT_ERR(ctxt->env, (descriptorNapi != nullptr && descriptorNapi->value_ != nullptr),
251 Status::E_ERROR, "invalid object!");
252 std::vector<std::string> mimeTypes = descriptorNapi->value_->GetMimeTypes();
253 ctxt->status = NapiDataUtils::SetValue(env, mimeTypes, ctxt->output);
254 return ctxt->output;
255 }
256 } // namespace UDMF
257 } // namespace OHOS