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 "SummaryNapi" 1614cf0368Sopenharmony_ci#include "summary_napi.h" 1714cf0368Sopenharmony_ci 1814cf0368Sopenharmony_ci#include "napi_data_utils.h" 1914cf0368Sopenharmony_ci#include "unified_types.h" 2014cf0368Sopenharmony_ci 2114cf0368Sopenharmony_cinamespace OHOS { 2214cf0368Sopenharmony_cinamespace UDMF { 2314cf0368Sopenharmony_cinapi_value SummaryNapi::Constructor(napi_env env) 2414cf0368Sopenharmony_ci{ 2514cf0368Sopenharmony_ci LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi"); 2614cf0368Sopenharmony_ci napi_property_descriptor properties[] = { 2714cf0368Sopenharmony_ci DECLARE_NAPI_GETTER_SETTER("summary", GetSummary, nullptr), 2814cf0368Sopenharmony_ci DECLARE_NAPI_GETTER_SETTER("totalSize", GetTotal, nullptr), 2914cf0368Sopenharmony_ci }; 3014cf0368Sopenharmony_ci size_t count = sizeof(properties) / sizeof(properties[0]); 3114cf0368Sopenharmony_ci return NapiDataUtils::DefineClass(env, "Summary", properties, count, SummaryNapi::New); 3214cf0368Sopenharmony_ci} 3314cf0368Sopenharmony_ci 3414cf0368Sopenharmony_cinapi_value SummaryNapi::New(napi_env env, napi_callback_info info) 3514cf0368Sopenharmony_ci{ 3614cf0368Sopenharmony_ci LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi"); 3714cf0368Sopenharmony_ci auto ctxt = std::make_shared<ContextBase>(); 3814cf0368Sopenharmony_ci ctxt->GetCbInfoSync(env, info); 3914cf0368Sopenharmony_ci ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error); 4014cf0368Sopenharmony_ci 4114cf0368Sopenharmony_ci auto *summary = new (std::nothrow) SummaryNapi(); 4214cf0368Sopenharmony_ci ASSERT_ERR(ctxt->env, summary != nullptr, Status::E_ERROR, "no memory for summary!"); 4314cf0368Sopenharmony_ci summary->value_ = std::make_shared<Summary>(); 4414cf0368Sopenharmony_ci ASSERT_CALL(env, napi_wrap(env, ctxt->self, summary, Destructor, nullptr, nullptr), summary); 4514cf0368Sopenharmony_ci return ctxt->self; 4614cf0368Sopenharmony_ci} 4714cf0368Sopenharmony_ci 4814cf0368Sopenharmony_civoid SummaryNapi::Destructor(napi_env env, void *data, void *hint) 4914cf0368Sopenharmony_ci{ 5014cf0368Sopenharmony_ci LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi finalize."); 5114cf0368Sopenharmony_ci auto *summary = static_cast<SummaryNapi *>(data); 5214cf0368Sopenharmony_ci ASSERT_VOID(summary != nullptr, "finalize null!"); 5314cf0368Sopenharmony_ci delete summary; 5414cf0368Sopenharmony_ci} 5514cf0368Sopenharmony_ci 5614cf0368Sopenharmony_civoid SummaryNapi::NewInstance(napi_env env, std::shared_ptr<Summary> in, napi_value &out) 5714cf0368Sopenharmony_ci{ 5814cf0368Sopenharmony_ci LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi"); 5914cf0368Sopenharmony_ci ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out)); 6014cf0368Sopenharmony_ci auto *summary = new (std::nothrow) SummaryNapi(); 6114cf0368Sopenharmony_ci ASSERT_ERR_VOID(env, summary != nullptr, Status::E_ERROR, "no memory for summary!"); 6214cf0368Sopenharmony_ci summary->value_ = in; 6314cf0368Sopenharmony_ci ASSERT_CALL_DELETE(env, napi_wrap(env, out, summary, Destructor, nullptr, nullptr), summary); 6414cf0368Sopenharmony_ci} 6514cf0368Sopenharmony_ci 6614cf0368Sopenharmony_ciSummaryNapi *SummaryNapi::GetDataSummary(napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt) 6714cf0368Sopenharmony_ci{ 6814cf0368Sopenharmony_ci LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi"); 6914cf0368Sopenharmony_ci ctxt->GetCbInfoSync(env, info); 7014cf0368Sopenharmony_ci ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error); 7114cf0368Sopenharmony_ci return static_cast<SummaryNapi *>(ctxt->native); 7214cf0368Sopenharmony_ci} 7314cf0368Sopenharmony_ci 7414cf0368Sopenharmony_cinapi_value SummaryNapi::GetSummary(napi_env env, napi_callback_info info) 7514cf0368Sopenharmony_ci{ 7614cf0368Sopenharmony_ci LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi"); 7714cf0368Sopenharmony_ci auto ctxt = std::make_shared<ContextBase>(); 7814cf0368Sopenharmony_ci auto summary = GetDataSummary(env, info, ctxt); 7914cf0368Sopenharmony_ci ASSERT_ERR(ctxt->env, (summary != nullptr && summary->value_ != nullptr), Status::E_ERROR, 8014cf0368Sopenharmony_ci "invalid object!"); 8114cf0368Sopenharmony_ci ctxt->status = NapiDataUtils::SetValue(env, summary->value_->summary, ctxt->output); 8214cf0368Sopenharmony_ci ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set summery failed!"); 8314cf0368Sopenharmony_ci return ctxt->output; 8414cf0368Sopenharmony_ci} 8514cf0368Sopenharmony_ci 8614cf0368Sopenharmony_cinapi_value SummaryNapi::GetTotal(napi_env env, napi_callback_info info) 8714cf0368Sopenharmony_ci{ 8814cf0368Sopenharmony_ci LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi"); 8914cf0368Sopenharmony_ci auto ctxt = std::make_shared<ContextBase>(); 9014cf0368Sopenharmony_ci auto summary = GetDataSummary(env, info, ctxt); 9114cf0368Sopenharmony_ci ASSERT_ERR(ctxt->env, (summary != nullptr && summary->value_ != nullptr), Status::E_ERROR, 9214cf0368Sopenharmony_ci "invalid object!"); 9314cf0368Sopenharmony_ci ctxt->status = NapiDataUtils::SetValue(env, summary->value_->totalSize, ctxt->output); 9414cf0368Sopenharmony_ci ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set total failed!"); 9514cf0368Sopenharmony_ci return ctxt->output; 9614cf0368Sopenharmony_ci} 9714cf0368Sopenharmony_ci} // namespace UDMF 9814cf0368Sopenharmony_ci} // namespace OHOS