1 /* 2 * Copyright (c) 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 COMMUNICATIONNETMANAGER_BASE_BASE_CONTEXT_H 17 #define COMMUNICATIONNETMANAGER_BASE_BASE_CONTEXT_H 18 19 #include <list> 20 21 #include <napi/native_api.h> 22 #include <napi/native_common.h> 23 24 #include "errorcode_convertor.h" 25 #include "event_manager.h" 26 #include "node_api_types.h" 27 #include "nocopyable.h" 28 29 namespace OHOS { 30 namespace NetManagerStandard { 31 using AsyncWorkExecutor = void (*)(napi_env env, void *data); 32 using AsyncWorkCallback = void (*)(napi_env env, napi_status status, void *data); 33 34 class BaseContext { 35 public: 36 DISALLOW_COPY_AND_MOVE(BaseContext); 37 38 BaseContext() = delete; 39 explicit BaseContext(napi_env env, EventManager *manager); 40 virtual ~BaseContext(); 41 42 void SetParseOK(bool parseOK); 43 void SetExecOK(bool requestOK); 44 void SetErrorCode(int32_t errorCode); 45 void SetError(int32_t errorCode, const std::string &errorMessage); 46 void DeleteCallback(); 47 void CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback); 48 void DeleteAsyncWork(); 49 void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv); 50 void SetNeedPromise(bool needPromise); 51 void SetNeedThrowException(bool needThrowException); 52 napi_status SetCallback(napi_value callback); 53 napi_value CreatePromise(); 54 [[nodiscard]] bool IsParseOK() const; 55 [[nodiscard]] bool IsExecOK() const; 56 [[nodiscard]] napi_env GetEnv() const; 57 [[nodiscard]] int32_t GetErrorCode() const; 58 [[nodiscard]] const std::string &GetErrorMessage() const; 59 [[nodiscard]] napi_value GetCallback() const; 60 [[nodiscard]] napi_deferred GetDeferred() const; 61 [[nodiscard]] const std::string &GetAsyncWorkName() const; 62 [[nodiscard]] EventManager *GetManager() const; 63 [[nodiscard]] bool IsNeedPromise() const; 64 [[nodiscard]] bool IsNeedThrowException() const; 65 66 protected: 67 EventManager *manager_; 68 69 private: 70 napi_env env_; 71 bool parseOK_; 72 bool requestOK_; 73 int32_t errorCode_; 74 std::string errorMessage_; 75 napi_ref callback_; 76 napi_async_work asyncWork_; 77 napi_deferred deferred_; 78 std::string asyncWorkName_; 79 bool needPromise_; 80 bool needThrowException_; 81 NetBaseErrorCodeConvertor convertor_; 82 }; 83 } // namespace NetManagerStandard 84 } // namespace OHOS 85 86 #endif // COMMUNICATIONNETMANAGER_BASE_BASE_CONTEXT_H 87