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,EventManager * manager)25 BaseContext::BaseContext(napi_env env, EventManager *manager)
26 : manager_(manager),
27 env_(env),
28 ref_(nullptr),
29 parseOK_(false),
30 requestOK_(false),
31 errorCode_(0),
32 callback_(nullptr),
33 asyncWork_(nullptr),
34 deferred_(nullptr),
35 needPromise_(true),
36 needThrowException_(false),
37 permissionDenied_(false)
38 {
39 }
40
~BaseContext()41 BaseContext::~BaseContext()
42 {
43 DeleteCallback();
44 DeleteAsyncWork();
45 }
46
SetParseOK(bool parseOK)47 void BaseContext::SetParseOK(bool parseOK)
48 {
49 parseOK_ = parseOK;
50 }
51
SetExecOK(bool requestOK)52 void BaseContext::SetExecOK(bool requestOK)
53 {
54 requestOK_ = requestOK;
55 }
56
SetErrorCode(int32_t errorCode)57 void BaseContext::SetErrorCode(int32_t errorCode)
58 {
59 errorCode_ = errorCode;
60 }
61
SetError(int32_t errorCode,const std::string & errorMessage)62 void BaseContext::SetError(int32_t errorCode, const std::string &errorMessage)
63 {
64 errorCode_ = errorCode;
65 errorMessage_ = errorMessage;
66 }
67
SetCallback(napi_value callback)68 napi_status BaseContext::SetCallback(napi_value callback)
69 {
70 if (callback_ != nullptr) {
71 (void)napi_delete_reference(env_, callback_);
72 }
73 return napi_create_reference(env_, callback, 1, &callback_);
74 }
75
DeleteCallback()76 void BaseContext::DeleteCallback()
77 {
78 if (callback_ == nullptr) {
79 return;
80 }
81 (void)napi_delete_reference(env_, callback_);
82 callback_ = nullptr;
83 }
84
CreateAsyncWork(const std::string & name,AsyncWorkExecutor executor,AsyncWorkCallback callback)85 void BaseContext::CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback)
86 {
87 napi_status ret = napi_create_async_work(env_, nullptr, NapiUtils::CreateStringUtf8(env_, name), executor, callback,
88 this, &asyncWork_);
89 if (ret != napi_ok) {
90 return;
91 }
92 asyncWorkName_ = name;
93 (void)napi_queue_async_work(env_, asyncWork_);
94 }
95
DeleteAsyncWork()96 void BaseContext::DeleteAsyncWork()
97 {
98 if (asyncWork_ == nullptr) {
99 return;
100 }
101 (void)napi_delete_async_work(env_, asyncWork_);
102 }
103
CreatePromise()104 napi_value BaseContext::CreatePromise()
105 {
106 napi_value result = nullptr;
107 NAPI_CALL(env_, napi_create_promise(env_, &deferred_, &result));
108 return result;
109 }
110
IsParseOK() const111 bool BaseContext::IsParseOK() const
112 {
113 return parseOK_;
114 }
115
IsExecOK() const116 bool BaseContext::IsExecOK() const
117 {
118 return requestOK_;
119 }
120
GetEnv() const121 napi_env BaseContext::GetEnv() const
122 {
123 return env_;
124 }
125
GetErrorCode() const126 int32_t BaseContext::GetErrorCode() const
127 {
128 return errorCode_;
129 }
130
GetErrorMessage() const131 std::string BaseContext::GetErrorMessage() const
132 {
133 return errorMessage_;
134 }
135
GetCallback() const136 napi_value BaseContext::GetCallback() const
137 {
138 if (callback_ == nullptr) {
139 return nullptr;
140 }
141 napi_value callback = nullptr;
142 NAPI_CALL(env_, napi_get_reference_value(env_, callback_, &callback));
143 return callback;
144 }
145
GetDeferred() const146 napi_deferred BaseContext::GetDeferred() const
147 {
148 return deferred_;
149 }
150
GetAsyncWorkName() const151 const std::string &BaseContext::GetAsyncWorkName() const
152 {
153 return asyncWorkName_;
154 }
155
Emit(const std::string & type,const std::pair<napi_value,napi_value> & argv)156 void BaseContext::Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv)
157 {
158 if (manager_ != nullptr) {
159 manager_->Emit(type, argv);
160 }
161 }
162
SetNeedPromise(bool needPromise)163 void BaseContext::SetNeedPromise(bool needPromise)
164 {
165 needPromise_ = needPromise;
166 }
167
IsNeedPromise() const168 bool BaseContext::IsNeedPromise() const
169 {
170 return needPromise_;
171 }
172
GetManager() const173 EventManager *BaseContext::GetManager() const
174 {
175 return manager_;
176 }
177
ParseParams(napi_value * params,size_t paramsCount)178 void BaseContext::ParseParams(napi_value *params, size_t paramsCount)
179 {
180 if (paramsCount != 0 && paramsCount != 1) {
181 return;
182 }
183
184 if (paramsCount == 1 && NapiUtils::GetValueType(env_, params[0]) != napi_function) {
185 return;
186 }
187
188 if (paramsCount == 1) {
189 SetParseOK(SetCallback(params[0]) == napi_ok);
190 return;
191 }
192 SetParseOK(true);
193 }
194
SetNeedThrowException(bool needThrowException)195 void BaseContext::SetNeedThrowException(bool needThrowException)
196 {
197 needThrowException_ = needThrowException;
198 }
199
IsNeedThrowException() const200 bool BaseContext::IsNeedThrowException() const
201 {
202 return needThrowException_;
203 }
204
SetPermissionDenied(bool permissionDenied)205 void BaseContext::SetPermissionDenied(bool permissionDenied)
206 {
207 permissionDenied_ = permissionDenied;
208 }
209
IsPermissionDenied() const210 bool BaseContext::IsPermissionDenied() const
211 {
212 return permissionDenied_;
213 }
214
CreateReference(napi_value value)215 void BaseContext::CreateReference(napi_value value)
216 {
217 if (env_ != nullptr && value != nullptr) {
218 ref_ = NapiUtils::CreateReference(env_, value);
219 }
220 }
221
DeleteReference()222 void BaseContext::DeleteReference()
223 {
224 if (env_ != nullptr && ref_ != nullptr) {
225 NapiUtils::DeleteReference(env_, ref_);
226 }
227 }
228 } // namespace OHOS::NetStack
229