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