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_BASE_CONTEXT_H 17 #define COMMUNICATIONNETSTACK_BASE_CONTEXT_H 18 19 #include <cstddef> 20 #include <cstdint> 21 #include <iosfwd> 22 #include <string> 23 #include <utility> 24 25 #include <napi/native_api.h> 26 #include <napi/native_common.h> 27 28 #include "event_manager.h" 29 #include "node_api_types.h" 30 31 namespace OHOS::NetStack { 32 typedef void (*AsyncWorkExecutor)(napi_env env, void *data); 33 typedef void (*AsyncWorkCallback)(napi_env env, napi_status status, void *data); 34 static constexpr size_t PERMISSION_DENIED_CODE = 201; 35 static constexpr const char *PERMISSION_DENIED_MSG = "Permission denied"; 36 static constexpr size_t PARSE_ERROR_CODE = 401; 37 static constexpr const char *PARSE_ERROR_MSG = "Parameter error"; 38 39 class BaseContext { 40 public: 41 BaseContext() = delete; 42 43 explicit BaseContext(napi_env env, EventManager *manager); 44 45 virtual ~BaseContext(); 46 47 void SetParseOK(bool parseOK); 48 49 void SetExecOK(bool requestOK); 50 51 void SetErrorCode(int32_t errorCode); 52 53 void SetError(int32_t errorCode, const std::string &errorMessage); 54 55 napi_status SetCallback(napi_value callback); 56 57 void DeleteCallback(); 58 59 void CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback); 60 61 void DeleteAsyncWork(); 62 63 napi_value CreatePromise(); 64 65 void DeletePromise(); 66 67 [[nodiscard]] bool IsParseOK() const; 68 69 [[nodiscard]] bool IsExecOK() const; 70 71 [[nodiscard]] napi_env GetEnv() const; 72 73 [[nodiscard]] virtual int32_t GetErrorCode() const; 74 75 [[nodiscard]] virtual std::string GetErrorMessage() const; 76 77 [[nodiscard]] napi_value GetCallback() const; 78 79 [[nodiscard]] napi_deferred GetDeferred() const; 80 81 [[nodiscard]] const std::string &GetAsyncWorkName() const; 82 83 void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv); 84 85 void SetNeedPromise(bool needPromise); 86 87 [[nodiscard]] bool IsNeedPromise() const; 88 89 void SetNeedThrowException(bool needThrowException); 90 91 [[nodiscard]] bool IsNeedThrowException() const; 92 93 void SetPermissionDenied(bool needThrowException); 94 95 [[nodiscard]] bool IsPermissionDenied() const; 96 97 [[nodiscard]] EventManager *GetManager() const; 98 99 void CreateReference(napi_value value); 100 101 void DeleteReference(); 102 103 virtual void ParseParams(napi_value *params, size_t paramsCount); 104 105 protected: 106 EventManager *manager_; 107 108 private: 109 napi_env env_; 110 111 napi_ref ref_; 112 113 bool parseOK_; 114 115 bool requestOK_; 116 117 int32_t errorCode_; 118 119 std::string errorMessage_; 120 121 napi_ref callback_; 122 123 napi_ref promiseRef_; 124 125 napi_async_work asyncWork_; 126 127 napi_deferred deferred_; 128 129 std::string asyncWorkName_; 130 131 bool needPromise_; 132 133 bool needThrowException_; 134 135 bool permissionDenied_; 136 }; 137 } // namespace OHOS::NetStack 138 139 #endif /* COMMUNICATIONNETSTACK_BASE_CONTEXT_H */ 140