• 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 <pthread.h>
22 #include "curl/curl.h"
23 #include "curl/easy.h"
24 #include "upload_common.h"
25 #include "i_header_receive_callback.h"
26 #include "i_progress_callback.h"
27 #include "i_fail_callback.h"
28 #include "i_upload_task.h"
29 #include "upload_config.h"
30 #include "curl_adp.h"
31 #include "obtain_file.h"
32 #include "upload_hilog_wrapper.h"
33 
34 #include "context.h"
35 #include "ability_context.h"
36 #include "data_ability_helper.h"
37 
38 namespace OHOS::Request::Upload {
39 enum UploadTaskState {
40     STATE_INIT,
41     STATE_RUNNING,
42     STATE_SUCCESS,
43     STATE_FAILURE,
44 };
45 
46 enum UploadErrorCode {
47     UPLOAD_OK = 0,
48     UPLOAD_ERRORCODE_UNSUPPORT_URI,
49     UPLOAD_ERRORCODE_GET_FILE_ERROR,
50     UPLOAD_ERRORCODE_CONFIG_ERROR,
51     UPLOAD_ERRORCODE_UPLOAD_LIB_ERROR,
52     UPLOAD_ERRORCODE_UPLOAD_FAIL,
53     UPLOAD_ERRORCODE_UPLOAD_OUTTIME,
54 };
55 
56 class UploadTask : public IUploadTask {
57 public:
58     UPLOAD_API UploadTask(std::shared_ptr<UploadConfig>& uploadConfig);
59     UPLOAD_API virtual ~UploadTask();
60     UPLOAD_API virtual bool Remove();
61     UPLOAD_API virtual void On(Type type, void *callback);
62     UPLOAD_API virtual void Off(Type type, void *callback);
63     UPLOAD_API void ExecuteTask();
64     static void Run(void *arg);
65     virtual void OnRun();
66 
67     UPLOAD_API virtual void SetCallback(Type type, void *callback);
68     UPLOAD_API virtual void SetContext(std::shared_ptr<OHOS::AbilityRuntime::Context> context);
69     virtual void OnProgress(curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
70     virtual void OnHeaderReceive(char *buffer, size_t size, size_t nitems);
71     virtual void OnFail(unsigned int error);
72     std::vector<std::string> StringSplit(const std::string& str, char delim);
73 
74 protected:
75     std::vector<FileData>& GetFileArray();
76     void ClearFileArray();
77 private:
78     std::shared_ptr<UploadConfig> uploadConfig_;
79     std::unique_ptr<std::thread> thread_;
80 
81     IProgressCallback* progressCallback_;
82     IHeaderReceiveCallback* headerReceiveCallback_;
83     IFailCallback* failCallback_;
84     std::shared_ptr<CUrlAdp> curlAdp_;
85     std::shared_ptr<ObtainFile> obtainFile_;
86     std::shared_ptr<OHOS::AbilityRuntime::Context> context_;
87     int64_t uploadedSize_;
88     int64_t totalSize_;
89     unsigned int error_;
90     std::vector<std::string> headerArray_;
91     std::string header_;
92     std::vector<FileData> fileArray_;
93     UploadTaskState state_;
94     std::mutex mutex_;
95     std::thread::native_handle_type thread_handle_;
96 };
97 } // end of OHOS::Request::Upload
98 #endif