1 /*
2  * Copyright (c) 2023 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 #ifndef HIVIEW_NAPI_UTIL_H
17 #define HIVIEW_NAPI_UTIL_H
18 
19 #include <string>
20 
21 #include "hiview_file_info.h"
22 #include "napi/native_api.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 class HiviewNapiUtil {
27 public:
28     static void CreateUndefined(const napi_env env, napi_value& ret);
29     static void CreateInt32Value(const napi_env env, int32_t value, napi_value& ret);
30     static void CreateStringValue(const napi_env env, const std::string& value, napi_value& ret);
31     static bool IsMatchType(napi_env env, const napi_value& value, napi_valuetype type);
32     static bool ParseStringValue(
33         const napi_env env, const std::string& paramName, const napi_value& value, std::string& retValue);
34     static napi_value GenerateFileInfoResult(const napi_env env, const std::vector<HiviewFileInfo>& fileInfos);
35     static void ThrowParamTypeError(napi_env env, const std::string& paramName, const std::string& paramType);
36     static void ThrowParamContentError(napi_env env, const std::string& paramName);
37     static void ThrowErrorByCode(napi_env env, int32_t errCode);
38     static void ThrowSystemAppPermissionError(napi_env env);
39     static bool CheckDirPath(const std::string& path);
40     static void CreateErrorByRet(napi_env env, const int32_t retCode, napi_value& ret);
41     static bool IsSystemAppCall();
42 
43 private:
44     HiviewNapiUtil() = default;
45     ~HiviewNapiUtil() = default;
46 
47     static void CreateInt64Value(const napi_env env, int64_t value, napi_value& ret);
48     static void CreateJsFileInfo(const napi_env env, const HiviewFileInfo& fileInfo, napi_value& val);
49     static void SetNamedProperty(
50         const napi_env env, napi_value& object, const std::string& propertyName, napi_value& propertyValue);
51     static void ThrowError(napi_env env, const int32_t code, const std::string& msg);
52     static std::pair<int32_t, std::string> GetErrorDetailByRet(napi_env env, const int32_t retCode);
53 };
54 } // namespace HiviewDFX
55 } // namespace OHOS
56 #endif