• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_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_init.h"
24 #include "napi_hiappevent_watch.h"
25 #include "napi_hiappevent_write.h"
26 #include "napi_util.h"
27 
28 using namespace OHOS::HiviewDFX;
29 
30 namespace {
31 constexpr HiLogLabel LABEL = { LOG_CORE, HIAPPEVENT_DOMAIN, "HiAppEvent_NAPI" };
32 constexpr size_t MAX_PARAM_NUM = 4;
33 }
34 
Write(napi_env env,napi_callback_info info)35 static napi_value Write(napi_env env, napi_callback_info info)
36 {
37     napi_value params[MAX_PARAM_NUM] = { 0 };
38     size_t paramNum = NapiUtil::GetCbInfo(env, info, params);
39     NapiHiAppEventBuilder builder;
40     auto appEventPack = builder.BuildV9(env, params, paramNum);
41     if (appEventPack == nullptr) {
42         HiLog::Error(LABEL, "failed to build appEventPack.");
43         return nullptr;
44     }
45 
46     auto asyncContext = new(std::nothrow) NapiHiAppEventWrite::HiAppEventAsyncContext(env);
47     if (asyncContext == nullptr) {
48         HiLog::Error(LABEL, "failed to new asyncContext.");
49         return nullptr;
50     }
51     asyncContext->appEventPack = appEventPack;
52     asyncContext->result = builder.GetResult();
53     asyncContext->callback = builder.GetCallback();
54     asyncContext->isV9 = true;
55 
56     if (asyncContext->result == 0) {
57         asyncContext->result = VerifyAppEvent(asyncContext->appEventPack);
58     }
59 
60     napi_value promise = nullptr;
61     if (asyncContext->callback == nullptr) {
62         napi_create_promise(env, &asyncContext->deferred, &promise);
63     }
64 
65     NapiHiAppEventWrite::Write(env, asyncContext);
66     return promise;
67 }
68 
Configure(napi_env env,napi_callback_info info)69 static napi_value Configure(napi_env env, napi_callback_info info)
70 {
71     napi_value params[MAX_PARAM_NUM] = { 0 };
72     if (NapiUtil::GetCbInfo(env, info, params) < 1) { // The min num of params for configure is 1
73         NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("config"));
74         return nullptr;
75     }
76     if (!NapiHiAppEventConfig::Configure(env, params[0], true)) {
77         HiLog::Error(LABEL, "failed to configure HiAppEvent");
78     }
79     return nullptr;
80 }
81 
ClearData(napi_env env,napi_callback_info info)82 static napi_value ClearData(napi_env env, napi_callback_info info)
83 {
84     HiAppEventClean::ClearData(NapiHiAppEventConfig::GetStorageDir());
85     return NapiUtil::CreateUndefined(env);
86 }
87 
AddWatcher(napi_env env,napi_callback_info info)88 static napi_value AddWatcher(napi_env env, napi_callback_info info)
89 {
90     napi_value params[MAX_PARAM_NUM] = { 0 };
91     if (NapiUtil::GetCbInfo(env, info, params) < 1) { // The min num of params for addWatcher is 1
92         NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("watcher"));
93         return nullptr;
94     }
95     return NapiHiAppEventWatch::AddWatcher(env, params[0]);
96 }
97 
RemoveWatcher(napi_env env,napi_callback_info info)98 static napi_value RemoveWatcher(napi_env env, napi_callback_info info)
99 {
100     napi_value params[MAX_PARAM_NUM] = { 0 };
101     if (NapiUtil::GetCbInfo(env, info, params) < 1) { // The min num of params for removeWatcher is 1
102         NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("watcher"));
103         return nullptr;
104     }
105     return NapiHiAppEventWatch::RemoveWatcher(env, params[0]);
106 }
107 
108 EXTERN_C_START
Init(napi_env env,napi_value exports)109 static napi_value Init(napi_env env, napi_value exports)
110 {
111     napi_property_descriptor desc[] = {
112         DECLARE_NAPI_FUNCTION("write", Write),
113         DECLARE_NAPI_FUNCTION("configure", Configure),
114         DECLARE_NAPI_FUNCTION("clearData", ClearData),
115         DECLARE_NAPI_FUNCTION("addWatcher", AddWatcher),
116         DECLARE_NAPI_FUNCTION("removeWatcher", RemoveWatcher)
117     };
118     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
119     NapiHiAppEventInit::InitNapiClassV9(env, exports);
120     NapiAppEventHolder::NapiExport(env, exports);
121     return exports;
122 }
123 EXTERN_C_END
124 
125 static napi_module g_module_v9 = {
126     .nm_version = 1,
127     .nm_flags = 0,
128     .nm_filename = nullptr,
129     .nm_register_func = Init,
130     .nm_modname = "hiviewdfx.hiAppEvent",
131     .nm_priv = ((void *)0),
132     .reserved = {0}
133 };
134 
RegisterModule(void)135 extern "C" __attribute__((constructor)) void RegisterModule(void)
136 {
137     napi_module_register(&g_module_v9);
138 }
139