• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use std::collections::HashMap;
15 
16 pub use ffi::State;
17 
18 use super::notify::{EachFileStatus, NotifyData, Progress};
19 use crate::task::config::{Action, Version};
20 use crate::task::reason::Reason;
21 use crate::utils::c_wrapper::{CFileSpec, CFormItem};
22 use crate::utils::form_item::{FileSpec, FormItem};
23 use crate::utils::hashmap_to_string;
24 
25 #[derive(Debug, Clone)]
26 pub(crate) struct TaskInfo {
27     pub(crate) bundle: String,
28     pub(crate) url: String,
29     pub(crate) data: String,
30     pub(crate) token: String,
31     pub(crate) form_items: Vec<FormItem>,
32     pub(crate) file_specs: Vec<FileSpec>,
33     pub(crate) title: String,
34     pub(crate) description: String,
35     pub(crate) mime_type: String,
36     pub(crate) progress: Progress,
37     pub(crate) extras: HashMap<String, String>,
38     pub(crate) common_data: CommonTaskInfo,
39     pub(crate) max_speed: i64,
40     pub(crate) task_time: u64,
41 }
42 
43 impl TaskInfo {
new() -> Self44     pub(crate) fn new() -> Self {
45         Self {
46             bundle: "".to_string(),
47             url: "".to_string(),
48             data: "".to_string(),
49             token: "".to_string(),
50             form_items: vec![],
51             file_specs: vec![],
52             title: "".to_string(),
53             description: "".to_string(),
54             mime_type: "".to_string(),
55             // Has at least one progress size.
56             progress: Progress::new(vec![0]),
57             extras: HashMap::new(),
58             common_data: CommonTaskInfo::new(),
59             max_speed: 0,
60             task_time: 0,
61         }
62     }
63 
uid(&self) -> u6464     pub(crate) fn uid(&self) -> u64 {
65         self.common_data.uid
66     }
67 
mime_type(&self) -> String68     pub(crate) fn mime_type(&self) -> String {
69         self.mime_type.clone()
70     }
71 
action(&self) -> Action72     pub(crate) fn action(&self) -> Action {
73         Action::from(self.common_data.action)
74     }
75 
token(&self) -> String76     pub(crate) fn token(&self) -> String {
77         self.token.clone()
78     }
79 }
80 
81 #[repr(C)]
82 #[derive(Copy, Clone, Debug)]
83 pub(crate) struct CommonTaskInfo {
84     pub(crate) task_id: u32,
85     pub(crate) uid: u64,
86     pub(crate) action: u8,
87     pub(crate) mode: u8,
88     pub(crate) ctime: u64,
89     pub(crate) mtime: u64,
90     pub(crate) reason: u8,
91     pub(crate) gauge: bool,
92     pub(crate) retry: bool,
93     pub(crate) tries: u32,
94     pub(crate) version: u8,
95     pub(crate) priority: u32,
96 }
97 
98 impl CommonTaskInfo {
new() -> Self99     pub(crate) fn new() -> Self {
100         Self {
101             task_id: 0,
102             uid: 0,
103             action: 0,
104             mode: 0,
105             ctime: 0,
106             mtime: 0,
107             reason: 0,
108             gauge: false,
109             retry: false,
110             tries: 0,
111             version: 0,
112             priority: 0,
113         }
114     }
115 }
116 
117 pub(crate) struct InfoSet {
118     pub(crate) form_items: Vec<CFormItem>,
119     pub(crate) file_specs: Vec<CFileSpec>,
120     pub(crate) sizes: String,
121     pub(crate) processed: String,
122     pub(crate) extras: String,
123 }
124 
125 #[cxx::bridge(namespace = "OHOS::Request")]
126 mod ffi {
127     #[derive(Clone, Copy, PartialEq, Debug)]
128     #[repr(u8)]
129     /// Task state
130     pub enum State {
131         /// Initialized
132         Initialized = 0x00,
133         /// Waiting
134         Waiting = 0x10,
135         /// Running
136         Running = 0x20,
137         /// Retrying
138         Retrying = 0x21,
139         /// Paused
140         Paused = 0x30,
141         /// Stopped
142         Stopped = 0x31,
143         /// Completed
144         Completed = 0x40,
145         /// Failed
146         Failed = 0x41,
147         /// Removed
148         Removed = 0x50,
149         /// Any
150         Any = 0x61,
151     }
152 }
153 
154 #[derive(Debug)]
155 pub(crate) struct UpdateInfo {
156     pub(crate) mtime: u64,
157     pub(crate) reason: u8,
158     pub(crate) tries: u32,
159     pub(crate) mime_type: String,
160     pub(crate) progress: Progress,
161 }
162 
163 impl From<u8> for State {
from(value: u8) -> Self164     fn from(value: u8) -> Self {
165         match value {
166             0 => State::Initialized,
167             16 => State::Waiting,
168             32 => State::Running,
169             33 => State::Retrying,
170             48 => State::Paused,
171             49 => State::Stopped,
172             64 => State::Completed,
173             65 => State::Failed,
174             80 => State::Removed,
175             _ => State::Any,
176         }
177     }
178 }
179 
180 impl TaskInfo {
build_info_set(&self) -> InfoSet181     pub(crate) fn build_info_set(&self) -> InfoSet {
182         InfoSet {
183             form_items: self.form_items.iter().map(|x| x.to_c_struct()).collect(),
184             file_specs: self.file_specs.iter().map(|x| x.to_c_struct()).collect(),
185             sizes: format!("{:?}", self.progress.sizes),
186             processed: format!("{:?}", self.progress.processed),
187             extras: hashmap_to_string(&self.extras),
188         }
189     }
190 
build_each_file_status(&self) -> Vec<EachFileStatus>191     pub(crate) fn build_each_file_status(&self) -> Vec<EachFileStatus> {
192         EachFileStatus::create_each_file_status(
193             &self.file_specs,
194             self.progress.common_data.index,
195             self.common_data.reason.into(),
196         )
197     }
198 
build_notify_data(&self) -> NotifyData199     pub(crate) fn build_notify_data(&self) -> NotifyData {
200         NotifyData {
201             bundle: self.bundle.clone(),
202             progress: self.progress.clone(),
203             action: Action::from(self.common_data.action),
204             version: Version::from(self.common_data.version),
205             each_file_status: self.build_each_file_status(),
206             task_id: self.common_data.task_id,
207             uid: self.common_data.uid,
208         }
209     }
210 }
211 
212 #[derive(Debug)]
213 pub(crate) struct DumpAllInfo {
214     pub(crate) vec: Vec<DumpAllEachInfo>,
215 }
216 
217 #[derive(Debug)]
218 pub(crate) struct DumpAllEachInfo {
219     pub(crate) task_id: u32,
220     pub(crate) action: Action,
221     pub(crate) state: State,
222     pub(crate) reason: Reason,
223 }
224 
225 #[derive(Debug)]
226 pub(crate) struct DumpOneInfo {
227     pub(crate) task_id: u32,
228     pub(crate) action: Action,
229     pub(crate) state: State,
230     pub(crate) reason: Reason,
231 }
232 
233 #[cfg(test)]
234 mod ut_info {
235     include!("../../tests/ut/task/ut_info.rs");
236 }
237