1 /*
2 * Copyright (c) 2024 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 "napi_util.h"
17
18 #include <fcntl.h>
19 #include <unistd.h>
20
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 #include "hilog/log.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace {
28 #undef LOG_DOMAIN
29 #define LOG_DOMAIN 0xD002D0A
30 #undef LOG_TAG
31 #define LOG_TAG "NapiUtil"
32 constexpr int ONE_VALUE_LIMIT = 1;
33 const std::string DEFAULT_FILENAME = "undefined";
34 }
35
CreateErrorMessage(napi_env env,const std::string & msg)36 napi_value CreateErrorMessage(napi_env env, const std::string& msg)
37 {
38 napi_value result = nullptr;
39 napi_value message = nullptr;
40 napi_create_string_utf8(env, msg.data(), msg.size(), &message);
41 napi_create_error(env, nullptr, message, &result);
42 return result;
43 }
44
CreateErrorMessage(napi_env env,const std::string & errCode,const std::string & msg)45 napi_value CreateErrorMessage(napi_env env, const std::string& errCode, const std::string& msg)
46 {
47 napi_value result = nullptr;
48 napi_value message = nullptr;
49 napi_value code = nullptr;
50 napi_create_string_utf8(env, errCode.data(), errCode.size(), &code);
51 napi_create_string_utf8(env, msg.data(), msg.size(), &message);
52 napi_create_error(env, code, message, &result);
53 return result;
54 }
55
CreateUndefined(napi_env env)56 napi_value CreateUndefined(napi_env env)
57 {
58 napi_value res = nullptr;
59 napi_get_undefined(env, &res);
60 return res;
61 }
62
MatchValueType(napi_env env,napi_value value,napi_valuetype targetType)63 bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
64 {
65 napi_valuetype valueType = napi_undefined;
66 napi_typeof(env, value, &valueType);
67 return valueType == targetType;
68 }
69
GetNapiBoolValue(napi_env env,napi_value value,bool & ret)70 bool GetNapiBoolValue(napi_env env, napi_value value, bool& ret)
71 {
72 if (MatchValueType(env, value, napi_boolean)) {
73 napi_get_value_bool(env, value, &ret);
74 return true;
75 }
76 return false;
77 }
78
GetNapiStringValue(napi_env env,napi_value value,std::string & ret,size_t maxSize)79 bool GetNapiStringValue(napi_env env, napi_value value, std::string& ret, size_t maxSize)
80 {
81 if (!MatchValueType(env, value, napi_string)) {
82 HILOG_ERROR(LOG_CORE, "Type error, should be string type!");
83 return false;
84 }
85 size_t bufLen = 0;
86 napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &bufLen);
87 if (status != napi_ok) {
88 HILOG_ERROR(LOG_CORE, "Get input filename param length failed.");
89 return false;
90 }
91 if (bufLen > maxSize || bufLen == 0) {
92 HILOG_ERROR(LOG_CORE, "input filename param length is illegal.");
93 return false;
94 }
95 ret = std::string(bufLen, '\0');
96 return napi_get_value_string_utf8(env, value, &ret[0], bufLen + 1, &bufLen) == napi_ok;
97 }
98
GetTheOnlyStringParam(napi_env env,napi_callback_info info,std::string & fileName)99 bool GetTheOnlyStringParam(napi_env env, napi_callback_info info, std::string &fileName)
100 {
101 size_t argc = ONE_VALUE_LIMIT;
102 napi_value argv = nullptr;
103 napi_value thisVar = nullptr;
104 void *data = nullptr;
105 napi_get_cb_info(env, info, &argc, &argv, &thisVar, &data);
106 if (argc != ONE_VALUE_LIMIT) {
107 HILOG_ERROR(LOG_CORE, "invalid number = %{public}d of params.", ONE_VALUE_LIMIT);
108 return false;
109 }
110 constexpr int paramLen = 128;
111 return GetNapiStringValue(env, argv, fileName, paramLen);
112 }
113
GetFileNameParam(napi_env env,napi_callback_info info)114 std::string GetFileNameParam(napi_env env, napi_callback_info info)
115 {
116 std::string fileName;
117 if (!GetTheOnlyStringParam(env, info, fileName)) {
118 return DEFAULT_FILENAME;
119 }
120 return fileName;
121 }
122
CreatePromise(napi_env env,napi_value & promise)123 bool AsyncTask::CreatePromise(napi_env env, napi_value &promise)
124 {
125 if (napi_create_promise(env, &deferred_, &promise) != napi_ok) {
126 return false;
127 }
128 napi_value resourceName;
129 if (napi_create_string_utf8(env, resourceName_.c_str(), resourceName_.size(), &resourceName) != napi_ok) {
130 return false;
131 };
132 if (napi_create_async_work(env, nullptr, resourceName, ExecuteCallBack, CompletedCallBack,
133 static_cast<void *>(this), &worker_) != napi_ok) {
134 return false;
135 }
136 return napi_queue_async_work(env, worker_) == napi_ok;
137 }
138
ExecuteCallBack(napi_env env,void * data)139 void AsyncTask::ExecuteCallBack(napi_env env, void* data)
140 {
141 auto asyncTaskPtr = reinterpret_cast<AsyncTask *>(data);
142 asyncTaskPtr->Work(env);
143 }
144
CompletedCallBack(napi_env env,napi_status status,void * data)145 void AsyncTask::CompletedCallBack(napi_env env, napi_status status, void* data)
146 {
147 auto asyncTaskPtr = reinterpret_cast<AsyncTask *>(data);
148 asyncTaskPtr->Done(env, status);
149 napi_delete_async_work(env, asyncTaskPtr->worker_);
150 delete asyncTaskPtr;
151 }
152 } // namespace HiviewDFX
153 } // namespace OHOS