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