1 /*
2  * Copyright (C) 2023-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  * Description: supply napi callback for interfaces.
15  * Author: zhangjingnan
16  * Create: 2023-4-11
17  */
18 
19 #ifndef NAPI_CALLBACK_H
20 #define NAPI_CALLBACK_H
21 
22 #include <functional>
23 #include "napi/native_api.h"
24 #include "napi/native_common.h"
25 #include "napi/native_node_api.h"
26 #include "uv.h"
27 
28 namespace OHOS {
29 namespace CastEngine {
30 namespace CastEngineClient {
31 class NapiCallback final {
32 public:
33     using NapiArgsGetter = std::function<void(napi_env env, int &argc, napi_value *argv)>;
34 
35     explicit NapiCallback(napi_env env);
36     ~NapiCallback();
37 
38     napi_env GetEnv() const;
39 
40     void Call(napi_ref method, NapiArgsGetter getter);
41 
42 private:
43     static void AfterWorkCallback(uv_work_t *work, int status);
44 
45     struct DataContext {
46         napi_env env;
47         napi_ref method;
48         NapiArgsGetter getter;
49     };
50     napi_env env_ = nullptr;
51     uv_loop_s *loop_ = nullptr;
52 
53     static constexpr size_t ARGC_MAX = 6;
54 };
55 } // namespace CastEngineClient
56 } // namespace CastEngine
57 } // namespace OHOS
58 #endif