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 <securec.h>
18 #include <unistd.h>
19 #if !defined(_IOS) && !defined(_ANDROID)
20 #include "hichecker.h"
21 #endif
22
23 namespace OHOS {
24 namespace Media {
25 const size_t NUM0 = 0;
26 const size_t NUM1 = 1;
GetBufferByName(napi_env env,napi_value root,const char * name,void ** res,size_t * len)27 bool ImageNapiUtils::GetBufferByName(napi_env env, napi_value root, const char* name, void **res, size_t* len)
28 {
29 napi_value tempValue = nullptr;
30
31 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, &tempValue)), false);
32
33 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_arraybuffer_info(env, tempValue, res, len)), false);
34
35 return true;
36 }
37
GetUint32ByName(napi_env env,napi_value root,const char * name,uint32_t * res)38 bool ImageNapiUtils::GetUint32ByName(napi_env env, napi_value root, const char* name, uint32_t *res)
39 {
40 napi_value tempValue = nullptr;
41
42 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, &tempValue)), false);
43
44 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_uint32(env, tempValue, res)), false);
45
46 return true;
47 }
48
GetInt32ByName(napi_env env,napi_value root,const char * name,int32_t * res)49 bool ImageNapiUtils::GetInt32ByName(napi_env env, napi_value root, const char* name, int32_t *res)
50 {
51 napi_value tempValue = nullptr;
52
53 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, &tempValue)), false);
54
55 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_int32(env, tempValue, res)), false);
56
57 return true;
58 }
59
GetBoolByName(napi_env env,napi_value root,const char * name,bool * res)60 bool ImageNapiUtils::GetBoolByName(napi_env env, napi_value root, const char* name, bool *res)
61 {
62 napi_value tempValue = nullptr;
63
64 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, &tempValue)), false);
65
66 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_bool(env, tempValue, res)), false);
67
68 return true;
69 }
70
GetNodeByName(napi_env env,napi_value root,const char * name,napi_value * res)71 bool ImageNapiUtils::GetNodeByName(napi_env env, napi_value root, const char* name, napi_value *res)
72 {
73 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, res)), false);
74
75 return true;
76 }
77
GetUtf8String(napi_env env,napi_value root,std::string & res,bool eof)78 bool ImageNapiUtils::GetUtf8String(napi_env env, napi_value root, std::string &res, bool eof)
79 {
80 size_t bufferSize = NUM0;
81 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_string_utf8(env, root, nullptr,
82 NUM0, &bufferSize)) && bufferSize > NUM0, false);
83
84 size_t resultSize = NUM0;
85 if (eof) {
86 bufferSize = bufferSize + NUM1;
87 }
88 std::vector<char> buffer(bufferSize);
89 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_string_utf8(env, root, &(buffer[NUM0]),
90 bufferSize, &resultSize)) && resultSize > NUM0, false);
91 res.assign(buffer.begin(), buffer.end());
92 return true;
93 }
94
CreateArrayBuffer(napi_env env,void * src,size_t srcLen,napi_value * res)95 bool ImageNapiUtils::CreateArrayBuffer(napi_env env, void* src, size_t srcLen, napi_value *res)
96 {
97 if (src == nullptr || srcLen == 0) {
98 return false;
99 }
100
101 void *nativePtr = nullptr;
102 if (napi_create_arraybuffer(env, srcLen, &nativePtr, res) != napi_ok || nativePtr == nullptr) {
103 return false;
104 }
105
106 if (memcpy_s(nativePtr, srcLen, src, srcLen) != EOK) {
107 return false;
108 }
109 return true;
110 }
111
getType(napi_env env,napi_value root)112 napi_valuetype ImageNapiUtils::getType(napi_env env, napi_value root)
113 {
114 napi_valuetype res = napi_undefined;
115 napi_typeof(env, root, &res);
116 return res;
117 }
118
HicheckerReport()119 void ImageNapiUtils::HicheckerReport()
120 {
121 #if !defined(_IOS) && !defined(_ANDROID)
122 uint32_t pid = getpid();
123 uint32_t tid = gettid();
124 std::string cautionMsg = "Trigger: pid = " + std::to_string(pid) + ", tid = " + std::to_string(tid);
125 HiviewDFX::HiChecker::NotifySlowProcess(cautionMsg);
126 #endif
127 }
128
CreateErrorObj(napi_env env,napi_value & errorObj,const int32_t errCode,const std::string errMsg)129 void ImageNapiUtils::CreateErrorObj(napi_env env, napi_value &errorObj,
130 const int32_t errCode, const std::string errMsg)
131 {
132 napi_value outErrorCode = nullptr;
133 napi_value outErrorMsg = nullptr;
134 napi_status status = napi_create_string_utf8(env, std::to_string(errCode).c_str(),
135 NAPI_AUTO_LENGTH, &outErrorCode);
136 if (status != napi_ok) {
137 return;
138 }
139
140 status = napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &outErrorMsg);
141 if (status != napi_ok) {
142 return;
143 }
144
145 status = napi_create_error(env, outErrorCode, outErrorMsg, &errorObj);
146 if (status != napi_ok) {
147 napi_get_undefined(env, &errorObj);
148 }
149 }
150
ThrowExceptionError(napi_env env,const int32_t errCode,const std::string errMsg)151 napi_value ImageNapiUtils::ThrowExceptionError(napi_env env, const int32_t errCode,
152 const std::string errMsg)
153 {
154 napi_value result = nullptr;
155 napi_status status = napi_throw_error(env, std::to_string(errCode).c_str(), errMsg.c_str());
156 if (status == napi_ok) {
157 napi_get_undefined(env, &result);
158 }
159 return result;
160 }
161 } // namespace Media
162 } // namespace OHOS
163