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 "unified_record_napi.h"
17
18 #include "napi_data_utils.h"
19 #include "napi_error_utils.h"
20 #include "napi_queue.h"
21 #include "unified_record.h"
22
23 namespace OHOS {
24 namespace UDMF {
Constructor(napi_env env)25 napi_value UnifiedRecordNapi::Constructor(napi_env env)
26 {
27 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedRecordNapi");
28 napi_property_descriptor properties[] = {
29 /* UnifiedRecord properties */
30 DECLARE_NAPI_FUNCTION("getType", GetType),
31 };
32 size_t count = sizeof(properties) / sizeof(properties[0]);
33 return NapiDataUtils::DefineClass(env, "UnifiedRecord", properties, count, UnifiedRecordNapi::New);
34 }
35
New(napi_env env,napi_callback_info info)36 napi_value UnifiedRecordNapi::New(napi_env env, napi_callback_info info)
37 {
38 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedRecordNapi");
39 auto ctxt = std::make_shared<ContextBase>();
40 ctxt->GetCbInfoSync(env, info);
41 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
42
43 auto *udRecord = new (std::nothrow) UnifiedRecordNapi();
44 ASSERT_ERR(ctxt->env, udRecord != nullptr, Status::E_UNKNOWN, "no memory for unified record!");
45 udRecord->value_ = std::make_shared<UnifiedRecord>();
46 ASSERT_CALL(env, napi_wrap(env, ctxt->self, udRecord, Destructor, nullptr, nullptr), udRecord);
47 return ctxt->self;
48 }
49
Destructor(napi_env env,void * data,void * hint)50 void UnifiedRecordNapi::Destructor(napi_env env, void *data, void *hint)
51 {
52 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedRecordNapi finalize.");
53 auto *uRecord = static_cast<UnifiedRecordNapi *>(data);
54 ASSERT_VOID(uRecord != nullptr, "finalize null!");
55 delete uRecord;
56 }
57
GetUnifiedRecord(napi_env env,napi_callback_info info,std::shared_ptr<ContextBase> ctxt)58 UnifiedRecordNapi *UnifiedRecordNapi::GetUnifiedRecord(
59 napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
60 {
61 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedRecordNapi");
62 ctxt->GetCbInfoSync(env, info);
63 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
64 return static_cast<UnifiedRecordNapi *>(ctxt->native);
65 }
66
GetType(napi_env env,napi_callback_info info)67 napi_value UnifiedRecordNapi::GetType(napi_env env, napi_callback_info info)
68 {
69 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedRecordNapi");
70 auto ctxt = std::make_shared<ContextBase>();
71 auto uRecord = GetUnifiedRecord(env, info, ctxt);
72 ASSERT_ERR(ctxt->env, (uRecord != nullptr && uRecord->value_ != nullptr), Status::E_INVALID_PARAMETERS,
73 "invalid object!");
74 ctxt->status = NapiDataUtils::SetValue(env, UD_TYPE_MAP.at(uRecord->value_->GetType()), ctxt->output);
75 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "set type failed!");
76 return ctxt->output;
77 }
78 } // namespace UDMF
79 } // namespace OHOS