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
16 #ifndef COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_MODULE_TEMPLATE_H
17 #define COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_MODULE_TEMPLATE_H
18
19 #include <initializer_list>
20
21 #include "netmanager_base_base_async_work.h"
22 #include "netmanager_base_base_context.h"
23 #include "netmanager_base_log.h"
24
25 #define MAX_PARAM_NUM 64
26
27 namespace OHOS::NetManagerStandard::ModuleTemplate {
28 typedef void (*Finalizer)(napi_env, void *data, void *);
29
30 template <class Context>
Interface(napi_env env,napi_callback_info info,const std::string & asyncWorkName,bool (* Work)(napi_env,napi_value,Context *),AsyncWorkExecutor executor,AsyncWorkCallback callback)31 napi_value Interface(napi_env env,
32 napi_callback_info info,
33 const std::string &asyncWorkName,
34 bool (*Work)(napi_env, napi_value, Context *),
35 AsyncWorkExecutor executor,
36 AsyncWorkCallback callback)
37 {
38 static_assert(std::is_base_of<BaseContext, Context>::value);
39
40 napi_value thisVal = nullptr;
41 size_t paramsCount = MAX_PARAM_NUM;
42 napi_value params[MAX_PARAM_NUM] = {nullptr};
43 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
44
45 EventManager *manager = nullptr;
46 napi_unwrap(env, thisVal, reinterpret_cast<void **>(&manager));
47
48 auto context = new Context(env, manager);
49 context->ParseParams(params, paramsCount);
50 NETMANAGER_BASE_LOGE("js params parse OK ? %{public}d", context->IsParseOK());
51 if (Work != nullptr) {
52 if (!Work(env, thisVal, context)) {
53 NETMANAGER_BASE_LOGE("work failed error code = %{public}d", context->GetErrorCode());
54 }
55 }
56
57 context->CreateAsyncWork(asyncWorkName, executor, callback);
58 if (NapiUtils::GetValueType(env, context->GetCallback()) != napi_function && context->IsNeedPromise()) {
59 return context->CreatePromise();
60 }
61 return NapiUtils::GetUndefined(env);
62 }
63
64 template <class Context>
65 napi_value
InterfaceWithOutAsyncWork(napi_env env,napi_callback_info info,bool (* Work)(napi_env,napi_value,Context *))66 InterfaceWithOutAsyncWork(napi_env env, napi_callback_info info, bool (*Work)(napi_env, napi_value, Context *))
67 {
68 static_assert(std::is_base_of<BaseContext, Context>::value);
69
70 napi_value thisVal = nullptr;
71 size_t paramsCount = MAX_PARAM_NUM;
72 napi_value params[MAX_PARAM_NUM] = {nullptr};
73 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
74
75 EventManager *manager = nullptr;
76 napi_unwrap(env, thisVal, reinterpret_cast<void **>(&manager));
77
78 auto context = new Context(env, manager);
79 context->ParseParams(params, paramsCount);
80 if (Work != nullptr) {
81 if (!Work(env, thisVal, context)) {
82 NETMANAGER_BASE_LOGE("work failed error code = %{public}d", context->GetErrorCode());
83 }
84 }
85
86 if (NapiUtils::GetValueType(env, context->GetCallback()) != napi_function && context->IsNeedPromise()) {
87 return context->CreatePromise();
88 }
89 return NapiUtils::GetUndefined(env);
90 }
91
92 napi_value
93 On(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events, bool asyncCallback);
94
95 napi_value
96 Once(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events, bool asyncCallback);
97
98 napi_value Off(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events);
99
100 void DefineClass(napi_env env,
101 napi_value exports,
102 const std::initializer_list<napi_property_descriptor> &properties,
103 const std::string &className);
104
105 napi_value NewInstance(napi_env env,
106 napi_callback_info info,
107 const std::string &className,
108 void *(*MakeData)(napi_env, size_t, napi_value *, EventManager *),
109 Finalizer finalizer);
110 } // namespace OHOS::NetManagerStandard::ModuleTemplate
111 #endif /* COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_MODULE_TEMPLATE_H */
112