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 return result;
108 }
109
IsParseOK() const110 bool BaseContext::IsParseOK() const
111 {
112 return parseOK_;
113 }
114
IsExecOK() const115 bool BaseContext::IsExecOK() const
116 {
117 return requestOK_;
118 }
119
GetEnv() const120 napi_env BaseContext::GetEnv() const
121 {
122 return env_;
123 }
124
GetErrorCode() const125 int32_t BaseContext::GetErrorCode() const
126 {
127 return errorCode_;
128 }
129
GetErrorMessage() const130 const std::string &BaseContext::GetErrorMessage() const
131 {
132 return errorMessage_;
133 }
134
GetCallback() const135 napi_value BaseContext::GetCallback() const
136 {
137 if (callback_ == nullptr) {
138 return nullptr;
139 }
140 napi_value callback = nullptr;
141 NAPI_CALL(env_, napi_get_reference_value(env_, callback_, &callback));
142 return callback;
143 }
144
GetDeferred() const145 napi_deferred BaseContext::GetDeferred() const
146 {
147 return deferred_;
148 }
149
GetAsyncWorkName() const150 const std::string &BaseContext::GetAsyncWorkName() const
151 {
152 return asyncWorkName_;
153 }
154
Emit(const std::string & type,const std::pair<napi_value,napi_value> & argv)155 void BaseContext::Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv)
156 {
157 if (manager_ != nullptr) {
158 manager_->Emit(type, argv);
159 }
160 }
161
GetManager() const162 EventManager *BaseContext::GetManager() const
163 {
164 return manager_;
165 }
166
SetNeedPromise(bool needPromise)167 void BaseContext::SetNeedPromise(bool needPromise)
168 {
169 needPromise_ = needPromise;
170 }
171
IsNeedPromise() const172 bool BaseContext::IsNeedPromise() const
173 {
174 return needPromise_;
175 }
176
SetNeedThrowException(bool needThrowException)177 void BaseContext::SetNeedThrowException(bool needThrowException)
178 {
179 needThrowException_ = needThrowException;
180 }
181
IsNeedThrowException() const182 bool BaseContext::IsNeedThrowException() const
183 {
184 return needThrowException_;
185 }
186 } // namespace NetManagerStandard
187 } // namespace OHOS
188