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 #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 private: 37 bool IsValidEventDomain(const napi_env env, const napi_value domain); 38 bool IsValidEventName(const napi_env env, const napi_value name); 39 bool IsValidEventType(const napi_env env, const napi_value type); 40 bool IsValidEventParam(const napi_env env, const napi_value param); 41 bool IsValidEventInfo(const napi_env env, const napi_value eventInfo); 42 bool IsOldWriteParams(const napi_env env, const napi_value params[], size_t len); 43 bool IsNewWriteParams(const napi_env env, const napi_value params[], size_t len); 44 void AddArrayParam2EventPack(napi_env env, const std::string &key, const napi_value arr); 45 void AddParam2EventPack(napi_env env, const std::string &key, const napi_value value); 46 void AddParams2EventPack(napi_env env, const napi_value paramObj); 47 void BuildEventPack(napi_env env, const napi_value params[]); 48 void BuildEventPack(napi_env env, const napi_value eventInfo); 49 void BuildCallback(const napi_env env, const napi_value callback); 50 51 private: 52 bool isV9_; 53 int result_; 54 napi_ref callback_; 55 std::shared_ptr<AppEventPack> appEventPack_; 56 }; 57 } // namespace HiviewDFX 58 } // namespace OHOS 59 #endif // HIAPPEVENT_FRAMEWORKS_JS_NAPI_INCLUDE_NAPI_HIAPPEVENT_BUILDER_H 60