• 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 #[derive(Debug)]
25 pub(crate) struct TaskInfo {
26     pub(crate) bundle: String,
27     pub(crate) url: String,
28     pub(crate) data: String,
29     pub(crate) token: String,
30     pub(crate) form_items: Vec<FormItem>,
31     pub(crate) file_specs: Vec<FileSpec>,
32     pub(crate) title: String,
33     pub(crate) description: String,
34     pub(crate) mime_type: String,
35     pub(crate) progress: Progress,
36     pub(crate) extras: HashMap<String, String>,
37     pub(crate) common_data: CommonTaskInfo,
38 }
39 
40 impl TaskInfo {
uid(&self) -> u6441     pub(crate) fn uid(&self) -> u64 {
42         self.common_data.uid
43     }
44 
mime_type(&self) -> String45     pub(crate) fn mime_type(&self) -> String {
46         self.mime_type.clone()
47     }
48 
action(&self) -> Action49     pub(crate) fn action(&self) -> Action {
50         Action::from(self.common_data.action)
51     }
52 
token(&self) -> String53     pub(crate) fn token(&self) -> String {
54         self.token.clone()
55     }
56 }
57 
58 #[repr(C)]
59 #[derive(Copy, Clone, Debug)]
60 pub(crate) struct CommonTaskInfo {
61     pub(crate) task_id: u32,
62     pub(crate) uid: u64,
63     pub(crate) action: u8,
64     pub(crate) mode: u8,
65     pub(crate) ctime: u64,
66     pub(crate) mtime: u64,
67     pub(crate) reason: u8,
68     pub(crate) gauge: bool,
69     pub(crate) retry: bool,
70     pub(crate) tries: u32,
71     pub(crate) version: u8,
72     pub(crate) priority: u32,
73 }
74 
75 pub(crate) struct InfoSet {
76     pub(crate) form_items: Vec<CFormItem>,
77     pub(crate) file_specs: Vec<CFileSpec>,
78     pub(crate) sizes: String,
79     pub(crate) processed: String,
80     pub(crate) extras: String,
81 }
82 
83 #[cxx::bridge(namespace = "OHOS::Request")]
84 mod ffi {
85     #[derive(Clone, Copy, PartialEq, Debug)]
86     #[repr(u8)]
87     /// Task state
88     pub enum State {
89         /// Initialized
90         Initialized = 0x00,
91         /// Waiting
92         Waiting = 0x10,
93         /// Running
94         Running = 0x20,
95         /// Retrying
96         Retrying = 0x21,
97         /// Paused
98         Paused = 0x30,
99         /// Stopped
100         Stopped = 0x31,
101         /// Completed
102         Completed = 0x40,
103         /// Failed
104         Failed = 0x41,
105         /// Removed
106         Removed = 0x50,
107         /// Any
108         Any = 0x61,
109     }
110 }
111 
112 #[derive(Debug)]
113 pub(crate) struct UpdateInfo {
114     pub(crate) mtime: u64,
115     pub(crate) reason: u8,
116     pub(crate) tries: u32,
117     pub(crate) mime_type: String,
118     pub(crate) progress: Progress,
119 }
120 
121 impl From<u8> for State {
from(value: u8) -> Self122     fn from(value: u8) -> Self {
123         match value {
124             0 => State::Initialized,
125             16 => State::Waiting,
126             32 => State::Running,
127             33 => State::Retrying,
128             48 => State::Paused,
129             49 => State::Stopped,
130             64 => State::Completed,
131             65 => State::Failed,
132             80 => State::Removed,
133             _ => State::Any,
134         }
135     }
136 }
137 
138 impl TaskInfo {
build_info_set(&self) -> InfoSet139     pub(crate) fn build_info_set(&self) -> InfoSet {
140         InfoSet {
141             form_items: self.form_items.iter().map(|x| x.to_c_struct()).collect(),
142             file_specs: self.file_specs.iter().map(|x| x.to_c_struct()).collect(),
143             sizes: format!("{:?}", self.progress.sizes),
144             processed: format!("{:?}", self.progress.processed),
145             extras: hashmap_to_string(&self.extras),
146         }
147     }
148 
build_each_file_status(&self) -> Vec<EachFileStatus>149     pub(crate) fn build_each_file_status(&self) -> Vec<EachFileStatus> {
150         EachFileStatus::create_each_file_status(
151             &self.file_specs,
152             self.progress.common_data.index,
153             self.common_data.reason.into(),
154         )
155     }
156 
build_notify_data(&self) -> NotifyData157     pub(crate) fn build_notify_data(&self) -> NotifyData {
158         NotifyData {
159             bundle: self.bundle.clone(),
160             progress: self.progress.clone(),
161             action: Action::from(self.common_data.action),
162             version: Version::from(self.common_data.version),
163             each_file_status: self.build_each_file_status(),
164             task_id: self.common_data.task_id,
165             uid: self.common_data.uid,
166         }
167     }
168 }
169 
170 #[derive(Debug)]
171 pub(crate) struct DumpAllInfo {
172     pub(crate) vec: Vec<DumpAllEachInfo>,
173 }
174 
175 #[derive(Debug)]
176 pub(crate) struct DumpAllEachInfo {
177     pub(crate) task_id: u32,
178     pub(crate) action: Action,
179     pub(crate) state: State,
180     pub(crate) reason: Reason,
181 }
182 
183 #[derive(Debug)]
184 pub(crate) struct DumpOneInfo {
185     pub(crate) task_id: u32,
186     pub(crate) action: Action,
187     pub(crate) state: State,
188     pub(crate) reason: Reason,
189 }
190 
191 #[cfg(test)]
192 mod test {
193     use super::*;
194 
195     #[test]
ut_enum_state()196     fn ut_enum_state() {
197         assert_eq!(State::Initialized.repr, 0);
198         assert_eq!(State::Waiting.repr, 16);
199         assert_eq!(State::Running.repr, 32);
200         assert_eq!(State::Retrying.repr, 33);
201         assert_eq!(State::Paused.repr, 48);
202         assert_eq!(State::Stopped.repr, 49);
203         assert_eq!(State::Completed.repr, 64);
204         assert_eq!(State::Failed.repr, 65);
205         assert_eq!(State::Removed.repr, 80);
206         assert_eq!(State::Any.repr, 97);
207     }
208 }
209