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