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