• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "hiview_napi_util.h"
17 
18 #include <map>
19 
20 #include "hiview_err_code.h"
21 #include "hiview_logger.h"
22 #include "ipc_skeleton.h"
23 #include "tokenid_kit.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 DEFINE_LOG_LABEL(0xD002D10, "HiviewNapiUtil");
28 namespace {
29 constexpr char FILE_NAME_KEY[] = "name";
30 constexpr char FILE_TIME_KEY[] = "mtime";
31 constexpr char FILE_SIZE_KEY[] = "size";
32 constexpr int32_t BUF_SIZE = 1024;
33 }
34 
CreateUndefined(const napi_env env,napi_value & ret)35 void HiviewNapiUtil::CreateUndefined(const napi_env env, napi_value& ret)
36 {
37     if (napi_get_undefined(env, &ret) != napi_ok) {
38         HIVIEW_LOGE("failed to create undefined value.");
39     }
40 }
41 
CreateInt32Value(const napi_env env,int32_t value,napi_value & ret)42 void HiviewNapiUtil::CreateInt32Value(const napi_env env, int32_t value, napi_value& ret)
43 {
44     if (napi_create_int32(env, value, &ret) != napi_ok) {
45         HIVIEW_LOGE("failed to create int32 napi value.");
46     }
47 }
48 
CreateInt64Value(const napi_env env,int64_t value,napi_value & ret)49 void HiviewNapiUtil::CreateInt64Value(const napi_env env, int64_t value, napi_value& ret)
50 {
51     if (napi_create_int64(env, value, &ret) != napi_ok) {
52         HIVIEW_LOGE("failed to create int64 napi value.");
53     }
54 }
55 
CreateStringValue(const napi_env env,const std::string & value,napi_value & ret)56 void HiviewNapiUtil::CreateStringValue(const napi_env env, const std::string& value, napi_value& ret)
57 {
58     if (napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &ret) != napi_ok) {
59         HIVIEW_LOGE("failed to create string napi value.");
60     }
61 }
62 
CreateErrorByRet(napi_env env,const int32_t retCode,napi_value & ret)63 void HiviewNapiUtil::CreateErrorByRet(napi_env env, const int32_t retCode, napi_value& ret)
64 {
65     auto detail = GetErrorDetailByRet(env, retCode);
66     napi_value napiCode = nullptr;
67     CreateStringValue(env, std::to_string(detail.first), napiCode);
68     napi_value napiStr = nullptr;
69     CreateStringValue(env, detail.second, napiStr);
70     if (napi_create_error(env, napiCode, napiStr, &ret) != napi_ok) {
71         HIVIEW_LOGE("failed to create napi error");
72     }
73 }
74 
GetErrorDetailByRet(napi_env env,const int32_t retCode)75 std::pair<int32_t, std::string> HiviewNapiUtil::GetErrorDetailByRet(napi_env env, const int32_t retCode)
76 {
77     HIVIEW_LOGI("origin result code is %{public}d.", retCode);
78     const std::map<int32_t, std::pair<int32_t, std::string>> errMap = {
79         {HiviewNapiErrCode::ERR_PERMISSION_CHECK, {HiviewNapiErrCode::ERR_PERMISSION_CHECK,
80             "Permission denied. The app does not have the necessary permissions."}},
81         {HiviewNapiErrCode::ERR_INNER_INVALID_LOGTYPE,
82             {HiviewNapiErrCode::ERR_PARAM_CHECK, "Parameter error. The value of logType is invalid."}},
83         {HiviewNapiErrCode::ERR_INNER_READ_ONLY,
84             {HiviewNapiErrCode::ERR_PARAM_CHECK, "Parameter error. The specified logType is read-only."}},
85         {HiviewNapiErrCode::ERR_SOURCE_FILE_NOT_EXIST,
86             {HiviewNapiErrCode::ERR_SOURCE_FILE_NOT_EXIST, "Source file does not exists."}}
87     };
88     return errMap.find(retCode) == errMap.end() ?
89         std::make_pair(HiviewNapiErrCode::ERR_DEFAULT, "Environment is abnormal.") : errMap.at(retCode);
90 }
91 
IsMatchType(napi_env env,const napi_value & value,napi_valuetype type)92 bool HiviewNapiUtil::IsMatchType(napi_env env, const napi_value& value, napi_valuetype type)
93 {
94     napi_valuetype paramType;
95     napi_typeof(env, value, &paramType);
96     return paramType == type;
97 }
98 
SetNamedProperty(const napi_env env,napi_value & object,const std::string & propertyName,napi_value & propertyValue)99 void HiviewNapiUtil::SetNamedProperty(
100     const napi_env env, napi_value& object, const std::string& propertyName, napi_value& propertyValue)
101 {
102     if (napi_set_named_property(env, object, propertyName.c_str(), propertyValue) != napi_ok) {
103         HIVIEW_LOGE("set %{public}s property failed", propertyName.c_str());
104     }
105 }
106 
ParseStringValue(const napi_env env,const std::string & paramName,const napi_value & value,std::string & retValue)107 bool HiviewNapiUtil::ParseStringValue(
108     const napi_env env, const std::string& paramName, const napi_value& value, std::string& retValue)
109 {
110     if (!IsMatchType(env, value, napi_string)) {
111         HIVIEW_LOGE("parameter %{public}s type isn't string", paramName.c_str());
112         ThrowParamTypeError(env, paramName, "string");
113         return false;
114     }
115     char buf[BUF_SIZE] = {0};
116     size_t bufLength = 0;
117     if (napi_get_value_string_utf8(env, value, buf, BUF_SIZE - 1, &bufLength) != napi_ok) {
118         HIVIEW_LOGE("failed to parse napi value of string type.");
119     } else {
120         retValue = std::string {buf};
121     }
122     return true;
123 }
124 
CreateJsFileInfo(const napi_env env,const HiviewFileInfo & fileInfo,napi_value & val)125 void HiviewNapiUtil::CreateJsFileInfo(const napi_env env, const HiviewFileInfo& fileInfo, napi_value& val)
126 {
127     napi_value name;
128     CreateStringValue(env, fileInfo.name, name);
129     SetNamedProperty(env, val, FILE_NAME_KEY, name);
130 
131     napi_value mtime;
132     CreateInt64Value(env, fileInfo.mtime, mtime);
133     SetNamedProperty(env, val, FILE_TIME_KEY, mtime);
134 
135     napi_value size;
136     CreateInt64Value(env, fileInfo.size, size);
137     SetNamedProperty(env, val, FILE_SIZE_KEY, size);
138 }
139 
GenerateFileInfoResult(const napi_env env,const std::vector<HiviewFileInfo> & fileInfos)140 napi_value HiviewNapiUtil::GenerateFileInfoResult(const napi_env env, const std::vector<HiviewFileInfo>& fileInfos)
141 {
142     napi_value result;
143     auto length = fileInfos.size();
144     napi_create_array_with_length(env, length, &result);
145     for (decltype(length) i = 0; i < length; ++i) {
146         napi_value item;
147         if (napi_create_object(env, &item) != napi_ok) {
148             HIVIEW_LOGE("failed to create a new napi object.");
149             break;
150         }
151         CreateJsFileInfo(env, fileInfos[i], item);
152         napi_set_element(env, result, i, item);
153     }
154     return result;
155 }
156 
CheckDirPath(const std::string & path)157 bool HiviewNapiUtil::CheckDirPath(const std::string& path)
158 {
159     return path.empty() || path.find("..") == std::string::npos;
160 }
161 
ThrowErrorByCode(napi_env env,int32_t errCode)162 void HiviewNapiUtil::ThrowErrorByCode(napi_env env, int32_t errCode)
163 {
164     auto detail = GetErrorDetailByRet(env, errCode);
165     ThrowError(env, detail.first, detail.second);
166 }
167 
ThrowParamContentError(napi_env env,const std::string & paramName)168 void HiviewNapiUtil::ThrowParamContentError(napi_env env, const std::string& paramName)
169 {
170     ThrowError(env, HiviewNapiErrCode::ERR_PARAM_CHECK,
171         "Parameter error. The content of " + paramName + " is invalid.");
172 }
173 
ThrowParamTypeError(napi_env env,const std::string & paramName,const std::string & paramType)174 void HiviewNapiUtil::ThrowParamTypeError(napi_env env, const std::string& paramName, const std::string& paramType)
175 {
176     ThrowError(env, HiviewNapiErrCode::ERR_PARAM_CHECK,
177         "Parameter error. The type of " + paramName + " must be " + paramType + ".");
178 }
179 
ThrowError(napi_env env,const int32_t code,const std::string & msg)180 void HiviewNapiUtil::ThrowError(napi_env env, const int32_t code, const std::string& msg)
181 {
182     if (napi_throw_error(env, std::to_string(code).c_str(), msg.c_str()) != napi_ok) {
183         HIVIEW_LOGE("failed to throw error, code=%{public}d, msg=%{public}s", code, msg.c_str());
184     }
185 }
186 
ThrowSystemAppPermissionError(napi_env env)187 void HiviewNapiUtil::ThrowSystemAppPermissionError(napi_env env)
188 {
189     ThrowError(env, HiviewNapiErrCode::ERR_NON_SYS_APP_PERMISSION,
190         "Permission denied, non-system app called system api.");
191 }
192 
IsSystemAppCall()193 bool HiviewNapiUtil::IsSystemAppCall()
194 {
195     uint64_t tokenId = IPCSkeleton::GetCallingFullTokenID();
196     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(tokenId);
197 }
198 } // namespace HiviewDFX
199 } // namespace OHOS