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 }; 86 87 enum class SubscribeType : uint32_t { 88 COMPLETED = 0, 89 FAILED, 90 HEADER_RECEIVE, 91 PAUSE, 92 PROGRESS, 93 REMOVE, 94 RESUME, 95 RESPONSE, 96 BUTT, 97 }; 98 99 struct UploadResponse { 100 int32_t code; 101 std::string data; 102 std::string headers; 103 }; 104 105 struct FormItem { 106 std::string name; 107 std::string value; 108 }; 109 110 struct FileSpec { 111 std::string name; 112 std::string uri; 113 std::string filename; 114 std::string type; 115 int32_t fd = -1; 116 bool isUserFile = false; 117 bool hasContentType = false; 118 }; 119 120 struct Notification { 121 std::optional<std::string> title = std::nullopt; 122 std::optional<std::string> text = std::nullopt; 123 }; 124 125 struct Config { 126 Action action; 127 std::string url; 128 std::vector<std::string> certsPath; 129 Version version; 130 std::string bundleName; 131 uint32_t bundleType = 0; 132 Mode mode = Mode::BACKGROUND; 133 Network network = Network::ANY; 134 uint32_t index = 0; 135 int64_t begins = 0; 136 int64_t ends = -1; 137 uint32_t priority = 0; 138 bool overwrite = false; 139 bool metered = false; 140 bool roaming = false; 141 bool retry = true; 142 bool redirect = true; 143 bool gauge = false; 144 bool precise = false; 145 bool background = false; 146 bool withErrCode = true; 147 bool firstInit = true; 148 bool multipart = false; 149 std::string title; 150 std::string saveas; 151 std::string method; 152 std::string token = "null"; 153 std::string description; 154 std::string data; 155 std::string proxy; 156 std::string certificatePins; 157 std::map<std::string, std::string> headers; 158 std::vector<FormItem> forms; 159 std::vector<FileSpec> files; 160 std::vector<std::string> bodyFileNames; 161 std::map<std::string, std::string> extras; 162 Notification notification; 163 }; 164 165 enum class State : uint32_t { 166 INITIALIZED = 0x00, 167 WAITING = 0x10, 168 RUNNING = 0x20, 169 RETRYING = 0x21, 170 PAUSED = 0x30, 171 STOPPED = 0x31, 172 COMPLETED = 0x40, 173 FAILED = 0x41, 174 REMOVED = 0x50, 175 DEFAULT = 0x60, 176 ANY = 0x61, 177 }; 178 179 struct Progress { 180 State state; 181 uint32_t index; 182 uint64_t processed; 183 uint64_t totalProcessed; 184 std::vector<int64_t> sizes; 185 std::map<std::string, std::string> extras; 186 std::vector<uint8_t> bodyBytes; 187 }; 188 189 enum class Faults : uint32_t { 190 OTHERS = 0xFF, 191 DISCONNECTED = 0x00, 192 TIMEOUT = 0x10, 193 PROTOCOL = 0x20, 194 PARAM = 0x30, 195 FSIO = 0x40, 196 DNS = 0X50, 197 TCP = 0X60, 198 SSL = 0X70, 199 REDIRECT = 0x80, 200 }; 201 202 struct TaskState { 203 std::string path; 204 uint32_t responseCode{ REASON_OK }; 205 std::string message; 206 }; 207 208 struct NotifyData { 209 SubscribeType type; 210 uint32_t taskId; 211 Progress progress; 212 Action action; 213 Version version; 214 Mode mode; 215 std::vector<TaskState> taskStates; 216 }; 217 218 enum class EventType : uint32_t { 219 DATA_CALLBACK = 0, 220 HEADER_CALLBACK, 221 TASK_STATE_CALLBACK, 222 PROGRESS_CALLBACK, 223 BUTT, 224 }; 225 226 struct Notify { 227 EventType type = EventType::BUTT; 228 std::vector<int64_t> data; 229 std::vector<TaskState> taskStates; 230 Progress progress; 231 }; 232 233 struct TaskInfo { 234 Version version; 235 std::string uid; 236 std::string bundle; 237 std::string url; 238 std::string data; 239 std::vector<FileSpec> files; 240 std::vector<FormItem> forms; 241 std::string tid; 242 std::string title; 243 std::string description; 244 Action action; 245 Mode mode; 246 std::string mimeType; 247 Progress progress; 248 bool gauge; 249 uint64_t ctime; 250 uint64_t mtime; 251 bool retry; 252 uint32_t tries; 253 Faults faults; 254 Reason code; 255 std::string reason; 256 bool withSystem = false; 257 uint32_t priority; 258 std::map<std::string, std::string> extras; 259 std::vector<TaskState> taskStates; 260 }; 261 262 struct TaskInfoRet { 263 ExceptionErrorCode code; 264 TaskInfo info; 265 }; 266 267 struct TaskIdAndToken { 268 std::string tid; 269 std::string token; 270 }; 271 272 struct SpeedConfig { 273 std::string tid; 274 int64_t maxSpeed; 275 }; 276 277 struct Filter { 278 std::string bundle; 279 int64_t before; 280 int64_t after; 281 State state = State::ANY; 282 Action action = Action::ANY; 283 Mode mode = Mode::ANY; 284 }; 285 286 enum DownloadErrorCode { 287 ERROR_CANNOT_RESUME, 288 ERROR_DEVICE_NOT_FOUND, 289 ERROR_FILE_ALREADY_EXISTS, 290 ERROR_FILE_ERROR, 291 ERROR_HTTP_DATA_ERROR, 292 ERROR_INSUFFICIENT_SPACE, 293 ERROR_TOO_MANY_REDIRECTS, 294 ERROR_UNHANDLED_HTTP_CODE, 295 ERROR_UNKNOWN, 296 ERROR_OFFLINE, 297 ERROR_UNSUPPORTED_NETWORK_TYPE, 298 }; 299 300 enum DownloadStatus { 301 SESSION_SUCCESS, 302 SESSION_RUNNING, 303 SESSION_PENDING, 304 SESSION_PAUSED, 305 SESSION_FAILED, 306 SESSION_UNKNOWN, 307 }; 308 309 enum RemoveTaskChecker { 310 DoNothing, 311 ClearFile, 312 ClearFileAndRemoveTask, 313 }; 314 315 struct DownloadInfo { 316 uint32_t downloadId; 317 DownloadErrorCode failedReason; 318 std::string fileName; 319 std::string filePath; 320 PausedReason pausedReason; 321 DownloadStatus status; 322 std::string url; 323 std::string downloadTitle; 324 int64_t downloadTotalBytes; 325 std::string description; 326 int64_t downloadedBytes; 327 }; 328 329 struct Response { 330 std::string taskId; 331 std::string version; 332 int32_t statusCode; 333 std::string reason; 334 std::map<std::string, std::vector<std::string>> headers; 335 }; 336 337 struct TaskRet { 338 ExceptionErrorCode code; 339 std::string tid; 340 }; 341 342 } // namespace OHOS::Request 343 #endif //REQUEST_COMMON_H