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 [[nodiscard]] bool IsParseOK() const; 66 67 [[nodiscard]] bool IsExecOK() const; 68 69 [[nodiscard]] napi_env GetEnv() const; 70 71 [[nodiscard]] virtual int32_t GetErrorCode() const; 72 73 [[nodiscard]] virtual std::string GetErrorMessage() const; 74 75 [[nodiscard]] napi_value GetCallback() const; 76 77 [[nodiscard]] napi_deferred GetDeferred() const; 78 79 [[nodiscard]] const std::string &GetAsyncWorkName() const; 80 81 void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv); 82 83 void SetNeedPromise(bool needPromise); 84 85 [[nodiscard]] bool IsNeedPromise() const; 86 87 void SetNeedThrowException(bool needThrowException); 88 89 [[nodiscard]] bool IsNeedThrowException() const; 90 91 void SetPermissionDenied(bool needThrowException); 92 93 [[nodiscard]] bool IsPermissionDenied() const; 94 95 [[nodiscard]] EventManager *GetManager() const; 96 97 void CreateReference(napi_value value); 98 99 void DeleteReference(); 100 101 virtual void ParseParams(napi_value *params, size_t paramsCount); 102 103 protected: 104 EventManager *manager_; 105 106 private: 107 napi_env env_; 108 109 napi_ref ref_; 110 111 bool parseOK_; 112 113 bool requestOK_; 114 115 int32_t errorCode_; 116 117 std::string errorMessage_; 118 119 napi_ref callback_; 120 121 napi_async_work asyncWork_; 122 123 napi_deferred deferred_; 124 125 std::string asyncWorkName_; 126 127 bool needPromise_; 128 129 bool needThrowException_; 130 131 bool permissionDenied_; 132 }; 133 } // namespace OHOS::NetStack 134 135 #endif /* COMMUNICATIONNETSTACK_BASE_CONTEXT_H */ 136