• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_
17 #define UPLOAD_TASK_
18 
19 #include <cstdio>
20 #include <thread>
21 #include <vector>
22 #include <pthread.h>
23 #include "curl/curl.h"
24 #include "curl/easy.h"
25 #include "upload_common.h"
26 #include "i_header_receive_callback.h"
27 #include "i_progress_callback.h"
28 #include "i_notify_callback.h"
29 #include "i_upload_task.h"
30 #include "upload_config.h"
31 #include "curl_adp.h"
32 #include "obtain_file.h"
33 #include "upload_hilog_wrapper.h"
34 
35 #include "context.h"
36 #include "ability_context.h"
37 #include "data_ability_helper.h"
38 
39 namespace OHOS::Request::Upload {
40 enum UploadTaskState {
41     STATE_INIT,
42     STATE_RUNNING,
43     STATE_SUCCESS,
44     STATE_FAILURE,
45 };
46 
47 class UploadTaskNapiV5;
48 class UploadTask : public IUploadTask, public std::enable_shared_from_this<UploadTask> {
49 public:
50     UPLOAD_API UploadTask(std::shared_ptr<UploadConfig>& uploadConfig);
51     UPLOAD_API ~UploadTask();
52     UPLOAD_API bool Remove();
53     UPLOAD_API void On(Type type, void *callback);
54     UPLOAD_API void Off(Type type, void *callback);
55     UPLOAD_API void ExecuteTask();
56     static void Run(std::shared_ptr<Upload::UploadTask> task);
57     void OnRun();
58 
59     UPLOAD_API void SetCallback(Type type, void *callback);
60     UPLOAD_API void SetContext(std::shared_ptr<OHOS::AbilityRuntime::Context> context);
61     UPLOAD_API void SetUploadProxy(std::shared_ptr<UploadTaskNapiV5> proxy);
62     void OnProgress(curl_off_t ulnow) override;
63     void OnHeaderReceive(const std::string &header) override;
64     void OnFail() override;
65     void OnComplete() override;
66 
67 protected:
68     uint32_t InitFileArray();
69     void ClearFileArray();
70     std::string GetCodeMessage(uint32_t code);
71     std::vector<TaskState> GetTaskStates();
72 private:
73     void ReportTaskFault(uint32_t ret) const;
74     uint32_t StartUploadFile();
75 
76     std::shared_ptr<UploadConfig> uploadConfig_;
77     std::unique_ptr<std::thread> thread_;
78     static constexpr const char *REQUEST_TASK_FAULT = "REQUEST_TASK_FAULT";
79     static constexpr const char *TASKS_TYPE = "TASKS_TYPE";
80     static constexpr const char *UPLOAD = "UPLOAD";
81     static constexpr const char *TOTAL_FILE_NUM = "TOTAL_FILE_NUM";
82     static constexpr const char *FAIL_FILE_NUM = "FAIL_FILE_NUM";
83     static constexpr const char *SUCCESS_FILE_NUM = "SUCCESS_FILE_NUM";
84     static constexpr const char *ERROR_INFO = "ERROR_INFO";
85 
86     IProgressCallback* progressCallback_;
87     IHeaderReceiveCallback* headerReceiveCallback_;
88     INotifyCallback* failCallback_;
89     INotifyCallback* completeCallback_;
90 
91     std::shared_ptr<CUrlAdp> curlAdp_;
92     std::shared_ptr<UploadTaskNapiV5> uploadProxy_;
93     std::shared_ptr<OHOS::AbilityRuntime::Context> context_;
94     int64_t uploadedSize_;
95     int64_t totalSize_;
96     std::vector<std::string> headerArray_;
97     std::string header_;
98     std::vector<FileData> fileDatas_;
99     std::vector<TaskState> taskStates_;
100     UploadTaskState state_;
101     std::mutex mutex_;
102     bool isRemoved_ {false};
103     std::mutex removeMutex_;
104     std::thread::native_handle_type thread_handle_;
105     static constexpr int USLEEP_INTERVEL_BEFOR_RUN = 50 * 1000;
106 };
107 } // end of OHOS::Request::Upload
108 #endif