• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "napi_hiappevent_init.h"
16 
17 #include <map>
18 #include <string>
19 
20 #include "napi_util.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace NapiHiAppEventInit {
25 namespace {
26 constexpr int FAULT_EVENT_TYPE = 1;
27 constexpr int STATISTIC_EVENT_TYPE = 2;
28 constexpr int SECURITY_EVENT_TYPE = 3;
29 constexpr int BEHAVIOR_EVENT_TYPE = 4;
30 
31 const std::string EVENT_CLASS_NAME = "Event";
32 const std::string EVENT_CLASS_NAME_V9 = "event";
33 const std::string PARAM_CLASS_NAME = "Param";
34 const std::string PARAM_CLASS_NAME_V9 = "param";
35 const std::string EVENT_TYPE_CLASS_NAME = "EventType";
36 
ClassConstructor(napi_env env,napi_callback_info info)37 napi_value ClassConstructor(napi_env env, napi_callback_info info)
38 {
39     size_t argc = 0;
40     napi_value argv = nullptr;
41     napi_value thisArg = nullptr;
42     void* data = nullptr;
43     napi_get_cb_info(env, info, &argc, &argv, &thisArg, &data);
44 
45     napi_value global = 0;
46     napi_get_global(env, &global);
47 
48     return thisArg;
49 }
50 
InitEventTypeMap(napi_env env,std::map<const char *,napi_value> & eventTypeMap)51 void InitEventTypeMap(napi_env env, std::map<const char*, napi_value>& eventTypeMap)
52 {
53     eventTypeMap["FAULT"] = NapiUtil::CreateInt32(env, FAULT_EVENT_TYPE);
54     eventTypeMap["STATISTIC"] = NapiUtil::CreateInt32(env, STATISTIC_EVENT_TYPE);
55     eventTypeMap["SECURITY"] = NapiUtil::CreateInt32(env, SECURITY_EVENT_TYPE);
56     eventTypeMap["BEHAVIOR"] = NapiUtil::CreateInt32(env, BEHAVIOR_EVENT_TYPE);
57 }
58 
InitEventMap(napi_env env,std::map<const char *,napi_value> & eventMap)59 void InitEventMap(napi_env env, std::map<const char*, napi_value>& eventMap)
60 {
61     eventMap["USER_LOGIN"] = NapiUtil::CreateString(env, "hiappevent.user_login");
62     eventMap["USER_LOGOUT"] = NapiUtil::CreateString(env, "hiappevent.user_logout");
63     eventMap["DISTRIBUTED_SERVICE_START"] = NapiUtil::CreateString(env, "hiappevent.distributed_service_start");
64 }
65 
InitParamMap(napi_env env,std::map<const char *,napi_value> & paramMap)66 void InitParamMap(napi_env env, std::map<const char*, napi_value>& paramMap)
67 {
68     paramMap["USER_ID"] = NapiUtil::CreateString(env, "user_id");
69     paramMap["DISTRIBUTED_SERVICE_NAME"] = NapiUtil::CreateString(env, "ds_name");
70     paramMap["DISTRIBUTED_SERVICE_INSTANCE_ID"] = NapiUtil::CreateString(env, "ds_instance_id");
71 }
72 
InitConstClassByName(napi_env env,napi_value exports,const std::string & name)73 void InitConstClassByName(napi_env env, napi_value exports, const std::string& name)
74 {
75     std::map<const char*, napi_value> propertyMap;
76     if (name == EVENT_CLASS_NAME || name == EVENT_CLASS_NAME_V9) {
77         InitEventMap(env, propertyMap);
78     } else if (name == PARAM_CLASS_NAME || name == PARAM_CLASS_NAME_V9) {
79         InitParamMap(env, propertyMap);
80     } else if (name == EVENT_TYPE_CLASS_NAME) {
81         InitEventTypeMap(env, propertyMap);
82     } else {
83         return;
84     }
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 
92     napi_value result = nullptr;
93     napi_define_class(env, name.c_str(), NAPI_AUTO_LENGTH, ClassConstructor, nullptr,
94         sizeof(descriptors) / sizeof(*descriptors), descriptors, &result);
95     napi_set_named_property(env, exports, name.c_str(), result);
96 }
97 }
98 
InitNapiClass(napi_env env,napi_value exports)99 napi_value InitNapiClass(napi_env env, napi_value exports)
100 {
101     InitConstClassByName(env, exports, EVENT_CLASS_NAME);
102     InitConstClassByName(env, exports, PARAM_CLASS_NAME);
103     InitConstClassByName(env, exports, EVENT_TYPE_CLASS_NAME);
104     return exports;
105 }
106 
InitNapiClassV9(napi_env env,napi_value exports)107 napi_value InitNapiClassV9(napi_env env, napi_value exports)
108 {
109     InitConstClassByName(env, exports, EVENT_CLASS_NAME_V9);
110     InitConstClassByName(env, exports, PARAM_CLASS_NAME_V9);
111     InitConstClassByName(env, exports, EVENT_TYPE_CLASS_NAME);
112     return exports;
113 }
114 } // namespace NapiHiAppEventInit
115 } // namespace HiviewDFX
116 } // namespace OHOS
117