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