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