• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, &paramsCount, 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->IsNeedThrowException()) { // only api9 or later need throw exception.
59         napi_throw_error(env, std::to_string(context->GetErrorCode()).c_str(), context->GetErrorMessage().c_str());
60         delete context;
61         context = nullptr;
62         return NapiUtils::GetUndefined(env);
63     }
64     if (Work != nullptr) {
65         if (!Work(env, thisVal, context)) {
66             NETSTACK_LOGE("work failed error code = %{public}d", context->GetErrorCode());
67         }
68     }
69 
70     context->CreateReference(thisVal);
71     context->CreateAsyncWork(asyncWorkName, executor, callback);
72     if (NapiUtils::GetValueType(env, context->GetCallback()) != napi_function && context->IsNeedPromise()) {
73         NETSTACK_LOGI("context->CreatePromise()");
74         return context->CreatePromise();
75     }
76     return NapiUtils::GetUndefined(env);
77 }
78 
79 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)80 napi_value InterfaceWithOutAsyncWork(napi_env env, napi_callback_info info,
81                                      bool (*Work)(napi_env, napi_value, Context *), const std::string &asyncWorkName,
82                                      AsyncWorkExecutor executor, AsyncWorkCallback callback)
83 {
84     static_assert(std::is_base_of<BaseContext, Context>::value);
85 
86     napi_value thisVal = nullptr;
87     size_t paramsCount = MAX_PARAM_NUM;
88     napi_value params[MAX_PARAM_NUM] = {nullptr};
89     NAPI_CALL(env, napi_get_cb_info(env, info, &paramsCount, params, &thisVal, nullptr));
90 
91     EventManager *manager = nullptr;
92     napi_unwrap(env, thisVal, reinterpret_cast<void **>(&manager));
93 
94     auto context = new Context(env, manager);
95     context->ParseParams(params, paramsCount);
96     napi_value ret = NapiUtils::GetUndefined(env);
97     if (NapiUtils::GetValueType(env, context->GetCallback()) != napi_function && context->IsNeedPromise()) {
98         ret = context->CreatePromise();
99     }
100     if (Work != nullptr) {
101         if (!Work(env, thisVal, context)) {
102             NETSTACK_LOGE("work failed error code = %{public}d", context->GetErrorCode());
103         }
104     }
105     context->CreateReference(thisVal);
106     if (!context->IsParseOK() || context->IsPermissionDenied()) {
107         context->CreateAsyncWork(asyncWorkName, executor, callback);
108     }
109     return ret;
110 }
111 
112 napi_value On(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events,
113               bool asyncCallback);
114 
115 napi_value Once(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events,
116                 bool asyncCallback);
117 
118 napi_value Off(napi_env env, napi_callback_info info, const std::initializer_list<std::string> &events);
119 
120 void DefineClass(napi_env env, napi_value exports, const std::initializer_list<napi_property_descriptor> &properties,
121                  const std::string &className);
122 
123 napi_value NewInstance(napi_env env, napi_callback_info info, const std::string &className, Finalizer finalizer);
124 
125 napi_value NewInstanceNoManager(napi_env env, napi_callback_info info, const std::string &name, Finalizer finalizer);
126 } // namespace OHOS::NetStack::ModuleTemplate
127 #endif /* COMMUNICATIONNETSTACK_NETSTACK_MODULE_TEMPLATE_H */
128