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