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