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 "SystemDefinedPixelMapNapi"
16 #include "system_defined_pixelmap_napi.h"
17 
18 #include "system_defined_pixelmap.h"
19 #include "unified_record_napi.h"
20 #include "system_defined_record_napi.h"
21 
22 namespace OHOS {
23 namespace UDMF {
Constructor(napi_env env)24 napi_value SystemDefinedPixelMapNapi::Constructor(napi_env env)
25 {
26     LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedPixelMapNapi");
27     napi_property_descriptor properties[] = {
28         /* SystemDefinedPixelMap extends UnifiedRecord */
29         DECLARE_NAPI_FUNCTION("getType", UnifiedRecordNapi::GetType),
30         DECLARE_NAPI_FUNCTION("getValue", UnifiedRecordNapi::GetValue),
31         /* SystemDefinedPixelMap extends SystemDefinedRecord */
32         DECLARE_NAPI_GETTER_SETTER("details", SystemDefinedRecordNapi::GetDetails, SystemDefinedRecordNapi::SetDetails),
33         /* SystemDefinedPixelMap properties */
34         DECLARE_NAPI_GETTER_SETTER("rawData", GetRawData, SetRawData),
35     };
36     size_t count = sizeof(properties) / sizeof(properties[0]);
37     return NapiDataUtils::DefineClass(env, "SystemDefinedPixelMap", properties, count, SystemDefinedPixelMapNapi::New);
38 }
39 
New(napi_env env,napi_callback_info info)40 napi_value SystemDefinedPixelMapNapi::New(napi_env env, napi_callback_info info)
41 {
42     LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedPixelMapNapi");
43     auto ctxt = std::make_shared<ContextBase>();
44     ctxt->GetCbInfoSync(env, info);
45     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
46 
47     auto *sdPixelMap = new (std::nothrow) SystemDefinedPixelMapNapi();
48     ASSERT_ERR(ctxt->env, sdPixelMap != nullptr, Status::E_ERROR, "no memory for system defined pixel map!");
49     sdPixelMap->value_ = std::make_shared<SystemDefinedPixelMap>();
50     ASSERT_CALL(env, napi_wrap(env, ctxt->self, sdPixelMap, Destructor, nullptr, nullptr), sdPixelMap);
51     return ctxt->self;
52 }
53 
NewInstance(napi_env env,std::shared_ptr<UnifiedRecord> in,napi_value & out)54 void SystemDefinedPixelMapNapi::NewInstance(napi_env env, std::shared_ptr<UnifiedRecord> in, napi_value &out)
55 {
56     LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedPixelMapNapi");
57     ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
58     auto *sdPixelMap = new (std::nothrow) SystemDefinedPixelMapNapi();
59     ASSERT_ERR_VOID(env, sdPixelMap != nullptr, Status::E_ERROR, "no memory for system defined pixel map!");
60     sdPixelMap->value_ = std::static_pointer_cast<SystemDefinedPixelMap>(in);
61     ASSERT_CALL_DELETE(env, napi_wrap(env, out, sdPixelMap, Destructor, nullptr, nullptr), sdPixelMap);
62 }
63 
Destructor(napi_env env,void * data,void * hint)64 void SystemDefinedPixelMapNapi::Destructor(napi_env env, void *data, void *hint)
65 {
66     LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedPixelMapNapi finalize.");
67     auto *sdPixelMap = static_cast<SystemDefinedPixelMapNapi *>(data);
68     ASSERT_VOID(sdPixelMap != nullptr, "finalize null!");
69     delete sdPixelMap;
70 }
71 
GetSystemDefinedPixelMap(napi_env env,napi_callback_info info,std::shared_ptr<ContextBase> ctxt)72 SystemDefinedPixelMapNapi *SystemDefinedPixelMapNapi::GetSystemDefinedPixelMap(
73     napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
74 {
75     LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedPixelMapNapi");
76     ctxt->GetCbInfoSync(env, info);
77     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
78     return static_cast<SystemDefinedPixelMapNapi *>(ctxt->native);
79 }
80 
GetRawData(napi_env env,napi_callback_info info)81 napi_value SystemDefinedPixelMapNapi::GetRawData(napi_env env, napi_callback_info info)
82 {
83     LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedPixelMapNapi");
84     auto ctxt = std::make_shared<ContextBase>();
85     auto sdPixelMap = GetSystemDefinedPixelMap(env, info, ctxt);
86     ASSERT_ERR(ctxt->env, (sdPixelMap != nullptr && sdPixelMap->value_ != nullptr), Status::E_ERROR,
87         "invalid object!");
88     ctxt->status = NapiDataUtils::SetValue(env, sdPixelMap->value_->GetRawData(), ctxt->output);
89     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set raw data failed!");
90     return ctxt->output;
91 }
92 
SetRawData(napi_env env,napi_callback_info info)93 napi_value SystemDefinedPixelMapNapi::SetRawData(napi_env env, napi_callback_info info)
94 {
95     LOG_DEBUG(UDMF_KITS_NAPI, "SystemDefinedPixelMapNapi");
96     auto ctxt = std::make_shared<ContextBase>();
97     std::vector<uint8_t> pixelMap;
98     auto input = [env, ctxt, &pixelMap](size_t argc, napi_value *argv) {
99         ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
100             Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
101         ctxt->status = NapiDataUtils::GetValue(env, argv[0], pixelMap);
102         ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
103             Status::E_INVALID_PARAMETERS, "Parameter error: parameter rawData type must be Uint8Array");
104     };
105     ctxt->GetCbInfoSync(env, info, input);
106     ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
107     auto sdPixelMap = static_cast<SystemDefinedPixelMapNapi *>(ctxt->native);
108     ASSERT_ERR(ctxt->env, (sdPixelMap != nullptr && sdPixelMap->value_ != nullptr), Status::E_ERROR,
109         "invalid object!");
110     sdPixelMap->value_->SetRawData(pixelMap);
111     return nullptr;
112 }
113 } // namespace UDMF
114 } // namespace OHOS