1 /*
2 * Copyright (c) 2021-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 #include "hiappevent_base.h"
16 #include "hiappevent_config.h"
17 #include "hiappevent_verify.h"
18 #include "hilog/log.h"
19 #include "napi_hiappevent_builder.h"
20 #include "napi_hiappevent_config.h"
21 #include "napi_hiappevent_init.h"
22 #include "napi_hiappevent_write.h"
23 #include "napi_util.h"
24
25 #undef LOG_DOMAIN
26 #define LOG_DOMAIN 0xD002D07
27
28 #undef LOG_TAG
29 #define LOG_TAG "Napi"
30
31 using namespace OHOS::HiviewDFX;
32
Write(napi_env env,napi_callback_info info)33 static napi_value Write(napi_env env, napi_callback_info info)
34 {
35 constexpr size_t PARAM_NUM = 4; // max param num is 4
36 size_t paramNum = PARAM_NUM;
37 napi_value params[PARAM_NUM] = { 0 };
38 NAPI_CALL(env, napi_get_cb_info(env, info, ¶mNum, params, nullptr, nullptr));
39
40 auto asyncContext = new(std::nothrow) NapiHiAppEventWrite::HiAppEventAsyncContext(env);
41 if (asyncContext == nullptr) {
42 HILOG_ERROR(LOG_CORE, "failed to new asyncContext.");
43 return NapiUtil::CreateUndefined(env);
44 }
45
46 // 1. build AppEventPack object
47 NapiHiAppEventBuilder builder;
48 asyncContext->appEventPack = builder.Build(env, params, paramNum);
49 asyncContext->result = builder.GetResult();
50 asyncContext->callback = builder.GetCallback();
51
52 // 2. if the build is successful, the event verification is performed
53 if (asyncContext->result >= 0) {
54 if (auto ret = VerifyAppEvent(asyncContext->appEventPack); ret != 0) {
55 asyncContext->result = ret;
56 }
57 }
58
59 // 3. set promise object if callback is null
60 napi_value promise = NapiUtil::CreateUndefined(env);
61 if (asyncContext->callback == nullptr) {
62 napi_create_promise(env, &asyncContext->deferred, &promise);
63 }
64
65 // 4. try to write the event to file
66 NapiHiAppEventWrite::Write(env, asyncContext);
67 return promise;
68 }
69
Configure(napi_env env,napi_callback_info info)70 static napi_value Configure(napi_env env, napi_callback_info info)
71 {
72 constexpr size_t PARAM_NUM = 1; // param num is 1
73 size_t paramNum = PARAM_NUM;
74 napi_value params[PARAM_NUM] = { 0 };
75 NAPI_CALL(env, napi_get_cb_info(env, info, ¶mNum, params, nullptr, nullptr));
76 if (paramNum != PARAM_NUM) {
77 HILOG_ERROR(LOG_CORE, "failed to check the number of configure param");
78 return NapiUtil::CreateBoolean(env, false);
79 }
80 return NapiUtil::CreateBoolean(env, NapiHiAppEventConfig::Configure(env, params[0]));
81 }
82
83 EXTERN_C_START
Init(napi_env env,napi_value exports)84 static napi_value Init(napi_env env, napi_value exports)
85 {
86 napi_property_descriptor desc[] = {
87 DECLARE_NAPI_FUNCTION("write", Write),
88 DECLARE_NAPI_FUNCTION("configure", Configure)
89 };
90 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
91
92 // init EventType class, Event class and Param class
93 NapiHiAppEventInit::InitNapiClass(env, exports);
94 return exports;
95 }
96 EXTERN_C_END
97
98 static napi_module _module = {
99 .nm_version = 1,
100 .nm_flags = 0,
101 .nm_filename = nullptr,
102 .nm_register_func = Init,
103 .nm_modname = "hiAppEvent",
104 .nm_priv = ((void *)0),
105 .reserved = {0}
106 };
107
RegisterModule(void)108 extern "C" __attribute__((constructor)) void RegisterModule(void)
109 {
110 napi_module_register(&_module);
111 }
112