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 #include "base_context.h"
17
18 #include <limits>
19
20 #include "napi_utils.h"
21 #include "node_api.h"
22
23 namespace OHOS {
24 namespace NetManagerStandard {
BaseContext(napi_env env,EventManager * manager)25 BaseContext::BaseContext(napi_env env, EventManager *manager)
26 : manager_(manager),
27 env_(env),
28 parseOK_(false),
29 requestOK_(false),
30 errorCode_(std::numeric_limits<int32_t>::max()),
31 callback_(nullptr),
32 asyncWork_(nullptr),
33 deferred_(nullptr),
34 needPromise_(true),
35 needThrowException_(false)
36 {
37 }
38
~BaseContext()39 BaseContext::~BaseContext()
40 {
41 DeleteCallback();
42 DeleteAsyncWork();
43 }
44
SetParseOK(bool parseOK)45 void BaseContext::SetParseOK(bool parseOK)
46 {
47 parseOK_ = parseOK;
48 }
49
SetErrorCode(int32_t errorCode)50 void BaseContext::SetErrorCode(int32_t errorCode)
51 {
52 errorCode_ = errorCode;
53 errorMessage_ = convertor_.ConvertErrorCode(errorCode_);
54 }
55
SetExecOK(bool requestOK)56 void BaseContext::SetExecOK(bool requestOK)
57 {
58 requestOK_ = requestOK;
59 }
60
SetError(int32_t errorCode,const std::string & errorMessage)61 void BaseContext::SetError(int32_t errorCode, const std::string &errorMessage)
62 {
63 errorCode_ = errorCode;
64 errorMessage_ = errorMessage;
65 }
66
SetCallback(napi_value callback)67 napi_status BaseContext::SetCallback(napi_value callback)
68 {
69 if (callback_ != nullptr) {
70 (void)napi_delete_reference(env_, callback_);
71 }
72 return napi_create_reference(env_, callback, 1, &callback_);
73 }
74
DeleteCallback()75 void BaseContext::DeleteCallback()
76 {
77 if (callback_ == nullptr) {
78 return;
79 }
80 (void)napi_delete_reference(env_, callback_);
81 callback_ = nullptr;
82 }
83
CreateAsyncWork(const std::string & name,AsyncWorkExecutor executor,AsyncWorkCallback callback)84 void BaseContext::CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback)
85 {
86 napi_status ret = napi_create_async_work(env_, nullptr, NapiUtils::CreateStringUtf8(env_, name), executor, callback,
87 this, &asyncWork_);
88 if (ret != napi_ok) {
89 return;
90 }
91 asyncWorkName_ = name;
92 (void)napi_queue_async_work_with_qos(env_, asyncWork_, napi_qos_default);
93 }
94
DeleteAsyncWork()95 void BaseContext::DeleteAsyncWork()
96 {
97 if (asyncWork_ == nullptr) {
98 return;
99 }
100 (void)napi_delete_async_work(env_, asyncWork_);
101 }
102
CreatePromise()103 napi_value BaseContext::CreatePromise()
104 {
105 napi_value result = nullptr;
106 NAPI_CALL(env_, napi_create_promise(env_, &deferred_, &result));
107 deferredBack1_ = deferred_;
108 deferredBack2_ = deferred_;
109 deferredBack3_ = deferred_;
110 deferredBack4_ = deferred_;
111 return result;
112 }
113
IsParseOK() const114 bool BaseContext::IsParseOK() const
115 {
116 return parseOK_;
117 }
118
IsExecOK() const119 bool BaseContext::IsExecOK() const
120 {
121 return requestOK_;
122 }
123
GetEnv() const124 napi_env BaseContext::GetEnv() const
125 {
126 return env_;
127 }
128
GetErrorCode() const129 int32_t BaseContext::GetErrorCode() const
130 {
131 return errorCode_;
132 }
133
GetErrorMessage() const134 const std::string &BaseContext::GetErrorMessage() const
135 {
136 return errorMessage_;
137 }
138
GetCallback() const139 napi_value BaseContext::GetCallback() const
140 {
141 if (callback_ == nullptr) {
142 return nullptr;
143 }
144 napi_value callback = nullptr;
145 NAPI_CALL(env_, napi_get_reference_value(env_, callback_, &callback));
146 return callback;
147 }
148
GetDeferred() const149 napi_deferred BaseContext::GetDeferred() const
150 {
151 return deferred_;
152 }
153
GetAsyncWorkName() const154 const std::string &BaseContext::GetAsyncWorkName() const
155 {
156 return asyncWorkName_;
157 }
158
Emit(const std::string & type,const std::pair<napi_value,napi_value> & argv)159 void BaseContext::Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv)
160 {
161 if (manager_ != nullptr) {
162 manager_->Emit(type, argv);
163 }
164 }
165
GetManager() const166 EventManager *BaseContext::GetManager() const
167 {
168 return manager_;
169 }
170
SetNeedPromise(bool needPromise)171 void BaseContext::SetNeedPromise(bool needPromise)
172 {
173 needPromise_ = needPromise;
174 }
175
IsNeedPromise() const176 bool BaseContext::IsNeedPromise() const
177 {
178 return needPromise_;
179 }
180
SetNeedThrowException(bool needThrowException)181 void BaseContext::SetNeedThrowException(bool needThrowException)
182 {
183 needThrowException_ = needThrowException;
184 }
185
IsNeedThrowException() const186 bool BaseContext::IsNeedThrowException() const
187 {
188 return needThrowException_;
189 }
190 } // namespace NetManagerStandard
191 } // namespace OHOS
192