1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci
16b1b8bc3fSopenharmony_ci#ifndef COMMUNICATIONNETMANAGER_BASE_BASE_CONTEXT_H
17b1b8bc3fSopenharmony_ci#define COMMUNICATIONNETMANAGER_BASE_BASE_CONTEXT_H
18b1b8bc3fSopenharmony_ci
19b1b8bc3fSopenharmony_ci#include <list>
20b1b8bc3fSopenharmony_ci
21b1b8bc3fSopenharmony_ci#include <napi/native_api.h>
22b1b8bc3fSopenharmony_ci#include <napi/native_common.h>
23b1b8bc3fSopenharmony_ci
24b1b8bc3fSopenharmony_ci#include "errorcode_convertor.h"
25b1b8bc3fSopenharmony_ci#include "event_manager.h"
26b1b8bc3fSopenharmony_ci#include "node_api_types.h"
27b1b8bc3fSopenharmony_ci#include "nocopyable.h"
28b1b8bc3fSopenharmony_ci
29b1b8bc3fSopenharmony_cinamespace OHOS {
30b1b8bc3fSopenharmony_cinamespace NetManagerStandard {
31b1b8bc3fSopenharmony_cistatic constexpr uint32_t BASE_CONTEXT_MAGIC_NUMBER = 0x18916123;
32b1b8bc3fSopenharmony_ciusing AsyncWorkExecutor = void (*)(napi_env env, void *data);
33b1b8bc3fSopenharmony_ciusing AsyncWorkCallback = void (*)(napi_env env, napi_status status, void *data);
34b1b8bc3fSopenharmony_ci
35b1b8bc3fSopenharmony_ciclass BaseContext {
36b1b8bc3fSopenharmony_cipublic:
37b1b8bc3fSopenharmony_ci    DISALLOW_COPY_AND_MOVE(BaseContext);
38b1b8bc3fSopenharmony_ci
39b1b8bc3fSopenharmony_ci    BaseContext() = delete;
40b1b8bc3fSopenharmony_ci    explicit BaseContext(napi_env env, EventManager *manager);
41b1b8bc3fSopenharmony_ci    virtual ~BaseContext();
42b1b8bc3fSopenharmony_ci
43b1b8bc3fSopenharmony_ci    void SetParseOK(bool parseOK);
44b1b8bc3fSopenharmony_ci    void SetExecOK(bool requestOK);
45b1b8bc3fSopenharmony_ci    void SetErrorCode(int32_t errorCode);
46b1b8bc3fSopenharmony_ci    void SetError(int32_t errorCode, const std::string &errorMessage);
47b1b8bc3fSopenharmony_ci    void DeleteCallback();
48b1b8bc3fSopenharmony_ci    void CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback);
49b1b8bc3fSopenharmony_ci    void DeleteAsyncWork();
50b1b8bc3fSopenharmony_ci    void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv);
51b1b8bc3fSopenharmony_ci    void SetNeedPromise(bool needPromise);
52b1b8bc3fSopenharmony_ci    void SetNeedThrowException(bool needThrowException);
53b1b8bc3fSopenharmony_ci    napi_status SetCallback(napi_value callback);
54b1b8bc3fSopenharmony_ci    napi_value CreatePromise();
55b1b8bc3fSopenharmony_ci    [[nodiscard]] bool IsParseOK() const;
56b1b8bc3fSopenharmony_ci    [[nodiscard]] bool IsExecOK() const;
57b1b8bc3fSopenharmony_ci    [[nodiscard]] napi_env GetEnv() const;
58b1b8bc3fSopenharmony_ci    [[nodiscard]] int32_t GetErrorCode() const;
59b1b8bc3fSopenharmony_ci    [[nodiscard]] const std::string &GetErrorMessage() const;
60b1b8bc3fSopenharmony_ci    [[nodiscard]] napi_value GetCallback() const;
61b1b8bc3fSopenharmony_ci    [[nodiscard]] napi_deferred GetDeferred() const;
62b1b8bc3fSopenharmony_ci    [[nodiscard]] const std::string &GetAsyncWorkName() const;
63b1b8bc3fSopenharmony_ci    [[nodiscard]] EventManager *GetManager() const;
64b1b8bc3fSopenharmony_ci    [[nodiscard]] bool IsNeedPromise() const;
65b1b8bc3fSopenharmony_ci    [[nodiscard]] bool IsNeedThrowException() const;
66b1b8bc3fSopenharmony_ci
67b1b8bc3fSopenharmony_ci    napi_deferred deferredBack1_ = nullptr;
68b1b8bc3fSopenharmony_ci    napi_deferred deferredBack2_ = nullptr;
69b1b8bc3fSopenharmony_ci    napi_deferred deferredBack3_ = nullptr;
70b1b8bc3fSopenharmony_ci    napi_deferred deferredBack4_ = nullptr;
71b1b8bc3fSopenharmony_ci
72b1b8bc3fSopenharmony_ci    uint32_t magic_ = BASE_CONTEXT_MAGIC_NUMBER;
73b1b8bc3fSopenharmony_ci
74b1b8bc3fSopenharmony_ciprotected:
75b1b8bc3fSopenharmony_ci    EventManager *manager_ = nullptr;
76b1b8bc3fSopenharmony_ci
77b1b8bc3fSopenharmony_ciprivate:
78b1b8bc3fSopenharmony_ci    napi_env env_ = nullptr;
79b1b8bc3fSopenharmony_ci    bool parseOK_;
80b1b8bc3fSopenharmony_ci    bool requestOK_;
81b1b8bc3fSopenharmony_ci    int32_t errorCode_;
82b1b8bc3fSopenharmony_ci    std::string errorMessage_;
83b1b8bc3fSopenharmony_ci    napi_ref callback_ = nullptr;
84b1b8bc3fSopenharmony_ci    napi_async_work asyncWork_ = nullptr;
85b1b8bc3fSopenharmony_ci    napi_deferred deferred_ = nullptr;
86b1b8bc3fSopenharmony_ci    std::string asyncWorkName_;
87b1b8bc3fSopenharmony_ci    bool needPromise_;
88b1b8bc3fSopenharmony_ci    bool needThrowException_;
89b1b8bc3fSopenharmony_ci    NetBaseErrorCodeConvertor convertor_;
90b1b8bc3fSopenharmony_ci};
91b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard
92b1b8bc3fSopenharmony_ci} // namespace OHOS
93b1b8bc3fSopenharmony_ci
94b1b8bc3fSopenharmony_ci#endif // COMMUNICATIONNETMANAGER_BASE_BASE_CONTEXT_H
95