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 "netstack_base_context.h"
17
18 #include "netstack_napi_utils.h"
19
20 namespace OHOS::NetStack {
BaseContext(napi_env env,EventManager * manager)21 BaseContext::BaseContext(napi_env env, EventManager *manager)
22 : manager_(manager),
23 env_(env),
24 parseOK_(false),
25 requestOK_(false),
26 errorCode_(0),
27 callback_(nullptr),
28 asyncWork_(nullptr),
29 deferred_(nullptr),
30 needPromise_(true)
31 {
32 }
33
~BaseContext()34 BaseContext::~BaseContext()
35 {
36 DeleteCallback();
37 DeleteAsyncWork();
38 }
39
SetParseOK(bool parseOK)40 void BaseContext::SetParseOK(bool parseOK)
41 {
42 parseOK_ = parseOK;
43 }
44
SetExecOK(bool requestOK)45 void BaseContext::SetExecOK(bool requestOK)
46 {
47 requestOK_ = requestOK;
48 }
49
SetErrorCode(int32_t errorCode)50 void BaseContext::SetErrorCode(int32_t errorCode)
51 {
52 errorCode_ = errorCode;
53 }
54
SetCallback(napi_value callback)55 napi_status BaseContext::SetCallback(napi_value callback)
56 {
57 if (callback_ != nullptr) {
58 (void)napi_delete_reference(env_, callback_);
59 }
60 return napi_create_reference(env_, callback, 1, &callback_);
61 }
62
DeleteCallback()63 void BaseContext::DeleteCallback()
64 {
65 if (callback_ == nullptr) {
66 return;
67 }
68 (void)napi_delete_reference(env_, callback_);
69 callback_ = nullptr;
70 }
71
CreateAsyncWork(const std::string & name,AsyncWorkExecutor executor,AsyncWorkCallback callback)72 void BaseContext::CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback)
73 {
74 napi_status ret = napi_create_async_work(env_, nullptr, NapiUtils::CreateStringUtf8(env_, name), executor, callback,
75 this, &asyncWork_);
76 if (ret != napi_ok) {
77 return;
78 }
79 asyncWorkName_ = name;
80 (void)napi_queue_async_work(env_, asyncWork_);
81 }
82
DeleteAsyncWork()83 void BaseContext::DeleteAsyncWork()
84 {
85 if (asyncWork_ == nullptr) {
86 return;
87 }
88 (void)napi_delete_async_work(env_, asyncWork_);
89 }
90
CreatePromise()91 napi_value BaseContext::CreatePromise()
92 {
93 napi_value result = nullptr;
94 NAPI_CALL(env_, napi_create_promise(env_, &deferred_, &result));
95 return result;
96 }
97
IsParseOK() const98 bool BaseContext::IsParseOK() const
99 {
100 return parseOK_;
101 }
102
IsExecOK() const103 bool BaseContext::IsExecOK() const
104 {
105 return requestOK_;
106 }
107
GetEnv() const108 napi_env BaseContext::GetEnv() const
109 {
110 return env_;
111 }
112
GetErrorCode() const113 int32_t BaseContext::GetErrorCode() const
114 {
115 return errorCode_;
116 }
117
GetCallback() const118 napi_value BaseContext::GetCallback() const
119 {
120 if (callback_ == nullptr) {
121 return nullptr;
122 }
123 napi_value callback = nullptr;
124 NAPI_CALL(env_, napi_get_reference_value(env_, callback_, &callback));
125 return callback;
126 }
127
GetDeferred() const128 napi_deferred BaseContext::GetDeferred() const
129 {
130 return deferred_;
131 }
132
GetAsyncWorkName() const133 const std::string &BaseContext::GetAsyncWorkName() const
134 {
135 return asyncWorkName_;
136 }
137
Emit(const std::string & type,const std::pair<napi_value,napi_value> & argv)138 void BaseContext::Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv)
139 {
140 if (manager_ != nullptr) {
141 manager_->Emit(type, argv);
142 }
143 }
144
SetNeedPromise(bool needPromise)145 void BaseContext::SetNeedPromise(bool needPromise)
146 {
147 needPromise_ = needPromise;
148 }
149
IsNeedPromise() const150 bool BaseContext::IsNeedPromise() const
151 {
152 return needPromise_;
153 }
154
GetManager() const155 EventManager *BaseContext::GetManager() const
156 {
157 return manager_;
158 }
159 } // namespace OHOS::NetStack