• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SCREENLOK_ASYNC_CALL_H
16 #define SCREENLOK_ASYNC_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 #include "screenlock_system_ability_interface.h"
25 
26 namespace OHOS::ScreenLock {
27 class AsyncCall final {
28 public:
29     class Context {
30     public:
31         using InputAction = std::function<napi_status(napi_env, size_t, napi_value[], napi_value)>;
32         using OutputAction = std::function<napi_status(napi_env, napi_value *)>;
33         using ExecAction = std::function<void(Context *)>;
Context(InputAction input,OutputAction output)34         Context(InputAction input, OutputAction output) : input_(std::move(input)), output_(std::move(output)) {};
~Context()35         virtual ~Context() {};
36         void SetAction(const InputAction input, const OutputAction output = nullptr)
37         {
38             input_ = input;
39             output_ = output;
40         }
SetAction(OutputAction output)41         void SetAction(OutputAction output)
42         {
43             SetAction(nullptr, std::move(output));
44         }
SetErrorInfo(const ErrorInfo & errorInfo)45         void SetErrorInfo(const ErrorInfo &errorInfo)
46         {
47             errorInfo_ = errorInfo;
48         }
operator()49         napi_status operator()(const napi_env env, size_t argc, napi_value argv[], napi_value self)
50         {
51             if (input_ == nullptr) {
52                 return napi_ok;
53             }
54             if (self == nullptr) {
55                 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
56             }
57             return input_(env, argc, argv, self);
58         }
operator()59         napi_status operator()(const napi_env env, napi_value *result)
60         {
61             if (output_ == nullptr) {
62                 *result = nullptr;
63                 return napi_ok;
64             }
65             if (status_ != napi_ok) {
66                 return status_;
67             }
68             return output_(env, result);
69         }
Exec()70         virtual void Exec()
71         {
72             if (exec_ == nullptr) {
73                 return;
74             }
75             exec_(this);
76         };
SetStatus(napi_status status)77         void SetStatus(napi_status status)
78         {
79             status_ = status;
80         }
81 
82     protected:
83         friend class AsyncCall;
84         InputAction input_ = nullptr;
85         OutputAction output_ = nullptr;
86         ExecAction exec_ = nullptr;
87         ErrorInfo errorInfo_;
88         napi_status status_ = napi_generic_failure;
89     };
90 
91     // The default AsyncCallback in the parameters is at the end position.
92     static constexpr size_t ASYNC_DEFAULT_POS = -1;
93     AsyncCall(napi_env env, napi_callback_info info, Context *context, size_t pos = ASYNC_DEFAULT_POS);
94     ~AsyncCall();
95     napi_value Call(const napi_env env, Context::ExecAction exec = nullptr);
96     napi_value SyncCall(const napi_env env, Context::ExecAction exec = nullptr);
97     static void GenerateBusinessError(napi_env env, const ErrorInfo &errorInfo, napi_value *result);
98 
99 private:
100     enum class ARG_INFO { ARG_ERROR, ARG_DATA, ARG_BUTT };
101     static void OnExecute(const napi_env env, void *data);
102     static void OnComplete(const napi_env env, napi_status status, void *data);
103     struct AsyncContext {
104         ~AsyncContext();
105         Context *ctx = nullptr;
106         napi_env env = nullptr;
107         napi_ref callback = nullptr;
108         napi_ref self = nullptr;
109         napi_deferred defer = nullptr;
110         napi_async_work work = nullptr;
111     };
112     AsyncContext *context_ = nullptr;
113 };
114 } // namespace OHOS::ScreenLock
115 
116 #endif // SCREENLOK_ASYNC_CALL_H
117