1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 #define LOG_TAG "SummaryNapi"
16 #include "summary_napi.h"
17 
18 #include "napi_data_utils.h"
19 #include "unified_types.h"
20 
21 namespace OHOS {
22 namespace UDMF {
Constructor(napi_env env)23 napi_value SummaryNapi::Constructor(napi_env env)
24 {
25     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi");
26     napi_property_descriptor properties[] = {
27         DECLARE_NAPI_GETTER_SETTER("summary", GetSummary, nullptr),
28         DECLARE_NAPI_GETTER_SETTER("totalSize", GetTotal, nullptr),
29     };
30     size_t count = sizeof(properties) / sizeof(properties[0]);
31     return NapiDataUtils::DefineClass(env, "Summary", properties, count, SummaryNapi::New);
32 }
33 
New(napi_env env,napi_callback_info info)34 napi_value SummaryNapi::New(napi_env env, napi_callback_info info)
35 {
36     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi");
37     auto ctxt = std::make_shared<ContextBase>();
38     ctxt->GetCbInfoSync(env, info);
39     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
40 
41     auto *summary = new (std::nothrow) SummaryNapi();
42     ASSERT_ERR(ctxt->env, summary != nullptr, Status::E_ERROR, "no memory for summary!");
43     summary->value_ = std::make_shared<Summary>();
44     ASSERT_CALL(env, napi_wrap(env, ctxt->self, summary, Destructor, nullptr, nullptr), summary);
45     return ctxt->self;
46 }
47 
Destructor(napi_env env,void * data,void * hint)48 void SummaryNapi::Destructor(napi_env env, void *data, void *hint)
49 {
50     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi finalize.");
51     auto *summary = static_cast<SummaryNapi *>(data);
52     ASSERT_VOID(summary != nullptr, "finalize null!");
53     delete summary;
54 }
55 
NewInstance(napi_env env,std::shared_ptr<Summary> in,napi_value & out)56 void SummaryNapi::NewInstance(napi_env env, std::shared_ptr<Summary> in, napi_value &out)
57 {
58     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi");
59     ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
60     auto *summary = new (std::nothrow) SummaryNapi();
61     ASSERT_ERR_VOID(env, summary != nullptr, Status::E_ERROR, "no memory for summary!");
62     summary->value_ = in;
63     ASSERT_CALL_DELETE(env, napi_wrap(env, out, summary, Destructor, nullptr, nullptr), summary);
64 }
65 
GetDataSummary(napi_env env,napi_callback_info info,std::shared_ptr<ContextBase> ctxt)66 SummaryNapi *SummaryNapi::GetDataSummary(napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
67 {
68     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi");
69     ctxt->GetCbInfoSync(env, info);
70     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
71     return static_cast<SummaryNapi *>(ctxt->native);
72 }
73 
GetSummary(napi_env env,napi_callback_info info)74 napi_value SummaryNapi::GetSummary(napi_env env, napi_callback_info info)
75 {
76     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi");
77     auto ctxt = std::make_shared<ContextBase>();
78     auto summary = GetDataSummary(env, info, ctxt);
79     ASSERT_ERR(ctxt->env, (summary != nullptr && summary->value_ != nullptr), Status::E_ERROR,
80         "invalid object!");
81     ctxt->status = NapiDataUtils::SetValue(env, summary->value_->summary, ctxt->output);
82     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set summery failed!");
83     return ctxt->output;
84 }
85 
GetTotal(napi_env env,napi_callback_info info)86 napi_value SummaryNapi::GetTotal(napi_env env, napi_callback_info info)
87 {
88     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi");
89     auto ctxt = std::make_shared<ContextBase>();
90     auto summary = GetDataSummary(env, info, ctxt);
91     ASSERT_ERR(ctxt->env, (summary != nullptr && summary->value_ != nullptr), Status::E_ERROR,
92         "invalid object!");
93     ctxt->status = NapiDataUtils::SetValue(env, summary->value_->totalSize, ctxt->output);
94     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set total failed!");
95     return ctxt->output;
96 }
97 } // namespace UDMF
98 } // namespace OHOS