1 /*
2  * Copyright (c) 2024 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 CLIENT_CONTEXT_H
17 #define CLIENT_CONTEXT_H
18 
19 #include <utility>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 
24 #include "common_error_define.h"
25 
26 namespace OHOS::UpdateEngine {
27 template <typename T>
28 struct ClientContext {
29     typedef napi_value (*GetNapiParam)(napi_env env, napi_callback_info info, std::unique_ptr<T> &clientContext);
30     typedef void (*GetIpcBusinessError)(const std::string &funcName, int32_t ipcRequestCode,
31         BusinessError &businessError);
32     typedef napi_value (*CreateNapiValue)(napi_env env, const T &context);
33 
ClientContextClientContext34     ClientContext(std::string method, GetNapiParam getNapiParam, napi_async_execute_callback executeFunc,
35         std::vector<std::pair<std::string, std::string>> paramInfos, GetIpcBusinessError getIpcBusinessError)
36         : method_(std::move(method)),
37           getNapiParam_(getNapiParam),
38           executeFunc_(executeFunc),
39           paramInfos_(std::move(paramInfos)),
40           getIpcBusinessError_(getIpcBusinessError)
41     {
42         ENGINE_LOGD("ClientContext construct");
43     }
44 
~ClientContextClientContext45     ~ClientContext()
46     {
47         ENGINE_LOGD("~ClientContext destruct");
48     }
49 
50     std::string method_; // 执行的接口名
51     BusinessError businessError_;
52     int32_t ipcRequestCode_ = 0;
53 
54     GetNapiParam getNapiParam_ = nullptr; // napi获取参数
55     CreateNapiValue createValueFunc_ = nullptr; // 通过ipc返回结果, 构建napi结果对象函数
56 
57     napi_async_execute_callback executeFunc_; // 异步执行函数
58     napi_ref callbackRef_ = nullptr; // callback 回调
59     napi_deferred deferred_ = nullptr; // promise deferred对象
60     napi_async_work work_ = nullptr;
61 
62     std::vector<std::pair<std::string, std::string>> paramInfos_; // 入参校验异常返回结果
63     GetIpcBusinessError getIpcBusinessError_; // 通过ipc返回异常,构造BusinessError
64 };
65 } // namespace OHOS::UpdateEngine
66 #endif // CLIENT_CONTEXT_H