1 /*
2 * Copyright (C) 2022 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 "image_napi_utils.h"
17 #include <chrono>
18 #include <securec.h>
19 #include <unistd.h>
20 #if !defined(IOS_PLATFORM) && !defined(A_PLATFORM) && defined(HICHECKER_ENABLE)
21 #include "hichecker.h"
22 #endif
23
24 namespace OHOS {
25 namespace Media {
26 const size_t NUM0 = 0;
27 const size_t NUM1 = 1;
28 const int ARGS3 = 3;
29 const int ARGS4 = 4;
30 const int PARAM0 = 0;
31 const int PARAM1 = 1;
32 const int PARAM2 = 2;
33 const int PARAM3 = 3;
34
GetBufferByName(napi_env env,napi_value root,const char * name,void ** res,size_t * len)35 bool ImageNapiUtils::GetBufferByName(napi_env env, napi_value root, const char* name, void **res, size_t* len)
36 {
37 napi_value tempValue = nullptr;
38
39 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, &tempValue)), false);
40
41 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_arraybuffer_info(env, tempValue, res, len)), false);
42
43 return true;
44 }
45
GetUint32ByName(napi_env env,napi_value root,const char * name,uint32_t * res)46 bool ImageNapiUtils::GetUint32ByName(napi_env env, napi_value root, const char* name, uint32_t *res)
47 {
48 napi_value tempValue = nullptr;
49
50 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, &tempValue)), false);
51
52 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_uint32(env, tempValue, res)), false);
53
54 return true;
55 }
56
GetInt32ByName(napi_env env,napi_value root,const char * name,int32_t * res)57 bool ImageNapiUtils::GetInt32ByName(napi_env env, napi_value root, const char* name, int32_t *res)
58 {
59 napi_value tempValue = nullptr;
60
61 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, &tempValue)), false);
62
63 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_int32(env, tempValue, res)), false);
64
65 return true;
66 }
67
GetBoolByName(napi_env env,napi_value root,const char * name,bool * res)68 bool ImageNapiUtils::GetBoolByName(napi_env env, napi_value root, const char* name, bool *res)
69 {
70 napi_value tempValue = nullptr;
71
72 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, &tempValue)), false);
73
74 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_bool(env, tempValue, res)), false);
75
76 return true;
77 }
78
GetNodeByName(napi_env env,napi_value root,const char * name,napi_value * res)79 bool ImageNapiUtils::GetNodeByName(napi_env env, napi_value root, const char* name, napi_value *res)
80 {
81 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, res)), false);
82
83 return true;
84 }
85
GetUtf8String(napi_env env,napi_value root,std::string & res,bool eof)86 bool ImageNapiUtils::GetUtf8String(napi_env env, napi_value root, std::string &res, bool eof)
87 {
88 size_t bufferSize = NUM0;
89 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_string_utf8(env, root, nullptr,
90 NUM0, &bufferSize)) && bufferSize > NUM0, false);
91
92 size_t resultSize = NUM0;
93 if (eof) {
94 bufferSize = bufferSize + NUM1;
95 }
96 std::vector<char> buffer(bufferSize);
97 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_string_utf8(env, root, &(buffer[NUM0]),
98 bufferSize, &resultSize)) && resultSize > NUM0, false);
99 res.assign(buffer.begin(), buffer.end());
100 return true;
101 }
102
CreateArrayBuffer(napi_env env,void * src,size_t srcLen,napi_value * res)103 bool ImageNapiUtils::CreateArrayBuffer(napi_env env, void* src, size_t srcLen, napi_value *res)
104 {
105 if (src == nullptr || srcLen == 0) {
106 return false;
107 }
108
109 void *nativePtr = nullptr;
110 if (napi_create_arraybuffer(env, srcLen, &nativePtr, res) != napi_ok || nativePtr == nullptr) {
111 return false;
112 }
113
114 if (memcpy_s(nativePtr, srcLen, src, srcLen) != EOK) {
115 return false;
116 }
117 return true;
118 }
119
getType(napi_env env,napi_value root)120 napi_valuetype ImageNapiUtils::getType(napi_env env, napi_value root)
121 {
122 napi_valuetype res = napi_undefined;
123 napi_typeof(env, root, &res);
124 return res;
125 }
126
ParseSize(napi_env env,napi_value root,int32_t & width,int32_t & height)127 static bool ParseSize(napi_env env, napi_value root, int32_t& width, int32_t& height)
128 {
129 if (!GET_INT32_BY_NAME(root, "width", width) || !GET_INT32_BY_NAME(root, "height", height)) {
130 return false;
131 }
132 return true;
133 }
134
ParseImageCreatorReceiverArgs(napi_env env,size_t argc,napi_value argv[],int32_t args[],std::string & errMsg)135 bool ImageNapiUtils::ParseImageCreatorReceiverArgs(napi_env env, size_t argc,
136 napi_value argv[], int32_t args[], std::string &errMsg)
137 {
138 if ((argc != ARGS3) && (argc != ARGS4)) {
139 errMsg = "Invalid arg counts ";
140 errMsg.append(std::to_string(argc));
141 return false;
142 }
143 if (argc == ARGS3) {
144 napi_valuetype argvType0 = ImageNapiUtils::getType(env, argv[PARAM0]);
145 if (argvType0 != napi_object || !ParseSize(env, argv[PARAM0], args[PARAM0], args[PARAM1])) {
146 errMsg = "Invalid arg ";
147 errMsg.append(std::to_string(PARAM0)).append(",type:").append(std::to_string(argvType0));
148 return false;
149 }
150 napi_valuetype argvType1 = ImageNapiUtils::getType(env, argv[PARAM1]);
151 if (argvType1 != napi_number || napi_get_value_int32(env, argv[PARAM1], &(args[PARAM2])) != napi_ok) {
152 errMsg = "Invalid arg ";
153 errMsg.append(std::to_string(PARAM1)).append(",type:").append(std::to_string(argvType1));
154 return false;
155 }
156 napi_valuetype argvType2 = ImageNapiUtils::getType(env, argv[PARAM2]);
157 if (argvType2 != napi_number || napi_get_value_int32(env, argv[PARAM2], &(args[PARAM3])) != napi_ok) {
158 errMsg = "Invalid arg ";
159 errMsg.append(std::to_string(PARAM2)).append(",type:").append(std::to_string(argvType2));
160 return false;
161 }
162 return true;
163 }
164 for (size_t i = PARAM0; i < argc; i++) {
165 napi_valuetype argvType = ImageNapiUtils::getType(env, argv[i]);
166 if (argvType != napi_number) {
167 errMsg = "Invalid arg ";
168 errMsg.append(std::to_string(i)).append(" type ").append(std::to_string(argvType));
169 return false;
170 }
171
172 napi_status status = napi_get_value_int32(env, argv[i], &(args[i]));
173 if (status != napi_ok) {
174 errMsg = "fail to get arg ";
175 errMsg.append(std::to_string(i)).append(" : ").append(std::to_string(status));
176 return false;
177 }
178 }
179 return true;
180 }
181
HicheckerReport()182 void ImageNapiUtils::HicheckerReport()
183 {
184 #if !defined(IOS_PLATFORM) && !defined(A_PLATFORM) && defined(HICHECKER_ENABLE)
185 uint32_t pid = getpid();
186 uint32_t tid = gettid();
187 std::string cautionMsg = "Trigger: pid = " + std::to_string(pid) + ", tid = " + std::to_string(tid);
188 HiviewDFX::HiChecker::NotifySlowProcess(cautionMsg);
189 #endif
190 }
191
CreateErrorObj(napi_env env,napi_value & errorObj,const int32_t errCode,const std::string errMsg)192 void ImageNapiUtils::CreateErrorObj(napi_env env, napi_value &errorObj,
193 const int32_t errCode, const std::string errMsg)
194 {
195 napi_value outErrorCode = nullptr;
196 napi_value outErrorMsg = nullptr;
197 napi_status status = napi_create_string_utf8(env, std::to_string(errCode).c_str(),
198 NAPI_AUTO_LENGTH, &outErrorCode);
199 if (status != napi_ok) {
200 return;
201 }
202
203 status = napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &outErrorMsg);
204 if (status != napi_ok) {
205 return;
206 }
207
208 status = napi_create_error(env, outErrorCode, outErrorMsg, &errorObj);
209 if (status != napi_ok) {
210 napi_get_undefined(env, &errorObj);
211 }
212 }
213
ThrowExceptionError(napi_env env,const int32_t errCode,const std::string errMsg)214 napi_value ImageNapiUtils::ThrowExceptionError(napi_env env, const int32_t errCode,
215 const std::string errMsg)
216 {
217 napi_value result = nullptr;
218 napi_status status = napi_throw_error(env, std::to_string(errCode).c_str(), errMsg.c_str());
219 if (status == napi_ok) {
220 napi_get_undefined(env, &result);
221 }
222 return result;
223 }
224
GetNowTimeMicroSeconds()225 uint64_t ImageNapiUtils::GetNowTimeMicroSeconds()
226 {
227 auto now = std::chrono::system_clock::now();
228 return std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
229 }
230 } // namespace Media
231 } // namespace OHOS
232