114cf0368Sopenharmony_ci/*
214cf0368Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
314cf0368Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
414cf0368Sopenharmony_ci * you may not use this file except in compliance with the License.
514cf0368Sopenharmony_ci * You may obtain a copy of the License at
614cf0368Sopenharmony_ci *
714cf0368Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
814cf0368Sopenharmony_ci *
914cf0368Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1014cf0368Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1114cf0368Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1214cf0368Sopenharmony_ci * See the License for the specific language governing permissions and
1314cf0368Sopenharmony_ci * limitations under the License.
1414cf0368Sopenharmony_ci */
1514cf0368Sopenharmony_ci#define LOG_TAG "LinkNapi"
1614cf0368Sopenharmony_ci#include "link_napi.h"
1714cf0368Sopenharmony_ci
1814cf0368Sopenharmony_ci#include "link.h"
1914cf0368Sopenharmony_ci#include "text_napi.h"
2014cf0368Sopenharmony_ci#include "unified_record_napi.h"
2114cf0368Sopenharmony_ci
2214cf0368Sopenharmony_cinamespace OHOS {
2314cf0368Sopenharmony_cinamespace UDMF {
2414cf0368Sopenharmony_cinapi_value LinkNapi::Constructor(napi_env env)
2514cf0368Sopenharmony_ci{
2614cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "LinkNapi");
2714cf0368Sopenharmony_ci    napi_property_descriptor properties[] = {
2814cf0368Sopenharmony_ci        /* Link extends UnifiedRecord */
2914cf0368Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getType", UnifiedRecordNapi::GetType),
3014cf0368Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getValue", UnifiedRecordNapi::GetValue),
3114cf0368Sopenharmony_ci        /* Link extends Text */
3214cf0368Sopenharmony_ci        DECLARE_NAPI_GETTER_SETTER("details", TextNapi::GetDetails, TextNapi::SetDetails),
3314cf0368Sopenharmony_ci        /* Link properties */
3414cf0368Sopenharmony_ci        DECLARE_NAPI_GETTER_SETTER("url", GetUrl, SetUrl),
3514cf0368Sopenharmony_ci        DECLARE_NAPI_GETTER_SETTER("description", GetDescription, SetDescription),
3614cf0368Sopenharmony_ci    };
3714cf0368Sopenharmony_ci    size_t count = sizeof(properties) / sizeof(properties[0]);
3814cf0368Sopenharmony_ci    return NapiDataUtils::DefineClass(env, "Hyperlink", properties, count, LinkNapi::New);
3914cf0368Sopenharmony_ci}
4014cf0368Sopenharmony_ci
4114cf0368Sopenharmony_cinapi_value LinkNapi::New(napi_env env, napi_callback_info info)
4214cf0368Sopenharmony_ci{
4314cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "LinkNapi");
4414cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
4514cf0368Sopenharmony_ci
4614cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info);
4714cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
4814cf0368Sopenharmony_ci
4914cf0368Sopenharmony_ci    auto *link = new (std::nothrow) LinkNapi();
5014cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, link != nullptr, Status::E_ERROR, "no memory for link!");
5114cf0368Sopenharmony_ci    link->value_ = std::make_shared<Link>();
5214cf0368Sopenharmony_ci    ASSERT_CALL(env, napi_wrap(env, ctxt->self, link, Destructor, nullptr, nullptr), link);
5314cf0368Sopenharmony_ci    return ctxt->self;
5414cf0368Sopenharmony_ci}
5514cf0368Sopenharmony_ci
5614cf0368Sopenharmony_civoid LinkNapi::NewInstance(napi_env env, std::shared_ptr<UnifiedRecord> in, napi_value &out)
5714cf0368Sopenharmony_ci{
5814cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "LinkNapi");
5914cf0368Sopenharmony_ci    ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
6014cf0368Sopenharmony_ci    auto *link = new (std::nothrow) LinkNapi();
6114cf0368Sopenharmony_ci    ASSERT_ERR_VOID(env, link != nullptr, Status::E_ERROR, "no memory for link!");
6214cf0368Sopenharmony_ci    link->value_ = std::static_pointer_cast<Link>(in);
6314cf0368Sopenharmony_ci    ASSERT_CALL_DELETE(env, napi_wrap(env, out, link, Destructor, nullptr, nullptr), link);
6414cf0368Sopenharmony_ci}
6514cf0368Sopenharmony_ci
6614cf0368Sopenharmony_civoid LinkNapi::Destructor(napi_env env, void *data, void *hint)
6714cf0368Sopenharmony_ci{
6814cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "LinkNapi finalize.");
6914cf0368Sopenharmony_ci    auto *link = static_cast<LinkNapi *>(data);
7014cf0368Sopenharmony_ci    ASSERT_VOID(link != nullptr, "finalize null!");
7114cf0368Sopenharmony_ci    delete link;
7214cf0368Sopenharmony_ci}
7314cf0368Sopenharmony_ci
7414cf0368Sopenharmony_ciLinkNapi *LinkNapi::GetLink(napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
7514cf0368Sopenharmony_ci{
7614cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "LinkNapi");
7714cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info);
7814cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
7914cf0368Sopenharmony_ci    return static_cast<LinkNapi *>(ctxt->native);
8014cf0368Sopenharmony_ci}
8114cf0368Sopenharmony_ci
8214cf0368Sopenharmony_cinapi_value LinkNapi::GetUrl(napi_env env, napi_callback_info info)
8314cf0368Sopenharmony_ci{
8414cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "LinkNapi");
8514cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
8614cf0368Sopenharmony_ci    auto link = GetLink(env, info, ctxt);
8714cf0368Sopenharmony_ci    ASSERT_ERR(
8814cf0368Sopenharmony_ci        ctxt->env, (link != nullptr && link->value_ != nullptr), Status::E_ERROR, "invalid object!");
8914cf0368Sopenharmony_ci    ctxt->status = NapiDataUtils::SetValue(env, link->value_->GetUrl(), ctxt->output);
9014cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set url failed!");
9114cf0368Sopenharmony_ci    return ctxt->output;
9214cf0368Sopenharmony_ci}
9314cf0368Sopenharmony_ci
9414cf0368Sopenharmony_cinapi_value LinkNapi::SetUrl(napi_env env, napi_callback_info info)
9514cf0368Sopenharmony_ci{
9614cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "LinkNapi");
9714cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
9814cf0368Sopenharmony_ci    std::string url;
9914cf0368Sopenharmony_ci    auto input = [env, ctxt, &url](size_t argc, napi_value *argv) {
10014cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
10114cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
10214cf0368Sopenharmony_ci        ctxt->status = NapiDataUtils::GetValue(env, argv[0], url);
10314cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
10414cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: parameter url type must be string");
10514cf0368Sopenharmony_ci    };
10614cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info, input);
10714cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
10814cf0368Sopenharmony_ci    auto link = static_cast<LinkNapi *>(ctxt->native);
10914cf0368Sopenharmony_ci    ASSERT_ERR(
11014cf0368Sopenharmony_ci        ctxt->env, (link != nullptr && link->value_ != nullptr), Status::E_ERROR, "invalid object!");
11114cf0368Sopenharmony_ci    link->value_->SetUrl(url);
11214cf0368Sopenharmony_ci    return nullptr;
11314cf0368Sopenharmony_ci}
11414cf0368Sopenharmony_ci
11514cf0368Sopenharmony_cinapi_value LinkNapi::GetDescription(napi_env env, napi_callback_info info)
11614cf0368Sopenharmony_ci{
11714cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "LinkNapi");
11814cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
11914cf0368Sopenharmony_ci    auto link = GetLink(env, info, ctxt);
12014cf0368Sopenharmony_ci    ASSERT_ERR(
12114cf0368Sopenharmony_ci        ctxt->env, (link != nullptr && link->value_ != nullptr), Status::E_ERROR, "invalid object!");
12214cf0368Sopenharmony_ci    ctxt->status = NapiDataUtils::SetValue(env, link->value_->GetDescription(), ctxt->output);
12314cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set description failed!");
12414cf0368Sopenharmony_ci    return ctxt->output;
12514cf0368Sopenharmony_ci}
12614cf0368Sopenharmony_ci
12714cf0368Sopenharmony_cinapi_value LinkNapi::SetDescription(napi_env env, napi_callback_info info)
12814cf0368Sopenharmony_ci{
12914cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "LinkNapi");
13014cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
13114cf0368Sopenharmony_ci    std::string description;
13214cf0368Sopenharmony_ci    auto input = [env, ctxt, &description](size_t argc, napi_value *argv) {
13314cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
13414cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
13514cf0368Sopenharmony_ci        ctxt->status = NapiDataUtils::GetValue(env, argv[0], description);
13614cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
13714cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: parameter description type must be string");
13814cf0368Sopenharmony_ci    };
13914cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info, input);
14014cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
14114cf0368Sopenharmony_ci    auto link = static_cast<LinkNapi *>(ctxt->native);
14214cf0368Sopenharmony_ci    ASSERT_ERR(
14314cf0368Sopenharmony_ci        ctxt->env, (link != nullptr && link->value_ != nullptr), Status::E_ERROR, "invalid object!");
14414cf0368Sopenharmony_ci    link->value_->SetDescription(description);
14514cf0368Sopenharmony_ci    return nullptr;
14614cf0368Sopenharmony_ci}
14714cf0368Sopenharmony_ci} // namespace UDMF
14814cf0368Sopenharmony_ci} // namespace OHOS