1 /*
2 * Copyright (C) 2021 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 #include "hichecker.h"
20
21 namespace OHOS {
22 namespace Media {
23 const size_t NUM0 = 0;
24 const size_t NUM1 = 1;
GetBufferByName(napi_env env,napi_value root,const char * name,void ** res,size_t * len)25 bool ImageNapiUtils::GetBufferByName(napi_env env, napi_value root, const char* name, void **res, size_t* len)
26 {
27 napi_value tempValue = nullptr;
28
29 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, &tempValue)), false);
30
31 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_arraybuffer_info(env, tempValue, res, len)), false);
32
33 return true;
34 }
35
GetUint32ByName(napi_env env,napi_value root,const char * name,uint32_t * res)36 bool ImageNapiUtils::GetUint32ByName(napi_env env, napi_value root, const char* name, uint32_t *res)
37 {
38 napi_value tempValue = nullptr;
39
40 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, &tempValue)), false);
41
42 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_uint32(env, tempValue, res)), false);
43
44 return true;
45 }
46
GetInt32ByName(napi_env env,napi_value root,const char * name,int32_t * res)47 bool ImageNapiUtils::GetInt32ByName(napi_env env, napi_value root, const char* name, int32_t *res)
48 {
49 napi_value tempValue = nullptr;
50
51 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, &tempValue)), false);
52
53 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_int32(env, tempValue, res)), false);
54
55 return true;
56 }
57
GetBoolByName(napi_env env,napi_value root,const char * name,bool * res)58 bool ImageNapiUtils::GetBoolByName(napi_env env, napi_value root, const char* name, bool *res)
59 {
60 napi_value tempValue = nullptr;
61
62 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, &tempValue)), false);
63
64 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_bool(env, tempValue, res)), false);
65
66 return true;
67 }
68
GetNodeByName(napi_env env,napi_value root,const char * name,napi_value * res)69 bool ImageNapiUtils::GetNodeByName(napi_env env, napi_value root, const char* name, napi_value *res)
70 {
71 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_named_property(env, root, name, res)), false);
72
73 return true;
74 }
75
GetUtf8String(napi_env env,napi_value root,std::string & res,bool eof)76 bool ImageNapiUtils::GetUtf8String(napi_env env, napi_value root, std::string &res, bool eof)
77 {
78 size_t bufferSize = NUM0;
79 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_string_utf8(env, root, nullptr,
80 NUM0, &bufferSize)) && bufferSize > NUM0, false);
81
82 size_t resultSize = NUM0;
83 if (eof) {
84 bufferSize = bufferSize + NUM1;
85 }
86 std::vector<char> buffer(bufferSize);
87 IMG_NAPI_CHECK_RET(IMG_IS_OK(napi_get_value_string_utf8(env, root, &(buffer[NUM0]),
88 bufferSize, &resultSize)) && resultSize > NUM0, false);
89 res.assign(buffer.begin(), buffer.end());
90 return true;
91 }
92
CreateArrayBuffer(napi_env env,void * src,size_t srcLen,napi_value * res)93 bool ImageNapiUtils::CreateArrayBuffer(napi_env env, void* src, size_t srcLen, napi_value *res)
94 {
95 if (src == nullptr || srcLen == 0) {
96 return false;
97 }
98
99 void *nativePtr = nullptr;
100 if (napi_create_arraybuffer(env, srcLen, &nativePtr, res) != napi_ok || nativePtr == nullptr) {
101 return false;
102 }
103
104 if (memcpy_s(nativePtr, srcLen, src, srcLen) != 0) {
105 return false;
106 }
107 return true;
108 }
109
getType(napi_env env,napi_value root)110 napi_valuetype ImageNapiUtils::getType(napi_env env, napi_value root)
111 {
112 napi_valuetype res = napi_undefined;
113 napi_typeof(env, root, &res);
114 return res;
115 }
116
HicheckerReport()117 void ImageNapiUtils::HicheckerReport()
118 {
119 uint32_t pid = getpid();
120 uint32_t tid = gettid();
121 std::string cautionMsg = "Trigger: pid = " + std::to_string(pid) + ", tid = " + std::to_string(tid);
122 HiviewDFX::HiChecker::NotifySlowProcess(cautionMsg);
123 }
124 } // namespace Media
125 } // namespace OHOS