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