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 "HtmlNapi"
16 #include "html_napi.h"
17 
18 #include "html.h"
19 #include "text_napi.h"
20 #include "unified_record_napi.h"
21 
22 namespace OHOS {
23 namespace UDMF {
Constructor(napi_env env)24 napi_value HtmlNapi::Constructor(napi_env env)
25 {
26     LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
27     napi_property_descriptor properties[] = {
28         /* Html extends UnifiedRecord */
29         DECLARE_NAPI_FUNCTION("getType", UnifiedRecordNapi::GetType),
30         DECLARE_NAPI_FUNCTION("getValue", UnifiedRecordNapi::GetValue),
31         /* Html extends Text */
32         DECLARE_NAPI_GETTER_SETTER("details", TextNapi::GetDetails, TextNapi::SetDetails),
33         /* Html properties */
34         DECLARE_NAPI_GETTER_SETTER("htmlContent", GetHtmlContent, SetHtmlContent),
35         DECLARE_NAPI_GETTER_SETTER("plainContent", GetPlainContent, SetPlainContent),
36     };
37     size_t count = sizeof(properties) / sizeof(properties[0]);
38     return NapiDataUtils::DefineClass(env, "HTML", properties, count, HtmlNapi::New);
39 }
40 
New(napi_env env,napi_callback_info info)41 napi_value HtmlNapi::New(napi_env env, napi_callback_info info)
42 {
43     LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
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 *html = new (std::nothrow) HtmlNapi();
49     ASSERT_ERR(ctxt->env, html != nullptr, Status::E_ERROR, "no memory for html!");
50     html->value_ = std::make_shared<Html>();
51     ASSERT_CALL(env, napi_wrap(env, ctxt->self, html, Destructor, nullptr, nullptr), html);
52     return ctxt->self;
53 }
54 
NewInstance(napi_env env,std::shared_ptr<UnifiedRecord> in,napi_value & out)55 void HtmlNapi::NewInstance(napi_env env, std::shared_ptr<UnifiedRecord> in, napi_value &out)
56 {
57     LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
58     ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
59     auto *html = new (std::nothrow) HtmlNapi();
60     ASSERT_ERR_VOID(env, html != nullptr, Status::E_ERROR, "no memory for html!");
61     html->value_ = std::static_pointer_cast<Html>(in);
62     ASSERT_CALL_DELETE(env, napi_wrap(env, out, html, Destructor, nullptr, nullptr), html);
63 }
64 
Destructor(napi_env env,void * data,void * hint)65 void HtmlNapi::Destructor(napi_env env, void *data, void *hint)
66 {
67     LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi finalize.");
68     auto *html = static_cast<HtmlNapi *>(data);
69     ASSERT_VOID(html != nullptr, "finalize null!");
70     delete html;
71 }
72 
GetHtml(napi_env env,napi_callback_info info,std::shared_ptr<ContextBase> ctxt)73 HtmlNapi *HtmlNapi::GetHtml(napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
74 {
75     LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
76     ctxt->GetCbInfoSync(env, info);
77     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
78     return static_cast<HtmlNapi *>(ctxt->native);
79 }
80 
GetPlainContent(napi_env env,napi_callback_info info)81 napi_value HtmlNapi::GetPlainContent(napi_env env, napi_callback_info info)
82 {
83     LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
84     auto ctxt = std::make_shared<ContextBase>();
85     auto html = GetHtml(env, info, ctxt);
86     ASSERT_ERR(
87         ctxt->env, (html != nullptr && html->value_ != nullptr), Status::E_ERROR, "invalid object!");
88     ctxt->status = NapiDataUtils::SetValue(env, html->value_->GetPlainContent(), ctxt->output);
89     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set plain content failed!");
90     return ctxt->output;
91 }
92 
SetPlainContent(napi_env env,napi_callback_info info)93 napi_value HtmlNapi::SetPlainContent(napi_env env, napi_callback_info info)
94 {
95     LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
96     auto ctxt = std::make_shared<ContextBase>();
97     std::string plainContent;
98     auto input = [env, ctxt, &plainContent](size_t argc, napi_value *argv) {
99         ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
100             Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
101         ctxt->status = NapiDataUtils::GetValue(env, argv[0], plainContent);
102         ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
103             Status::E_INVALID_PARAMETERS, "Parameter error: parameter plainContent type must be string");
104     };
105     ctxt->GetCbInfoSync(env, info, input);
106     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
107     auto html = static_cast<HtmlNapi *>(ctxt->native);
108     ASSERT_ERR(
109         ctxt->env, (html != nullptr && html->value_ != nullptr), Status::E_ERROR, "invalid object!");
110     html->value_->SetPlainContent(plainContent);
111     return nullptr;
112 }
113 
GetHtmlContent(napi_env env,napi_callback_info info)114 napi_value HtmlNapi::GetHtmlContent(napi_env env, napi_callback_info info)
115 {
116     LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
117     auto ctxt = std::make_shared<ContextBase>();
118     auto html = GetHtml(env, info, ctxt);
119     ASSERT_ERR(
120         ctxt->env, (html != nullptr && html->value_ != nullptr), Status::E_ERROR, "invalid object!");
121     ctxt->status = NapiDataUtils::SetValue(env, html->value_->GetHtmlContent(), ctxt->output);
122     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set html failed!");
123     return ctxt->output;
124 }
125 
SetHtmlContent(napi_env env,napi_callback_info info)126 napi_value HtmlNapi::SetHtmlContent(napi_env env, napi_callback_info info)
127 {
128     LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
129     auto ctxt = std::make_shared<ContextBase>();
130     std::string htmlContent;
131     auto input = [env, ctxt, &htmlContent](size_t argc, napi_value *argv) {
132         ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
133             Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
134         ctxt->status = NapiDataUtils::GetValue(env, argv[0], htmlContent);
135         ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
136             Status::E_INVALID_PARAMETERS, "Parameter error: parameter htmlContent type must be string");
137     };
138     ctxt->GetCbInfoSync(env, info, input);
139     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
140     auto html = static_cast<HtmlNapi *>(ctxt->native);
141     ASSERT_ERR(
142         ctxt->env, (html != nullptr && html->value_ != nullptr), Status::E_ERROR, "invalid object!");
143     html->value_->SetHtmlContent(htmlContent);
144     return nullptr;
145 }
146 } // namespace UDMF
147 } // namespace OHOS