• 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 #ifndef HIVIEWDFX_NAPI_HISYSEVENT_UTIL_H
17 #define HIVIEWDFX_NAPI_HISYSEVENT_UTIL_H
18 
19 #include <memory>
20 #include <string>
21 #include <sys/syscall.h>
22 #include <vector>
23 #include <unistd.h>
24 #include <unordered_map>
25 
26 #include "hisysevent_manager.h"
27 #include "napi/native_api.h"
28 #include "napi/native_node_api.h"
29 #include "napi_hisysevent_adapter.h"
30 #include "napi_callback_context.h"
31 
32 namespace OHOS {
33 namespace HiviewDFX {
34 class NapiHiSysEventUtil {
35 public:
36     static constexpr char DOMAIN_ATTR[] = "domain";
37     static constexpr char NAME_ATTR[] = "name";
38     static constexpr char EVENT_TYPE_ATTR[] = "eventType";
39 
40 public:
41     static void ParseHiSysEventInfo(const napi_env env, napi_value* param, size_t paramNum, HiSysEventInfo& info);
42     static bool HasStrParamLenOverLimit(HiSysEventInfo& info);
43     static void CreateHiSysEventInfoJsObject(const napi_env env, const std::string& eventDetail,
44         napi_value& sysEventInfo);
45     static void AppendStringPropertyToJsObject(const napi_env env, const std::string& key,
46         const std::string& value, napi_value& jsObj);
47     static void AppendInt32PropertyToJsObject(const napi_env env, const std::string& key,
48         const int32_t& value, napi_value& jsObj);
49     static void CreateJsSysEventInfoArray(const napi_env env, const std::vector<std::string>& originValues,
50         napi_value& array);
51     static int32_t ParseListenerRules(const napi_env env, napi_value& jsObj,
52         std::vector<ListenerRule>& listenerRules);
53     static int32_t ParseQueryRules(const napi_env env, napi_value& jsObj, std::vector<QueryRule>& queryRules);
54     static int32_t ParseQueryArg(const napi_env env, napi_value& jsObj, QueryArg& queryArg);
55     static void CreateNull(const napi_env env, napi_value& ret);
56     static void CreateInt32Value(const napi_env env, int32_t value, napi_value& ret);
57     static void CreateInt64Value(const napi_env env, int64_t value, napi_value& ret);
58     static void CreateUInt64Value(const napi_env env, uint64_t value, napi_value& ret);
59     static void CreateStringValue(const napi_env env, std::string value, napi_value& ret);
60     static napi_value GetPropertyByName(const napi_env env, const napi_value& object,
61         const std::string& propertyName);
62 
63 public:
64     static void ThrowParamMandatoryError(napi_env env, const std::string paramName);
65     static void ThrowParamTypeError(napi_env env, const std::string paramName, std::string paramType);
66     static napi_value CreateError(napi_env env, const int32_t code, const std::string& msg);
67     static napi_value CreateErrorByRet(napi_env env, const int32_t retCode);
68     static void ThrowErrorByRet(napi_env env, const int32_t retCode);
69 
70 public:
71     template<typename T>
72     static typename std::unordered_map<napi_ref, std::pair<pid_t, std::shared_ptr<T>>>::iterator
CompareAndReturnCacheItem(const napi_env env,napi_value & standard,std::unordered_map<napi_ref,std::pair<pid_t,std::shared_ptr<T>>> & resources)73     CompareAndReturnCacheItem(const napi_env env, napi_value& standard,
74         std::unordered_map<napi_ref, std::pair<pid_t, std::shared_ptr<T>>>& resources)
75     {
76         bool found = false;
77         napi_status status;
78         auto iter = resources.begin();
79         for (; iter != resources.end(); iter++) {
80             if (iter->second.first != syscall(SYS_gettid)) { // avoid error caused by vm run in multi-thread
81                 continue;
82             }
83             napi_value val = nullptr;
84             status = napi_get_reference_value(env, iter->first, &val);
85             if (status != napi_ok) {
86                 continue;
87             }
88             status = napi_strict_equals(env, standard, val, &found);
89             if (status != napi_ok) {
90                 continue;
91             }
92             if (found) {
93                 break;
94             }
95         }
96         return iter;
97     }
98 
99 private:
100     static std::pair<int32_t, std::string> GetErrorDetailByRet(napi_env env, int32_t retCode);
101     static void ThrowError(napi_env env, int32_t code, const std::string& msg);
102 };
103 } // namespace HiviewDFX
104 } // namespace OHOS
105 
106 #endif // HIVIEWDFX_NAPI_HISYSEVENT_UTIL_H
107