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 "SystemDefinedRecordNapi"
16 #include "system_defined_record_napi.h"
17
18 #include "system_defined_record.h"
19 #include "unified_record_napi.h"
20
21 namespace OHOS {
22 namespace UDMF {
Constructor(napi_env env)23 napi_value SystemDefinedRecordNapi::Constructor(napi_env env)
24 {
25 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedRecordNapi");
26 napi_property_descriptor properties[] = {
27 /* SystemDefinedRecord extends UnifiedRecord */
28 DECLARE_NAPI_FUNCTION("getType", UnifiedRecordNapi::GetType),
29 DECLARE_NAPI_FUNCTION("getValue", UnifiedRecordNapi::GetValue),
30 DECLARE_NAPI_FUNCTION("addEntry", UnifiedRecordNapi::AddEntry),
31 DECLARE_NAPI_FUNCTION("getEntry", UnifiedRecordNapi::GetEntry),
32 DECLARE_NAPI_FUNCTION("getEntries", UnifiedRecordNapi::GetEntries),
33 DECLARE_NAPI_FUNCTION("getTypes", UnifiedRecordNapi::GetTypes),
34 /* SystemDefinedRecord properties */
35 DECLARE_NAPI_GETTER_SETTER("details", GetDetails, SetDetails),
36 };
37 size_t count = sizeof(properties) / sizeof(properties[0]);
38 return NapiDataUtils::DefineClass(env, "SystemDefinedRecord", properties, count, SystemDefinedRecordNapi::New);
39 }
40
New(napi_env env,napi_callback_info info)41 napi_value SystemDefinedRecordNapi::New(napi_env env, napi_callback_info info)
42 {
43 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedRecordNapi");
44 auto ctxt = std::make_shared<ContextBase>();
45 ctxt->GetCbInfoSync(env, info);
46 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
47
48 auto *sdRecord = new (std::nothrow) SystemDefinedRecordNapi();
49 ASSERT_ERR(ctxt->env, sdRecord != nullptr, Status::E_ERROR, "no memory for system defined record!");
50 sdRecord->value_ = std::make_shared<SystemDefinedRecord>();
51 ASSERT_CALL(env, napi_wrap(env, ctxt->self, sdRecord, Destructor, nullptr, nullptr), sdRecord);
52 return ctxt->self;
53 }
54
NewInstance(napi_env env,std::shared_ptr<UnifiedRecord> in,napi_value & out)55 void SystemDefinedRecordNapi::NewInstance(napi_env env, std::shared_ptr<UnifiedRecord> in, napi_value &out)
56 {
57 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedRecordNapi");
58 ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
59 auto *sdRecord = new (std::nothrow) SystemDefinedRecordNapi();
60 ASSERT_ERR_VOID(env, sdRecord != nullptr, Status::E_ERROR, "no memory for system defined record!");
61 sdRecord->value_ = std::static_pointer_cast<SystemDefinedRecord>(in);
62 ASSERT_CALL_DELETE(env, napi_wrap(env, out, sdRecord, Destructor, nullptr, nullptr), sdRecord);
63 }
64
Destructor(napi_env env,void * data,void * hint)65 void SystemDefinedRecordNapi::Destructor(napi_env env, void *data, void *hint)
66 {
67 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedRecordNapi finalize.");
68 auto *sdRecord = static_cast<SystemDefinedRecordNapi *>(data);
69 ASSERT_VOID(sdRecord != nullptr, "finalize null!");
70 delete sdRecord;
71 }
72
GetSystemDefinedRecord(napi_env env,napi_callback_info info,std::shared_ptr<ContextBase> ctxt)73 SystemDefinedRecordNapi *SystemDefinedRecordNapi::GetSystemDefinedRecord(
74 napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
75 {
76 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedRecordNapi");
77 ctxt->GetCbInfoSync(env, info);
78 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
79 return static_cast<SystemDefinedRecordNapi *>(ctxt->native);
80 }
81
GetDetails(napi_env env,napi_callback_info info)82 napi_value SystemDefinedRecordNapi::GetDetails(napi_env env, napi_callback_info info)
83 {
84 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedRecordNapi");
85 auto ctxt = std::make_shared<ContextBase>();
86 auto sdRecord = GetSystemDefinedRecord(env, info, ctxt);
87 ASSERT_ERR(ctxt->env, (sdRecord != nullptr && sdRecord->value_ != nullptr), Status::E_ERROR,
88 "invalid object!");
89 ctxt->status = NapiDataUtils::SetValue(env, sdRecord->value_->GetDetails(), ctxt->output);
90 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set details failed!");
91 return ctxt->output;
92 }
93
SetDetails(napi_env env,napi_callback_info info)94 napi_value SystemDefinedRecordNapi::SetDetails(napi_env env, napi_callback_info info)
95 {
96 LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedRecordNapi");
97 auto ctxt = std::make_shared<ContextBase>();
98
99 UDDetails details;
100 auto input = [env, ctxt, &details](size_t argc, napi_value *argv) {
101 ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
102 Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
103 ctxt->status = NapiDataUtils::GetValue(env, argv[0], details);
104 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS,
105 "Parameter error: parameter details type must be Record<string, number | string | Uint8Array>");
106 };
107 ctxt->GetCbInfoSync(env, info, input);
108 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
109 auto sdRecord = static_cast<SystemDefinedRecordNapi *>(ctxt->native);
110 ASSERT_ERR(
111 ctxt->env, (sdRecord != nullptr && sdRecord->value_ != nullptr), Status::E_ERROR,
112 "invalid object!");
113 sdRecord->value_->SetDetails(details);
114 return nullptr;
115 }
116 } // namespace UDMF
117 } // namespace OHOS