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