1 /* 2 * Copyright (c) 2022-2023 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_ADAPTER_H 17 #define HIVIEWDFX_NAPI_HISYSEVENT_ADAPTER_H 18 19 #include <string> 20 #include <unordered_map> 21 #include <vector> 22 23 #include "hisysevent.h" 24 #include "inner_writer.h" 25 #include "napi/native_api.h" 26 #include "napi/native_node_api.h" 27 #include "stringfilter.h" 28 #include "write_controller.h" 29 30 namespace OHOS { 31 namespace HiviewDFX { 32 using namespace Encoded; 33 using JsCallerInfo = std::pair<std::string, int64_t>; 34 using HiSysEventInfo = struct HiSysEventInfo { 35 std::string domain; 36 std::string name; 37 HiSysEvent::EventType eventType; 38 std::unordered_map<std::string, bool> boolParams; 39 std::unordered_map<std::string, std::vector<bool>> boolArrayParams; 40 std::unordered_map<std::string, double> doubleParams; 41 std::unordered_map<std::string, std::vector<double>> doubleArrayParams; 42 std::unordered_map<std::string, std::string> stringParams; 43 std::unordered_map<std::string, std::vector<std::string>> stringArrayParams; 44 }; 45 46 using HiSysEventAsyncContext = struct HiSysEventAsyncContext { 47 napi_env env; 48 napi_async_work asyncWork; 49 napi_deferred deferred; 50 napi_ref callback; 51 HiSysEventInfo eventInfo; 52 int eventWroteResult; 53 JsCallerInfo jsCallerInfo; 54 }; 55 56 class NapiHiSysEventAdapter { 57 public: 58 static void Write(const napi_env env, HiSysEventAsyncContext* eventAsyncContext); 59 static void ParseJsCallerInfo(const napi_env env, JsCallerInfo& callerInfo); 60 61 private: 62 static void CheckThenWriteSysEvent(HiSysEventAsyncContext* eventAsyncContext); 63 static void InnerWrite(InnerWriter::EventBase& eventBase, const HiSysEventInfo& eventInfo); 64 static int Write(const HiSysEventInfo& eventInfo); 65 66 private: 67 template<typename T> AppendParams(InnerWriter::EventBase & eventBase,const std::unordered_map<std::string,T> & params)68 static void AppendParams(InnerWriter::EventBase& eventBase, const std::unordered_map<std::string, T>& params) 69 { 70 for (auto iter = params.cbegin(); iter != params.cend(); ++iter) { 71 InnerWriter::InnerWrite(eventBase, iter->first, iter->second); 72 } 73 } 74 }; 75 } // namespace HiviewDFX 76 } // namespace OHOS 77 78 #endif // HIVIEWDFX_NAPI_HISYSEVENT_ADAPTER_H