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 "FileNapi"
1614cf0368Sopenharmony_ci#include "file_napi.h"
1714cf0368Sopenharmony_ci
1814cf0368Sopenharmony_ci#include "file.h"
1914cf0368Sopenharmony_ci#include "unified_record_napi.h"
2014cf0368Sopenharmony_ci
2114cf0368Sopenharmony_cinamespace OHOS {
2214cf0368Sopenharmony_cinamespace UDMF {
2314cf0368Sopenharmony_cinapi_value FileNapi::Constructor(napi_env env)
2414cf0368Sopenharmony_ci{
2514cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "FileNapi");
2614cf0368Sopenharmony_ci    napi_property_descriptor properties[] = {
2714cf0368Sopenharmony_ci        /* File extends UnifiedRecord */
2814cf0368Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getType", UnifiedRecordNapi::GetType),
2914cf0368Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getValue", UnifiedRecordNapi::GetValue),
3014cf0368Sopenharmony_ci        /* File properties */
3114cf0368Sopenharmony_ci        DECLARE_NAPI_GETTER_SETTER("details", GetDetails, SetDetails),
3214cf0368Sopenharmony_ci        DECLARE_NAPI_GETTER_SETTER("uri", GetUri, SetUri),
3314cf0368Sopenharmony_ci    };
3414cf0368Sopenharmony_ci    size_t count = sizeof(properties) / sizeof(properties[0]);
3514cf0368Sopenharmony_ci    return NapiDataUtils::DefineClass(env, "File", properties, count, FileNapi::New);
3614cf0368Sopenharmony_ci}
3714cf0368Sopenharmony_ci
3814cf0368Sopenharmony_cinapi_value FileNapi::New(napi_env env, napi_callback_info info)
3914cf0368Sopenharmony_ci{
4014cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "FileNapi");
4114cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
4214cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info);
4314cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
4414cf0368Sopenharmony_ci
4514cf0368Sopenharmony_ci    auto *file = new (std::nothrow) FileNapi();
4614cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, file != nullptr, Status::E_ERROR, "no memory for file!");
4714cf0368Sopenharmony_ci    file->value_ = std::make_shared<File>();
4814cf0368Sopenharmony_ci    ASSERT_CALL(env, napi_wrap(env, ctxt->self, file, Destructor, nullptr, nullptr), file);
4914cf0368Sopenharmony_ci    return ctxt->self;
5014cf0368Sopenharmony_ci}
5114cf0368Sopenharmony_ci
5214cf0368Sopenharmony_civoid FileNapi::NewInstance(napi_env env, std::shared_ptr<UnifiedRecord> in, napi_value &out)
5314cf0368Sopenharmony_ci{
5414cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "FileNapi");
5514cf0368Sopenharmony_ci    ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
5614cf0368Sopenharmony_ci    auto *file = new (std::nothrow) FileNapi();
5714cf0368Sopenharmony_ci    ASSERT_ERR_VOID(env, file != nullptr, Status::E_ERROR, "no memory for file!");
5814cf0368Sopenharmony_ci    file->value_ = std::static_pointer_cast<File>(in);
5914cf0368Sopenharmony_ci    ASSERT_CALL_DELETE(env, napi_wrap(env, out, file, Destructor, nullptr, nullptr), file);
6014cf0368Sopenharmony_ci}
6114cf0368Sopenharmony_ci
6214cf0368Sopenharmony_civoid FileNapi::Destructor(napi_env env, void *data, void *hint)
6314cf0368Sopenharmony_ci{
6414cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "FileNapi finalize.");
6514cf0368Sopenharmony_ci    auto *file = static_cast<FileNapi *>(data);
6614cf0368Sopenharmony_ci    ASSERT_VOID(file != nullptr, "finalize null!");
6714cf0368Sopenharmony_ci    delete file;
6814cf0368Sopenharmony_ci}
6914cf0368Sopenharmony_ci
7014cf0368Sopenharmony_ciFileNapi *FileNapi::GetFile(napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
7114cf0368Sopenharmony_ci{
7214cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "FileNapi");
7314cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info);
7414cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
7514cf0368Sopenharmony_ci    return static_cast<FileNapi *>(ctxt->native);
7614cf0368Sopenharmony_ci}
7714cf0368Sopenharmony_ci
7814cf0368Sopenharmony_cinapi_value FileNapi::GetDetails(napi_env env, napi_callback_info info)
7914cf0368Sopenharmony_ci{
8014cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "FileNapi");
8114cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
8214cf0368Sopenharmony_ci    auto file = GetFile(env, info, ctxt);
8314cf0368Sopenharmony_ci    ASSERT_ERR(
8414cf0368Sopenharmony_ci        ctxt->env, (file != nullptr && file->value_ != nullptr), Status::E_ERROR, "invalid object!");
8514cf0368Sopenharmony_ci    ctxt->status = NapiDataUtils::SetValue(env, file->value_->GetDetails(), ctxt->output);
8614cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set details failed!");
8714cf0368Sopenharmony_ci    return ctxt->output;
8814cf0368Sopenharmony_ci}
8914cf0368Sopenharmony_ci
9014cf0368Sopenharmony_cinapi_value FileNapi::SetDetails(napi_env env, napi_callback_info info)
9114cf0368Sopenharmony_ci{
9214cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "FileNapi");
9314cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
9414cf0368Sopenharmony_ci    UDDetails details;
9514cf0368Sopenharmony_ci    auto input = [env, ctxt, &details](size_t argc, napi_value *argv) {
9614cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
9714cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
9814cf0368Sopenharmony_ci        ctxt->status = NapiDataUtils::GetValue(env, argv[0], details);
9914cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
10014cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: parameter details type must be Record<string, string>");
10114cf0368Sopenharmony_ci    };
10214cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info, input);
10314cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
10414cf0368Sopenharmony_ci    auto file = static_cast<FileNapi *>(ctxt->native);
10514cf0368Sopenharmony_ci    ASSERT_ERR(
10614cf0368Sopenharmony_ci        ctxt->env, (file != nullptr && file->value_ != nullptr), Status::E_ERROR, "invalid object!");
10714cf0368Sopenharmony_ci    file->value_->SetDetails(details);
10814cf0368Sopenharmony_ci    return nullptr;
10914cf0368Sopenharmony_ci}
11014cf0368Sopenharmony_ci
11114cf0368Sopenharmony_cinapi_value FileNapi::GetUri(napi_env env, napi_callback_info info)
11214cf0368Sopenharmony_ci{
11314cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "FileNapi");
11414cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
11514cf0368Sopenharmony_ci    auto file = GetFile(env, info, ctxt);
11614cf0368Sopenharmony_ci    ASSERT_ERR(
11714cf0368Sopenharmony_ci        ctxt->env, (file != nullptr && file->value_ != nullptr), Status::E_ERROR, "invalid object!");
11814cf0368Sopenharmony_ci    ctxt->status = NapiDataUtils::SetValue(env, file->value_->GetUri(), ctxt->output);
11914cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set uri failed!");
12014cf0368Sopenharmony_ci    return ctxt->output;
12114cf0368Sopenharmony_ci}
12214cf0368Sopenharmony_ci
12314cf0368Sopenharmony_cinapi_value FileNapi::SetUri(napi_env env, napi_callback_info info)
12414cf0368Sopenharmony_ci{
12514cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "FileNapi");
12614cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
12714cf0368Sopenharmony_ci    std::string uri;
12814cf0368Sopenharmony_ci    auto input = [env, ctxt, &uri](size_t argc, napi_value *argv) {
12914cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
13014cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
13114cf0368Sopenharmony_ci        ctxt->status = NapiDataUtils::GetValue(env, argv[0], uri);
13214cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
13314cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: parameter uri type must be string");
13414cf0368Sopenharmony_ci    };
13514cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info, input);
13614cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
13714cf0368Sopenharmony_ci    auto file = static_cast<FileNapi *>(ctxt->native);
13814cf0368Sopenharmony_ci    ASSERT_ERR(
13914cf0368Sopenharmony_ci        ctxt->env, (file != nullptr && file->value_ != nullptr), Status::E_ERROR, "invalid object!");
14014cf0368Sopenharmony_ci    file->value_->SetUri(uri);
14114cf0368Sopenharmony_ci    return nullptr;
14214cf0368Sopenharmony_ci}
14314cf0368Sopenharmony_ci} // namespace UDMF
14414cf0368Sopenharmony_ci} // namespace OHOS