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 (status != napi_ok) { 61 return; 62 } 63 auto deleter = [](Context *context) { 64 context->DeleteReference(); 65 delete context; 66 context = nullptr; 67 }; 68 std::unique_ptr<Context, decltype(deleter)> context(static_cast<Context *>(data), deleter); 69 size_t argc = 2; 70 napi_value argv[2] = {nullptr}; 71 if (context->IsParseOK() && context->IsExecOK()) { 72 argv[0] = NapiUtils::GetUndefined(env); 73 74 if (Callback != nullptr) { 75 argv[1] = Callback(context.get()); 76 } else { 77 argv[1] = NapiUtils::GetUndefined(env); 78 } 79 if (argv[1] == nullptr) { 80 return; 81 } 82 } else { 83 argv[0] = NapiUtils::CreateErrorMessage(env, context->GetErrorCode(), context->GetErrorMessage()); 84 if (argv[0] == nullptr) { 85 return; 86 } 87 88 argv[1] = NapiUtils::GetUndefined(env); 89 } 90 91 if (context->GetDeferred() != nullptr) { 92 if (context->IsExecOK()) { 93 napi_resolve_deferred(env, context->GetDeferred(), argv[1]); 94 } else { 95 napi_reject_deferred(env, context->GetDeferred(), argv[0]); 96 } 97 return; 98 } 99 100 napi_value func = context->GetCallback(); 101 if (NapiUtils::GetValueType(env, func) == napi_function) { 102 napi_value undefined = NapiUtils::GetUndefined(env); 103 (void)NapiUtils::CallFunction(env, undefined, func, argc, argv); 104 } 105 } 106 107 template <class Context, napi_value (*Callback)(Context *)> AsyncWorkCallbackForSystem(napi_env env,napi_status status,void * data)108 static void AsyncWorkCallbackForSystem(napi_env env, napi_status status, void *data) 109 { 110 static_assert(std::is_base_of<BaseContext, Context>::value); 111 112 if (status != napi_ok) { 113 return; 114 } 115 auto deleter = [](Context *context) { delete context; }; 116 std::unique_ptr<Context, decltype(deleter)> context(static_cast<Context *>(data), deleter); 117 if (Callback != nullptr) { 118 (void)Callback(context.get()); 119 } 120 } 121 }; 122 } // namespace OHOS::NetStack 123 124 #endif /* COMMUNICATIONNETSTACK_NETSTACK_BASE_ASYNC_WORK_H */ 125