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