1190978c3Sopenharmony_ci/*
2190978c3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3190978c3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4190978c3Sopenharmony_ci * you may not use this file except in compliance with the License.
5190978c3Sopenharmony_ci * You may obtain a copy of the License at
6190978c3Sopenharmony_ci *
7190978c3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8190978c3Sopenharmony_ci *
9190978c3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10190978c3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11190978c3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12190978c3Sopenharmony_ci * See the License for the specific language governing permissions and
13190978c3Sopenharmony_ci * limitations under the License.
14190978c3Sopenharmony_ci */
15190978c3Sopenharmony_ci
16190978c3Sopenharmony_ci#ifndef CLIENT_CONTEXT_H
17190978c3Sopenharmony_ci#define CLIENT_CONTEXT_H
18190978c3Sopenharmony_ci
19190978c3Sopenharmony_ci#include <utility>
20190978c3Sopenharmony_ci
21190978c3Sopenharmony_ci#include "napi/native_api.h"
22190978c3Sopenharmony_ci#include "napi/native_node_api.h"
23190978c3Sopenharmony_ci
24190978c3Sopenharmony_ci#include "common_error_define.h"
25190978c3Sopenharmony_ci
26190978c3Sopenharmony_cinamespace OHOS::UpdateEngine {
27190978c3Sopenharmony_citemplate <typename T>
28190978c3Sopenharmony_cistruct ClientContext {
29190978c3Sopenharmony_ci    typedef napi_value (*GetNapiParam)(napi_env env, napi_callback_info info, std::unique_ptr<T> &clientContext);
30190978c3Sopenharmony_ci    typedef void (*GetIpcBusinessError)(const std::string &funcName, int32_t ipcRequestCode,
31190978c3Sopenharmony_ci        BusinessError &businessError);
32190978c3Sopenharmony_ci    typedef napi_value (*CreateNapiValue)(napi_env env, const T &context);
33190978c3Sopenharmony_ci
34190978c3Sopenharmony_ci    ClientContext(std::string method, GetNapiParam getNapiParam, napi_async_execute_callback executeFunc,
35190978c3Sopenharmony_ci        std::vector<std::pair<std::string, std::string>> paramInfos, GetIpcBusinessError getIpcBusinessError)
36190978c3Sopenharmony_ci        : method_(std::move(method)),
37190978c3Sopenharmony_ci          getNapiParam_(getNapiParam),
38190978c3Sopenharmony_ci          executeFunc_(executeFunc),
39190978c3Sopenharmony_ci          paramInfos_(std::move(paramInfos)),
40190978c3Sopenharmony_ci          getIpcBusinessError_(getIpcBusinessError)
41190978c3Sopenharmony_ci    {
42190978c3Sopenharmony_ci        ENGINE_LOGD("ClientContext construct");
43190978c3Sopenharmony_ci    }
44190978c3Sopenharmony_ci
45190978c3Sopenharmony_ci    ~ClientContext()
46190978c3Sopenharmony_ci    {
47190978c3Sopenharmony_ci        ENGINE_LOGD("~ClientContext destruct");
48190978c3Sopenharmony_ci    }
49190978c3Sopenharmony_ci
50190978c3Sopenharmony_ci    std::string method_; // 执行的接口名
51190978c3Sopenharmony_ci    BusinessError businessError_;
52190978c3Sopenharmony_ci    int32_t ipcRequestCode_ = 0;
53190978c3Sopenharmony_ci
54190978c3Sopenharmony_ci    GetNapiParam getNapiParam_ = nullptr; // napi获取参数
55190978c3Sopenharmony_ci    CreateNapiValue createValueFunc_ = nullptr; // 通过ipc返回结果, 构建napi结果对象函数
56190978c3Sopenharmony_ci
57190978c3Sopenharmony_ci    napi_async_execute_callback executeFunc_; // 异步执行函数
58190978c3Sopenharmony_ci    napi_ref callbackRef_ = nullptr; // callback 回调
59190978c3Sopenharmony_ci    napi_deferred deferred_ = nullptr; // promise deferred对象
60190978c3Sopenharmony_ci    napi_async_work work_ = nullptr;
61190978c3Sopenharmony_ci
62190978c3Sopenharmony_ci    std::vector<std::pair<std::string, std::string>> paramInfos_; // 入参校验异常返回结果
63190978c3Sopenharmony_ci    GetIpcBusinessError getIpcBusinessError_; // 通过ipc返回异常,构造BusinessError
64190978c3Sopenharmony_ci};
65190978c3Sopenharmony_ci} // namespace OHOS::UpdateEngine
66190978c3Sopenharmony_ci#endif // CLIENT_CONTEXT_H