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