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_BASE_ASYNC_WORK_H
17 #define COMMUNICATIONNETSTACK_NETSTACK_BASE_ASYNC_WORK_H
18 
19 #include <limits>
20 #include <memory>
21 #include <securec.h>
22 
23 #include <napi/native_api.h>
24 #include <napi/native_common.h>
25 
26 #include "base_context.h"
27 #include "napi_utils.h"
28 #include "netstack_log.h"
29 
30 namespace OHOS::NetStack {
31 class BaseAsyncWork final {
32 public:
33     BaseAsyncWork() = delete;
34 
ExecAsyncWork(napi_env env,void * data)35     template <class Context, bool (*Executor)(Context *)> static void ExecAsyncWork(napi_env env, void *data)
36     {
37         static_assert(std::is_base_of<BaseContext, Context>::value);
38 
39         (void)env;
40 
41         auto context = reinterpret_cast<Context *>(data);
42         if (context == nullptr || Executor == nullptr) {
43             NETSTACK_LOGE("context or Executor is nullptr");
44             return;
45         }
46         if (!context->IsParseOK()) {
47             context->SetError(PARSE_ERROR_CODE, PARSE_ERROR_MSG); // if developer not set error, there will set.
48             NETSTACK_LOGE("parameter error");
49             return;
50         }
51         context->SetExecOK(Executor(context));
52         /* do not have async executor, execOK should be set in sync work */
53     }
54 
55     template <class Context, napi_value (*Callback)(Context *)>
AsyncWorkCallback(napi_env env,napi_status status,void * data)56     static void AsyncWorkCallback(napi_env env, napi_status status, void *data)
57     {
58         static_assert(std::is_base_of<BaseContext, Context>::value);
59 
60         if (!data) {
61             return;
62         }
63         auto baseContext = reinterpret_cast<BaseContext *>(data);
64         if (baseContext->GetDeferred() != baseContext->deferredBack1_ ||
65             baseContext->GetDeferred() != baseContext->deferredBack2_ ||
66             baseContext->GetDeferred() != baseContext->deferredBack3_ ||
67             baseContext->GetDeferred() != baseContext->deferredBack4_) {
68             return;
69         }
70         if (baseContext->GetAsyncWork() != baseContext->asyncWorkBack1_ ||
71             baseContext->GetAsyncWork() != baseContext->asyncWorkBack2_ ||
72             baseContext->GetAsyncWork() != baseContext->asyncWorkBack3_ ||
73             baseContext->GetAsyncWork() != baseContext->asyncWorkBack4_) {
74             return;
75         }
76 
77         if (status != napi_ok) {
78             return;
79         }
80         auto deleter = [](Context *context) {
81             context->DeleteReference();
82             delete context;
83             context = nullptr;
84         };
85         std::unique_ptr<Context, decltype(deleter)> context(static_cast<Context *>(data), deleter);
86         size_t argc = 2;
87         napi_value argv[2] = {nullptr};
88         if (context->IsParseOK() && context->IsExecOK()) {
89             argv[0] = NapiUtils::GetUndefined(env);
90 
91             if (Callback != nullptr) {
92                 argv[1] = Callback(context.get());
93             } else {
94                 argv[1] = NapiUtils::GetUndefined(env);
95             }
96             if (argv[1] == nullptr) {
97                 return;
98             }
99         } else {
100             argv[0] = NapiUtils::CreateErrorMessage(env, context->GetErrorCode(), context->GetErrorMessage());
101             if (argv[0] == nullptr) {
102                 return;
103             }
104 
105             argv[1] = NapiUtils::GetUndefined(env);
106         }
107 
108         if (context->GetDeferred() != nullptr) {
109             if (context->IsExecOK()) {
110                 napi_resolve_deferred(env, context->GetDeferred(), argv[1]);
111             } else {
112                 napi_reject_deferred(env, context->GetDeferred(), argv[0]);
113             }
114             return;
115         }
116 
117         napi_value func = context->GetCallback();
118         if (NapiUtils::GetValueType(env, func) == napi_function) {
119             napi_value undefined = NapiUtils::GetUndefined(env);
120             (void)NapiUtils::CallFunction(env, undefined, func, argc, argv);
121         }
122     }
123 
124     template <class Context, napi_value (*Callback)(Context *)>
AsyncWorkCallbackForSystem(napi_env env,napi_status status,void * data)125     static void AsyncWorkCallbackForSystem(napi_env env, napi_status status, void *data)
126     {
127         static_assert(std::is_base_of<BaseContext, Context>::value);
128 
129         if (status != napi_ok) {
130             return;
131         }
132         auto deleter = [](Context *context) { delete context; };
133         std::unique_ptr<Context, decltype(deleter)> context(static_cast<Context *>(data), deleter);
134         if (Callback != nullptr) {
135             (void)Callback(context.get());
136         }
137     }
138 };
139 } // namespace OHOS::NetStack
140 
141 #endif /* COMMUNICATIONNETSTACK_NETSTACK_BASE_ASYNC_WORK_H */
142