1 /*
2 * Copyright (c) 2021-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 "napi_hitrace_util.h"
17
18 #include "hilog/log.h"
19
20 namespace OHOS {
21 namespace HiviewDFX {
22 namespace {
23 constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D03, "HITRACE_UTIL_NAPI" };
24 constexpr uint32_t UINT32_T_PRO_DEFAULT_VALUE = 0;
25 constexpr uint64_t UINT64_T_PRO_DEFAULT_VALUE = 0;
26 constexpr uint64_t INVALID_CHAIN_ID = 0;
27 constexpr char CHAIN_ID_ATTR[] = "chainId";
28 constexpr char SPAN_ID_ATTR[] = "spanId";
29 constexpr char PARENT_SPAN_ID_ATTR[] = "parentSpanId";
30 constexpr char FLAGS_ATTR[] = "flags";
31
CreateInt32Value(const napi_env env,int32_t value)32 napi_value CreateInt32Value(const napi_env env, int32_t value)
33 {
34 napi_value result = nullptr;
35 NAPI_CALL(env, napi_create_int32(env, value, &result));
36 return result;
37 }
38
CreateInt64Value(const napi_env env,int64_t value)39 napi_value CreateInt64Value(const napi_env env, int64_t value)
40 {
41 napi_value result = nullptr;
42 NAPI_CALL(env, napi_create_int64(env, value, &result));
43 return result;
44 }
45
CreateBigInt64Value(const napi_env env,uint64_t value)46 napi_value CreateBigInt64Value(const napi_env env, uint64_t value)
47 {
48 napi_value result = nullptr;
49 NAPI_CALL(env, napi_create_bigint_uint64(env, value, &result));
50 return result;
51 }
52
SetNamedProperty(const napi_env env,napi_value & object,const std::string & propertyName,napi_value & propertyValue)53 napi_status SetNamedProperty(const napi_env env, napi_value& object,
54 const std::string& propertyName, napi_value& propertyValue)
55 {
56 napi_status status = napi_set_named_property(env, object, propertyName.c_str(), propertyValue);
57 if (status != napi_ok) {
58 HiLog::Error(LABEL, "set property %{public}s failed.", propertyName.c_str());
59 }
60 return status;
61 }
62
GetPropertyByName(const napi_env env,const napi_value & object,const std::string & propertyName)63 napi_value GetPropertyByName(const napi_env env, const napi_value& object,
64 const std::string& propertyName)
65 {
66 napi_value result = nullptr;
67 NAPI_CALL(env, napi_get_named_property(env, object, propertyName.c_str(), &result));
68 return result;
69 }
70 }
71
CheckValueTypeValidity(const napi_env env,const napi_value & jsObj,const napi_valuetype typeName)72 bool NapiHitraceUtil::CheckValueTypeValidity(const napi_env env, const napi_value& jsObj,
73 const napi_valuetype typeName)
74 {
75 napi_valuetype valueType = napi_undefined;
76 napi_status ret = napi_typeof(env, jsObj, &valueType);
77 if (ret != napi_ok) {
78 HiLog::Error(LABEL, "failed to parse the type of napi value.");
79 return false;
80 }
81 if (valueType != typeName) {
82 HiLog::Error(LABEL, "you have called a function with parameters of wrong type.");
83 return false;
84 }
85 return true;
86 }
87
CreateHiTraceIdJsObject(const napi_env env,HiTraceId & traceId,napi_value & valueObject)88 void NapiHitraceUtil::CreateHiTraceIdJsObject(const napi_env env, HiTraceId& traceId,
89 napi_value& valueObject)
90 {
91 napi_create_object(env, &valueObject);
92 NapiHitraceUtil::SetPropertyBigInt64(env, valueObject, CHAIN_ID_ATTR,
93 traceId.GetChainId());
94 HiLog::Debug(LABEL, "Native2Js: chainId is %{public}llx.",
95 static_cast<unsigned long long>(traceId.GetChainId()));
96 NapiHitraceUtil::SetPropertyInt64(env, valueObject, SPAN_ID_ATTR, traceId.GetSpanId());
97 HiLog::Debug(LABEL, "Native2Js: spanId is %{public}llx.",
98 static_cast<unsigned long long>(traceId.GetSpanId()));
99 NapiHitraceUtil::SetPropertyInt64(env, valueObject, PARENT_SPAN_ID_ATTR,
100 traceId.GetParentSpanId());
101 HiLog::Debug(LABEL, "Native2Js: parentSpanId is %{public}llx.",
102 static_cast<unsigned long long>(traceId.GetParentSpanId()));
103 NapiHitraceUtil::SetPropertyInt32(env, valueObject, FLAGS_ATTR,
104 traceId.GetFlags());
105 HiLog::Debug(LABEL, "Native2Js: flags is %{public}d.", traceId.GetFlags());
106 }
107
TransHiTraceIdJsObjectToNative(const napi_env env,HiTraceId & traceId,const napi_value & valueObject)108 void NapiHitraceUtil::TransHiTraceIdJsObjectToNative(const napi_env env, HiTraceId& traceId,
109 const napi_value& valueObject)
110 {
111 uint64_t chainId = NapiHitraceUtil::GetPropertyBigInt64(env, valueObject, CHAIN_ID_ATTR);
112 HiLog::Debug(LABEL, "Js2Native: chainId is %{public}llx.",
113 static_cast<unsigned long long>(chainId));
114 if (chainId == INVALID_CHAIN_ID) {
115 return;
116 }
117 traceId.SetChainId(chainId);
118 uint64_t spanId = NapiHitraceUtil::GetPropertyInt64(env, valueObject, SPAN_ID_ATTR);
119 HiLog::Debug(LABEL, "Js2Native: spanId is %{public}llx.",
120 static_cast<unsigned long long>(spanId));
121 traceId.SetSpanId(spanId);
122 uint64_t parentSpanId = NapiHitraceUtil::GetPropertyInt64(env, valueObject,
123 PARENT_SPAN_ID_ATTR);
124 HiLog::Debug(LABEL, "Js2Native: parentSpanId is %{public}llx.",
125 static_cast<unsigned long long>(parentSpanId));
126 traceId.SetParentSpanId(parentSpanId);
127 uint32_t flags = NapiHitraceUtil::GetPropertyInt32(env, valueObject, FLAGS_ATTR);
128 HiLog::Debug(LABEL, "Js2Native: flags is %{public}d.", flags);
129 traceId.SetFlags(flags);
130 }
131
EnableTraceIdObjectFlag(const napi_env env,HiTraceId & traceId,napi_value & traceIdObject)132 void NapiHitraceUtil::EnableTraceIdObjectFlag(const napi_env env, HiTraceId& traceId,
133 napi_value& traceIdObject)
134 {
135 NapiHitraceUtil::SetPropertyInt32(env, traceIdObject, FLAGS_ATTR, traceId.GetFlags());
136 }
137
SetPropertyInt32(const napi_env env,napi_value & object,const std::string & propertyName,uint32_t value)138 void NapiHitraceUtil::SetPropertyInt32(const napi_env env, napi_value& object,
139 const std::string& propertyName, uint32_t value)
140 {
141 napi_value peropertyValue = CreateInt32Value(env, value);
142 SetNamedProperty(env, object, propertyName, peropertyValue);
143 }
144
SetPropertyInt64(const napi_env env,napi_value & object,const std::string & propertyName,uint64_t value)145 void NapiHitraceUtil::SetPropertyInt64(const napi_env env, napi_value& object,
146 const std::string& propertyName, uint64_t value)
147 {
148 napi_value peropertyValue = CreateInt64Value(env, value);
149 SetNamedProperty(env, object, propertyName, peropertyValue);
150 }
151
SetPropertyBigInt64(const napi_env env,napi_value & object,const std::string & propertyName,uint64_t value)152 void NapiHitraceUtil::SetPropertyBigInt64(const napi_env env, napi_value& object,
153 const std::string& propertyName, uint64_t value)
154 {
155 napi_value peropertyValue = CreateBigInt64Value(env, value);
156 SetNamedProperty(env, object, propertyName, peropertyValue);
157 }
158
GetPropertyInt32(const napi_env env,const napi_value & object,const std::string & propertyName)159 uint32_t NapiHitraceUtil::GetPropertyInt32(const napi_env env, const napi_value& object,
160 const std::string& propertyName)
161 {
162 napi_value propertyValue = GetPropertyByName(env, object, propertyName);
163 napi_valuetype type;
164 napi_status status = napi_typeof(env, propertyValue, &type);
165 if (status != napi_ok) {
166 HiLog::Error(LABEL, "failed to get %{public}s from HiTraceId Js Object.",
167 propertyName.c_str());
168 return UINT32_T_PRO_DEFAULT_VALUE;
169 }
170 if (type != napi_valuetype::napi_number) {
171 HiLog::Error(LABEL, "type is not napi_number property.");
172 return UINT32_T_PRO_DEFAULT_VALUE;
173 }
174 int32_t numberValue = 0;
175 status = napi_get_value_int32(env, propertyValue, &numberValue);
176 if (status == napi_ok) {
177 return numberValue;
178 }
179 HiLog::Error(LABEL, "failed to get napi_number property from HiTraceId Js Object.");
180 return UINT32_T_PRO_DEFAULT_VALUE;
181 }
182
GetPropertyInt64(const napi_env env,const napi_value & object,const std::string & propertyName)183 uint64_t NapiHitraceUtil::GetPropertyInt64(const napi_env env, const napi_value& object,
184 const std::string& propertyName)
185 {
186 napi_value propertyValue = GetPropertyByName(env, object, propertyName);
187 napi_valuetype type;
188 napi_status status = napi_typeof(env, propertyValue, &type);
189 if (status != napi_ok) {
190 HiLog::Error(LABEL, "failed to get %{public}s from HiTraceId Js Object.",
191 propertyName.c_str());
192 return UINT64_T_PRO_DEFAULT_VALUE;
193 }
194 if (type != napi_valuetype::napi_number) {
195 HiLog::Error(LABEL, "type is not napi_number property.");
196 return UINT64_T_PRO_DEFAULT_VALUE;
197 }
198 int64_t numberValue = 0;
199 status = napi_get_value_int64(env, propertyValue, &numberValue);
200 if (status == napi_ok) {
201 return numberValue;
202 }
203 HiLog::Error(LABEL, "failed to get napi_number property from HiTraceId Js Object.");
204 return UINT64_T_PRO_DEFAULT_VALUE;
205 }
206
GetPropertyBigInt64(const napi_env env,const napi_value & object,const std::string & propertyName)207 uint64_t NapiHitraceUtil::GetPropertyBigInt64(const napi_env env, const napi_value& object,
208 const std::string& propertyName)
209 {
210 napi_value propertyValue = GetPropertyByName(env, object, propertyName);
211 napi_valuetype type;
212 napi_status status = napi_typeof(env, propertyValue, &type);
213 if (status != napi_ok) {
214 HiLog::Error(LABEL, "failed to get %{public}s from HiTraceId Js Object.",
215 propertyName.c_str());
216 return UINT64_T_PRO_DEFAULT_VALUE;
217 }
218 uint64_t bigInt64Value = 0;
219 bool lossless = true;
220 napi_get_value_bigint_uint64(env, propertyValue, &bigInt64Value, &lossless);
221 return bigInt64Value;
222 }
223 } // namespace HiviewDFX
224 } // namespace OHOS
225