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 "UnifiedDataNapi"
1614cf0368Sopenharmony_ci#include "unified_data_napi.h"
1714cf0368Sopenharmony_ci
1814cf0368Sopenharmony_ci#include "application_defined_record_napi.h"
1914cf0368Sopenharmony_ci#include "audio_napi.h"
2014cf0368Sopenharmony_ci#include "file_napi.h"
2114cf0368Sopenharmony_ci#include "folder_napi.h"
2214cf0368Sopenharmony_ci#include "html_napi.h"
2314cf0368Sopenharmony_ci#include "image_napi.h"
2414cf0368Sopenharmony_ci#include "link_napi.h"
2514cf0368Sopenharmony_ci#include "plain_text_napi.h"
2614cf0368Sopenharmony_ci#include "system_defined_appitem_napi.h"
2714cf0368Sopenharmony_ci#include "system_defined_form_napi.h"
2814cf0368Sopenharmony_ci#include "system_defined_pixelmap_napi.h"
2914cf0368Sopenharmony_ci#include "system_defined_record_napi.h"
3014cf0368Sopenharmony_ci#include "text_napi.h"
3114cf0368Sopenharmony_ci#include "unified_data.h"
3214cf0368Sopenharmony_ci#include "unified_record_napi.h"
3314cf0368Sopenharmony_ci#include "video_napi.h"
3414cf0368Sopenharmony_ci
3514cf0368Sopenharmony_cinamespace OHOS {
3614cf0368Sopenharmony_cinamespace UDMF {
3714cf0368Sopenharmony_cinapi_value UnifiedDataNapi::Constructor(napi_env env)
3814cf0368Sopenharmony_ci{
3914cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataNapi");
4014cf0368Sopenharmony_ci    napi_property_descriptor properties[] = {
4114cf0368Sopenharmony_ci        /* UnifiedData properties */
4214cf0368Sopenharmony_ci        DECLARE_NAPI_FUNCTION("addRecord", AddRecord),
4314cf0368Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getRecords", GetRecords),
4414cf0368Sopenharmony_ci        DECLARE_NAPI_FUNCTION("hasType", HasType),
4514cf0368Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getTypes", GetTypes),
4614cf0368Sopenharmony_ci        DECLARE_NAPI_GETTER_SETTER("properties", GetProperties, SetProperties),
4714cf0368Sopenharmony_ci    };
4814cf0368Sopenharmony_ci    size_t count = sizeof(properties) / sizeof(properties[0]);
4914cf0368Sopenharmony_ci    return NapiDataUtils::DefineClass(env, "UnifiedData", properties, count, UnifiedDataNapi::New);
5014cf0368Sopenharmony_ci}
5114cf0368Sopenharmony_ci
5214cf0368Sopenharmony_cinapi_value UnifiedDataNapi::New(napi_env env, napi_callback_info info)
5314cf0368Sopenharmony_ci{
5414cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataNapi");
5514cf0368Sopenharmony_ci    UnifiedRecordNapi *uRecord = nullptr;
5614cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
5714cf0368Sopenharmony_ci    auto input = [env, info, ctxt, &uRecord](size_t argc, napi_value *argv) {
5814cf0368Sopenharmony_ci        if (argc == 1) {
5914cf0368Sopenharmony_ci            ctxt->status = napi_unwrap(env, *argv, reinterpret_cast<void **>(&uRecord));
6014cf0368Sopenharmony_ci            ASSERT_BUSINESS_ERR(ctxt, (ctxt->status == napi_ok && uRecord != nullptr && uRecord->value_ != nullptr),
6114cf0368Sopenharmony_ci                Status::E_INVALID_PARAMETERS, "Parameter error: parameter record type must be UnifiedRecord");
6214cf0368Sopenharmony_ci            ctxt->status = UnifiedDataUtils::IsValidType(uRecord->value_->GetType()) ? napi_ok : napi_invalid_arg;
6314cf0368Sopenharmony_ci            ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::E_ERROR, "invalid type!");
6414cf0368Sopenharmony_ci        }
6514cf0368Sopenharmony_ci    };
6614cf0368Sopenharmony_ci
6714cf0368Sopenharmony_ci    // Parsing input parameters
6814cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info, input);
6914cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
7014cf0368Sopenharmony_ci    auto *uData = new (std::nothrow) UnifiedDataNapi();
7114cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, uData != nullptr, Status::E_ERROR, "no memory for unified data!");
7214cf0368Sopenharmony_ci
7314cf0368Sopenharmony_ci    // Create and bind UnifiedDataPropertiesNapi
7414cf0368Sopenharmony_ci    int argc = 0;
7514cf0368Sopenharmony_ci    napi_value argv[0] = {};
7614cf0368Sopenharmony_ci    UnifiedDataPropertiesNapi* propertiesNapi = nullptr;
7714cf0368Sopenharmony_ci    uData->propertyRef_ = NewWithRef(env, argc, argv, reinterpret_cast<void **>(&propertiesNapi),
7814cf0368Sopenharmony_ci        UnifiedDataPropertiesNapi::Constructor(env));
7914cf0368Sopenharmony_ci    uData->value_ = std::make_shared<UnifiedData>(propertiesNapi->value_);
8014cf0368Sopenharmony_ci    if (uRecord) {
8114cf0368Sopenharmony_ci        uData->value_->AddRecord(uRecord->value_);
8214cf0368Sopenharmony_ci    }
8314cf0368Sopenharmony_ci    ASSERT_CALL(env, napi_wrap(env, ctxt->self, uData, Destructor, nullptr, nullptr), uData);
8414cf0368Sopenharmony_ci    return ctxt->self;
8514cf0368Sopenharmony_ci}
8614cf0368Sopenharmony_ci
8714cf0368Sopenharmony_civoid UnifiedDataNapi::Destructor(napi_env env, void *data, void *hint)
8814cf0368Sopenharmony_ci{
8914cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataNapi finalize.");
9014cf0368Sopenharmony_ci    auto *uData = static_cast<UnifiedDataNapi *>(data);
9114cf0368Sopenharmony_ci    ASSERT_VOID(uData != nullptr, "finalize null!");
9214cf0368Sopenharmony_ci    if (uData->propertyRef_ != nullptr) {
9314cf0368Sopenharmony_ci        napi_delete_reference(env, uData->propertyRef_);
9414cf0368Sopenharmony_ci    }
9514cf0368Sopenharmony_ci    delete uData;
9614cf0368Sopenharmony_ci}
9714cf0368Sopenharmony_ci
9814cf0368Sopenharmony_cinapi_status UnifiedDataNapi::NewInstance(napi_env env, std::shared_ptr<UnifiedData> in, napi_value &out)
9914cf0368Sopenharmony_ci{
10014cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataNapi");
10114cf0368Sopenharmony_ci    ASSERT_CALL_STATUS(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
10214cf0368Sopenharmony_ci    auto *unifiedData = new (std::nothrow) UnifiedDataNapi();
10314cf0368Sopenharmony_ci    ASSERT_ERR_STATUS(env, unifiedData != nullptr, Status::E_ERROR, "no memory for unified data!");
10414cf0368Sopenharmony_ci    unifiedData->value_ = in;
10514cf0368Sopenharmony_ci    ASSERT_CALL_DELETE_STATUS(env, napi_wrap(env, out, unifiedData, Destructor, nullptr, nullptr), unifiedData);
10614cf0368Sopenharmony_ci    return napi_ok;
10714cf0368Sopenharmony_ci}
10814cf0368Sopenharmony_ci
10914cf0368Sopenharmony_ciUnifiedDataNapi *UnifiedDataNapi::GetUnifiedData(napi_env env, napi_callback_info info)
11014cf0368Sopenharmony_ci{
11114cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataNapi");
11214cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
11314cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info);
11414cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
11514cf0368Sopenharmony_ci    return static_cast<UnifiedDataNapi *>(ctxt->native);
11614cf0368Sopenharmony_ci}
11714cf0368Sopenharmony_ci
11814cf0368Sopenharmony_cinapi_value UnifiedDataNapi::AddRecord(napi_env env, napi_callback_info info)
11914cf0368Sopenharmony_ci{
12014cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataNapi");
12114cf0368Sopenharmony_ci    UnifiedRecordNapi *uRecord = nullptr;
12214cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
12314cf0368Sopenharmony_ci    auto input = [env, info, ctxt, &uRecord](size_t argc, napi_value *argv) {
12414cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
12514cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
12614cf0368Sopenharmony_ci        ctxt->status = napi_unwrap(env, *argv, reinterpret_cast<void **>(&uRecord));
12714cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, (ctxt->status == napi_ok && uRecord != nullptr && uRecord->value_ != nullptr),
12814cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: parameter record type must be UnifiedRecord");
12914cf0368Sopenharmony_ci        ctxt->status = UnifiedDataUtils::IsValidType(uRecord->value_->GetType()) ? napi_ok : napi_invalid_arg;
13014cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::E_ERROR, "invalid type!");
13114cf0368Sopenharmony_ci    };
13214cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info, input);
13314cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
13414cf0368Sopenharmony_ci    auto *uData = static_cast<UnifiedDataNapi *>(ctxt->native);
13514cf0368Sopenharmony_ci    ASSERT_ERR(
13614cf0368Sopenharmony_ci        ctxt->env, (uData != nullptr && uData->value_ != nullptr), Status::E_ERROR, "invalid object!");
13714cf0368Sopenharmony_ci    uData->value_->AddRecord(uRecord->value_);
13814cf0368Sopenharmony_ci    return nullptr;
13914cf0368Sopenharmony_ci}
14014cf0368Sopenharmony_ci
14114cf0368Sopenharmony_cinapi_value UnifiedDataNapi::GetRecords(napi_env env, napi_callback_info info)
14214cf0368Sopenharmony_ci{
14314cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataNapi");
14414cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
14514cf0368Sopenharmony_ci    auto uData = GetUnifiedData(env, info);
14614cf0368Sopenharmony_ci    ASSERT_ERR(
14714cf0368Sopenharmony_ci        ctxt->env, (uData != nullptr && uData->value_ != nullptr), Status::E_ERROR, "invalid object!");
14814cf0368Sopenharmony_ci    std::vector<std::shared_ptr<UnifiedRecord>> records = uData->value_->GetRecords();
14914cf0368Sopenharmony_ci    napi_status status = napi_create_array_with_length(env, records.size(), &ctxt->output);
15014cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, status == napi_ok, Status::E_ERROR, "init array failed!");
15114cf0368Sopenharmony_ci    int index = 0;
15214cf0368Sopenharmony_ci    for (const std::shared_ptr<UnifiedRecord> &recordPtr : records) {
15314cf0368Sopenharmony_ci        napi_value recordNapi = nullptr;
15414cf0368Sopenharmony_ci        GetRecord(env, recordPtr, recordNapi);
15514cf0368Sopenharmony_ci        ctxt->status = napi_set_element(env, ctxt->output, index++, recordNapi);
15614cf0368Sopenharmony_ci        ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set element failed!");
15714cf0368Sopenharmony_ci    }
15814cf0368Sopenharmony_ci    return ctxt->output;
15914cf0368Sopenharmony_ci}
16014cf0368Sopenharmony_ci
16114cf0368Sopenharmony_civoid UnifiedDataNapi::GetRecord(napi_env env, std::shared_ptr<UnifiedRecord> in, napi_value &out)
16214cf0368Sopenharmony_ci{
16314cf0368Sopenharmony_ci    switch (in->GetType()) {
16414cf0368Sopenharmony_ci        case TEXT: {
16514cf0368Sopenharmony_ci            TextNapi::NewInstance(env, in, out);
16614cf0368Sopenharmony_ci            break;
16714cf0368Sopenharmony_ci        }
16814cf0368Sopenharmony_ci        case PLAIN_TEXT: {
16914cf0368Sopenharmony_ci            PlainTextNapi::NewInstance(env, in, out);
17014cf0368Sopenharmony_ci            break;
17114cf0368Sopenharmony_ci        }
17214cf0368Sopenharmony_ci        case HTML: {
17314cf0368Sopenharmony_ci            HtmlNapi::NewInstance(env, in, out);
17414cf0368Sopenharmony_ci            break;
17514cf0368Sopenharmony_ci        }
17614cf0368Sopenharmony_ci        case HYPERLINK: {
17714cf0368Sopenharmony_ci            LinkNapi::NewInstance(env, in, out);
17814cf0368Sopenharmony_ci            break;
17914cf0368Sopenharmony_ci        }
18014cf0368Sopenharmony_ci        case FILE: {
18114cf0368Sopenharmony_ci            FileNapi::NewInstance(env, in, out);
18214cf0368Sopenharmony_ci            break;
18314cf0368Sopenharmony_ci        }
18414cf0368Sopenharmony_ci        case IMAGE: {
18514cf0368Sopenharmony_ci            ImageNapi::NewInstance(env, in, out);
18614cf0368Sopenharmony_ci            break;
18714cf0368Sopenharmony_ci        }
18814cf0368Sopenharmony_ci        case VIDEO: {
18914cf0368Sopenharmony_ci            VideoNapi::NewInstance(env, in, out);
19014cf0368Sopenharmony_ci            break;
19114cf0368Sopenharmony_ci        }
19214cf0368Sopenharmony_ci        case AUDIO: {
19314cf0368Sopenharmony_ci            AudioNapi::NewInstance(env, in, out);
19414cf0368Sopenharmony_ci            break;
19514cf0368Sopenharmony_ci        }
19614cf0368Sopenharmony_ci        case FOLDER: {
19714cf0368Sopenharmony_ci            FolderNapi::NewInstance(env, in, out);
19814cf0368Sopenharmony_ci            break;
19914cf0368Sopenharmony_ci        }
20014cf0368Sopenharmony_ci        case SYSTEM_DEFINED_RECORD: {
20114cf0368Sopenharmony_ci            SystemDefinedRecordNapi::NewInstance(env, in, out);
20214cf0368Sopenharmony_ci            break;
20314cf0368Sopenharmony_ci        }
20414cf0368Sopenharmony_ci        case SYSTEM_DEFINED_APP_ITEM: {
20514cf0368Sopenharmony_ci            SystemDefinedAppItemNapi::NewInstance(env, in, out);
20614cf0368Sopenharmony_ci            break;
20714cf0368Sopenharmony_ci        }
20814cf0368Sopenharmony_ci        case SYSTEM_DEFINED_FORM: {
20914cf0368Sopenharmony_ci            SystemDefinedFormNapi::NewInstance(env, in, out);
21014cf0368Sopenharmony_ci            break;
21114cf0368Sopenharmony_ci        }
21214cf0368Sopenharmony_ci        case SYSTEM_DEFINED_PIXEL_MAP: {
21314cf0368Sopenharmony_ci            auto value = in->GetValue();
21414cf0368Sopenharmony_ci            if (std::get_if<std::shared_ptr<OHOS::Media::PixelMap>>(&value)) {
21514cf0368Sopenharmony_ci                UnifiedRecordNapi::NewInstance(env, in, out);
21614cf0368Sopenharmony_ci            } else {
21714cf0368Sopenharmony_ci                SystemDefinedPixelMapNapi::NewInstance(env, in, out);
21814cf0368Sopenharmony_ci            }
21914cf0368Sopenharmony_ci            break;
22014cf0368Sopenharmony_ci        }
22114cf0368Sopenharmony_ci        case APPLICATION_DEFINED_RECORD: {
22214cf0368Sopenharmony_ci            ApplicationDefinedRecordNapi::NewInstance(env, in, out);
22314cf0368Sopenharmony_ci            break;
22414cf0368Sopenharmony_ci        }
22514cf0368Sopenharmony_ci        default:
22614cf0368Sopenharmony_ci            UnifiedRecordNapi::NewInstance(env, in, out);
22714cf0368Sopenharmony_ci            LOG_INFO(UDMF_KITS_NAPI, "GetRecord default");
22814cf0368Sopenharmony_ci            break;
22914cf0368Sopenharmony_ci    }
23014cf0368Sopenharmony_ci}
23114cf0368Sopenharmony_ci
23214cf0368Sopenharmony_cinapi_value UnifiedDataNapi::HasType(napi_env env, napi_callback_info info)
23314cf0368Sopenharmony_ci{
23414cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataNapi");
23514cf0368Sopenharmony_ci    std::string type;
23614cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
23714cf0368Sopenharmony_ci    auto input = [env, info, ctxt, &type](size_t argc, napi_value *argv) {
23814cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
23914cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
24014cf0368Sopenharmony_ci        ctxt->status = NapiDataUtils::GetValue(env, argv[0], type);
24114cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
24214cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: parameter type type must be string");
24314cf0368Sopenharmony_ci    };
24414cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info, input);
24514cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
24614cf0368Sopenharmony_ci    auto *uData = static_cast<UnifiedDataNapi *>(ctxt->native);
24714cf0368Sopenharmony_ci    ASSERT_ERR(
24814cf0368Sopenharmony_ci        ctxt->env, (uData != nullptr && uData->value_ != nullptr), Status::E_ERROR, "invalid object!");
24914cf0368Sopenharmony_ci    bool ret = uData->value_->HasType(type);
25014cf0368Sopenharmony_ci    napi_value result = nullptr;
25114cf0368Sopenharmony_ci    napi_get_boolean(env, ret, &result);
25214cf0368Sopenharmony_ci    return result;
25314cf0368Sopenharmony_ci}
25414cf0368Sopenharmony_ci
25514cf0368Sopenharmony_cinapi_value UnifiedDataNapi::GetTypes(napi_env env, napi_callback_info info)
25614cf0368Sopenharmony_ci{
25714cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataNapi");
25814cf0368Sopenharmony_ci    auto uData = GetUnifiedData(env, info);
25914cf0368Sopenharmony_ci    ASSERT_ERR(env, (uData != nullptr && uData->value_ != nullptr), Status::E_ERROR, "invalid object!");
26014cf0368Sopenharmony_ci
26114cf0368Sopenharmony_ci    std::vector<std::string> Types = uData->value_->GetTypesLabels();
26214cf0368Sopenharmony_ci    napi_value nTypes = nullptr;
26314cf0368Sopenharmony_ci    napi_status status = napi_create_array(env, &nTypes);
26414cf0368Sopenharmony_ci    ASSERT_ERR(env, status == napi_ok, Status::E_ERROR, "create array fail!");
26514cf0368Sopenharmony_ci
26614cf0368Sopenharmony_ci    size_t index = 0;
26714cf0368Sopenharmony_ci    napi_value value = nullptr;
26814cf0368Sopenharmony_ci    for (auto type : Types) {
26914cf0368Sopenharmony_ci        napi_create_string_utf8(env, type.c_str(), NAPI_AUTO_LENGTH, &value);
27014cf0368Sopenharmony_ci        napi_set_element(env, nTypes, index, value);
27114cf0368Sopenharmony_ci        index++;
27214cf0368Sopenharmony_ci    }
27314cf0368Sopenharmony_ci    return nTypes;
27414cf0368Sopenharmony_ci}
27514cf0368Sopenharmony_ci
27614cf0368Sopenharmony_cinapi_value UnifiedDataNapi::GetProperties(napi_env env, napi_callback_info info)
27714cf0368Sopenharmony_ci{
27814cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataNapi");
27914cf0368Sopenharmony_ci    napi_value value;
28014cf0368Sopenharmony_ci    auto uData = GetUnifiedData(env, info);
28114cf0368Sopenharmony_ci    ASSERT_ERR(env, (uData != nullptr && uData->value_ != nullptr), Status::E_ERROR, "invalid object!");
28214cf0368Sopenharmony_ci    NAPI_CALL(env, napi_get_reference_value(env, uData->propertyRef_, &value));
28314cf0368Sopenharmony_ci    return value;
28414cf0368Sopenharmony_ci}
28514cf0368Sopenharmony_ci
28614cf0368Sopenharmony_ciUnifiedDataPropertiesNapi* UnifiedDataNapi::GetPropertiesNapi(napi_env env)
28714cf0368Sopenharmony_ci{
28814cf0368Sopenharmony_ci    napi_value value;
28914cf0368Sopenharmony_ci    NAPI_CALL(env, napi_get_reference_value(env, propertyRef_, &value));
29014cf0368Sopenharmony_ci    UnifiedDataPropertiesNapi* out = nullptr;
29114cf0368Sopenharmony_ci    napi_status status = napi_unwrap(env, value, reinterpret_cast<void**>(&out));
29214cf0368Sopenharmony_ci    ASSERT_ERR(env, status == napi_ok && out != nullptr, Status::E_ERROR, "napi_unwrap failed");
29314cf0368Sopenharmony_ci    return out;
29414cf0368Sopenharmony_ci}
29514cf0368Sopenharmony_ci
29614cf0368Sopenharmony_cinapi_value UnifiedDataNapi::SetProperties(napi_env env, napi_callback_info info)
29714cf0368Sopenharmony_ci{
29814cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataNapi");
29914cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
30014cf0368Sopenharmony_ci    auto input = [env, ctxt](size_t argc, napi_value* argv) {
30114cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
30214cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
30314cf0368Sopenharmony_ci        UnifiedDataPropertiesNapi* propertiesNapi = nullptr;
30414cf0368Sopenharmony_ci        ctxt->status = Unwrap(env, argv[0], reinterpret_cast<void**>(&propertiesNapi),
30514cf0368Sopenharmony_ci            UnifiedDataPropertiesNapi::Constructor(env));
30614cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, propertiesNapi != nullptr,
30714cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: parameter properties type must be UnifiedDataProperties");
30814cf0368Sopenharmony_ci
30914cf0368Sopenharmony_ci        auto uData = static_cast<UnifiedDataNapi*>(ctxt->native);
31014cf0368Sopenharmony_ci        if (uData->propertyRef_ != nullptr) {
31114cf0368Sopenharmony_ci            napi_delete_reference(env, uData->propertyRef_);
31214cf0368Sopenharmony_ci        }
31314cf0368Sopenharmony_ci        ctxt->status = napi_create_reference(env, argv[0], 1, &uData->propertyRef_);
31414cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
31514cf0368Sopenharmony_ci            Status::E_ERROR, "napi_create_reference to properties failed!");
31614cf0368Sopenharmony_ci        uData->value_->SetProperties(propertiesNapi->value_);
31714cf0368Sopenharmony_ci    };
31814cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info, input);
31914cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
32014cf0368Sopenharmony_ci    return ctxt->self;
32114cf0368Sopenharmony_ci}
32214cf0368Sopenharmony_ci
32314cf0368Sopenharmony_cinapi_ref UnifiedDataNapi::NewWithRef(napi_env env, size_t argc, napi_value *argv, void **out, napi_value constructor)
32414cf0368Sopenharmony_ci{
32514cf0368Sopenharmony_ci    napi_value object = nullptr;
32614cf0368Sopenharmony_ci    napi_status status = napi_new_instance(env, constructor, argc, argv, &object);
32714cf0368Sopenharmony_ci    ASSERT_ERR(env, status == napi_ok && object != nullptr, Status::E_ERROR, "napi_new_instance failed");
32814cf0368Sopenharmony_ci
32914cf0368Sopenharmony_ci    status = napi_unwrap(env, object, out);
33014cf0368Sopenharmony_ci    ASSERT_ERR(env, status == napi_ok && out != nullptr, Status::E_ERROR, "napi_unwrap failed");
33114cf0368Sopenharmony_ci
33214cf0368Sopenharmony_ci    napi_ref ref = nullptr;
33314cf0368Sopenharmony_ci    status = napi_create_reference(env, object, 1, &ref);
33414cf0368Sopenharmony_ci    ASSERT_ERR(env, status == napi_ok && ref != nullptr, Status::E_ERROR, "napi_create_reference failed");
33514cf0368Sopenharmony_ci    return ref;
33614cf0368Sopenharmony_ci}
33714cf0368Sopenharmony_ci
33814cf0368Sopenharmony_cinapi_status UnifiedDataNapi::Unwrap(napi_env env, napi_value in, void **out, napi_value constructor)
33914cf0368Sopenharmony_ci{
34014cf0368Sopenharmony_ci    if (constructor != nullptr) {
34114cf0368Sopenharmony_ci        bool isInstance = false;
34214cf0368Sopenharmony_ci        napi_instanceof(env, in, constructor, &isInstance);
34314cf0368Sopenharmony_ci        if (!isInstance) {
34414cf0368Sopenharmony_ci            LOG_ERROR(UDMF_KITS_NAPI, "not a instance of *");
34514cf0368Sopenharmony_ci            return napi_invalid_arg;
34614cf0368Sopenharmony_ci        }
34714cf0368Sopenharmony_ci    }
34814cf0368Sopenharmony_ci    return napi_unwrap(env, in, out);
34914cf0368Sopenharmony_ci}
35014cf0368Sopenharmony_ci} // namespace UDMF
35114cf0368Sopenharmony_ci} // namespace OHOS