1 /*
2 * Copyright (c) 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_hichecker.h"
17
18 #include <map>
19
20 #include "hichecker.h"
21 #include "hilog/log_c.h"
22 #include "hilog/log_cpp.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace {
27 constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D0B, "HiChecker_NAPI" };
28 constexpr int ONE_VALUE_LIMIT = 1;
29 constexpr int ARRAY_INDEX_FIRST = 0;
30 constexpr uint64_t GET_RULE_PARAM_FAIL = 0;
31 constexpr int ERR_PARAM = 401;
32 }
33
AddRule(napi_env env,napi_callback_info info)34 napi_value AddRule(napi_env env, napi_callback_info info)
35 {
36 uint64_t rule = GetRuleParam(env, info);
37 if (rule != GET_RULE_PARAM_FAIL) {
38 HiChecker::AddRule(rule);
39 }
40 return CreateUndefined(env);
41 }
42
RemoveRule(napi_env env,napi_callback_info info)43 napi_value RemoveRule(napi_env env, napi_callback_info info)
44 {
45 uint64_t rule = GetRuleParam(env, info);
46 if (rule != GET_RULE_PARAM_FAIL) {
47 HiChecker::RemoveRule(rule);
48 }
49 return CreateUndefined(env);
50 }
51
GetRule(napi_env env,napi_callback_info info)52 napi_value GetRule(napi_env env, napi_callback_info info)
53 {
54 uint64_t rule = HiChecker::GetRule();
55 return ToUInt64Value(env, rule);
56 }
57
Contains(napi_env env,napi_callback_info info)58 napi_value Contains(napi_env env, napi_callback_info info)
59 {
60 uint64_t rule = GetRuleParam(env, info);
61 napi_value result = nullptr;
62 napi_get_boolean(env, HiChecker::Contains(rule), &result);
63 return result;
64 }
65
AddCheckRule(napi_env env,napi_callback_info info)66 napi_value AddCheckRule(napi_env env, napi_callback_info info)
67 {
68 uint64_t rule = GetRuleParam(env, info);
69 if (rule != GET_RULE_PARAM_FAIL) {
70 HiChecker::AddRule(rule);
71 } else {
72 ThrowError(env, ERR_PARAM);
73 }
74 return CreateUndefined(env);
75 }
76
RemoveCheckRule(napi_env env,napi_callback_info info)77 napi_value RemoveCheckRule(napi_env env, napi_callback_info info)
78 {
79 uint64_t rule = GetRuleParam(env, info);
80 if (rule != GET_RULE_PARAM_FAIL) {
81 HiChecker::RemoveRule(rule);
82 } else {
83 ThrowError(env, ERR_PARAM);
84 }
85 return CreateUndefined(env);
86 }
87
ContainsCheckRule(napi_env env,napi_callback_info info)88 napi_value ContainsCheckRule(napi_env env, napi_callback_info info)
89 {
90 napi_value result = nullptr;
91 uint64_t rule = GetRuleParam(env, info);
92 if (rule == GET_RULE_PARAM_FAIL) {
93 ThrowError(env, ERR_PARAM);
94 }
95 napi_get_boolean(env, HiChecker::Contains(rule), &result);
96 return result;
97 }
98
DeclareHiCheckerInterface(napi_env env,napi_value exports)99 napi_value DeclareHiCheckerInterface(napi_env env, napi_value exports)
100 {
101 napi_property_descriptor desc[] = {
102 DECLARE_NAPI_FUNCTION("addRule", AddRule),
103 DECLARE_NAPI_FUNCTION("removeRule", RemoveRule),
104 DECLARE_NAPI_FUNCTION("getRule", GetRule),
105 DECLARE_NAPI_FUNCTION("contains", Contains),
106 DECLARE_NAPI_FUNCTION("addCheckRule", AddCheckRule),
107 DECLARE_NAPI_FUNCTION("removeCheckRule", RemoveCheckRule),
108 DECLARE_NAPI_FUNCTION("containsCheckRule", ContainsCheckRule),
109 };
110 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
111 DeclareHiCheckerRuleEnum(env, exports);
112 return exports;
113 }
114
DeclareHiCheckerRuleEnum(napi_env env,napi_value exports)115 napi_value DeclareHiCheckerRuleEnum(napi_env env, napi_value exports)
116 {
117 napi_property_descriptor desc[] = {
118 DECLARE_NAPI_STATIC_PROPERTY("RULE_CAUTION_PRINT_LOG", ToUInt64Value(env, Rule::RULE_CAUTION_PRINT_LOG)),
119 DECLARE_NAPI_STATIC_PROPERTY("RULE_CAUTION_TRIGGER_CRASH",
120 ToUInt64Value(env, Rule::RULE_CAUTION_TRIGGER_CRASH)),
121 DECLARE_NAPI_STATIC_PROPERTY("RULE_THREAD_CHECK_SLOW_PROCESS",
122 ToUInt64Value(env, Rule::RULE_THREAD_CHECK_SLOW_PROCESS)),
123 DECLARE_NAPI_STATIC_PROPERTY("RULE_CHECK_SLOW_EVENT", ToUInt64Value(env, Rule::RULE_CHECK_SLOW_EVENT)),
124 DECLARE_NAPI_STATIC_PROPERTY("RULE_CHECK_ABILITY_CONNECTION_LEAK",
125 ToUInt64Value(env, Rule::RULE_CHECK_ABILITY_CONNECTION_LEAK)),
126 DECLARE_NAPI_STATIC_PROPERTY("RULE_CHECK_ARKUI_PERFORMANCE",
127 ToUInt64Value(env, Rule::RULE_CHECK_ARKUI_PERFORMANCE)),
128 };
129 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
130 return exports;
131 }
132
ToUInt64Value(napi_env env,uint64_t value)133 napi_value ToUInt64Value(napi_env env, uint64_t value)
134 {
135 napi_value staticValue = nullptr;
136 napi_create_bigint_uint64(env, value, &staticValue);
137 return staticValue;
138 }
139
CreateUndefined(napi_env env)140 napi_value CreateUndefined(napi_env env)
141 {
142 napi_value result = nullptr;
143 napi_get_undefined(env, &result);
144 return result;
145 }
146
ThrowError(napi_env env,int errCode)147 void ThrowError(napi_env env, int errCode)
148 {
149 std::map<int, std::string> errMap = {
150 { ERR_PARAM, "Invalid input parameter!" },
151 };
152 if (errMap.find(errCode) != errMap.end()) {
153 napi_throw_error(env, std::to_string(errCode).c_str(), errMap[errCode].c_str());
154 }
155 return;
156 }
157
GetRuleParam(napi_env env,napi_callback_info info)158 uint64_t GetRuleParam(napi_env env, napi_callback_info info)
159 {
160 size_t argc = ONE_VALUE_LIMIT;
161 napi_value argv[ONE_VALUE_LIMIT] = { nullptr };
162 napi_value thisVar = nullptr;
163 void *data = nullptr;
164 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
165 if (argc != ONE_VALUE_LIMIT) {
166 HiLog::Error(LABEL, "invalid number=%{public}d of params.", ONE_VALUE_LIMIT);
167 return GET_RULE_PARAM_FAIL;
168 }
169 if (!MatchValueType(env, argv[ARRAY_INDEX_FIRST], napi_bigint)) {
170 HiLog::Error(LABEL, "Type error, should be bigint type!");
171 return GET_RULE_PARAM_FAIL;
172 }
173 uint64_t rule = GET_RULE_PARAM_FAIL;
174 bool lossless = true;
175 napi_get_value_bigint_uint64(env, argv[ARRAY_INDEX_FIRST], &rule, &lossless);
176 if (!lossless) {
177 HiLog::Error(LABEL, "Type error, bigint should be 64!");
178 return GET_RULE_PARAM_FAIL;
179 }
180 if (rule == GET_RULE_PARAM_FAIL) {
181 HiLog::Error(LABEL, "invalid input, please check!");
182 }
183 return rule;
184 }
185
MatchValueType(napi_env env,napi_value value,napi_valuetype targetType)186 bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
187 {
188 napi_valuetype valueType = napi_undefined;
189 napi_typeof(env, value, &valueType);
190 return valueType == targetType;
191 }
192
193 static napi_module g_module = {
194 .nm_version = 1,
195 .nm_flags = 0,
196 .nm_filename = nullptr,
197 .nm_register_func = DeclareHiCheckerInterface,
198 .nm_modname = "hichecker",
199 .nm_priv = ((void *)0),
200 .reserved = {0}
201 };
202
HiCheckerRegisterModule(void)203 extern "C" __attribute__((constructor)) void HiCheckerRegisterModule(void)
204 {
205 napi_module_register(&g_module);
206 }
207 } // HiviewDFX
208 } // OHOS