• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef COMMUNICATIONNETSTACK_BASE_CONTEXT_H
17 #define COMMUNICATIONNETSTACK_BASE_CONTEXT_H
18 
19 #include <list>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_common.h"
23 #include "netstack_event_manager.h"
24 #include "nocopyable.h"
25 
26 namespace OHOS::NetStack {
27 typedef void (*AsyncWorkExecutor)(napi_env env, void *data);
28 
29 typedef void (*AsyncWorkCallback)(napi_env env, napi_status status, void *data);
30 
31 class BaseContext {
32 public:
33     DISALLOW_COPY_AND_MOVE(BaseContext);
34 
35     BaseContext() = delete;
36 
37     explicit BaseContext(napi_env env, EventManager *manager);
38 
39     virtual ~BaseContext();
40 
41     void SetParseOK(bool parseOK);
42 
43     void SetExecOK(bool requestOK);
44 
45     void SetErrorCode(int32_t errorCode);
46 
47     napi_status SetCallback(napi_value callback);
48 
49     void DeleteCallback();
50 
51     void CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback);
52 
53     void DeleteAsyncWork();
54 
55     napi_value CreatePromise();
56 
57     [[nodiscard]] bool IsParseOK() const;
58 
59     [[nodiscard]] bool IsExecOK() const;
60 
61     [[nodiscard]] napi_env GetEnv() const;
62 
63     [[nodiscard]] int32_t GetErrorCode() const;
64 
65     [[nodiscard]] napi_value GetCallback() const;
66 
67     [[nodiscard]] napi_deferred GetDeferred() const;
68 
69     [[nodiscard]] const std::string &GetAsyncWorkName() const;
70 
71     void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv);
72 
73     void SetNeedPromise(bool needPromise);
74 
75     [[nodiscard]] bool IsNeedPromise() const;
76 
77     [[nodiscard]] EventManager *GetManager() const;
78 
79 protected:
80     EventManager *manager_;
81 
82 private:
83     napi_env env_;
84 
85     bool parseOK_;
86 
87     bool requestOK_;
88 
89     int32_t errorCode_;
90 
91     napi_ref callback_;
92 
93     napi_async_work asyncWork_;
94 
95     napi_deferred deferred_;
96 
97     std::string asyncWorkName_;
98 
99     bool needPromise_;
100 };
101 } // namespace OHOS::NetStack
102 
103 #endif /* COMMUNICATIONNETSTACK_BASE_CONTEXT_H */
104