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