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 
16 #ifndef UPLOAD_TASK_NAPIV5_H
17 #define UPLOAD_TASK_NAPIV5_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "context.h"
23 #include "data_ability_helper.h"
24 #include "napi/native_api.h"
25 #include "napi/native_common.h"
26 #include "upload/upload_task.h"
27 #include "upload_config.h"
28 
29 namespace OHOS::Request::Upload {
30 class UploadTaskNapiV5 : public std::enable_shared_from_this<UploadTaskNapiV5> {
31 public:
32     struct SystemFailCallback {
33         std::string data;
34         int32_t code;
35         napi_env env;
36         napi_ref ref;
37     };
38 
39     struct SystemSuccessCallback {
40         napi_env env;
41         napi_ref ref;
42         Upload::UploadResponse response;
43     };
44 
45     struct SystemCompleteCallback {
46         std::shared_ptr<Upload::UploadTaskNapiV5> proxy;
47     };
48 
49     struct RecycleRef {
50         napi_env env;
51         napi_ref successRef;
52         napi_ref failRef;
53         napi_ref completeRef;
54     };
55 
UploadTaskNapiV5(napi_env env)56     UploadTaskNapiV5(napi_env env) : env_(env){};
57     ~UploadTaskNapiV5();
58     static void OnSystemSuccess(napi_env env, napi_ref ref, Upload::UploadResponse &response);
59     static void OnSystemFail(napi_env env, napi_ref ref, std::string &response, int32_t &code);
60     static void OnSystemComplete(std::shared_ptr<Upload::UploadTaskNapiV5> proxy);
61     napi_value JsUpload(napi_env env, napi_callback_info info);
62     bool ParseCallback(napi_env env, napi_callback_info info);
63     void AddCallbackToConfig(napi_env env, std::shared_ptr<UploadConfig> &config);
SetEnv(napi_env env)64     inline void SetEnv(napi_env env)
65     {
66         env_ = env;
67     }
68 
69 private:
70     std::shared_ptr<Upload::UploadTask> uploadTask_ = nullptr;
71     napi_ref success_ = nullptr;
72     napi_ref fail_ = nullptr;
73     napi_ref complete_ = nullptr;
74     napi_env env_ = nullptr;
75 };
76 } // namespace OHOS::Request::Upload
77 
78 #endif // UPLOAD_TASK_NAPIV5_H