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