• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 NAPI_ASYNC_CALLBACK_H
17 #define NAPI_ASYNC_CALLBACK_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include "napi/native_api.h"
24 #include "napi_async_work.h"
25 
26 namespace OHOS {
27 namespace Bluetooth {
28 class NapiCallback;
29 class NapiPromise;
30 class NapiNativeObject;
31 
32 struct NapiAsyncCallback {
33     enum class Type {
34         CALLBACK = 1,
35         PROMISE,
36     };
37 
38     void CallFunction(int errCode, const std::shared_ptr<NapiNativeObject> &object);
39     napi_value GetRet(void);
40 
41     Type type;
42     napi_env env;
43     std::shared_ptr<NapiCallback> callback = nullptr;
44     std::shared_ptr<NapiPromise> promise = nullptr;
45 };
46 
47 class NapiCallback {
48 public:
49     // napi_value 'callback' shall be type of napi_founction, check it use NapiIsFunction().
50     NapiCallback(napi_env env, napi_value callback);
51     ~NapiCallback();
52 
53     void CallFunction(const std::shared_ptr<NapiNativeObject> &object);
54     void CallFunction(int errCode, const std::shared_ptr<NapiNativeObject> &object);
55     napi_env GetNapiEnv(void);
56     bool Equal(napi_env env, napi_value &callback) const;
57     std::string ToLogString(void) const;
SetNapiEnvValidity(bool isValid)58     void SetNapiEnvValidity(bool isValid)
59     {
60         isValid_ = isValid;
61     }
IsValidNapiEnv(void)62     bool IsValidNapiEnv(void) const
63     {
64         return isValid_;
65     }
66 
67 private:
68     NapiCallback(const NapiCallback &) = delete;
69     NapiCallback &operator=(const NapiCallback &) = delete;
70     NapiCallback(NapiCallback &&) = delete;
71     NapiCallback &operator=(NapiCallback &&) noexcept = delete;
72 
73     napi_env env_;
74     napi_ref callbackRef_;
75     int id_;
76     /*************************** env_cleanup_hook ********************************/
77     bool isValid_ = true;
78     /*************************** env_cleanup_hook ********************************/
79 };
80 
81 class NapiPromise {
82 public:
83     explicit NapiPromise(napi_env env);
84     ~NapiPromise();
85 
86     void ResolveOrReject(int errCode, const std::shared_ptr<NapiNativeObject> &object);
87     void Resolve(napi_value resolution);
88     void Reject(napi_value rejection);
89     napi_value GetPromise(void) const;
SetNapiEnvValidity(bool isValid)90     void SetNapiEnvValidity(bool isValid)
91     {
92         isValid_ = isValid;
93     }
IsValidNapiEnv(void)94     bool IsValidNapiEnv(void) const
95     {
96         return isValid_;
97     }
98 
99 private:
100     napi_env env_;
101     napi_value promise_;
102     napi_deferred deferred_;
103     bool isResolvedOrRejected_ = false;
104     bool isValid_ = true;
105 };
106 
107 class NapiHandleScope {
108 public:
109     explicit NapiHandleScope(napi_env env);
110     ~NapiHandleScope();
111 
112 private:
113     napi_env env_;
114     napi_handle_scope scope_;
115 };
116 
117 }  // namespace Bluetooth
118 }  // namespace OHOS
119 #endif  // NAPI_ASYNC_CALLBACK_H