1 /* 2 * Copyright (c) 2021-2024 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 #ifndef HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_HIAPPEVENT_BUILDER_H 16 #define HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_HIAPPEVENT_BUILDER_H 17 18 #include <memory> 19 20 #include "napi/native_api.h" 21 #include "napi/native_node_api.h" 22 23 namespace OHOS { 24 namespace HiviewDFX { 25 class AppEventPack; 26 27 class NapiHiAppEventBuilder { 28 public: NapiHiAppEventBuilder()29 NapiHiAppEventBuilder() : isV9_(false), result_(0), callback_(nullptr), appEventPack_(nullptr) {} ~NapiHiAppEventBuilder()30 ~NapiHiAppEventBuilder() {} 31 int GetResult() const; 32 napi_ref GetCallback() const; 33 std::shared_ptr<AppEventPack> Build(const napi_env env, const napi_value params[], size_t len); 34 std::shared_ptr<AppEventPack> BuildV9(const napi_env env, const napi_value params[], size_t len); 35 36 protected: 37 virtual void AddArrayParam2EventPack(napi_env env, const std::string &key, const napi_value arr); 38 virtual void AddParams2EventPack(napi_env env, const napi_value paramObj); 39 void AddParam2EventPack(napi_env env, const std::string &key, const napi_value value); 40 bool IsValidEventDomain(const napi_env env, const napi_value domain); 41 bool IsValidEventName(const napi_env env, const napi_value name); 42 bool IsValidEventParam(const napi_env env, const napi_value param); 43 44 private: 45 bool IsValidEventType(const napi_env env, const napi_value type); 46 bool IsValidEventInfo(const napi_env env, const napi_value eventInfo); 47 bool IsOldWriteParams(const napi_env env, const napi_value params[], size_t len); 48 bool IsNewWriteParams(const napi_env env, const napi_value params[], size_t len); 49 void BuildEventPack(napi_env env, const napi_value params[]); 50 void BuildEventPack(napi_env env, const napi_value eventInfo); 51 void BuildCallback(const napi_env env, const napi_value callback); 52 53 protected: 54 bool isV9_; 55 int result_; 56 napi_ref callback_; 57 std::shared_ptr<AppEventPack> appEventPack_; 58 }; 59 } // namespace HiviewDFX 60 } // namespace OHOS 61 #endif // HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_HIAPPEVENT_BUILDER_H 62