• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "summary_napi.h"
17 
18 #include "napi_data_utils.h"
19 #include "napi_error_utils.h"
20 #include "napi_queue.h"
21 #include "unified_types.h"
22 
23 namespace OHOS {
24 namespace UDMF {
Constructor(napi_env env)25 napi_value SummaryNapi::Constructor(napi_env env)
26 {
27     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi");
28     napi_property_descriptor properties[] = {
29         DECLARE_NAPI_GETTER_SETTER("summary", GetSummary, nullptr),
30         DECLARE_NAPI_GETTER_SETTER("totalSize", GetTotal, nullptr),
31     };
32     size_t count = sizeof(properties) / sizeof(properties[0]);
33     return NapiDataUtils::DefineClass(env, "Summary", properties, count, SummaryNapi::New);
34 }
35 
New(napi_env env,napi_callback_info info)36 napi_value SummaryNapi::New(napi_env env, napi_callback_info info)
37 {
38     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi");
39     auto ctxt = std::make_shared<ContextBase>();
40     ctxt->GetCbInfoSync(env, info);
41     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
42 
43     auto *summary = new (std::nothrow) SummaryNapi();
44     ASSERT_ERR(ctxt->env, summary != nullptr, Status::E_UNKNOWN, "no memory for summary!");
45     summary->value_ = std::make_shared<Summary>();
46     ASSERT_CALL(env, napi_wrap(env, ctxt->self, summary, Destructor, nullptr, nullptr), summary);
47     return ctxt->self;
48 }
49 
Destructor(napi_env env,void * data,void * hint)50 void SummaryNapi::Destructor(napi_env env, void *data, void *hint)
51 {
52     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi finalize.");
53     auto *summary = static_cast<SummaryNapi *>(data);
54     ASSERT_VOID(summary != nullptr, "finalize null!");
55     delete summary;
56 }
57 
NewInstance(napi_env env,std::shared_ptr<Summary> in,napi_value & out)58 void SummaryNapi::NewInstance(napi_env env, std::shared_ptr<Summary> in, napi_value &out)
59 {
60     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi");
61     ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
62     auto *summary = new (std::nothrow) SummaryNapi();
63     ASSERT_ERR_VOID(env, summary != nullptr, Status::E_UNKNOWN, "no memory for summary!");
64     summary->value_ = in;
65     ASSERT_CALL_DELETE(env, napi_wrap(env, out, summary, Destructor, nullptr, nullptr), summary);
66 }
67 
GetDataSummary(napi_env env,napi_callback_info info,std::shared_ptr<ContextBase> ctxt)68 SummaryNapi *SummaryNapi::GetDataSummary(napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
69 {
70     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi");
71     ctxt->GetCbInfoSync(env, info);
72     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "invalid arguments!");
73     return static_cast<SummaryNapi *>(ctxt->native);
74 }
75 
GetSummary(napi_env env,napi_callback_info info)76 napi_value SummaryNapi::GetSummary(napi_env env, napi_callback_info info)
77 {
78     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi");
79     auto ctxt = std::make_shared<ContextBase>();
80     auto summary = GetDataSummary(env, info, ctxt);
81     ASSERT_ERR(ctxt->env, (summary != nullptr && summary->value_ != nullptr), Status::E_INVALID_PARAMETERS,
82         "invalid object!");
83     ctxt->status = NapiDataUtils::SetValue(env, summary->value_->summary, ctxt->output);
84     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "set summery failed!");
85     return ctxt->output;
86 }
87 
GetTotal(napi_env env,napi_callback_info info)88 napi_value SummaryNapi::GetTotal(napi_env env, napi_callback_info info)
89 {
90     LOG_DEBUG(UDMF_KITS_NAPI, "SummaryNapi");
91     auto ctxt = std::make_shared<ContextBase>();
92     auto summary = GetDataSummary(env, info, ctxt);
93     ASSERT_ERR(ctxt->env, (summary != nullptr && summary->value_ != nullptr), Status::E_INVALID_PARAMETERS,
94         "invalid object!");
95     ctxt->status = NapiDataUtils::SetValue(env, summary->value_->totalSize, ctxt->output);
96     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_INVALID_PARAMETERS, "set total failed!");
97     return ctxt->output;
98 }
99 } // namespace UDMF
100 } // namespace OHOS