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 static bool IsNullOrUndefined(napi_env env, const napi_value& val); 63 64 public: 65 static void ThrowParamMandatoryError(napi_env env, const std::string paramName); 66 static void ThrowParamTypeError(napi_env env, const std::string paramName, std::string paramType); 67 static void ThrowSystemAppPermissionError(napi_env env); 68 static napi_value CreateError(napi_env env, const int32_t code, const std::string& msg); 69 static napi_value CreateErrorByRet(napi_env env, const int32_t retCode); 70 static void ThrowErrorByRet(napi_env env, const int32_t retCode); 71 72 public: 73 static bool IsSystemAppCall(); 74 75 public: 76 template<typename T> 77 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)78 CompareAndReturnCacheItem(const napi_env env, napi_value& standard, 79 std::unordered_map<napi_ref, std::pair<pid_t, std::shared_ptr<T>>>& resources) 80 { 81 bool found = false; 82 napi_status status; 83 auto iter = resources.begin(); 84 for (; iter != resources.end(); iter++) { 85 if (iter->second.first != getproctid()) { // avoid error caused by vm run in multi-thread 86 continue; 87 } 88 napi_value val = nullptr; 89 status = napi_get_reference_value(env, iter->first, &val); 90 if (status != napi_ok) { 91 continue; 92 } 93 status = napi_strict_equals(env, standard, val, &found); 94 if (status != napi_ok) { 95 continue; 96 } 97 if (found) { 98 break; 99 } 100 } 101 return iter; 102 } 103 104 private: 105 static std::pair<int32_t, std::string> GetErrorDetailByRet(napi_env env, int32_t retCode); 106 static void ThrowError(napi_env env, int32_t code, const std::string& msg); 107 }; 108 } // namespace HiviewDFX 109 } // namespace OHOS 110 111 #endif // HIVIEWDFX_NAPI_HISYSEVENT_UTIL_H 112