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 static constexpr uint32_t BASE_CONTEXT_MAGIC_NUMBER = 0x18916123;
32 using AsyncWorkExecutor = void (*)(napi_env env, void *data);
33 using AsyncWorkCallback = void (*)(napi_env env, napi_status status, void *data);
34 
35 class BaseContext {
36 public:
37     DISALLOW_COPY_AND_MOVE(BaseContext);
38 
39     BaseContext() = delete;
40     explicit BaseContext(napi_env env, EventManager *manager);
41     virtual ~BaseContext();
42 
43     void SetParseOK(bool parseOK);
44     void SetExecOK(bool requestOK);
45     void SetErrorCode(int32_t errorCode);
46     void SetError(int32_t errorCode, const std::string &errorMessage);
47     void DeleteCallback();
48     void CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback);
49     void DeleteAsyncWork();
50     void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv);
51     void SetNeedPromise(bool needPromise);
52     void SetNeedThrowException(bool needThrowException);
53     napi_status SetCallback(napi_value callback);
54     napi_value CreatePromise();
55     [[nodiscard]] bool IsParseOK() const;
56     [[nodiscard]] bool IsExecOK() const;
57     [[nodiscard]] napi_env GetEnv() const;
58     [[nodiscard]] int32_t GetErrorCode() const;
59     [[nodiscard]] const std::string &GetErrorMessage() const;
60     [[nodiscard]] napi_value GetCallback() const;
61     [[nodiscard]] napi_deferred GetDeferred() const;
62     [[nodiscard]] const std::string &GetAsyncWorkName() const;
63     [[nodiscard]] EventManager *GetManager() const;
64     [[nodiscard]] bool IsNeedPromise() const;
65     [[nodiscard]] bool IsNeedThrowException() const;
66 
67     napi_deferred deferredBack1_ = nullptr;
68     napi_deferred deferredBack2_ = nullptr;
69     napi_deferred deferredBack3_ = nullptr;
70     napi_deferred deferredBack4_ = nullptr;
71 
72     uint32_t magic_ = BASE_CONTEXT_MAGIC_NUMBER;
73 
74 protected:
75     EventManager *manager_ = nullptr;
76 
77 private:
78     napi_env env_ = nullptr;
79     bool parseOK_;
80     bool requestOK_;
81     int32_t errorCode_;
82     std::string errorMessage_;
83     napi_ref callback_ = nullptr;
84     napi_async_work asyncWork_ = nullptr;
85     napi_deferred deferred_ = nullptr;
86     std::string asyncWorkName_;
87     bool needPromise_;
88     bool needThrowException_;
89     NetBaseErrorCodeConvertor convertor_;
90 };
91 } // namespace NetManagerStandard
92 } // namespace OHOS
93 
94 #endif // COMMUNICATIONNETMANAGER_BASE_BASE_CONTEXT_H
95