• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "base_context.h"
17 
18 #include "event_manager.h"
19 #include "napi/native_api.h"
20 #include "napi/native_common.h"
21 #include "napi_utils.h"
22 #include "node_api.h"
23 
24 namespace OHOS::NetStack {
BaseContext(napi_env env,const std::shared_ptr<EventManager> & sharedManager)25 BaseContext::BaseContext(napi_env env, const std::shared_ptr<EventManager> &sharedManager)
26     : env_(env),
27       ref_(nullptr),
28       parseOK_(false),
29       requestOK_(false),
30       errorCode_(0),
31       callback_(nullptr),
32       promiseRef_(nullptr),
33       asyncWork_(nullptr),
34       deferred_(nullptr),
35       needPromise_(true),
36       needThrowException_(false),
37       permissionDenied_(false),
38       noAllowedHost_(false),
39       cleartextNotPermitted_(false),
40       sharedManager_(sharedManager)
41 {
42 }
43 
~BaseContext()44 BaseContext::~BaseContext()
45 {
46     DeleteCallback();
47     DeletePromise();
48     DeleteAsyncWork();
49 }
50 
SetParseOK(bool parseOK)51 void BaseContext::SetParseOK(bool parseOK)
52 {
53     parseOK_ = parseOK;
54 }
55 
SetExecOK(bool requestOK)56 void BaseContext::SetExecOK(bool requestOK)
57 {
58     requestOK_ = requestOK;
59 }
60 
SetErrorCode(int32_t errorCode)61 void BaseContext::SetErrorCode(int32_t errorCode)
62 {
63     errorCode_ = errorCode;
64 }
65 
SetError(int32_t errorCode,const std::string & errorMessage)66 void BaseContext::SetError(int32_t errorCode, const std::string &errorMessage)
67 {
68     errorCode_ = errorCode;
69     errorMessage_ = errorMessage;
70 }
71 
SetCallback(napi_value callback)72 napi_status BaseContext::SetCallback(napi_value callback)
73 {
74     if (callback_ != nullptr) {
75         (void)napi_delete_reference(env_, callback_);
76     }
77     auto status = napi_create_reference(env_, callback, 1, &callback_);
78     callbackBak1_ = callback_;
79     callbackBak2_ = callback_;
80     callbackBak3_ = callback_;
81     callbackBak4_ = callback_;
82     return status;
83 }
84 
DeleteCallback()85 void BaseContext::DeleteCallback()
86 {
87     if (callback_ == nullptr || callback_ != callbackBak1_ || callback_ != callbackBak2_ ||
88         callback_ != callbackBak3_ || callback_ != callbackBak4_) {
89         return;
90     }
91     (void)napi_delete_reference(env_, callback_);
92     callback_ = nullptr;
93 }
94 
GetAsyncWork()95 napi_async_work BaseContext::GetAsyncWork()
96 {
97     return asyncWork_;
98 }
99 
CreateAsyncWork(const std::string & name,AsyncWorkExecutor executor,AsyncWorkCallback callback)100 void BaseContext::CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback)
101 {
102     napi_status ret = napi_create_async_work(env_, nullptr, NapiUtils::CreateStringUtf8(env_, name), executor, callback,
103                                              this, &asyncWork_);
104     asyncWorkBack1_ = asyncWork_;
105     asyncWorkBack2_ = asyncWork_;
106     asyncWorkBack3_ = asyncWork_;
107     asyncWorkBack4_ = asyncWork_;
108     if (ret != napi_ok) {
109         return;
110     }
111     asyncWorkName_ = name;
112     (void)napi_queue_async_work_with_qos(env_, asyncWork_, napi_qos_default);
113 }
114 
DeleteAsyncWork()115 void BaseContext::DeleteAsyncWork()
116 {
117     if (asyncWork_ == nullptr) {
118         return;
119     }
120     (void)napi_delete_async_work(env_, asyncWork_);
121 }
122 
CreatePromise()123 napi_value BaseContext::CreatePromise()
124 {
125     napi_value result = nullptr;
126     NAPI_CALL(env_, napi_create_promise(env_, &deferred_, &result));
127     promiseRef_ = NapiUtils::CreateReference(env_, result);
128     deferredBack1_ = deferred_;
129     deferredBack2_ = deferred_;
130     deferredBack3_ = deferred_;
131     deferredBack4_ = deferred_;
132     return result;
133 }
134 
DeletePromise()135 void BaseContext::DeletePromise()
136 {
137     if (promiseRef_ == nullptr) {
138         return;
139     }
140     (void)napi_delete_reference(env_, promiseRef_);
141     promiseRef_ = nullptr;
142 }
143 
IsParseOK() const144 bool BaseContext::IsParseOK() const
145 {
146     return parseOK_;
147 }
148 
IsExecOK() const149 bool BaseContext::IsExecOK() const
150 {
151     return requestOK_;
152 }
153 
GetEnv() const154 napi_env BaseContext::GetEnv() const
155 {
156     return env_;
157 }
158 
GetErrorCode() const159 int32_t BaseContext::GetErrorCode() const
160 {
161     return errorCode_;
162 }
163 
GetErrorMessage() const164 std::string BaseContext::GetErrorMessage() const
165 {
166     return errorMessage_;
167 }
168 
GetCallback() const169 napi_value BaseContext::GetCallback() const
170 {
171     if (callback_ == nullptr || callback_ != callbackBak1_ || callback_ != callbackBak2_ ||
172         callback_ != callbackBak3_ || callback_ != callbackBak4_) {
173         return nullptr;
174     }
175     napi_value callback = nullptr;
176     NAPI_CALL(env_, napi_get_reference_value(env_, callback_, &callback));
177     return callback;
178 }
179 
GetDeferred() const180 napi_deferred BaseContext::GetDeferred() const
181 {
182     return deferred_;
183 }
184 
GetAsyncWorkName() const185 const std::string &BaseContext::GetAsyncWorkName() const
186 {
187     return asyncWorkName_;
188 }
189 
EmitSharedManager(const std::string & type,const std::pair<napi_value,napi_value> & argv)190 void BaseContext::EmitSharedManager(const std::string &type, const std::pair<napi_value, napi_value> &argv)
191 {
192     if (sharedManager_ != nullptr) {
193         sharedManager_->Emit(type, argv);
194     }
195 }
196 
SetNeedPromise(bool needPromise)197 void BaseContext::SetNeedPromise(bool needPromise)
198 {
199     needPromise_ = needPromise;
200 }
201 
IsNeedPromise() const202 bool BaseContext::IsNeedPromise() const
203 {
204     return needPromise_;
205 }
206 
GetSharedManager() const207 std::shared_ptr<EventManager> BaseContext::GetSharedManager() const
208 {
209     return sharedManager_;
210 }
211 
SetSharedManager(const std::shared_ptr<EventManager> & sharedManager)212 void BaseContext::SetSharedManager(const std::shared_ptr<EventManager> &sharedManager)
213 {
214     sharedManager_ = sharedManager;
215 }
216 
ParseParams(napi_value * params,size_t paramsCount)217 void BaseContext::ParseParams(napi_value *params, size_t paramsCount)
218 {
219     if (paramsCount != 0 && paramsCount != 1) {
220         return;
221     }
222 
223     if (paramsCount == 1 && NapiUtils::GetValueType(env_, params[0]) != napi_function) {
224         return;
225     }
226 
227     if (paramsCount == 1) {
228         SetParseOK(SetCallback(params[0]) == napi_ok);
229         return;
230     }
231     SetParseOK(true);
232 }
233 
SetNeedThrowException(bool needThrowException)234 void BaseContext::SetNeedThrowException(bool needThrowException)
235 {
236     needThrowException_ = needThrowException;
237 }
238 
IsNeedThrowException() const239 bool BaseContext::IsNeedThrowException() const
240 {
241     return needThrowException_;
242 }
243 
SetPermissionDenied(bool permissionDenied)244 void BaseContext::SetPermissionDenied(bool permissionDenied)
245 {
246     permissionDenied_ = permissionDenied;
247 }
248 
IsPermissionDenied() const249 bool BaseContext::IsPermissionDenied() const
250 {
251     return permissionDenied_;
252 }
253 
SetNoAllowedHost(bool noAllowed)254 void BaseContext::SetNoAllowedHost(bool noAllowed)
255 {
256     noAllowedHost_ = noAllowed;
257 }
258 
IsNoAllowedHost() const259 bool BaseContext::IsNoAllowedHost() const
260 {
261     return noAllowedHost_;
262 }
263 
SetCleartextNotPermitted(bool notPermitted)264 void BaseContext::SetCleartextNotPermitted(bool notPermitted)
265 {
266     cleartextNotPermitted_ = notPermitted;
267 }
268 
IsCleartextNotPermitted() const269 bool BaseContext::IsCleartextNotPermitted() const
270 {
271     return cleartextNotPermitted_;
272 }
273 
CreateReference(napi_value value)274 void BaseContext::CreateReference(napi_value value)
275 {
276     if (env_ != nullptr && value != nullptr) {
277         ref_ = NapiUtils::CreateReference(env_, value);
278     }
279 }
280 
DeleteReference()281 void BaseContext::DeleteReference()
282 {
283     if (env_ != nullptr && ref_ != nullptr) {
284         NapiUtils::DeleteReference(env_, ref_);
285     }
286 }
287 } // namespace OHOS::NetStack
288