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