• 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 #ifndef ASYNC_CALL_H
16 #define ASYNC_CALL_H
17 
18 #include <functional>
19 #include <memory>
20 #include <string>
21 #include "constant.h"
22 #include "napi_utils.h"
23 #include "js_common.h"
24 #include "napi/native_api.h"
25 
26 namespace OHOS::Request {
27 class AsyncCall final {
28 public:
29     class Context {
30     public:
31         using InputAction = std::function<napi_status(size_t, napi_value *, napi_value)>;
32         using OutputAction = std::function<napi_status(napi_value *)>;
33         using ExecAction = std::function<void()>;
34         Context() = default;
~Context()35         virtual ~Context()
36         {
37             napi_delete_async_work(env_, work_);
38             napi_delete_reference(env_, self_);
39             if (callbackRef_ != nullptr) {
40                 napi_delete_reference(env_, callbackRef_);
41             }
42         };
SetInput(InputAction action)43         inline Context &SetInput(InputAction action)
44         {
45             input_ = std::move(action);
46             return *this;
47         }
SetOutput(OutputAction action)48         inline Context &SetOutput(OutputAction action)
49         {
50             output_ = std::move(action);
51             return *this;
52         }
SetExec(ExecAction action)53         inline Context &SetExec(ExecAction action)
54         {
55             exec_ = std::move(action);
56             return *this;
57         }
Creator()58         inline napi_value Creator()
59         {
60             ExceptionError error;
61             NapiUtils::ConvertError(innerCode_, error);
62             return NapiUtils::CreateBusinessError(env_, error.code, error.errInfo, withErrCode_);
63         }
64 
65         InputAction input_ = nullptr;
66         OutputAction output_ = nullptr;
67         ExecAction exec_ = nullptr;
68 
69         napi_env env_;
70         napi_ref callbackRef_ = nullptr;
71         napi_ref self_ = nullptr;
72         napi_deferred defer_ = nullptr;
73         napi_async_work work_ = nullptr;
74 
75         int32_t innerCode_;
76         bool withErrCode_;
77         Version version_;
78     };
79 
80     AsyncCall(napi_env env, napi_callback_info info, const std::shared_ptr<Context> &context);
81     ~AsyncCall();
82     napi_value Call(const std::shared_ptr<Context> &context, const std::string &resourceName = "AsyncCall");
83 
SetQosLevel(napi_qos_t napiQosLevel)84     inline void SetQosLevel(napi_qos_t napiQosLevel)
85     {
86         napiQosLevel_ = napiQosLevel;
87     }
88 
89 private:
90     enum {
91         ARG_ERROR,
92         ARG_DATA,
93         ARG_BUTT
94     };
95 
96     struct WorkData {
97         std::shared_ptr<Context> ctx = nullptr;
~WorkDataWorkData98         ~WorkData()
99         {
100             ctx = nullptr;
101         }
102     };
103     static void OnExecute(napi_env env, void *data);
104     static void OnComplete(napi_env env, napi_status status, void *data);
105     napi_qos_t napiQosLevel_ = napi_qos_default;
106 };
107 } // namespace OHOS::Request
108 #endif // ASYNC_CALL_H
109