• 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 REQUEST_COMMON_H
17 #define REQUEST_COMMON_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <optional>
22 #include <string>
23 #include <vector>
24 
25 #include "constant.h"
26 
27 namespace OHOS::Request {
28 
29 enum class Action : uint32_t {
30     DOWNLOAD = 0,
31     UPLOAD,
32     ANY,
33 };
34 
35 enum class Mode : uint32_t {
36     BACKGROUND = 0,
37     FOREGROUND,
38     ANY,
39 };
40 
41 enum class Network : uint32_t {
42     ANY = 0,
43     WIFI,
44     CELLULAR,
45 };
46 
47 enum class Version : uint32_t {
48     API8 = 0,
49     API9,
50     API10,
51 };
52 
53 enum Reason : uint32_t {
54     REASON_OK = 0,
55     TASK_SURVIVAL_ONE_MONTH,
56     WAITTING_NETWORK_ONE_DAY,
57     STOPPED_NEW_FRONT_TASK,
58     RUNNING_TASK_MEET_LIMITS,
59     USER_OPERATION,
60     APP_BACKGROUND_OR_TERMINATE,
61     NETWORK_OFFLINE,
62     UNSUPPORTED_NETWORK_TYPE,
63     BUILD_CLIENT_FAILED,
64     BUILD_REQUEST_FAILED,
65     GET_FILESIZE_FAILED,
66     CONTINUOUS_TASK_TIMEOUT,
67     CONNECT_ERROR,
68     REQUEST_ERROR,
69     UPLOAD_FILE_ERROR,
70     REDIRECT_ERROR,
71     PROTOCOL_ERROR,
72     IO_ERROR,
73     UNSUPPORT_RANGE_REQUEST,
74     OTHERS_ERROR,
75     ACCOUNT_STOPPED,
76     NETWORK_CHANGED,
77     DNS,
78     TCP,
79     SSL,
80     INSUFFICIENT_SPACE,
81     NETWORK_APP,
82     NETWORK_ACCOUNT,
83     APP_ACCOUNT,
84     NETWORK_APP_ACCOUNT,
85     LOW_SPEED,
86 };
87 
88 enum WaitingReason : uint32_t {
89     TaskQueueFull = 0x00,
90     NetworkNotMatch = 0x01,
91     AppBackground = 0x02,
92     UserInactivated = 0x03,
93 };
94 
95 enum class SubscribeType : uint32_t {
96     COMPLETED = 0,
97     FAILED,
98     HEADER_RECEIVE,
99     PAUSE,
100     PROGRESS,
101     REMOVE,
102     RESUME,
103     RESPONSE,
104     FAULT_OCCUR,
105     WAIT,
106     BUTT,
107 };
108 
109 struct UploadResponse {
110     int32_t code;
111     std::string data;
112     std::string headers;
113 };
114 
115 struct FormItem {
116     std::string name;
117     std::string value;
118 };
119 
120 struct FileSpec {
121     std::string name;
122     std::string uri;
123     std::string filename;
124     std::string type;
125     int32_t fd = -1;
126     bool isUserFile = false;
127     bool hasContentType = false;
128 };
129 
130 struct Notification {
131     std::optional<std::string> title = std::nullopt;
132     std::optional<std::string> text = std::nullopt;
133     bool disable = false;
134 };
135 
136 struct MinSpeed {
137     int64_t speed = 0;
138     int64_t duration = 0;
139 };
140 
141 struct Timeout {
142     uint64_t connectionTimeout = 0;
143     uint64_t totalTimeout = 0;
144 };
145 
146 struct Config {
147     Action action;
148     std::string url;
149     std::vector<std::string> certsPath;
150     Version version;
151     std::string bundleName;
152     uint32_t bundleType = 0;
153     Mode mode = Mode::BACKGROUND;
154     Network network = Network::ANY;
155     uint32_t index = 0;
156     int64_t begins = 0;
157     int64_t ends = -1;
158     uint32_t priority = 0;
159     bool overwrite = false;
160     bool metered = false;
161     bool roaming = false;
162     bool retry = true;
163     bool redirect = true;
164     bool gauge = false;
165     bool precise = false;
166     bool background = false;
167     bool withErrCode = true;
168     bool firstInit = true;
169     bool multipart = false;
170     std::string title;
171     std::string saveas;
172     std::string method;
173     std::string token = "null";
174     std::string description;
175     std::string data;
176     std::string proxy;
177     std::string certificatePins;
178     std::map<std::string, std::string> headers;
179     std::vector<FormItem> forms;
180     std::vector<FileSpec> files;
181     std::vector<std::string> bodyFileNames;
182     std::map<std::string, std::string> extras;
183     Notification notification;
184     MinSpeed minSpeed;
185     Timeout timeout;
186 };
187 
188 enum class State : uint32_t {
189     INITIALIZED = 0x00,
190     WAITING = 0x10,
191     RUNNING = 0x20,
192     RETRYING = 0x21,
193     PAUSED = 0x30,
194     STOPPED = 0x31,
195     COMPLETED = 0x40,
196     FAILED = 0x41,
197     REMOVED = 0x50,
198     DEFAULT = 0x60,
199     ANY = 0x61,
200 };
201 
202 struct Progress {
203     State state;
204     uint32_t index;
205     uint64_t processed;
206     uint64_t totalProcessed;
207     std::vector<int64_t> sizes;
208     std::map<std::string, std::string> extras;
209     std::vector<uint8_t> bodyBytes;
210 };
211 
212 enum class Faults : uint32_t {
213     OTHERS = 0xFF,
214     DISCONNECTED = 0x00,
215     TIMEOUT = 0x10,
216     PROTOCOL = 0x20,
217     PARAM = 0x30,
218     FSIO = 0x40,
219     DNS = 0X50,
220     TCP = 0X60,
221     SSL = 0X70,
222     REDIRECT = 0x80,
223     LOW_SPEED = 0x90,
224 };
225 
226 struct TaskState {
227     std::string path;
228     uint32_t responseCode{ REASON_OK };
229     std::string message;
230 };
231 
232 struct NotifyData {
233     SubscribeType type;
234     uint32_t taskId;
235     Progress progress;
236     Action action;
237     Version version;
238     Mode mode;
239     std::vector<TaskState> taskStates;
240 };
241 
242 enum class EventType : uint32_t {
243     DATA_CALLBACK = 0,
244     HEADER_CALLBACK,
245     TASK_STATE_CALLBACK,
246     PROGRESS_CALLBACK,
247     BUTT,
248 };
249 
250 struct Notify {
251     EventType type = EventType::BUTT;
252     std::vector<int64_t> data;
253     std::vector<TaskState> taskStates;
254     Progress progress;
255 };
256 
257 struct TaskInfo {
258     Version version;
259     std::string uid;
260     std::string bundle;
261     std::string url;
262     std::string data;
263     std::vector<FileSpec> files;
264     std::vector<FormItem> forms;
265     std::string tid;
266     std::string title;
267     std::string description;
268     Action action;
269     Mode mode;
270     std::string mimeType;
271     Progress progress;
272     bool gauge;
273     uint64_t ctime;
274     uint64_t mtime;
275     bool retry;
276     uint32_t tries;
277     Faults faults;
278     Reason code;
279     std::string reason;
280     bool withSystem = false;
281     uint32_t priority;
282     uint64_t taskTime = 0;
283     std::map<std::string, std::string> extras;
284     std::vector<TaskState> taskStates;
285 };
286 
287 struct TaskInfoRet {
288     ExceptionErrorCode code;
289     TaskInfo info;
290 };
291 
292 struct TaskIdAndToken {
293     std::string tid;
294     std::string token;
295 };
296 
297 struct SpeedConfig {
298     std::string tid;
299     int64_t maxSpeed;
300 };
301 
302 struct Filter {
303     std::string bundle;
304     int64_t before;
305     int64_t after;
306     State state = State::ANY;
307     Action action = Action::ANY;
308     Mode mode = Mode::ANY;
309 };
310 
311 enum DownloadErrorCode {
312     ERROR_CANNOT_RESUME,
313     ERROR_DEVICE_NOT_FOUND,
314     ERROR_FILE_ALREADY_EXISTS,
315     ERROR_FILE_ERROR,
316     ERROR_HTTP_DATA_ERROR,
317     ERROR_INSUFFICIENT_SPACE,
318     ERROR_TOO_MANY_REDIRECTS,
319     ERROR_UNHANDLED_HTTP_CODE,
320     ERROR_UNKNOWN,
321     ERROR_OFFLINE,
322     ERROR_UNSUPPORTED_NETWORK_TYPE,
323 };
324 
325 enum DownloadStatus {
326     SESSION_SUCCESS,
327     SESSION_RUNNING,
328     SESSION_PENDING,
329     SESSION_PAUSED,
330     SESSION_FAILED,
331     SESSION_UNKNOWN,
332 };
333 
334 enum RemoveTaskChecker {
335     DoNothing,
336     ClearFile,
337     ClearFileAndRemoveTask,
338 };
339 
340 struct DownloadInfo {
341     uint32_t downloadId;
342     DownloadErrorCode failedReason;
343     std::string fileName;
344     std::string filePath;
345     PausedReason pausedReason;
346     DownloadStatus status;
347     std::string url;
348     std::string downloadTitle;
349     int64_t downloadTotalBytes;
350     std::string description;
351     int64_t downloadedBytes;
352 };
353 
354 struct Response {
355     std::string taskId;
356     std::string version;
357     int32_t statusCode;
358     std::string reason;
359     std::map<std::string, std::vector<std::string>> headers;
360 };
361 
362 struct TaskRet {
363     ExceptionErrorCode code;
364     std::string tid;
365 };
366 
367 static uint64_t REQUEST_FDSAN_TAG = 0xC01C50;
368 
369 } // namespace OHOS::Request
370 #endif //REQUEST_COMMON_H