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 #ifndef WALLPAPER_CALL_H
16 #define WALLPAPER_CALL_H
17 
18 #include <functional>
19 #include <memory>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_common.h"
23 #include "napi/native_node_api.h"
24 
25 namespace OHOS::WallpaperNAPI {
26 class Call final {
27 public:
28     class Context {
29     public:
30         using InputAction = std::function<napi_status(napi_env, size_t, napi_value *, napi_value)>;
31         using OutputAction = std::function<napi_status(napi_env, napi_value *)>;
32         using ExecAction = std::function<void(Context *)>;
Context(InputAction input,OutputAction output)33         Context(InputAction input, OutputAction output) : input_(std::move(input)), output_(std::move(output)) {};
~Context()34         virtual ~Context() {};
35         void SetAction(InputAction input, OutputAction output = nullptr)
36         {
37             input_ = input;
38             output_ = output;
39         }
40 
SetExecution(ExecAction exec)41         void SetExecution(ExecAction exec)
42         {
43             exec_ = exec;
44         }
45 
SetErrInfo(int32_t errCode,std::string errMsg)46         void SetErrInfo(int32_t errCode, std::string errMsg)
47         {
48             errCode_ = errCode;
49             errMsg_ = errMsg;
50         }
51 
operator()52         virtual napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self)
53         {
54             if (input_ == nullptr) {
55                 return napi_ok;
56             }
57             auto ret = input_(env, argc, argv, self);
58             input_ = nullptr;
59             return ret;
60         }
61 
operator()62         virtual napi_status operator()(napi_env env, napi_value *result)
63         {
64             if (output_ == nullptr) {
65                 *result = nullptr;
66                 return napi_ok;
67             }
68             auto ret = output_(env, result);
69             output_ = nullptr;
70             return ret;
71         }
72 
Exec()73         virtual void Exec()
74         {
75             if (exec_ == nullptr) {
76                 return;
77             }
78             exec_(this);
79             exec_ = nullptr;
80         };
81 
82     protected:
83         friend class Call;
84         InputAction input_ = nullptr;
85         OutputAction output_ = nullptr;
86         ExecAction exec_ = nullptr;
87         int32_t errCode_ = 0;
88         std::string errMsg_;
89     };
90 
91     // The default AsyncCallback in the parameters is at the end position.
92     static constexpr size_t ASYNC_DEFAULT_POS = -1;
93     Call(napi_env env, napi_callback_info info, std::shared_ptr<Context> context, size_t pos = ASYNC_DEFAULT_POS,
94         bool needException = true);
95     ~Call();
96     napi_value AsyncCall(napi_env env, const std::string &resourceName = "AsyncCall");
97     napi_value SyncCall(napi_env env);
98 
99 private:
100     enum Arg : int32_t { ARG_ERROR, ARG_DATA, ARG_BUTT };
101     struct CallContext {
102         std::shared_ptr<Context> ctx = nullptr;
103         napi_ref callback = nullptr;
104         napi_ref self = nullptr;
105         napi_deferred defer = nullptr;
106         napi_async_work work = nullptr;
107     };
108     static void OnExecute(napi_env env, void *data);
109     static void OnComplete(napi_env env, napi_status status, void *data);
110     static void DeleteContext(napi_env env, CallContext *context);
111 
112     CallContext *context_ = nullptr;
113     napi_env env_ = nullptr;
114 };
115 } // namespace OHOS::WallpaperNAPI
116 
117 #endif // WALLPAPER_CALL_H
118