• 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;
58 private:
59     NapiCallback(const NapiCallback &) = delete;
60     NapiCallback &operator=(const NapiCallback &) = delete;
61     NapiCallback(NapiCallback &&) = delete;
62     NapiCallback &operator=(NapiCallback &&) noexcept = delete;
63 
64     napi_env env_;
65     napi_ref callbackRef_;
66     int id_;
67 };
68 
69 class NapiPromise {
70 public:
71     explicit NapiPromise(napi_env env);
72     ~NapiPromise() = default;
73 
74     void ResolveOrReject(int errCode, const std::shared_ptr<NapiNativeObject> &object);
75     void Resolve(napi_value resolution);
76     void Reject(napi_value rejection);
77     napi_value GetPromise(void) const;
78 private:
79     napi_env env_;
80     napi_value promise_;
81     napi_deferred deferred_;
82     bool isResolvedOrRejected_ = false;
83 };
84 
85 class NapiHandleScope {
86 public:
87     explicit NapiHandleScope(napi_env env);
88     ~NapiHandleScope();
89 
90 private:
91     napi_env env_;
92     napi_handle_scope scope_;
93 };
94 
95 }  // namespace Bluetooth
96 }  // namespace OHOS
97 #endif  // NAPI_ASYNC_CALLBACK_H