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 <memory> 20 21 #include "napi/native_api.h" 22 #include "napi/native_common.h" 23 #include "netstack_base_context.h" 24 #include "netstack_napi_utils.h" 25 #include "nocopyable.h" 26 27 static constexpr const int PARSE_PARAM_FAILED = -1; 28 29 static constexpr const char *BUSINESS_ERROR_KEY = "code"; 30 31 namespace OHOS::NetStack { 32 class BaseAsyncWork final { 33 public: 34 DISALLOW_COPY_AND_MOVE(BaseAsyncWork); 35 36 BaseAsyncWork() = delete; 37 ExecAsyncWork(napi_env env,void * data)38 template <class Context, bool (*Executor)(Context *)> static void ExecAsyncWork(napi_env env, void *data) 39 { 40 static_assert(std::is_base_of<BaseContext, Context>::value); 41 42 (void)env; 43 44 auto context = static_cast<Context *>(data); 45 if (!context->IsParseOK()) { 46 context->SetErrorCode(PARSE_PARAM_FAILED); 47 return; 48 } 49 50 if (Executor != nullptr) { 51 context->SetExecOK(Executor(context)); 52 } 53 /* do not have async executor, execOK should be set in sync work */ 54 } 55 56 template <class Context, napi_value (*Callback)(Context *)> AsyncWorkCallback(napi_env env,napi_status status,void * data)57 static void AsyncWorkCallback(napi_env env, napi_status status, void *data) 58 { 59 static_assert(std::is_base_of<BaseContext, Context>::value); 60 61 if (status != napi_ok) { 62 return; 63 } 64 auto deleter = [](Context *context) { delete context; }; 65 std::unique_ptr<Context, decltype(deleter)> context(static_cast<Context *>(data), deleter); 66 size_t argc = 2; 67 napi_value argv[2] = {nullptr}; 68 if (context->IsExecOK()) { 69 argv[0] = NapiUtils::GetUndefined(env); 70 71 if (Callback != nullptr) { 72 argv[1] = Callback(context.get()); 73 } else { 74 argv[1] = NapiUtils::GetUndefined(env); 75 } 76 if (argv[1] == nullptr) { 77 return; 78 } 79 } else { 80 argv[0] = NapiUtils::CreateObject(env); 81 if (argv[0] == nullptr) { 82 return; 83 } 84 NapiUtils::SetInt32Property(env, argv[0], BUSINESS_ERROR_KEY, context->GetErrorCode()); 85 86 argv[1] = NapiUtils::GetUndefined(env); 87 } 88 89 if (context->GetDeferred() != nullptr) { 90 if (context->IsExecOK()) { 91 napi_resolve_deferred(env, context->GetDeferred(), argv[1]); 92 } else { 93 napi_reject_deferred(env, context->GetDeferred(), argv[0]); 94 } 95 return; 96 } 97 98 napi_value func = context->GetCallback(); 99 if (NapiUtils::GetValueType(env, func) == napi_function) { 100 napi_value undefined = NapiUtils::GetUndefined(env); 101 (void)NapiUtils::CallFunction(env, undefined, func, argc, argv); 102 } 103 } 104 105 template <class Context, napi_value (*Callback)(Context *)> AsyncWorkCallbackForSystem(napi_env env,napi_status status,void * data)106 static void AsyncWorkCallbackForSystem(napi_env env, napi_status status, void *data) 107 { 108 static_assert(std::is_base_of<BaseContext, Context>::value); 109 110 if (status != napi_ok) { 111 return; 112 } 113 auto deleter = [](Context *context) { delete context; }; 114 std::unique_ptr<Context, decltype(deleter)> context(static_cast<Context *>(data), deleter); 115 if (Callback != nullptr) { 116 (void)Callback(context.get()); 117 } 118 } 119 }; 120 } // namespace OHOS::NetStack 121 122 #endif /* COMMUNICATIONNETSTACK_NETSTACK_BASE_ASYNC_WORK_H */ 123