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