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