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_hisysevent_init.h"
17
18 #include <map>
19
20 #include "hisysevent.h"
21 #include "rule_type.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace {
26 constexpr char EVENT_TYPE_ENUM_NAME[] = "EventType";
27 constexpr char RULE_TYPE_ENUM_NAME[] = "RuleType";
28
ClassConstructor(napi_env env,napi_callback_info info)29 napi_value ClassConstructor(napi_env env, napi_callback_info info)
30 {
31 size_t argc = 0;
32 napi_value argv = nullptr;
33 napi_value thisArg = nullptr;
34 void* data = nullptr;
35 napi_get_cb_info(env, info, &argc, &argv, &thisArg, &data);
36
37 napi_value global = 0;
38 napi_get_global(env, &global);
39
40 return thisArg;
41 }
42
InitEventTypeEnum(napi_env env,std::map<const char *,napi_value> & eventTypeMap)43 void InitEventTypeEnum(napi_env env,
44 std::map<const char*, napi_value>& eventTypeMap)
45 {
46 napi_value fault = nullptr;
47 napi_create_int32(env, HiSysEvent::FAULT, &fault);
48 napi_value statistic = nullptr;
49 napi_create_int32(env, HiSysEvent::STATISTIC, &statistic);
50 napi_value security = nullptr;
51 napi_create_int32(env, HiSysEvent::SECURITY, &security);
52 napi_value behavior = nullptr;
53 napi_create_int32(env, HiSysEvent::BEHAVIOR, &behavior);
54
55 eventTypeMap["FAULT"] = fault;
56 eventTypeMap["STATISTIC"] = statistic;
57 eventTypeMap["SECURITY"] = security;
58 eventTypeMap["BEHAVIOR"] = behavior;
59 }
60
InitRuleTypeEnum(napi_env env,std::map<const char *,napi_value> & ruleTypeMap)61 void InitRuleTypeEnum(napi_env env,
62 std::map<const char*, napi_value>& ruleTypeMap)
63 {
64 napi_value wholeWord = nullptr;
65 napi_create_int32(env, WHOLE_WORD, &wholeWord);
66 napi_value prefix = nullptr;
67 napi_create_int32(env, PREFIX, &prefix);
68 napi_value regular = nullptr;
69 napi_create_int32(env, REGULAR, ®ular);
70
71 ruleTypeMap["WHOLE_WORD"] = wholeWord;
72 ruleTypeMap["PREFIX"] = prefix;
73 ruleTypeMap["REGULAR"] = regular;
74 }
75
InitConstClassByName(napi_env env,napi_value exports,std::string name)76 void InitConstClassByName(napi_env env, napi_value exports, std::string name)
77 {
78 std::map<const char*, napi_value> propertyMap;
79 if (name == EVENT_TYPE_ENUM_NAME) {
80 InitEventTypeEnum(env, propertyMap);
81 } else if (name == RULE_TYPE_ENUM_NAME) {
82 InitRuleTypeEnum(env, propertyMap);
83 } else {
84 return;
85 }
86 int i = 0;
87 napi_property_descriptor descriptors[propertyMap.size()];
88 for (auto& it : propertyMap) {
89 descriptors[i++] = DECLARE_NAPI_STATIC_PROPERTY(it.first, it.second);
90 }
91 napi_value result = nullptr;
92 napi_define_class(env, name.c_str(), NAPI_AUTO_LENGTH, ClassConstructor,
93 nullptr, sizeof(descriptors) / sizeof(*descriptors), descriptors, &result);
94 napi_set_named_property(env, exports, name.c_str(), result);
95 }
96 }
InitNapiClass(napi_env env,napi_value exports)97 napi_value InitNapiClass(napi_env env, napi_value exports)
98 {
99 InitConstClassByName(env, exports, EVENT_TYPE_ENUM_NAME);
100 InitConstClassByName(env, exports, RULE_TYPE_ENUM_NAME);
101 return exports;
102 }
103 } // namespace HiviewDFX
104 } // namespace OHOS