• 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 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 };
75 
76 struct UploadResponse {
77     int32_t code;
78     std::string data;
79     std::string headers;
80 };
81 
82 struct FormItem {
83     std::string name;
84     std::string value;
85 };
86 
87 struct FileSpec {
88     std::string name;
89     std::string uri;
90     std::string filename;
91     std::string type;
92     int32_t fd = -1;
93 };
94 
95 struct Config {
96     Action action;
97     std::string url;
98     std::vector<std::string> certsPath;
99     Version version;
100     std::string bundleName;
101     Mode mode = Mode::BACKGROUND;
102     Network network = Network::ANY;
103     uint32_t index = 0;
104     int64_t begins = 0;
105     int64_t ends = -1;
106     uint32_t priority = 0;
107     bool overwrite = false;
108     bool metered = false;
109     bool roaming = false;
110     bool retry = true;
111     bool redirect = true;
112     bool gauge = false;
113     bool precise = false;
114     bool background = false;
115     bool withErrCode = true;
116     bool firstInit = true;
117     std::string title;
118     std::string saveas;
119     std::string method;
120     std::string token = "null";
121     std::string description;
122     std::string data;
123     std::map<std::string, std::string> headers;
124     std::vector<FormItem> forms;
125     std::vector<FileSpec> files;
126     std::vector<int32_t> bodyFds;
127     std::vector<std::string> bodyFileNames;
128     std::map<std::string, std::string> extras;
129 };
130 
131 enum class State : uint32_t {
132     INITIALIZED = 0x00,
133     WAITING = 0x10,
134     RUNNING = 0x20,
135     RETRYING = 0x21,
136     PAUSED = 0x30,
137     STOPPED = 0x31,
138     COMPLETED = 0x40,
139     FAILED = 0x41,
140     REMOVED = 0x50,
141     DEFAULT = 0x60,
142     ANY = 0x61,
143 };
144 
145 struct Progress {
146     State state;
147     uint32_t index;
148     uint64_t processed;
149     uint64_t totalProcessed;
150     std::vector<int64_t> sizes;
151     std::map<std::string, std::string> extras;
152     std::vector<uint8_t> bodyBytes;
153 };
154 
155 enum class Faults : uint32_t {
156     OTHERS = 0xFF,
157     DISCONNECTED = 0x00,
158     TIMEOUT = 0x10,
159     PROTOCOL = 0x20,
160     FSIO = 0x40,
161 };
162 
163 struct TaskState {
164     std::string path;
165     uint32_t responseCode{ REASON_OK };
166     std::string message;
167 };
168 
169 struct NotifyData {
170     Progress progress;
171     Action action;
172     Version version;
173     Mode mode;
174     std::vector<TaskState> taskStates;
175 };
176 
177 enum class EventType : uint32_t {
178     DATA_CALLBACK = 0,
179     HEADER_CALLBACK,
180     TASK_STATE_CALLBACK,
181     PROGRESS_CALLBACK,
182     BUTT,
183 };
184 
185 struct Notify {
186     EventType type = EventType::BUTT;
187     std::vector<int64_t> data;
188     std::vector<TaskState> taskStates;
189     Progress progress;
190 };
191 
192 struct TaskInfo {
193     Version version;
194     std::string uid;
195     std::string bundle;
196     std::string url;
197     std::string data;
198     std::vector<FileSpec> files;
199     std::vector<FormItem> forms;
200     std::string tid;
201     std::string title;
202     std::string description;
203     Action action;
204     Mode mode;
205     std::string mimeType;
206     Progress progress;
207     bool gauge;
208     uint64_t ctime;
209     uint64_t mtime;
210     bool retry;
211     uint32_t tries;
212     Faults faults;
213     Reason code;
214     std::string reason;
215     bool withSystem = false;
216     uint32_t priority;
217     std::map<std::string, std::string> extras;
218     std::vector<TaskState> taskStates;
219 };
220 
221 struct Filter {
222     std::string bundle;
223     int64_t before;
224     int64_t after;
225     State state = State::ANY;
226     Action action = Action::ANY;
227     Mode mode = Mode::ANY;
228 };
229 
230 enum DownloadErrorCode {
231     ERROR_CANNOT_RESUME,
232     ERROR_DEVICE_NOT_FOUND,
233     ERROR_FILE_ALREADY_EXISTS,
234     ERROR_FILE_ERROR,
235     ERROR_HTTP_DATA_ERROR,
236     ERROR_INSUFFICIENT_SPACE,
237     ERROR_TOO_MANY_REDIRECTS,
238     ERROR_UNHANDLED_HTTP_CODE,
239     ERROR_UNKNOWN,
240     ERROR_OFFLINE,
241     ERROR_UNSUPPORTED_NETWORK_TYPE,
242 };
243 
244 enum DownloadStatus {
245     SESSION_SUCCESS,
246     SESSION_RUNNING,
247     SESSION_PENDING,
248     SESSION_PAUSED,
249     SESSION_FAILED,
250     SESSION_UNKNOWN,
251 };
252 
253 struct DownloadInfo {
254     uint32_t downloadId;
255     DownloadErrorCode failedReason;
256     std::string fileName;
257     std::string filePath;
258     PausedReason pausedReason;
259     DownloadStatus status;
260     std::string url;
261     std::string downloadTitle;
262     int64_t downloadTotalBytes;
263     std::string description;
264     int64_t downloadedBytes;
265 };
266 } // namespace OHOS::Request
267 #endif //JS_COMMON_H