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
16 #ifndef COMMUNICATIONNETSTACK_NETSTACK_MODULE_TEMPLATE_H
17 #define COMMUNICATIONNETSTACK_NETSTACK_MODULE_TEMPLATE_H
18
19 #include <cstddef>
20 #include <initializer_list>
21 #include <iosfwd>
22 #include <type_traits>
23 #include <vector>
24
25 #include "base_async_work.h"
26 #include "napi/native_api.h"
27 #include "napi/native_common.h"
28 #include "base_context.h"
29 #include "netstack_log.h"
30 #include "napi_utils.h"
31
32 namespace OHOS::NetStack {
33 class EventManager;
34 }
35
36 #define MAX_PARAM_NUM 64
37
38 namespace OHOS::NetStack::ModuleTemplate {
39 typedef void (*Finalizer)(napi_env, void *data, void *);
40
41 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)42 napi_value Interface(napi_env env, napi_callback_info info, const std::string &asyncWorkName,
43 bool (*Work)(napi_env, napi_value, Context *), AsyncWorkExecutor executor,
44 AsyncWorkCallback callback)
45 {
46 static_assert(std::is_base_of<BaseContext, Context>::value);
47
48 napi_value thisVal = nullptr;
49 size_t paramsCount = MAX_PARAM_NUM;
50 napi_value params[MAX_PARAM_NUM] = {nullptr};
51 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
52
53 EventManager *manager = nullptr;
54 napi_unwrap(env, thisVal, reinterpret_cast<void **>(&manager));
55
56 auto context = new Context(env, manager);
57 context->ParseParams(params, paramsCount);
58 if (!context->IsParseOK()) {
59 context->SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
60 }
61 NETSTACK_LOGI("js params parse OK ? %{public}d", context->IsParseOK());
62 if (context->IsNeedThrowException()) { // only api9 or later need throw exception.
63 napi_throw_error(env, std::to_string(context->GetErrorCode()).c_str(), context->GetErrorMessage().c_str());
64 delete context;
65 context = nullptr;
66 return NapiUtils::GetUndefined(env);
67 }
68 if (Work != nullptr) {
69 if (!Work(env, thisVal, context)) {
70 NETSTACK_LOGE("work failed error code = %{public}d", context->GetErrorCode());
71 }
72 }
73
74 context->CreateReference(thisVal);
75 context->CreateAsyncWork(asyncWorkName, executor, callback);
76 if (NapiUtils::GetValueType(env, context->GetCallback()) != napi_function && context->IsNeedPromise()) {
77 NETSTACK_LOGI("context->CreatePromise()");
78 return context->CreatePromise();
79 }
80 return NapiUtils::GetUndefined(env);
81 }
82
83 template <class Context>
InterfaceWithOutAsyncWork(napi_env env,napi_callback_info info,bool (* Work)(napi_env,napi_value,Context *),const std::string & asyncWorkName,AsyncWorkExecutor executor,AsyncWorkCallback callback)84 napi_value InterfaceWithOutAsyncWork(napi_env env, napi_callback_info info,
85 bool (*Work)(napi_env, napi_value, Context *), const std::string &asyncWorkName,
86 AsyncWorkExecutor executor, AsyncWorkCallback callback)
87 {
88 static_assert(std::is_base_of<BaseContext, Context>::value);
89
90 napi_value thisVal = nullptr;
91 size_t paramsCount = MAX_PARAM_NUM;
92 napi_value params[MAX_PARAM_NUM] = {nullptr};
93 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
94
95 EventManager *manager = nullptr;
96 napi_unwrap(env, thisVal, reinterpret_cast<void **>(&manager));
97
98 auto context = new Context(env, manager);
99 context->ParseParams(params, paramsCount);
100 if (!context->IsParseOK()) {
101 context->SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG);
102 }
103 napi_value ret = NapiUtils::GetUndefined(env);
104 if (NapiUtils::GetValueType(env, context->GetCallback()) != napi_function && context->IsNeedPromise()) {
105 ret = context->CreatePromise();
106 }
107 if (Work != nullptr) {
108 if (!Work(env, thisVal, context)) {
109 NETSTACK_LOGE("work failed error code = %{public}d", context->GetErrorCode());
110 }
111 }
112 context->CreateReference(thisVal);
113 if (context->IsPermissionDenied() || !context->IsParseOK()) {
114 context->CreateAsyncWork(asyncWorkName, executor, callback);
115 }
116 return ret;
117 }
118
119 napi_value On(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events,
120 bool asyncCallback);
121
122 napi_value Once(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events,
123 bool asyncCallback);
124
125 napi_value Off(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events);
126
127 void DefineClass(napi_env env, napi_value exports, const std::initializer_list<napi_property_descriptor> &properties,
128 const std::string &className);
129
130 napi_value NewInstance(napi_env env, napi_callback_info info, const std::string &className, Finalizer finalizer);
131
132 napi_value NewInstanceNoManager(napi_env env, napi_callback_info info, const std::string &name, Finalizer finalizer);
133 } // namespace OHOS::NetStack::ModuleTemplate
134 #endif /* COMMUNICATIONNETSTACK_NETSTACK_MODULE_TEMPLATE_H */
135