1 /*
2 * Copyright (c) 2022-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 #include "hiappevent_base.h"
16 #include "hiappevent_clean.h"
17 #include "hiappevent_verify.h"
18 #include "hilog/log.h"
19 #include "napi_app_event_holder.h"
20 #include "napi_error.h"
21 #include "napi_hiappevent_builder.h"
22 #include "napi_hiappevent_config.h"
23 #include "napi_hiappevent_processor.h"
24 #include "napi_hiappevent_userinfo.h"
25 #include "napi_hiappevent_init.h"
26 #include "napi_hiappevent_watch.h"
27 #include "napi_hiappevent_write.h"
28 #include "napi_param_builder.h"
29 #include "napi_util.h"
30
31 #undef LOG_DOMAIN
32 #define LOG_DOMAIN 0xD002D07
33
34 #undef LOG_TAG
35 #define LOG_TAG "HiAppEventNapi"
36
37 using namespace OHOS::HiviewDFX;
38
39 namespace {
40 constexpr size_t MAX_PARAM_NUM = 4;
41 }
42
AddProcessor(napi_env env,napi_callback_info info)43 static napi_value AddProcessor(napi_env env, napi_callback_info info)
44 {
45 napi_value params[MAX_PARAM_NUM] = { 0 };
46 if (NapiUtil::GetCbInfo(env, info, params) < 1) { // The min num of params for addProcessor is 1
47 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("addProcessor"));
48 return nullptr;
49 }
50 napi_value id = nullptr;
51 if (!NapiHiAppEventProcessor::AddProcessor(env, params[0], id)) {
52 HILOG_ERROR(LOG_CORE, "failed to add processor");
53 }
54 return id;
55 }
56
RemoveProcessor(napi_env env,napi_callback_info info)57 static napi_value RemoveProcessor(napi_env env, napi_callback_info info)
58 {
59 napi_value params[MAX_PARAM_NUM] = { 0 };
60 if (NapiUtil::GetCbInfo(env, info, params) < 1) { // The min num of params for removeProcessor is 1
61 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("removeProcessor"));
62 return nullptr;
63 }
64 if (!NapiHiAppEventProcessor::RemoveProcessor(env, params[0])) {
65 HILOG_ERROR(LOG_CORE, "failed to remove processor");
66 }
67 return nullptr;
68 }
69
Write(napi_env env,napi_callback_info info)70 static napi_value Write(napi_env env, napi_callback_info info)
71 {
72 napi_value params[MAX_PARAM_NUM] = { 0 };
73 size_t paramNum = NapiUtil::GetCbInfo(env, info, params);
74 NapiHiAppEventBuilder builder;
75 auto appEventPack = builder.BuildV9(env, params, paramNum);
76 if (appEventPack == nullptr) {
77 HILOG_ERROR(LOG_CORE, "failed to build appEventPack.");
78 return nullptr;
79 }
80
81 auto asyncContext = new(std::nothrow) NapiHiAppEventWrite::HiAppEventAsyncContext(env);
82 if (asyncContext == nullptr) {
83 HILOG_ERROR(LOG_CORE, "failed to new asyncContext.");
84 return nullptr;
85 }
86 asyncContext->appEventPack = appEventPack;
87 asyncContext->result = builder.GetResult();
88 asyncContext->callback = builder.GetCallback();
89 asyncContext->isV9 = true;
90
91 // if the build is successful, the event verification is performed
92 if (asyncContext->result >= 0) {
93 if (auto ret = VerifyAppEvent(asyncContext->appEventPack); ret != 0) {
94 asyncContext->result = ret;
95 }
96 }
97
98 napi_value promise = nullptr;
99 if (asyncContext->callback == nullptr) {
100 napi_create_promise(env, &asyncContext->deferred, &promise);
101 }
102
103 NapiHiAppEventWrite::Write(env, asyncContext);
104 return promise;
105 }
106
Configure(napi_env env,napi_callback_info info)107 static napi_value Configure(napi_env env, napi_callback_info info)
108 {
109 napi_value params[MAX_PARAM_NUM] = { 0 };
110 if (NapiUtil::GetCbInfo(env, info, params) < 1) { // The min num of params for configure is 1
111 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("config"));
112 return nullptr;
113 }
114 if (!NapiHiAppEventConfig::Configure(env, params[0], true)) {
115 HILOG_ERROR(LOG_CORE, "failed to configure HiAppEvent");
116 }
117 return nullptr;
118 }
119
SetUserId(napi_env env,napi_callback_info info)120 static napi_value SetUserId(napi_env env, napi_callback_info info)
121 {
122 napi_value params[MAX_PARAM_NUM] = { 0 };
123 if (NapiUtil::GetCbInfo(env, info, params) < 2) { // The min num of params for setUserId is 2
124 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("setUserId"));
125 return nullptr;
126 }
127 if (!NapiHiAppEventUserInfo::SetUserId(env, params[0], params[1])) {
128 HILOG_ERROR(LOG_CORE, "failed to set userId");
129 }
130 return nullptr;
131 }
132
GetUserId(napi_env env,napi_callback_info info)133 static napi_value GetUserId(napi_env env, napi_callback_info info)
134 {
135 napi_value params[MAX_PARAM_NUM] = { 0 };
136 if (NapiUtil::GetCbInfo(env, info, params) < 1) { // The min num of params for getUserId is 1
137 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("getUserId"));
138 return nullptr;
139 }
140
141 napi_value userId = nullptr;
142 if (!NapiHiAppEventUserInfo::GetUserId(env, params[0], userId)) {
143 HILOG_ERROR(LOG_CORE, "failed to get userId");
144 }
145 return userId;
146 }
147
SetUserProperty(napi_env env,napi_callback_info info)148 static napi_value SetUserProperty(napi_env env, napi_callback_info info)
149 {
150 napi_value params[MAX_PARAM_NUM] = { 0 };
151 if (NapiUtil::GetCbInfo(env, info, params) < 2) { // The min num of params for setUserProperty is 2
152 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("setUserProperty"));
153 return nullptr;
154 }
155 if (!NapiHiAppEventUserInfo::SetUserProperty(env, params[0], params[1])) {
156 HILOG_ERROR(LOG_CORE, "failed to set userProperty");
157 }
158 return nullptr;
159 }
160
GetUserProperty(napi_env env,napi_callback_info info)161 static napi_value GetUserProperty(napi_env env, napi_callback_info info)
162 {
163 napi_value params[MAX_PARAM_NUM] = { 0 };
164 if (NapiUtil::GetCbInfo(env, info, params) < 1) { // The min num of params for getUserProperty is 1
165 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("getUserProperty"));
166 return nullptr;
167 }
168
169 napi_value userProperty = nullptr;
170 if (!NapiHiAppEventUserInfo::GetUserProperty(env, params[0], userProperty)) {
171 HILOG_ERROR(LOG_CORE, "failed to get userProperty");
172 }
173 return userProperty;
174 }
175
ClearData(napi_env env,napi_callback_info info)176 static napi_value ClearData(napi_env env, napi_callback_info info)
177 {
178 HiAppEventClean::ClearData(NapiHiAppEventConfig::GetStorageDir());
179 return NapiUtil::CreateUndefined(env);
180 }
181
AddWatcher(napi_env env,napi_callback_info info)182 static napi_value AddWatcher(napi_env env, napi_callback_info info)
183 {
184 napi_value params[MAX_PARAM_NUM] = { 0 };
185 if (NapiUtil::GetCbInfo(env, info, params) < 1) { // The min num of params for addWatcher is 1
186 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("watcher"));
187 return nullptr;
188 }
189 return NapiHiAppEventWatch::AddWatcher(env, params[0]);
190 }
191
RemoveWatcher(napi_env env,napi_callback_info info)192 static napi_value RemoveWatcher(napi_env env, napi_callback_info info)
193 {
194 napi_value params[MAX_PARAM_NUM] = { 0 };
195 if (NapiUtil::GetCbInfo(env, info, params) < 1) { // The min num of params for removeWatcher is 1
196 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("watcher"));
197 return nullptr;
198 }
199 return NapiHiAppEventWatch::RemoveWatcher(env, params[0]);
200 }
201
SetEventParam(napi_env env,napi_callback_info info)202 static napi_value SetEventParam(napi_env env, napi_callback_info info)
203 {
204 napi_value params[MAX_PARAM_NUM] = { 0 };
205 size_t paramNum = NapiUtil::GetCbInfo(env, info, params);
206 NapiParamBuilder builder;
207 auto appEventPack = builder.BuildEventParam(env, params, paramNum);
208 if (appEventPack == nullptr) {
209 HILOG_ERROR(LOG_CORE, "failed to build appEventPack.");
210 return nullptr;
211 }
212
213 auto asyncContext = new(std::nothrow) NapiHiAppEventWrite::HiAppEventAsyncContext(env);
214 if (asyncContext == nullptr) {
215 HILOG_ERROR(LOG_CORE, "failed to new asyncContext.");
216 return nullptr;
217 }
218 asyncContext->appEventPack = appEventPack;
219 asyncContext->result = builder.GetResult();
220
221 napi_value promise = nullptr;
222 napi_create_promise(env, &asyncContext->deferred, &promise);
223
224 NapiHiAppEventWrite::SetEventParam(env, asyncContext);
225 return promise;
226 }
227
228 EXTERN_C_START
Init(napi_env env,napi_value exports)229 static napi_value Init(napi_env env, napi_value exports)
230 {
231 napi_property_descriptor desc[] = {
232 DECLARE_NAPI_FUNCTION("addProcessor", AddProcessor),
233 DECLARE_NAPI_FUNCTION("removeProcessor", RemoveProcessor),
234 DECLARE_NAPI_FUNCTION("setUserId", SetUserId),
235 DECLARE_NAPI_FUNCTION("getUserId", GetUserId),
236 DECLARE_NAPI_FUNCTION("setUserProperty", SetUserProperty),
237 DECLARE_NAPI_FUNCTION("getUserProperty", GetUserProperty),
238 DECLARE_NAPI_FUNCTION("write", Write),
239 DECLARE_NAPI_FUNCTION("configure", Configure),
240 DECLARE_NAPI_FUNCTION("clearData", ClearData),
241 DECLARE_NAPI_FUNCTION("addWatcher", AddWatcher),
242 DECLARE_NAPI_FUNCTION("removeWatcher", RemoveWatcher),
243 DECLARE_NAPI_FUNCTION("setEventParam", SetEventParam)
244 };
245 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
246 NapiHiAppEventInit::InitNapiClassV9(env, exports);
247 NapiAppEventHolder::NapiExport(env, exports);
248 return exports;
249 }
250 EXTERN_C_END
251
252 static napi_module g_module_v9 = {
253 .nm_version = 1,
254 .nm_flags = 0,
255 .nm_filename = nullptr,
256 .nm_register_func = Init,
257 .nm_modname = "hiviewdfx.hiAppEvent",
258 .nm_priv = ((void *)0),
259 .reserved = {0}
260 };
261
RegisterModule(void)262 extern "C" __attribute__((constructor)) void RegisterModule(void)
263 {
264 napi_module_register(&g_module_v9);
265 }
266