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