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