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 BaseContext(napi_env env, EventManager *manager); 44 45 BaseContext(napi_env env, const std::shared_ptr<EventManager> &sharedManager); 46 47 virtual ~BaseContext(); 48 49 void SetParseOK(bool parseOK); 50 51 void SetExecOK(bool requestOK); 52 53 void SetErrorCode(int32_t errorCode); 54 55 void SetError(int32_t errorCode, const std::string &errorMessage); 56 57 napi_status SetCallback(napi_value callback); 58 59 void DeleteCallback(); 60 61 void CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback); 62 63 void DeleteAsyncWork(); 64 65 napi_value CreatePromise(); 66 67 void DeletePromise(); 68 69 [[nodiscard]] bool IsParseOK() const; 70 71 [[nodiscard]] bool IsExecOK() const; 72 73 [[nodiscard]] napi_env GetEnv() const; 74 75 [[nodiscard]] virtual int32_t GetErrorCode() const; 76 77 [[nodiscard]] virtual std::string GetErrorMessage() const; 78 79 [[nodiscard]] napi_value GetCallback() const; 80 81 [[nodiscard]] napi_deferred GetDeferred() const; 82 83 [[nodiscard]] const std::string &GetAsyncWorkName() const; 84 85 void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv); 86 87 void EmitSharedManager(const std::string &type, const std::pair<napi_value, napi_value> &argv); 88 89 void SetNeedPromise(bool needPromise); 90 91 [[nodiscard]] bool IsNeedPromise() const; 92 93 void SetNeedThrowException(bool needThrowException); 94 95 [[nodiscard]] bool IsNeedThrowException() const; 96 97 void SetPermissionDenied(bool needThrowException); 98 99 [[nodiscard]] bool IsPermissionDenied() const; 100 101 void SetNoAllowedHost(bool needThrowException); 102 103 [[nodiscard]] bool IsNoAllowedHost() const; 104 105 [[nodiscard]] EventManager *GetManager() const; 106 107 [[nodiscard]] std::shared_ptr<EventManager> GetSharedManager() const; 108 109 void SetSharedManager(const std::shared_ptr<EventManager> &sharedManager); 110 111 void CreateReference(napi_value value); 112 113 void DeleteReference(); 114 115 napi_async_work GetAsyncWork(); 116 117 virtual void ParseParams(napi_value *params, size_t paramsCount); 118 119 napi_deferred deferredBack1_ = nullptr; 120 napi_deferred deferredBack2_ = nullptr; 121 napi_deferred deferredBack3_ = nullptr; 122 napi_deferred deferredBack4_ = nullptr; 123 napi_async_work asyncWorkBack1_ = nullptr; 124 napi_async_work asyncWorkBack2_ = nullptr; 125 napi_async_work asyncWorkBack3_ = nullptr; 126 napi_async_work asyncWorkBack4_ = nullptr; 127 128 protected: 129 EventManager *manager_ = nullptr; 130 131 private: 132 napi_env env_ = nullptr; 133 134 napi_ref ref_ = nullptr; 135 136 bool parseOK_; 137 138 bool requestOK_; 139 140 int32_t errorCode_; 141 142 napi_ref callback_ = nullptr; 143 144 napi_ref promiseRef_ = nullptr; 145 146 napi_async_work asyncWork_ = nullptr; 147 148 napi_deferred deferred_ = nullptr; 149 150 bool needPromise_; 151 152 bool needThrowException_; 153 154 bool permissionDenied_; 155 156 bool noAllowedHost_; 157 158 std::string asyncWorkName_; 159 160 std::string errorMessage_; 161 162 protected: 163 std::shared_ptr<EventManager> sharedManager_; 164 165 private: 166 napi_ref callbackBak1_ = nullptr; 167 napi_ref callbackBak2_ = nullptr; 168 napi_ref callbackBak3_ = nullptr; 169 napi_ref callbackBak4_ = nullptr; 170 }; 171 } // namespace OHOS::NetStack 172 173 #endif /* COMMUNICATIONNETSTACK_BASE_CONTEXT_H */ 174